Merge tag 'upstream/1.0.3'
[psensor-pkg-debian.git] / src / ui_sensorlist.c
1 /*
2  * Copyright (C) 2010-2014 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 <stdlib.h>
20 #include <string.h>
21
22 #include "ui.h"
23 #include "ui_pref.h"
24 #include "ui_sensorlist.h"
25 #include "ui_sensorpref.h"
26 #include "cfg.h"
27 #include "ui_color.h"
28
29 enum {
30         COL_NAME = 0,
31         COL_TEMP,
32         COL_TEMP_MIN,
33         COL_TEMP_MAX,
34         COL_COLOR,
35         COL_COLOR_STR,
36         COL_ENABLED,
37         COL_EMPTY,
38         COL_SENSOR,
39 };
40
41 struct cb_data {
42         struct ui_psensor *ui;
43         struct psensor *sensor;
44 };
45
46 static int col_index_to_col(int idx)
47 {
48         if (idx == 5)
49                 return COL_ENABLED;
50         else if (idx > 5)
51                 return -1;
52
53         return idx;
54 }
55
56 static void populate(struct ui_psensor *ui)
57 {
58         GtkTreeIter iter;
59         GtkListStore *store;
60         GdkColor color;
61         char *scolor;
62         struct psensor **ordered_sensors, **s_cur, *s;
63
64         ordered_sensors = ui_get_sensors_ordered_by_position(ui);
65         store = ui->sensors_store;
66
67         gtk_list_store_clear(store);
68
69         for (s_cur = ordered_sensors; *s_cur; s_cur++) {
70                 s = *s_cur;
71
72                 gtk_list_store_append(store, &iter);
73
74                 color.red = s->color->red;
75                 color.green = s->color->green;
76                 color.blue = s->color->blue;
77
78                 scolor = gdk_color_to_string(&color);
79
80                 gtk_list_store_set(store, &iter,
81                                    COL_NAME, s->name,
82                                    COL_COLOR_STR, scolor,
83                                    COL_ENABLED, s->graph_enabled,
84                                    COL_SENSOR, s,
85                                    -1);
86                 free(scolor);
87         }
88         free(ordered_sensors);
89 }
90
91 void ui_sensorlist_update(struct ui_psensor *ui, bool complete)
92 {
93         char *value, *min, *max;
94         struct psensor *s;
95         GtkTreeIter iter;
96         GtkTreeModel *model;
97         gboolean valid;
98         int use_celsius;
99         GtkListStore *store;
100
101         if (complete)
102                 populate(ui);
103
104         model = gtk_tree_view_get_model(ui->sensors_tree);
105         store = ui->sensors_store;
106
107         use_celsius = ui->config->temperature_unit == CELSIUS;
108
109         valid = gtk_tree_model_get_iter_first(model, &iter);
110         while (valid) {
111                 gtk_tree_model_get(model, &iter, COL_SENSOR, &s, -1);
112
113                 value = psensor_value_to_str(s->type,
114                                              psensor_get_current_value(s),
115                                              use_celsius);
116                 min = psensor_value_to_str(s->type, s->min, use_celsius);
117                 max = psensor_value_to_str(s->type, s->max, use_celsius);
118
119                 gtk_list_store_set(store, &iter,
120                                    COL_TEMP, value,
121                                    COL_TEMP_MIN, min,
122                                    COL_TEMP_MAX, max,
123                                    -1);
124                 free(value);
125                 free(min);
126                 free(max);
127
128                 valid = gtk_tree_model_iter_next(model, &iter);
129         }
130 }
131
132 /*
133  * Returns the sensor corresponding to the x/y position
134  * in the table.
135  *
136  * <null> if none.
137  */
138 static struct psensor *
139 get_sensor_at_pos(GtkTreeView *view, int x, int y, struct ui_psensor *ui)
140 {
141         GtkTreePath *path;
142         GtkTreeModel *model;
143         GtkTreeIter iter;
144         struct psensor *s;
145
146         gtk_tree_view_get_path_at_pos(view, x, y, &path, NULL, NULL, NULL);
147         model = gtk_tree_view_get_model(ui->sensors_tree);
148
149         if (path) {
150                 if (gtk_tree_model_get_iter(model, &iter, path)) {
151                         gtk_tree_model_get(model, &iter, COL_SENSOR, &s, -1);
152                         return s;
153                 }
154         }
155         return NULL;
156 }
157
158 /*
159  * Returns the index of the column corresponding
160  * to the x position in the table.
161  *
162  * -1 if none
163  */
164 static int get_col_index_at_pos(GtkTreeView *view, int x)
165 {
166         GList *cols, *node;
167         int colx, coli;
168         GtkTreeViewColumn *checkcol;
169
170         cols = gtk_tree_view_get_columns(view);
171         colx = 0;
172         coli = 0;
173         for (node = cols; node; node = node->next) {
174                 checkcol = (GtkTreeViewColumn *)node->data;
175
176                 if (x >= colx
177                     && x < (colx + gtk_tree_view_column_get_width(checkcol)))
178                         return coli;
179                 else
180                         colx += gtk_tree_view_column_get_width(checkcol);
181
182                 coli++;
183         }
184
185         return -1;
186 }
187
188 static void preferences_activated_cbk(GtkWidget *menu_item, gpointer data)
189 {
190         struct cb_data *cb_data = data;
191
192         ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
193 }
194
195 static GtkWidget *
196 create_sensor_popup(struct ui_psensor *ui, struct psensor *sensor)
197 {
198         GtkWidget *menu, *item, *separator;
199         struct cb_data *data;
200
201         menu = gtk_menu_new();
202
203         item = gtk_menu_item_new_with_label(sensor->name);
204         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
205
206         separator = gtk_separator_menu_item_new();
207         gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
208
209         item = gtk_menu_item_new_with_label(_("Preferences"));
210         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
211
212         data = malloc(sizeof(struct cb_data));
213         data->ui = ui;
214         data->sensor = sensor;
215
216         g_signal_connect(item,
217                          "activate",
218                          G_CALLBACK(preferences_activated_cbk), data);
219
220         gtk_widget_show_all(menu);
221
222         return menu;
223 }
224
225 static int clicked_cbk(GtkWidget *widget, GdkEventButton *event, gpointer data)
226 {
227         GtkWidget *menu;
228         struct ui_psensor *ui;
229         GtkTreeView *view;
230         struct psensor *s;
231         int coli;
232
233         if (event->button != 3)
234                 return FALSE;
235
236         ui = (struct ui_psensor *)data;
237         view = ui->sensors_tree;
238
239         s = get_sensor_at_pos(view, event->x, event->y, ui);
240
241         if (s) {
242                 coli = col_index_to_col(get_col_index_at_pos(view, event->x));
243
244                 if (coli == COL_COLOR) {
245                         if (ui_change_color(_("Select foreground color"),
246                                             s->color,
247                                             GTK_WINDOW(ui->main_window))) {
248                                 ui_sensorlist_update(ui, 1);
249                                 config_set_sensor_color(s->id, s->color);
250                         }
251                 } else if (coli >= 0 && coli != COL_ENABLED) {
252                         menu = create_sensor_popup(ui, s);
253
254                         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
255                                        event->button, event->time);
256                 }
257
258         }
259         return TRUE;
260 }
261
262 static void
263 toggled_cbk(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
264 {
265         struct ui_psensor *ui;
266         GtkTreeModel *model;
267         GtkTreeIter iter;
268         GtkTreePath *path;
269         struct psensor *s;
270
271         ui = (struct ui_psensor *)data;
272         model = gtk_tree_view_get_model(ui->sensors_tree);
273         path = gtk_tree_path_new_from_string(path_str);
274
275         gtk_tree_model_get_iter(model, &iter, path);
276
277         gtk_tree_model_get(model, &iter, COL_SENSOR, &s, -1);
278
279         s->graph_enabled ^= 1;
280         config_set_sensor_enabled(s->id, s->graph_enabled);
281
282         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
283                            COL_ENABLED, s->graph_enabled, -1);
284
285         gtk_tree_path_free(path);
286 }
287
288 void ui_sensorlist_create(struct ui_psensor *ui)
289 {
290         GtkCellRenderer *renderer;
291
292         log_debug("ui_sensorlist_create()");
293
294         renderer = gtk_cell_renderer_text_new();
295         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
296                                                     -1,
297                                                     _("Sensor"),
298                                                     renderer,
299                                                     "text", COL_NAME, NULL);
300
301         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
302                                                     -1,
303                                                     _("Value"),
304                                                     renderer,
305                                                     "text", COL_TEMP, NULL);
306
307         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
308                                                     -1,
309                                                     _("Min"),
310                                                     renderer,
311                                                     "text", COL_TEMP_MIN, NULL);
312
313         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
314                                                     -1,
315                                                     _("Max"),
316                                                     renderer,
317                                                     "text", COL_TEMP_MAX, NULL);
318
319         renderer = gtk_cell_renderer_text_new();
320         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
321                                                     -1,
322                                                     _("Color"),
323                                                     renderer,
324                                                     "text", COL_COLOR,
325                                                     "background", COL_COLOR_STR,
326                                                     NULL);
327
328         g_signal_connect(ui->sensors_tree,
329                          "button-press-event", (GCallback)clicked_cbk, ui);
330
331         renderer = gtk_cell_renderer_toggle_new();
332         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
333                                                     -1,
334                                                     _("Graph"),
335                                                     renderer,
336                                                     "active", COL_ENABLED,
337                                                     NULL);
338         g_signal_connect(G_OBJECT(renderer),
339                          "toggled", (GCallback) toggled_cbk, ui);
340
341         renderer = gtk_cell_renderer_text_new();
342         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
343                                                     -1,
344                                                     "",
345                                                     renderer,
346                                                     "text", COL_EMPTY, NULL);
347
348         ui_sensorlist_update(ui, 1);
349 }