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];
40 cmd = malloc(strlen("task ") + strlen(opts) + 1);
44 log_debug("execute: %s", cmd);
56 while ((s = fread(buf, 1, 1024, f))) {
57 tmp = malloc(strlen(str) + s + (size_t)1);
58 memcpy(tmp, str, strlen(str));
59 memcpy(tmp + strlen(str), buf, s);
60 tmp[strlen(str) + s] = '\0';
68 log_err("pclose fails");
73 static char *task_get_version()
77 out = task_exec("--version");
84 static int task_check_version()
88 ver = task_get_version();
93 log_debug("task version: %s", ver);
95 if (!strcmp(ver, "2.2.0") || !strcmp(ver, "2.0.0"))
101 static char *tw_exec(char *opts)
105 if (!task_check_version()) {
106 log_err("ptask is not compatible with the installed version of"
111 opts2 = malloc(strlen("rc.confirmation:no ")
114 strcpy(opts2, "rc.confirmation:no ");
117 return task_exec(opts2);
120 static struct json_object *task_exec_json(const char *opts)
122 struct json_object *o;
125 cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1);
126 strcpy(cmd, "rc.json.array=on ");
132 o = json_tokener_parse(str);
143 struct task **tw_get_all_tasks(const char *status)
146 struct json_object *jtasks, *jtask, *json;
150 opts = malloc(strlen("export status:") + strlen(status) + 1);
152 strcpy(opts, "export status:");
153 strcat(opts, status);
155 jtasks = task_exec_json(opts);
161 n = json_object_array_length(jtasks);
163 tasks = malloc((n + 1) * sizeof(struct task *));
165 for (i = 0; i < n; i++) {
166 jtask = json_object_array_get_idx(jtasks, i);
168 tasks[i] = malloc(sizeof(struct task));
170 json = json_object_object_get(jtask, "id");
171 tasks[i]->id = json_object_get_int(json);
173 json = json_object_object_get(jtask, "description");
174 tasks[i]->description = strdup(json_object_get_string(json));
176 json = json_object_object_get(jtask, "status");
177 tasks[i]->status = strdup(json_object_get_string(json));
179 json = json_object_object_get(jtask, "project");
182 = strdup(json_object_get_string(json));
184 tasks[i]->project = strdup("");
186 json = json_object_object_get(jtask, "priority");
189 = strdup(json_object_get_string(json));
191 tasks[i]->priority = strdup("");
193 json = json_object_object_get(jtask, "uuid");
194 tasks[i]->uuid = strdup(json_object_get_string(json));
196 tasks[i]->note = note_get(tasks[i]->uuid);
201 json_object_put(jtasks);
206 static char *escape(const char *txt)
211 result = malloc(2*strlen(txt)+1);
236 void tw_modify_description(const char *uuid, const char *newdesc)
242 + strlen(" modify :\"")
246 sprintf(opts, " %s modify \"%s\"", uuid, newdesc);
253 void tw_modify_project(const char *uuid, const char *newproject)
258 str = escape(newproject);
262 + strlen(" modify project:\"")
266 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
274 void tw_modify_priority(const char *uuid, const char *priority)
279 str = escape(priority);
283 + strlen(" modify priority:\"")
287 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
295 void tw_add(const char *newdesc, const char *prj, const char *prio)
301 opts = malloc(strlen(" add")
302 + strlen(" priority:")
304 + strlen(" project:\\\"")
312 strcpy(opts, " add");
314 if (prio && strlen(prio) == 1) {
315 strcat(opts, " priority:");
319 if (eprj && strlen(prj)) {
320 strcat(opts, " project:\\\"");
322 strcat(opts, "\\\"");
326 strcat(opts, newdesc);
335 void tw_done(const char *uuid)
343 sprintf(opts, " %s done", uuid);
350 static void task_free(struct task *task)
355 free(task->description);
360 free(task->priority);
365 void tw_task_list_free(struct task **tasks)
372 for (cur = tasks; *cur; cur++)
378 static void project_free(struct project *p)
387 void tw_project_list_free(struct project **prjs)
389 struct project **cur;
394 for (cur = prjs; *cur; cur++)
400 static struct project *project_list_get(struct project **prjs, const char *name)
402 struct project **cur;
404 for (cur = prjs; *cur; cur++)
405 if (!strcmp((*cur)->name, name))
411 static struct project *project_new(const char *name, int count)
415 prj = malloc(sizeof(struct project));
417 prj->name = strdup(name);
423 struct project **tw_get_projects(struct task **tasks)
426 struct project **prjs, **tmp, *prj;
427 const char *prj_name;
431 prjs = malloc(2 * sizeof(struct project *));
432 prjs[0] = project_new("ALL", 0);
435 for (t_cur = tasks; *t_cur; t_cur++) {
436 prj_name = (*t_cur)->project;
437 prj = project_list_get(prjs, prj_name);
441 prj = project_new(prj_name, 1);
443 tmp = (struct project **)list_add((void **)prjs, prj);