Imported Upstream version 0.6.2.17
[psensor-pkg-debian.git] / src / ui_pref.c
1 /*
2  * Copyright (C) 2010-2012 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "ui.h"
23 #include "cfg.h"
24 #include "ui_pref.h"
25 #include "ui_color.h"
26 #include "compat.h"
27
28 GdkColor *color_to_gdkcolor(struct color *color)
29 {
30         GdkColor *c = malloc(sizeof(GdkColor));
31
32         c->red = color->red;
33         c->green = color->green;
34         c->blue = color->blue;
35
36         return c;
37 }
38
39 void ui_pref_dialog_run(struct ui_psensor *ui)
40 {
41         GtkDialog *diag;
42         gint result;
43         struct config *cfg;
44         GtkBuilder *builder;
45         guint ok;
46         GError *error = NULL;
47         GdkColor *color_fg, *color_bg;
48         GtkColorButton *w_color_fg, *w_color_bg;
49         GtkHScale *w_bg_opacity;
50         GtkSpinButton *w_update_interval, *w_monitoring_duration,
51                 *w_s_update_interval;
52         GtkComboBox *w_sensorlist_pos;
53         GtkToggleButton *w_hide_window_decoration, *w_keep_window_below,
54                 *w_enable_menu, *w_enable_launcher_counter, *w_hide_on_startup,
55                 *w_win_restore;
56
57         cfg = ui->config;
58
59         builder = gtk_builder_new();
60
61         ok = gtk_builder_add_from_file
62                 (builder,
63                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor-pref.glade",
64                  &error);
65
66         if (!ok) {
67                 log_printf(LOG_ERR, error->message);
68                 g_error_free(error);
69                 return ;
70         }
71
72         diag = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
73
74         color_fg = color_to_gdkcolor(cfg->graph_fgcolor);
75         w_color_fg = GTK_COLOR_BUTTON(gtk_builder_get_object(builder,
76                                                              "color_fg"));
77         gtk_color_button_set_color(w_color_fg, color_fg);
78
79         color_bg = color_to_gdkcolor(cfg->graph_bgcolor);
80         w_color_bg = GTK_COLOR_BUTTON(gtk_builder_get_object(builder,
81                                                              "color_bg"));
82         gtk_color_button_set_color(w_color_bg, color_bg);
83
84         w_bg_opacity = GTK_HSCALE(gtk_builder_get_object(builder,
85                                                          "bg_opacity"));
86         gtk_range_set_value(GTK_RANGE(w_bg_opacity), cfg->graph_bg_alpha);
87
88         w_update_interval = GTK_SPIN_BUTTON(gtk_builder_get_object
89                                             (builder, "update_interval"));
90         gtk_spin_button_set_value(w_update_interval,
91                                   cfg->graph_update_interval);
92
93         w_s_update_interval
94                 = GTK_SPIN_BUTTON(gtk_builder_get_object
95                                   (builder, "sensor_update_interval"));
96         gtk_spin_button_set_value(w_s_update_interval,
97                                   cfg->sensor_update_interval);
98
99         w_monitoring_duration
100                 = GTK_SPIN_BUTTON(gtk_builder_get_object
101                                   (builder, "monitoring_duration"));
102         gtk_spin_button_set_value(w_monitoring_duration,
103                                   cfg->graph_monitoring_duration);
104
105         w_sensorlist_pos = GTK_COMBO_BOX
106                 (gtk_builder_get_object(builder, "sensors_list_position"));
107         gtk_combo_box_set_active(w_sensorlist_pos, cfg->sensorlist_position);
108
109         w_hide_window_decoration = GTK_TOGGLE_BUTTON
110                 (gtk_builder_get_object(builder, "hide_window_decoration"));
111         gtk_toggle_button_set_active(w_hide_window_decoration,
112                                      !cfg->window_decoration_enabled);
113
114         w_keep_window_below = GTK_TOGGLE_BUTTON
115                 (gtk_builder_get_object(builder, "keep_window_below"));
116         gtk_toggle_button_set_active(w_keep_window_below,
117                                      cfg->window_keep_below_enabled);
118
119         w_enable_menu = GTK_TOGGLE_BUTTON
120                 (gtk_builder_get_object(builder, "enable_menu"));
121         gtk_toggle_button_set_active(w_enable_menu, !cfg->menu_bar_disabled);
122
123         w_enable_launcher_counter = GTK_TOGGLE_BUTTON
124                 (gtk_builder_get_object(builder, "enable_launcher_counter"));
125         gtk_toggle_button_set_active(w_enable_launcher_counter,
126                                      !cfg->unity_launcher_count_disabled);
127
128         w_hide_on_startup
129                 = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
130                                                            "hide_on_startup"));
131         gtk_toggle_button_set_active(w_hide_on_startup, cfg->hide_on_startup);
132
133         w_win_restore
134                 = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
135                                                            "restore_window"));
136         gtk_toggle_button_set_active(w_win_restore,
137                                      cfg->window_restore_enabled);
138
139         result = gtk_dialog_run(diag);
140
141         if (result == GTK_RESPONSE_ACCEPT) {
142                 double value;
143                 GdkColor color;
144
145                 g_mutex_lock(ui->sensors_mutex);
146
147                 gtk_color_button_get_color(w_color_fg, &color);
148                 color_set(cfg->graph_fgcolor,
149                           color.red, color.green, color.blue);
150
151                 gtk_color_button_get_color(w_color_bg, &color);
152                 color_set(cfg->graph_bgcolor,
153                           color.red, color.green, color.blue);
154
155                 value = gtk_range_get_value(GTK_RANGE(w_bg_opacity));
156                 cfg->graph_bg_alpha = value;
157
158                 if (value == 1.0)
159                         cfg->alpha_channel_enabled = 0;
160                 else
161                         cfg->alpha_channel_enabled = 1;
162
163                 cfg->sensorlist_position
164                         = gtk_combo_box_get_active(w_sensorlist_pos);
165
166                 cfg->window_decoration_enabled =
167                         !gtk_toggle_button_get_active(w_hide_window_decoration);
168
169                 cfg->window_keep_below_enabled
170                         = gtk_toggle_button_get_active(w_keep_window_below);
171
172                 cfg->menu_bar_disabled
173                         = !gtk_toggle_button_get_active(w_enable_menu);
174
175                 cfg->unity_launcher_count_disabled
176                         = !gtk_toggle_button_get_active
177                         (w_enable_launcher_counter);
178
179                 gtk_window_set_decorated(GTK_WINDOW(ui->main_window),
180                                          cfg->window_decoration_enabled);
181
182                 gtk_window_set_keep_below(GTK_WINDOW(ui->main_window),
183                                           cfg->window_keep_below_enabled);
184
185                 cfg->sensor_update_interval
186                         = gtk_spin_button_get_value_as_int(w_s_update_interval);
187
188                 cfg->graph_update_interval = gtk_spin_button_get_value_as_int
189                         (w_update_interval);
190
191                 cfg->graph_monitoring_duration
192                     = gtk_spin_button_get_value_as_int
193                         (w_monitoring_duration);
194
195                 cfg->sensor_values_max_length
196                     = (cfg->graph_monitoring_duration * 60) /
197                     cfg->sensor_update_interval;
198
199                 cfg->hide_on_startup
200                         = gtk_toggle_button_get_active(w_hide_on_startup);
201
202                 cfg->window_restore_enabled
203                         = gtk_toggle_button_get_active(w_win_restore);
204
205                 config_save(cfg);
206
207                 g_mutex_unlock(ui->sensors_mutex);
208
209                 ui_window_update(ui);
210         }
211         g_object_unref(G_OBJECT(builder));
212         gtk_widget_destroy(GTK_WIDGET(diag));
213 }