2 * Copyright (C) 2012-2013 jeanfi@gmail.com
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.
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.
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
25 #include <json/json.h>
33 static char *task_exec(char *opts)
38 char *str, *tmp, *cmd, buf[1024];
42 cmd = malloc(strlen("task ") + strlen(opts) + 1);
46 log_debug("execute: %s", cmd);
58 while ((s = fread(buf, 1, 1024, f))) {
59 tmp = malloc(strlen(str) + s + (size_t)1);
60 memcpy(tmp, str, strlen(str));
61 memcpy(tmp + strlen(str), buf, s);
62 tmp[strlen(str) + s] = '\0';
70 log_err("pclose fails");
77 static char *task_get_version()
81 out = task_exec("--version");
88 static int task_check_version()
92 ver = task_get_version();
97 log_debug("task version: %s", ver);
99 if (!strcmp(ver, "2.2.0") || !strcmp(ver, "2.0.0"))
105 static char *tw_exec(char *opts)
109 if (!task_check_version()) {
110 log_err("ptask is not compatible with the installed version of"
115 opts2 = malloc(strlen("rc.confirmation:no ")
118 strcpy(opts2, "rc.confirmation:no ");
121 return task_exec(opts2);
124 static struct json_object *task_exec_json(const char *opts)
126 struct json_object *o;
129 cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1);
130 strcpy(cmd, "rc.json.array=on ");
136 o = json_tokener_parse(str);
147 struct task **tw_get_all_tasks(const char *status)
150 struct json_object *jtasks, *jtask, *json;
154 opts = malloc(strlen("export status:") + strlen(status) + 1);
156 strcpy(opts, "export status:");
157 strcat(opts, status);
159 jtasks = task_exec_json(opts);
165 n = json_object_array_length(jtasks);
167 tasks = malloc((n + 1) * sizeof(struct task *));
169 for (i = 0; i < n; i++) {
170 jtask = json_object_array_get_idx(jtasks, i);
172 tasks[i] = malloc(sizeof(struct task));
174 json = json_object_object_get(jtask, "id");
175 tasks[i]->id = json_object_get_int(json);
177 json = json_object_object_get(jtask, "description");
178 tasks[i]->description = strdup(json_object_get_string(json));
180 json = json_object_object_get(jtask, "status");
181 tasks[i]->status = strdup(json_object_get_string(json));
183 json = json_object_object_get(jtask, "project");
186 = strdup(json_object_get_string(json));
188 tasks[i]->project = strdup("");
190 json = json_object_object_get(jtask, "priority");
193 = strdup(json_object_get_string(json));
195 tasks[i]->priority = strdup("");
197 json = json_object_object_get(jtask, "uuid");
198 tasks[i]->uuid = strdup(json_object_get_string(json));
200 tasks[i]->note = note_get(tasks[i]->uuid);
205 json_object_put(jtasks);
210 static char *escape(const char *txt)
215 result = malloc(2*strlen(txt)+1);
240 void tw_modify_description(const char *uuid, const char *newdesc)
246 + strlen(" modify :\"")
250 sprintf(opts, " %s modify \"%s\"", uuid, newdesc);
257 void tw_modify_project(const char *uuid, const char *newproject)
262 str = escape(newproject);
266 + strlen(" modify project:\"")
270 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
278 void tw_modify_priority(const char *uuid, const char *priority)
285 str = escape(priority);
289 + strlen(" modify priority:\"")
293 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
303 void tw_add(const char *newdesc, const char *prj, const char *prio)
311 opts = malloc(strlen("add")
312 + strlen(" priority:")
314 + strlen(" project:\\\"")
324 if (prio && strlen(prio) == 1) {
325 strcat(opts, " priority:");
329 if (eprj && strlen(prj)) {
330 strcat(opts, " project:\\\"");
332 strcat(opts, "\\\"");
336 strcat(opts, newdesc);
347 void tw_done(const char *uuid)
355 sprintf(opts, " %s done", uuid);
362 static void task_free(struct task *task)
367 free(task->description);
372 free(task->priority);
377 void tw_task_list_free(struct task **tasks)
384 for (cur = tasks; *cur; cur++)
390 static void project_free(struct project *p)
399 void tw_project_list_free(struct project **prjs)
401 struct project **cur;
406 for (cur = prjs; *cur; cur++)
412 static struct project *project_list_get(struct project **prjs, const char *name)
414 struct project **cur;
416 for (cur = prjs; *cur; cur++)
417 if (!strcmp((*cur)->name, name))
423 static struct project *project_new(const char *name, int count)
427 prj = malloc(sizeof(struct project));
429 prj->name = strdup(name);
435 struct project **tw_get_projects(struct task **tasks)
438 struct project **prjs, **tmp, *prj;
439 const char *prj_name;
443 prjs = malloc(2 * sizeof(struct project *));
444 prjs[0] = project_new("ALL", 0);
447 for (t_cur = tasks; *t_cur; t_cur++) {
448 prj_name = (*t_cur)->project;
449 prj = project_list_get(prjs, prj_name);
453 prj = project_new(prj_name, 1);
455 tmp = (struct project **)list_add((void **)prjs, prj);