(no commit message)
[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         char *note;
33         char *project;
34 };
35
36 static struct task **tasks;
37 static GtkTextView *w_note;
38 static GtkEntry *w_description;
39 static GtkTreeView *w_treeview;
40
41 static char *task_exec(char *opts)
42 {
43         FILE *f;
44         int ret, s;
45         char *str, *tmp, *cmd, buf[1024];
46
47         str = NULL;
48
49         cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
50         strcpy(cmd, "task rc.json.array=on ");
51         strcat(cmd, opts);
52
53         printf("execute: %s\n", cmd);
54
55         f = popen(cmd, "r");
56
57         if (!f) {
58                 perror("popen");
59                 goto exit_free;
60         }
61
62         str = malloc(1);
63         str[0] = '\0';
64         while ((s = fread(buf, 1, 1024, f))) {
65                 tmp = malloc(strlen(str) + s + 1);
66                 memcpy(tmp, str, strlen(str));
67                 memcpy(tmp + strlen(str), buf, s);
68                 tmp[strlen(str) + s] = '\0';
69                 free(str);
70                 str = tmp;
71         }
72
73         ret = pclose(f);
74
75         if (ret == -1) {
76                 printf("pclose fails\n");
77                 perror("pclose");
78         }
79
80  exit_free:
81         free(cmd);
82
83         return str;
84 }
85
86 static struct json_object *task_exec_json(char *opts)
87 {
88         struct json_object *o;
89         char *str;
90
91         str = task_exec(opts);
92
93         if (str) {
94                 o = json_tokener_parse(str);
95                 free(str);
96                 return o;
97         }
98
99         return NULL;
100 }
101
102 static struct task **get_all_tasks()
103 {
104         int i, n;
105         struct json_object *jtasks, *jtask, *json;
106         struct task **tasks;
107
108         jtasks = task_exec_json("export");
109
110         if (!jtasks)
111                 return NULL;
112
113         n = json_object_array_length(jtasks);
114
115         tasks = malloc((n + 1) * sizeof(struct task *));
116
117         for (i = 0; i < n; i++) {
118                 jtask = json_object_array_get_idx(jtasks, i);
119
120                 tasks[i] = malloc(sizeof(struct task));
121
122                 json = json_object_object_get(jtask, "id");
123                 tasks[i]->id = json_object_get_int(json);
124
125                 json = json_object_object_get(jtask, "description");
126                 tasks[i]->description = strdup(json_object_get_string(json));
127
128                 json = json_object_object_get(jtask, "status");
129                 tasks[i]->status = strdup(json_object_get_string(json));
130
131                 json = json_object_object_get(jtask, "project");
132                 if (json)
133                         tasks[i]->project
134                                 = strdup(json_object_get_string(json));
135                 else
136                         tasks[i]->project = NULL;
137
138                 json = json_object_object_get(jtask, "uuid");
139                 tasks[i]->uuid = strdup(json_object_get_string(json));
140
141                 tasks[i]->note = NULL;
142         }
143
144         tasks[n] = NULL;
145
146         json_object_put(jtasks);
147
148         return tasks;
149 }
150
151 static struct task *get_selected_task(GtkTreeView *treeview)
152 {
153         GtkTreePath *path;
154         GtkTreeViewColumn *cols;
155         gint *i;
156         struct task *task;
157
158         gtk_tree_view_get_cursor(treeview, &path, &cols);
159
160         if (path) {
161                 i = gtk_tree_path_get_indices(path);
162                 
163                 if (i)
164                         printf("row selected: %d\n", *i);
165
166                 task = tasks[*i];
167
168                 gtk_tree_path_free(path);
169
170                 return task;
171         }
172
173         return NULL;
174 }
175
176 static char *escape(const char *txt)
177 {
178         char *result;
179         char *c;
180
181         result = malloc(2*strlen(txt)+1);
182         c = result;
183
184         while(*txt) {
185                 switch(*txt) {
186                 case '"':
187                         *c = '\\'; c++;
188                         *c = '"';
189                         break;
190                 case '$':
191                         *c = '\\'; c++;
192                         *c = '$';
193                         break;
194                 case '&':
195                         *c = '\\'; c++;
196                         *c = '&';
197                         break;
198                 default:
199                         *c = *txt;
200                 }
201                 c++;
202                 txt++;
203         }
204
205         *c = '\0';
206
207         return result;
208 }
209
210 static int tasksave_clicked_cbk(GtkButton *btn, gpointer data)
211 {
212         struct task *task;
213         GtkTextBuffer *buf;
214         char *txt, *opts;
215         GtkTextIter sIter, eIter;
216         const char *ctxt;
217
218         task = get_selected_task(GTK_TREE_VIEW(w_treeview));
219
220         printf("tasksave_clicked_cbk %d\n", task->id);  
221
222         if (task->note) {
223                 buf = gtk_text_view_get_buffer(w_note);
224
225                 gtk_text_buffer_get_iter_at_offset(buf, &sIter, 0);
226                 gtk_text_buffer_get_iter_at_offset(buf, &eIter, -1);
227                 txt = gtk_text_buffer_get_text(buf, &sIter, &eIter, TRUE);
228
229                 txt = escape(txt);
230
231                 printf("%s\n", txt);
232         }
233
234         ctxt = gtk_entry_get_text(w_description);
235         txt = escape(ctxt);
236
237         opts = malloc(1
238                       + strlen(task->uuid)
239                       + strlen(" modify description:\"")
240                       + strlen(txt)
241                       + strlen("\"")
242                       + 1);
243         sprintf(opts, " %s modify \"%s\"", task->uuid, txt);
244         
245         task_exec(opts);
246
247         free(txt);
248         
249         return FALSE;
250 }
251
252 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
253 {
254         struct task *task;
255         GtkTextBuffer *buf;
256
257         printf("cursor_changed_cbk\n");
258
259         task = get_selected_task(treeview);
260
261         if (task) {
262
263                 if (task->note) {
264                         buf = gtk_text_view_get_buffer(w_note);
265                         gtk_text_buffer_set_text(buf,
266                                                  task->note,
267                                                  strlen(task->note));
268                 }
269
270                 gtk_entry_set_text(w_description, task->description);
271         }
272
273         return FALSE;
274 }
275
276 int main(int argc, char **argv)
277 {
278         GtkWidget *window;
279         GtkWidget *btn;
280         GtkBuilder *builder;
281         GtkTreeIter iter;
282         int i;
283         GtkTreeModel *model;
284         struct task **tasks_cur;
285         struct task *task;
286
287         gtk_init(NULL, NULL);
288         builder = gtk_builder_new();
289         gtk_builder_add_from_file
290                 (builder,
291                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
292                  NULL);
293         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
294         printf("%p\n", window);
295
296         w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview"));
297
298         w_note = GTK_TEXT_VIEW(gtk_builder_get_object(builder, "tasknote"));
299
300         w_description = GTK_ENTRY(gtk_builder_get_object(builder,
301                                                          "taskdescription"));
302
303         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
304
305         tasks = get_all_tasks();
306
307         for (tasks_cur = tasks, i = 0; *tasks_cur; tasks_cur++, i++) {
308                 task = (*tasks_cur);
309
310                 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
311                 
312                 if (task->project)
313                         gtk_list_store_set(GTK_LIST_STORE(model),
314                                            &iter,
315                                            2, task->project,
316                                            -1);
317
318                 gtk_list_store_set(GTK_LIST_STORE(model),
319                                    &iter,
320                                    0, (*tasks_cur)->id,
321                                    1, (*tasks_cur)->description,
322                                    -1);
323         }
324
325         g_signal_connect(w_treeview,
326                          "cursor-changed", (GCallback)cursor_changed_cbk, tasks);
327
328         btn = GTK_WIDGET(gtk_builder_get_object(builder, "tasksave"));
329         g_signal_connect(btn,
330                          "clicked", (GCallback)tasksave_clicked_cbk, tasks);
331
332         g_object_unref(G_OBJECT(builder));
333
334         gtk_widget_show_all(window);
335
336         gtk_main();
337
338         exit(EXIT_SUCCESS);
339 }