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;
339 GtkTreeSelection *sel;
342 for (s_cur = sensors, i = 0; *s_cur; s_cur++, i++)
344 p = gtk_tree_path_new_from_indices(i, -1);
349 sel = gtk_tree_view_get_selection(tree);
351 gtk_tree_selection_select_path(sel, p);
352 gtk_tree_path_free(p);
356 static void apply_pref(struct sensor_pref *p, int pos, struct config *cfg)
362 if (strcmp(p->name, s->name)) {
364 s->name = strdup(p->name);
365 config_set_sensor_name(s->id, s->name);
368 if (s->graph_enabled != p->graph_enabled) {
369 s->graph_enabled = p->graph_enabled;
370 config_set_sensor_graph_enabled(s->id, s->graph_enabled);
373 if (is_temp_type(s->type) && cfg->temperature_unit == FAHRENHEIT) {
374 s->alarm_high_threshold
375 = fahrenheit_to_celsius(p->alarm_high_threshold);
376 s->alarm_low_threshold
377 = fahrenheit_to_celsius(p->alarm_low_threshold);
379 s->alarm_high_threshold = p->alarm_high_threshold;
380 s->alarm_low_threshold = p->alarm_low_threshold;
383 config_set_sensor_alarm_high_threshold(s->id, s->alarm_high_threshold);
384 config_set_sensor_alarm_low_threshold(s->id, s->alarm_low_threshold);
386 if (s->alarm_enabled != p->alarm_enabled) {
387 s->alarm_enabled = p->alarm_enabled;
388 config_set_sensor_alarm_enabled(s->id, s->alarm_enabled);
395 config_set_sensor_color(s->id, s->color);
397 if (s->appindicator_enabled != p->appindicator_enabled) {
398 s->appindicator_enabled = p->appindicator_enabled;
399 config_set_appindicator_enabled(s->id, s->appindicator_enabled);
402 config_set_appindicator_label_enabled(s->id,
403 p->appindicator_label_enabled);
405 config_set_sensor_position(s->id, pos);
407 config_set_sensor_enabled(s->id, p->display_enabled);
410 static void apply_prefs(GtkTreeModel *model, struct config *cfg)
413 struct sensor_pref *spref;
417 valid = gtk_tree_model_get_iter_first(model, &iter);
420 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
421 apply_pref(spref, i, cfg);
422 valid = gtk_tree_model_iter_next(model, &iter);
428 void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
435 GtkTreeView *w_sensors_list;
437 struct psensor **s_cur, *s, **ordered_sensors;
438 GtkTreeSelection *selection;
439 struct cb_data cbdata;
441 struct sensor_pref *spref;
447 builder = gtk_builder_new();
448 cbdata.builder = builder;
451 ok = gtk_builder_add_from_file
453 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "sensor-edit.glade",
457 log_printf(LOG_ERR, error->message);
463 = GTK_TREE_VIEW(gtk_builder_get_object(builder,
465 gtk_builder_connect_signals(builder, w_sensors_list);
467 store = GTK_LIST_STORE(gtk_builder_get_object(builder,
468 "sensors_liststore"));
470 ordered_sensors = ui_get_sensors_ordered_by_position(ui);
471 for (s_cur = ordered_sensors; *s_cur; s_cur++) {
473 gtk_list_store_append(store, &iter);
475 spref = sensor_pref_new(s, ui->config);
476 gtk_list_store_set(store, &iter,
478 COL_SENSOR_PREF, spref,
482 update_pref(spref, ui->config, builder);
485 selection = gtk_tree_view_get_selection(w_sensors_list);
486 g_signal_connect(selection, "changed", G_CALLBACK(on_changed), &cbdata);
487 select_sensor(sensor, ordered_sensors, w_sensors_list);
489 free(ordered_sensors);
491 diag = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
492 result = gtk_dialog_run(diag);
494 model = gtk_tree_view_get_model(w_sensors_list);
496 if (result == GTK_RESPONSE_ACCEPT) {
497 apply_prefs(model, ui->config);
498 ui_sensorlist_update(ui, 1);
499 #if defined(HAVE_APPINDICATOR)
500 ui_appindicator_update_menu(ui);
504 valid = gtk_tree_model_get_iter_first(model, &iter);
506 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
507 sensor_pref_free(spref);
508 valid = gtk_tree_model_iter_next(model, &iter);
511 g_object_unref(G_OBJECT(builder));
513 gtk_widget_destroy(GTK_WIDGET(diag));