1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
/* battstat A MATE battery meter for laptops.
* Copyright (C) 2000 by Jörgen Pehrson <jp@spektr.eu.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
$Id$
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#ifdef HAVE_ERR_H
#include <err.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include <gio/gio.h>
#include "battstat-preferences.h"
enum {
PROP_0,
PROP_PROGRESS_DATA
};
struct _BattstatPreferences
{
GtkDialog parent;
GtkWidget *radio_text_1;
GtkWidget *radio_text_2;
GtkWidget *check_text;
GtkWidget *check_text_ptr;
GtkWidget *lowbatt_toggle;
GtkWidget *full_toggle;
GtkWidget *hbox_ptr;
GtkWidget *combo_ptr;
GtkWidget *spin_ptr;
ProgressData *battstat;
};
G_DEFINE_TYPE (BattstatPreferences, battstat_preferences, GTK_TYPE_DIALOG);
static void
combo_ptr_cb (GtkWidget *combo_ptr,
gpointer data)
{
BattstatPreferences *dialog = data;
if (gtk_combo_box_get_active (GTK_COMBO_BOX (combo_ptr)))
dialog->battstat->red_value_is_time = TRUE;
else
dialog->battstat->red_value_is_time = FALSE;
g_settings_set_boolean (dialog->battstat->settings,
"red-value-is-time",
dialog->battstat->red_value_is_time);
}
static void
spin_ptr_cb (GtkWidget *spin_ptr,
gpointer data)
{
BattstatPreferences *dialog = data;
gdouble red_val = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_ptr));
dialog->battstat->red_val = (guint) red_val;
/* automatically calculate orangle and yellow values from the
* red value
*/
dialog->battstat->orange_val = (guint) (ORANGE_MULTIPLIER * red_val);
dialog->battstat->orange_val = MIN (dialog->battstat->orange_val, 100);
dialog->battstat->yellow_val = (guint) (YELLOW_MULTIPLIER * red_val);
dialog->battstat->yellow_val = MIN (dialog->battstat->yellow_val, 100);
g_settings_set_int (dialog->battstat->settings,
"red-value",
dialog->battstat->red_val);
}
static void
show_text_toggled (GtkToggleButton *button,
gpointer data)
{
BattstatPreferences *dialog = data;
gboolean show_text_active;
show_text_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->check_text));
if (show_text_active) {
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->radio_text_1)))
dialog->battstat->showtext = APPLET_SHOW_TIME;
else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->radio_text_2)))
dialog->battstat->showtext = APPLET_SHOW_PERCENT;
else
dialog->battstat->showtext = APPLET_SHOW_NONE;
} else {
dialog->battstat->showtext = APPLET_SHOW_NONE;
}
dialog->battstat->refresh_label = TRUE;
reconfigure_layout (dialog->battstat);
g_settings_set_int (dialog->battstat->settings,
"show-text",
dialog->battstat->showtext);
}
static void
lowbatt_toggled (GtkToggleButton *button,
gpointer data)
{
BattstatPreferences *dialog = data;
dialog->battstat->lowbattnotification = gtk_toggle_button_get_active (button);
}
static void
full_toggled (GtkToggleButton *button,
gpointer data)
{
BattstatPreferences *dialog = data;
dialog->battstat->fullbattnot = gtk_toggle_button_get_active (button);
}
static void
response_cb (GtkDialog *dialog,
gint id,
gpointer data)
{
BattstatPreferences *self = data;
if (id == GTK_RESPONSE_HELP)
battstat_show_help (self->battstat, "battstat-appearance");
else
gtk_widget_hide (GTK_WIDGET (dialog));
}
static void
battstat_preferences_update (BattstatPreferences *dialog)
{
gboolean check_text_active = TRUE;
if (!g_settings_is_writable (dialog->battstat->settings, "low-battery-notification")) {
gtk_widget_set_sensitive (dialog->lowbatt_toggle, FALSE);
}
gtk_widget_set_sensitive (dialog->hbox_ptr, dialog->battstat->lowbattnotification);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (dialog->spin_ptr), dialog->battstat->red_val);
if (dialog->battstat->red_value_is_time)
gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combo_ptr), 1);
else
gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combo_ptr), 0);
if (!g_settings_is_writable (dialog->battstat->settings, "full-battery-notification"))
gtk_widget_set_sensitive (dialog->full_toggle, FALSE);
if (dialog->battstat->fullbattnot)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->full_toggle), TRUE);
if (dialog->battstat->lowbattnotification)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->lowbatt_toggle), TRUE);
if (!g_settings_is_writable (dialog->battstat->settings, "show-text"))
{
gtk_widget_set_sensitive (dialog->check_text, FALSE);
check_text_active = FALSE;
}
switch (dialog->battstat->showtext) {
case APPLET_SHOW_PERCENT:
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->check_text), TRUE);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radio_text_2), TRUE);
break;
case APPLET_SHOW_TIME:
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->check_text), TRUE);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radio_text_1), TRUE);
break;
case APPLET_SHOW_NONE:
default:
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->check_text), FALSE);
check_text_active = FALSE;
}
gtk_widget_set_sensitive (dialog->check_text_ptr, check_text_active);
}
static void
battstat_preferences_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
BattstatPreferences *dialog;
dialog = BATTSTAT_PREFERENCES (object);
switch (prop_id) {
case PROP_PROGRESS_DATA:
dialog->battstat = g_value_get_pointer (value);
break;
default:
/* We don't have any other property... */
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
battstat_preferences_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
BattstatPreferences *dialog;
dialog = BATTSTAT_PREFERENCES (object);
switch (prop_id) {
case PROP_PROGRESS_DATA:
g_value_set_pointer (value, dialog->battstat);
break;
default:
/* We don't have any other property... */
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
battstat_preferences_init (BattstatPreferences *dialog)
{
gtk_widget_init_template (GTK_WIDGET (dialog));
g_object_bind_property (dialog->check_text, "active",
dialog->check_text_ptr, "sensitive",
G_BINDING_DEFAULT);
g_object_bind_property (dialog->lowbatt_toggle, "active",
dialog->hbox_ptr, "sensitive",
G_BINDING_DEFAULT);
}
static GObject*
battstat_preferences_constructor (GType type,
guint n_construct_params,
GObjectConstructParam *construct_params)
{
GObject *object;
BattstatPreferences *self;
object = G_OBJECT_CLASS (battstat_preferences_parent_class)->
constructor (type, n_construct_params, construct_params);
self = BATTSTAT_PREFERENCES (object);
battstat_preferences_update (self);
g_settings_bind (self->battstat->settings, "low-battery-notification",
self->lowbatt_toggle, "active",
G_SETTINGS_BIND_DEFAULT);
g_settings_bind (self->battstat->settings, "full-battery-notification",
self->full_toggle, "active",
G_SETTINGS_BIND_DEFAULT);
return object;
}
GtkWidget*
battstat_preferences_new (ProgressData *battstat)
{
return g_object_new (BATTSTAT_TYPE_PREFERENCES,
"progress-data", battstat,
NULL);
}
static void
battstat_preferences_class_init (BattstatPreferencesClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->set_property = battstat_preferences_set_property;
object_class->get_property = battstat_preferences_get_property;
object_class->constructor = battstat_preferences_constructor;
g_object_class_install_property (object_class,
PROP_PROGRESS_DATA,
g_param_spec_pointer ("progress-data",
"Progress Data",
"The Progress Data",
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
gtk_widget_class_set_template_from_resource (widget_class,
BATTSTAT_RESOURCE_PATH "battstat-preferences.ui");
gtk_widget_class_bind_template_child (widget_class, BattstatPreferences, check_text);
gtk_widget_class_bind_template_child (widget_class, BattstatPreferences, check_text_ptr);
gtk_widget_class_bind_template_child (widget_class, BattstatPreferences, radio_text_1);
gtk_widget_class_bind_template_child (widget_class, BattstatPreferences, radio_text_2);
gtk_widget_class_bind_template_child (widget_class, BattstatPreferences, lowbatt_toggle);
gtk_widget_class_bind_template_child (widget_class, BattstatPreferences, full_toggle);
gtk_widget_class_bind_template_child (widget_class, BattstatPreferences, hbox_ptr);
gtk_widget_class_bind_template_child (widget_class, BattstatPreferences, combo_ptr);
gtk_widget_class_bind_template_child (widget_class, BattstatPreferences, spin_ptr);
gtk_widget_class_bind_template_callback (widget_class, lowbatt_toggled);
gtk_widget_class_bind_template_callback (widget_class, combo_ptr_cb);
gtk_widget_class_bind_template_callback (widget_class, spin_ptr_cb);
gtk_widget_class_bind_template_callback (widget_class, full_toggled);
gtk_widget_class_bind_template_callback (widget_class, show_text_toggled);
gtk_widget_class_bind_template_callback (widget_class, response_cb);
}
|