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
27 #include <json/json.h>
35 struct tm *parse_time(const char *t)
39 tm = malloc(sizeof(struct tm));
40 memset(tm, 0, sizeof(struct tm));
41 strptime(t, "%Y%m%dT%H%M%S%Z", tm);
46 static char *task_exec(char *opts)
51 char *str, *tmp, *cmd, buf[1024];
55 cmd = malloc(strlen("task ") + strlen(opts) + 1);
59 log_debug("execute: %s", cmd);
71 while ((s = fread(buf, 1, 1024, f))) {
72 tmp = malloc(strlen(str) + s + (size_t)1);
73 memcpy(tmp, str, strlen(str));
74 memcpy(tmp + strlen(str), buf, s);
75 tmp[strlen(str) + s] = '\0';
83 log_err("pclose fails");
90 static char *task_get_version()
94 out = task_exec("--version");
101 static int task_check_version()
105 ver = task_get_version();
110 log_debug("task version: %s", ver);
112 if (!strcmp(ver, "2.2.0") || !strcmp(ver, "2.0.0"))
118 static char *tw_exec(char *opts)
122 if (!task_check_version()) {
123 log_err("ptask is not compatible with the installed version of"
128 opts2 = malloc(strlen("rc.confirmation:no ")
131 strcpy(opts2, "rc.confirmation:no ");
134 return task_exec(opts2);
137 static struct json_object *task_exec_json(const char *opts)
139 struct json_object *o;
142 cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1);
143 strcpy(cmd, "rc.json.array=on ");
149 o = json_tokener_parse(str);
160 struct task **tw_get_all_tasks(const char *status)
163 struct json_object *jtasks, *jtask, *json;
168 opts = malloc(strlen("export status:") + strlen(status) + 1);
170 strcpy(opts, "export status:");
171 strcat(opts, status);
173 jtasks = task_exec_json(opts);
179 n = json_object_array_length(jtasks);
181 tasks = malloc((n + 1) * sizeof(struct task *));
183 for (i = 0; i < n; i++) {
184 jtask = json_object_array_get_idx(jtasks, i);
186 tasks[i] = malloc(sizeof(struct task));
188 json = json_object_object_get(jtask, "id");
189 tasks[i]->id = json_object_get_int(json);
191 json = json_object_object_get(jtask, "description");
192 tasks[i]->description = strdup(json_object_get_string(json));
194 json = json_object_object_get(jtask, "status");
195 tasks[i]->status = strdup(json_object_get_string(json));
197 json = json_object_object_get(jtask, "project");
200 = strdup(json_object_get_string(json));
202 tasks[i]->project = strdup("");
204 json = json_object_object_get(jtask, "priority");
207 = strdup(json_object_get_string(json));
209 tasks[i]->priority = strdup("");
211 json = json_object_object_get(jtask, "uuid");
212 tasks[i]->uuid = strdup(json_object_get_string(json));
214 json = json_object_object_get(jtask, "urgency");
215 urg = json_object_get_string(json);
217 tasks[i]->urgency = strdup(urg);
219 tasks[i]->urgency = NULL;
221 tasks[i]->note = note_get(tasks[i]->uuid);
223 json = json_object_object_get(jtask, "entry");
224 tasks[i]->entry = parse_time(json_object_get_string(json));
226 json = json_object_object_get(jtask, "due");
229 = parse_time(json_object_get_string(json));
231 tasks[i]->due = NULL;
233 json = json_object_object_get(jtask, "start");
236 = parse_time(json_object_get_string(json));
238 tasks[i]->start = NULL;
243 json_object_put(jtasks);
248 static char *escape(const char *txt)
253 result = malloc(2*strlen(txt)+1);
278 void tw_modify_description(const char *uuid, const char *newdesc)
284 + strlen(" modify :\"")
288 sprintf(opts, " %s modify \"%s\"", uuid, newdesc);
295 void tw_modify_project(const char *uuid, const char *newproject)
300 str = escape(newproject);
304 + strlen(" modify project:\"")
308 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
316 void tw_modify_priority(const char *uuid, const char *priority)
323 str = escape(priority);
327 + strlen(" modify priority:\"")
331 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
341 void tw_add(const char *newdesc, const char *prj, const char *prio)
349 opts = malloc(strlen("add")
350 + strlen(" priority:")
352 + strlen(" project:\\\"")
362 if (prio && strlen(prio) == 1) {
363 strcat(opts, " priority:");
367 if (eprj && strlen(prj)) {
368 strcat(opts, " project:\\\"");
370 strcat(opts, "\\\"");
374 strcat(opts, newdesc);
385 void tw_done(const char *uuid)
393 sprintf(opts, " %s done", uuid);
400 void tw_task_remove(const char *uuid)
408 sprintf(opts, " %s delete", uuid);
415 static void task_free(struct task *task)
420 free(task->description);
425 free(task->priority);
434 void tw_task_list_free(struct task **tasks)
441 for (cur = tasks; *cur; cur++)
447 static void project_free(struct project *p)
456 void tw_project_list_free(struct project **prjs)
458 struct project **cur;
463 for (cur = prjs; *cur; cur++)
469 static struct project *project_list_get(struct project **prjs, const char *name)
471 struct project **cur;
473 for (cur = prjs; *cur; cur++)
474 if (!strcmp((*cur)->name, name))
480 static struct project *project_new(const char *name, int count)
484 prj = malloc(sizeof(struct project));
486 prj->name = strdup(name);
492 struct project **tw_get_projects(struct task **tasks)
495 struct project **prjs, **tmp, *prj;
496 const char *prj_name;
500 prjs = malloc(2 * sizeof(struct project *));
501 prjs[0] = project_new("ALL", 0);
504 for (t_cur = tasks; *t_cur; t_cur++) {
505 prj_name = (*t_cur)->project;
506 prj = project_list_get(prjs, prj_name);
510 prj = project_new(prj_name, 1);
512 tmp = (struct project **)list_add((void **)prjs, prj);