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