use rc.confirmation=no for tw 2.2.0
[ptask.git] / src / tw.c
index c92c1ee..30f5dcb 100644 (file)
--- a/src/tw.c
+++ b/src/tw.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2012 jeanfi@gmail.com
+ * Copyright (C) 2012-2013 jeanfi@gmail.com
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
 #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"
 
-char *task_exec(char *opts)
+static char *task_exec(char *opts)
 {
        FILE *f;
        int ret;
        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("");
@@ -56,35 +63,80 @@ char *task_exec(char *opts)
 
        ret = pclose(f);
 
-       if (ret == -1) {
-               printf("pclose fails\n");
-               perror("pclose");
-       }
-
- exit_free:
-       free(cmd);
+       if (ret == -1)
+               log_err("pclose fails");
 
        return str;
 }
 
-#include <json/json.h>
+static char *task_get_version()
+{
+       char *out;
 
-#include "tw.h"
+       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;
+       }
+
+       opts2 = malloc(strlen("rc.confirmation:no ")
+                      + strlen(opts)
+                      + 1);
+       strcpy(opts2, "rc.confirmation:no ");
+       strcat(opts2, opts);
+
+       return task_exec(opts2);
+}
 
 static struct json_object *task_exec_json(char *opts)
 {
        struct json_object *o;
-       char *str;
+       char *str, *cmd;
+
+       cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1);
+       strcpy(cmd, "rc.json.array=on ");
+       strcat(cmd, opts);
 
-       str = task_exec(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)
@@ -148,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;
@@ -197,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);
@@ -218,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);
@@ -239,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);
@@ -259,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);
@@ -275,7 +327,35 @@ void tw_done(const char *uuid)
                      + 1);
        sprintf(opts, " %s done", uuid);
 
-       task_exec(opts);
+       tw_exec(opts);
 
        free(opts);
 }
+
+static void task_free(struct task *task)
+{
+       if (!task)
+               return ;
+
+       free(task->description);
+       free(task->status);
+       free(task->uuid);
+       free(task->note);
+       free(task->project);
+       free(task->priority);
+
+       free(task);
+}
+
+void tw_task_list_free(struct task **tasks)
+{
+       struct task **cur;
+
+       if (!tasks)
+               return ;
+
+       for (cur = tasks; *cur; cur++)
+               task_free(*cur);
+
+       free(tasks);
+}