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