2 * Copyright (C) 2012-2016 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);
169 char **json_to_tags(struct json_object *jtask)
171 struct json_object *jtags, *jtag;
175 jtags = json_object_object_get(jtask, "tags");
181 n = json_object_array_length(jtags);
183 tags = malloc((n + 1) * sizeof(char *));
185 for (i = 0; i < n; i++) {
186 jtag = json_object_array_get_idx(jtags, i);
187 tags[i] = strdup(json_object_get_string(jtag));
195 struct task *json_to_task(struct json_object *jtask)
199 struct json_object *json;
201 task = malloc(sizeof(struct task));
203 json = json_object_object_get(jtask, "id");
204 task->id = json_object_get_int(json);
206 json = json_object_object_get(jtask, "description");
207 task->description = strdup(json_object_get_string(json));
209 json = json_object_object_get(jtask, "status");
210 task->status = strdup(json_object_get_string(json));
212 json = json_object_object_get(jtask, "project");
215 = strdup(json_object_get_string(json));
217 task->project = strdup("");
219 json = json_object_object_get(jtask, "priority");
222 = strdup(json_object_get_string(json));
224 task->priority = strdup("");
226 json = json_object_object_get(jtask, "uuid");
227 task->uuid = strdup(json_object_get_string(json));
229 json = json_object_object_get(jtask, "urgency");
230 urg = json_object_get_string(json);
232 task->urgency = strdup(urg);
234 task->urgency = NULL;
236 task->note = note_get(task->uuid);
238 json = json_object_object_get(jtask, "entry");
239 task->entry = parse_time(json_object_get_string(json));
241 json = json_object_object_get(jtask, "due");
244 = parse_time(json_object_get_string(json));
248 json = json_object_object_get(jtask, "start");
251 = parse_time(json_object_get_string(json));
255 json = json_object_object_get(jtask, "recur");
257 task->recur = strdup(json_object_get_string(json));
261 task->tags = json_to_tags(jtask);
266 struct task **tw_get_all_tasks(const char *status)
269 struct json_object *jtasks, *jtask;
273 opts = malloc(strlen("export status:") + strlen(status) + 1);
275 strcpy(opts, "export status:");
276 strcat(opts, status);
278 jtasks = task_exec_json(opts);
284 n = json_object_array_length(jtasks);
286 tasks = malloc((n + 1) * sizeof(struct task *));
288 for (i = 0; i < n; i++) {
289 jtask = json_object_array_get_idx(jtasks, i);
291 tasks[i] = json_to_task(jtask);
296 json_object_put(jtasks);
301 static char *escape(const char *txt)
306 result = malloc(2*strlen(txt)+1);
331 void tw_modify_description(const char *uuid, const char *newdesc)
337 + strlen(" modify :\"")
341 sprintf(opts, " %s modify \"%s\"", uuid, newdesc);
348 void tw_modify_project(const char *uuid, const char *newproject)
353 str = escape(newproject);
357 + strlen(" modify project:\"")
361 sprintf(opts, " %s modify project:\"%s\"", uuid, str);
369 void tw_modify_priority(const char *uuid, const char *priority)
376 str = escape(priority);
380 + strlen(" modify priority:\"")
384 sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
394 void tw_add(const char *newdesc, const char *prj, const char *prio)
402 opts = malloc(strlen("add")
403 + strlen(" priority:")
405 + strlen(" project:\\\"")
415 if (prio && strlen(prio) == 1) {
416 strcat(opts, " priority:");
420 if (eprj && strlen(prj)) {
421 strcat(opts, " project:\\\"");
423 strcat(opts, "\\\"");
427 strcat(opts, newdesc);
438 void tw_task_done(const char *uuid)
446 sprintf(opts, " %s done", uuid);
453 void tw_task_start(const char *uuid)
461 sprintf(opts, " %s start", uuid);
468 void tw_task_stop(const char *uuid)
476 sprintf(opts, " %s stop", uuid);
483 void tw_task_remove(const char *uuid)
491 sprintf(opts, " %s delete", uuid);
498 static void task_free(struct task *task)
505 free(task->description);
510 free(task->priority);
529 void tw_task_list_free(struct task **tasks)
536 for (cur = tasks; *cur; cur++)
542 static void project_free(struct project *p)
551 void tw_project_list_free(struct project **prjs)
553 struct project **cur;
558 for (cur = prjs; *cur; cur++)
564 static struct project *project_list_get(struct project **prjs, const char *name)
566 struct project **cur;
568 for (cur = prjs; *cur; cur++)
569 if (!strcmp((*cur)->name, name))
575 static struct project *project_new(const char *name, int count)
579 prj = malloc(sizeof(struct project));
581 prj->name = strdup(name);
587 static int projects_length(struct project **list)
603 static struct project **projects_add(struct project **list, void *item)
606 struct project **result;
608 n = projects_length(list);
610 result = (struct project **)malloc
611 ((n + 1 + 1) * sizeof(struct project *));
614 memcpy(result, list, n * sizeof(struct project *));
617 result[n + 1] = NULL;
622 struct project **tw_get_projects(struct task **tasks)
625 struct project **prjs, **tmp, *prj;
626 const char *prj_name;
630 prjs = malloc(2 * sizeof(struct project *));
631 prjs[0] = project_new("ALL", 0);
634 for (t_cur = tasks; *t_cur; t_cur++) {
635 prj_name = (*t_cur)->project;
636 prj = project_list_get(prjs, prj_name);
640 prj = project_new(prj_name, 1);
642 tmp = projects_add(prjs, prj);
655 void tw_enable_check_version(int e)
657 check_version_enabled = e;