X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Ftw.c;h=4da313a15fe99e92d5c4828b7480f50b45dc7389;hb=6bc49f33de2291c045e64d6c60c661d714ca481b;hp=e48ab59f01ce3a43c0b04f75cddf6e593a3082e0;hpb=017d1fb012447d9b60b71e55fc637bf2422f8d5b;p=ptask.git diff --git a/src/tw.c b/src/tw.c index e48ab59..4da313a 100644 --- a/src/tw.c +++ b/src/tw.c @@ -16,20 +16,35 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA */ +#define _GNU_SOURCE #include #include #include #include +#include -#include +#include -#include #include #include "note.h" #include #include "tw.h" +/* Whether ptask check that the taskwarrior version is supported. */ +static int check_version_enabled = 1; + +struct tm *parse_time(const char *t) +{ + struct tm *tm; + + tm = malloc(sizeof(struct tm)); + memset(tm, 0, sizeof(struct tm)); + strptime(t, "%Y%m%dT%H%M%S%Z", tm); + + return tm; +} + static char *task_exec(char *opts) { FILE *f; @@ -96,7 +111,11 @@ static int task_check_version() log_debug("task version: %s", ver); - if (!strcmp(ver, "2.2.0") || !strcmp(ver, "2.0.0")) + if (!strcmp(ver, "2.2.0") + || !strcmp(ver, "2.0.0") + || !strcmp(ver, "2.3.0") + || !strcmp(ver, "2.4.0") + || !strcmp(ver, "2.4.1")) return 1; else return 0; @@ -106,9 +125,11 @@ static char *tw_exec(char *opts) { char *opts2; - if (!task_check_version()) { - log_err("ptask is not compatible with the installed version of" - " taskwarrior."); + if (check_version_enabled && !task_check_version()) { + log_err("ptask is not compatible with the installed version of " + "taskwarrior. The command line option -f can force " + "the usage of an unsupported version of taskwarrior " + "(risk of malfunction like damaging data)."); return NULL; } @@ -141,16 +162,115 @@ static struct json_object *task_exec_json(const char *opts) free(cmd); + if (o && is_error(o)) + return NULL; + return o; } +char **json_to_tags(struct json_object *jtask) +{ + struct json_object *jtags, *jtag; + char **tags; + int n, i; + + jtags = json_object_object_get(jtask, "tags"); + + if (!jtags) + return NULL; + + + n = json_object_array_length(jtags); + + tags = malloc((n + 1) * sizeof(char *)); + + for (i = 0; i < n; i++) { + jtag = json_object_array_get_idx(jtags, i); + tags[i] = strdup(json_object_get_string(jtag)); + } + + tags[n] = NULL; + + return tags; +} + +struct task *json_to_task(struct json_object *jtask) +{ + struct task *task; + const char *urg; + struct json_object *json; + + task = malloc(sizeof(struct task)); + + json = json_object_object_get(jtask, "id"); + task->id = json_object_get_int(json); + + json = json_object_object_get(jtask, "description"); + task->description = strdup(json_object_get_string(json)); + + json = json_object_object_get(jtask, "status"); + task->status = strdup(json_object_get_string(json)); + + json = json_object_object_get(jtask, "project"); + if (json) + task->project + = strdup(json_object_get_string(json)); + else + task->project = strdup(""); + + json = json_object_object_get(jtask, "priority"); + if (json) + task->priority + = strdup(json_object_get_string(json)); + else + task->priority = strdup(""); + + json = json_object_object_get(jtask, "uuid"); + task->uuid = strdup(json_object_get_string(json)); + + json = json_object_object_get(jtask, "urgency"); + urg = json_object_get_string(json); + if (urg) + task->urgency = strdup(urg); + else + task->urgency = NULL; + + task->note = note_get(task->uuid); + + json = json_object_object_get(jtask, "entry"); + task->entry = parse_time(json_object_get_string(json)); + + json = json_object_object_get(jtask, "due"); + if (json) + task->due + = parse_time(json_object_get_string(json)); + else + task->due = NULL; + + json = json_object_object_get(jtask, "start"); + if (json) + task->start + = parse_time(json_object_get_string(json)); + else + task->start = NULL; + + json = json_object_object_get(jtask, "recur"); + if (json) + task->recur = strdup(json_object_get_string(json)); + else + task->recur = NULL; + + task->tags = json_to_tags(jtask); + + return task; +} + struct task **tw_get_all_tasks(const char *status) { int i, n; - struct json_object *jtasks, *jtask, *json; + struct json_object *jtasks, *jtask; struct task **tasks; char *opts; - const char *urg; opts = malloc(strlen("export status:") + strlen(status) + 1); @@ -170,42 +290,7 @@ struct task **tw_get_all_tasks(const char *status) for (i = 0; i < n; i++) { jtask = json_object_array_get_idx(jtasks, i); - tasks[i] = malloc(sizeof(struct task)); - - json = json_object_object_get(jtask, "id"); - tasks[i]->id = json_object_get_int(json); - - json = json_object_object_get(jtask, "description"); - tasks[i]->description = strdup(json_object_get_string(json)); - - json = json_object_object_get(jtask, "status"); - tasks[i]->status = strdup(json_object_get_string(json)); - - json = json_object_object_get(jtask, "project"); - if (json) - tasks[i]->project - = strdup(json_object_get_string(json)); - else - tasks[i]->project = strdup(""); - - json = json_object_object_get(jtask, "priority"); - if (json) - tasks[i]->priority - = strdup(json_object_get_string(json)); - else - tasks[i]->priority = strdup(""); - - json = json_object_object_get(jtask, "uuid"); - tasks[i]->uuid = strdup(json_object_get_string(json)); - - json = json_object_object_get(jtask, "urgency"); - urg = json_object_get_string(json); - if (urg) - tasks[i]->urgency = strdup(urg); - else - tasks[i]->urgency = NULL; - - tasks[i]->note = note_get(tasks[i]->uuid); + tasks[i] = json_to_task(jtask); } tasks[n] = NULL; @@ -352,7 +437,7 @@ void tw_add(const char *newdesc, const char *prj, const char *prio) log_fct_exit(); } -void tw_done(const char *uuid) +void tw_task_done(const char *uuid) { char *opts; @@ -367,6 +452,36 @@ void tw_done(const char *uuid) free(opts); } +void tw_task_start(const char *uuid) +{ + char *opts; + + opts = malloc(1 + + strlen(uuid) + + strlen(" start") + + 1); + sprintf(opts, " %s start", uuid); + + tw_exec(opts); + + free(opts); +} + +void tw_task_stop(const char *uuid) +{ + char *opts; + + opts = malloc(1 + + strlen(uuid) + + strlen(" stop") + + 1); + sprintf(opts, " %s stop", uuid); + + tw_exec(opts); + + free(opts); +} + void tw_task_remove(const char *uuid) { char *opts; @@ -384,6 +499,8 @@ void tw_task_remove(const char *uuid) static void task_free(struct task *task) { + char **tags; + if (!task) return ; @@ -394,6 +511,19 @@ static void task_free(struct task *task) free(task->project); free(task->priority); free(task->urgency); + free(task->entry); + free(task->due); + free(task->start); + free(task->recur); + + tags = task->tags; + if (tags) { + while (*tags) { + free(*tags); + tags++; + } + free(task->tags); + } free(task); } @@ -456,6 +586,41 @@ static struct project *project_new(const char *name, int count) return prj; } +static int projects_length(struct project **list) +{ + int n; + + if (!list) + return 0; + + n = 0; + while (*list) { + n++; + list++; + } + + return n; +} + +static struct project **projects_add(struct project **list, void *item) +{ + int n; + struct project **result; + + n = projects_length(list); + + result = (struct project **)malloc + ((n + 1 + 1) * sizeof(struct project *)); + + if (list) + memcpy(result, list, n * sizeof(struct project *)); + + result[n] = item; + result[n + 1] = NULL; + + return result; +} + struct project **tw_get_projects(struct task **tasks) { struct task **t_cur; @@ -476,7 +641,7 @@ struct project **tw_get_projects(struct task **tasks) } else { prj = project_new(prj_name, 1); - tmp = (struct project **)list_add((void **)prjs, prj); + tmp = projects_add(prjs, prj); free(prjs); prjs = tmp; @@ -488,3 +653,8 @@ struct project **tw_get_projects(struct task **tasks) return prjs; } + +void tw_enable_check_version(int e) +{ + check_version_enabled = e; +}