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>
33 static int has_taskrc()
39 home = getenv("HOME");
42 log_err("HOME environment variable not defined");
46 path = malloc(strlen(home) + 1 + strlen(".taskrc") + 1);
47 sprintf(path, "%s/%s", home, ".taskrc");
49 ret = lstat(path, &st);
56 static char *task_exec(char *opts)
61 char *str, *tmp, *cmd, buf[1024];
63 cmd = malloc(strlen("task ") + strlen(opts) + 1);
67 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");
96 static char *task_get_version()
100 out = task_exec("--version");
107 static int task_check_version()
111 ver = task_get_version();
116 log_debug("task version: %s", ver);
118 if (!strcmp(ver, "2.2.0"))
124 static char *tw_exec(char *opts)
129 if (!task_check_version()) {
130 log_err("ptask is not compatible with the installed version of"
135 return task_exec(opts);
138 static struct json_object *task_exec_json(char *opts)
140 struct json_object *o;
143 cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1);
144 strcpy(cmd, "rc.json.array=on ");
150 o = json_tokener_parse(str);
161 struct task **tw_get_all_tasks(const char *status)
164 struct json_object *jtasks, *jtask, *json;
168 opts = malloc(strlen("export status:") + strlen(status) + 1);
169 sprintf(opts, "export status:%s", status);
171 jtasks = task_exec_json(opts);
177 n = json_object_array_length(jtasks);
179 tasks = malloc((n + 1) * sizeof(struct task *));
181 for (i = 0; i < n; i++) {
182 jtask = json_object_array_get_idx(jtasks, i);
184 tasks[i] = malloc(sizeof(struct task));
186 json = json_object_object_get(jtask, "id");
187 tasks[i]->id = json_object_get_int(json);
189 json = json_object_object_get(jtask, "description");
190 tasks[i]->description = strdup(json_object_get_string(json));
192 json = json_object_object_get(jtask, "status");
193 tasks[i]->status = strdup(json_object_get_string(json));
195 json = json_object_object_get(jtask, "project");
198 = strdup(json_object_get_string(json));
200 tasks[i]->project = NULL;
202 json = json_object_object_get(jtask, "priority");
205 = strdup(json_object_get_string(json));
207 tasks[i]->priority = strdup("");
209 json = json_object_object_get(jtask, "uuid");
210 tasks[i]->uuid = strdup(json_object_get_string(json));
212 tasks[i]->note = note_get(tasks[i]->uuid);
217 json_object_put(jtasks);
222 static char *escape(const char *txt)
227 result = malloc(2*strlen(txt)+1);
256 void tw_modify_description(const char *uuid, const char *newdesc)
261 str = escape(newdesc);
265 + strlen(" modify :\"")
269 sprintf(opts, " %s modify \"%s\"", uuid, str);
277 void tw_modify_project(const char *uuid, const char *newproject)
282 str = escape(newproject);
286 + strlen(" modify project:\"")
290 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
298 void tw_modify_priority(const char *uuid, const char *priority)
303 str = escape(priority);
307 + strlen(" modify priority:\"")
311 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
319 void tw_add(const char *newdesc)
324 str = escape(newdesc);
331 sprintf(opts, " add \"%s\"", str);
339 void tw_done(const char *uuid)
347 sprintf(opts, " %s done", uuid);
354 static void task_free(struct task *task)
359 free(task->description);
364 free(task->priority);
369 void tw_task_list_free(struct task **tasks)
376 for (cur = tasks; *cur; cur++)