Imported Upstream version 0.0.7
[ptask-pkg-ubuntu.git] / src / ui_projecttree.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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <log.h>
24 #include <ui_projecttree.h>
25 #include <ui_tasktree.h>
26
27 enum {
28         COL_NAME,
29         COL_COUNT
30 };
31
32 static GtkTreeView *w_treeview;
33
34 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
35 {
36         const char *prj;
37
38         log_fct_enter();
39
40         prj = ui_projecttree_get_project();
41
42         ui_tasktree_update_filter(prj);
43
44         log_fct_exit();
45
46         return FALSE;
47 }
48
49 void ui_projecttree_init(GtkBuilder *builder)
50 {
51         w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder,
52                                                           "projecttree"));
53         g_signal_connect(w_treeview,
54                          "cursor-changed", (GCallback)cursor_changed_cbk,
55                          NULL);
56 }
57
58 const char *ui_projecttree_get_project()
59 {
60         GtkTreePath *path;
61         GtkTreeViewColumn *cols;
62         GtkTreeIter iter;
63         GtkTreeModel *model;
64         GValue value = {0,};
65         const char *prj;
66
67         log_fct_enter();
68
69         gtk_tree_view_get_cursor(w_treeview, &path, &cols);
70
71         if (path) {
72                 model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
73                 gtk_tree_model_get_iter(model, &iter, path);
74                 gtk_tree_model_get_value(model, &iter, COL_NAME, &value);
75
76                 prj = g_value_get_string(&value);
77
78                 if (!strcmp(prj, "ALL"))
79                         prj = NULL;
80         } else {
81                 prj = NULL;
82         }
83
84         log_fct_exit();
85
86         return prj;
87 }
88
89
90 void ui_projecttree_update(struct task **ts)
91 {
92         struct project **prjs, **cur;
93         GtkTreeModel *model;
94         GtkTreeIter iter;
95         GtkTreePath *p;
96         const char *current_prj;
97
98         log_fct_enter();
99
100         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
101
102         current_prj = ui_projecttree_get_project();
103
104         gtk_list_store_clear(GTK_LIST_STORE(model));
105
106         prjs = tw_get_projects(ts);
107         for (cur = prjs; *cur; cur++) {
108                 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
109
110                 gtk_list_store_set(GTK_LIST_STORE(model),
111                                    &iter,
112                                    COL_NAME, (*cur)->name,
113                                    COL_COUNT, (*cur)->count,
114                                    -1);
115
116                 if (current_prj) {
117                         if (!strcmp((*cur)->name, current_prj)) {
118                                 p = gtk_tree_model_get_path(model, &iter);
119                                 if (p) {
120                                         gtk_tree_view_set_cursor(w_treeview,
121                                                                  p,
122                                                                  NULL,
123                                                                  FALSE);
124                                 }
125                         }
126                 }
127         }
128
129         tw_project_list_free(prjs);
130
131         log_fct_exit();
132 }
133