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>
31 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
32 #include "ui_appindicator.h"
41 struct psensor *sensor;
46 int alarm_high_threshold;
47 int alarm_low_threshold;
48 unsigned int appindicator_enabled;
49 unsigned int appindicator_label_enabled;
50 unsigned int display_enabled;
54 struct ui_psensor *ui;
58 static struct sensor_pref *
59 sensor_pref_new(struct psensor *s, struct config *cfg)
61 struct sensor_pref *p;
63 p = malloc(sizeof(struct sensor_pref));
66 p->name = strdup(s->name);
67 p->graph_enabled = s->graph_enabled;
68 p->alarm_enabled = s->alarm_enabled;
69 p->color = color_dup(s->color);
70 p->display_enabled = config_is_sensor_enabled(s->id);
72 if (cfg->temperature_unit == CELSIUS) {
73 p->alarm_high_threshold = s->alarm_high_threshold;
74 p->alarm_low_threshold = s->alarm_low_threshold;
76 p->alarm_high_threshold
77 = celsius_to_fahrenheit(s->alarm_high_threshold);
78 p->alarm_low_threshold
79 = celsius_to_fahrenheit(s->alarm_low_threshold);
82 p->appindicator_enabled = s->appindicator_enabled;
83 p->appindicator_label_enabled
84 = config_is_appindicator_label_enabled(s->id);
89 static void sensor_pref_free(struct sensor_pref *p)
100 static struct sensor_pref *get_selected_sensor_pref(GtkTreeView *tree)
104 struct sensor_pref *pref;
105 GtkTreeSelection *selection;
107 selection = gtk_tree_view_get_selection(tree);
110 if (gtk_tree_selection_get_selected(selection, &model, &iter))
111 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &pref, -1);
116 void ui_sensorpref_name_changed_cb(GtkEntry *entry, gpointer data)
118 struct sensor_pref *p;
121 str = gtk_entry_get_text(entry);
123 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
125 if (p && strcmp(p->name, str)) {
127 p->name = strdup(str);
131 void ui_sensorpref_draw_toggled_cb(GtkToggleButton *btn, gpointer data)
133 struct sensor_pref *p;
135 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
138 p->graph_enabled = gtk_toggle_button_get_active(btn);
141 void ui_sensorpref_display_toggled_cb(GtkToggleButton *btn, gpointer data)
143 struct sensor_pref *p;
145 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
148 p->display_enabled = gtk_toggle_button_get_active(btn);
151 void ui_sensorpref_alarm_toggled_cb(GtkToggleButton *btn, gpointer data)
153 struct sensor_pref *p;
155 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
158 p->alarm_enabled = gtk_toggle_button_get_active(btn);
162 ui_sensorpref_appindicator_menu_toggled_cb(GtkToggleButton *btn, gpointer data)
164 struct sensor_pref *p;
166 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
169 p->appindicator_enabled = gtk_toggle_button_get_active(btn);
173 ui_sensorpref_appindicator_label_toggled_cb(GtkToggleButton *btn, gpointer data)
175 struct sensor_pref *p;
177 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
180 p->appindicator_label_enabled
181 = gtk_toggle_button_get_active(btn);
184 void ui_sensorpref_color_set_cb(GtkColorButton *widget, gpointer data)
186 struct sensor_pref *p;
189 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
192 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), &color);
193 color_set(p->color, color.red, color.green, color.blue);
198 ui_sensorpref_alarm_high_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
200 struct sensor_pref *p;
202 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
205 p->alarm_high_threshold = gtk_spin_button_get_value(btn);
209 ui_sensorpref_alarm_low_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
211 struct sensor_pref *p;
213 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
216 p->alarm_low_threshold = gtk_spin_button_get_value(btn);
220 update_pref(struct sensor_pref *p, struct config *cfg, GtkBuilder *builder)
222 GtkLabel *w_id, *w_type, *w_high_threshold_unit, *w_low_threshold_unit,
225 GtkToggleButton *w_draw, *w_alarm, *w_appindicator_enabled,
226 *w_appindicator_label_enabled, *w_display;
227 GtkColorButton *w_color;
228 GtkSpinButton *w_high_threshold, *w_low_threshold;
235 w_id = GTK_LABEL(gtk_builder_get_object(builder, "sensor_id"));
236 gtk_label_set_text(w_id, s->id);
238 w_type = GTK_LABEL(gtk_builder_get_object(builder, "sensor_type"));
239 gtk_label_set_text(w_type, psensor_type_to_str(s->type));
241 w_name = GTK_ENTRY(gtk_builder_get_object(builder, "sensor_name"));
242 gtk_entry_set_text(w_name, p->name);
244 w_chipname = GTK_LABEL(gtk_builder_get_object(builder, "chip_name"));
246 gtk_label_set_text(w_chipname, s->chip);
248 gtk_label_set_text(w_chipname, _("Unknown"));
250 w_draw = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
252 gtk_toggle_button_set_active(w_draw, p->graph_enabled);
254 w_display = GTK_TOGGLE_BUTTON(gtk_builder_get_object
256 "sensor_enable_checkbox"));
257 gtk_toggle_button_set_active(w_display, p->display_enabled);
259 color = color_to_GdkRGBA(p->color);
260 w_color = GTK_COLOR_BUTTON(gtk_builder_get_object(builder,
262 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(w_color), &color);
264 w_alarm = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
266 w_high_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
268 "sensor_alarm_high_threshold"));
269 w_low_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
271 "sensor_alarm_low_threshold"));
273 w_high_threshold_unit = GTK_LABEL(gtk_builder_get_object
275 "sensor_alarm_high_threshold_unit"));
276 w_low_threshold_unit = GTK_LABEL(gtk_builder_get_object
278 "sensor_alarm_low_threshold_unit"));
280 use_celsius = cfg->temperature_unit == CELSIUS ? 1 : 0;
281 gtk_label_set_text(w_high_threshold_unit,
282 psensor_type_to_unit_str(s->type,
284 gtk_label_set_text(w_low_threshold_unit,
285 psensor_type_to_unit_str(s->type,
288 w_appindicator_enabled = GTK_TOGGLE_BUTTON
289 (gtk_builder_get_object(builder, "indicator_checkbox"));
290 w_appindicator_label_enabled = GTK_TOGGLE_BUTTON
291 (gtk_builder_get_object(builder, "indicator_label_checkbox"));
293 gtk_toggle_button_set_active(w_alarm, p->alarm_enabled);
294 gtk_spin_button_set_value(w_high_threshold, p->alarm_high_threshold);
295 gtk_spin_button_set_value(w_low_threshold, p->alarm_low_threshold);
296 gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), TRUE);
297 gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), TRUE);
298 gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), TRUE);
300 gtk_toggle_button_set_active(w_appindicator_enabled,
301 p->appindicator_enabled);
303 gtk_toggle_button_set_active(w_appindicator_label_enabled,
304 p->appindicator_label_enabled);
307 static void on_changed(GtkTreeSelection *selection, gpointer data)
309 struct cb_data *cbdata = data;
310 struct ui_psensor *ui = cbdata->ui;
311 struct sensor_pref *p;
314 tree = GTK_TREE_VIEW(gtk_builder_get_object(cbdata->builder,
316 p = get_selected_sensor_pref(tree);
317 update_pref(p, ui->config, cbdata->builder);
321 select_sensor(struct psensor *s, struct psensor **sensors, GtkTreeView *tree)
323 struct psensor **s_cur;
328 for (s_cur = sensors, i = 0; *s_cur; s_cur++, i++)
330 p = gtk_tree_path_new_from_indices(i, -1);
335 GtkTreeSelection *s = gtk_tree_view_get_selection(tree);
337 gtk_tree_selection_select_path(s, p);
338 gtk_tree_path_free(p);
342 static void apply_pref(struct sensor_pref *p, int pos, struct config *cfg)
348 if (strcmp(p->name, s->name)) {
350 s->name = strdup(p->name);
351 config_set_sensor_name(s->id, s->name);
354 if (s->graph_enabled != p->graph_enabled) {
355 s->graph_enabled = p->graph_enabled;
356 config_set_sensor_graph_enabled(s->id, s->graph_enabled);
359 if (is_temp_type(s->type) && cfg->temperature_unit == FAHRENHEIT) {
360 s->alarm_high_threshold
361 = fahrenheit_to_celsius(p->alarm_high_threshold);
362 s->alarm_low_threshold
363 = fahrenheit_to_celsius(p->alarm_low_threshold);
365 s->alarm_high_threshold = p->alarm_high_threshold;
366 s->alarm_low_threshold = p->alarm_low_threshold;
369 config_set_sensor_alarm_high_threshold(s->id, s->alarm_high_threshold);
370 config_set_sensor_alarm_low_threshold(s->id, s->alarm_low_threshold);
372 if (s->alarm_enabled != p->alarm_enabled) {
373 s->alarm_enabled = p->alarm_enabled;
374 config_set_sensor_alarm_enabled(s->id, s->alarm_enabled);
381 config_set_sensor_color(s->id, s->color);
383 if (s->appindicator_enabled != p->appindicator_enabled) {
384 s->appindicator_enabled = p->appindicator_enabled;
385 config_set_appindicator_enabled(s->id, s->appindicator_enabled);
388 config_set_appindicator_label_enabled(s->id,
389 p->appindicator_label_enabled);
391 config_set_sensor_position(s->id, pos);
393 config_set_sensor_enabled(s->id, p->display_enabled);
396 static void apply_prefs(GtkTreeModel *model, struct config *cfg)
399 struct sensor_pref *spref;
403 valid = gtk_tree_model_get_iter_first(model, &iter);
406 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
407 apply_pref(spref, i, cfg);
408 valid = gtk_tree_model_iter_next(model, &iter);
414 void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
421 GtkTreeView *w_sensors_list;
423 struct psensor **s_cur, *s, **ordered_sensors;
424 GtkTreeSelection *selection;
425 struct cb_data cbdata;
427 struct sensor_pref *spref;
433 builder = gtk_builder_new();
434 cbdata.builder = builder;
437 ok = gtk_builder_add_from_file
439 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "sensor-edit.glade",
443 log_printf(LOG_ERR, error->message);
449 = GTK_TREE_VIEW(gtk_builder_get_object(builder,
451 gtk_builder_connect_signals(builder, w_sensors_list);
453 store = GTK_LIST_STORE(gtk_builder_get_object(builder,
454 "sensors_liststore"));
456 ordered_sensors = ui_get_sensors_ordered_by_position(ui);
457 for (s_cur = ordered_sensors; *s_cur; s_cur++) {
459 gtk_list_store_append(store, &iter);
461 spref = sensor_pref_new(s, ui->config);
462 gtk_list_store_set(store, &iter,
464 COL_SENSOR_PREF, spref,
468 update_pref(spref, ui->config, builder);
471 selection = gtk_tree_view_get_selection(w_sensors_list);
472 g_signal_connect(selection, "changed", G_CALLBACK(on_changed), &cbdata);
473 select_sensor(sensor, ordered_sensors, w_sensors_list);
475 free(ordered_sensors);
477 diag = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
478 result = gtk_dialog_run(diag);
480 model = gtk_tree_view_get_model(w_sensors_list);
482 if (result == GTK_RESPONSE_ACCEPT) {
483 apply_prefs(model, ui->config);
484 ui_sensorlist_update(ui, 1);
485 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
486 ui_appindicator_update_menu(ui);
490 valid = gtk_tree_model_get_iter_first(model, &iter);
492 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
493 sensor_pref_free(spref);
494 valid = gtk_tree_model_iter_next(model, &iter);
497 g_object_unref(G_OBJECT(builder));
499 gtk_widget_destroy(GTK_WIDGET(diag));