c1789f39aeb1049f5806e83e17cfe1b80d36067e
[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 <gtk/gtk.h>
20
21 #include <log.h>
22
23 static GtkTreeView *w_treeview;
24
25 enum {
26         COL_ID,
27         COL_DESCRIPTION,
28         COL_PROJECT,
29         COL_UUID,
30         COL_PRIORITY
31 };
32
33 static int priority_to_int(const char *str)
34 {
35         switch (*str) {
36         case 'H':
37                 return 3;
38         case 'M':
39                 return 2;
40         case 'L':
41                 return 1;
42         default:
43                 return 0;
44         }
45 }
46
47 static gint priority_cmp(GtkTreeModel *model,
48                          GtkTreeIter *a,
49                          GtkTreeIter *b,
50                          gpointer user_data)
51 {
52         GValue v1 = {0,}, v2 = {0,};
53         const char *str1, *str2;
54         int i1, i2;
55
56         gtk_tree_model_get_value(model, a, COL_PRIORITY, &v1);
57         str1 = g_value_get_string(&v1);
58         i1 = priority_to_int(str1);
59
60         gtk_tree_model_get_value(model, b, COL_PRIORITY, &v2);
61         str2 = g_value_get_string(&v2);
62         i2 = priority_to_int(str2);
63
64         if (i1 < i2)
65                 return -1;
66         else if (i1 > i2)
67                 return 1;
68         else
69                 return 0;
70 }
71
72 void ui_tasktree_init(GtkBuilder *builder)
73 {
74         GtkTreeModel *model;
75
76         w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "tasktree"));
77
78         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
79         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(model),
80                                         COL_PRIORITY,
81                                         priority_cmp,
82                                         NULL,
83                                         NULL);
84 }
85
86 void ui_tasktree_load_settings(GSettings *settings)
87 {
88         int sort_col_id;
89         GtkSortType sort_order;
90         GtkTreeModel *model;
91
92         sort_col_id = g_settings_get_int(settings, "tasks-sort-col");
93         sort_order = g_settings_get_int(settings, "tasks-sort-order");
94         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
95         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
96                                              sort_col_id, sort_order);
97 }
98
99 void ui_tasktree_save_settings(GSettings *settings)
100 {
101         int sort_col_id;
102         GtkTreeModel *model;
103         GtkSortType sort_order;
104
105         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
106         gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(model),
107                                              &sort_col_id,
108                                              &sort_order);
109         log_debug("ui_tasktree_save_settings(): sort_col_id=%d", sort_col_id);
110         log_debug("ui_tasktree_save_settings(): sort_col_order=%d", sort_order);
111
112         g_settings_set_int(settings, "tasks-sort-col", sort_col_id);
113         g_settings_set_int(settings, "tasks-sort-order", sort_order);
114 }