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);
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 log_debug("execute: %s", 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 log_err("pclose fails");
99 static struct json_object *task_exec_json(char *opts)
101 struct json_object *o;
104 str = task_exec(opts);
107 o = json_tokener_parse(str);
115 struct task **tw_get_all_tasks(const char *status)
118 struct json_object *jtasks, *jtask, *json;
122 opts = malloc(strlen("export status:") + strlen(status) + 1);
123 sprintf(opts, "export status:%s", status);
125 jtasks = task_exec_json(opts);
131 n = json_object_array_length(jtasks);
133 tasks = malloc((n + 1) * sizeof(struct task *));
135 for (i = 0; i < n; i++) {
136 jtask = json_object_array_get_idx(jtasks, i);
138 tasks[i] = malloc(sizeof(struct task));
140 json = json_object_object_get(jtask, "id");
141 tasks[i]->id = json_object_get_int(json);
143 json = json_object_object_get(jtask, "description");
144 tasks[i]->description = strdup(json_object_get_string(json));
146 json = json_object_object_get(jtask, "status");
147 tasks[i]->status = strdup(json_object_get_string(json));
149 json = json_object_object_get(jtask, "project");
152 = strdup(json_object_get_string(json));
154 tasks[i]->project = NULL;
156 json = json_object_object_get(jtask, "priority");
159 = strdup(json_object_get_string(json));
161 tasks[i]->priority = strdup("");
163 json = json_object_object_get(jtask, "uuid");
164 tasks[i]->uuid = strdup(json_object_get_string(json));
166 tasks[i]->note = note_get(tasks[i]->uuid);
171 json_object_put(jtasks);
176 static char *escape(const char *txt)
181 result = malloc(2*strlen(txt)+1);
210 void tw_modify_description(const char *uuid, const char *newdesc)
215 str = escape(newdesc);
219 + strlen(" modify :\"")
223 sprintf(opts, " %s modify \"%s\"", uuid, str);
231 void tw_modify_project(const char *uuid, const char *newproject)
236 str = escape(newproject);
240 + strlen(" modify project:\"")
244 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
252 void tw_modify_priority(const char *uuid, const char *priority)
257 str = escape(priority);
261 + strlen(" modify priority:\"")
265 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
273 void tw_add(const char *newdesc)
278 str = escape(newdesc);
285 sprintf(opts, " add \"%s\"", str);
293 void tw_done(const char *uuid)
301 sprintf(opts, " %s done", uuid);
308 static void task_free(struct task *task)
313 free(task->description);
318 free(task->priority);
323 void tw_task_list_free(struct task **tasks)
330 for (cur = tasks; *cur; cur++)