2 * Copyright (C) 2010-2012 jeanfi@gmail.com
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.
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.
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
23 #include <json/json.h>
36 static struct task **tasks;
37 static GtkTextView *w_note;
38 static GtkEntry *w_description;
39 static GtkTreeView *w_treeview;
41 static char *task_exec(char *opts)
45 char *str, *tmp, *cmd, buf[1024];
49 cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
50 strcpy(cmd, "task rc.json.array=on ");
53 printf("execute: %s\n", cmd);
64 while ((s = fread(buf, 1, 1024, f))) {
65 tmp = malloc(strlen(str) + s + 1);
66 memcpy(tmp, str, strlen(str));
67 memcpy(tmp + strlen(str), buf, s);
68 tmp[strlen(str) + s] = '\0';
76 printf("pclose fails\n");
86 static struct json_object *task_exec_json(char *opts)
88 struct json_object *o;
91 str = task_exec(opts);
94 o = json_tokener_parse(str);
102 static struct task **get_all_tasks()
105 struct json_object *jtasks, *jtask, *json;
108 jtasks = task_exec_json("export");
113 n = json_object_array_length(jtasks);
115 tasks = malloc((n + 1) * sizeof(struct task *));
117 for (i = 0; i < n; i++) {
118 jtask = json_object_array_get_idx(jtasks, i);
120 tasks[i] = malloc(sizeof(struct task));
122 json = json_object_object_get(jtask, "id");
123 tasks[i]->id = json_object_get_int(json);
125 json = json_object_object_get(jtask, "description");
126 tasks[i]->description = strdup(json_object_get_string(json));
128 json = json_object_object_get(jtask, "status");
129 tasks[i]->status = strdup(json_object_get_string(json));
131 json = json_object_object_get(jtask, "project");
134 = strdup(json_object_get_string(json));
136 tasks[i]->project = NULL;
138 json = json_object_object_get(jtask, "uuid");
139 tasks[i]->uuid = strdup(json_object_get_string(json));
141 tasks[i]->note = NULL;
146 json_object_put(jtasks);
151 static struct task *get_selected_task(GtkTreeView *treeview)
154 GtkTreeViewColumn *cols;
158 gtk_tree_view_get_cursor(treeview, &path, &cols);
161 i = gtk_tree_path_get_indices(path);
164 printf("row selected: %d\n", *i);
168 gtk_tree_path_free(path);
176 static char *escape(const char *txt)
181 result = malloc(2*strlen(txt)+1);
210 static int tasksave_clicked_cbk(GtkButton *btn, gpointer data)
215 GtkTextIter sIter, eIter;
218 task = get_selected_task(GTK_TREE_VIEW(w_treeview));
220 printf("tasksave_clicked_cbk %d\n", task->id);
223 buf = gtk_text_view_get_buffer(w_note);
225 gtk_text_buffer_get_iter_at_offset(buf, &sIter, 0);
226 gtk_text_buffer_get_iter_at_offset(buf, &eIter, -1);
227 txt = gtk_text_buffer_get_text(buf, &sIter, &eIter, TRUE);
234 ctxt = gtk_entry_get_text(w_description);
239 + strlen(" modify description:\"")
243 sprintf(opts, " %s modify \"%s\"", task->uuid, txt);
252 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
257 printf("cursor_changed_cbk\n");
259 task = get_selected_task(treeview);
264 buf = gtk_text_view_get_buffer(w_note);
265 gtk_text_buffer_set_text(buf,
270 gtk_entry_set_text(w_description, task->description);
276 int main(int argc, char **argv)
284 struct task **tasks_cur;
287 gtk_init(NULL, NULL);
288 builder = gtk_builder_new();
289 gtk_builder_add_from_file
291 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
293 window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
294 printf("%p\n", window);
296 w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview"));
298 w_note = GTK_TEXT_VIEW(gtk_builder_get_object(builder, "tasknote"));
300 w_description = GTK_ENTRY(gtk_builder_get_object(builder,
303 model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
305 tasks = get_all_tasks();
307 for (tasks_cur = tasks, i = 0; *tasks_cur; tasks_cur++, i++) {
310 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
313 gtk_list_store_set(GTK_LIST_STORE(model),
318 gtk_list_store_set(GTK_LIST_STORE(model),
321 1, (*tasks_cur)->description,
325 g_signal_connect(w_treeview,
326 "cursor-changed", (GCallback)cursor_changed_cbk, tasks);
328 btn = GTK_WIDGET(gtk_builder_get_object(builder, "tasksave"));
329 g_signal_connect(btn,
330 "clicked", (GCallback)tasksave_clicked_cbk, tasks);
332 g_object_unref(G_OBJECT(builder));
334 gtk_widget_show_all(window);