759cf49ff65ccfc446374eb30d6b5e79d796b00a
[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
34 static struct task *get_selected_task(GtkTreeView *treeview)
35 {
36         GtkTreePath *path;
37         GtkTreeViewColumn *cols;
38         gint *i;
39         struct task *task;
40
41         printf("get_selected_task\n");
42
43         gtk_tree_view_get_cursor(treeview, &path, &cols);
44
45         if (path) {
46                 i = gtk_tree_path_get_indices(path);
47                 
48                 if (i)
49                         printf("row selected: %d\n", *i);
50
51                 task = tasks[*i];
52
53                 gtk_tree_path_free(path);
54
55                 return task;
56         }
57
58         printf("get_selected_task returns NULL\n");
59
60         return NULL;
61 }
62
63 static void refresh()
64 {
65         GtkTreeModel *model;
66         struct task **tasks_cur;
67         struct task *task;
68         int i;
69         GtkTreeIter iter;
70         /*GtkTreeSelection *sel;*/
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         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w_treeview));
96         gtk_tree_model_get_iter_first(model, &iter);
97         gtk_tree_selection_select_iter(sel, &iter);*/
98 }
99
100 static int tasksave_clicked_cbk(GtkButton *btn, gpointer data)
101 {
102         struct task *task;
103         GtkTextBuffer *buf;
104         char *txt, *opts;
105         GtkTextIter sIter, eIter;
106         const char *ctxt;
107
108         task = get_selected_task(GTK_TREE_VIEW(w_treeview));
109
110         printf("tasksave_clicked_cbk %d\n", task->id);  
111
112         if (task->note) {
113                 buf = gtk_text_view_get_buffer(w_note);
114
115                 gtk_text_buffer_get_iter_at_offset(buf, &sIter, 0);
116                 gtk_text_buffer_get_iter_at_offset(buf, &eIter, -1);
117                 txt = gtk_text_buffer_get_text(buf, &sIter, &eIter, TRUE);
118
119                 txt = escape(txt);
120
121                 printf("%s\n", txt);
122         }
123
124         ctxt = gtk_entry_get_text(w_description);
125         txt = escape(ctxt);
126
127         opts = malloc(1
128                       + strlen(task->uuid)
129                       + strlen(" modify description:\"")
130                       + strlen(txt)
131                       + strlen("\"")
132                       + 1);
133         sprintf(opts, " %s modify \"%s\"", task->uuid, txt);
134         
135         task_exec(opts);
136
137         free(txt);
138         
139         refresh();
140
141         return FALSE;
142 }
143
144 static int refresh_clicked_cbk(GtkButton *btn, gpointer data)
145 {
146         printf("refresh_clicked_cbk\n");
147         refresh();
148
149         return FALSE;
150 }
151
152 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
153 {
154         struct task *task;
155         GtkTextBuffer *buf;
156
157         printf("cursor_changed_cbk\n");
158
159         task = get_selected_task(treeview);
160
161         if (task) {
162
163                 if (task->note) {
164                         buf = gtk_text_view_get_buffer(w_note);
165                         gtk_text_buffer_set_text(buf,
166                                                  task->note,
167                                                  strlen(task->note));
168                 }
169
170                 gtk_entry_set_text(w_description, task->description);
171         }
172
173         return FALSE;
174 }
175
176 int main(int argc, char **argv)
177 {
178         GtkWidget *window;
179         GtkWidget *btn;
180         GtkBuilder *builder;
181
182         gtk_init(NULL, NULL);
183         builder = gtk_builder_new();
184         gtk_builder_add_from_file
185                 (builder,
186                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
187                  NULL);
188         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
189         printf("%p\n", 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, tasks);
202
203         btn = GTK_WIDGET(gtk_builder_get_object(builder, "tasksave"));
204         g_signal_connect(btn,
205                          "clicked", (GCallback)tasksave_clicked_cbk, tasks);
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 }