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
26 char *task_exec(char *opts)
31 char *str, *tmp, *cmd, buf[1024];
33 cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
34 strcpy(cmd, "task rc.json.array=on ");
37 printf("execute: %s\n", cmd);
48 while ((s = fread(buf, 1, 1024, f))) {
49 tmp = malloc(strlen(str) + s + (size_t)1);
50 memcpy(tmp, str, strlen(str));
51 memcpy(tmp + strlen(str), buf, s);
52 tmp[strlen(str) + s] = '\0';
60 printf("pclose fails\n");
70 #include <json/json.h>
74 static struct json_object *task_exec_json(char *opts)
76 struct json_object *o;
79 str = task_exec(opts);
82 o = json_tokener_parse(str);
90 struct task **tw_get_all_tasks(const char *status)
93 struct json_object *jtasks, *jtask, *json;
97 opts = malloc(strlen("export status:") + strlen(status) + 1);
98 sprintf(opts, "export status:%s", status);
100 jtasks = task_exec_json(opts);
106 n = json_object_array_length(jtasks);
108 tasks = malloc((n + 1) * sizeof(struct task *));
110 for (i = 0; i < n; i++) {
111 jtask = json_object_array_get_idx(jtasks, i);
113 tasks[i] = malloc(sizeof(struct task));
115 json = json_object_object_get(jtask, "id");
116 tasks[i]->id = json_object_get_int(json);
118 json = json_object_object_get(jtask, "description");
119 tasks[i]->description = strdup(json_object_get_string(json));
121 json = json_object_object_get(jtask, "status");
122 tasks[i]->status = strdup(json_object_get_string(json));
124 json = json_object_object_get(jtask, "project");
127 = strdup(json_object_get_string(json));
129 tasks[i]->project = NULL;
131 json = json_object_object_get(jtask, "priority");
134 = strdup(json_object_get_string(json));
136 tasks[i]->priority = strdup("");
138 json = json_object_object_get(jtask, "uuid");
139 tasks[i]->uuid = strdup(json_object_get_string(json));
141 tasks[i]->note = note_get(tasks[i]->uuid);
146 json_object_put(jtasks);
151 char *escape(const char *txt)
156 result = malloc(2*strlen(txt)+1);
185 void tw_modify_description(const char *uuid, const char *newdesc)
190 str = escape(newdesc);
194 + strlen(" modify :\"")
198 sprintf(opts, " %s modify \"%s\"", uuid, str);
206 void tw_modify_project(const char *uuid, const char *newproject)
211 str = escape(newproject);
215 + strlen(" modify project:\"")
219 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
227 void tw_modify_priority(const char *uuid, const char *priority)
232 str = escape(priority);
236 + strlen(" modify priority:\"")
240 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
248 void tw_add(const char *newdesc)
253 str = escape(newdesc);
260 sprintf(opts, " add \"%s\"", str);
268 void tw_done(const char *uuid)
276 sprintf(opts, " %s done", uuid);
283 static void task_free(struct task *task)
288 free(task->description);
293 free(task->priority);
298 void tw_task_list_free(struct task **tasks)
305 for (cur = tasks; *cur; cur++)