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