30f5dcb0ba45821c7f65b162ec353e72756ac50c
[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(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         sprintf(opts, "export status:%s", status);
151
152         jtasks = task_exec_json(opts);
153         free(opts);
154
155         if (!jtasks)
156                 return NULL;
157
158         n = json_object_array_length(jtasks);
159
160         tasks = malloc((n + 1) * sizeof(struct task *));
161
162         for (i = 0; i < n; i++) {
163                 jtask = json_object_array_get_idx(jtasks, i);
164
165                 tasks[i] = malloc(sizeof(struct task));
166
167                 json = json_object_object_get(jtask, "id");
168                 tasks[i]->id = json_object_get_int(json);
169
170                 json = json_object_object_get(jtask, "description");
171                 tasks[i]->description = strdup(json_object_get_string(json));
172
173                 json = json_object_object_get(jtask, "status");
174                 tasks[i]->status = strdup(json_object_get_string(json));
175
176                 json = json_object_object_get(jtask, "project");
177                 if (json)
178                         tasks[i]->project
179                                 = strdup(json_object_get_string(json));
180                 else
181                         tasks[i]->project = NULL;
182
183                 json = json_object_object_get(jtask, "priority");
184                 if (json)
185                         tasks[i]->priority
186                                 = strdup(json_object_get_string(json));
187                 else
188                         tasks[i]->priority = strdup("");
189
190                 json = json_object_object_get(jtask, "uuid");
191                 tasks[i]->uuid = strdup(json_object_get_string(json));
192
193                 tasks[i]->note = note_get(tasks[i]->uuid);
194         }
195
196         tasks[n] = NULL;
197
198         json_object_put(jtasks);
199
200         return tasks;
201 }
202
203 static char *escape(const char *txt)
204 {
205         char *result;
206         char *c;
207
208         result = malloc(2*strlen(txt)+1);
209         c = result;
210
211         while (*txt) {
212                 switch (*txt) {
213                 case '"':
214                         *c = '\\'; c++;
215                         *c = '"';
216                         break;
217                 case '$':
218                         *c = '\\'; c++;
219                         *c = '$';
220                         break;
221                 case '&':
222                         *c = '\\'; c++;
223                         *c = '&';
224                         break;
225                 default:
226                         *c = *txt;
227                 }
228                 c++;
229                 txt++;
230         }
231
232         *c = '\0';
233
234         return result;
235 }
236
237 void tw_modify_description(const char *uuid, const char *newdesc)
238 {
239         char *str;
240         char *opts;
241
242         str = escape(newdesc);
243
244         opts = malloc(1
245                       + strlen(uuid)
246                       + strlen(" modify :\"")
247                       + strlen(str)
248                       + strlen("\"")
249                       + 1);
250         sprintf(opts, " %s modify \"%s\"", uuid, str);
251
252         tw_exec(opts);
253
254         free(str);
255         free(opts);
256 }
257
258 void tw_modify_project(const char *uuid, const char *newproject)
259 {
260         char *str;
261         char *opts;
262
263         str = escape(newproject);
264
265         opts = malloc(1
266                       + strlen(uuid)
267                       + strlen(" modify project:\"")
268                       + strlen(str)
269                       + strlen("\"")
270                       + 1);
271         sprintf(opts, " %s modify project:\"%s\"", uuid, str);
272
273         tw_exec(opts);
274
275         free(str);
276         free(opts);
277 }
278
279 void tw_modify_priority(const char *uuid, const char *priority)
280 {
281         char *str;
282         char *opts;
283
284         str = escape(priority);
285
286         opts = malloc(1
287                       + strlen(uuid)
288                       + strlen(" modify priority:\"")
289                       + strlen(str)
290                       + strlen("\"")
291                       + 1);
292         sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
293
294         tw_exec(opts);
295
296         free(str);
297         free(opts);
298 }
299
300 void tw_add(const char *newdesc)
301 {
302         char *str;
303         char *opts;
304
305         str = escape(newdesc);
306
307         opts = malloc(1
308                       + strlen(" add \"")
309                       + strlen(str)
310                       + strlen("\"")
311                       + 1);
312         sprintf(opts, " add \"%s\"", str);
313
314         tw_exec(opts);
315
316         free(str);
317         free(opts);
318 }
319
320 void tw_done(const char *uuid)
321 {
322         char *opts;
323
324         opts = malloc(1
325                       + strlen(uuid)
326                       + strlen(" done")
327                       + 1);
328         sprintf(opts, " %s done", uuid);
329
330         tw_exec(opts);
331
332         free(opts);
333 }
334
335 static void task_free(struct task *task)
336 {
337         if (!task)
338                 return ;
339
340         free(task->description);
341         free(task->status);
342         free(task->uuid);
343         free(task->note);
344         free(task->project);
345         free(task->priority);
346
347         free(task);
348 }
349
350 void tw_task_list_free(struct task **tasks)
351 {
352         struct task **cur;
353
354         if (!tasks)
355                 return ;
356
357         for (cur = tasks; *cur; cur++)
358                 task_free(*cur);
359
360         free(tasks);
361 }