~ctf/unity-settings-daemon/bug1389099_mic_volume_icons

« back to all changes in this revision

Viewing changes to gnome-settings-daemon/gnome-settings-manager.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-02-07 11:44:36 UTC
  • Revision ID: package-import@ubuntu.com-20140207114436-7t5u3yvwc4ul7w3e
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <stdlib.h>
 
24
#include <stdio.h>
 
25
#include <unistd.h>
 
26
#include <string.h>
 
27
 
 
28
#include <glib.h>
 
29
#include <glib/gi18n.h>
 
30
#include <glib-object.h>
 
31
#include <gio/gio.h>
 
32
 
 
33
#define GNOME_DESKTOP_USE_UNSTABLE_API
 
34
#include <libgnome-desktop/gnome-pnp-ids.h>
 
35
 
 
36
#include "gnome-settings-plugin.h"
 
37
#include "gnome-settings-plugin-info.h"
 
38
#include "gnome-settings-manager.h"
 
39
#include "gnome-settings-profile.h"
 
40
 
 
41
#define DEFAULT_SETTINGS_PREFIX "org.gnome.settings-daemon"
 
42
 
 
43
#define PLUGIN_EXT ".gnome-settings-plugin"
 
44
 
 
45
#define GNOME_SETTINGS_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNOME_TYPE_SETTINGS_MANAGER, GnomeSettingsManagerPrivate))
 
46
 
 
47
static const gchar introspection_xml[] =
 
48
"<node name='/org/gnome/SettingsDaemon'>"
 
49
"  <interface name='org.gnome.SettingsDaemon'>"
 
50
"    <annotation name='org.freedesktop.DBus.GLib.CSymbol' value='gnome_settings_manager'/>"
 
51
"    <signal name='PluginActivated'>"
 
52
"      <arg name='name' type='s'/>"
 
53
"    </signal>"
 
54
"    <signal name='PluginDeactivated'>"
 
55
"      <arg name='name' type='s'/>"
 
56
"    </signal>"
 
57
"  </interface>"
 
58
"</node>";
 
59
 
 
60
struct GnomeSettingsManagerPrivate
 
61
{
 
62
        guint                       owner_id;
 
63
        GDBusNodeInfo              *introspection_data;
 
64
        GDBusConnection            *connection;
 
65
        GSettings                  *settings;
 
66
        char                      **whitelist;
 
67
        GnomePnpIds                *pnp_ids;
 
68
        GSList                     *plugins;
 
69
};
 
70
 
 
71
static void     gnome_settings_manager_class_init  (GnomeSettingsManagerClass *klass);
 
72
static void     gnome_settings_manager_init        (GnomeSettingsManager      *settings_manager);
 
73
static void     gnome_settings_manager_finalize    (GObject                   *object);
 
74
 
 
75
G_DEFINE_TYPE (GnomeSettingsManager, gnome_settings_manager, G_TYPE_OBJECT)
 
76
 
 
77
static gpointer manager_object = NULL;
 
78
 
 
79
GQuark
 
80
gnome_settings_manager_error_quark (void)
 
81
{
 
82
        static GQuark ret = 0;
 
83
        if (ret == 0) {
 
84
                ret = g_quark_from_static_string ("gnome_settings_manager_error");
 
85
        }
 
86
 
 
87
        return ret;
 
88
}
 
89
 
 
90
static void
 
91
maybe_activate_plugin (GnomeSettingsPluginInfo *info, gpointer user_data)
 
92
{
 
93
        if (gnome_settings_plugin_info_get_enabled (info)) {
 
94
                gboolean res;
 
95
                res = gnome_settings_plugin_info_activate (info);
 
96
                if (res) {
 
97
                        g_debug ("Plugin %s: active", gnome_settings_plugin_info_get_location (info));
 
98
                } else {
 
99
                        g_debug ("Plugin %s: activation failed", gnome_settings_plugin_info_get_location (info));
 
100
                }
 
101
        } else {
 
102
                g_debug ("Plugin %s: inactive", gnome_settings_plugin_info_get_location (info));
 
103
        }
 
104
}
 
105
 
 
106
static gint
 
107
compare_location (GnomeSettingsPluginInfo *a,
 
108
                  GnomeSettingsPluginInfo *b)
 
109
{
 
110
        const char *loc_a;
 
111
        const char *loc_b;
 
112
 
 
113
        loc_a = gnome_settings_plugin_info_get_location (a);
 
114
        loc_b = gnome_settings_plugin_info_get_location (b);
 
115
 
 
116
        if (loc_a == NULL || loc_b == NULL) {
 
117
                return -1;
 
118
        }
 
119
 
 
120
        return strcmp (loc_a, loc_b);
 
121
}
 
122
 
 
123
static int
 
124
compare_priority (GnomeSettingsPluginInfo *a,
 
125
                  GnomeSettingsPluginInfo *b)
 
126
{
 
127
        int prio_a;
 
128
        int prio_b;
 
129
 
 
130
        prio_a = gnome_settings_plugin_info_get_priority (a);
 
131
        prio_b = gnome_settings_plugin_info_get_priority (b);
 
132
 
 
133
        return prio_a - prio_b;
 
134
}
 
135
 
 
136
static void
 
137
emit_signal (GnomeSettingsManager    *manager,
 
138
             const char              *signal,
 
139
             const char              *name)
 
140
{
 
141
        GError *error = NULL;
 
142
 
 
143
        /* FIXME: maybe we should queue those up until the D-Bus
 
144
         * connection is available... */
 
145
        if (manager->priv->connection == NULL)
 
146
                return;
 
147
 
 
148
        if (g_dbus_connection_emit_signal (manager->priv->connection,
 
149
                                           NULL,
 
150
                                           GSD_DBUS_PATH,
 
151
                                           GSD_DBUS_NAME,
 
152
                                           "PluginActivated",
 
153
                                           g_variant_new ("(s)", name),
 
154
                                           &error) == FALSE) {
 
155
                g_debug ("Error emitting signal: %s", error->message);
 
156
                g_error_free (error);
 
157
        }
 
158
 
 
159
}
 
160
 
 
161
static void
 
162
on_plugin_activated (GnomeSettingsPluginInfo *info,
 
163
                     GnomeSettingsManager    *manager)
 
164
{
 
165
        const char *name;
 
166
 
 
167
        name = gnome_settings_plugin_info_get_location (info);
 
168
        g_debug ("GnomeSettingsManager: emitting plugin-activated %s", name);
 
169
        emit_signal (manager, "PluginActivated", name);
 
170
}
 
171
 
 
172
static void
 
173
on_plugin_deactivated (GnomeSettingsPluginInfo *info,
 
174
                       GnomeSettingsManager    *manager)
 
175
{
 
176
        const char *name;
 
177
 
 
178
        name = gnome_settings_plugin_info_get_location (info);
 
179
        g_debug ("GnomeSettingsManager: emitting plugin-deactivated %s", name);
 
180
        emit_signal (manager, "PluginDeactivated", name);
 
181
}
 
182
 
 
183
static gboolean
 
184
contained (const char * const *items,
 
185
           const char         *item)
 
186
{
 
187
        while (*items) {
 
188
                if (g_strcmp0 (*items++, item) == 0) {
 
189
                        return TRUE;
 
190
                }
 
191
        }
 
192
 
 
193
        return FALSE;
 
194
}
 
195
 
 
196
static gboolean
 
197
is_schema (const char *schema)
 
198
{
 
199
        return contained (g_settings_list_schemas (), schema);
 
200
}
 
201
 
 
202
static gboolean
 
203
is_whitelisted (char       **whitelist,
 
204
                const char  *plugin_name)
 
205
{
 
206
        if (whitelist == NULL ||
 
207
            whitelist[0] == NULL ||
 
208
            g_strcmp0 (whitelist[0], "all") == 0)
 
209
                return TRUE;
 
210
 
 
211
        return contained ((const char * const *) whitelist, plugin_name);
 
212
}
 
213
 
 
214
static void
 
215
_load_file (GnomeSettingsManager *manager,
 
216
            const char           *filename)
 
217
{
 
218
        GnomeSettingsPluginInfo *info;
 
219
        char                    *key_name;
 
220
        GSList                  *l;
 
221
 
 
222
        g_debug ("Loading plugin: %s", filename);
 
223
        gnome_settings_profile_start ("%s", filename);
 
224
 
 
225
        info = gnome_settings_plugin_info_new_from_file (filename);
 
226
        if (info == NULL) {
 
227
                goto out;
 
228
        }
 
229
 
 
230
        l = g_slist_find_custom (manager->priv->plugins,
 
231
                                 info,
 
232
                                 (GCompareFunc) compare_location);
 
233
        if (l != NULL) {
 
234
                goto out;
 
235
        }
 
236
 
 
237
        if (!is_whitelisted (manager->priv->whitelist,
 
238
                             gnome_settings_plugin_info_get_location (info))) {
 
239
                g_debug ("Plugin %s ignored as it's not whitelisted",
 
240
                         gnome_settings_plugin_info_get_location (info));
 
241
                goto out;
 
242
        }
 
243
 
 
244
        key_name = g_strdup_printf ("%s.plugins.%s",
 
245
                                    DEFAULT_SETTINGS_PREFIX,
 
246
                                    gnome_settings_plugin_info_get_location (info));
 
247
 
 
248
        /* Ignore unknown schemas or else we'll assert */
 
249
        if (is_schema (key_name)) {
 
250
                manager->priv->plugins = g_slist_prepend (manager->priv->plugins,
 
251
                                                          g_object_ref (info));
 
252
 
 
253
                g_signal_connect (info, "activated",
 
254
                                  G_CALLBACK (on_plugin_activated), manager);
 
255
                g_signal_connect (info, "deactivated",
 
256
                                  G_CALLBACK (on_plugin_deactivated), manager);
 
257
 
 
258
                gnome_settings_plugin_info_set_settings_prefix (info, key_name);
 
259
        } else {
 
260
                g_warning ("Ignoring unknown module '%s'", key_name);
 
261
        }
 
262
 
 
263
        /* Priority is set in the call above */
 
264
        g_free (key_name);
 
265
 
 
266
 out:
 
267
        if (info != NULL) {
 
268
                g_object_unref (info);
 
269
        }
 
270
 
 
271
        gnome_settings_profile_end ("%s", filename);
 
272
}
 
273
 
 
274
static void
 
275
_load_dir (GnomeSettingsManager *manager,
 
276
           const char           *path)
 
277
{
 
278
        GError     *error;
 
279
        GDir       *d;
 
280
        const char *name;
 
281
 
 
282
        g_debug ("Loading settings plugins from dir: %s", path);
 
283
        gnome_settings_profile_start (NULL);
 
284
 
 
285
        error = NULL;
 
286
        d = g_dir_open (path, 0, &error);
 
287
        if (d == NULL) {
 
288
                g_warning ("%s", error->message);
 
289
                g_error_free (error);
 
290
                return;
 
291
        }
 
292
 
 
293
        while ((name = g_dir_read_name (d))) {
 
294
                char *filename;
 
295
 
 
296
                if (!g_str_has_suffix (name, PLUGIN_EXT)) {
 
297
                        continue;
 
298
                }
 
299
 
 
300
                filename = g_build_filename (path, name, NULL);
 
301
                if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
 
302
                        _load_file (manager, filename);
 
303
                }
 
304
                g_free (filename);
 
305
        }
 
306
 
 
307
        g_dir_close (d);
 
308
 
 
309
        gnome_settings_profile_end (NULL);
 
310
}
 
311
 
 
312
static void
 
313
_load_all (GnomeSettingsManager *manager)
 
314
{
 
315
        gnome_settings_profile_start (NULL);
 
316
 
 
317
        /* load system plugins */
 
318
        _load_dir (manager, GNOME_SETTINGS_PLUGINDIR G_DIR_SEPARATOR_S);
 
319
 
 
320
        manager->priv->plugins = g_slist_sort (manager->priv->plugins, (GCompareFunc) compare_priority);
 
321
        g_slist_foreach (manager->priv->plugins, (GFunc) maybe_activate_plugin, NULL);
 
322
        gnome_settings_profile_end (NULL);
 
323
}
 
324
 
 
325
static void
 
326
_unload_plugin (GnomeSettingsPluginInfo *info, gpointer user_data)
 
327
{
 
328
        if (gnome_settings_plugin_info_get_enabled (info)) {
 
329
                gnome_settings_plugin_info_deactivate (info);
 
330
        }
 
331
        g_object_unref (info);
 
332
}
 
333
 
 
334
static void
 
335
_unload_all (GnomeSettingsManager *manager)
 
336
{
 
337
         g_slist_foreach (manager->priv->plugins, (GFunc) _unload_plugin, NULL);
 
338
         g_slist_free (manager->priv->plugins);
 
339
         manager->priv->plugins = NULL;
 
340
}
 
341
 
 
342
static void
 
343
on_bus_gotten (GObject             *source_object,
 
344
               GAsyncResult        *res,
 
345
               GnomeSettingsManager *manager)
 
346
{
 
347
        GDBusConnection *connection;
 
348
        GError *error = NULL;
 
349
 
 
350
        connection = g_bus_get_finish (res, &error);
 
351
        if (connection == NULL) {
 
352
                g_warning ("Could not get session bus: %s", error->message);
 
353
                g_error_free (error);
 
354
                return;
 
355
        }
 
356
        manager->priv->connection = connection;
 
357
 
 
358
        g_dbus_connection_register_object (connection,
 
359
                                           GSD_DBUS_PATH,
 
360
                                           manager->priv->introspection_data->interfaces[0],
 
361
                                           NULL,
 
362
                                           NULL,
 
363
                                           NULL,
 
364
                                           NULL);
 
365
}
 
366
 
 
367
static void
 
368
register_manager (GnomeSettingsManager *manager)
 
369
{
 
370
        manager->priv->introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
 
371
        g_assert (manager->priv->introspection_data != NULL);
 
372
 
 
373
        g_bus_get (G_BUS_TYPE_SESSION,
 
374
                   NULL,
 
375
                   (GAsyncReadyCallback) on_bus_gotten,
 
376
                   manager);
 
377
}
 
378
 
 
379
gboolean
 
380
gnome_settings_manager_start (GnomeSettingsManager *manager,
 
381
                              GError              **error)
 
382
{
 
383
        gboolean ret;
 
384
 
 
385
        g_debug ("Starting settings manager");
 
386
 
 
387
        ret = FALSE;
 
388
 
 
389
        gnome_settings_profile_start (NULL);
 
390
 
 
391
        if (!g_module_supported ()) {
 
392
                g_warning ("gnome-settings-daemon is not able to initialize the plugins.");
 
393
                g_set_error (error,
 
394
                             GNOME_SETTINGS_MANAGER_ERROR,
 
395
                             GNOME_SETTINGS_MANAGER_ERROR_GENERAL,
 
396
                             "Plugins not supported");
 
397
 
 
398
                goto out;
 
399
        }
 
400
 
 
401
        g_debug ("loading PNPIDs");
 
402
        manager->priv->pnp_ids = gnome_pnp_ids_new ();
 
403
 
 
404
        gnome_settings_profile_start ("initializing plugins");
 
405
        manager->priv->settings = g_settings_new (DEFAULT_SETTINGS_PREFIX ".plugins");
 
406
        manager->priv->whitelist = g_settings_get_strv (manager->priv->settings, "whitelisted-plugins");
 
407
 
 
408
        _load_all (manager);
 
409
        gnome_settings_profile_end ("initializing plugins");
 
410
 
 
411
        ret = TRUE;
 
412
 out:
 
413
        gnome_settings_profile_end (NULL);
 
414
 
 
415
        return ret;
 
416
}
 
417
 
 
418
void
 
419
gnome_settings_manager_stop (GnomeSettingsManager *manager)
 
420
{
 
421
        g_debug ("Stopping settings manager");
 
422
 
 
423
        _unload_all (manager);
 
424
 
 
425
        if (manager->priv->owner_id > 0) {
 
426
                g_bus_unown_name (manager->priv->owner_id);
 
427
                manager->priv->owner_id = 0;
 
428
        }
 
429
 
 
430
        g_clear_pointer (&manager->priv->whitelist, g_strfreev);
 
431
        g_clear_object (&manager->priv->settings);
 
432
        g_clear_object (&manager->priv->pnp_ids);
 
433
}
 
434
 
 
435
static void
 
436
gnome_settings_manager_dispose (GObject *object)
 
437
{
 
438
        GnomeSettingsManager *manager;
 
439
 
 
440
        manager = GNOME_SETTINGS_MANAGER (object);
 
441
 
 
442
        gnome_settings_manager_stop (manager);
 
443
 
 
444
        G_OBJECT_CLASS (gnome_settings_manager_parent_class)->dispose (object);
 
445
}
 
446
 
 
447
static void
 
448
gnome_settings_manager_class_init (GnomeSettingsManagerClass *klass)
 
449
{
 
450
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
 
451
 
 
452
        object_class->dispose = gnome_settings_manager_dispose;
 
453
        object_class->finalize = gnome_settings_manager_finalize;
 
454
 
 
455
        g_type_class_add_private (klass, sizeof (GnomeSettingsManagerPrivate));
 
456
}
 
457
 
 
458
static void
 
459
gnome_settings_manager_init (GnomeSettingsManager *manager)
 
460
{
 
461
 
 
462
        manager->priv = GNOME_SETTINGS_MANAGER_GET_PRIVATE (manager);
 
463
}
 
464
 
 
465
static void
 
466
gnome_settings_manager_finalize (GObject *object)
 
467
{
 
468
        GnomeSettingsManager *manager;
 
469
 
 
470
        g_return_if_fail (object != NULL);
 
471
        g_return_if_fail (GNOME_IS_SETTINGS_MANAGER (object));
 
472
 
 
473
        manager = GNOME_SETTINGS_MANAGER (object);
 
474
 
 
475
        g_return_if_fail (manager->priv != NULL);
 
476
 
 
477
        G_OBJECT_CLASS (gnome_settings_manager_parent_class)->finalize (object);
 
478
}
 
479
 
 
480
GnomeSettingsManager *
 
481
gnome_settings_manager_new (void)
 
482
{
 
483
        if (manager_object != NULL) {
 
484
                g_object_ref (manager_object);
 
485
        } else {
 
486
                manager_object = g_object_new (GNOME_TYPE_SETTINGS_MANAGER,
 
487
                                               NULL);
 
488
                g_object_add_weak_pointer (manager_object,
 
489
                                           (gpointer *) &manager_object);
 
490
                register_manager (manager_object);
 
491
        }
 
492
 
 
493
        return GNOME_SETTINGS_MANAGER (manager_object);
 
494
}