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>
32 static char *task_exec(char *opts)
37 char *str, *tmp, *cmd, buf[1024];
39 cmd = malloc(strlen("task ") + strlen(opts) + 1);
43 log_debug("execute: %s", cmd);
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';
67 log_err("pclose fails");
72 static char *task_get_version()
76 out = task_exec("--version");
83 static int task_check_version()
87 ver = task_get_version();
92 log_debug("task version: %s", ver);
94 if (!strcmp(ver, "2.2.0") || !strcmp(ver, "2.0.0"))
100 static char *tw_exec(char *opts)
104 if (!task_check_version()) {
105 log_err("ptask is not compatible with the installed version of"
110 opts2 = malloc(strlen("rc.confirmation:no ")
113 strcpy(opts2, "rc.confirmation:no ");
116 return task_exec(opts2);
119 static struct json_object *task_exec_json(char *opts)
121 struct json_object *o;
124 cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1);
125 strcpy(cmd, "rc.json.array=on ");
131 o = json_tokener_parse(str);
142 struct task **tw_get_all_tasks(const char *status)
145 struct json_object *jtasks, *jtask, *json;
149 opts = malloc(strlen("export status:") + strlen(status) + 1);
150 sprintf(opts, "export status:%s", status);
152 jtasks = task_exec_json(opts);
158 n = json_object_array_length(jtasks);
160 tasks = malloc((n + 1) * sizeof(struct task *));
162 for (i = 0; i < n; i++) {
163 jtask = json_object_array_get_idx(jtasks, i);
165 tasks[i] = malloc(sizeof(struct task));
167 json = json_object_object_get(jtask, "id");
168 tasks[i]->id = json_object_get_int(json);
170 json = json_object_object_get(jtask, "description");
171 tasks[i]->description = strdup(json_object_get_string(json));
173 json = json_object_object_get(jtask, "status");
174 tasks[i]->status = strdup(json_object_get_string(json));
176 json = json_object_object_get(jtask, "project");
179 = strdup(json_object_get_string(json));
181 tasks[i]->project = NULL;
183 json = json_object_object_get(jtask, "priority");
186 = strdup(json_object_get_string(json));
188 tasks[i]->priority = strdup("");
190 json = json_object_object_get(jtask, "uuid");
191 tasks[i]->uuid = strdup(json_object_get_string(json));
193 tasks[i]->note = note_get(tasks[i]->uuid);
198 json_object_put(jtasks);
203 static char *escape(const char *txt)
208 result = malloc(2*strlen(txt)+1);
237 void tw_modify_description(const char *uuid, const char *newdesc)
242 str = escape(newdesc);
246 + strlen(" modify :\"")
250 sprintf(opts, " %s modify \"%s\"", uuid, str);
258 void tw_modify_project(const char *uuid, const char *newproject)
263 str = escape(newproject);
267 + strlen(" modify project:\"")
271 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
279 void tw_modify_priority(const char *uuid, const char *priority)
284 str = escape(priority);
288 + strlen(" modify priority:\"")
292 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
300 void tw_add(const char *newdesc)
305 str = escape(newdesc);
312 sprintf(opts, " add \"%s\"", str);
320 void tw_done(const char *uuid)
328 sprintf(opts, " %s done", uuid);
335 static void task_free(struct task *task)
340 free(task->description);
345 free(task->priority);
350 void tw_task_list_free(struct task **tasks)
357 for (cur = tasks; *cur; cur++)