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
24 #include <json/json.h>
29 static char *task_exec(char *opts)
34 char *str, *tmp, *cmd, buf[1024];
36 cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
37 strcpy(cmd, "task rc.json.array=on ");
40 printf("execute: %s\n", cmd);
51 while ((s = fread(buf, 1, 1024, f))) {
52 tmp = malloc(strlen(str) + s + (size_t)1);
53 memcpy(tmp, str, strlen(str));
54 memcpy(tmp + strlen(str), buf, s);
55 tmp[strlen(str) + s] = '\0';
63 printf("pclose fails\n");
73 static struct json_object *task_exec_json(char *opts)
75 struct json_object *o;
78 str = task_exec(opts);
81 o = json_tokener_parse(str);
89 struct task **tw_get_all_tasks(const char *status)
92 struct json_object *jtasks, *jtask, *json;
96 opts = malloc(strlen("export status:") + strlen(status) + 1);
97 sprintf(opts, "export status:%s", status);
99 jtasks = task_exec_json(opts);
105 n = json_object_array_length(jtasks);
107 tasks = malloc((n + 1) * sizeof(struct task *));
109 for (i = 0; i < n; i++) {
110 jtask = json_object_array_get_idx(jtasks, i);
112 tasks[i] = malloc(sizeof(struct task));
114 json = json_object_object_get(jtask, "id");
115 tasks[i]->id = json_object_get_int(json);
117 json = json_object_object_get(jtask, "description");
118 tasks[i]->description = strdup(json_object_get_string(json));
120 json = json_object_object_get(jtask, "status");
121 tasks[i]->status = strdup(json_object_get_string(json));
123 json = json_object_object_get(jtask, "project");
126 = strdup(json_object_get_string(json));
128 tasks[i]->project = NULL;
130 json = json_object_object_get(jtask, "priority");
133 = strdup(json_object_get_string(json));
135 tasks[i]->priority = strdup("");
137 json = json_object_object_get(jtask, "uuid");
138 tasks[i]->uuid = strdup(json_object_get_string(json));
140 tasks[i]->note = note_get(tasks[i]->uuid);
145 json_object_put(jtasks);
150 char *escape(const char *txt)
155 result = malloc(2*strlen(txt)+1);
184 void tw_modify_description(const char *uuid, const char *newdesc)
189 str = escape(newdesc);
193 + strlen(" modify :\"")
197 sprintf(opts, " %s modify \"%s\"", uuid, str);
205 void tw_modify_project(const char *uuid, const char *newproject)
210 str = escape(newproject);
214 + strlen(" modify project:\"")
218 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
226 void tw_modify_priority(const char *uuid, const char *priority)
231 str = escape(priority);
235 + strlen(" modify priority:\"")
239 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
247 void tw_add(const char *newdesc)
252 str = escape(newdesc);
259 sprintf(opts, " add \"%s\"", str);
267 void tw_done(const char *uuid)
275 sprintf(opts, " %s done", uuid);
282 static void task_free(struct task *task)
287 free(task->description);
292 free(task->priority);
297 void tw_task_list_free(struct task **tasks)
304 for (cur = tasks; *cur; cur++)