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