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>
20 #include "dconf-dbus-1.h"
22 #include "../engine/dconf-engine.h"
23 #include "dconf-libdbus-1.h"
27 struct _DConfDBusClient
37 DConfDBusNotify notify
;
42 dconf_engine_change_notify (DConfEngine
*engine
,
44 const gchar
* const *changes
,
49 DConfDBusClient
*dcdbc
= user_data
;
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
);
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
);
75 dconf_dbus_client_read (DConfDBusClient
*dcdbc
,
78 return dconf_engine_read (dcdbc
->engine
, NULL
, key
);
82 dconf_dbus_client_write (DConfDBusClient
*dcdbc
,
86 DConfChangeset
*changeset
;
89 changeset
= dconf_changeset_new_write (key
, value
);
90 success
= dconf_engine_change_fast (dcdbc
->engine
, changeset
, NULL
, NULL
);
91 dconf_changeset_unref (changeset
);
97 dconf_dbus_client_subscribe (DConfDBusClient
*dcdbc
,
99 DConfDBusNotify notify
,
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
);
115 dconf_dbus_client_unsubscribe (DConfDBusClient
*dcdbc
,
116 DConfDBusNotify notify
,
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
);
135 g_warning ("No matching watch found to unsubscribe");
139 dconf_dbus_client_has_pending (DConfDBusClient
*dcdbc
)
141 return dconf_engine_has_outstanding (dcdbc
->engine
);
145 dconf_dbus_client_new (const gchar
*profile
,
146 DBusConnection
*session
,
147 DBusConnection
*system
)
149 DConfDBusClient
*dcdbc
;
152 session
= dbus_bus_get (DBUS_BUS_SESSION
, 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;
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
);
180 dconf_dbus_client_ref (DConfDBusClient
*dcdbc
)