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 ui_psensor *ui;
41 static struct psensor *get_selected_sensor(GtkBuilder *builder)
46 GtkTreeSelection *selection;
49 tree = GTK_TREE_VIEW(gtk_builder_get_object(builder, "sensors_list"));
51 selection = gtk_tree_view_get_selection(tree);
54 if (gtk_tree_selection_get_selected(selection, &model, &iter))
55 gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &s, -1);
60 static void apply_config(struct ui_psensor *ui)
64 ui_sensorlist_update(ui, 1);
65 ui_appindicator_update_menu(ui);
68 void ui_sensorpref_name_changed_cb(GtkEntry *entry, gpointer data)
71 struct cb_data *cbdata;
75 cbdata = (struct cb_data *)data;
77 s = get_selected_sensor(cbdata->builder);
79 str = gtk_entry_get_text(entry);
82 if (strcmp(str, s->name)) {
84 s->name = strdup(str);
85 config_set_sensor_name(s->id, str);
87 apply_config(cbdata->ui);
92 void ui_sensorpref_draw_toggled_cb(GtkToggleButton *btn, gpointer data)
95 struct cb_data *cbdata;
98 cbdata = (struct cb_data *)data;
100 s = get_selected_sensor(cbdata->builder);
102 active = gtk_toggle_button_get_active(btn);
103 config_set_sensor_graph_enabled(s->id, active);
105 apply_config(cbdata->ui);
109 void ui_sensorpref_display_toggled_cb(GtkToggleButton *btn, gpointer data)
112 struct cb_data *cbdata;
115 cbdata = (struct cb_data *)data;
117 s = get_selected_sensor(cbdata->builder);
120 active = gtk_toggle_button_get_active(btn);
121 config_set_sensor_enabled(s->id, active);
123 apply_config(cbdata->ui);
128 void ui_sensorpref_alarm_toggled_cb(GtkToggleButton *btn, gpointer data)
131 struct cb_data *cbdata;
134 cbdata = (struct cb_data *)data;
136 s = get_selected_sensor(cbdata->builder);
139 active = gtk_toggle_button_get_active(btn);
140 config_set_sensor_alarm_enabled(s->id, active);
142 apply_config(cbdata->ui);
147 ui_sensorpref_appindicator_menu_toggled_cb(GtkToggleButton *btn, gpointer data)
150 struct cb_data *cbdata;
153 cbdata = (struct cb_data *)data;
155 s = get_selected_sensor(cbdata->builder);
158 active = gtk_toggle_button_get_active(btn);
159 config_set_appindicator_enabled(s->id, active);
161 apply_config(cbdata->ui);
166 ui_sensorpref_appindicator_label_toggled_cb(GtkToggleButton *btn, gpointer data)
169 struct cb_data *cbdata;
172 cbdata = (struct cb_data *)data;
174 s = get_selected_sensor(cbdata->builder);
177 active = gtk_toggle_button_get_active(btn);
178 config_set_appindicator_label_enabled(s->id, active);
180 apply_config(cbdata->ui);
184 void ui_sensorpref_color_set_cb(GtkColorButton *widget, gpointer data)
186 struct cb_data *cbdata;
190 cbdata = (struct cb_data *)data;
192 s = get_selected_sensor(cbdata->builder);
195 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), &color);
196 config_set_sensor_color(s->id, &color);
198 apply_config(cbdata->ui);
203 ui_sensorpref_alarm_high_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
205 struct cb_data *cbdata;
209 cbdata = (struct cb_data *)data;
211 s = get_selected_sensor(cbdata->builder);
214 v = gtk_spin_button_get_value(btn);
215 config_set_sensor_alarm_high_threshold(s->id, v);
217 apply_config(cbdata->ui);
219 s->alarm_high_threshold = v;
224 ui_sensorpref_alarm_low_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
226 struct cb_data *cbdata;
230 cbdata = (struct cb_data *)data;
232 s = get_selected_sensor(cbdata->builder);
235 v = gtk_spin_button_get_value(btn);
236 config_set_sensor_alarm_low_threshold(s->id, v);
238 apply_config(cbdata->ui);
240 s->alarm_low_threshold = v;
245 update_pref(struct psensor *s, struct config *cfg, GtkBuilder *builder)
247 GtkLabel *w_id, *w_type, *w_high_threshold_unit, *w_low_threshold_unit,
250 GtkToggleButton *w_draw, *w_alarm, *w_appindicator_enabled,
251 *w_appindicator_label_enabled, *w_display;
252 GtkColorButton *w_color;
253 GtkSpinButton *w_high_threshold, *w_low_threshold;
254 int use_celsius, threshold;
257 w_id = GTK_LABEL(gtk_builder_get_object(builder, "sensor_id"));
258 gtk_label_set_text(w_id, s->id);
260 w_type = GTK_LABEL(gtk_builder_get_object(builder, "sensor_type"));
261 gtk_label_set_text(w_type, psensor_type_to_str(s->type));
263 w_name = GTK_ENTRY(gtk_builder_get_object(builder, "sensor_name"));
264 gtk_entry_set_text(w_name, s->name);
266 w_chipname = GTK_LABEL(gtk_builder_get_object(builder, "chip_name"));
268 gtk_label_set_text(w_chipname, s->chip);
270 gtk_label_set_text(w_chipname, _("Unknown"));
272 w_draw = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
274 gtk_toggle_button_set_active(w_draw,
275 config_is_sensor_graph_enabled(s->id));
277 w_display = GTK_TOGGLE_BUTTON(gtk_builder_get_object
279 "sensor_enable_checkbox"));
280 gtk_toggle_button_set_active(w_display,
281 config_is_sensor_enabled(s->id));
283 w_color = GTK_COLOR_BUTTON(gtk_builder_get_object(builder,
285 color = config_get_sensor_color(s->id);
286 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(w_color), color);
287 gdk_rgba_free(color);
289 w_alarm = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
291 w_high_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
293 "sensor_alarm_high_threshold"));
294 w_low_threshold = GTK_SPIN_BUTTON(gtk_builder_get_object
296 "sensor_alarm_low_threshold"));
298 w_high_threshold_unit = GTK_LABEL(gtk_builder_get_object
300 "sensor_alarm_high_threshold_unit"));
301 w_low_threshold_unit = GTK_LABEL(gtk_builder_get_object
303 "sensor_alarm_low_threshold_unit"));
305 use_celsius = cfg->temperature_unit == CELSIUS ? 1 : 0;
306 gtk_label_set_text(w_high_threshold_unit,
307 psensor_type_to_unit_str(s->type, use_celsius));
308 gtk_label_set_text(w_low_threshold_unit,
309 psensor_type_to_unit_str(s->type, use_celsius));
311 w_appindicator_enabled = GTK_TOGGLE_BUTTON
312 (gtk_builder_get_object(builder, "indicator_checkbox"));
313 w_appindicator_label_enabled = GTK_TOGGLE_BUTTON
314 (gtk_builder_get_object(builder, "indicator_label_checkbox"));
316 if (is_appindicator_supported()) {
317 gtk_widget_set_has_tooltip
318 (GTK_WIDGET(w_appindicator_label_enabled), FALSE);
319 gtk_widget_set_has_tooltip
320 (GTK_WIDGET(w_appindicator_enabled), FALSE);
322 gtk_widget_set_sensitive
323 (GTK_WIDGET(w_appindicator_label_enabled), FALSE);
324 gtk_widget_set_has_tooltip
325 (GTK_WIDGET(w_appindicator_label_enabled), TRUE);
326 gtk_widget_set_sensitive
327 (GTK_WIDGET(w_appindicator_enabled), FALSE);
328 gtk_widget_set_has_tooltip
329 (GTK_WIDGET(w_appindicator_enabled), TRUE);
332 gtk_toggle_button_set_active(w_alarm,
333 config_get_sensor_alarm_enabled(s->id));
335 threshold = config_get_sensor_alarm_high_threshold(s->id);
337 threshold = celsius_to_fahrenheit(threshold);
338 gtk_spin_button_set_value(w_high_threshold, threshold);
340 threshold = config_get_sensor_alarm_low_threshold(s->id);
342 threshold = celsius_to_fahrenheit(threshold);
343 gtk_spin_button_set_value(w_low_threshold, threshold);
345 gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), TRUE);
346 gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), TRUE);
347 gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), TRUE);
349 gtk_toggle_button_set_active(w_appindicator_enabled,
350 config_is_appindicator_enabled(s->id));
352 gtk_toggle_button_set_active
353 (w_appindicator_label_enabled,
354 config_is_appindicator_label_enabled(s->id));
357 static void on_changed(GtkTreeSelection *selection, gpointer data)
359 struct cb_data *cbdata = data;
360 struct ui_psensor *ui = cbdata->ui;
363 s = get_selected_sensor(cbdata->builder);
365 update_pref(s, ui->config, cbdata->builder);
369 select_sensor(struct psensor *s, struct psensor **sensors, GtkTreeView *tree)
371 struct psensor **s_cur;
374 GtkTreeSelection *sel;
377 for (s_cur = sensors, i = 0; *s_cur; s_cur++, i++)
379 p = gtk_tree_path_new_from_indices(i, -1);
384 sel = gtk_tree_view_get_selection(tree);
386 gtk_tree_selection_select_path(sel, p);
387 gtk_tree_path_free(p);
391 void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
397 GtkTreeView *w_sensors_list;
399 struct psensor **s_cur, *s, **ordered_sensors;
400 GtkTreeSelection *selection;
401 struct cb_data cbdata;
406 builder = gtk_builder_new();
407 cbdata.builder = builder;
410 ok = gtk_builder_add_from_file
412 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "sensor-edit.glade",
416 log_printf(LOG_ERR, error->message);
422 = GTK_TREE_VIEW(gtk_builder_get_object(builder,
425 store = GTK_LIST_STORE(gtk_builder_get_object(builder,
426 "sensors_liststore"));
428 ordered_sensors = ui_get_sensors_ordered_by_position(ui);
429 for (s_cur = ordered_sensors; *s_cur; s_cur++) {
431 gtk_list_store_append(store, &iter);
433 gtk_list_store_set(store, &iter,
439 update_pref(s, ui->config, builder);
442 selection = gtk_tree_view_get_selection(w_sensors_list);
443 g_signal_connect(selection, "changed", G_CALLBACK(on_changed), &cbdata);
444 select_sensor(sensor, ordered_sensors, w_sensors_list);
446 free(ordered_sensors);
448 diag = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
450 gtk_builder_connect_signals(builder, &cbdata);
452 gtk_dialog_run(diag);
454 g_object_unref(G_OBJECT(builder));
456 gtk_widget_destroy(GTK_WIDGET(diag));