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>
34 static struct task **tasks;
35 static GtkTextView *w_description;
36 static GtkTreeView *w_treeview;
38 static char *task_exec(char *opts)
42 char *str, *tmp, *cmd, buf[1024];
46 cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
47 strcpy(cmd, "task rc.json.array=on ");
50 printf("execute: %s\n", cmd);
61 while ((s = fread(buf, 1, 1024, f))) {
62 tmp = malloc(strlen(str) + s + 1);
63 memcpy(tmp, str, strlen(str));
64 memcpy(tmp + strlen(str), buf, s);
65 tmp[strlen(str) + s] = '\0';
73 printf("pclose fails\n");
83 static struct json_object *task_exec_json(char *opts)
85 struct json_object *o;
88 str = task_exec(opts);
91 o = json_tokener_parse(str);
99 static struct task **get_all_tasks()
102 struct json_object *jtasks, *jtask, *json;
105 jtasks = task_exec_json("export");
110 n = json_object_array_length(jtasks);
112 tasks = malloc((n + 1) * sizeof(struct task *));
114 for (i = 0; i < n; i++) {
115 jtask = json_object_array_get_idx(jtasks, i);
117 tasks[i] = malloc(sizeof(struct task));
119 json = json_object_object_get(jtask, "id");
120 tasks[i]->id = json_object_get_int(json);
122 json = json_object_object_get(jtask, "description");
123 tasks[i]->description = strdup(json_object_get_string(json));
125 json = json_object_object_get(jtask, "status");
126 tasks[i]->status = strdup(json_object_get_string(json));
128 json = json_object_object_get(jtask, "uuid");
129 tasks[i]->uuid = strdup(json_object_get_string(json));
134 json_object_put(jtasks);
139 static struct task *get_selected_task(GtkTreeView *treeview)
142 GtkTreeViewColumn *cols;
146 gtk_tree_view_get_cursor(treeview, &path, &cols);
149 i = gtk_tree_path_get_indices(path);
152 printf("row selected: %d\n", *i);
156 gtk_tree_path_free(path);
164 static char *escape(char *txt)
169 result = malloc(2*strlen(txt)+1);
198 static int tasksave_clicked_cbk(GtkButton *btn, gpointer data)
203 GtkTextIter sIter, eIter;
205 task = get_selected_task(GTK_TREE_VIEW(w_treeview));
207 printf("tasksave_clicked_cbk %d\n", task->id);
209 buf = gtk_text_view_get_buffer(w_description);
211 gtk_text_buffer_get_iter_at_offset(buf, &sIter, 0);
212 gtk_text_buffer_get_iter_at_offset(buf, &eIter, -1);
213 txt = gtk_text_buffer_get_text(buf, &sIter, &eIter, TRUE);
221 + strlen(" modify description:\"")
225 sprintf(opts, " %s modify \"%s\"", task->uuid, txt);
232 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
237 printf("cursor_changed_cbk\n");
239 task = get_selected_task(treeview);
242 buf = gtk_text_view_get_buffer(w_description);
243 gtk_text_buffer_set_text(buf,
245 strlen(task->description));
252 int main(int argc, char **argv)
260 struct task **tasks_cur;
262 gtk_init(NULL, NULL);
263 builder = gtk_builder_new();
264 gtk_builder_add_from_file
266 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
268 window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
269 printf("%p\n", window);
271 w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview"));
273 w_description = GTK_TEXT_VIEW(gtk_builder_get_object(builder,
276 model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
278 tasks = get_all_tasks();
280 for (tasks_cur = tasks, i = 0; *tasks_cur; tasks_cur++, i++) {
281 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
282 gtk_list_store_set(GTK_LIST_STORE(model),
285 1, (*tasks_cur)->description,
289 g_signal_connect(w_treeview,
290 "cursor-changed", (GCallback)cursor_changed_cbk, tasks);
292 btn = GTK_WIDGET(gtk_builder_get_object(builder, "tasksave"));
293 g_signal_connect(btn,
294 "clicked", (GCallback)tasksave_clicked_cbk, tasks);
296 g_object_unref(G_OBJECT(builder));
298 gtk_widget_show_all(window);