2 * Copyright (C) 2010-2014 jeanfi@gmail.com
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.
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.
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
24 #include <temperature.h>
25 #include <ui_appindicator.h>
28 #include <ui_sensorlist.h>
29 #include <ui_sensorpref.h>
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;
56 /* 'true' when the notifications of field changes are due to the change
57 * of the selected sensor. */
58 static bool ignore_changes;
60 static struct psensor *get_selected_sensor(void)
65 GtkTreeSelection *selection;
67 selection = gtk_tree_view_get_selection(w_sensors_list);
70 if (gtk_tree_selection_get_selected(selection, &model, &iter))
71 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &s, -1);
76 static void apply_config(struct ui_psensor *ui)
80 ui_sensorlist_update(ui, 1);
81 ui_appindicator_update_menu(ui);
84 void ui_sensorpref_name_changed_cb(GtkEntry *entry, gpointer data)
93 s = get_selected_sensor();
98 str = gtk_entry_get_text(entry);
100 if (strcmp(str, s->name)) {
102 s->name = strdup(str);
103 config_set_sensor_name(s->id, str);
105 apply_config((struct ui_psensor *)data);
109 void ui_sensorpref_draw_toggled_cb(GtkToggleButton *btn, gpointer data)
117 s = get_selected_sensor();
122 active = gtk_toggle_button_get_active(btn);
123 config_set_sensor_graph_enabled(s->id, active);
125 apply_config((struct ui_psensor *)data);
128 void ui_sensorpref_display_toggled_cb(GtkToggleButton *btn, gpointer data)
136 s = get_selected_sensor();
141 active = gtk_toggle_button_get_active(btn);
142 config_set_sensor_enabled(s->id, active);
144 apply_config((struct ui_psensor *)data);
147 void ui_sensorpref_alarm_toggled_cb(GtkToggleButton *btn, gpointer data)
155 s = get_selected_sensor();
160 active = gtk_toggle_button_get_active(btn);
161 config_set_sensor_alarm_enabled(s->id, active);
163 apply_config((struct ui_psensor *)data);
167 ui_sensorpref_appindicator_menu_toggled_cb(GtkToggleButton *btn, gpointer data)
175 s = get_selected_sensor();
180 active = gtk_toggle_button_get_active(btn);
181 config_set_appindicator_enabled(s->id, active);
183 apply_config((struct ui_psensor *)data);
187 ui_sensorpref_appindicator_label_toggled_cb(GtkToggleButton *btn, gpointer data)
195 s = get_selected_sensor();
200 active = gtk_toggle_button_get_active(btn);
201 config_set_appindicator_label_enabled(s->id, active);
203 apply_config((struct ui_psensor *)data);
206 void ui_sensorpref_color_set_cb(GtkColorButton *widget, gpointer data)
214 s = get_selected_sensor();
218 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), &color);
219 config_set_sensor_color(s->id, &color);
221 apply_config((struct ui_psensor *)data);
225 ui_sensorpref_alarm_high_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
233 s = get_selected_sensor();
237 v = gtk_spin_button_get_value(btn);
238 if (config_get_sensor_unit() == FAHRENHEIT)
239 v = fahrenheit_to_celsius(v);
241 config_set_sensor_alarm_high_threshold(s->id, v);
242 s->alarm_high_threshold = v;
244 apply_config((struct ui_psensor *)data);
248 ui_sensorpref_alarm_low_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
256 s = get_selected_sensor();
260 v = gtk_spin_button_get_value(btn);
261 if (config_get_sensor_unit() == FAHRENHEIT)
262 v = fahrenheit_to_celsius(v);
264 config_set_sensor_alarm_low_threshold(s->id, v);
265 s->alarm_low_threshold = v;
267 apply_config((struct ui_psensor *)data);
270 static void update_pref(struct psensor *s)
272 int use_celsius, threshold;
280 ignore_changes = true;
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);
290 gtk_label_set_text(w_sensor_chipname, chip);
292 use_celsius = config_get_sensor_unit() == CELSIUS ? 1 : 0;
294 if (s->min == UNKNOWN_DBL_VALUE)
295 smin = strdup(_("Unknown"));
297 smin = psensor_value_to_str(s->type, s->min, use_celsius);
299 gtk_label_set_text(w_sensor_min, smin);
302 if (s->max == UNKNOWN_DBL_VALUE)
303 smax = strdup(_("Unknown"));
305 smax = psensor_value_to_str(s->type, s->max, use_celsius);
306 gtk_label_set_text(w_sensor_max, smax);
309 gtk_toggle_button_set_active(w_sensor_draw,
310 config_is_sensor_graph_enabled(s->id));
312 gtk_toggle_button_set_active(w_sensor_display,
313 config_is_sensor_enabled(s->id));
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);
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));
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);
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);
340 gtk_toggle_button_set_active(w_sensor_alarm,
341 config_get_sensor_alarm_enabled(s->id));
343 threshold = s->alarm_high_threshold;
345 threshold = celsius_to_fahrenheit(threshold);
346 gtk_spin_button_set_value(w_sensor_high_threshold, threshold);
348 threshold = s->alarm_low_threshold;
350 threshold = celsius_to_fahrenheit(threshold);
351 gtk_spin_button_set_value(w_sensor_low_threshold, threshold);
353 gtk_toggle_button_set_active(w_appindicator_enabled,
354 config_is_appindicator_enabled(s->id));
356 gtk_toggle_button_set_active
357 (w_appindicator_label_enabled,
358 config_is_appindicator_label_enabled(s->id));
360 ignore_changes = false;
364 ui_sensorpref_tree_selection_changed_cb(GtkTreeSelection *sel, gpointer data)
366 update_pref(get_selected_sensor());
369 static void select_sensor(struct psensor *s, struct psensor **sensors)
371 struct psensor **s_cur;
374 GtkTreeSelection *sel;
376 for (s_cur = sensors, i = 0; *s_cur; s_cur++, i++)
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);
382 gtk_tree_path_free(p);
389 static void quit(void)
391 gtk_widget_destroy(GTK_WIDGET(w_dialog));
396 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
402 void ui_sensorpref_close_clicked_cb(GtkButton *btn, gpointer data)
407 static GtkBuilder *load_ui(struct ui_psensor *ui)
415 builder = gtk_builder_new();
416 ok = gtk_builder_add_from_file
418 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "sensor-edit.glade",
422 log_printf(LOG_ERR, error->message);
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"));
466 store = GTK_LIST_STORE(gtk_builder_get_object(builder,
467 "sensors_liststore"));
469 gtk_builder_connect_signals(builder, ui);
471 g_signal_connect(w_dialog,
473 G_CALLBACK(on_delete_event_cb),
479 static void populate(struct psensor *sensor, struct psensor **sensors)
482 struct psensor **s_cur, **ordered_sensors, *s;
484 gtk_list_store_clear(store);
486 ordered_sensors = ui_get_sensors_ordered_by_position(sensors);
487 for (s_cur = ordered_sensors; *s_cur; s_cur++) {
489 gtk_list_store_append(store, &iter);
491 gtk_list_store_set(store, &iter,
497 select_sensor(sensor, ordered_sensors);
499 free(ordered_sensors);
502 void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
506 if (w_dialog == NULL) {
507 builder = load_ui(ui);
512 g_object_unref(G_OBJECT(builder));
515 populate(sensor, ui->sensors);
517 gtk_window_present(GTK_WINDOW(w_dialog));