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>
30 static int has_taskrc()
36 home = getenv("HOME");
39 fprintf(stderr, "HOME environment variable not defined\n");
43 path = malloc(strlen(home) + 1 + strlen(".taskrc") + 1);
44 sprintf(path, "%s/%s", home, ".taskrc");
46 ret = lstat(path, &st);
54 static char *task_exec(char *opts)
59 char *str, *tmp, *cmd, buf[1024];
64 cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
65 strcpy(cmd, "task rc.json.array=on ");
68 printf("execute: %s\n", cmd);
79 while ((s = fread(buf, 1, 1024, f))) {
80 tmp = malloc(strlen(str) + s + (size_t)1);
81 memcpy(tmp, str, strlen(str));
82 memcpy(tmp + strlen(str), buf, s);
83 tmp[strlen(str) + s] = '\0';
91 printf("pclose fails\n");
101 static struct json_object *task_exec_json(char *opts)
103 struct json_object *o;
106 str = task_exec(opts);
109 o = json_tokener_parse(str);
117 struct task **tw_get_all_tasks(const char *status)
120 struct json_object *jtasks, *jtask, *json;
124 opts = malloc(strlen("export status:") + strlen(status) + 1);
125 sprintf(opts, "export status:%s", status);
127 jtasks = task_exec_json(opts);
133 n = json_object_array_length(jtasks);
135 tasks = malloc((n + 1) * sizeof(struct task *));
137 for (i = 0; i < n; i++) {
138 jtask = json_object_array_get_idx(jtasks, i);
140 tasks[i] = malloc(sizeof(struct task));
142 json = json_object_object_get(jtask, "id");
143 tasks[i]->id = json_object_get_int(json);
145 json = json_object_object_get(jtask, "description");
146 tasks[i]->description = strdup(json_object_get_string(json));
148 json = json_object_object_get(jtask, "status");
149 tasks[i]->status = strdup(json_object_get_string(json));
151 json = json_object_object_get(jtask, "project");
154 = strdup(json_object_get_string(json));
156 tasks[i]->project = NULL;
158 json = json_object_object_get(jtask, "priority");
161 = strdup(json_object_get_string(json));
163 tasks[i]->priority = strdup("");
165 json = json_object_object_get(jtask, "uuid");
166 tasks[i]->uuid = strdup(json_object_get_string(json));
168 tasks[i]->note = note_get(tasks[i]->uuid);
173 json_object_put(jtasks);
178 static char *escape(const char *txt)
183 result = malloc(2*strlen(txt)+1);
212 void tw_modify_description(const char *uuid, const char *newdesc)
217 str = escape(newdesc);
221 + strlen(" modify :\"")
225 sprintf(opts, " %s modify \"%s\"", uuid, str);
233 void tw_modify_project(const char *uuid, const char *newproject)
238 str = escape(newproject);
242 + strlen(" modify project:\"")
246 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
254 void tw_modify_priority(const char *uuid, const char *priority)
259 str = escape(priority);
263 + strlen(" modify priority:\"")
267 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
275 void tw_add(const char *newdesc)
280 str = escape(newdesc);
287 sprintf(opts, " add \"%s\"", str);
295 void tw_done(const char *uuid)
303 sprintf(opts, " %s done", uuid);
310 static void task_free(struct task *task)
315 free(task->description);
320 free(task->priority);
325 void tw_task_list_free(struct task **tasks)
332 for (cur = tasks; *cur; cur++)