3f95c3150718d091968348c06261cc19fe0ec2b9
[ptask.git] / src / main.c
1 /*
2  * Copyright (C) 2010-2012 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <json/json.h>
24
25 #include <gtk/gtk.h>
26
27 struct task {
28         int id;
29         char *description;
30         char *status;
31         char *uuid;
32 };
33
34 static struct task **tasks;
35 static GtkTextView *w_description;
36 static GtkTreeView *w_treeview;
37
38 static char *task_exec(char *opts)
39 {
40         FILE *f;
41         int ret, s;
42         char *str, *tmp, *cmd, buf[1024];
43
44         str = NULL;
45
46         cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
47         strcpy(cmd, "task rc.json.array=on ");
48         strcat(cmd, opts);
49
50         printf("execute: %s\n", cmd);
51
52         f = popen(cmd, "r");
53
54         if (!f) {
55                 perror("popen");
56                 goto exit_free;
57         }
58
59         str = malloc(1);
60         str[0] = '\0';
61         while ((s = fread(buf, 1, 1024, f))) {
62                 tmp = malloc(strlen(str) + s + 1);
63                 memcpy(tmp, str, strlen(str));
64                 memcpy(tmp + strlen(str), buf, s);
65                 tmp[strlen(str) + s] = '\0';
66                 free(str);
67                 str = tmp;
68         }
69
70         ret = pclose(f);
71
72         if (ret == -1) {
73                 printf("pclose fails\n");
74                 perror("pclose");
75         }
76
77  exit_free:
78         free(cmd);
79
80         return str;
81 }
82
83 static struct json_object *task_exec_json(char *opts)
84 {
85         struct json_object *o;
86         char *str;
87
88         str = task_exec(opts);
89
90         if (str) {
91                 o = json_tokener_parse(str);
92                 free(str);
93                 return o;
94         }
95
96         return NULL;
97 }
98
99 static struct task **get_all_tasks()
100 {
101         int i, n;
102         struct json_object *jtasks, *jtask, *json;
103         struct task **tasks;
104
105         jtasks = task_exec_json("export");
106
107         if (!jtasks)
108                 return NULL;
109
110         n = json_object_array_length(jtasks);
111
112         tasks = malloc((n + 1) * sizeof(struct task *));
113
114         for (i = 0; i < n; i++) {
115                 jtask = json_object_array_get_idx(jtasks, i);
116
117                 tasks[i] = malloc(sizeof(struct task));
118
119                 json = json_object_object_get(jtask, "id");
120                 tasks[i]->id = json_object_get_int(json);
121
122                 json = json_object_object_get(jtask, "description");
123                 tasks[i]->description = strdup(json_object_get_string(json));
124
125                 json = json_object_object_get(jtask, "status");
126                 tasks[i]->status = strdup(json_object_get_string(json));
127
128                 json = json_object_object_get(jtask, "uuid");
129                 tasks[i]->uuid = strdup(json_object_get_string(json));
130         }
131
132         tasks[n] = NULL;
133
134         json_object_put(jtasks);
135
136         return tasks;
137 }
138
139 static struct task *get_selected_task(GtkTreeView *treeview)
140 {
141         GtkTreePath *path;
142         GtkTreeViewColumn *cols;
143         gint *i;
144         struct task *task;
145
146         gtk_tree_view_get_cursor(treeview, &path, &cols);
147
148         if (path) {
149                 i = gtk_tree_path_get_indices(path);
150                 
151                 if (i)
152                         printf("row selected: %d\n", *i);
153
154                 task = tasks[*i];
155
156                 gtk_tree_path_free(path);
157
158                 return task;
159         }
160
161         return NULL;
162 }
163
164 static char *escape(char *txt)
165 {
166         char *result;
167         char *c;
168
169         result = malloc(2*strlen(txt)+1);
170         c = result;
171
172         while(*txt) {
173                 switch(*txt) {
174                 case '"':
175                         *c = '\\'; c++;
176                         *c = '"';
177                         break;
178                 case '$':
179                         *c = '\\'; c++;
180                         *c = '$';
181                         break;
182                 case '&':
183                         *c = '\\'; c++;
184                         *c = '&';
185                         break;
186                 default:
187                         *c = *txt;
188                 }
189                 c++;
190                 txt++;
191         }
192
193         *c = '\0';
194
195         return result;
196 }
197
198 static int tasksave_clicked_cbk(GtkButton *btn, gpointer data)
199 {
200         struct task *task;
201         GtkTextBuffer *buf;
202         char *txt, *opts;
203         GtkTextIter sIter, eIter;
204
205         task = get_selected_task(GTK_TREE_VIEW(w_treeview));
206
207         printf("tasksave_clicked_cbk %d\n", task->id);  
208
209         buf = gtk_text_view_get_buffer(w_description);
210
211         gtk_text_buffer_get_iter_at_offset(buf, &sIter, 0);
212         gtk_text_buffer_get_iter_at_offset(buf, &eIter, -1);
213         txt = gtk_text_buffer_get_text(buf, &sIter, &eIter, TRUE);
214
215         txt = escape(txt);
216
217         printf("%s\n", txt);
218
219         opts = malloc(1
220                       + strlen(task->uuid)
221                       + strlen(" modify description:\"")
222                       + strlen(txt)
223                       + strlen("\"")
224                       + 1);
225         sprintf(opts, " %s modify \"%s\"", task->uuid, txt);
226
227         task_exec(opts);
228         
229         return FALSE;
230 }
231
232 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
233 {
234         struct task *task;
235         GtkTextBuffer *buf;
236
237         printf("cursor_changed_cbk\n");
238
239         task = get_selected_task(treeview);
240
241         if (task) {
242                 buf = gtk_text_view_get_buffer(w_description);
243                 gtk_text_buffer_set_text(buf,
244                                          task->description,
245                                          strlen(task->description));
246
247         }
248
249         return FALSE;
250 }
251
252 int main(int argc, char **argv)
253 {
254         GtkWidget *window;
255         GtkWidget *btn;
256         GtkBuilder *builder;
257         GtkTreeIter iter;
258         int i;
259         GtkTreeModel *model;
260         struct task **tasks_cur;
261
262         gtk_init(NULL, NULL);
263         builder = gtk_builder_new();
264         gtk_builder_add_from_file
265                 (builder,
266                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
267                  NULL);
268         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
269         printf("%p\n", window);
270
271         w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview"));
272
273         w_description = GTK_TEXT_VIEW(gtk_builder_get_object(builder,
274                                                              "taskdescription"));
275
276         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
277
278         tasks = get_all_tasks();
279
280         for (tasks_cur = tasks, i = 0; *tasks_cur; tasks_cur++, i++) {
281                 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
282                 gtk_list_store_set(GTK_LIST_STORE(model),
283                                    &iter,
284                                    0, (*tasks_cur)->id,
285                                    1, (*tasks_cur)->description,
286                                    -1);
287         }
288
289         g_signal_connect(w_treeview,
290                          "cursor-changed", (GCallback)cursor_changed_cbk, tasks);
291
292         btn = GTK_WIDGET(gtk_builder_get_object(builder, "tasksave"));
293         g_signal_connect(btn,
294                          "clicked", (GCallback)tasksave_clicked_cbk, tasks);
295
296         g_object_unref(G_OBJECT(builder));
297
298         gtk_widget_show_all(window);
299
300         gtk_main();
301
302         exit(EXIT_SUCCESS);
303 }