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>
31 static int has_taskrc()
37 home = getenv("HOME");
40 log_err("HOME environment variable not defined");
44 path = malloc(strlen(home) + 1 + strlen(".taskrc") + 1);
45 sprintf(path, "%s/%s", home, ".taskrc");
47 ret = lstat(path, &st);
55 static char *task_exec(char *opts)
60 char *str, *tmp, *cmd, buf[1024];
65 cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
66 strcpy(cmd, "task rc.json.array=on ");
69 log_debug("execute: %s", cmd);
80 while ((s = fread(buf, 1, 1024, f))) {
81 tmp = malloc(strlen(str) + s + (size_t)1);
82 memcpy(tmp, str, strlen(str));
83 memcpy(tmp + strlen(str), buf, s);
84 tmp[strlen(str) + s] = '\0';
92 log_err("pclose fails");
100 static struct json_object *task_exec_json(char *opts)
102 struct json_object *o;
105 str = task_exec(opts);
108 o = json_tokener_parse(str);
116 struct task **tw_get_all_tasks(const char *status)
119 struct json_object *jtasks, *jtask, *json;
123 opts = malloc(strlen("export status:") + strlen(status) + 1);
124 sprintf(opts, "export status:%s", status);
126 jtasks = task_exec_json(opts);
132 n = json_object_array_length(jtasks);
134 tasks = malloc((n + 1) * sizeof(struct task *));
136 for (i = 0; i < n; i++) {
137 jtask = json_object_array_get_idx(jtasks, i);
139 tasks[i] = malloc(sizeof(struct task));
141 json = json_object_object_get(jtask, "id");
142 tasks[i]->id = json_object_get_int(json);
144 json = json_object_object_get(jtask, "description");
145 tasks[i]->description = strdup(json_object_get_string(json));
147 json = json_object_object_get(jtask, "status");
148 tasks[i]->status = strdup(json_object_get_string(json));
150 json = json_object_object_get(jtask, "project");
153 = strdup(json_object_get_string(json));
155 tasks[i]->project = NULL;
157 json = json_object_object_get(jtask, "priority");
160 = strdup(json_object_get_string(json));
162 tasks[i]->priority = strdup("");
164 json = json_object_object_get(jtask, "uuid");
165 tasks[i]->uuid = strdup(json_object_get_string(json));
167 tasks[i]->note = note_get(tasks[i]->uuid);
172 json_object_put(jtasks);
177 static char *escape(const char *txt)
182 result = malloc(2*strlen(txt)+1);
211 void tw_modify_description(const char *uuid, const char *newdesc)
216 str = escape(newdesc);
220 + strlen(" modify :\"")
224 sprintf(opts, " %s modify \"%s\"", uuid, str);
232 void tw_modify_project(const char *uuid, const char *newproject)
237 str = escape(newproject);
241 + strlen(" modify project:\"")
245 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
253 void tw_modify_priority(const char *uuid, const char *priority)
258 str = escape(priority);
262 + strlen(" modify priority:\"")
266 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
274 void tw_add(const char *newdesc)
279 str = escape(newdesc);
286 sprintf(opts, " add \"%s\"", str);
294 void tw_done(const char *uuid)
302 sprintf(opts, " %s done", uuid);
309 static void task_free(struct task *task)
314 free(task->description);
319 free(task->priority);
324 void tw_task_list_free(struct task **tasks)
331 for (cur = tasks; *cur; cur++)