added urgency column
[ptask.git] / src / tw.c
index 30f5dcb..8235784 100644 (file)
--- a/src/tw.c
+++ b/src/tw.c
@@ -24,6 +24,7 @@
 
 #include <json/json.h>
 
+#include <list.h>
 #include <log.h>
 #include "note.h"
 #include <pstr.h>
@@ -36,6 +37,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 +69,8 @@ static char *task_exec(char *opts)
        if (ret == -1)
                log_err("pclose fails");
 
+       log_fct_exit();
+
        return str;
 }
 
@@ -116,7 +121,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;
@@ -147,7 +152,9 @@ struct task **tw_get_all_tasks(const char *status)
        char *opts;
 
        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 +185,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,6 +197,9 @@ 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");
+               tasks[i]->urgency = strdup(json_object_get_string(json));
+
                tasks[i]->note = note_get(tasks[i]->uuid);
        }
 
@@ -211,16 +221,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 +242,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 +283,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 +299,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 +362,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 +388,7 @@ static void task_free(struct task *task)
        free(task->note);
        free(task->project);
        free(task->priority);
+       free(task->urgency);
 
        free(task);
 }
@@ -359,3 +405,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;
+}