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