cleanup
[ptask.git] / src / tw.c
1 /*
2  * Copyright (C) 2012-2013 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
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <json/json.h>
25
26 #include "note.h"
27 #include "tw.h"
28
29 static char *task_exec(char *opts)
30 {
31         FILE *f;
32         int ret;
33         size_t s;
34         char *str, *tmp, *cmd, buf[1024];
35
36         cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
37         strcpy(cmd, "task rc.json.array=on ");
38         strcat(cmd, opts);
39
40         printf("execute: %s\n", cmd);
41
42         f = popen(cmd, "r");
43
44         if (!f) {
45                 perror("popen");
46                 str = NULL;
47                 goto exit_free;
48         }
49
50         str = strdup("");
51         while ((s = fread(buf, 1, 1024, f))) {
52                 tmp = malloc(strlen(str) + s + (size_t)1);
53                 memcpy(tmp, str, strlen(str));
54                 memcpy(tmp + strlen(str), buf, s);
55                 tmp[strlen(str) + s] = '\0';
56                 free(str);
57                 str = tmp;
58         }
59
60         ret = pclose(f);
61
62         if (ret == -1) {
63                 printf("pclose fails\n");
64                 perror("pclose");
65         }
66
67  exit_free:
68         free(cmd);
69
70         return str;
71 }
72
73 static struct json_object *task_exec_json(char *opts)
74 {
75         struct json_object *o;
76         char *str;
77
78         str = task_exec(opts);
79
80         if (str) {
81                 o = json_tokener_parse(str);
82                 free(str);
83                 return o;
84         }
85
86         return NULL;
87 }
88
89 struct task **tw_get_all_tasks(const char *status)
90 {
91         int i, n;
92         struct json_object *jtasks, *jtask, *json;
93         struct task **tasks;
94         char *opts;
95
96         opts = malloc(strlen("export status:") + strlen(status) + 1);
97         sprintf(opts, "export status:%s", status);
98
99         jtasks = task_exec_json(opts);
100         free(opts);
101
102         if (!jtasks)
103                 return NULL;
104
105         n = json_object_array_length(jtasks);
106
107         tasks = malloc((n + 1) * sizeof(struct task *));
108
109         for (i = 0; i < n; i++) {
110                 jtask = json_object_array_get_idx(jtasks, i);
111
112                 tasks[i] = malloc(sizeof(struct task));
113
114                 json = json_object_object_get(jtask, "id");
115                 tasks[i]->id = json_object_get_int(json);
116
117                 json = json_object_object_get(jtask, "description");
118                 tasks[i]->description = strdup(json_object_get_string(json));
119
120                 json = json_object_object_get(jtask, "status");
121                 tasks[i]->status = strdup(json_object_get_string(json));
122
123                 json = json_object_object_get(jtask, "project");
124                 if (json)
125                         tasks[i]->project
126                                 = strdup(json_object_get_string(json));
127                 else
128                         tasks[i]->project = NULL;
129
130                 json = json_object_object_get(jtask, "priority");
131                 if (json)
132                         tasks[i]->priority
133                                 = strdup(json_object_get_string(json));
134                 else
135                         tasks[i]->priority = strdup("");
136
137                 json = json_object_object_get(jtask, "uuid");
138                 tasks[i]->uuid = strdup(json_object_get_string(json));
139
140                 tasks[i]->note = note_get(tasks[i]->uuid);
141         }
142
143         tasks[n] = NULL;
144
145         json_object_put(jtasks);
146
147         return tasks;
148 }
149
150 char *escape(const char *txt)
151 {
152         char *result;
153         char *c;
154
155         result = malloc(2*strlen(txt)+1);
156         c = result;
157
158         while (*txt) {
159                 switch (*txt) {
160                 case '"':
161                         *c = '\\'; c++;
162                         *c = '"';
163                         break;
164                 case '$':
165                         *c = '\\'; c++;
166                         *c = '$';
167                         break;
168                 case '&':
169                         *c = '\\'; c++;
170                         *c = '&';
171                         break;
172                 default:
173                         *c = *txt;
174                 }
175                 c++;
176                 txt++;
177         }
178
179         *c = '\0';
180
181         return result;
182 }
183
184 void tw_modify_description(const char *uuid, const char *newdesc)
185 {
186         char *str;
187         char *opts;
188
189         str = escape(newdesc);
190
191         opts = malloc(1
192                       + strlen(uuid)
193                       + strlen(" modify :\"")
194                       + strlen(str)
195                       + strlen("\"")
196                       + 1);
197         sprintf(opts, " %s modify \"%s\"", uuid, str);
198
199         task_exec(opts);
200
201         free(str);
202         free(opts);
203 }
204
205 void tw_modify_project(const char *uuid, const char *newproject)
206 {
207         char *str;
208         char *opts;
209
210         str = escape(newproject);
211
212         opts = malloc(1
213                       + strlen(uuid)
214                       + strlen(" modify project:\"")
215                       + strlen(str)
216                       + strlen("\"")
217                       + 1);
218         sprintf(opts, " %s modify project:\"%s\"", uuid, str);
219
220         task_exec(opts);
221
222         free(str);
223         free(opts);
224 }
225
226 void tw_modify_priority(const char *uuid, const char *priority)
227 {
228         char *str;
229         char *opts;
230
231         str = escape(priority);
232
233         opts = malloc(1
234                       + strlen(uuid)
235                       + strlen(" modify priority:\"")
236                       + strlen(str)
237                       + strlen("\"")
238                       + 1);
239         sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
240
241         task_exec(opts);
242
243         free(str);
244         free(opts);
245 }
246
247 void tw_add(const char *newdesc)
248 {
249         char *str;
250         char *opts;
251
252         str = escape(newdesc);
253
254         opts = malloc(1
255                       + strlen(" add \"")
256                       + strlen(str)
257                       + strlen("\"")
258                       + 1);
259         sprintf(opts, " add \"%s\"", str);
260
261         task_exec(opts);
262
263         free(str);
264         free(opts);
265 }
266
267 void tw_done(const char *uuid)
268 {
269         char *opts;
270
271         opts = malloc(1
272                       + strlen(uuid)
273                       + strlen(" done")
274                       + 1);
275         sprintf(opts, " %s done", uuid);
276
277         task_exec(opts);
278
279         free(opts);
280 }
281
282 static void task_free(struct task *task)
283 {
284         if (!task)
285                 return ;
286
287         free(task->description);
288         free(task->status);
289         free(task->uuid);
290         free(task->note);
291         free(task->project);
292         free(task->priority);
293
294         free(task);
295 }
296
297 void tw_task_list_free(struct task **tasks)
298 {
299         struct task **cur;
300
301         if (!tasks)
302                 return ;
303
304         for (cur = tasks; *cur; cur++)
305                 task_free(*cur);
306
307         free(tasks);
308 }