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