Pass service GType to secret_service_new() and friends.

* This allows callers to use a custom SecretService derived
   type with the rest of libsecret
 * The default way to get a SecretService is via secret_service_get()
   which remains simple without the GType parameter
This commit is contained in:
Stef Walter 2012-06-27 11:45:09 +02:00
parent 268f2dfd62
commit fda131178a
3 changed files with 42 additions and 17 deletions

View File

@ -834,6 +834,7 @@ secret_service_get_sync (SecretServiceFlags flags,
/** /**
* secret_service_new: * secret_service_new:
* @service_gtype: the GType of the new secret service
* @service_bus_name: (allow-none): the D-Bus service name of the secret service * @service_bus_name: (allow-none): the D-Bus service name of the secret service
* @flags: flags for which service functionality to ensure is initialized * @flags: flags for which service functionality to ensure is initialized
* @cancellable: optional cancellation object * @cancellable: optional cancellation object
@ -844,6 +845,9 @@ secret_service_get_sync (SecretServiceFlags flags,
* *
* This function is rarely used, see secret_service_get() instead. * This function is rarely used, see secret_service_get() instead.
* *
* The @service_gtype argument should be set to %SECRET_TYPE_SERVICE or a the type
* of a derived class.
*
* If @flags contains any flags of which parts of the secret service to * If @flags contains any flags of which parts of the secret service to
* ensure are initialized, then those will be initialized before returning. * ensure are initialized, then those will be initialized before returning.
* *
@ -852,18 +856,20 @@ secret_service_get_sync (SecretServiceFlags flags,
* This method will return immediately and complete asynchronously. * This method will return immediately and complete asynchronously.
*/ */
void void
secret_service_new (const gchar *service_bus_name, secret_service_new (GType service_gtype,
const gchar *service_bus_name,
SecretServiceFlags flags, SecretServiceFlags flags,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable)); g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
g_return_if_fail (g_type_is_a (service_gtype, SECRET_TYPE_SERVICE));
if (service_bus_name == NULL) if (service_bus_name == NULL)
service_bus_name = default_bus_name; service_bus_name = default_bus_name;
g_async_initable_new_async (SECRET_TYPE_SERVICE, G_PRIORITY_DEFAULT, g_async_initable_new_async (service_gtype, G_PRIORITY_DEFAULT,
cancellable, callback, user_data, cancellable, callback, user_data,
"g-flags", G_DBUS_PROXY_FLAGS_NONE, "g-flags", G_DBUS_PROXY_FLAGS_NONE,
"g-interface-info", _secret_gen_service_interface_info (), "g-interface-info", _secret_gen_service_interface_info (),
@ -909,6 +915,7 @@ secret_service_new_finish (GAsyncResult *result,
/** /**
* secret_service_new_sync: * secret_service_new_sync:
* @service_gtype: the GType of the new secret service
* @service_bus_name: (allow-none): the D-Bus service name of the secret service * @service_bus_name: (allow-none): the D-Bus service name of the secret service
* @flags: flags for which service functionality to ensure is initialized * @flags: flags for which service functionality to ensure is initialized
* @cancellable: optional cancellation object * @cancellable: optional cancellation object
@ -918,6 +925,9 @@ secret_service_new_finish (GAsyncResult *result,
* *
* This function is rarely used, see secret_service_get_sync() instead. * This function is rarely used, see secret_service_get_sync() instead.
* *
* The @service_gtype argument should be set to %SECRET_TYPE_SERVICE or a the
* type of a derived class.
*
* If @flags contains any flags of which parts of the secret service to * If @flags contains any flags of which parts of the secret service to
* ensure are initialized, then those will be initialized before returning. * ensure are initialized, then those will be initialized before returning.
* *
@ -930,17 +940,19 @@ secret_service_new_finish (GAsyncResult *result,
* should be released with g_object_unref(). * should be released with g_object_unref().
*/ */
SecretService * SecretService *
secret_service_new_sync (const gchar *service_bus_name, secret_service_new_sync (GType service_gtype,
const gchar *service_bus_name,
SecretServiceFlags flags, SecretServiceFlags flags,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL); g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
g_return_val_if_fail (g_type_is_a (service_gtype, SECRET_TYPE_SERVICE), NULL);
if (service_bus_name == NULL) if (service_bus_name == NULL)
service_bus_name = default_bus_name; service_bus_name = default_bus_name;
return g_initable_new (SECRET_TYPE_SERVICE, cancellable, error, return g_initable_new (service_gtype, cancellable, error,
"g-flags", G_DBUS_PROXY_FLAGS_NONE, "g-flags", G_DBUS_PROXY_FLAGS_NONE,
"g-interface-info", _secret_gen_service_interface_info (), "g-interface-info", _secret_gen_service_interface_info (),
"g-name", service_bus_name, "g-name", service_bus_name,

View File

@ -93,7 +93,8 @@ SecretService * secret_service_get_sync (SecretService
GCancellable *cancellable, GCancellable *cancellable,
GError **error); GError **error);
void secret_service_new (const gchar *service_bus_name, void secret_service_new (GType service_gtype,
const gchar *service_bus_name,
SecretServiceFlags flags, SecretServiceFlags flags,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
@ -102,7 +103,8 @@ void secret_service_new (const gchar *
SecretService * secret_service_new_finish (GAsyncResult *result, SecretService * secret_service_new_finish (GAsyncResult *result,
GError **error); GError **error);
SecretService * secret_service_new_sync (const gchar *service_bus_name, SecretService * secret_service_new_sync (GType service_gtype,
const gchar *service_bus_name,
SecretServiceFlags flags, SecretServiceFlags flags,
GCancellable *cancellable, GCancellable *cancellable,
GError **error); GError **error);

View File

@ -245,10 +245,12 @@ test_new_sync (void)
/* Both these sohuld point to different things */ /* Both these sohuld point to different things */
service1 = secret_service_new_sync (NULL, SECRET_SERVICE_NONE, NULL, &error); service1 = secret_service_new_sync (SECRET_TYPE_SERVICE, NULL,
SECRET_SERVICE_NONE, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
service2 = secret_service_new_sync (NULL, SECRET_SERVICE_NONE, NULL, &error); service2 = secret_service_new_sync (SECRET_TYPE_SERVICE, NULL,
SECRET_SERVICE_NONE, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
g_assert (SECRET_IS_SERVICE (service1)); g_assert (SECRET_IS_SERVICE (service1));
@ -272,14 +274,16 @@ test_new_async (void)
/* Both these sohuld point to different things */ /* Both these sohuld point to different things */
secret_service_new (NULL, SECRET_SERVICE_NONE, NULL, on_complete_get_result, &result); secret_service_new (SECRET_TYPE_SERVICE, NULL, SECRET_SERVICE_NONE,
NULL, on_complete_get_result, &result);
g_assert (result == NULL); g_assert (result == NULL);
egg_test_wait (); egg_test_wait ();
service1 = secret_service_new_finish (result, &error); service1 = secret_service_new_finish (result, &error);
g_assert_no_error (error); g_assert_no_error (error);
g_clear_object (&result); g_clear_object (&result);
secret_service_new (NULL, SECRET_SERVICE_NONE, NULL, on_complete_get_result, &result); secret_service_new (SECRET_TYPE_SERVICE, NULL, SECRET_SERVICE_NONE, NULL,
on_complete_get_result, &result);
g_assert (result == NULL); g_assert (result == NULL);
egg_test_wait (); egg_test_wait ();
service2 = secret_service_new_finish (result, &error); service2 = secret_service_new_finish (result, &error);
@ -306,7 +310,8 @@ test_new_more_sync (Test *test,
const gchar *path; const gchar *path;
GList *collections; GList *collections;
service = secret_service_new_sync (NULL, SECRET_SERVICE_NONE, NULL, &error); service = secret_service_new_sync (SECRET_TYPE_SERVICE, NULL, SECRET_SERVICE_NONE,
NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
g_assert (SECRET_IS_SERVICE (service)); g_assert (SECRET_IS_SERVICE (service));
@ -317,7 +322,8 @@ test_new_more_sync (Test *test,
g_object_unref (service); g_object_unref (service);
egg_assert_not_object (service); egg_assert_not_object (service);
service = secret_service_new_sync (NULL, SECRET_SERVICE_LOAD_COLLECTIONS, NULL, &error); service = secret_service_new_sync (SECRET_TYPE_SERVICE, NULL,
SECRET_SERVICE_LOAD_COLLECTIONS, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
g_assert (SECRET_IS_SERVICE (service)); g_assert (SECRET_IS_SERVICE (service));
@ -330,7 +336,8 @@ test_new_more_sync (Test *test,
g_object_unref (service); g_object_unref (service);
egg_assert_not_object (service); egg_assert_not_object (service);
service = secret_service_new_sync (NULL, SECRET_SERVICE_OPEN_SESSION, NULL, &error); service = secret_service_new_sync (SECRET_TYPE_SERVICE, NULL,
SECRET_SERVICE_OPEN_SESSION, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
g_assert (SECRET_IS_SERVICE (service)); g_assert (SECRET_IS_SERVICE (service));
@ -353,7 +360,8 @@ test_new_more_async (Test *test,
const gchar *path; const gchar *path;
GList *collections; GList *collections;
secret_service_new (NULL, SECRET_SERVICE_LOAD_COLLECTIONS | SECRET_SERVICE_OPEN_SESSION, NULL, on_complete_get_result, &result); secret_service_new (SECRET_TYPE_SERVICE, NULL,
SECRET_SERVICE_LOAD_COLLECTIONS | SECRET_SERVICE_OPEN_SESSION, NULL, on_complete_get_result, &result);
g_assert (result == NULL); g_assert (result == NULL);
egg_test_wait (); egg_test_wait ();
@ -376,7 +384,8 @@ test_new_more_async (Test *test,
/* Now get a session with just collections */ /* Now get a session with just collections */
secret_service_new (NULL, SECRET_SERVICE_LOAD_COLLECTIONS, NULL, on_complete_get_result, &result); secret_service_new (SECRET_TYPE_SERVICE, NULL, SECRET_SERVICE_LOAD_COLLECTIONS,
NULL, on_complete_get_result, &result);
g_assert (result == NULL); g_assert (result == NULL);
egg_test_wait (); egg_test_wait ();
@ -462,7 +471,8 @@ test_ensure_sync (Test *test,
gboolean ret; gboolean ret;
/* Passing true, ensures session is established */ /* Passing true, ensures session is established */
service = secret_service_new_sync (NULL, SECRET_SERVICE_NONE, NULL, &error); service = secret_service_new_sync (SECRET_TYPE_SERVICE, NULL,
SECRET_SERVICE_NONE, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
g_assert (service != NULL); g_assert (service != NULL);
@ -499,7 +509,8 @@ test_ensure_async (Test *test,
gboolean ret; gboolean ret;
/* Passing true, ensures session is established */ /* Passing true, ensures session is established */
service = secret_service_new_sync (NULL, SECRET_SERVICE_NONE, NULL, &error); service = secret_service_new_sync (SECRET_TYPE_SERVICE, NULL,
SECRET_SERVICE_NONE, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
g_assert (service != NULL); g_assert (service != NULL);