(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 GtkTreeView *w_treeview;
33 static GtkWidget *w_tasksave_btn;
34
35 static struct task *get_selected_task(GtkTreeView *treeview)
36 {
37         GtkTreePath *path;
38         GtkTreeViewColumn *cols;
39         gint *i;
40         struct task *task;
41
42         printf("get_selected_task\n");
43
44         gtk_tree_view_get_cursor(treeview, &path, &cols);
45
46         if (path) {
47                 i = gtk_tree_path_get_indices(path);
48
49                 if (i) {
50                         printf("row selected: %d\n", *i);
51
52                         task = tasks[*i];
53                 } else {
54                         task = NULL;
55                 }
56
57                 gtk_tree_path_free(path);
58         } else {
59                 task = NULL;
60         }
61
62         return task;
63 }
64
65 static void refresh()
66 {
67         GtkTreeModel *model;
68         struct task **tasks_cur;
69         struct task *task;
70         int i;
71         GtkTreeIter iter;
72
73         tasks = get_all_tasks();
74
75         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
76         gtk_list_store_clear(GTK_LIST_STORE(model));
77         for (tasks_cur = tasks, i = 0; *tasks_cur; tasks_cur++, i++) {
78                 task = (*tasks_cur);
79
80                 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
81
82                 if (task->project)
83                         gtk_list_store_set(GTK_LIST_STORE(model),
84                                            &iter,
85                                            2, task->project,
86                                            -1);
87
88                 gtk_list_store_set(GTK_LIST_STORE(model),
89                                    &iter,
90                                    0, (*tasks_cur)->id,
91                                    1, (*tasks_cur)->description,
92                                    -1);
93         }
94 }
95
96 static int tasksave_clicked_cbk(GtkButton *btn, gpointer data)
97 {
98         struct task *task;
99         GtkTextBuffer *buf;
100         char *txt, *opts;
101         GtkTextIter sIter, eIter;
102         const char *ctxt;
103
104         task = get_selected_task(GTK_TREE_VIEW(w_treeview));
105
106         printf("tasksave_clicked_cbk %d\n", task->id);
107
108         if (task->note) {
109                 buf = gtk_text_view_get_buffer(w_note);
110
111                 gtk_text_buffer_get_iter_at_offset(buf, &sIter, 0);
112                 gtk_text_buffer_get_iter_at_offset(buf, &eIter, -1);
113                 txt = gtk_text_buffer_get_text(buf, &sIter, &eIter, TRUE);
114
115                 txt = escape(txt);
116
117                 printf("%s\n", txt);
118         }
119
120         ctxt = gtk_entry_get_text(w_description);
121         txt = escape(ctxt);
122
123         opts = malloc(1
124                       + strlen(task->uuid)
125                       + strlen(" modify description:\"")
126                       + strlen(txt)
127                       + strlen("\"")
128                       + 1);
129         sprintf(opts, " %s modify \"%s\"", task->uuid, txt);
130
131         task_exec(opts);
132
133         free(txt);
134
135         refresh();
136
137         return FALSE;
138 }
139
140 static int refresh_clicked_cbk(GtkButton *btn, gpointer data)
141 {
142         printf("refresh_clicked_cbk\n");
143         refresh();
144
145         return FALSE;
146 }
147
148 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
149 {
150         struct task *task;
151         GtkTextBuffer *buf;
152
153         printf("cursor_changed_cbk\n");
154
155         task = get_selected_task(treeview);
156
157         if (task) {
158
159                 if (task->note) {
160                         buf = gtk_text_view_get_buffer(w_note);
161                         gtk_text_buffer_set_text(buf,
162                                                  task->note,
163                                                  strlen(task->note));
164                 }
165
166                 gtk_entry_set_text(w_description, task->description);
167                 gtk_widget_set_sensitive(w_tasksave_btn, 1);
168         } else {
169                 gtk_widget_set_sensitive(w_tasksave_btn, 0);
170         }
171
172         return FALSE;
173 }
174
175 int main(int argc, char **argv)
176 {
177         GtkWidget *window;
178         GtkWidget *btn;
179         GtkBuilder *builder;
180
181         gtk_init(NULL, NULL);
182         builder = gtk_builder_new();
183         gtk_builder_add_from_file
184                 (builder,
185                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
186                  NULL);
187         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
188
189         w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview"));
190
191         w_note = GTK_TEXT_VIEW(gtk_builder_get_object(builder, "tasknote"));
192
193         w_description = GTK_ENTRY(gtk_builder_get_object(builder,
194                                                          "taskdescription"));
195
196         refresh();
197
198         g_signal_connect(w_treeview,
199                          "cursor-changed", (GCallback)cursor_changed_cbk,
200                          tasks);
201
202         btn = GTK_WIDGET(gtk_builder_get_object(builder, "tasksave"));
203         g_signal_connect(btn,
204                          "clicked", (GCallback)tasksave_clicked_cbk, tasks);
205         gtk_widget_set_sensitive(btn, 0);
206         w_tasksave_btn = btn;
207
208         btn = GTK_WIDGET(gtk_builder_get_object(builder, "refresh"));
209         g_signal_connect(btn,
210                          "clicked", (GCallback)refresh_clicked_cbk, tasks);
211
212         g_object_unref(G_OBJECT(builder));
213
214         gtk_widget_show_all(window);
215
216         gtk_main();
217
218         exit(EXIT_SUCCESS);
219 }