(no commit message)
[ptask.git] / src / main.c
1 /*
2  * Copyright (C) 2010-2012 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <json/json.h>
24
25 #include <gtk/gtk.h>
26
27 #include "tw.h"
28
29 static struct task **tasks;
30 static GtkTextView *w_note;
31 static GtkEntry *w_description;
32 static GtkTreeView *w_treeview;
33
34 static struct task *get_selected_task(GtkTreeView *treeview)
35 {
36         GtkTreePath *path;
37         GtkTreeViewColumn *cols;
38         gint *i;
39         struct task *task;
40
41         gtk_tree_view_get_cursor(treeview, &path, &cols);
42
43         if (path) {
44                 i = gtk_tree_path_get_indices(path);
45                 
46                 if (i)
47                         printf("row selected: %d\n", *i);
48
49                 task = tasks[*i];
50
51                 gtk_tree_path_free(path);
52
53                 return task;
54         }
55
56         return NULL;
57 }
58
59 static int tasksave_clicked_cbk(GtkButton *btn, gpointer data)
60 {
61         struct task *task;
62         GtkTextBuffer *buf;
63         char *txt, *opts;
64         GtkTextIter sIter, eIter;
65         const char *ctxt;
66
67         task = get_selected_task(GTK_TREE_VIEW(w_treeview));
68
69         printf("tasksave_clicked_cbk %d\n", task->id);  
70
71         if (task->note) {
72                 buf = gtk_text_view_get_buffer(w_note);
73
74                 gtk_text_buffer_get_iter_at_offset(buf, &sIter, 0);
75                 gtk_text_buffer_get_iter_at_offset(buf, &eIter, -1);
76                 txt = gtk_text_buffer_get_text(buf, &sIter, &eIter, TRUE);
77
78                 txt = escape(txt);
79
80                 printf("%s\n", txt);
81         }
82
83         ctxt = gtk_entry_get_text(w_description);
84         txt = escape(ctxt);
85
86         opts = malloc(1
87                       + strlen(task->uuid)
88                       + strlen(" modify description:\"")
89                       + strlen(txt)
90                       + strlen("\"")
91                       + 1);
92         sprintf(opts, " %s modify \"%s\"", task->uuid, txt);
93         
94         task_exec(opts);
95
96         free(txt);
97         
98         return FALSE;
99 }
100
101 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
102 {
103         struct task *task;
104         GtkTextBuffer *buf;
105
106         printf("cursor_changed_cbk\n");
107
108         task = get_selected_task(treeview);
109
110         if (task) {
111
112                 if (task->note) {
113                         buf = gtk_text_view_get_buffer(w_note);
114                         gtk_text_buffer_set_text(buf,
115                                                  task->note,
116                                                  strlen(task->note));
117                 }
118
119                 gtk_entry_set_text(w_description, task->description);
120         }
121
122         return FALSE;
123 }
124
125 int main(int argc, char **argv)
126 {
127         GtkWidget *window;
128         GtkWidget *btn;
129         GtkBuilder *builder;
130         GtkTreeIter iter;
131         int i;
132         GtkTreeModel *model;
133         struct task **tasks_cur;
134         struct task *task;
135
136         gtk_init(NULL, NULL);
137         builder = gtk_builder_new();
138         gtk_builder_add_from_file
139                 (builder,
140                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "gtask.glade",
141                  NULL);
142         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
143         printf("%p\n", window);
144
145         w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview"));
146
147         w_note = GTK_TEXT_VIEW(gtk_builder_get_object(builder, "tasknote"));
148
149         w_description = GTK_ENTRY(gtk_builder_get_object(builder,
150                                                          "taskdescription"));
151
152         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
153
154         tasks = get_all_tasks();
155
156         for (tasks_cur = tasks, i = 0; *tasks_cur; tasks_cur++, i++) {
157                 task = (*tasks_cur);
158
159                 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
160                 
161                 if (task->project)
162                         gtk_list_store_set(GTK_LIST_STORE(model),
163                                            &iter,
164                                            2, task->project,
165                                            -1);
166
167                 gtk_list_store_set(GTK_LIST_STORE(model),
168                                    &iter,
169                                    0, (*tasks_cur)->id,
170                                    1, (*tasks_cur)->description,
171                                    -1);
172         }
173
174         g_signal_connect(w_treeview,
175                          "cursor-changed", (GCallback)cursor_changed_cbk, tasks);
176
177         btn = GTK_WIDGET(gtk_builder_get_object(builder, "tasksave"));
178         g_signal_connect(btn,
179                          "clicked", (GCallback)tasksave_clicked_cbk, tasks);
180
181         g_object_unref(G_OBJECT(builder));
182
183         gtk_widget_show_all(window);
184
185         gtk_main();
186
187         exit(EXIT_SUCCESS);
188 }