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