From 2b8e6f4abbedd42ca15b9e67a35ee63c7b290d84 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Tue, 16 Oct 2012 07:40:08 +0000 Subject: [PATCH] --- src/Makefile.am | 3 +- src/Makefile.in | 7 ++- src/main.c | 153 +------------------------------------------------- src/tw.c | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tw.h | 36 ++++++++++++ 5 files changed, 215 insertions(+), 155 deletions(-) create mode 100644 src/tw.c create mode 100644 src/tw.h diff --git a/src/Makefile.am b/src/Makefile.am index ef9af98..09d72ff 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,8 @@ LIBS = $(JSON_LIBS) $(GTK_LIBS) bin_PROGRAMS = gtask -gtask_SOURCES = main.c +gtask_SOURCES = main.c \ + tw.c tw.h #dist_man_MANS = gtask.1 diff --git a/src/Makefile.in b/src/Makefile.in index a7e8de7..bb49d46 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -63,7 +63,7 @@ CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = am__installdirs = "$(DESTDIR)$(bindir)" PROGRAMS = $(bin_PROGRAMS) -am_gtask_OBJECTS = main.$(OBJEXT) +am_gtask_OBJECTS = main.$(OBJEXT) tw.$(OBJEXT) gtask_OBJECTS = $(am_gtask_OBJECTS) gtask_LDADD = $(LDADD) DEFAULT_INCLUDES = -I.@am__isrc@ @@ -250,7 +250,9 @@ top_srcdir = @top_srcdir@ SUBDIRS = glade AM_LDFLAGS = -Wl,--as-needed AM_CPPFLAGS = -Wall -Werror $(GTK_CFLAGS) $(JSON_CFLAGS) -gtask_SOURCES = main.c +gtask_SOURCES = main.c \ + tw.c tw.h + all: all-recursive .SUFFIXES: @@ -336,6 +338,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tw.Po@am__quote@ .c.o: @am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< 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; diff --git a/src/tw.c b/src/tw.c new file mode 100644 index 0000000..366a0dc --- /dev/null +++ b/src/tw.c @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2010-2012 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 + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include +#include +#include + +#include + +#include "tw.h" + +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; +} + +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; +} + +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; +} + diff --git a/src/tw.h b/src/tw.h new file mode 100644 index 0000000..5942fa1 --- /dev/null +++ b/src/tw.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2010-2012 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 + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef _GTASK_TW_H_ +#define _GTASK_TW_H_ + +struct task { + int id; + char *description; + char *status; + char *uuid; + char *note; + char *project; +}; + +struct task **get_all_tasks(); +char *task_exec(char *opts); +char *escape(const char *txt); + +#endif -- 2.7.4