(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, "uuid");
131                 tasks[i]->uuid = strdup(json_object_get_string(json));
132
133                 tasks[i]->note = NULL;
134         }
135
136         tasks[n] = NULL;
137
138         json_object_put(jtasks);
139
140         return tasks;
141 }
142
143 char *escape(const char *txt)
144 {
145         char *result;
146         char *c;
147
148         result = malloc(2*strlen(txt)+1);
149         c = result;
150
151         while (*txt) {
152                 switch (*txt) {
153                 case '"':
154                         *c = '\\'; c++;
155                         *c = '"';
156                         break;
157                 case '$':
158                         *c = '\\'; c++;
159                         *c = '$';
160                         break;
161                 case '&':
162                         *c = '\\'; c++;
163                         *c = '&';
164                         break;
165                 default:
166                         *c = *txt;
167                 }
168                 c++;
169                 txt++;
170         }
171
172         *c = '\0';
173
174         return result;
175 }
176
177 void tw_modify_description(const char *uuid, const char *newdesc)
178 {
179         char *str;
180         char *opts;
181
182         str = escape(newdesc);
183
184         opts = malloc(1
185                       + strlen(uuid)
186                       + strlen(" modify :\"")
187                       + strlen(str)
188                       + strlen("\"")
189                       + 1);
190         sprintf(opts, " %s modify \"%s\"", uuid, str);
191
192         task_exec(opts);
193
194         free(str);
195         free(opts);
196 }
197
198 void tw_modify_project(const char *uuid, const char *newproject)
199 {
200         char *str;
201         char *opts;
202
203         str = escape(newproject);
204
205         opts = malloc(1
206                       + strlen(uuid)
207                       + strlen(" modify project:\"")
208                       + strlen(str)
209                       + strlen("\"")
210                       + 1);
211         sprintf(opts, " %s modify project:\"%s\"", uuid, str);
212
213         task_exec(opts);
214
215         free(str);
216         free(opts);
217 }
218
219 void tw_add(const char *newdesc)
220 {
221         char *str;
222         char *opts;
223
224         str = escape(newdesc);
225
226         opts = malloc(1
227                       + strlen(" add \"")
228                       + strlen(str)
229                       + strlen("\"")
230                       + 1);
231         sprintf(opts, " add \"%s\"", str);
232
233         task_exec(opts);
234
235         free(str);
236         free(opts);
237 }