new task diag allow to set description and priority
[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 *str;
238         char *opts;
239
240         str = escape(newdesc);
241
242         opts = malloc(1
243                       + strlen(uuid)
244                       + strlen(" modify :\"")
245                       + strlen(str)
246                       + strlen("\"")
247                       + 1);
248         sprintf(opts, " %s modify \"%s\"", uuid, str);
249
250         tw_exec(opts);
251
252         free(str);
253         free(opts);
254 }
255
256 void tw_modify_project(const char *uuid, const char *newproject)
257 {
258         char *str;
259         char *opts;
260
261         str = escape(newproject);
262
263         opts = malloc(1
264                       + strlen(uuid)
265                       + strlen(" modify project:\"")
266                       + strlen(str)
267                       + strlen("\"")
268                       + 1);
269         sprintf(opts, " %s modify project:\"%s\"", uuid, str);
270
271         tw_exec(opts);
272
273         free(str);
274         free(opts);
275 }
276
277 void tw_modify_priority(const char *uuid, const char *priority)
278 {
279         char *str;
280         char *opts;
281
282         str = escape(priority);
283
284         opts = malloc(1
285                       + strlen(uuid)
286                       + strlen(" modify priority:\"")
287                       + strlen(str)
288                       + strlen("\"")
289                       + 1);
290         sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
291
292         tw_exec(opts);
293
294         free(str);
295         free(opts);
296 }
297
298 void tw_add(const char *newdesc, const char *prj, const char *prio)
299 {
300         char *opts, *eprj;
301
302         eprj = escape(prj);
303
304         opts = malloc(strlen(" add")
305                       + strlen(" priority:")
306                       + 1
307                       + strlen(" project:\\\"")
308                       + strlen(eprj)
309                       + strlen("\\\"")
310                       + strlen(" \"")
311                       + strlen(newdesc)
312                       + strlen("\"")
313                       + 1);
314
315         strcpy(opts, " add");
316
317         if (prio && strlen(prio) == 1) {
318                 strcat(opts, " priority:");
319                 strcat(opts, prio);
320         }
321
322         if (eprj && strlen(prj)) {
323                 strcat(opts, " project:\\\"");
324                 strcat(opts, eprj);
325                 strcat(opts, "\\\"");
326         }
327
328         strcat(opts, " \"");
329         strcat(opts, newdesc);
330         strcat(opts, " \"");
331
332         tw_exec(opts);
333
334         free(opts);
335         free(eprj);
336 }
337
338 void tw_done(const char *uuid)
339 {
340         char *opts;
341
342         opts = malloc(1
343                       + strlen(uuid)
344                       + strlen(" done")
345                       + 1);
346         sprintf(opts, " %s done", uuid);
347
348         tw_exec(opts);
349
350         free(opts);
351 }
352
353 static void task_free(struct task *task)
354 {
355         if (!task)
356                 return ;
357
358         free(task->description);
359         free(task->status);
360         free(task->uuid);
361         free(task->note);
362         free(task->project);
363         free(task->priority);
364
365         free(task);
366 }
367
368 void tw_task_list_free(struct task **tasks)
369 {
370         struct task **cur;
371
372         if (!tasks)
373                 return ;
374
375         for (cur = tasks; *cur; cur++)
376                 task_free(*cur);
377
378         free(tasks);
379 }