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