(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
28 struct task {
29         int id;
30         char *description;
31         char *status;
32 };
33
34 static struct task **tasks;
35
36 static char *task_exec(char *opts)
37 {
38         FILE *f;
39         int ret, s;
40         char *str, *tmp, *cmd, buf[1024];
41
42         str = NULL;
43
44         cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
45         strcpy(cmd, "task rc.json.array=on ");
46         strcat(cmd, opts);
47
48         f = popen(cmd, "r");
49
50         if (!f) {
51                 perror("popen");
52                 goto exit_free;
53         }
54
55         str = malloc(1);
56         str[0] = '\0';
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';
62                 free(str);
63                 str = tmp;
64         }
65
66         ret = pclose(f);
67
68         if (ret == -1) {
69                 printf("pclose fails\n");
70                 perror("pclose");
71         }
72
73  exit_free:
74         free(cmd);
75
76         return str;
77 }
78
79 static struct json_object *task_exec_json(char *opts)
80 {
81         struct json_object *o;
82         char *str;
83
84         str = task_exec(opts);
85
86         if (str) {
87                 o = json_tokener_parse(str);
88                 free(str);
89                 return o;
90         }
91
92         return NULL;
93 }
94
95 static struct task **get_all_tasks()
96 {
97         int i, n;
98         struct json_object *jtasks, *jtask, *json;
99         struct task **tasks;
100
101         jtasks = task_exec_json("export");
102
103         if (!jtasks)
104                 return NULL;
105
106         n = json_object_array_length(jtasks);
107
108         tasks = malloc((n + 1) * sizeof(struct task *));
109
110         for (i = 0; i < n; i++) {
111                 jtask = json_object_array_get_idx(jtasks, i);
112
113                 tasks[i] = malloc(sizeof(struct task));
114
115                 json = json_object_object_get(jtask, "id");
116                 tasks[i]->id = json_object_get_int(json);
117
118                 json = json_object_object_get(jtask, "description");
119                 tasks[i]->description = strdup(json_object_get_string(json));
120
121                 json = json_object_object_get(jtask, "status");
122                 tasks[i]->status = strdup(json_object_get_string(json));
123         }
124
125         tasks[n] = NULL;
126
127         json_object_put(jtasks);
128
129         return tasks;
130 }
131
132 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
133 {
134         GtkTreePath *path;
135         GtkTreeViewColumn *cols;
136         gint *i;
137
138         printf("cursor_changed_cbk\n");
139
140         gtk_tree_view_get_cursor(treeview, &path, &cols);
141
142         if (path) {
143                 i = gtk_tree_path_get_indices(path);
144                 
145                 if (i)
146                         printf("row selected: %d\n", *i);
147                 
148
149         }
150
151         gtk_tree_path_free(path);
152
153         return FALSE;
154 }
155 int main(int argc, char **argv)
156 {
157         GtkWidget *window;
158         GtkWidget *treeview;
159         GtkBuilder *builder;
160         GtkTreeIter iter;
161         int i;
162         GtkTreeModel *model;
163         struct task **tasks_cur;
164
165         gtk_init(NULL, NULL);
166         builder = gtk_builder_new();
167         gtk_builder_add_from_file
168                 (builder,
169                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
170                  NULL);
171         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
172         printf("%p\n", window);
173
174         treeview = GTK_WIDGET(gtk_builder_get_object(builder, "treeview"));
175
176         model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
177
178         tasks = get_all_tasks();
179
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),
183                                    &iter,
184                                    0, (*tasks_cur)->id,
185                                    1, (*tasks_cur)->description,
186                                    -1);
187         }
188
189         g_signal_connect(treeview,
190                          "cursor-changed", (GCallback)cursor_changed_cbk, tasks);
191
192         g_object_unref(G_OBJECT(builder));
193
194         gtk_widget_show_all(window);
195
196         gtk_main();
197
198         exit(EXIT_SUCCESS);
199 }