(no commit message)
[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
25 char *task_exec(char *opts)
26 {
27         FILE *f;
28         int ret;
29         size_t s;
30         char *str, *tmp, *cmd, buf[1024];
31
32         cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
33         strcpy(cmd, "task rc.json.array=on ");
34         strcat(cmd, opts);
35
36         printf("execute: %s\n", cmd);
37
38         f = popen(cmd, "r");
39
40         if (!f) {
41                 perror("popen");
42                 str = NULL;
43                 goto exit_free;
44         }
45
46         str = strdup("");
47         while ((s = fread(buf, 1, 1024, f))) {
48                 tmp = malloc(strlen(str) + s + (size_t)1);
49                 memcpy(tmp, str, strlen(str));
50                 memcpy(tmp + strlen(str), buf, s);
51                 tmp[strlen(str) + s] = '\0';
52                 free(str);
53                 str = tmp;
54         }
55
56         ret = pclose(f);
57
58         if (ret == -1) {
59                 printf("pclose fails\n");
60                 perror("pclose");
61         }
62
63  exit_free:
64         free(cmd);
65
66         return str;
67 }
68
69 #include <json/json.h>
70
71 #include "tw.h"
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 = NULL;
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 }