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;
155 opts = malloc(strlen("export status:") + strlen(status) + 1);
157 strcpy(opts, "export status:");
158 strcat(opts, status);
160 jtasks = task_exec_json(opts);
166 n = json_object_array_length(jtasks);
168 tasks = malloc((n + 1) * sizeof(struct task *));
170 for (i = 0; i < n; i++) {
171 jtask = json_object_array_get_idx(jtasks, i);
173 tasks[i] = malloc(sizeof(struct task));
175 json = json_object_object_get(jtask, "id");
176 tasks[i]->id = json_object_get_int(json);
178 json = json_object_object_get(jtask, "description");
179 tasks[i]->description = strdup(json_object_get_string(json));
181 json = json_object_object_get(jtask, "status");
182 tasks[i]->status = strdup(json_object_get_string(json));
184 json = json_object_object_get(jtask, "project");
187 = strdup(json_object_get_string(json));
189 tasks[i]->project = strdup("");
191 json = json_object_object_get(jtask, "priority");
194 = strdup(json_object_get_string(json));
196 tasks[i]->priority = strdup("");
198 json = json_object_object_get(jtask, "uuid");
199 tasks[i]->uuid = strdup(json_object_get_string(json));
201 json = json_object_object_get(jtask, "urgency");
202 urg = json_object_get_string(json);
204 tasks[i]->urgency = strdup(urg);
206 tasks[i]->urgency = NULL;
208 tasks[i]->note = note_get(tasks[i]->uuid);
213 json_object_put(jtasks);
218 static char *escape(const char *txt)
223 result = malloc(2*strlen(txt)+1);
248 void tw_modify_description(const char *uuid, const char *newdesc)
254 + strlen(" modify :\"")
258 sprintf(opts, " %s modify \"%s\"", uuid, newdesc);
265 void tw_modify_project(const char *uuid, const char *newproject)
270 str = escape(newproject);
274 + strlen(" modify project:\"")
278 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
286 void tw_modify_priority(const char *uuid, const char *priority)
293 str = escape(priority);
297 + strlen(" modify priority:\"")
301 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
311 void tw_add(const char *newdesc, const char *prj, const char *prio)
319 opts = malloc(strlen("add")
320 + strlen(" priority:")
322 + strlen(" project:\\\"")
332 if (prio && strlen(prio) == 1) {
333 strcat(opts, " priority:");
337 if (eprj && strlen(prj)) {
338 strcat(opts, " project:\\\"");
340 strcat(opts, "\\\"");
344 strcat(opts, newdesc);
355 void tw_done(const char *uuid)
363 sprintf(opts, " %s done", uuid);
370 void tw_task_remove(const char *uuid)
378 sprintf(opts, " %s delete", uuid);
385 static void task_free(struct task *task)
390 free(task->description);
395 free(task->priority);
401 void tw_task_list_free(struct task **tasks)
408 for (cur = tasks; *cur; cur++)
414 static void project_free(struct project *p)
423 void tw_project_list_free(struct project **prjs)
425 struct project **cur;
430 for (cur = prjs; *cur; cur++)
436 static struct project *project_list_get(struct project **prjs, const char *name)
438 struct project **cur;
440 for (cur = prjs; *cur; cur++)
441 if (!strcmp((*cur)->name, name))
447 static struct project *project_new(const char *name, int count)
451 prj = malloc(sizeof(struct project));
453 prj->name = strdup(name);
459 struct project **tw_get_projects(struct task **tasks)
462 struct project **prjs, **tmp, *prj;
463 const char *prj_name;
467 prjs = malloc(2 * sizeof(struct project *));
468 prjs[0] = project_new("ALL", 0);
471 for (t_cur = tasks; *t_cur; t_cur++) {
472 prj_name = (*t_cur)->project;
473 prj = project_list_get(prjs, prj_name);
477 prj = project_new(prj_name, 1);
479 tmp = (struct project **)list_add((void **)prjs, prj);