hide/show sensors in the main window.
[psensor.git] / src / ui_sensorpref.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
21 #include <gtk/gtk.h>
22
23 #include "cfg.h"
24 #include "ui_pref.h"
25 #include "ui_sensorlist.h"
26 #include "ui_sensorpref.h"
27 #include "ui_color.h"
28
29 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
30 #include "ui_appindicator.h"
31 #endif
32
33 enum {
34         COL_NAME = 0,
35         COL_SENSOR_PREF
36 };
37
38 struct sensor_pref {
39         struct psensor *sensor;
40         char *name;
41         int graph_enabled;
42         struct color *color;
43         int alarm_enabled;
44         int alarm_high_threshold;
45         int alarm_low_threshold;
46         unsigned int appindicator_enabled;
47         unsigned int appindicator_label_enabled;
48         unsigned int display_enabled;
49 };
50
51 struct cb_data {
52         struct ui_psensor *ui;
53         GtkBuilder *builder;
54 };
55
56 static struct sensor_pref *
57 sensor_pref_new(struct psensor *s, struct config *cfg)
58 {
59         struct sensor_pref *p;
60
61         p = malloc(sizeof(struct sensor_pref));
62
63         p->sensor = s;
64         p->name = strdup(s->name);
65         p->graph_enabled = s->graph_enabled;
66         p->alarm_enabled = s->alarm_enabled;
67         p->color = color_dup(s->color);
68         p->display_enabled = config_is_sensor_enabled(s->id);
69
70         if (cfg->temperature_unit == CELSIUS) {
71                 p->alarm_high_threshold = s->alarm_high_threshold;
72                 p->alarm_low_threshold = s->alarm_low_threshold;
73         } else {
74                 p->alarm_high_threshold
75                         = celsius_to_fahrenheit(s->alarm_high_threshold);
76                 p->alarm_low_threshold
77                         = celsius_to_fahrenheit(s->alarm_low_threshold);
78         }
79
80         p->appindicator_enabled = s->appindicator_enabled;
81         p->appindicator_label_enabled
82                 = config_is_appindicator_label_enabled(s->id);
83
84         return p;
85 }
86
87 static void sensor_pref_free(struct sensor_pref *p)
88 {
89         if (!p)
90                 return ;
91
92         free(p->name);
93         free(p->color);
94
95         free(p);
96 }
97
98 static struct sensor_pref *get_selected_sensor_pref(GtkTreeView *tree)
99 {
100         GtkTreeModel *model;
101         GtkTreeIter iter;
102         struct sensor_pref *pref;
103         GtkTreeSelection *selection;
104
105         selection = gtk_tree_view_get_selection(tree);
106
107         pref = NULL;
108         if (gtk_tree_selection_get_selected(selection, &model, &iter))
109                 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &pref, -1);
110
111         return pref;
112 }
113
114 void ui_sensorpref_name_changed_cb(GtkEntry *entry, gpointer data)
115 {
116         struct sensor_pref *p;
117         const char *str;
118
119         str = gtk_entry_get_text(entry);
120
121         p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
122
123         if (p && strcmp(p->name, str)) {
124                 free(p->name);
125                 p->name = strdup(str);
126         }
127 }
128
129 void ui_sensorpref_draw_toggled_cb(GtkToggleButton *btn, gpointer data)
130 {
131         struct sensor_pref *p;
132
133         p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
134
135         if (p)
136                 p->graph_enabled = gtk_toggle_button_get_active(btn);
137 }
138
139 void ui_sensorpref_display_toggled_cb(GtkToggleButton *btn, gpointer data)
140 {
141         struct sensor_pref *p;
142
143         p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
144
145         if (p)
146                 p->display_enabled = gtk_toggle_button_get_active(btn);
147 }
148
149 void ui_sensorpref_alarm_toggled_cb(GtkToggleButton *btn, gpointer data)
150 {
151         struct sensor_pref *p;
152
153         p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
154
155         if (p)
156                 p->alarm_enabled = gtk_toggle_button_get_active(btn);
157 }
158
159 void
160 ui_sensorpref_appindicator_menu_toggled_cb(GtkToggleButton *btn, gpointer data)
161 {
162         struct sensor_pref *p;
163
164         p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
165
166         if (p)
167                 p->appindicator_enabled = gtk_toggle_button_get_active(btn);
168 }
169
170 void
171 ui_sensorpref_appindicator_label_toggled_cb(GtkToggleButton *btn, gpointer data)
172 {
173         struct sensor_pref *p;
174
175         p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
176
177         if (p)
178                 p->appindicator_label_enabled
179                         = gtk_toggle_button_get_active(btn);
180 }
181
182 void ui_sensorpref_color_set_cb(GtkColorButton *widget, gpointer data)
183 {
184         struct sensor_pref *p;
185         GdkColor color;
186
187         p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
188
189         if (p) {
190                 gtk_color_button_get_color(widget, &color);
191                 color_set(p->color, color.red, color.green, color.blue);
192         }
193 }
194
195 void
196 ui_sensorpref_alarm_high_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
197 {
198         struct sensor_pref *p;
199
200         p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
201
202         if (p)
203                 p->alarm_high_threshold = gtk_spin_button_get_value(btn);
204 }
205
206 void
207 ui_sensorpref_alarm_low_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
208 {
209         struct sensor_pref *p;
210
211         p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
212
213         if (p)
214                 p->alarm_low_threshold = gtk_spin_button_get_value(btn);
215 }
216
217 static void
218 update_pref(struct sensor_pref *p, struct config *cfg, GtkBuilder *builder)
219 {
220         GtkLabel *w_id, *w_type, *w_high_threshold_unit, *w_low_threshold_unit,
221                 *w_chipname;
222         GtkEntry *w_name;
223         GtkToggleButton *w_draw, *w_alarm, *w_appindicator_enabled,
224                 *w_appindicator_label_enabled, *w_display;
225         GtkColorButton *w_color;
226         GtkSpinButton *w_high_threshold, *w_low_threshold;
227         GdkColor *color;
228         struct psensor *s;
229         int use_celsius;
230
231         s = p->sensor;
232
233         w_id = GTK_LABEL(gtk_builder_get_object(builder, "sensor_id"));
234         gtk_label_set_text(w_id, s->id);
235
236         w_type = GTK_LABEL(gtk_builder_get_object(builder, "sensor_type"));
237         gtk_label_set_text(w_type, psensor_type_to_str(s->type));
238
239         w_name = GTK_ENTRY(gtk_builder_get_object(builder, "sensor_name"));
240         gtk_entry_set_text(w_name, p->name);
241
242         w_chipname = GTK_LABEL(gtk_builder_get_object(builder, "chip_name"));
243         if (s->chip)
244                 gtk_label_set_text(w_chipname, s->chip);
245         else
246                 gtk_label_set_text(w_chipname, _("Unknown"));
247
248         w_draw = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
249                                                           "sensor_draw"));
250         gtk_toggle_button_set_active(w_draw, p->graph_enabled);
251
252         w_display = GTK_TOGGLE_BUTTON(gtk_builder_get_object
253                                       (builder,
254                                        "sensor_enable_checkbox"));
255         gtk_toggle_button_set_active(w_display, p->display_enabled);
256
257         color = color_to_gdkcolor(p->color);
258         w_color = GTK_COLOR_BUTTON(gtk_builder_get_object(builder,
259                                                           "sensor_color"));
260         gtk_color_button_set_color(w_color, color);
261
262         w_alarm = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
263                                                            "sensor_alarm"));
264         w_high_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
265                                           (builder,
266                                            "sensor_alarm_high_threshold"));
267         w_low_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
268                                          (builder,
269                                           "sensor_alarm_low_threshold"));
270
271         w_high_threshold_unit = GTK_LABEL(gtk_builder_get_object
272                                          (builder,
273                                           "sensor_alarm_high_threshold_unit"));
274         w_low_threshold_unit = GTK_LABEL(gtk_builder_get_object
275                                         (builder,
276                                          "sensor_alarm_low_threshold_unit"));
277
278         use_celsius = cfg->temperature_unit == CELSIUS ? 1 : 0;
279         gtk_label_set_text(w_high_threshold_unit,
280                            psensor_type_to_unit_str(s->type,
281                                                     use_celsius));
282         gtk_label_set_text(w_low_threshold_unit,
283                            psensor_type_to_unit_str(s->type,
284                                                     use_celsius));
285
286         w_appindicator_enabled = GTK_TOGGLE_BUTTON
287                 (gtk_builder_get_object(builder, "indicator_checkbox"));
288         w_appindicator_label_enabled = GTK_TOGGLE_BUTTON
289                 (gtk_builder_get_object(builder, "indicator_label_checkbox"));
290
291         if (is_temp_type(s->type) || is_fan_type(s->type)) {
292                 gtk_toggle_button_set_active(w_alarm, p->alarm_enabled);
293                 gtk_spin_button_set_value(w_high_threshold,
294                                           p->alarm_high_threshold);
295                 gtk_spin_button_set_value(w_low_threshold,
296                                           p->alarm_low_threshold);
297                 gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), TRUE);
298                 gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), TRUE);
299                 gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), TRUE);
300         } else {
301                 gtk_toggle_button_set_active(w_alarm, 0);
302                 gtk_spin_button_set_value(w_high_threshold, 0);
303                 gtk_spin_button_set_value(w_low_threshold, 0);
304                 gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), FALSE);
305                 gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), FALSE);
306                 gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), FALSE);
307         }
308
309         gtk_toggle_button_set_active(w_appindicator_enabled,
310                                      p->appindicator_enabled);
311
312         gtk_toggle_button_set_active(w_appindicator_label_enabled,
313                                      p->appindicator_label_enabled);
314 }
315
316 static void on_changed(GtkTreeSelection *selection, gpointer data)
317 {
318         struct cb_data *cbdata = data;
319         struct ui_psensor *ui = cbdata->ui;
320         struct sensor_pref *p;
321         GtkTreeView *tree;
322
323         tree = GTK_TREE_VIEW(gtk_builder_get_object(cbdata->builder,
324                                                     "sensors_list"));
325         p = get_selected_sensor_pref(tree);
326         update_pref(p, ui->config, cbdata->builder);
327 }
328
329 static void
330 select_sensor(struct psensor *s, struct psensor **sensors, GtkTreeView *tree)
331 {
332         struct psensor **s_cur;
333         int i;
334         GtkTreePath *p;
335
336         p = NULL;
337         for (s_cur = sensors, i = 0; *s_cur; s_cur++, i++)
338                 if (s == *s_cur) {
339                         p = gtk_tree_path_new_from_indices(i, -1);
340                         break;
341                 }
342
343         if (p) {
344                 GtkTreeSelection *s = gtk_tree_view_get_selection(tree);
345
346                 gtk_tree_selection_select_path(s, p);
347                 gtk_tree_path_free(p);
348         }
349 }
350
351 static void apply_pref(struct sensor_pref *p, int pos, struct config *cfg)
352 {
353         struct psensor *s;
354
355         s = p->sensor;
356
357         if (strcmp(p->name, s->name)) {
358                 free(s->name);
359                 s->name = strdup(p->name);
360                 config_set_sensor_name(s->id, s->name);
361         }
362
363         if (s->graph_enabled != p->graph_enabled) {
364                 s->graph_enabled = p->graph_enabled;
365                 config_set_sensor_graph_enabled(s->id, s->graph_enabled);
366         }
367
368         if (is_temp_type(s->type) && cfg->temperature_unit == FAHRENHEIT) {
369                 s->alarm_high_threshold
370                         = fahrenheit_to_celsius(p->alarm_high_threshold);
371                 s->alarm_low_threshold
372                         = fahrenheit_to_celsius(p->alarm_low_threshold);
373         } else {
374                 s->alarm_high_threshold = p->alarm_high_threshold;
375                 s->alarm_low_threshold = p->alarm_low_threshold;
376         }
377
378         config_set_sensor_alarm_high_threshold(s->id, s->alarm_high_threshold);
379         config_set_sensor_alarm_low_threshold(s->id, s->alarm_low_threshold);
380
381         if (s->alarm_enabled != p->alarm_enabled) {
382                 s->alarm_enabled = p->alarm_enabled;
383                 config_set_sensor_alarm_enabled(s->id, s->alarm_enabled);
384         }
385
386         color_set(s->color, p->color->red, p->color->green, p->color->blue);
387         config_set_sensor_color(s->id, s->color);
388
389         if (s->appindicator_enabled != p->appindicator_enabled) {
390                 s->appindicator_enabled = p->appindicator_enabled;
391                 config_set_appindicator_enabled(s->id, s->appindicator_enabled);
392         }
393
394         config_set_appindicator_label_enabled(s->id,
395                                               p->appindicator_label_enabled);
396
397         config_set_sensor_position(s->id, pos);
398
399         config_set_sensor_enabled(s->id, p->display_enabled);
400 }
401
402 static void apply_prefs(GtkTreeModel *model, struct config *cfg)
403 {
404         gboolean valid;
405         struct sensor_pref *spref;
406         GtkTreeIter iter;
407         int i;
408
409         valid = gtk_tree_model_get_iter_first(model, &iter);
410         i = 0;
411         while (valid) {
412                 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
413                 apply_pref(spref, i, cfg);
414                 valid = gtk_tree_model_iter_next(model, &iter);
415                 i++;
416         }
417         config_sync();
418 }
419
420 void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
421 {
422         GtkDialog *diag;
423         gint result;
424         guint ok;
425         GtkBuilder *builder;
426         GError *error;
427         GtkTreeView *w_sensors_list;
428         GtkListStore *store;
429         struct psensor **s_cur, *s, **ordered_sensors;
430         GtkTreeSelection *selection;
431         struct cb_data cbdata;
432         GtkTreeIter iter;
433         struct sensor_pref *spref;
434         gboolean valid;
435         GtkTreeModel *model;
436
437         cbdata.ui = ui;
438
439         builder = gtk_builder_new();
440         cbdata.builder = builder;
441
442         error = NULL;
443         ok = gtk_builder_add_from_file
444                 (builder,
445                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "sensor-edit.glade",
446                  &error);
447
448         if (!ok) {
449                 log_printf(LOG_ERR, error->message);
450                 g_error_free(error);
451                 return ;
452         }
453
454         w_sensors_list
455                 = GTK_TREE_VIEW(gtk_builder_get_object(builder,
456                                                        "sensors_list"));
457         gtk_builder_connect_signals(builder, w_sensors_list);
458
459         store = GTK_LIST_STORE(gtk_builder_get_object(builder,
460                                                       "sensors_liststore"));
461
462         ordered_sensors = ui_get_sensors_ordered_by_position(ui);
463         for (s_cur = ordered_sensors; *s_cur; s_cur++) {
464                 s = *s_cur;
465                 gtk_list_store_append(store, &iter);
466
467                 spref = sensor_pref_new(s, ui->config);
468                 gtk_list_store_set(store, &iter,
469                                    COL_NAME, s->name,
470                                    COL_SENSOR_PREF, spref,
471                                    -1);
472
473                 if (s == sensor)
474                         update_pref(spref, ui->config, builder);
475         }
476
477         selection = gtk_tree_view_get_selection(w_sensors_list);
478         g_signal_connect(selection, "changed", G_CALLBACK(on_changed), &cbdata);
479         select_sensor(sensor, ordered_sensors, w_sensors_list);
480
481         free(ordered_sensors);
482
483         diag = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
484         result = gtk_dialog_run(diag);
485
486         model = gtk_tree_view_get_model(w_sensors_list);
487
488         if (result == GTK_RESPONSE_ACCEPT) {
489                 apply_prefs(model, ui->config);
490                 ui_sensorlist_update(ui, 1);
491 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
492                 ui_appindicator_update_menu(ui);
493 #endif
494         }
495
496         valid = gtk_tree_model_get_iter_first(model, &iter);
497         while (valid) {
498                 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
499                 sensor_pref_free(spref);
500                 valid = gtk_tree_model_iter_next(model, &iter);
501         }
502
503         g_object_unref(G_OBJECT(builder));
504
505         gtk_widget_destroy(GTK_WIDGET(diag));
506 }