dist the D-Bus interface description
[dconf.git] / dbus-1 / dconf-dbus-1.c
blob500700d09fcaa51af49d0d32d0fb2ac7e65dc393
1 /**
2 * Copyright © 2010 Canonical Limited
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the licence, or (at
7 * 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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
18 **/
20 #include "dconf-dbus-1.h"
22 #include "../engine/dconf-engine.h"
23 #include "dconf-libdbus-1.h"
25 #include <string.h>
27 struct _DConfDBusClient
29 DConfEngine *engine;
30 GSList *watches;
31 gint ref_count;
34 typedef struct
36 gchar *path;
37 DConfDBusNotify notify;
38 gpointer user_data;
39 } Watch;
41 void
42 dconf_engine_change_notify (DConfEngine *engine,
43 const gchar *prefix,
44 const gchar * const *changes,
45 const gchar *tag,
46 gpointer origin_tag,
47 gpointer user_data)
49 DConfDBusClient *dcdbc = user_data;
50 gchar **my_changes;
51 gint n_changes;
52 GSList *iter;
53 gint i;
55 n_changes = g_strv_length ((gchar **) changes);
56 my_changes = g_new (gchar *, n_changes + 1);
58 for (i = 0; i < n_changes; i++)
59 my_changes[i] = g_strconcat (prefix, changes[i], NULL);
60 my_changes[i] = NULL;
62 for (iter = dcdbc->watches; iter; iter = iter->next)
64 Watch *watch = iter->data;
66 for (i = 0; i < n_changes; i++)
67 if (g_str_has_prefix (my_changes[i], watch->path))
68 watch->notify (dcdbc, my_changes[i], watch->user_data);
71 g_strfreev (my_changes);
74 GVariant *
75 dconf_dbus_client_read (DConfDBusClient *dcdbc,
76 const gchar *key)
78 return dconf_engine_read (dcdbc->engine, NULL, key);
81 gboolean
82 dconf_dbus_client_write (DConfDBusClient *dcdbc,
83 const gchar *key,
84 GVariant *value)
86 DConfChangeset *changeset;
87 gboolean success;
89 changeset = dconf_changeset_new_write (key, value);
90 success = dconf_engine_change_fast (dcdbc->engine, changeset, NULL, NULL);
91 dconf_changeset_unref (changeset);
93 return success;
96 void
97 dconf_dbus_client_subscribe (DConfDBusClient *dcdbc,
98 const gchar *path,
99 DConfDBusNotify notify,
100 gpointer user_data)
102 Watch *watch;
104 watch = g_slice_new (Watch);
105 watch->path = g_strdup (path);
106 watch->notify = notify;
107 watch->user_data = user_data;
109 dcdbc->watches = g_slist_prepend (dcdbc->watches, watch);
111 dconf_engine_watch_fast (dcdbc->engine, path);
114 void
115 dconf_dbus_client_unsubscribe (DConfDBusClient *dcdbc,
116 DConfDBusNotify notify,
117 gpointer user_data)
119 GSList **ptr;
121 for (ptr = &dcdbc->watches; *ptr; ptr = &(*ptr)->next)
123 Watch *watch = (*ptr)->data;
125 if (watch->notify == notify && watch->user_data == user_data)
127 *ptr = g_slist_remove_link (*ptr, *ptr);
128 dconf_engine_unwatch_fast (dcdbc->engine, watch->path);
129 g_free (watch->path);
130 g_slice_free (Watch, watch);
131 return;
135 g_warning ("No matching watch found to unsubscribe");
138 gboolean
139 dconf_dbus_client_has_pending (DConfDBusClient *dcdbc)
141 return dconf_engine_has_outstanding (dcdbc->engine);
144 DConfDBusClient *
145 dconf_dbus_client_new (const gchar *profile,
146 DBusConnection *session,
147 DBusConnection *system)
149 DConfDBusClient *dcdbc;
151 if (session == NULL)
152 session = dbus_bus_get (DBUS_BUS_SESSION, NULL);
154 if (system == NULL)
155 system = dbus_bus_get (DBUS_BUS_SYSTEM, NULL);
157 dconf_libdbus_1_provide_bus (G_BUS_TYPE_SESSION, session);
158 dconf_libdbus_1_provide_bus (G_BUS_TYPE_SYSTEM, system);
160 dcdbc = g_slice_new (DConfDBusClient);
161 dcdbc->engine = dconf_engine_new (dcdbc, NULL);
162 dcdbc->watches = NULL;
163 dcdbc->ref_count = 1;
165 return dcdbc;
168 void
169 dconf_dbus_client_unref (DConfDBusClient *dcdbc)
171 if (--dcdbc->ref_count == 0)
173 g_return_if_fail (dcdbc->watches == NULL);
175 g_slice_free (DConfDBusClient, dcdbc);
179 DConfDBusClient *
180 dconf_dbus_client_ref (DConfDBusClient *dcdbc)
182 dcdbc->ref_count++;
184 return dcdbc;