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>
27 #include <ui_sensorlist.h>
28 #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_low_threshold_unit;
42 static GtkLabel *w_sensor_high_threshold_unit;
43 static GtkEntry *w_sensor_name;
44 static GtkToggleButton *w_sensor_draw;
45 static GtkToggleButton *w_sensor_display;
46 static GtkToggleButton *w_sensor_alarm;
47 static GtkToggleButton *w_appindicator_enabled;
48 static GtkToggleButton *w_appindicator_label_enabled;
49 static GtkColorButton *w_sensor_color;
50 static GtkSpinButton *w_sensor_high_threshold;
51 static GtkSpinButton *w_sensor_low_threshold;
52 static GtkListStore *store;
55 /* 'true' when the notifications of field changes are due to the change
56 * of the selected sensor. */
57 static bool ignore_changes;
59 static struct psensor *get_selected_sensor(void)
64 GtkTreeSelection *selection;
66 selection = gtk_tree_view_get_selection(w_sensors_list);
69 if (gtk_tree_selection_get_selected(selection, &model, &iter))
70 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &s, -1);
75 static void apply_config(struct ui_psensor *ui)
79 ui_sensorlist_update(ui, 1);
80 ui_appindicator_update_menu(ui);
83 void ui_sensorpref_name_changed_cb(GtkEntry *entry, gpointer data)
92 s = get_selected_sensor();
97 str = gtk_entry_get_text(entry);
99 if (strcmp(str, s->name)) {
101 s->name = strdup(str);
102 config_set_sensor_name(s->id, str);
104 apply_config((struct ui_psensor *)data);
108 void ui_sensorpref_draw_toggled_cb(GtkToggleButton *btn, gpointer data)
116 s = get_selected_sensor();
121 active = gtk_toggle_button_get_active(btn);
122 config_set_sensor_graph_enabled(s->id, active);
124 apply_config((struct ui_psensor *)data);
127 void ui_sensorpref_display_toggled_cb(GtkToggleButton *btn, gpointer data)
135 s = get_selected_sensor();
140 active = gtk_toggle_button_get_active(btn);
141 config_set_sensor_enabled(s->id, active);
143 apply_config((struct ui_psensor *)data);
146 void ui_sensorpref_alarm_toggled_cb(GtkToggleButton *btn, gpointer data)
154 s = get_selected_sensor();
159 active = gtk_toggle_button_get_active(btn);
160 config_set_sensor_alarm_enabled(s->id, active);
162 apply_config((struct ui_psensor *)data);
166 ui_sensorpref_appindicator_menu_toggled_cb(GtkToggleButton *btn, gpointer data)
174 s = get_selected_sensor();
179 active = gtk_toggle_button_get_active(btn);
180 config_set_appindicator_enabled(s->id, active);
182 apply_config((struct ui_psensor *)data);
186 ui_sensorpref_appindicator_label_toggled_cb(GtkToggleButton *btn, gpointer data)
194 s = get_selected_sensor();
199 active = gtk_toggle_button_get_active(btn);
200 config_set_appindicator_label_enabled(s->id, active);
202 apply_config((struct ui_psensor *)data);
205 void ui_sensorpref_color_set_cb(GtkColorButton *widget, gpointer data)
213 s = get_selected_sensor();
217 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), &color);
218 config_set_sensor_color(s->id, &color);
220 apply_config((struct ui_psensor *)data);
224 ui_sensorpref_alarm_high_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
232 s = get_selected_sensor();
236 v = gtk_spin_button_get_value(btn);
237 if (config_get_sensor_unit() == FAHRENHEIT)
238 v = fahrenheit_to_celsius(v);
240 config_set_sensor_alarm_high_threshold(s->id, v);
241 s->alarm_high_threshold = v;
243 apply_config((struct ui_psensor *)data);
247 ui_sensorpref_alarm_low_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
255 s = get_selected_sensor();
259 v = gtk_spin_button_get_value(btn);
260 if (config_get_sensor_unit() == FAHRENHEIT)
261 v = fahrenheit_to_celsius(v);
263 config_set_sensor_alarm_low_threshold(s->id, v);
264 s->alarm_low_threshold = v;
266 apply_config((struct ui_psensor *)data);
269 static void update_pref(struct psensor *s)
271 int use_celsius, threshold;
278 ignore_changes = true;
280 gtk_label_set_text(w_sensor_id, s->id);
281 gtk_label_set_text(w_sensor_type, psensor_type_to_str(s->type));
282 gtk_entry_set_text(w_sensor_name, s->name);
288 gtk_label_set_text(w_sensor_chipname, chip);
290 gtk_toggle_button_set_active(w_sensor_draw,
291 config_is_sensor_graph_enabled(s->id));
293 gtk_toggle_button_set_active(w_sensor_display,
294 config_is_sensor_enabled(s->id));
296 color = config_get_sensor_color(s->id);
297 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(w_sensor_color), color);
298 gdk_rgba_free(color);
300 use_celsius = config_get_sensor_unit() == CELSIUS ? 1 : 0;
301 gtk_label_set_text(w_sensor_high_threshold_unit,
302 psensor_type_to_unit_str(s->type, use_celsius));
303 gtk_label_set_text(w_sensor_low_threshold_unit,
304 psensor_type_to_unit_str(s->type, use_celsius));
306 if (is_appindicator_supported()) {
307 gtk_widget_set_has_tooltip
308 (GTK_WIDGET(w_appindicator_label_enabled), FALSE);
309 gtk_widget_set_has_tooltip
310 (GTK_WIDGET(w_appindicator_enabled), FALSE);
312 gtk_widget_set_sensitive
313 (GTK_WIDGET(w_appindicator_label_enabled), FALSE);
314 gtk_widget_set_has_tooltip
315 (GTK_WIDGET(w_appindicator_label_enabled), TRUE);
316 gtk_widget_set_sensitive
317 (GTK_WIDGET(w_appindicator_enabled), FALSE);
318 gtk_widget_set_has_tooltip
319 (GTK_WIDGET(w_appindicator_enabled), TRUE);
322 gtk_toggle_button_set_active(w_sensor_alarm,
323 config_get_sensor_alarm_enabled(s->id));
325 threshold = config_get_sensor_alarm_high_threshold(s->id);
327 threshold = celsius_to_fahrenheit(threshold);
328 gtk_spin_button_set_value(w_sensor_high_threshold, threshold);
330 threshold = config_get_sensor_alarm_low_threshold(s->id);
332 threshold = celsius_to_fahrenheit(threshold);
333 gtk_spin_button_set_value(w_sensor_low_threshold, threshold);
335 gtk_toggle_button_set_active(w_appindicator_enabled,
336 config_is_appindicator_enabled(s->id));
338 gtk_toggle_button_set_active
339 (w_appindicator_label_enabled,
340 config_is_appindicator_label_enabled(s->id));
342 ignore_changes = false;
346 ui_sensorpref_tree_selection_changed_cb(GtkTreeSelection *sel, gpointer data)
348 update_pref(get_selected_sensor());
351 static void select_sensor(struct psensor *s, struct psensor **sensors)
353 struct psensor **s_cur;
356 GtkTreeSelection *sel;
358 for (s_cur = sensors, i = 0; *s_cur; s_cur++, i++)
360 p = gtk_tree_path_new_from_indices(i, -1);
361 sel = gtk_tree_view_get_selection(w_sensors_list);
362 gtk_tree_selection_select_path(sel, p);
364 gtk_tree_path_free(p);
371 static void quit(void)
373 gtk_widget_destroy(GTK_WIDGET(w_dialog));
378 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
384 void ui_sensorpref_close_clicked_cb(GtkButton *btn, gpointer data)
389 static GtkBuilder *load_ui(struct ui_psensor *ui)
397 builder = gtk_builder_new();
398 ok = gtk_builder_add_from_file
400 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "sensor-edit.glade",
404 log_printf(LOG_ERR, error->message);
409 w_sensors_list = GTK_TREE_VIEW
410 (gtk_builder_get_object(builder, "sensors_list"));
411 w_dialog = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
412 w_sensor_id = GTK_LABEL(gtk_builder_get_object(builder, "sensor_id"));
413 w_sensor_type = GTK_LABEL
414 (gtk_builder_get_object(builder, "sensor_type"));
415 w_sensor_name = GTK_ENTRY
416 (gtk_builder_get_object(builder, "sensor_name"));
417 w_sensor_chipname = GTK_LABEL
418 (gtk_builder_get_object(builder, "chip_name"));
419 w_sensor_draw = GTK_TOGGLE_BUTTON
420 (gtk_builder_get_object(builder, "sensor_draw"));
421 w_sensor_display = GTK_TOGGLE_BUTTON
422 (gtk_builder_get_object(builder, "sensor_enable_checkbox"));
423 w_sensor_color = GTK_COLOR_BUTTON
424 (gtk_builder_get_object(builder, "sensor_color"));
425 w_sensor_alarm = GTK_TOGGLE_BUTTON
426 (gtk_builder_get_object(builder, "sensor_alarm"));
427 w_sensor_high_threshold
428 = GTK_SPIN_BUTTON(gtk_builder_get_object
429 (builder, "sensor_alarm_high_threshold"));
430 w_sensor_low_threshold
431 = GTK_SPIN_BUTTON(gtk_builder_get_object
432 (builder, "sensor_alarm_low_threshold"));
433 w_sensor_high_threshold_unit
434 = GTK_LABEL(gtk_builder_get_object
435 (builder, "sensor_alarm_high_threshold_unit"));
436 w_sensor_low_threshold_unit
437 = GTK_LABEL(gtk_builder_get_object
438 (builder, "sensor_alarm_low_threshold_unit"));
439 w_appindicator_enabled = GTK_TOGGLE_BUTTON
440 (gtk_builder_get_object(builder, "indicator_checkbox"));
441 w_appindicator_label_enabled = GTK_TOGGLE_BUTTON
442 (gtk_builder_get_object(builder, "indicator_label_checkbox"));
444 store = GTK_LIST_STORE(gtk_builder_get_object(builder,
445 "sensors_liststore"));
447 gtk_builder_connect_signals(builder, ui);
449 g_signal_connect(w_dialog,
451 G_CALLBACK(on_delete_event_cb),
457 static void populate(struct psensor *sensor, struct psensor **sensors)
460 struct psensor **s_cur, **ordered_sensors, *s;
462 gtk_list_store_clear(store);
464 ordered_sensors = ui_get_sensors_ordered_by_position(sensors);
465 for (s_cur = ordered_sensors; *s_cur; s_cur++) {
467 gtk_list_store_append(store, &iter);
469 gtk_list_store_set(store, &iter,
475 select_sensor(sensor, ordered_sensors);
477 free(ordered_sensors);
480 void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
484 if (w_dialog == NULL) {
485 builder = load_ui(ui);
490 g_object_unref(G_OBJECT(builder));
493 populate(sensor, ui->sensors);
495 gtk_window_present(GTK_WINDOW(w_dialog));