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>
38 struct psensor *sensor;
43 int alarm_high_threshold;
44 int alarm_low_threshold;
45 bool appindicator_enabled;
46 unsigned int appindicator_label_enabled;
47 unsigned int display_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->graph_enabled = config_is_sensor_graph_enabled(s->id);
65 p->alarm_enabled = config_get_sensor_alarm_enabled(s->id);
66 p->color = color_dup(s->color);
67 p->display_enabled = config_is_sensor_enabled(s->id);
69 if (cfg->temperature_unit == CELSIUS) {
70 p->alarm_high_threshold = s->alarm_high_threshold;
71 p->alarm_low_threshold = s->alarm_low_threshold;
73 p->alarm_high_threshold
74 = celsius_to_fahrenheit(s->alarm_high_threshold);
75 p->alarm_low_threshold
76 = celsius_to_fahrenheit(s->alarm_low_threshold);
79 p->appindicator_enabled = config_is_appindicator_enabled(s->id);
80 p->appindicator_label_enabled
81 = config_is_appindicator_label_enabled(s->id);
86 static void sensor_pref_free(struct sensor_pref *p)
97 static struct sensor_pref *get_selected_sensor_pref(GtkTreeView *tree)
101 struct sensor_pref *pref;
102 GtkTreeSelection *selection;
104 selection = gtk_tree_view_get_selection(tree);
107 if (gtk_tree_selection_get_selected(selection, &model, &iter))
108 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &pref, -1);
113 void ui_sensorpref_name_changed_cb(GtkEntry *entry, gpointer data)
115 struct sensor_pref *p;
118 str = gtk_entry_get_text(entry);
120 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
122 if (p && strcmp(p->name, str)) {
124 p->name = strdup(str);
128 void ui_sensorpref_draw_toggled_cb(GtkToggleButton *btn, gpointer data)
130 struct sensor_pref *p;
132 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
135 p->graph_enabled = gtk_toggle_button_get_active(btn);
138 void ui_sensorpref_display_toggled_cb(GtkToggleButton *btn, gpointer data)
140 struct sensor_pref *p;
142 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
145 p->display_enabled = gtk_toggle_button_get_active(btn);
148 void ui_sensorpref_alarm_toggled_cb(GtkToggleButton *btn, gpointer data)
150 struct sensor_pref *p;
152 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
155 p->alarm_enabled = gtk_toggle_button_get_active(btn);
159 ui_sensorpref_appindicator_menu_toggled_cb(GtkToggleButton *btn, gpointer data)
161 struct sensor_pref *p;
163 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
166 p->appindicator_enabled = gtk_toggle_button_get_active(btn);
170 ui_sensorpref_appindicator_label_toggled_cb(GtkToggleButton *btn, gpointer data)
172 struct sensor_pref *p;
174 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
177 p->appindicator_label_enabled
178 = gtk_toggle_button_get_active(btn);
181 void ui_sensorpref_color_set_cb(GtkColorButton *widget, gpointer data)
183 struct sensor_pref *p;
186 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
189 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), &color);
190 color_set(p->color, color.red, color.green, color.blue);
195 ui_sensorpref_alarm_high_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_high_threshold = gtk_spin_button_get_value(btn);
206 ui_sensorpref_alarm_low_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
208 struct sensor_pref *p;
210 p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
213 p->alarm_low_threshold = gtk_spin_button_get_value(btn);
217 update_pref(struct sensor_pref *p, struct config *cfg, GtkBuilder *builder)
219 GtkLabel *w_id, *w_type, *w_high_threshold_unit, *w_low_threshold_unit,
222 GtkToggleButton *w_draw, *w_alarm, *w_appindicator_enabled,
223 *w_appindicator_label_enabled, *w_display;
224 GtkColorButton *w_color;
225 GtkSpinButton *w_high_threshold, *w_low_threshold;
232 w_id = GTK_LABEL(gtk_builder_get_object(builder, "sensor_id"));
233 gtk_label_set_text(w_id, s->id);
235 w_type = GTK_LABEL(gtk_builder_get_object(builder, "sensor_type"));
236 gtk_label_set_text(w_type, psensor_type_to_str(s->type));
238 w_name = GTK_ENTRY(gtk_builder_get_object(builder, "sensor_name"));
239 gtk_entry_set_text(w_name, p->name);
241 w_chipname = GTK_LABEL(gtk_builder_get_object(builder, "chip_name"));
243 gtk_label_set_text(w_chipname, s->chip);
245 gtk_label_set_text(w_chipname, _("Unknown"));
247 w_draw = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
249 gtk_toggle_button_set_active(w_draw, p->graph_enabled);
251 w_display = GTK_TOGGLE_BUTTON(gtk_builder_get_object
253 "sensor_enable_checkbox"));
254 gtk_toggle_button_set_active(w_display, p->display_enabled);
256 color = color_to_GdkRGBA(p->color);
257 w_color = GTK_COLOR_BUTTON(gtk_builder_get_object(builder,
259 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(w_color), &color);
261 w_alarm = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
263 w_high_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
265 "sensor_alarm_high_threshold"));
266 w_low_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
268 "sensor_alarm_low_threshold"));
270 w_high_threshold_unit = GTK_LABEL(gtk_builder_get_object
272 "sensor_alarm_high_threshold_unit"));
273 w_low_threshold_unit = GTK_LABEL(gtk_builder_get_object
275 "sensor_alarm_low_threshold_unit"));
277 use_celsius = cfg->temperature_unit == CELSIUS ? 1 : 0;
278 gtk_label_set_text(w_high_threshold_unit,
279 psensor_type_to_unit_str(s->type,
281 gtk_label_set_text(w_low_threshold_unit,
282 psensor_type_to_unit_str(s->type,
285 w_appindicator_enabled = GTK_TOGGLE_BUTTON
286 (gtk_builder_get_object(builder, "indicator_checkbox"));
287 w_appindicator_label_enabled = GTK_TOGGLE_BUTTON
288 (gtk_builder_get_object(builder, "indicator_label_checkbox"));
291 if (is_appindicator_supported()) {
292 gtk_widget_set_has_tooltip
293 (GTK_WIDGET(w_appindicator_label_enabled), FALSE);
294 gtk_widget_set_has_tooltip
295 (GTK_WIDGET(w_appindicator_enabled), FALSE);
297 gtk_widget_set_sensitive
298 (GTK_WIDGET(w_appindicator_label_enabled), FALSE);
299 gtk_widget_set_has_tooltip
300 (GTK_WIDGET(w_appindicator_label_enabled), TRUE);
301 gtk_widget_set_sensitive
302 (GTK_WIDGET(w_appindicator_enabled), FALSE);
303 gtk_widget_set_has_tooltip
304 (GTK_WIDGET(w_appindicator_enabled), TRUE);
307 gtk_toggle_button_set_active(w_alarm, p->alarm_enabled);
308 gtk_spin_button_set_value(w_high_threshold, p->alarm_high_threshold);
309 gtk_spin_button_set_value(w_low_threshold, p->alarm_low_threshold);
310 gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), TRUE);
311 gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), TRUE);
312 gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), TRUE);
314 gtk_toggle_button_set_active(w_appindicator_enabled,
315 p->appindicator_enabled);
317 gtk_toggle_button_set_active(w_appindicator_label_enabled,
318 p->appindicator_label_enabled);
321 static void on_changed(GtkTreeSelection *selection, gpointer data)
323 struct cb_data *cbdata = data;
324 struct ui_psensor *ui = cbdata->ui;
325 struct sensor_pref *p;
328 tree = GTK_TREE_VIEW(gtk_builder_get_object(cbdata->builder,
330 p = get_selected_sensor_pref(tree);
331 update_pref(p, ui->config, cbdata->builder);
335 select_sensor(struct psensor *s, struct psensor **sensors, GtkTreeView *tree)
337 struct psensor **s_cur;
340 GtkTreeSelection *sel;
343 for (s_cur = sensors, i = 0; *s_cur; s_cur++, i++)
345 p = gtk_tree_path_new_from_indices(i, -1);
350 sel = gtk_tree_view_get_selection(tree);
352 gtk_tree_selection_select_path(sel, p);
353 gtk_tree_path_free(p);
357 static void apply_pref(struct sensor_pref *p, int pos, struct config *cfg)
363 if (strcmp(p->name, s->name)) {
365 s->name = strdup(p->name);
366 config_set_sensor_name(s->id, s->name);
369 config_set_sensor_graph_enabled(s->id, p->graph_enabled);
371 if (is_temp_type(s->type) && cfg->temperature_unit == FAHRENHEIT) {
372 s->alarm_high_threshold
373 = fahrenheit_to_celsius(p->alarm_high_threshold);
374 s->alarm_low_threshold
375 = fahrenheit_to_celsius(p->alarm_low_threshold);
377 s->alarm_high_threshold = p->alarm_high_threshold;
378 s->alarm_low_threshold = p->alarm_low_threshold;
381 config_set_sensor_alarm_high_threshold(s->id, s->alarm_high_threshold);
382 config_set_sensor_alarm_low_threshold(s->id, s->alarm_low_threshold);
384 config_set_sensor_alarm_enabled(s->id, p->alarm_enabled);
390 config_set_sensor_color(s->id, s->color);
392 config_set_appindicator_enabled(s->id, p->appindicator_enabled);
394 config_set_appindicator_label_enabled(s->id,
395 p->appindicator_label_enabled);
397 config_set_sensor_position(s->id, pos);
399 config_set_sensor_enabled(s->id, p->display_enabled);
402 static void apply_prefs(GtkTreeModel *model, struct config *cfg)
405 struct sensor_pref *spref;
409 valid = gtk_tree_model_get_iter_first(model, &iter);
412 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
413 apply_pref(spref, i, cfg);
414 valid = gtk_tree_model_iter_next(model, &iter);
420 void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
427 GtkTreeView *w_sensors_list;
429 struct psensor **s_cur, *s, **ordered_sensors;
430 GtkTreeSelection *selection;
431 struct cb_data cbdata;
433 struct sensor_pref *spref;
439 builder = gtk_builder_new();
440 cbdata.builder = builder;
443 ok = gtk_builder_add_from_file
445 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "sensor-edit.glade",
449 log_printf(LOG_ERR, error->message);
455 = GTK_TREE_VIEW(gtk_builder_get_object(builder,
457 gtk_builder_connect_signals(builder, w_sensors_list);
459 store = GTK_LIST_STORE(gtk_builder_get_object(builder,
460 "sensors_liststore"));
462 ordered_sensors = ui_get_sensors_ordered_by_position(ui);
463 for (s_cur = ordered_sensors; *s_cur; s_cur++) {
465 gtk_list_store_append(store, &iter);
467 spref = sensor_pref_new(s, ui->config);
468 gtk_list_store_set(store, &iter,
470 COL_SENSOR_PREF, spref,
474 update_pref(spref, ui->config, builder);
477 selection = gtk_tree_view_get_selection(w_sensors_list);
478 g_signal_connect(selection, "changed", G_CALLBACK(on_changed), &cbdata);
479 select_sensor(sensor, ordered_sensors, w_sensors_list);
481 free(ordered_sensors);
483 diag = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
484 result = gtk_dialog_run(diag);
486 model = gtk_tree_view_get_model(w_sensors_list);
488 if (result == GTK_RESPONSE_ACCEPT) {
489 apply_prefs(model, ui->config);
490 ui_sensorlist_update(ui, 1);
491 #if defined(HAVE_APPINDICATOR)
492 ui_appindicator_update_menu(ui);
496 valid = gtk_tree_model_get_iter_first(model, &iter);
498 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
499 sensor_pref_free(spref);
500 valid = gtk_tree_model_iter_next(model, &iter);
503 g_object_unref(G_OBJECT(builder));
505 gtk_widget_destroy(GTK_WIDGET(diag));