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"))
124 static char *tw_exec(char *opts)
128 if (check_version_enabled && !task_check_version()) {
129 log_err("ptask is not compatible with the installed version of "
130 "taskwarrior. The command line option -f can force "
131 "the usage of an unsupported version of taskwarrior "
132 "(risk of malfunction like damaging data).");
136 opts2 = malloc(strlen("rc.confirmation:no ")
139 strcpy(opts2, "rc.confirmation:no ");
142 return task_exec(opts2);
145 static struct json_object *task_exec_json(const char *opts)
147 struct json_object *o;
150 cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1);
151 strcpy(cmd, "rc.json.array=on ");
157 o = json_tokener_parse(str);
165 if (o && is_error(o))
171 char **json_to_tags(struct json_object *jtask)
173 struct json_object *jtags, *jtag;
177 jtags = json_object_object_get(jtask, "tags");
183 n = json_object_array_length(jtags);
185 tags = malloc((n + 1) * sizeof(char *));
187 for (i = 0; i < n; i++) {
188 jtag = json_object_array_get_idx(jtags, i);
189 tags[i] = strdup(json_object_get_string(jtag));
197 struct task *json_to_task(struct json_object *jtask)
201 struct json_object *json;
203 task = malloc(sizeof(struct task));
205 json = json_object_object_get(jtask, "id");
206 task->id = json_object_get_int(json);
208 json = json_object_object_get(jtask, "description");
209 task->description = strdup(json_object_get_string(json));
211 json = json_object_object_get(jtask, "status");
212 task->status = strdup(json_object_get_string(json));
214 json = json_object_object_get(jtask, "project");
217 = strdup(json_object_get_string(json));
219 task->project = strdup("");
221 json = json_object_object_get(jtask, "priority");
224 = strdup(json_object_get_string(json));
226 task->priority = strdup("");
228 json = json_object_object_get(jtask, "uuid");
229 task->uuid = strdup(json_object_get_string(json));
231 json = json_object_object_get(jtask, "urgency");
232 urg = json_object_get_string(json);
234 task->urgency = strdup(urg);
236 task->urgency = NULL;
238 task->note = note_get(task->uuid);
240 json = json_object_object_get(jtask, "entry");
241 task->entry = parse_time(json_object_get_string(json));
243 json = json_object_object_get(jtask, "due");
246 = parse_time(json_object_get_string(json));
250 json = json_object_object_get(jtask, "start");
253 = parse_time(json_object_get_string(json));
257 json = json_object_object_get(jtask, "recur");
259 task->recur = strdup(json_object_get_string(json));
263 task->tags = json_to_tags(jtask);
268 struct task **tw_get_all_tasks(const char *status)
271 struct json_object *jtasks, *jtask;
275 opts = malloc(strlen("export status:") + strlen(status) + 1);
277 strcpy(opts, "export status:");
278 strcat(opts, status);
280 jtasks = task_exec_json(opts);
286 n = json_object_array_length(jtasks);
288 tasks = malloc((n + 1) * sizeof(struct task *));
290 for (i = 0; i < n; i++) {
291 jtask = json_object_array_get_idx(jtasks, i);
293 tasks[i] = json_to_task(jtask);
298 json_object_put(jtasks);
303 static char *escape(const char *txt)
308 result = malloc(2*strlen(txt)+1);
333 void tw_modify_description(const char *uuid, const char *newdesc)
339 + strlen(" modify :\"")
343 sprintf(opts, " %s modify \"%s\"", uuid, newdesc);
350 void tw_modify_project(const char *uuid, const char *newproject)
355 str = escape(newproject);
359 + strlen(" modify project:\"")
363 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
371 void tw_modify_priority(const char *uuid, const char *priority)
378 str = escape(priority);
382 + strlen(" modify priority:\"")
386 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
396 void tw_add(const char *newdesc, const char *prj, const char *prio)
404 opts = malloc(strlen("add")
405 + strlen(" priority:")
407 + strlen(" project:\\\"")
417 if (prio && strlen(prio) == 1) {
418 strcat(opts, " priority:");
422 if (eprj && strlen(prj)) {
423 strcat(opts, " project:\\\"");
425 strcat(opts, "\\\"");
429 strcat(opts, newdesc);
440 void tw_task_done(const char *uuid)
448 sprintf(opts, " %s done", uuid);
455 void tw_task_start(const char *uuid)
463 sprintf(opts, " %s start", uuid);
470 void tw_task_stop(const char *uuid)
478 sprintf(opts, " %s stop", uuid);
485 void tw_task_remove(const char *uuid)
493 sprintf(opts, " %s delete", uuid);
500 static void task_free(struct task *task)
507 free(task->description);
512 free(task->priority);
531 void tw_task_list_free(struct task **tasks)
538 for (cur = tasks; *cur; cur++)
544 static void project_free(struct project *p)
553 void tw_project_list_free(struct project **prjs)
555 struct project **cur;
560 for (cur = prjs; *cur; cur++)
566 static struct project *project_list_get(struct project **prjs, const char *name)
568 struct project **cur;
570 for (cur = prjs; *cur; cur++)
571 if (!strcmp((*cur)->name, name))
577 static struct project *project_new(const char *name, int count)
581 prj = malloc(sizeof(struct project));
583 prj->name = strdup(name);
589 static int projects_length(struct project **list)
605 static struct project **projects_add(struct project **list, void *item)
608 struct project **result;
610 n = projects_length(list);
612 result = (struct project **)malloc
613 ((n + 1 + 1) * sizeof(struct project *));
616 memcpy(result, list, n * sizeof(struct project *));
619 result[n + 1] = NULL;
624 struct project **tw_get_projects(struct task **tasks)
627 struct project **prjs, **tmp, *prj;
628 const char *prj_name;
632 prjs = malloc(2 * sizeof(struct project *));
633 prjs[0] = project_new("ALL", 0);
636 for (t_cur = tasks; *t_cur; t_cur++) {
637 prj_name = (*t_cur)->project;
638 prj = project_list_get(prjs, prj_name);
642 prj = project_new(prj_name, 1);
644 tmp = projects_add(prjs, prj);
657 void tw_enable_check_version(int e)
659 check_version_enabled = e;