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>
37 struct psensor *sensor;
42 int alarm_high_threshold;
43 int alarm_low_threshold;
44 bool appindicator_enabled;
45 unsigned int appindicator_label_enabled;
46 unsigned int display_enabled;
50 struct ui_psensor *ui;
54 static struct sensor_pref *
55 sensor_pref_new(struct psensor *s, struct config *cfg)
57 struct sensor_pref *p;
59 p = malloc(sizeof(struct sensor_pref));
62 p->name = strdup(s->name);
63 p->graph_enabled = config_is_sensor_graph_enabled(s->id);
64 p->alarm_enabled = config_get_sensor_alarm_enabled(s->id);
65 p->color = config_get_sensor_color(s->id);
66 p->display_enabled = config_is_sensor_enabled(s->id);
68 if (cfg->temperature_unit == CELSIUS) {
69 p->alarm_high_threshold = s->alarm_high_threshold;
70 p->alarm_low_threshold = s->alarm_low_threshold;
72 p->alarm_high_threshold
73 = celsius_to_fahrenheit(s->alarm_high_threshold);
74 p->alarm_low_threshold
75 = celsius_to_fahrenheit(s->alarm_low_threshold);
78 p->appindicator_enabled = config_is_appindicator_enabled(s->id);
79 p->appindicator_label_enabled
80 = config_is_appindicator_label_enabled(s->id);
85 static void sensor_pref_free(struct sensor_pref *p)
91 gdk_rgba_free(p->color);
96 static struct sensor_pref *get_selected_sensor_pref(GtkTreeView *tree)
100 struct sensor_pref *pref;
101 GtkTreeSelection *selection;
103 selection = gtk_tree_view_get_selection(tree);
106 if (gtk_tree_selection_get_selected(selection, &model, &iter))
107 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &pref, -1);
112 void ui_sensorpref_name_changed_cb(GtkEntry *entry, gpointer data)
114 struct sensor_pref *p;
117 str = gtk_entry_get_text(entry);
119 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
121 if (p && strcmp(p->name, str)) {
123 p->name = strdup(str);
127 void ui_sensorpref_draw_toggled_cb(GtkToggleButton *btn, gpointer data)
129 struct sensor_pref *p;
131 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
134 p->graph_enabled = gtk_toggle_button_get_active(btn);
137 void ui_sensorpref_display_toggled_cb(GtkToggleButton *btn, gpointer data)
139 struct sensor_pref *p;
141 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
144 p->display_enabled = gtk_toggle_button_get_active(btn);
147 void ui_sensorpref_alarm_toggled_cb(GtkToggleButton *btn, gpointer data)
149 struct sensor_pref *p;
151 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
154 p->alarm_enabled = gtk_toggle_button_get_active(btn);
158 ui_sensorpref_appindicator_menu_toggled_cb(GtkToggleButton *btn, gpointer data)
160 struct sensor_pref *p;
162 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
165 p->appindicator_enabled = gtk_toggle_button_get_active(btn);
169 ui_sensorpref_appindicator_label_toggled_cb(GtkToggleButton *btn, gpointer data)
171 struct sensor_pref *p;
173 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
176 p->appindicator_label_enabled
177 = gtk_toggle_button_get_active(btn);
180 void ui_sensorpref_color_set_cb(GtkColorButton *widget, gpointer data)
182 struct sensor_pref *p;
184 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
187 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), p->color);
191 ui_sensorpref_alarm_high_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
193 struct sensor_pref *p;
195 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
198 p->alarm_high_threshold = gtk_spin_button_get_value(btn);
202 ui_sensorpref_alarm_low_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
204 struct sensor_pref *p;
206 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
209 p->alarm_low_threshold = gtk_spin_button_get_value(btn);
213 update_pref(struct sensor_pref *p, struct config *cfg, GtkBuilder *builder)
215 GtkLabel *w_id, *w_type, *w_high_threshold_unit, *w_low_threshold_unit,
218 GtkToggleButton *w_draw, *w_alarm, *w_appindicator_enabled,
219 *w_appindicator_label_enabled, *w_display;
220 GtkColorButton *w_color;
221 GtkSpinButton *w_high_threshold, *w_low_threshold;
227 w_id = GTK_LABEL(gtk_builder_get_object(builder, "sensor_id"));
228 gtk_label_set_text(w_id, s->id);
230 w_type = GTK_LABEL(gtk_builder_get_object(builder, "sensor_type"));
231 gtk_label_set_text(w_type, psensor_type_to_str(s->type));
233 w_name = GTK_ENTRY(gtk_builder_get_object(builder, "sensor_name"));
234 gtk_entry_set_text(w_name, p->name);
236 w_chipname = GTK_LABEL(gtk_builder_get_object(builder, "chip_name"));
238 gtk_label_set_text(w_chipname, s->chip);
240 gtk_label_set_text(w_chipname, _("Unknown"));
242 w_draw = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
244 gtk_toggle_button_set_active(w_draw, p->graph_enabled);
246 w_display = GTK_TOGGLE_BUTTON(gtk_builder_get_object
248 "sensor_enable_checkbox"));
249 gtk_toggle_button_set_active(w_display, p->display_enabled);
251 w_color = GTK_COLOR_BUTTON(gtk_builder_get_object(builder,
253 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(w_color), p->color);
255 w_alarm = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
257 w_high_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
259 "sensor_alarm_high_threshold"));
260 w_low_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
262 "sensor_alarm_low_threshold"));
264 w_high_threshold_unit = GTK_LABEL(gtk_builder_get_object
266 "sensor_alarm_high_threshold_unit"));
267 w_low_threshold_unit = GTK_LABEL(gtk_builder_get_object
269 "sensor_alarm_low_threshold_unit"));
271 use_celsius = cfg->temperature_unit == CELSIUS ? 1 : 0;
272 gtk_label_set_text(w_high_threshold_unit,
273 psensor_type_to_unit_str(s->type,
275 gtk_label_set_text(w_low_threshold_unit,
276 psensor_type_to_unit_str(s->type,
279 w_appindicator_enabled = GTK_TOGGLE_BUTTON
280 (gtk_builder_get_object(builder, "indicator_checkbox"));
281 w_appindicator_label_enabled = GTK_TOGGLE_BUTTON
282 (gtk_builder_get_object(builder, "indicator_label_checkbox"));
285 if (is_appindicator_supported()) {
286 gtk_widget_set_has_tooltip
287 (GTK_WIDGET(w_appindicator_label_enabled), FALSE);
288 gtk_widget_set_has_tooltip
289 (GTK_WIDGET(w_appindicator_enabled), FALSE);
291 gtk_widget_set_sensitive
292 (GTK_WIDGET(w_appindicator_label_enabled), FALSE);
293 gtk_widget_set_has_tooltip
294 (GTK_WIDGET(w_appindicator_label_enabled), TRUE);
295 gtk_widget_set_sensitive
296 (GTK_WIDGET(w_appindicator_enabled), FALSE);
297 gtk_widget_set_has_tooltip
298 (GTK_WIDGET(w_appindicator_enabled), TRUE);
301 gtk_toggle_button_set_active(w_alarm, p->alarm_enabled);
302 gtk_spin_button_set_value(w_high_threshold, p->alarm_high_threshold);
303 gtk_spin_button_set_value(w_low_threshold, p->alarm_low_threshold);
304 gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), TRUE);
305 gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), TRUE);
306 gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), TRUE);
308 gtk_toggle_button_set_active(w_appindicator_enabled,
309 p->appindicator_enabled);
311 gtk_toggle_button_set_active(w_appindicator_label_enabled,
312 p->appindicator_label_enabled);
315 static void on_changed(GtkTreeSelection *selection, gpointer data)
317 struct cb_data *cbdata = data;
318 struct ui_psensor *ui = cbdata->ui;
319 struct sensor_pref *p;
322 tree = GTK_TREE_VIEW(gtk_builder_get_object(cbdata->builder,
324 p = get_selected_sensor_pref(tree);
325 update_pref(p, ui->config, cbdata->builder);
329 select_sensor(struct psensor *s, struct psensor **sensors, GtkTreeView *tree)
331 struct psensor **s_cur;
334 GtkTreeSelection *sel;
337 for (s_cur = sensors, i = 0; *s_cur; s_cur++, i++)
339 p = gtk_tree_path_new_from_indices(i, -1);
344 sel = gtk_tree_view_get_selection(tree);
346 gtk_tree_selection_select_path(sel, p);
347 gtk_tree_path_free(p);
351 static void apply_pref(struct sensor_pref *p, int pos, struct config *cfg)
357 if (strcmp(p->name, s->name)) {
359 s->name = strdup(p->name);
360 config_set_sensor_name(s->id, s->name);
363 config_set_sensor_graph_enabled(s->id, p->graph_enabled);
365 if (is_temp_type(s->type) && cfg->temperature_unit == FAHRENHEIT) {
366 s->alarm_high_threshold
367 = fahrenheit_to_celsius(p->alarm_high_threshold);
368 s->alarm_low_threshold
369 = fahrenheit_to_celsius(p->alarm_low_threshold);
371 s->alarm_high_threshold = p->alarm_high_threshold;
372 s->alarm_low_threshold = p->alarm_low_threshold;
375 config_set_sensor_alarm_high_threshold(s->id, s->alarm_high_threshold);
376 config_set_sensor_alarm_low_threshold(s->id, s->alarm_low_threshold);
378 config_set_sensor_alarm_enabled(s->id, p->alarm_enabled);
380 config_set_sensor_color(s->id, p->color);
382 config_set_appindicator_enabled(s->id, p->appindicator_enabled);
384 config_set_appindicator_label_enabled(s->id,
385 p->appindicator_label_enabled);
387 config_set_sensor_position(s->id, pos);
389 config_set_sensor_enabled(s->id, p->display_enabled);
392 static void apply_prefs(GtkTreeModel *model, struct config *cfg)
395 struct sensor_pref *spref;
399 valid = gtk_tree_model_get_iter_first(model, &iter);
402 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
403 apply_pref(spref, i, cfg);
404 valid = gtk_tree_model_iter_next(model, &iter);
410 void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
417 GtkTreeView *w_sensors_list;
419 struct psensor **s_cur, *s, **ordered_sensors;
420 GtkTreeSelection *selection;
421 struct cb_data cbdata;
423 struct sensor_pref *spref;
429 builder = gtk_builder_new();
430 cbdata.builder = builder;
433 ok = gtk_builder_add_from_file
435 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "sensor-edit.glade",
439 log_printf(LOG_ERR, error->message);
445 = GTK_TREE_VIEW(gtk_builder_get_object(builder,
447 gtk_builder_connect_signals(builder, w_sensors_list);
449 store = GTK_LIST_STORE(gtk_builder_get_object(builder,
450 "sensors_liststore"));
452 ordered_sensors = ui_get_sensors_ordered_by_position(ui);
453 for (s_cur = ordered_sensors; *s_cur; s_cur++) {
455 gtk_list_store_append(store, &iter);
457 spref = sensor_pref_new(s, ui->config);
458 gtk_list_store_set(store, &iter,
460 COL_SENSOR_PREF, spref,
464 update_pref(spref, ui->config, builder);
467 selection = gtk_tree_view_get_selection(w_sensors_list);
468 g_signal_connect(selection, "changed", G_CALLBACK(on_changed), &cbdata);
469 select_sensor(sensor, ordered_sensors, w_sensors_list);
471 free(ordered_sensors);
473 diag = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
474 result = gtk_dialog_run(diag);
476 model = gtk_tree_view_get_model(w_sensors_list);
478 if (result == GTK_RESPONSE_ACCEPT) {
479 apply_prefs(model, ui->config);
480 ui_sensorlist_update(ui, 1);
481 #if defined(HAVE_APPINDICATOR)
482 ui_appindicator_update_menu(ui);
486 valid = gtk_tree_model_get_iter_first(model, &iter);
488 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
489 sensor_pref_free(spref);
490 valid = gtk_tree_model_iter_next(model, &iter);
493 g_object_unref(G_OBJECT(builder));
495 gtk_widget_destroy(GTK_WIDGET(diag));