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 /* Whether ptask check that the taskwarrior version is supported. */
35 static int check_version_enabled = 1;
37 struct tm *parse_time(const char *t)
41 tm = malloc(sizeof(struct tm));
42 memset(tm, 0, sizeof(struct tm));
43 strptime(t, "%Y%m%dT%H%M%S%Z", tm);
48 static char *task_exec(char *opts)
53 char *str, *tmp, *cmd, buf[1024];
57 cmd = malloc(strlen("task ") + strlen(opts) + 1);
61 log_debug("execute: %s", cmd);
73 while ((s = fread(buf, 1, 1024, f))) {
74 tmp = malloc(strlen(str) + s + (size_t)1);
75 memcpy(tmp, str, strlen(str));
76 memcpy(tmp + strlen(str), buf, s);
77 tmp[strlen(str) + s] = '\0';
85 log_err("pclose fails");
92 static char *task_get_version()
96 out = task_exec("--version");
103 static int task_check_version()
107 ver = task_get_version();
112 log_debug("task version: %s", ver);
114 if (!strcmp(ver, "2.2.0")
115 || !strcmp(ver, "2.0.0")
116 || !strcmp(ver, "2.3.0")
117 || !strcmp(ver, "2.4.0")
118 || !strcmp(ver, "2.4.1")
119 || !strcmp(ver, "2.5.0"))
125 static char *tw_exec(char *opts)
129 if (check_version_enabled && !task_check_version()) {
130 log_err("ptask is not compatible with the installed version of "
131 "taskwarrior. The command line option -f can force "
132 "the usage of an unsupported version of taskwarrior "
133 "(risk of malfunction like damaging data).");
137 opts2 = malloc(strlen("rc.confirmation:no ")
140 strcpy(opts2, "rc.confirmation:no ");
143 return task_exec(opts2);
146 static struct json_object *task_exec_json(const char *opts)
148 struct json_object *o;
151 cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1);
152 strcpy(cmd, "rc.json.array=on ");
158 o = json_tokener_parse(str);
166 if (o && is_error(o))
172 char **json_to_tags(struct json_object *jtask)
174 struct json_object *jtags, *jtag;
178 jtags = json_object_object_get(jtask, "tags");
184 n = json_object_array_length(jtags);
186 tags = malloc((n + 1) * sizeof(char *));
188 for (i = 0; i < n; i++) {
189 jtag = json_object_array_get_idx(jtags, i);
190 tags[i] = strdup(json_object_get_string(jtag));
198 struct task *json_to_task(struct json_object *jtask)
202 struct json_object *json;
204 task = malloc(sizeof(struct task));
206 json = json_object_object_get(jtask, "id");
207 task->id = json_object_get_int(json);
209 json = json_object_object_get(jtask, "description");
210 task->description = strdup(json_object_get_string(json));
212 json = json_object_object_get(jtask, "status");
213 task->status = strdup(json_object_get_string(json));
215 json = json_object_object_get(jtask, "project");
218 = strdup(json_object_get_string(json));
220 task->project = strdup("");
222 json = json_object_object_get(jtask, "priority");
225 = strdup(json_object_get_string(json));
227 task->priority = strdup("");
229 json = json_object_object_get(jtask, "uuid");
230 task->uuid = strdup(json_object_get_string(json));
232 json = json_object_object_get(jtask, "urgency");
233 urg = json_object_get_string(json);
235 task->urgency = strdup(urg);
237 task->urgency = NULL;
239 task->note = note_get(task->uuid);
241 json = json_object_object_get(jtask, "entry");
242 task->entry = parse_time(json_object_get_string(json));
244 json = json_object_object_get(jtask, "due");
247 = parse_time(json_object_get_string(json));
251 json = json_object_object_get(jtask, "start");
254 = parse_time(json_object_get_string(json));
258 json = json_object_object_get(jtask, "recur");
260 task->recur = strdup(json_object_get_string(json));
264 task->tags = json_to_tags(jtask);
269 struct task **tw_get_all_tasks(const char *status)
272 struct json_object *jtasks, *jtask;
276 opts = malloc(strlen("export status:") + strlen(status) + 1);
278 strcpy(opts, "export status:");
279 strcat(opts, status);
281 jtasks = task_exec_json(opts);
287 n = json_object_array_length(jtasks);
289 tasks = malloc((n + 1) * sizeof(struct task *));
291 for (i = 0; i < n; i++) {
292 jtask = json_object_array_get_idx(jtasks, i);
294 tasks[i] = json_to_task(jtask);
299 json_object_put(jtasks);
304 static char *escape(const char *txt)
309 result = malloc(2*strlen(txt)+1);
334 void tw_modify_description(const char *uuid, const char *newdesc)
340 + strlen(" modify :\"")
344 sprintf(opts, " %s modify \"%s\"", uuid, newdesc);
351 void tw_modify_project(const char *uuid, const char *newproject)
356 str = escape(newproject);
360 + strlen(" modify project:\"")
364 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
372 void tw_modify_priority(const char *uuid, const char *priority)
379 str = escape(priority);
383 + strlen(" modify priority:\"")
387 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
397 void tw_add(const char *newdesc, const char *prj, const char *prio)
405 opts = malloc(strlen("add")
406 + strlen(" priority:")
408 + strlen(" project:\\\"")
418 if (prio && strlen(prio) == 1) {
419 strcat(opts, " priority:");
423 if (eprj && strlen(prj)) {
424 strcat(opts, " project:\\\"");
426 strcat(opts, "\\\"");
430 strcat(opts, newdesc);
441 void tw_task_done(const char *uuid)
449 sprintf(opts, " %s done", uuid);
456 void tw_task_start(const char *uuid)
464 sprintf(opts, " %s start", uuid);
471 void tw_task_stop(const char *uuid)
479 sprintf(opts, " %s stop", uuid);
486 void tw_task_remove(const char *uuid)
494 sprintf(opts, " %s delete", uuid);
501 static void task_free(struct task *task)
508 free(task->description);
513 free(task->priority);
532 void tw_task_list_free(struct task **tasks)
539 for (cur = tasks; *cur; cur++)
545 static void project_free(struct project *p)
554 void tw_project_list_free(struct project **prjs)
556 struct project **cur;
561 for (cur = prjs; *cur; cur++)
567 static struct project *project_list_get(struct project **prjs, const char *name)
569 struct project **cur;
571 for (cur = prjs; *cur; cur++)
572 if (!strcmp((*cur)->name, name))
578 static struct project *project_new(const char *name, int count)
582 prj = malloc(sizeof(struct project));
584 prj->name = strdup(name);
590 static int projects_length(struct project **list)
606 static struct project **projects_add(struct project **list, void *item)
609 struct project **result;
611 n = projects_length(list);
613 result = (struct project **)malloc
614 ((n + 1 + 1) * sizeof(struct project *));
617 memcpy(result, list, n * sizeof(struct project *));
620 result[n + 1] = NULL;
625 struct project **tw_get_projects(struct task **tasks)
628 struct project **prjs, **tmp, *prj;
629 const char *prj_name;
633 prjs = malloc(2 * sizeof(struct project *));
634 prjs[0] = project_new("ALL", 0);
637 for (t_cur = tasks; *t_cur; t_cur++) {
638 prj_name = (*t_cur)->project;
639 prj = project_list_get(prjs, prj_name);
643 prj = project_new(prj_name, 1);
645 tmp = projects_add(prjs, prj);
658 void tw_enable_check_version(int e)
660 check_version_enabled = e;