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;
36 static char *task_exec(char *opts)
40 char *str, *tmp, *cmd, buf[1024];
44 cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
45 strcpy(cmd, "task rc.json.array=on ");
57 while ((s = fread(buf, 1, 1024, f))) {
58 tmp = malloc(strlen(str) + s + 1);
59 memcpy(tmp, str, strlen(str));
60 memcpy(tmp + strlen(str), buf, s);
61 tmp[strlen(str) + s] = '\0';
69 printf("pclose fails\n");
79 static struct json_object *task_exec_json(char *opts)
81 struct json_object *o;
84 str = task_exec(opts);
87 o = json_tokener_parse(str);
95 static struct task **get_all_tasks()
98 struct json_object *jtasks, *jtask, *json;
101 jtasks = task_exec_json("export");
106 n = json_object_array_length(jtasks);
108 tasks = malloc((n + 1) * sizeof(struct task *));
110 for (i = 0; i < n; i++) {
111 jtask = json_object_array_get_idx(jtasks, i);
113 tasks[i] = malloc(sizeof(struct task));
115 json = json_object_object_get(jtask, "id");
116 tasks[i]->id = json_object_get_int(json);
118 json = json_object_object_get(jtask, "description");
119 tasks[i]->description = strdup(json_object_get_string(json));
121 json = json_object_object_get(jtask, "status");
122 tasks[i]->status = strdup(json_object_get_string(json));
127 json_object_put(jtasks);
132 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
135 GtkTreeViewColumn *cols;
138 printf("cursor_changed_cbk\n");
140 gtk_tree_view_get_cursor(treeview, &path, &cols);
143 i = gtk_tree_path_get_indices(path);
146 printf("row selected: %d\n", *i);
151 gtk_tree_path_free(path);
155 int main(int argc, char **argv)
163 struct task **tasks_cur;
165 gtk_init(NULL, NULL);
166 builder = gtk_builder_new();
167 gtk_builder_add_from_file
169 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
171 window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
172 printf("%p\n", window);
174 treeview = GTK_WIDGET(gtk_builder_get_object(builder, "treeview"));
176 model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
178 tasks = get_all_tasks();
180 for (tasks_cur = tasks, i = 0; *tasks_cur; tasks_cur++, i++) {
181 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
182 gtk_list_store_set(GTK_LIST_STORE(model),
185 1, (*tasks_cur)->description,
189 g_signal_connect(treeview,
190 "cursor-changed", (GCallback)cursor_changed_cbk, tasks);
192 g_object_unref(G_OBJECT(builder));
194 gtk_widget_show_all(window);