added urgency column
[ptask.git] / src / ui_tasktree.c
1 /*
2  * Copyright (C) 2012-2013 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 <string.h>
20
21 #include <gtk/gtk.h>
22
23 #include <log.h>
24 #include <ui_projecttree.h>
25 #include <ui_tasktree.h>
26
27 static GtkTreeView *w_treeview;
28 static struct task **current_tasks;
29
30 enum {
31         COL_ID,
32         COL_DESCRIPTION,
33         COL_PROJECT,
34         COL_UUID,
35         COL_PRIORITY,
36         COL_URGENCY
37 };
38
39 static int priority_to_int(const char *str)
40 {
41         switch (*str) {
42         case 'H':
43                 return 3;
44         case 'M':
45                 return 2;
46         case 'L':
47                 return 1;
48         default:
49                 return 0;
50         }
51 }
52
53 static gint priority_cmp(GtkTreeModel *model,
54                          GtkTreeIter *a,
55                          GtkTreeIter *b,
56                          gpointer user_data)
57 {
58         GValue v1 = {0,}, v2 = {0,};
59         const char *str1, *str2;
60         int i1, i2;
61
62         gtk_tree_model_get_value(model, a, COL_PRIORITY, &v1);
63         str1 = g_value_get_string(&v1);
64         i1 = priority_to_int(str1);
65
66         gtk_tree_model_get_value(model, b, COL_PRIORITY, &v2);
67         str2 = g_value_get_string(&v2);
68         i2 = priority_to_int(str2);
69
70         if (i1 < i2)
71                 return -1;
72         else if (i1 > i2)
73                 return 1;
74         else
75                 return 0;
76 }
77
78 void ui_tasktree_init(GtkBuilder *builder)
79 {
80         GtkTreeModel *model;
81
82         w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "tasktree"));
83
84         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
85         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(model),
86                                         COL_PRIORITY,
87                                         priority_cmp,
88                                         NULL,
89                                         NULL);
90 }
91
92 void ui_tasktree_load_settings(GSettings *settings)
93 {
94         int sort_col_id;
95         GtkSortType sort_order;
96         GtkTreeModel *model;
97
98         sort_col_id = g_settings_get_int(settings, "tasks-sort-col");
99         sort_order = g_settings_get_int(settings, "tasks-sort-order");
100         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
101         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
102                                              sort_col_id, sort_order);
103 }
104
105 void ui_tasktree_save_settings(GSettings *settings)
106 {
107         int sort_col_id;
108         GtkTreeModel *model;
109         GtkSortType sort_order;
110
111         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
112         gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(model),
113                                              &sort_col_id,
114                                              &sort_order);
115         log_debug("ui_tasktree_save_settings(): sort_col_id=%d", sort_col_id);
116         log_debug("ui_tasktree_save_settings(): sort_col_order=%d", sort_order);
117
118         g_settings_set_int(settings, "tasks-sort-col", sort_col_id);
119         g_settings_set_int(settings, "tasks-sort-order", sort_order);
120 }
121
122 const char *ui_tasktree_get_task_uuid()
123 {
124         struct task *t;
125
126         t = ui_tasktree_get_selected_task();
127
128         if (t)
129                 return t->uuid;
130         else
131                 return NULL;
132 }
133
134 struct task *ui_tasktree_get_selected_task()
135 {
136         GtkTreePath *path;
137         GtkTreeViewColumn *cols;
138         struct task **tasks_cur, *result;
139         GtkTreeIter iter;
140         GtkTreeModel *model;
141         GValue value = {0,};
142         const char *uuid;
143
144         log_fct_enter();
145
146         result = NULL;
147
148         if (current_tasks) {
149                 gtk_tree_view_get_cursor(w_treeview, &path, &cols);
150
151                 if (path) {
152                         model = gtk_tree_view_get_model(w_treeview);
153                         gtk_tree_model_get_iter(model, &iter, path);
154                         gtk_tree_model_get_value(model,
155                                                  &iter,
156                                                  COL_UUID,
157                                                  &value);
158
159                         uuid = g_value_get_string(&value);
160
161                         for (tasks_cur = current_tasks; *tasks_cur; tasks_cur++)
162                                 if (!strcmp((*tasks_cur)->uuid, uuid))
163                                         result = *tasks_cur;
164
165                         gtk_tree_path_free(path);
166                 }
167         }
168
169         log_fct_exit();
170
171         return result;
172 }
173
174 void ui_tasktree_set_selected_task(const char *uuid)
175 {
176         GtkTreePath *path;
177         GtkTreeIter iter;
178         GtkTreeModel *model;
179         GValue value = {0,};
180         const char *c_uuid;
181
182         log_fct_enter();
183
184         if (current_tasks) {
185                 model = gtk_tree_view_get_model(w_treeview);
186
187                 if (!gtk_tree_model_get_iter_first(model, &iter))
188                         return ;
189
190                 path = NULL;
191                 while (gtk_tree_model_iter_next(model, &iter)) {
192                         gtk_tree_model_get_value(model,
193                                                  &iter,
194                                                  COL_UUID,
195                                                  &value);
196                         c_uuid = g_value_get_string(&value);
197
198                         if (!strcmp(uuid, c_uuid)) {
199                                 path = gtk_tree_model_get_path(model, &iter);
200                                 break;
201                         }
202
203                         g_value_unset(&value);
204                 }
205
206                 if (!path)
207                         path = gtk_tree_path_new_first();
208                 gtk_tree_view_set_cursor(w_treeview, path, NULL, FALSE);
209         }
210
211         log_fct_exit();
212 }
213
214
215 void ui_tasktree_update(struct task **tasks, const char *prj_filter)
216 {
217         GtkTreeModel *model;
218         struct task **tasks_cur;
219         struct task *task;
220         GtkTreeIter iter;
221         const char *prj;
222
223         current_tasks = tasks;
224
225         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
226         gtk_list_store_clear(GTK_LIST_STORE(model));
227
228         if (current_tasks) {
229                 for (tasks_cur = current_tasks; *tasks_cur; tasks_cur++) {
230                         task = (*tasks_cur);
231
232                         if (task->project)
233                                 prj = task->project;
234                         else
235                                 prj = "";
236
237                         if (prj_filter && strcmp(prj, prj_filter))
238                                 continue;
239
240                         gtk_list_store_append(GTK_LIST_STORE(model), &iter);
241
242                         gtk_list_store_set(GTK_LIST_STORE(model),
243                                            &iter,
244                                            COL_ID, (*tasks_cur)->id,
245                                            COL_DESCRIPTION,
246                                            (*tasks_cur)->description,
247                                            COL_PROJECT, prj,
248                                            COL_UUID, (*tasks_cur)->uuid,
249                                            COL_PRIORITY, (*tasks_cur)->priority,
250                                            COL_URGENCY, (*tasks_cur)->urgency,
251                                            -1);
252                 }
253         }
254
255 }
256
257 void ui_tasktree_update_filter(const char *prj_filter)
258 {
259         ui_tasktree_update(current_tasks, prj_filter);
260 }