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
25 #include "ui_sensorlist.h"
26 #include "ui_sensorpref.h"
29 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
30 #include "ui_appindicator.h"
39 struct psensor *sensor;
44 int alarm_high_threshold;
45 int alarm_low_threshold;
46 unsigned int appindicator_enabled;
47 unsigned int appindicator_label_enabled;
51 struct ui_psensor *ui;
55 static struct sensor_pref *
56 sensor_pref_new(struct psensor *s, struct config *cfg)
58 struct sensor_pref *p;
60 p = malloc(sizeof(struct sensor_pref));
63 p->name = strdup(s->name);
64 p->enabled = s->graph_enabled;
65 p->alarm_enabled = s->alarm_enabled;
66 p->color = color_dup(s->color);
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 = s->appindicator_enabled;
79 p->appindicator_label_enabled
80 = config_is_appindicator_label_enabled(s->id);
85 static void sensor_pref_free(struct sensor_pref *p)
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->enabled = gtk_toggle_button_get_active(btn);
137 void ui_sensorpref_alarm_toggled_cb(GtkToggleButton *btn, gpointer data)
139 struct sensor_pref *p;
141 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
144 p->alarm_enabled = gtk_toggle_button_get_active(btn);
148 ui_sensorpref_appindicator_menu_toggled_cb(GtkToggleButton *btn, gpointer data)
150 struct sensor_pref *p;
152 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
155 p->appindicator_enabled = gtk_toggle_button_get_active(btn);
159 ui_sensorpref_appindicator_label_toggled_cb(GtkToggleButton *btn, gpointer data)
161 struct sensor_pref *p;
163 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
166 p->appindicator_label_enabled
167 = gtk_toggle_button_get_active(btn);
170 void ui_sensorpref_color_set_cb(GtkColorButton *widget, gpointer data)
172 struct sensor_pref *p;
175 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
178 gtk_color_button_get_color(widget, &color);
179 color_set(p->color, color.red, color.green, color.blue);
184 ui_sensorpref_alarm_high_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
186 struct sensor_pref *p;
188 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
191 p->alarm_high_threshold = gtk_spin_button_get_value(btn);
195 ui_sensorpref_alarm_low_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
197 struct sensor_pref *p;
199 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
202 p->alarm_low_threshold = gtk_spin_button_get_value(btn);
206 update_pref(struct sensor_pref *p, struct config *cfg, GtkBuilder *builder)
208 GtkLabel *w_id, *w_type, *w_high_threshold_unit, *w_low_threshold_unit,
211 GtkToggleButton *w_draw, *w_alarm, *w_appindicator_enabled,
212 *w_appindicator_label_enabled;
213 GtkColorButton *w_color;
214 GtkSpinButton *w_high_threshold, *w_low_threshold;
221 w_id = GTK_LABEL(gtk_builder_get_object(builder, "sensor_id"));
222 gtk_label_set_text(w_id, s->id);
224 w_type = GTK_LABEL(gtk_builder_get_object(builder, "sensor_type"));
225 gtk_label_set_text(w_type, psensor_type_to_str(s->type));
227 w_name = GTK_ENTRY(gtk_builder_get_object(builder, "sensor_name"));
228 gtk_entry_set_text(w_name, p->name);
230 w_chipname = GTK_LABEL(gtk_builder_get_object(builder, "chip_name"));
232 gtk_label_set_text(w_chipname, s->chip);
234 gtk_label_set_text(w_chipname, _("Unknown"));
236 w_draw = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
238 gtk_toggle_button_set_active(w_draw, p->enabled);
240 color = color_to_gdkcolor(p->color);
241 w_color = GTK_COLOR_BUTTON(gtk_builder_get_object(builder,
243 gtk_color_button_set_color(w_color, color);
245 w_alarm = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
247 w_high_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
249 "sensor_alarm_high_threshold"));
250 w_low_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
252 "sensor_alarm_low_threshold"));
254 w_high_threshold_unit = GTK_LABEL(gtk_builder_get_object
256 "sensor_alarm_high_threshold_unit"));
257 w_low_threshold_unit = GTK_LABEL(gtk_builder_get_object
259 "sensor_alarm_low_threshold_unit"));
261 use_celsius = cfg->temperature_unit == CELSIUS ? 1 : 0;
262 gtk_label_set_text(w_high_threshold_unit,
263 psensor_type_to_unit_str(s->type,
265 gtk_label_set_text(w_low_threshold_unit,
266 psensor_type_to_unit_str(s->type,
269 w_appindicator_enabled = GTK_TOGGLE_BUTTON
270 (gtk_builder_get_object(builder, "indicator_checkbox"));
271 w_appindicator_label_enabled = GTK_TOGGLE_BUTTON
272 (gtk_builder_get_object(builder, "indicator_label_checkbox"));
275 if (is_temp_type(s->type) || is_fan_type(s->type)) {
276 gtk_toggle_button_set_active(w_alarm, p->alarm_enabled);
277 gtk_spin_button_set_value(w_high_threshold,
278 p->alarm_high_threshold);
279 gtk_spin_button_set_value(w_low_threshold,
280 p->alarm_low_threshold);
281 gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), TRUE);
282 gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), TRUE);
283 gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), TRUE);
285 gtk_toggle_button_set_active(w_alarm, 0);
286 gtk_spin_button_set_value(w_high_threshold, 0);
287 gtk_spin_button_set_value(w_low_threshold, 0);
288 gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), FALSE);
289 gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), FALSE);
290 gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), FALSE);
293 gtk_toggle_button_set_active(w_appindicator_enabled,
294 p->appindicator_enabled);
296 gtk_toggle_button_set_active(w_appindicator_label_enabled,
297 p->appindicator_label_enabled);
300 static void on_changed(GtkTreeSelection *selection, gpointer data)
302 struct cb_data *cbdata = data;
303 struct ui_psensor *ui = cbdata->ui;
304 struct sensor_pref *p;
307 tree = GTK_TREE_VIEW(gtk_builder_get_object(cbdata->builder,
309 p = get_selected_sensor_pref(tree);
310 update_pref(p, ui->config, cbdata->builder);
314 select_sensor(struct psensor *s, struct psensor **sensors, GtkTreeView *tree)
316 struct psensor **s_cur;
321 for (s_cur = sensors, i = 0; *s_cur; s_cur++, i++)
323 p = gtk_tree_path_new_from_indices(i, -1);
328 GtkTreeSelection *s = gtk_tree_view_get_selection(tree);
330 gtk_tree_selection_select_path(s, p);
331 gtk_tree_path_free(p);
335 static void apply_pref(struct sensor_pref *p, int pos, struct config *cfg)
341 if (strcmp(p->name, s->name)) {
343 s->name = strdup(p->name);
344 config_set_sensor_name(s->id, s->name);
347 if (s->graph_enabled != p->enabled) {
348 s->graph_enabled = p->enabled;
349 config_set_sensor_enabled(s->id, s->graph_enabled);
352 if (is_temp_type(s->type) && cfg->temperature_unit == FAHRENHEIT) {
353 s->alarm_high_threshold
354 = fahrenheit_to_celsius(p->alarm_high_threshold);
355 s->alarm_low_threshold
356 = fahrenheit_to_celsius(p->alarm_low_threshold);
358 s->alarm_high_threshold = p->alarm_high_threshold;
359 s->alarm_low_threshold = p->alarm_low_threshold;
362 config_set_sensor_alarm_high_threshold(s->id, s->alarm_high_threshold);
363 config_set_sensor_alarm_low_threshold(s->id, s->alarm_low_threshold);
365 if (s->alarm_enabled != p->alarm_enabled) {
366 s->alarm_enabled = p->alarm_enabled;
367 config_set_sensor_alarm_enabled(s->id, s->alarm_enabled);
370 color_set(s->color, p->color->red, p->color->green, p->color->blue);
371 config_set_sensor_color(s->id, s->color);
373 if (s->appindicator_enabled != p->appindicator_enabled) {
374 s->appindicator_enabled = p->appindicator_enabled;
375 config_set_appindicator_enabled(s->id, s->appindicator_enabled);
378 config_set_appindicator_label_enabled(s->id,
379 p->appindicator_label_enabled);
381 config_set_sensor_position(s->id, pos);
384 static void apply_prefs(GtkTreeModel *model, struct config *cfg)
387 struct sensor_pref *spref;
391 valid = gtk_tree_model_get_iter_first(model, &iter);
394 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
395 apply_pref(spref, i, cfg);
396 valid = gtk_tree_model_iter_next(model, &iter);
401 void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
408 GtkTreeView *w_sensors_list;
410 struct psensor **s_cur, *s, **ordered_sensors;
411 GtkTreeSelection *selection;
412 struct cb_data cbdata;
414 struct sensor_pref *spref;
420 builder = gtk_builder_new();
421 cbdata.builder = builder;
424 ok = gtk_builder_add_from_file
426 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "sensor-edit.glade",
430 log_printf(LOG_ERR, error->message);
436 = GTK_TREE_VIEW(gtk_builder_get_object(builder,
438 gtk_builder_connect_signals(builder, w_sensors_list);
440 store = GTK_LIST_STORE(gtk_builder_get_object(builder,
441 "sensors_liststore"));
443 ordered_sensors = ui_get_sensors_ordered_by_position(ui);
444 for (s_cur = ordered_sensors; *s_cur; s_cur++) {
446 gtk_list_store_append(store, &iter);
448 spref = sensor_pref_new(s, ui->config);
449 gtk_list_store_set(store, &iter,
451 COL_SENSOR_PREF, spref,
455 update_pref(spref, ui->config, builder);
458 selection = gtk_tree_view_get_selection(w_sensors_list);
459 g_signal_connect(selection, "changed", G_CALLBACK(on_changed), &cbdata);
460 select_sensor(sensor, ordered_sensors, w_sensors_list);
462 free(ordered_sensors);
464 diag = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
465 result = gtk_dialog_run(diag);
467 model = gtk_tree_view_get_model(w_sensors_list);
469 if (result == GTK_RESPONSE_ACCEPT) {
470 apply_prefs(model, ui->config);
471 ui_sensorlist_update(ui, 1);
472 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
473 ui_appindicator_update_menu(ui);
477 valid = gtk_tree_model_get_iter_first(model, &iter);
479 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
480 sensor_pref_free(spref);
481 valid = gtk_tree_model_iter_next(model, &iter);
484 g_object_unref(G_OBJECT(builder));
486 gtk_widget_destroy(GTK_WIDGET(diag));