X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Ftw.c;h=df73030e8d466a5d4ff0fa27cc840ab9d9e2f991;hb=a2a8032740a38115ffdca3d7356a50196c75a6b7;hp=82357844299de0ca9e08f45231e36858385a1d19;hpb=8e16d048071d7c880b118643b3beea50ede5b4c6;p=ptask.git diff --git a/src/tw.c b/src/tw.c index 8235784..df73030 100644 --- a/src/tw.c +++ b/src/tw.c @@ -16,20 +16,32 @@ * 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; @@ -150,6 +162,7 @@ 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); @@ -198,9 +211,30 @@ struct task **tw_get_all_tasks(const char *status) tasks[i]->uuid = strdup(json_object_get_string(json)); json = json_object_object_get(jtask, "urgency"); - tasks[i]->urgency = strdup(json_object_get_string(json)); + 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; @@ -347,7 +381,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; @@ -362,6 +396,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; @@ -389,6 +453,9 @@ 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); } @@ -451,6 +518,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; @@ -471,7 +573,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;