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")
114 || !strcmp(ver, "2.4.0")
115 || !strcmp(ver, "2.4.1"))
121 static char *tw_exec(char *opts)
125 if (!task_check_version()) {
126 log_err("ptask is not compatible with the installed version of"
131 opts2 = malloc(strlen("rc.confirmation:no ")
134 strcpy(opts2, "rc.confirmation:no ");
137 return task_exec(opts2);
140 static struct json_object *task_exec_json(const char *opts)
142 struct json_object *o;
145 cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1);
146 strcpy(cmd, "rc.json.array=on ");
152 o = json_tokener_parse(str);
160 if (o && is_error(o))
166 char **json_to_tags(struct json_object *jtask)
168 struct json_object *jtags, *jtag;
172 jtags = json_object_object_get(jtask, "tags");
178 n = json_object_array_length(jtags);
180 tags = malloc((n + 1) * sizeof(char *));
182 for (i = 0; i < n; i++) {
183 jtag = json_object_array_get_idx(jtags, i);
184 tags[i] = strdup(json_object_get_string(jtag));
192 struct task *json_to_task(struct json_object *jtask)
196 struct json_object *json;
198 task = malloc(sizeof(struct task));
200 json = json_object_object_get(jtask, "id");
201 task->id = json_object_get_int(json);
203 json = json_object_object_get(jtask, "description");
204 task->description = strdup(json_object_get_string(json));
206 json = json_object_object_get(jtask, "status");
207 task->status = strdup(json_object_get_string(json));
209 json = json_object_object_get(jtask, "project");
212 = strdup(json_object_get_string(json));
214 task->project = strdup("");
216 json = json_object_object_get(jtask, "priority");
219 = strdup(json_object_get_string(json));
221 task->priority = strdup("");
223 json = json_object_object_get(jtask, "uuid");
224 task->uuid = strdup(json_object_get_string(json));
226 json = json_object_object_get(jtask, "urgency");
227 urg = json_object_get_string(json);
229 task->urgency = strdup(urg);
231 task->urgency = NULL;
233 task->note = note_get(task->uuid);
235 json = json_object_object_get(jtask, "entry");
236 task->entry = parse_time(json_object_get_string(json));
238 json = json_object_object_get(jtask, "due");
241 = parse_time(json_object_get_string(json));
245 json = json_object_object_get(jtask, "start");
248 = parse_time(json_object_get_string(json));
252 json = json_object_object_get(jtask, "recur");
254 task->recur = strdup(json_object_get_string(json));
258 task->tags = json_to_tags(jtask);
263 struct task **tw_get_all_tasks(const char *status)
266 struct json_object *jtasks, *jtask;
270 opts = malloc(strlen("export status:") + strlen(status) + 1);
272 strcpy(opts, "export status:");
273 strcat(opts, status);
275 jtasks = task_exec_json(opts);
281 n = json_object_array_length(jtasks);
283 tasks = malloc((n + 1) * sizeof(struct task *));
285 for (i = 0; i < n; i++) {
286 jtask = json_object_array_get_idx(jtasks, i);
288 tasks[i] = json_to_task(jtask);
293 json_object_put(jtasks);
298 static char *escape(const char *txt)
303 result = malloc(2*strlen(txt)+1);
328 void tw_modify_description(const char *uuid, const char *newdesc)
334 + strlen(" modify :\"")
338 sprintf(opts, " %s modify \"%s\"", uuid, newdesc);
345 void tw_modify_project(const char *uuid, const char *newproject)
350 str = escape(newproject);
354 + strlen(" modify project:\"")
358 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
366 void tw_modify_priority(const char *uuid, const char *priority)
373 str = escape(priority);
377 + strlen(" modify priority:\"")
381 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
391 void tw_add(const char *newdesc, const char *prj, const char *prio)
399 opts = malloc(strlen("add")
400 + strlen(" priority:")
402 + strlen(" project:\\\"")
412 if (prio && strlen(prio) == 1) {
413 strcat(opts, " priority:");
417 if (eprj && strlen(prj)) {
418 strcat(opts, " project:\\\"");
420 strcat(opts, "\\\"");
424 strcat(opts, newdesc);
435 void tw_task_done(const char *uuid)
443 sprintf(opts, " %s done", uuid);
450 void tw_task_start(const char *uuid)
458 sprintf(opts, " %s start", uuid);
465 void tw_task_stop(const char *uuid)
473 sprintf(opts, " %s stop", uuid);
480 void tw_task_remove(const char *uuid)
488 sprintf(opts, " %s delete", uuid);
495 static void task_free(struct task *task)
502 free(task->description);
507 free(task->priority);
526 void tw_task_list_free(struct task **tasks)
533 for (cur = tasks; *cur; cur++)
539 static void project_free(struct project *p)
548 void tw_project_list_free(struct project **prjs)
550 struct project **cur;
555 for (cur = prjs; *cur; cur++)
561 static struct project *project_list_get(struct project **prjs, const char *name)
563 struct project **cur;
565 for (cur = prjs; *cur; cur++)
566 if (!strcmp((*cur)->name, name))
572 static struct project *project_new(const char *name, int count)
576 prj = malloc(sizeof(struct project));
578 prj->name = strdup(name);
584 static int projects_length(struct project **list)
600 static struct project **projects_add(struct project **list, void *item)
603 struct project **result;
605 n = projects_length(list);
607 result = (struct project **)malloc
608 ((n + 1 + 1) * sizeof(struct project *));
611 memcpy(result, list, n * sizeof(struct project *));
614 result[n + 1] = NULL;
619 struct project **tw_get_projects(struct task **tasks)
622 struct project **prjs, **tmp, *prj;
623 const char *prj_name;
627 prjs = malloc(2 * sizeof(struct project *));
628 prjs[0] = project_new("ALL", 0);
631 for (t_cur = tasks; *t_cur; t_cur++) {
632 prj_name = (*t_cur)->project;
633 prj = project_list_get(prjs, prj_name);
637 prj = project_new(prj_name, 1);
639 tmp = projects_add(prjs, prj);