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 json = json_object_object_get(jtask, "urgency");
201 tasks[i]->urgency = strdup(json_object_get_string(json));
203 tasks[i]->note = note_get(tasks[i]->uuid);
208 json_object_put(jtasks);
213 static char *escape(const char *txt)
218 result = malloc(2*strlen(txt)+1);
243 void tw_modify_description(const char *uuid, const char *newdesc)
249 + strlen(" modify :\"")
253 sprintf(opts, " %s modify \"%s\"", uuid, newdesc);
260 void tw_modify_project(const char *uuid, const char *newproject)
265 str = escape(newproject);
269 + strlen(" modify project:\"")
273 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
281 void tw_modify_priority(const char *uuid, const char *priority)
288 str = escape(priority);
292 + strlen(" modify priority:\"")
296 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
306 void tw_add(const char *newdesc, const char *prj, const char *prio)
314 opts = malloc(strlen("add")
315 + strlen(" priority:")
317 + strlen(" project:\\\"")
327 if (prio && strlen(prio) == 1) {
328 strcat(opts, " priority:");
332 if (eprj && strlen(prj)) {
333 strcat(opts, " project:\\\"");
335 strcat(opts, "\\\"");
339 strcat(opts, newdesc);
350 void tw_done(const char *uuid)
358 sprintf(opts, " %s done", uuid);
365 void tw_task_remove(const char *uuid)
373 sprintf(opts, " %s delete", uuid);
380 static void task_free(struct task *task)
385 free(task->description);
390 free(task->priority);
396 void tw_task_list_free(struct task **tasks)
403 for (cur = tasks; *cur; cur++)
409 static void project_free(struct project *p)
418 void tw_project_list_free(struct project **prjs)
420 struct project **cur;
425 for (cur = prjs; *cur; cur++)
431 static struct project *project_list_get(struct project **prjs, const char *name)
433 struct project **cur;
435 for (cur = prjs; *cur; cur++)
436 if (!strcmp((*cur)->name, name))
442 static struct project *project_new(const char *name, int count)
446 prj = malloc(sizeof(struct project));
448 prj->name = strdup(name);
454 struct project **tw_get_projects(struct task **tasks)
457 struct project **prjs, **tmp, *prj;
458 const char *prj_name;
462 prjs = malloc(2 * sizeof(struct project *));
463 prjs[0] = project_new("ALL", 0);
466 for (t_cur = tasks; *t_cur; t_cur++) {
467 prj_name = (*t_cur)->project;
468 prj = project_list_get(prjs, prj_name);
472 prj = project_new(prj_name, 1);
474 tmp = (struct project **)list_add((void **)prjs, prj);