(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 #include "tw.h"
28
29 static struct task **tasks;
30 static GtkTextView *w_note;
31 static GtkEntry *w_description;
32 static GtkEntry *w_project;
33 static GtkTreeView *w_treeview;
34 static GtkWidget *w_tasksave_btn;
35 static GtkComboBox *w_status;
36 static GtkComboBox *w_priority;
37
38 enum {
39         COL_ID,
40         COL_DESCRIPTION,
41         COL_PROJECT,
42         COL_UUID,
43         COL_PRIORITY
44 };
45
46 static struct task *get_selected_task(GtkTreeView *treeview)
47 {
48         GtkTreePath *path;
49         GtkTreeViewColumn *cols;
50         struct task **tasks_cur;
51         GtkTreeIter iter;
52         GtkTreeModel *model;
53         GValue value = {0,};
54         const char *uuid;
55
56         printf("get_selected_task\n");
57
58         gtk_tree_view_get_cursor(treeview, &path, &cols);
59
60         if (path) {
61                 model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
62                 gtk_tree_model_get_iter(model, &iter, path);
63                 gtk_tree_model_get_value(model, &iter, COL_UUID, &value);
64
65                 uuid = g_value_get_string(&value);
66
67                 for (tasks_cur = tasks; *tasks_cur; tasks_cur++)
68                         if (!strcmp((*tasks_cur)->uuid, uuid))
69                                 return *tasks_cur;
70
71                 gtk_tree_path_free(path);
72         }
73
74         return NULL;
75 }
76
77 static void clear_task_panel()
78 {
79         GtkTextBuffer *buf;
80
81         gtk_widget_set_sensitive(w_tasksave_btn, 0);
82
83         buf = gtk_text_view_get_buffer(w_note);
84         gtk_text_buffer_set_text(buf, "", 0);
85         gtk_widget_set_sensitive(GTK_WIDGET(w_note), 0);
86
87         gtk_entry_set_text(w_description, "");
88         gtk_widget_set_sensitive(GTK_WIDGET(w_description), 0);
89
90         gtk_entry_set_text(w_project, "");
91         gtk_widget_set_sensitive(GTK_WIDGET(w_project), 0);
92
93         gtk_combo_box_set_active(w_priority, 0);
94         gtk_widget_set_sensitive(GTK_WIDGET(w_priority), 0);
95 }
96
97 static void refresh()
98 {
99         GtkTreeModel *model;
100         struct task **tasks_cur;
101         struct task *task;
102         int i;
103         GtkTreeIter iter;
104         int status;
105         const char *project;
106
107         printf("refresh\n");
108         clear_task_panel();
109
110         status = gtk_combo_box_get_active(w_status);
111         printf("status: %d\n", status);
112
113         switch (status) {
114         case 0:
115                 tasks = tw_get_all_tasks("pending");
116                 break;
117         case 1:
118                 tasks = tw_get_all_tasks("completed");
119                 break;
120         default:
121                 tasks = tw_get_all_tasks("pending");
122         }
123
124         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
125         gtk_list_store_clear(GTK_LIST_STORE(model));
126         for (tasks_cur = tasks, i = 0; *tasks_cur; tasks_cur++, i++) {
127                 task = (*tasks_cur);
128
129                 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
130
131                 if (task->project)
132                         project = task->project;
133                 else
134                         project = "";
135
136                 gtk_list_store_set(GTK_LIST_STORE(model),
137                                    &iter,
138                                    COL_ID, (*tasks_cur)->id,
139                                    COL_DESCRIPTION, (*tasks_cur)->description,
140                                    COL_PROJECT, project,
141                                    COL_UUID, (*tasks_cur)->uuid,
142                                    COL_PRIORITY, (*tasks_cur)->priority,
143                                    -1);
144         }
145         printf("refresh done\n");
146 }
147
148 static int tasksave_clicked_cbk(GtkButton *btn, gpointer data)
149 {
150         struct task *task;
151         GtkTextBuffer *buf;
152         char *txt, *pri;
153         GtkTextIter sIter, eIter;
154         const char *ctxt;
155         int priority;
156
157         task = get_selected_task(GTK_TREE_VIEW(w_treeview));
158
159         printf("tasksave_clicked_cbk %d\n", task->id);
160
161         if (task->note) {
162                 buf = gtk_text_view_get_buffer(w_note);
163
164                 gtk_text_buffer_get_iter_at_offset(buf, &sIter, 0);
165                 gtk_text_buffer_get_iter_at_offset(buf, &eIter, -1);
166                 txt = gtk_text_buffer_get_text(buf, &sIter, &eIter, TRUE);
167
168                 txt = escape(txt);
169
170                 printf("%s\n", txt);
171         }
172
173         ctxt = gtk_entry_get_text(w_description);
174         if (!task->description || strcmp(ctxt, task->description))
175                 tw_modify_description(task->uuid, ctxt);
176
177         ctxt = gtk_entry_get_text(w_project);
178         if (!task->project || strcmp(ctxt, task->project))
179                 tw_modify_project(task->uuid, ctxt);
180
181         priority = gtk_combo_box_get_active(w_priority);
182         printf("priority: %d\n", priority);
183
184         switch (priority) {
185         case 3:
186                 pri = "H";
187                 break;
188         case 2:
189                 pri = "M";
190                 break;
191         case 1:
192                 pri = "L";
193                 break;
194         default:
195                 pri = "";
196         }
197
198         if (strcmp(task->priority, pri))
199                 tw_modify_priority(task->uuid, pri);
200
201         refresh();
202
203         return FALSE;
204 }
205
206 int refresh_clicked_cbk(GtkButton *btn, gpointer data)
207 {
208         printf("refresh_clicked_cbk\n");
209         refresh();
210
211         return FALSE;
212 }
213
214 int newtask_clicked_cbk(GtkButton *btn, gpointer data)
215 {
216         gint result;
217         static GtkDialog *diag;
218         GtkBuilder *builder;
219         GtkEntry *entry;
220         const char *ctxt;
221
222         printf("newtask_clicked_cbk\n");
223
224         builder = gtk_builder_new();
225         gtk_builder_add_from_file
226                 (builder,
227                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
228                  NULL);
229         diag = GTK_DIALOG(gtk_builder_get_object(builder, "diag_tasknew"));
230         gtk_builder_connect_signals(builder, NULL);
231
232         result = gtk_dialog_run(diag);
233
234         if (result == GTK_RESPONSE_ACCEPT) {
235                 printf("ok\n");
236                 entry = GTK_ENTRY(gtk_builder_get_object
237                                   (builder, "diag_tasknew_description"));
238                 ctxt = gtk_entry_get_text(entry);
239
240                 printf("%s\n", ctxt);
241
242                 tw_add(ctxt);
243                 refresh();
244         } else {
245                 printf("cancel\n");
246         }
247
248         g_object_unref(G_OBJECT(builder));
249
250         gtk_widget_destroy(GTK_WIDGET(diag));
251
252         return FALSE;
253 }
254
255 static int status_changed_cbk(GtkComboBox *w, gpointer data)
256 {
257         printf("status_changed_cbk\n");
258         refresh();
259
260         return FALSE;
261 }
262
263 static int priority_to_int(const char *str)
264 {
265         switch (*str) {
266         case 'H':
267                 return 3;
268         case 'M':
269                 return 2;
270         case 'L':
271                 return 1;
272         default:
273                 return 0;
274         }
275 }
276
277 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
278 {
279         struct task *task;
280         GtkTextBuffer *buf;
281         int priority;
282
283         printf("cursor_changed_cbk\n");
284
285         task = get_selected_task(treeview);
286
287         if (task) {
288
289                 buf = gtk_text_view_get_buffer(w_note);
290                 if (task->note)
291                         gtk_text_buffer_set_text(buf,
292                                                  task->note,
293                                                  strlen(task->note));
294                 else
295                         gtk_text_buffer_set_text(buf, "", 0);
296                 gtk_widget_set_sensitive(GTK_WIDGET(w_note), 1);
297
298                 gtk_entry_set_text(w_description, task->description);
299                 gtk_widget_set_sensitive(GTK_WIDGET(w_description), 1);
300
301                 if (task->project)
302                         gtk_entry_set_text(w_project, task->project);
303                 else
304                         gtk_entry_set_text(w_project, "");
305                 gtk_widget_set_sensitive(GTK_WIDGET(w_project), 1);
306
307                 gtk_widget_set_sensitive(w_tasksave_btn, 1);
308
309                 gtk_widget_set_sensitive(GTK_WIDGET(w_priority), 1);
310                 priority = priority_to_int(task->priority);
311                 gtk_combo_box_set_active(w_priority, priority);
312         } else {
313                 printf("clear task widgets\n");
314                 clear_task_panel();
315                 printf("clear task widgets done\n");
316         }
317
318         return FALSE;
319 }
320
321 static gint priority_cmp(GtkTreeModel *model,
322                          GtkTreeIter *a,
323                          GtkTreeIter *b,
324                          gpointer user_data)
325 {
326         GValue v1 = {0,}, v2 = {0,};
327         const char *str1, *str2;
328         int i1, i2;
329
330         gtk_tree_model_get_value(model, a, COL_PRIORITY, &v1);
331         str1 = g_value_get_string(&v1);
332         i1 = priority_to_int(str1);
333
334         gtk_tree_model_get_value(model, b, COL_PRIORITY, &v2);
335         str2 = g_value_get_string(&v2);
336         i2 = priority_to_int(str2);
337
338         if (i1 < i2)
339                 return -1;
340         else if (i1 > i2)
341                 return 1;
342         else
343                 return 0;
344 }
345
346 int main(int argc, char **argv)
347 {
348         GtkWidget *window;
349         GtkWidget *btn;
350         GtkBuilder *builder;
351         GtkTreeModel *model;
352
353         gtk_init(NULL, NULL);
354         builder = gtk_builder_new();
355         gtk_builder_add_from_file
356                 (builder,
357                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
358                  NULL);
359         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
360
361         w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview"));
362         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
363         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(model),
364                                         COL_PRIORITY,
365                                         priority_cmp,
366                                         NULL,
367                                         NULL);
368
369         w_note = GTK_TEXT_VIEW(gtk_builder_get_object(builder, "tasknote"));
370
371         w_description = GTK_ENTRY(gtk_builder_get_object(builder,
372                                                          "taskdescription"));
373         w_project = GTK_ENTRY(gtk_builder_get_object(builder, "taskproject"));
374         w_status = GTK_COMBO_BOX(gtk_builder_get_object(builder, "status"));
375         w_priority = GTK_COMBO_BOX(gtk_builder_get_object(builder,
376                                                           "taskpriority"));
377
378         refresh();
379
380         gtk_builder_connect_signals(builder, NULL);
381
382         g_signal_connect(w_treeview,
383                          "cursor-changed", (GCallback)cursor_changed_cbk,
384                          tasks);
385         g_signal_connect(w_status,
386                          "changed", (GCallback)status_changed_cbk,
387                          tasks);
388         btn = GTK_WIDGET(gtk_builder_get_object(builder, "tasksave"));
389         g_signal_connect(btn,
390                          "clicked", (GCallback)tasksave_clicked_cbk, tasks);
391         gtk_widget_set_sensitive(btn, 0);
392         w_tasksave_btn = btn;
393
394         /*btn = GTK_WIDGET(gtk_builder_get_object(builder, "refresh"));
395         g_signal_connect(btn,
396         "clicked", (GCallback)refresh_clicked_cbk, tasks);*/
397
398         g_object_unref(G_OBJECT(builder));
399
400         gtk_widget_show_all(window);
401
402         gtk_main();
403
404         exit(EXIT_SUCCESS);
405 }