1 /* libsecret - GLib wrapper for Secret Service
3 * Copyright 2011 Collabora Ltd.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2.1 of the licence or (at
8 * your option) any later version.
10 * See the included COPYING file for more information.
12 * Author: Stef Walter <stefw@gnome.org>
17 #include "secret-private.h"
18 #include "secret-types.h"
23 * SECTION:secret-error
25 * @short_description: libsecret errors
27 * Various errors reported by the libsecret library. No error returned from
28 * the libsecret API is suitable for direct display to the user. It is up
29 * to the application to handle them appropriately.
36 * @SECRET_ERROR_PROTOCOL: received an invalid data or message from the Secret
38 * @SECRET_ERROR_IS_LOCKED: the item or collection is locked and the operation
40 * @SECRET_ERROR_NO_SUCH_OBJECT: no such item or collection found in the
42 * @SECRET_ERROR_ALREADY_EXISTS: a relevant item or collection already exists
44 * Errors returned by the Secret Service. None of the errors are appropriate
45 * for display to the user.
49 list_unref_free (GList *reflist)
52 for (l = reflist; l; l = g_list_next (l)) {
53 g_return_if_fail (G_IS_OBJECT (l->data));
54 g_object_unref (l->data);
56 g_list_free (reflist);
60 list_ref_copy (GList *reflist)
62 GList *l, *copy = g_list_copy (reflist);
63 for (l = copy; l; l = g_list_next (l)) {
64 g_return_val_if_fail (G_IS_OBJECT (l->data), NULL);
65 g_object_ref (l->data);
71 _secret_list_get_type (void)
73 static GType type = 0;
75 type = g_boxed_type_register_static ("SecretObjectList",
76 (GBoxedCopyFunc)list_ref_copy,
77 (GBoxedFreeFunc)list_unref_free);
83 secret_error_get_quark (void)
85 static volatile gsize quark = 0;
87 static const GDBusErrorEntry entries[] = {
88 { SECRET_ERROR_IS_LOCKED, "org.freedesktop.Secret.Error.IsLocked", },
89 { SECRET_ERROR_NO_SUCH_OBJECT, "org.freedesktop.Secret.Error.NoSuchObject", },
90 { SECRET_ERROR_ALREADY_EXISTS, "org.freedesktop.Secret.Error.AlreadyExists" },
93 g_dbus_error_register_error_domain ("secret-error", &quark,
94 entries, G_N_ELEMENTS (entries));
100 _secret_util_strip_remote_error (GError **error)
104 if (error == NULL || *error == NULL)
107 remote = g_dbus_error_get_remote_error (*error);
109 if (g_dbus_error_strip_remote_error (*error)) {
110 g_message ("Remote error from secret service: %s: %s",
111 remote, (*error)->message);
118 _secret_util_parent_path (const gchar *path)
122 g_return_val_if_fail (path != NULL, NULL);
124 pos = strrchr (path, '/');
125 g_return_val_if_fail (pos != NULL, NULL);
126 g_return_val_if_fail (pos != path, NULL);
128 return g_strndup (path, pos - path);
132 _secret_util_empty_path (const gchar *path)
134 g_return_val_if_fail (path != NULL, TRUE);
135 return (g_str_equal (path, "") || g_str_equal (path, "/"));
139 _secret_util_collection_to_path (const gchar *collection)
141 if (collection == NULL)
142 collection = SECRET_COLLECTION_DEFAULT;
143 if (strchr (collection, '/') == NULL)
144 return g_strdup_printf ("/org/freedesktop/secrets/aliases/%s", collection);
145 return g_strdup (collection);
149 _secret_util_variant_for_properties (GHashTable *properties)
152 GVariantBuilder builder;
156 g_return_val_if_fail (properties != NULL, NULL);
158 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
160 g_hash_table_iter_init (&iter, properties);
161 while (g_hash_table_iter_next (&iter, (gpointer *)&name, (gpointer *)&value))
162 g_variant_builder_add (&builder, "{sv}", name, value);
164 return g_variant_builder_end (&builder);
168 process_get_all_reply (GDBusProxy *proxy,
171 const gchar *invalidated_properties[1] = { NULL };
172 GVariant *changed_properties;
177 if (!g_variant_is_of_type (retval, G_VARIANT_TYPE ("(a{sv})"))) {
178 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
179 g_variant_get_type_string (retval));
183 g_variant_get (retval, "(a{sv})", &iter);
184 while (g_variant_iter_loop (iter, "{sv}", &key, &value))
185 g_dbus_proxy_set_cached_property (proxy, key, value);
186 g_variant_iter_free (iter);
188 g_variant_get (retval, "(@a{sv})", &changed_properties);
189 g_signal_emit_by_name (proxy, "g-properties-changed",
190 changed_properties, invalidated_properties);
191 g_variant_unref (changed_properties);
195 on_get_properties (GObject *source,
196 GAsyncResult *result,
199 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
200 GDBusProxy *proxy = G_DBUS_PROXY (g_async_result_get_source_object (user_data));
201 GError *error = NULL;
204 retval = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
207 process_get_all_reply (proxy, retval);
209 _secret_util_strip_remote_error (&error);
210 g_simple_async_result_take_error (res, error);
213 g_variant_unref (retval);
215 g_simple_async_result_complete (res);
216 g_object_unref (proxy);
217 g_object_unref (res);
221 _secret_util_get_properties (GDBusProxy *proxy,
223 GCancellable *cancellable,
224 GAsyncReadyCallback callback,
227 GSimpleAsyncResult *res;
229 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
231 res = g_simple_async_result_new (G_OBJECT (proxy), callback, user_data, result_tag);
233 g_dbus_connection_call (g_dbus_proxy_get_connection (proxy),
234 g_dbus_proxy_get_name (proxy),
235 g_dbus_proxy_get_object_path (proxy),
236 "org.freedesktop.DBus.Properties", "GetAll",
237 g_variant_new ("(s)", g_dbus_proxy_get_interface_name (proxy)),
238 G_VARIANT_TYPE ("(a{sv})"),
239 G_DBUS_CALL_FLAGS_NONE, -1,
240 cancellable, on_get_properties,
243 g_object_unref (res);
247 _secret_util_get_properties_finish (GDBusProxy *proxy,
249 GAsyncResult *result,
252 GSimpleAsyncResult *res;
254 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (proxy), result_tag), FALSE);
255 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
257 res = G_SIMPLE_ASYNC_RESULT (result);
259 if (g_simple_async_result_propagate_error (res, error))
272 set_closure_free (gpointer data)
274 SetClosure *closure = data;
275 g_free (closure->property);
276 g_variant_unref (closure->value);
277 g_slice_free (SetClosure, closure);
281 on_set_property (GObject *source,
282 GAsyncResult *result,
285 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
286 SetClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
287 GDBusProxy *proxy = G_DBUS_PROXY (g_async_result_get_source_object (user_data));
288 GError *error = NULL;
291 retval = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
294 _secret_util_strip_remote_error (&error);
295 g_simple_async_result_take_error (res, error);
298 g_variant_unref (retval);
300 closure->result = retval != NULL;
302 g_dbus_proxy_set_cached_property (proxy, closure->property, closure->value);
304 g_simple_async_result_complete (res);
305 g_object_unref (proxy);
306 g_object_unref (res);
310 _secret_util_set_property (GDBusProxy *proxy,
311 const gchar *property,
314 GCancellable *cancellable,
315 GAsyncReadyCallback callback,
318 GSimpleAsyncResult *res;
321 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
323 res = g_simple_async_result_new (G_OBJECT (proxy), callback, user_data, result_tag);
324 closure = g_slice_new0 (SetClosure);
325 closure->property = g_strdup (property);
326 closure->value = g_variant_ref_sink (value);
327 g_simple_async_result_set_op_res_gpointer (res, closure, set_closure_free);
329 g_dbus_connection_call (g_dbus_proxy_get_connection (proxy),
330 g_dbus_proxy_get_name (proxy),
331 g_dbus_proxy_get_object_path (proxy),
332 SECRET_PROPERTIES_INTERFACE,
334 g_variant_new ("(ssv)",
335 g_dbus_proxy_get_interface_name (proxy),
338 G_VARIANT_TYPE ("()"),
339 G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
340 cancellable, on_set_property,
343 g_object_unref (res);
347 _secret_util_set_property_finish (GDBusProxy *proxy,
349 GAsyncResult *result,
352 GSimpleAsyncResult *res;
355 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (proxy), result_tag), FALSE);
356 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
358 res = G_SIMPLE_ASYNC_RESULT (result);
360 if (g_simple_async_result_propagate_error (res, error))
363 closure = g_simple_async_result_get_op_res_gpointer (res);
364 return closure->result;
368 _secret_util_set_property_sync (GDBusProxy *proxy,
369 const gchar *property,
371 GCancellable *cancellable,
374 gboolean result = FALSE;
377 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
378 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
380 g_variant_ref_sink (value);
382 retval = g_dbus_connection_call_sync (g_dbus_proxy_get_connection (proxy),
383 g_dbus_proxy_get_name (proxy),
384 g_dbus_proxy_get_object_path (proxy),
385 SECRET_PROPERTIES_INTERFACE,
387 g_variant_new ("(ssv)",
388 g_dbus_proxy_get_interface_name (proxy),
391 G_VARIANT_TYPE ("()"),
392 G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
395 if (retval != NULL) {
397 g_variant_unref (retval);
398 g_dbus_proxy_set_cached_property (proxy, property, value);
401 g_variant_unref (value);
407 _secret_util_have_cached_properties (GDBusProxy *proxy)
411 names = g_dbus_proxy_get_cached_property_names (proxy);
414 return names != NULL;
418 _secret_sync_new (void)
422 sync = g_new0 (SecretSync, 1);
424 sync->context = g_main_context_new ();
425 sync->loop = g_main_loop_new (sync->context, FALSE);
431 _secret_sync_free (gpointer data)
433 SecretSync *sync = data;
435 g_clear_object (&sync->result);
436 g_main_loop_unref (sync->loop);
437 g_main_context_unref (sync->context);
441 _secret_sync_on_result (GObject *source,
442 GAsyncResult *result,
445 SecretSync *sync = user_data;
446 g_assert (sync->result == NULL);
447 sync->result = g_object_ref (result);
448 g_main_loop_quit (sync->loop);