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
34 struct tm *parse_time(const char *t)
38 tm = malloc(sizeof(struct tm));
39 memset(tm, 0, sizeof(struct tm));
40 strptime(t, "%Y%m%dT%H%M%S%Z", tm);
45 static char *task_exec(char *opts)
50 char *str, *tmp, *cmd, buf[1024];
54 cmd = malloc(strlen("task ") + strlen(opts) + 1);
58 log_debug("execute: %s", cmd);
70 while ((s = fread(buf, 1, 1024, f))) {
71 tmp = malloc(strlen(str) + s + (size_t)1);
72 memcpy(tmp, str, strlen(str));
73 memcpy(tmp + strlen(str), buf, s);
74 tmp[strlen(str) + s] = '\0';
82 log_err("pclose fails");
89 static char *task_get_version()
93 out = task_exec("--version");
100 static int task_check_version()
104 ver = task_get_version();
109 log_debug("task version: %s", ver);
111 if (!strcmp(ver, "2.2.0")
112 || !strcmp(ver, "2.0.0")
113 || !strcmp(ver, "2.3.0"))
119 static char *tw_exec(char *opts)
123 if (!task_check_version()) {
124 log_err("ptask is not compatible with the installed version of"
129 opts2 = malloc(strlen("rc.confirmation:no ")
132 strcpy(opts2, "rc.confirmation:no ");
135 return task_exec(opts2);
138 static struct json_object *task_exec_json(const 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);
158 if (o && is_error(o))
164 char **json_to_tags(struct json_object *jtask)
166 struct json_object *jtags, *jtag;
170 jtags = json_object_object_get(jtask, "tags");
176 n = json_object_array_length(jtags);
178 tags = malloc((n + 1) * sizeof(char *));
180 for (i = 0; i < n; i++) {
181 jtag = json_object_array_get_idx(jtags, i);
182 tags[i] = strdup(json_object_get_string(jtag));
190 struct task *json_to_task(struct json_object *jtask)
194 struct json_object *json;
196 task = malloc(sizeof(struct task));
198 json = json_object_object_get(jtask, "id");
199 task->id = json_object_get_int(json);
201 json = json_object_object_get(jtask, "description");
202 task->description = strdup(json_object_get_string(json));
204 json = json_object_object_get(jtask, "status");
205 task->status = strdup(json_object_get_string(json));
207 json = json_object_object_get(jtask, "project");
210 = strdup(json_object_get_string(json));
212 task->project = strdup("");
214 json = json_object_object_get(jtask, "priority");
217 = strdup(json_object_get_string(json));
219 task->priority = strdup("");
221 json = json_object_object_get(jtask, "uuid");
222 task->uuid = strdup(json_object_get_string(json));
224 json = json_object_object_get(jtask, "urgency");
225 urg = json_object_get_string(json);
227 task->urgency = strdup(urg);
229 task->urgency = NULL;
231 task->note = note_get(task->uuid);
233 json = json_object_object_get(jtask, "entry");
234 task->entry = parse_time(json_object_get_string(json));
236 json = json_object_object_get(jtask, "due");
239 = parse_time(json_object_get_string(json));
243 json = json_object_object_get(jtask, "start");
246 = parse_time(json_object_get_string(json));
250 json = json_object_object_get(jtask, "recur");
252 task->recur = strdup(json_object_get_string(json));
256 task->tags = json_to_tags(jtask);
261 struct task **tw_get_all_tasks(const char *status)
264 struct json_object *jtasks, *jtask;
268 opts = malloc(strlen("export status:") + strlen(status) + 1);
270 strcpy(opts, "export status:");
271 strcat(opts, status);
273 jtasks = task_exec_json(opts);
279 n = json_object_array_length(jtasks);
281 tasks = malloc((n + 1) * sizeof(struct task *));
283 for (i = 0; i < n; i++) {
284 jtask = json_object_array_get_idx(jtasks, i);
286 tasks[i] = json_to_task(jtask);
291 json_object_put(jtasks);
296 static char *escape(const char *txt)
301 result = malloc(2*strlen(txt)+1);
326 void tw_modify_description(const char *uuid, const char *newdesc)
332 + strlen(" modify :\"")
336 sprintf(opts, " %s modify \"%s\"", uuid, newdesc);
343 void tw_modify_project(const char *uuid, const char *newproject)
348 str = escape(newproject);
352 + strlen(" modify project:\"")
356 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
364 void tw_modify_priority(const char *uuid, const char *priority)
371 str = escape(priority);
375 + strlen(" modify priority:\"")
379 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
389 void tw_add(const char *newdesc, const char *prj, const char *prio)
397 opts = malloc(strlen("add")
398 + strlen(" priority:")
400 + strlen(" project:\\\"")
410 if (prio && strlen(prio) == 1) {
411 strcat(opts, " priority:");
415 if (eprj && strlen(prj)) {
416 strcat(opts, " project:\\\"");
418 strcat(opts, "\\\"");
422 strcat(opts, newdesc);
433 void tw_task_done(const char *uuid)
441 sprintf(opts, " %s done", uuid);
448 void tw_task_start(const char *uuid)
456 sprintf(opts, " %s start", uuid);
463 void tw_task_stop(const char *uuid)
471 sprintf(opts, " %s stop", uuid);
478 void tw_task_remove(const char *uuid)
486 sprintf(opts, " %s delete", uuid);
493 static void task_free(struct task *task)
500 free(task->description);
505 free(task->priority);
524 void tw_task_list_free(struct task **tasks)
531 for (cur = tasks; *cur; cur++)
537 static void project_free(struct project *p)
546 void tw_project_list_free(struct project **prjs)
548 struct project **cur;
553 for (cur = prjs; *cur; cur++)
559 static struct project *project_list_get(struct project **prjs, const char *name)
561 struct project **cur;
563 for (cur = prjs; *cur; cur++)
564 if (!strcmp((*cur)->name, name))
570 static struct project *project_new(const char *name, int count)
574 prj = malloc(sizeof(struct project));
576 prj->name = strdup(name);
582 static int projects_length(struct project **list)
598 static struct project **projects_add(struct project **list, void *item)
601 struct project **result;
603 n = projects_length(list);
605 result = (struct project **)malloc
606 ((n + 1 + 1) * sizeof(struct project *));
609 memcpy(result, list, n * sizeof(struct project *));
612 result[n + 1] = NULL;
617 struct project **tw_get_projects(struct task **tasks)
620 struct project **prjs, **tmp, *prj;
621 const char *prj_name;
625 prjs = malloc(2 * sizeof(struct project *));
626 prjs[0] = project_new("ALL", 0);
629 for (t_cur = tasks; *t_cur; t_cur++) {
630 prj_name = (*t_cur)->project;
631 prj = project_list_get(prjs, prj_name);
635 prj = project_new(prj_name, 1);
637 tmp = projects_add(prjs, prj);