926a0392d063519d13b6566c866a9d94918219ad
[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 #include <sys/stat.h>
24
25 #include <json/json.h>
26
27 #include <log.h>
28 #include "note.h"
29 #include <pstr.h>
30 #include "tw.h"
31
32 static char *task_exec(char *opts)
33 {
34         FILE *f;
35         int ret;
36         size_t s;
37         char *str, *tmp, *cmd, buf[1024];
38
39         cmd = malloc(strlen("task ") + strlen(opts) + 1);
40         strcpy(cmd, "task ");
41         strcat(cmd, opts);
42
43         log_debug("execute: %s", cmd);
44
45         f = popen(cmd, "r");
46
47         free(cmd);
48
49         if (!f) {
50                 perror("popen");
51                 return NULL;
52         }
53
54         str = strdup("");
55         while ((s = fread(buf, 1, 1024, f))) {
56                 tmp = malloc(strlen(str) + s + (size_t)1);
57                 memcpy(tmp, str, strlen(str));
58                 memcpy(tmp + strlen(str), buf, s);
59                 tmp[strlen(str) + s] = '\0';
60                 free(str);
61                 str = tmp;
62         }
63
64         ret = pclose(f);
65
66         if (ret == -1)
67                 log_err("pclose fails");
68
69         return str;
70 }
71
72 static char *task_get_version()
73 {
74         char *out;
75
76         out = task_exec("--version");
77
78         trim(out);
79
80         return out;
81 }
82
83 static int task_check_version()
84 {
85         char *ver;
86
87         ver = task_get_version();
88
89         if (!ver)
90                 return 0;
91
92         log_debug("task version: %s", ver);
93
94         if (!strcmp(ver, "2.2.0") || !strcmp(ver, "2.0.0"))
95                 return 1;
96         else
97                 return 0;
98 }
99
100 static char *tw_exec(char *opts)
101 {
102         char *opts2;
103
104         if (!task_check_version()) {
105                 log_err("ptask is not compatible with the installed version of"
106                         " taskwarrior.");
107                 return NULL;
108         }
109
110         opts2 = malloc(strlen("rc.confirmation:no ")
111                        + strlen(opts)
112                        + 1);
113         strcpy(opts2, "rc.confirmation:no ");
114         strcat(opts2, opts);
115
116         return task_exec(opts2);
117 }
118
119 static struct json_object *task_exec_json(const char *opts)
120 {
121         struct json_object *o;
122         char *str, *cmd;
123
124         cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1);
125         strcpy(cmd, "rc.json.array=on ");
126         strcat(cmd, opts);
127
128         str = tw_exec(cmd);
129
130         if (str) {
131                 o = json_tokener_parse(str);
132                 free(str);
133         } else {
134                 o = NULL;
135         }
136
137         free(cmd);
138
139         return o;
140 }
141
142 struct task **tw_get_all_tasks(const char *status)
143 {
144         int i, n;
145         struct json_object *jtasks, *jtask, *json;
146         struct task **tasks;
147         char *opts;
148
149         opts = malloc(strlen("export status:") + strlen(status) + 1);
150
151         strcpy(opts, "export status:");
152         strcat(opts, status);
153
154         jtasks = task_exec_json(opts);
155         free(opts);
156
157         if (!jtasks)
158                 return NULL;
159
160         n = json_object_array_length(jtasks);
161
162         tasks = malloc((n + 1) * sizeof(struct task *));
163
164         for (i = 0; i < n; i++) {
165                 jtask = json_object_array_get_idx(jtasks, i);
166
167                 tasks[i] = malloc(sizeof(struct task));
168
169                 json = json_object_object_get(jtask, "id");
170                 tasks[i]->id = json_object_get_int(json);
171
172                 json = json_object_object_get(jtask, "description");
173                 tasks[i]->description = strdup(json_object_get_string(json));
174
175                 json = json_object_object_get(jtask, "status");
176                 tasks[i]->status = strdup(json_object_get_string(json));
177
178                 json = json_object_object_get(jtask, "project");
179                 if (json)
180                         tasks[i]->project
181                                 = strdup(json_object_get_string(json));
182                 else
183                         tasks[i]->project = NULL;
184
185                 json = json_object_object_get(jtask, "priority");
186                 if (json)
187                         tasks[i]->priority
188                                 = strdup(json_object_get_string(json));
189                 else
190                         tasks[i]->priority = strdup("");
191
192                 json = json_object_object_get(jtask, "uuid");
193                 tasks[i]->uuid = strdup(json_object_get_string(json));
194
195                 tasks[i]->note = note_get(tasks[i]->uuid);
196         }
197
198         tasks[n] = NULL;
199
200         json_object_put(jtasks);
201
202         return tasks;
203 }
204
205 static char *escape(const char *txt)
206 {
207         char *result;
208         char *c;
209
210         result = malloc(2*strlen(txt)+1);
211         c = result;
212
213         while (*txt) {
214                 switch (*txt) {
215                 case '"':
216                 case '$':
217                 case '&':
218                 case '<':
219                 case '>':
220                         *c = '\\'; c++;
221                         *c = *txt;
222                         break;
223                 default:
224                         *c = *txt;
225                 }
226                 c++;
227                 txt++;
228         }
229
230         *c = '\0';
231
232         return result;
233 }
234
235 void tw_modify_description(const char *uuid, const char *newdesc)
236 {
237         char *opts;
238
239         opts = malloc(1
240                       + strlen(uuid)
241                       + strlen(" modify :\"")
242                       + strlen(newdesc)
243                       + strlen("\"")
244                       + 1);
245         sprintf(opts, " %s modify \"%s\"", uuid, newdesc);
246
247         tw_exec(opts);
248
249         free(opts);
250 }
251
252 void tw_modify_project(const char *uuid, const char *newproject)
253 {
254         char *str;
255         char *opts;
256
257         str = escape(newproject);
258
259         opts = malloc(1
260                       + strlen(uuid)
261                       + strlen(" modify project:\"")
262                       + strlen(str)
263                       + strlen("\"")
264                       + 1);
265         sprintf(opts, " %s modify project:\"%s\"", uuid, str);
266
267         tw_exec(opts);
268
269         free(str);
270         free(opts);
271 }
272
273 void tw_modify_priority(const char *uuid, const char *priority)
274 {
275         char *str;
276         char *opts;
277
278         str = escape(priority);
279
280         opts = malloc(1
281                       + strlen(uuid)
282                       + strlen(" modify priority:\"")
283                       + strlen(str)
284                       + strlen("\"")
285                       + 1);
286         sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
287
288         tw_exec(opts);
289
290         free(str);
291         free(opts);
292 }
293
294 void tw_add(const char *newdesc, const char *prj, const char *prio)
295 {
296         char *opts, *eprj;
297
298         eprj = escape(prj);
299
300         opts = malloc(strlen(" add")
301                       + strlen(" priority:")
302                       + 1
303                       + strlen(" project:\\\"")
304                       + strlen(eprj)
305                       + strlen("\\\"")
306                       + strlen(" \"")
307                       + strlen(newdesc)
308                       + strlen("\"")
309                       + 1);
310
311         strcpy(opts, " add");
312
313         if (prio && strlen(prio) == 1) {
314                 strcat(opts, " priority:");
315                 strcat(opts, prio);
316         }
317
318         if (eprj && strlen(prj)) {
319                 strcat(opts, " project:\\\"");
320                 strcat(opts, eprj);
321                 strcat(opts, "\\\"");
322         }
323
324         strcat(opts, " \"");
325         strcat(opts, newdesc);
326         strcat(opts, " \"");
327
328         tw_exec(opts);
329
330         free(opts);
331         free(eprj);
332 }
333
334 void tw_done(const char *uuid)
335 {
336         char *opts;
337
338         opts = malloc(1
339                       + strlen(uuid)
340                       + strlen(" done")
341                       + 1);
342         sprintf(opts, " %s done", uuid);
343
344         tw_exec(opts);
345
346         free(opts);
347 }
348
349 static void task_free(struct task *task)
350 {
351         if (!task)
352                 return ;
353
354         free(task->description);
355         free(task->status);
356         free(task->uuid);
357         free(task->note);
358         free(task->project);
359         free(task->priority);
360
361         free(task);
362 }
363
364 void tw_task_list_free(struct task **tasks)
365 {
366         struct task **cur;
367
368         if (!tasks)
369                 return ;
370
371         for (cur = tasks; *cur; cur++)
372                 task_free(*cur);
373
374         free(tasks);
375 }