#include <json/json.h>
+#include <gtk/gtk.h>
+
+struct task {
+ int id;
+ char *description;
+ char *status;
+ char *uuid;
+};
+
+static struct task **tasks;
+static GtkTextView *w_description;
+static GtkTreeView *w_treeview;
+
static char *task_exec(char *opts)
{
FILE *f;
strcpy(cmd, "task rc.json.array=on ");
strcat(cmd, opts);
+ printf("execute: %s\n", cmd);
+
f = popen(cmd, "r");
if (!f) {
{
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, "uuid");
+ tasks[i]->uuid = strdup(json_object_get_string(json));
+ }
+
+ tasks[n] = NULL;
+
+ json_object_put(jtasks);
+
+ return tasks;
+}
+
+static struct task *get_selected_task(GtkTreeView *treeview)
+{
+ GtkTreePath *path;
+ GtkTreeViewColumn *cols;
+ gint *i;
+ struct task *task;
+
+ gtk_tree_view_get_cursor(treeview, &path, &cols);
+
+ if (path) {
+ i = gtk_tree_path_get_indices(path);
+
+ if (i)
+ printf("row selected: %d\n", *i);
+
+ task = tasks[*i];
+
+ gtk_tree_path_free(path);
+
+ return task;
+ }
return NULL;
}
+static char *escape(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;
+ GtkTextBuffer *buf;
+ char *txt, *opts;
+ GtkTextIter sIter, eIter;
+
+ task = get_selected_task(GTK_TREE_VIEW(w_treeview));
+
+ printf("tasksave_clicked_cbk %d\n", task->id);
+
+ buf = gtk_text_view_get_buffer(w_description);
+
+ gtk_text_buffer_get_iter_at_offset(buf, &sIter, 0);
+ gtk_text_buffer_get_iter_at_offset(buf, &eIter, -1);
+ txt = gtk_text_buffer_get_text(buf, &sIter, &eIter, TRUE);
+
+ txt = escape(txt);
+
+ printf("%s\n", txt);
+
+ opts = malloc(1
+ + strlen(task->uuid)
+ + strlen(" modify description:\"")
+ + strlen(txt)
+ + strlen("\"")
+ + 1);
+ sprintf(opts, " %s modify \"%s\"", task->uuid, txt);
+
+ task_exec(opts);
+
+ return FALSE;
+}
+
+static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
+{
+ struct task *task;
+ GtkTextBuffer *buf;
+
+ printf("cursor_changed_cbk\n");
+
+ task = get_selected_task(treeview);
+
+ if (task) {
+ buf = gtk_text_view_get_buffer(w_description);
+ gtk_text_buffer_set_text(buf,
+ task->description,
+ strlen(task->description));
+
+ }
+
+ return FALSE;
+}
+
int main(int argc, char **argv)
{
- struct json_object *o;
+ GtkWidget *window;
+ GtkWidget *btn;
+ GtkBuilder *builder;
+ GtkTreeIter iter;
+ int i;
+ GtkTreeModel *model;
+ struct task **tasks_cur;
+
+ gtk_init(NULL, NULL);
+ builder = gtk_builder_new();
+ gtk_builder_add_from_file
+ (builder,
+ PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
+ NULL);
+ window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
+ printf("%p\n", window);
+
+ w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview"));
+
+ w_description = GTK_TEXT_VIEW(gtk_builder_get_object(builder,
+ "taskdescription"));
+
+ model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
+
+ tasks = get_all_tasks();
+
+ for (tasks_cur = tasks, i = 0; *tasks_cur; tasks_cur++, i++) {
+ gtk_list_store_append(GTK_LIST_STORE(model), &iter);
+ gtk_list_store_set(GTK_LIST_STORE(model),
+ &iter,
+ 0, (*tasks_cur)->id,
+ 1, (*tasks_cur)->description,
+ -1);
+ }
+
+ g_signal_connect(w_treeview,
+ "cursor-changed", (GCallback)cursor_changed_cbk, tasks);
+
+ btn = GTK_WIDGET(gtk_builder_get_object(builder, "tasksave"));
+ g_signal_connect(btn,
+ "clicked", (GCallback)tasksave_clicked_cbk, tasks);
+
+ g_object_unref(G_OBJECT(builder));
- o = task_exec_json("export");
+ gtk_widget_show_all(window);
- printf("%s\n", json_object_to_json_string(o));
+ gtk_main();
exit(EXIT_SUCCESS);
}