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