summaryrefslogtreecommitdiff
path: root/src/gs-prefs.c
blob: f82b667fb5f09494e8ba5fbf550501b69a91ce71 (plain)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2004-2006 William Jon McCann <mccann@jhu.edu>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: William Jon McCann <mccann@jhu.edu>
 *
 */

#include "config.h"

#include <string.h>

#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>

#include "gs-prefs.h"

static void gs_prefs_class_init (GSPrefsClass *klass);
static void gs_prefs_init       (GSPrefs      *prefs);
static void gs_prefs_finalize   (GObject      *object);

#define LOCKDOWN_SETTINGS_SCHEMA "org.mate.lockdown"
#define KEY_LOCK_DISABLE "disable-lock-screen"
#define KEY_USER_SWITCH_DISABLE "disable-user-switching"

#define GSETTINGS_SCHEMA "org.mate.screensaver"
#define KEY_IDLE_ACTIVATION_ENABLED "idle-activation-enabled"
#define KEY_LOCK_ENABLED "lock-enabled"
#define KEY_MODE "mode"
#define KEY_ACTIVATE_DELAY "idle-delay"
#define KEY_POWER_DELAY "power-management-delay"
#define KEY_LOCK_DELAY "lock-delay"
#define KEY_CYCLE_DELAY "cycle-delay"
#define KEY_THEMES "themes"
#define KEY_USER_SWITCH_ENABLED "user-switch-enabled"
#define KEY_LOGOUT_ENABLED "logout-enabled"
#define KEY_LOGOUT_DELAY "logout-delay"
#define KEY_LOGOUT_COMMAND "logout-command"
#define KEY_KEYBOARD_ENABLED "embedded-keyboard-enabled"
#define KEY_KEYBOARD_COMMAND "embedded-keyboard-command"
#define KEY_STATUS_MESSAGE_ENABLED "status-message-enabled"

#define GS_PREFS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_PREFS, GSPrefsPrivate))

struct GSPrefsPrivate
{
	GSettings *settings;
	GSettings *lockdown_settings;
};

enum
{
    CHANGED,
    LAST_SIGNAL
};

enum
{
    PROP_0
};

static guint         signals [LAST_SIGNAL] = { 0, };

G_DEFINE_TYPE (GSPrefs, gs_prefs, G_TYPE_OBJECT)

static void
gs_prefs_set_property (GObject            *object,
                       guint               prop_id,
                       const GValue       *value,
                       GParamSpec         *pspec)
{
	switch (prop_id)
	{
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gs_prefs_get_property (GObject            *object,
                       guint               prop_id,
                       GValue             *value,
                       GParamSpec         *pspec)
{
	switch (prop_id)
	{
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gs_prefs_class_init (GSPrefsClass *klass)
{
	GObjectClass   *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize     = gs_prefs_finalize;
	object_class->get_property = gs_prefs_get_property;
	object_class->set_property = gs_prefs_set_property;


	signals [CHANGED] =
	    g_signal_new ("changed",
	                  G_TYPE_FROM_CLASS (object_class),
	                  G_SIGNAL_RUN_LAST,
	                  G_STRUCT_OFFSET (GSPrefsClass, changed),
	                  NULL,
	                  NULL,
	                  g_cclosure_marshal_VOID__VOID,
	                  G_TYPE_NONE,
	                  0);

	g_type_class_add_private (klass, sizeof (GSPrefsPrivate));

}

static void
_gs_prefs_set_timeout (GSPrefs *prefs,
                       int      value)
{
	if (value < 1)
		value = 10;

	/* pick a reasonable large number for the
	   upper bound */
	if (value > 480)
		value = 480;

	prefs->timeout = value * 60000;
}

static void
_gs_prefs_set_power_timeout (GSPrefs *prefs,
                             int      value)
{
	if (value < 1)
		value = 60;

	/* pick a reasonable large number for the
	   upper bound */
	if (value > 480)
		value = 480;

	/* this value is in seconds - others are in minutes */
	prefs->power_timeout = value * 1000;
}

static void
_gs_prefs_set_lock_timeout (GSPrefs *prefs,
                            int      value)
{
	if (value < 0)
		value = 0;

	/* pick a reasonable large number for the
	   upper bound */
	if (value > 480)
		value = 480;

	prefs->lock_timeout = value * 60000;
}

static void
_gs_prefs_set_cycle_timeout (GSPrefs *prefs,
                             int      value)
{
	if (value < 1)
		value = 1;

	/* pick a reasonable large number for the
	   upper bound */
	if (value > 480)
		value = 480;

	prefs->cycle = value * 60000;
}

static void
_gs_prefs_set_mode (GSPrefs    *prefs,
                    gint         mode)
{
	prefs->mode = mode;
}

static void
_gs_prefs_set_themes (GSPrefs *prefs,
                      gchar **values)
{
	guint i;
	if (prefs->themes)
	{
		g_slist_foreach (prefs->themes, (GFunc)g_free, NULL);
		g_slist_free (prefs->themes);
	}

	/* take ownership of the list */
	prefs->themes = NULL;
        for (i=0; values[i] != NULL; i++)
		prefs->themes = g_slist_append (prefs->themes, g_strdup (values[i]));
}

static void
_gs_prefs_set_idle_activation_enabled (GSPrefs *prefs,
                                       gboolean value)
{
	prefs->idle_activation_enabled = value;
}

static void
_gs_prefs_set_lock_enabled (GSPrefs *prefs,
                            gboolean value)
{
	prefs->lock_enabled = value;
}

static void
_gs_prefs_set_lock_disabled (GSPrefs *prefs,
                             gboolean value)
{
	prefs->lock_disabled = value;
}

static void
_gs_prefs_set_user_switch_disabled (GSPrefs *prefs,
                                    gboolean value)
{
	prefs->user_switch_disabled = value;
}

static void
_gs_prefs_set_keyboard_enabled (GSPrefs *prefs,
                                gboolean value)
{
	prefs->keyboard_enabled = value;
}

static void
_gs_prefs_set_keyboard_command (GSPrefs    *prefs,
                                const char *value)
{
	g_free (prefs->keyboard_command);
	prefs->keyboard_command = NULL;

	if (value)
	{
		/* FIXME: check command */

		prefs->keyboard_command = g_strdup (value);
	}
}

static void
_gs_prefs_set_status_message_enabled (GSPrefs  *prefs,
                                      gboolean  enabled)
{
	prefs->status_message_enabled = enabled;
}

static void
_gs_prefs_set_logout_enabled (GSPrefs *prefs,
                              gboolean value)
{
	prefs->logout_enabled = value;
}

static void
_gs_prefs_set_logout_command (GSPrefs    *prefs,
                              const char *value)
{
	g_free (prefs->logout_command);
	prefs->logout_command = NULL;

	if (value)
	{
		/* FIXME: check command */

		prefs->logout_command = g_strdup (value);
	}
}

static void
_gs_prefs_set_logout_timeout (GSPrefs *prefs,
                              int      value)
{
	if (value < 0)
		value = 0;

	/* pick a reasonable large number for the
	   upper bound */
	if (value > 480)
		value = 480;

	prefs->logout_timeout = value * 60000;
}

static void
_gs_prefs_set_user_switch_enabled (GSPrefs *prefs,
                                   gboolean value)
{
	prefs->user_switch_enabled = value;
}

static void
gs_prefs_load_from_settings (GSPrefs *prefs)
{
	glong    value;
	gboolean bvalue;
	char    *string;
	gchar    **strv;
	gint       mode;

	bvalue = g_settings_get_boolean (prefs->priv->settings, KEY_IDLE_ACTIVATION_ENABLED);
	_gs_prefs_set_idle_activation_enabled (prefs, bvalue);

	bvalue = g_settings_get_boolean (prefs->priv->settings, KEY_LOCK_ENABLED);
	_gs_prefs_set_lock_enabled (prefs, bvalue);

	bvalue = g_settings_get_boolean (prefs->priv->lockdown_settings, KEY_LOCK_DISABLE);
	_gs_prefs_set_lock_disabled (prefs, bvalue);

	bvalue = g_settings_get_boolean (prefs->priv->lockdown_settings, KEY_USER_SWITCH_DISABLE);
	_gs_prefs_set_user_switch_disabled (prefs, bvalue);

	value = g_settings_get_int (prefs->priv->settings, KEY_ACTIVATE_DELAY);
	_gs_prefs_set_timeout (prefs, value);

	value = g_settings_get_int (prefs->priv->settings, KEY_POWER_DELAY);
	_gs_prefs_set_power_timeout (prefs, value);

	value = g_settings_get_int (prefs->priv->settings, KEY_LOCK_DELAY);
	_gs_prefs_set_lock_timeout (prefs, value);

	value = g_settings_get_int (prefs->priv->settings, KEY_CYCLE_DELAY);
	_gs_prefs_set_cycle_timeout (prefs, value);

	mode = g_settings_get_enum (prefs->priv->settings, KEY_MODE);
	_gs_prefs_set_mode (prefs, mode);

	strv = g_settings_get_strv (prefs->priv->settings, KEY_THEMES);
	_gs_prefs_set_themes (prefs, strv);
	g_strfreev (strv);

	/* Embedded keyboard options */

	bvalue = g_settings_get_boolean (prefs->priv->settings, KEY_KEYBOARD_ENABLED);
	_gs_prefs_set_keyboard_enabled (prefs, bvalue);

	string = g_settings_get_string (prefs->priv->settings, KEY_KEYBOARD_COMMAND);
	_gs_prefs_set_keyboard_command (prefs, string);
	g_free (string);

	bvalue = g_settings_get_boolean (prefs->priv->settings, KEY_STATUS_MESSAGE_ENABLED);
	_gs_prefs_set_status_message_enabled (prefs, bvalue);

	/* Logout options */

	bvalue = g_settings_get_boolean (prefs->priv->settings, KEY_LOGOUT_ENABLED);
	_gs_prefs_set_logout_enabled (prefs, bvalue);

	string = g_settings_get_string (prefs->priv->settings, KEY_LOGOUT_COMMAND);
	_gs_prefs_set_logout_command (prefs, string);
	g_free (string);

	value = g_settings_get_int (prefs->priv->settings, KEY_LOGOUT_DELAY);
	_gs_prefs_set_logout_timeout (prefs, value);

	/* User switching options */

	bvalue = g_settings_get_boolean (prefs->priv->settings, KEY_USER_SWITCH_ENABLED);
	_gs_prefs_set_user_switch_enabled (prefs, bvalue);
}

static void
key_changed_cb (GSettings *settings,
		gchar *key,
		GSPrefs *prefs)
{
	if (strcmp (key, KEY_MODE) == 0)
	{
		gint mode;

		mode = g_settings_get_enum (settings, key);
		_gs_prefs_set_mode (prefs, mode);

	}
	else if (strcmp (key, KEY_THEMES) == 0)
	{
		gchar **strv = NULL;

		strv = g_settings_get_strv (settings, key);
		_gs_prefs_set_themes (prefs, strv);
		g_strfreev (strv);

	}
	else if (strcmp (key, KEY_ACTIVATE_DELAY) == 0)
	{
		int delay;

		delay = g_settings_get_int (settings, key);
		_gs_prefs_set_timeout (prefs, delay);

	}
	else if (strcmp (key, KEY_POWER_DELAY) == 0)
	{
		int delay;

		delay = g_settings_get_int (settings, key);
		_gs_prefs_set_power_timeout (prefs, delay);

	}
	else if (strcmp (key, KEY_LOCK_DELAY) == 0)
	{
		int delay;

		delay = g_settings_get_int (settings, key);
		_gs_prefs_set_lock_timeout (prefs, delay);
	}
	else if (strcmp (key, KEY_IDLE_ACTIVATION_ENABLED) == 0)
	{
		gboolean enabled;

		enabled = g_settings_get_boolean (settings, key);
		_gs_prefs_set_idle_activation_enabled (prefs, enabled);

	}
	else if (strcmp (key, KEY_LOCK_ENABLED) == 0)
	{
		gboolean enabled;

		enabled = g_settings_get_boolean (settings, key);
		_gs_prefs_set_lock_enabled (prefs, enabled);

	}
	else if (strcmp (key, KEY_LOCK_DISABLE) == 0)
	{
		gboolean disabled;

		disabled = g_settings_get_boolean (settings, key);
		_gs_prefs_set_lock_disabled (prefs, disabled);

	}
	else if (strcmp (key, KEY_USER_SWITCH_DISABLE) == 0)
	{
		gboolean disabled;

		disabled = g_settings_get_boolean (settings, key);
		_gs_prefs_set_user_switch_disabled (prefs, disabled);

	}
	else if (strcmp (key, KEY_CYCLE_DELAY) == 0)
	{
		int delay;

		delay = g_settings_get_int (settings, key);
		_gs_prefs_set_cycle_timeout (prefs, delay);

	}
	else if (strcmp (key, KEY_KEYBOARD_ENABLED) == 0)
	{
		gboolean enabled;

		enabled = g_settings_get_boolean (settings, key);
		_gs_prefs_set_keyboard_enabled (prefs, enabled);

	}
	else if (strcmp (key, KEY_KEYBOARD_COMMAND) == 0)
	{
		char *command;

		command = g_settings_get_string (settings, key);
		_gs_prefs_set_keyboard_command (prefs, command);
		g_free (command);

	}
	else if (strcmp (key, KEY_STATUS_MESSAGE_ENABLED) == 0)
	{
		gboolean enabled;

		enabled = g_settings_get_boolean (settings, key);
		_gs_prefs_set_status_message_enabled (prefs, enabled);

	}
	else if (strcmp (key, KEY_LOGOUT_ENABLED) == 0)
	{
		gboolean enabled;

		enabled = g_settings_get_boolean (settings, key);
		_gs_prefs_set_logout_enabled (prefs, enabled);

	}
	else if (strcmp (key, KEY_LOGOUT_DELAY) == 0)
	{
		int delay;

		delay = g_settings_get_int (settings, key);
		_gs_prefs_set_logout_timeout (prefs, delay);

	}
	else if (strcmp (key, KEY_LOGOUT_COMMAND) == 0)
	{
		char *command;
		command = g_settings_get_string (settings, key);
		_gs_prefs_set_logout_command (prefs, command);
		g_free (command);
	}
	else if (strcmp (key, KEY_USER_SWITCH_ENABLED) == 0)
	{
		gboolean enabled;

		enabled = g_settings_get_boolean (settings, key);
		_gs_prefs_set_user_switch_enabled (prefs, enabled);

	}
	else
	{
		g_warning ("Config key not handled: %s", key);
	}

	g_signal_emit (prefs, signals [CHANGED], 0);
}

static void
gs_prefs_init (GSPrefs *prefs)
{
	prefs->priv = GS_PREFS_GET_PRIVATE (prefs);

	prefs->priv->settings     = g_settings_new (GSETTINGS_SCHEMA);
	g_signal_connect (prefs->priv->settings,
			  "changed",
			  G_CALLBACK (key_changed_cb),
			  prefs);
	prefs->priv->lockdown_settings = g_settings_new (LOCKDOWN_SETTINGS_SCHEMA);
	g_signal_connect (prefs->priv->lockdown_settings,
			  "changed",
			  G_CALLBACK (key_changed_cb),
			  prefs);
	prefs->idle_activation_enabled = TRUE;
	prefs->lock_enabled            = TRUE;
	prefs->lock_disabled           = FALSE;
	prefs->logout_enabled          = FALSE;
	prefs->user_switch_enabled     = FALSE;

	prefs->timeout                 = 600000;
	prefs->power_timeout           = 60000;
	prefs->lock_timeout            = 0;
	prefs->logout_timeout          = 14400000;
	prefs->cycle                   = 600000;

	prefs->mode                    = GS_MODE_SINGLE;

	gs_prefs_load_from_settings (prefs);
}

static void
gs_prefs_finalize (GObject *object)
{
	GSPrefs *prefs;

	g_return_if_fail (object != NULL);
	g_return_if_fail (GS_IS_PREFS (object));

	prefs = GS_PREFS (object);

	g_return_if_fail (prefs->priv != NULL);

	if (prefs->priv->settings)
	{
		g_object_unref (prefs->priv->settings);
		prefs->priv->settings = NULL;
	}

	if (prefs->priv->lockdown_settings) {
		g_object_unref (prefs->priv->lockdown_settings);
		prefs->priv->lockdown_settings = NULL;
	}

	if (prefs->themes)
	{
		g_slist_foreach (prefs->themes, (GFunc)g_free, NULL);
		g_slist_free (prefs->themes);
	}

	g_free (prefs->logout_command);
	g_free (prefs->keyboard_command);

	G_OBJECT_CLASS (gs_prefs_parent_class)->finalize (object);
}

GSPrefs *
gs_prefs_new (void)
{
	GObject *prefs;

	prefs = g_object_new (GS_TYPE_PREFS, NULL);

	return GS_PREFS (prefs);
}