use rc.confirmation=no for tw 2.2.0
[ptask.git] / src / tw.c
index 33fbc94..30f5dcb 100644 (file)
--- a/src/tw.c
+++ b/src/tw.c
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
 
 #include <json/json.h>
 
+#include <log.h>
 #include "note.h"
+#include <pstr.h>
 #include "tw.h"
 
 static char *task_exec(char *opts)
@@ -33,18 +36,19 @@ static char *task_exec(char *opts)
        size_t s;
        char *str, *tmp, *cmd, buf[1024];
 
-       cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
-       strcpy(cmd, "task rc.json.array=on ");
+       cmd = malloc(strlen("task ") + strlen(opts) + 1);
+       strcpy(cmd, "task ");
        strcat(cmd, opts);
 
-       printf("execute: %s\n", cmd);
+       log_debug("execute: %s", cmd);
 
        f = popen(cmd, "r");
 
+       free(cmd);
+
        if (!f) {
                perror("popen");
-               str = NULL;
-               goto exit_free;
+               return NULL;
        }
 
        str = strdup("");
@@ -59,31 +63,80 @@ static char *task_exec(char *opts)
 
        ret = pclose(f);
 
-       if (ret == -1) {
-               printf("pclose fails\n");
-               perror("pclose");
+       if (ret == -1)
+               log_err("pclose fails");
+
+       return str;
+}
+
+static char *task_get_version()
+{
+       char *out;
+
+       out = task_exec("--version");
+
+       trim(out);
+
+       return out;
+}
+
+static int task_check_version()
+{
+       char *ver;
+
+       ver = task_get_version();
+
+       if (!ver)
+               return 0;
+
+       log_debug("task version: %s", ver);
+
+       if (!strcmp(ver, "2.2.0") || !strcmp(ver, "2.0.0"))
+               return 1;
+       else
+               return 0;
+}
+
+static char *tw_exec(char *opts)
+{
+       char *opts2;
+
+       if (!task_check_version()) {
+               log_err("ptask is not compatible with the installed version of"
+                       " taskwarrior.");
+               return NULL;
        }
 
- exit_free:
-       free(cmd);
+       opts2 = malloc(strlen("rc.confirmation:no ")
+                      + strlen(opts)
+                      + 1);
+       strcpy(opts2, "rc.confirmation:no ");
+       strcat(opts2, opts);
 
-       return str;
+       return task_exec(opts2);
 }
 
 static struct json_object *task_exec_json(char *opts)
 {
        struct json_object *o;
-       char *str;
+       char *str, *cmd;
 
-       str = task_exec(opts);
+       cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1);
+       strcpy(cmd, "rc.json.array=on ");
+       strcat(cmd, opts);
+
+       str = tw_exec(cmd);
 
        if (str) {
                o = json_tokener_parse(str);
                free(str);
-               return o;
+       } else {
+               o = NULL;
        }
 
-       return NULL;
+       free(cmd);
+
+       return o;
 }
 
 struct task **tw_get_all_tasks(const char *status)
@@ -147,7 +200,7 @@ struct task **tw_get_all_tasks(const char *status)
        return tasks;
 }
 
-char *escape(const char *txt)
+static char *escape(const char *txt)
 {
        char *result;
        char *c;
@@ -196,7 +249,7 @@ void tw_modify_description(const char *uuid, const char *newdesc)
                      + 1);
        sprintf(opts, " %s modify \"%s\"", uuid, str);
 
-       task_exec(opts);
+       tw_exec(opts);
 
        free(str);
        free(opts);
@@ -217,7 +270,7 @@ void tw_modify_project(const char *uuid, const char *newproject)
                      + 1);
        sprintf(opts, " %s modify project:\"%s\"", uuid, str);
 
-       task_exec(opts);
+       tw_exec(opts);
 
        free(str);
        free(opts);
@@ -238,7 +291,7 @@ void tw_modify_priority(const char *uuid, const char *priority)
                      + 1);
        sprintf(opts, " %s modify priority:\"%s\"", uuid, str);
 
-       task_exec(opts);
+       tw_exec(opts);
 
        free(str);
        free(opts);
@@ -258,7 +311,7 @@ void tw_add(const char *newdesc)
                      + 1);
        sprintf(opts, " add \"%s\"", str);
 
-       task_exec(opts);
+       tw_exec(opts);
 
        free(str);
        free(opts);
@@ -274,7 +327,7 @@ void tw_done(const char *uuid)
                      + 1);
        sprintf(opts, " %s done", uuid);
 
-       task_exec(opts);
+       tw_exec(opts);
 
        free(opts);
 }