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>
26 #include <ui_sensorlist.h>
27 #include <ui_sensorpref.h>
30 #if defined(HAVE_APPINDICATOR)
31 #include <ui_appindicator.h>
40 struct psensor *sensor;
45 int alarm_high_threshold;
46 int alarm_low_threshold;
47 unsigned int appindicator_enabled;
48 unsigned int appindicator_label_enabled;
49 unsigned int display_enabled;
53 struct ui_psensor *ui;
57 static struct sensor_pref *
58 sensor_pref_new(struct psensor *s, struct config *cfg)
60 struct sensor_pref *p;
62 p = malloc(sizeof(struct sensor_pref));
65 p->name = strdup(s->name);
66 p->graph_enabled = s->graph_enabled;
67 p->alarm_enabled = s->alarm_enabled;
68 p->color = color_dup(s->color);
69 p->display_enabled = config_is_sensor_enabled(s->id);
71 if (cfg->temperature_unit == CELSIUS) {
72 p->alarm_high_threshold = s->alarm_high_threshold;
73 p->alarm_low_threshold = s->alarm_low_threshold;
75 p->alarm_high_threshold
76 = celsius_to_fahrenheit(s->alarm_high_threshold);
77 p->alarm_low_threshold
78 = celsius_to_fahrenheit(s->alarm_low_threshold);
81 p->appindicator_enabled = s->appindicator_enabled;
82 p->appindicator_label_enabled
83 = config_is_appindicator_label_enabled(s->id);
88 static void sensor_pref_free(struct sensor_pref *p)
99 static struct sensor_pref *get_selected_sensor_pref(GtkTreeView *tree)
103 struct sensor_pref *pref;
104 GtkTreeSelection *selection;
106 selection = gtk_tree_view_get_selection(tree);
109 if (gtk_tree_selection_get_selected(selection, &model, &iter))
110 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &pref, -1);
115 void ui_sensorpref_name_changed_cb(GtkEntry *entry, gpointer data)
117 struct sensor_pref *p;
120 str = gtk_entry_get_text(entry);
122 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
124 if (p && strcmp(p->name, str)) {
126 p->name = strdup(str);
130 void ui_sensorpref_draw_toggled_cb(GtkToggleButton *btn, gpointer data)
132 struct sensor_pref *p;
134 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
137 p->graph_enabled = gtk_toggle_button_get_active(btn);
140 void ui_sensorpref_display_toggled_cb(GtkToggleButton *btn, gpointer data)
142 struct sensor_pref *p;
144 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
147 p->display_enabled = gtk_toggle_button_get_active(btn);
150 void ui_sensorpref_alarm_toggled_cb(GtkToggleButton *btn, gpointer data)
152 struct sensor_pref *p;
154 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
157 p->alarm_enabled = gtk_toggle_button_get_active(btn);
161 ui_sensorpref_appindicator_menu_toggled_cb(GtkToggleButton *btn, gpointer data)
163 struct sensor_pref *p;
165 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
168 p->appindicator_enabled = gtk_toggle_button_get_active(btn);
172 ui_sensorpref_appindicator_label_toggled_cb(GtkToggleButton *btn, gpointer data)
174 struct sensor_pref *p;
176 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
179 p->appindicator_label_enabled
180 = gtk_toggle_button_get_active(btn);
183 void ui_sensorpref_color_set_cb(GtkColorButton *widget, gpointer data)
185 struct sensor_pref *p;
188 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
191 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), &color);
192 color_set(p->color, color.red, color.green, color.blue);
197 ui_sensorpref_alarm_high_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
199 struct sensor_pref *p;
201 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
204 p->alarm_high_threshold = gtk_spin_button_get_value(btn);
208 ui_sensorpref_alarm_low_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
210 struct sensor_pref *p;
212 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
215 p->alarm_low_threshold = gtk_spin_button_get_value(btn);
219 update_pref(struct sensor_pref *p, struct config *cfg, GtkBuilder *builder)
221 GtkLabel *w_id, *w_type, *w_high_threshold_unit, *w_low_threshold_unit,
224 GtkToggleButton *w_draw, *w_alarm, *w_appindicator_enabled,
225 *w_appindicator_label_enabled, *w_display;
226 GtkColorButton *w_color;
227 GtkSpinButton *w_high_threshold, *w_low_threshold;
234 w_id = GTK_LABEL(gtk_builder_get_object(builder, "sensor_id"));
235 gtk_label_set_text(w_id, s->id);
237 w_type = GTK_LABEL(gtk_builder_get_object(builder, "sensor_type"));
238 gtk_label_set_text(w_type, psensor_type_to_str(s->type));
240 w_name = GTK_ENTRY(gtk_builder_get_object(builder, "sensor_name"));
241 gtk_entry_set_text(w_name, p->name);
243 w_chipname = GTK_LABEL(gtk_builder_get_object(builder, "chip_name"));
245 gtk_label_set_text(w_chipname, s->chip);
247 gtk_label_set_text(w_chipname, _("Unknown"));
249 w_draw = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
251 gtk_toggle_button_set_active(w_draw, p->graph_enabled);
253 w_display = GTK_TOGGLE_BUTTON(gtk_builder_get_object
255 "sensor_enable_checkbox"));
256 gtk_toggle_button_set_active(w_display, p->display_enabled);
258 color = color_to_GdkRGBA(p->color);
259 w_color = GTK_COLOR_BUTTON(gtk_builder_get_object(builder,
261 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(w_color), &color);
263 w_alarm = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
265 w_high_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
267 "sensor_alarm_high_threshold"));
268 w_low_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
270 "sensor_alarm_low_threshold"));
272 w_high_threshold_unit = GTK_LABEL(gtk_builder_get_object
274 "sensor_alarm_high_threshold_unit"));
275 w_low_threshold_unit = GTK_LABEL(gtk_builder_get_object
277 "sensor_alarm_low_threshold_unit"));
279 use_celsius = cfg->temperature_unit == CELSIUS ? 1 : 0;
280 gtk_label_set_text(w_high_threshold_unit,
281 psensor_type_to_unit_str(s->type,
283 gtk_label_set_text(w_low_threshold_unit,
284 psensor_type_to_unit_str(s->type,
287 w_appindicator_enabled = GTK_TOGGLE_BUTTON
288 (gtk_builder_get_object(builder, "indicator_checkbox"));
289 w_appindicator_label_enabled = GTK_TOGGLE_BUTTON
290 (gtk_builder_get_object(builder, "indicator_label_checkbox"));
293 #if !HAVE_APPINDICATOR
294 gtk_widget_set_sensitive(GTK_WIDGET(w_appindicator_label_enabled),
296 gtk_widget_set_sensitive(GTK_WIDGET(w_appindicator_enabled), FALSE);
297 gtk_widget_set_has_tooltip(GTK_WIDGET(w_appindicator_label_enabled),
299 gtk_widget_set_has_tooltip(GTK_WIDGET(w_appindicator_enabled), TRUE);
301 gtk_widget_set_has_tooltip(GTK_WIDGET(w_appindicator_label_enabled),
303 gtk_widget_set_has_tooltip(GTK_WIDGET(w_appindicator_enabled), FALSE);
306 gtk_toggle_button_set_active(w_alarm, p->alarm_enabled);
307 gtk_spin_button_set_value(w_high_threshold, p->alarm_high_threshold);
308 gtk_spin_button_set_value(w_low_threshold, p->alarm_low_threshold);
309 gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), TRUE);
310 gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), TRUE);
311 gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), TRUE);
313 gtk_toggle_button_set_active(w_appindicator_enabled,
314 p->appindicator_enabled);
316 gtk_toggle_button_set_active(w_appindicator_label_enabled,
317 p->appindicator_label_enabled);
320 static void on_changed(GtkTreeSelection *selection, gpointer data)
322 struct cb_data *cbdata = data;
323 struct ui_psensor *ui = cbdata->ui;
324 struct sensor_pref *p;
327 tree = GTK_TREE_VIEW(gtk_builder_get_object(cbdata->builder,
329 p = get_selected_sensor_pref(tree);
330 update_pref(p, ui->config, cbdata->builder);
334 select_sensor(struct psensor *s, struct psensor **sensors, GtkTreeView *tree)
336 struct psensor **s_cur;
341 for (s_cur = sensors, i = 0; *s_cur; s_cur++, i++)
343 p = gtk_tree_path_new_from_indices(i, -1);
348 GtkTreeSelection *s = gtk_tree_view_get_selection(tree);
350 gtk_tree_selection_select_path(s, p);
351 gtk_tree_path_free(p);
355 static void apply_pref(struct sensor_pref *p, int pos, struct config *cfg)
361 if (strcmp(p->name, s->name)) {
363 s->name = strdup(p->name);
364 config_set_sensor_name(s->id, s->name);
367 if (s->graph_enabled != p->graph_enabled) {
368 s->graph_enabled = p->graph_enabled;
369 config_set_sensor_graph_enabled(s->id, s->graph_enabled);
372 if (is_temp_type(s->type) && cfg->temperature_unit == FAHRENHEIT) {
373 s->alarm_high_threshold
374 = fahrenheit_to_celsius(p->alarm_high_threshold);
375 s->alarm_low_threshold
376 = fahrenheit_to_celsius(p->alarm_low_threshold);
378 s->alarm_high_threshold = p->alarm_high_threshold;
379 s->alarm_low_threshold = p->alarm_low_threshold;
382 config_set_sensor_alarm_high_threshold(s->id, s->alarm_high_threshold);
383 config_set_sensor_alarm_low_threshold(s->id, s->alarm_low_threshold);
385 if (s->alarm_enabled != p->alarm_enabled) {
386 s->alarm_enabled = p->alarm_enabled;
387 config_set_sensor_alarm_enabled(s->id, s->alarm_enabled);
394 config_set_sensor_color(s->id, s->color);
396 if (s->appindicator_enabled != p->appindicator_enabled) {
397 s->appindicator_enabled = p->appindicator_enabled;
398 config_set_appindicator_enabled(s->id, s->appindicator_enabled);
401 config_set_appindicator_label_enabled(s->id,
402 p->appindicator_label_enabled);
404 config_set_sensor_position(s->id, pos);
406 config_set_sensor_enabled(s->id, p->display_enabled);
409 static void apply_prefs(GtkTreeModel *model, struct config *cfg)
412 struct sensor_pref *spref;
416 valid = gtk_tree_model_get_iter_first(model, &iter);
419 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
420 apply_pref(spref, i, cfg);
421 valid = gtk_tree_model_iter_next(model, &iter);
427 void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
434 GtkTreeView *w_sensors_list;
436 struct psensor **s_cur, *s, **ordered_sensors;
437 GtkTreeSelection *selection;
438 struct cb_data cbdata;
440 struct sensor_pref *spref;
446 builder = gtk_builder_new();
447 cbdata.builder = builder;
450 ok = gtk_builder_add_from_file
452 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "sensor-edit.glade",
456 log_printf(LOG_ERR, error->message);
462 = GTK_TREE_VIEW(gtk_builder_get_object(builder,
464 gtk_builder_connect_signals(builder, w_sensors_list);
466 store = GTK_LIST_STORE(gtk_builder_get_object(builder,
467 "sensors_liststore"));
469 ordered_sensors = ui_get_sensors_ordered_by_position(ui);
470 for (s_cur = ordered_sensors; *s_cur; s_cur++) {
472 gtk_list_store_append(store, &iter);
474 spref = sensor_pref_new(s, ui->config);
475 gtk_list_store_set(store, &iter,
477 COL_SENSOR_PREF, spref,
481 update_pref(spref, ui->config, builder);
484 selection = gtk_tree_view_get_selection(w_sensors_list);
485 g_signal_connect(selection, "changed", G_CALLBACK(on_changed), &cbdata);
486 select_sensor(sensor, ordered_sensors, w_sensors_list);
488 free(ordered_sensors);
490 diag = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
491 result = gtk_dialog_run(diag);
493 model = gtk_tree_view_get_model(w_sensors_list);
495 if (result == GTK_RESPONSE_ACCEPT) {
496 apply_prefs(model, ui->config);
497 ui_sensorlist_update(ui, 1);
498 #if defined(HAVE_APPINDICATOR)
499 ui_appindicator_update_menu(ui);
503 valid = gtk_tree_model_get_iter_first(model, &iter);
505 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
506 sensor_pref_free(spref);
507 valid = gtk_tree_model_iter_next(model, &iter);
510 g_object_unref(G_OBJECT(builder));
512 gtk_widget_destroy(GTK_WIDGET(diag));