X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Ftw.c;h=8c685926e8fdae94b465e8a5cb4ebb79f2cc4203;hb=5b28468c1d7f28cdb664c6e23d71c48d4645901f;hp=30f5dcb0ba45821c7f65b162ec353e72756ac50c;hpb=3a4f20601cd297f4e7db412e377a0f649363e136;p=ptask.git diff --git a/src/tw.c b/src/tw.c index 30f5dcb..8c68592 100644 --- a/src/tw.c +++ b/src/tw.c @@ -16,19 +16,33 @@ * 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" +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; @@ -36,6 +50,8 @@ static char *task_exec(char *opts) size_t s; char *str, *tmp, *cmd, buf[1024]; + log_fct_enter(); + cmd = malloc(strlen("task ") + strlen(opts) + 1); strcpy(cmd, "task "); strcat(cmd, opts); @@ -66,6 +82,8 @@ static char *task_exec(char *opts) if (ret == -1) log_err("pclose fails"); + log_fct_exit(); + return str; } @@ -116,7 +134,7 @@ static char *tw_exec(char *opts) return task_exec(opts2); } -static struct json_object *task_exec_json(char *opts) +static struct json_object *task_exec_json(const char *opts) { struct json_object *o; char *str, *cmd; @@ -145,9 +163,12 @@ struct task **tw_get_all_tasks(const char *status) struct json_object *jtasks, *jtask, *json; struct task **tasks; char *opts; + const char *urg; opts = malloc(strlen("export status:") + strlen(status) + 1); - sprintf(opts, "export status:%s", status); + + strcpy(opts, "export status:"); + strcat(opts, status); jtasks = task_exec_json(opts); free(opts); @@ -178,7 +199,7 @@ struct task **tw_get_all_tasks(const char *status) tasks[i]->project = strdup(json_object_get_string(json)); else - tasks[i]->project = NULL; + tasks[i]->project = strdup(""); json = json_object_object_get(jtask, "priority"); if (json) @@ -190,7 +211,31 @@ struct task **tw_get_all_tasks(const char *status) 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); + + json = json_object_object_get(jtask, "entry"); + tasks[i]->entry = parse_time(json_object_get_string(json)); + + json = json_object_object_get(jtask, "due"); + if (json) + tasks[i]->due + = parse_time(json_object_get_string(json)); + else + tasks[i]->due = NULL; + + json = json_object_object_get(jtask, "start"); + if (json) + tasks[i]->start + = parse_time(json_object_get_string(json)); + else + tasks[i]->start = NULL; } tasks[n] = NULL; @@ -211,16 +256,12 @@ static char *escape(const char *txt) while (*txt) { switch (*txt) { case '"': - *c = '\\'; c++; - *c = '"'; - break; case '$': - *c = '\\'; c++; - *c = '$'; - break; case '&': + case '<': + case '>': *c = '\\'; c++; - *c = '&'; + *c = *txt; break; default: *c = *txt; @@ -236,22 +277,18 @@ static char *escape(const char *txt) void tw_modify_description(const char *uuid, const char *newdesc) { - char *str; char *opts; - str = escape(newdesc); - opts = malloc(1 + strlen(uuid) + strlen(" modify :\"") - + strlen(str) + + strlen(newdesc) + strlen("\"") + 1); - sprintf(opts, " %s modify \"%s\"", uuid, str); + sprintf(opts, " %s modify \"%s\"", uuid, newdesc); tw_exec(opts); - free(str); free(opts); } @@ -281,6 +318,8 @@ void tw_modify_priority(const char *uuid, const char *priority) char *str; char *opts; + log_fct_enter(); + str = escape(priority); opts = malloc(1 @@ -295,26 +334,52 @@ void tw_modify_priority(const char *uuid, const char *priority) free(str); free(opts); + + log_fct_exit(); } -void tw_add(const char *newdesc) +void tw_add(const char *newdesc, const char *prj, const char *prio) { - char *str; - char *opts; + char *opts, *eprj; - str = escape(newdesc); + log_fct_enter(); - opts = malloc(1 - + strlen(" add \"") - + strlen(str) + eprj = escape(prj); + + opts = malloc(strlen("add") + + strlen(" priority:") + + 1 + + strlen(" project:\\\"") + + strlen(eprj) + + strlen("\\\"") + + strlen(" \"") + + strlen(newdesc) + strlen("\"") + 1); - sprintf(opts, " add \"%s\"", str); + + strcpy(opts, "add"); + + if (prio && strlen(prio) == 1) { + strcat(opts, " priority:"); + strcat(opts, prio); + } + + if (eprj && strlen(prj)) { + strcat(opts, " project:\\\""); + strcat(opts, eprj); + strcat(opts, "\\\""); + } + + strcat(opts, " \""); + strcat(opts, newdesc); + strcat(opts, "\""); tw_exec(opts); - free(str); free(opts); + free(eprj); + + log_fct_exit(); } void tw_done(const char *uuid) @@ -332,6 +397,21 @@ void tw_done(const char *uuid) free(opts); } +void tw_task_remove(const char *uuid) +{ + char *opts; + + opts = malloc(1 + + strlen(uuid) + + strlen(" delete") + + 1); + sprintf(opts, " %s delete", uuid); + + tw_exec(opts); + + free(opts); +} + static void task_free(struct task *task) { if (!task) @@ -343,6 +423,10 @@ static void task_free(struct task *task) free(task->note); free(task->project); free(task->priority); + free(task->urgency); + free(task->entry); + free(task->due); + free(task->start); free(task); } @@ -359,3 +443,81 @@ void tw_task_list_free(struct task **tasks) free(tasks); } + +static void project_free(struct project *p) +{ + if (!p) + return ; + + free(p->name); + free(p); +} + +void tw_project_list_free(struct project **prjs) +{ + struct project **cur; + + if (!prjs) + return ; + + for (cur = prjs; *cur; cur++) + project_free(*cur); + + free(prjs); +} + +static struct project *project_list_get(struct project **prjs, const char *name) +{ + struct project **cur; + + for (cur = prjs; *cur; cur++) + if (!strcmp((*cur)->name, name)) + return *cur; + + return NULL; +} + +static struct project *project_new(const char *name, int count) +{ + struct project *prj; + + prj = malloc(sizeof(struct project)); + + prj->name = strdup(name); + prj->count = count; + + return prj; +} + +struct project **tw_get_projects(struct task **tasks) +{ + struct task **t_cur; + struct project **prjs, **tmp, *prj; + const char *prj_name; + + log_fct_enter(); + + prjs = malloc(2 * sizeof(struct project *)); + prjs[0] = project_new("ALL", 0); + prjs[1] = NULL; + + for (t_cur = tasks; *t_cur; t_cur++) { + prj_name = (*t_cur)->project; + prj = project_list_get(prjs, prj_name); + if (prj) { + prj->count++; + } else { + prj = project_new(prj_name, 1); + + tmp = (struct project **)list_add((void **)prjs, prj); + + free(prjs); + prjs = tmp; + } + prjs[0]->count++; + } + + log_fct_exit(); + + return prjs; +}