X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Fmain.c;h=51768229980841cce6a39608ba92a7af00cf2414;hb=2b8e6f4abbedd42ca15b9e67a35ee63c7b290d84;hp=62fa99c35054131005d0411d1c8e6377436d543c;hpb=150347d61ee08df85b377c5af0c5c2d7b3cc8716;p=ptask.git diff --git a/src/main.c b/src/main.c index 62fa99c..5176822 100644 --- a/src/main.c +++ b/src/main.c @@ -24,130 +24,13 @@ #include -struct task { - int id; - char *description; - char *status; - char *uuid; - char *note; - char *project; -}; +#include "tw.h" static struct task **tasks; static GtkTextView *w_note; static GtkEntry *w_description; static GtkTreeView *w_treeview; -static char *task_exec(char *opts) -{ - FILE *f; - int ret, s; - char *str, *tmp, *cmd, buf[1024]; - - str = NULL; - - cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1); - strcpy(cmd, "task rc.json.array=on "); - strcat(cmd, opts); - - printf("execute: %s\n", cmd); - - f = popen(cmd, "r"); - - if (!f) { - perror("popen"); - goto exit_free; - } - - str = malloc(1); - str[0] = '\0'; - while ((s = fread(buf, 1, 1024, f))) { - tmp = malloc(strlen(str) + s + 1); - memcpy(tmp, str, strlen(str)); - memcpy(tmp + strlen(str), buf, s); - tmp[strlen(str) + s] = '\0'; - free(str); - str = tmp; - } - - ret = pclose(f); - - if (ret == -1) { - printf("pclose fails\n"); - perror("pclose"); - } - - exit_free: - free(cmd); - - return str; -} - -static struct json_object *task_exec_json(char *opts) -{ - struct json_object *o; - char *str; - - str = task_exec(opts); - - if (str) { - o = json_tokener_parse(str); - free(str); - return o; - } - - return NULL; -} - -static struct task **get_all_tasks() -{ - int i, n; - struct json_object *jtasks, *jtask, *json; - struct task **tasks; - - jtasks = task_exec_json("export"); - - if (!jtasks) - return NULL; - - n = json_object_array_length(jtasks); - - tasks = malloc((n + 1) * sizeof(struct task *)); - - for (i = 0; i < n; i++) { - jtask = json_object_array_get_idx(jtasks, i); - - tasks[i] = malloc(sizeof(struct task)); - - json = json_object_object_get(jtask, "id"); - tasks[i]->id = json_object_get_int(json); - - json = json_object_object_get(jtask, "description"); - tasks[i]->description = strdup(json_object_get_string(json)); - - json = json_object_object_get(jtask, "status"); - tasks[i]->status = strdup(json_object_get_string(json)); - - json = json_object_object_get(jtask, "project"); - if (json) - tasks[i]->project - = strdup(json_object_get_string(json)); - else - tasks[i]->project = NULL; - - json = json_object_object_get(jtask, "uuid"); - tasks[i]->uuid = strdup(json_object_get_string(json)); - - tasks[i]->note = NULL; - } - - tasks[n] = NULL; - - json_object_put(jtasks); - - return tasks; -} - static struct task *get_selected_task(GtkTreeView *treeview) { GtkTreePath *path; @@ -173,40 +56,6 @@ static struct task *get_selected_task(GtkTreeView *treeview) return NULL; } -static char *escape(const char *txt) -{ - char *result; - char *c; - - result = malloc(2*strlen(txt)+1); - c = result; - - while(*txt) { - switch(*txt) { - case '"': - *c = '\\'; c++; - *c = '"'; - break; - case '$': - *c = '\\'; c++; - *c = '$'; - break; - case '&': - *c = '\\'; c++; - *c = '&'; - break; - default: - *c = *txt; - } - c++; - txt++; - } - - *c = '\0'; - - return result; -} - static int tasksave_clicked_cbk(GtkButton *btn, gpointer data) { struct task *task;