Simpler way to use custom service/collection/item types

The previous way of setting collection_gtype and item_gtype
on SecretServiceClass was not very bindings friendly. Instead
allow per instance virtual functions to return the GTypes.
This commit is contained in:
Stef Walter 2013-06-21 12:35:54 +02:00
parent 143f00ceef
commit d6367b1de0
3 changed files with 89 additions and 5 deletions

View File

@ -74,7 +74,7 @@ secret_collection_new_for_dbus_path (SecretService *service,
proxy = G_DBUS_PROXY (service); proxy = G_DBUS_PROXY (service);
g_async_initable_new_async (SECRET_SERVICE_GET_CLASS (service)->collection_gtype, g_async_initable_new_async (secret_service_get_collection_gtype (service),
G_PRIORITY_DEFAULT, cancellable, callback, user_data, G_PRIORITY_DEFAULT, cancellable, callback, user_data,
"g-flags", G_DBUS_CALL_FLAGS_NONE, "g-flags", G_DBUS_CALL_FLAGS_NONE,
"g-interface-info", _secret_gen_collection_interface_info (), "g-interface-info", _secret_gen_collection_interface_info (),
@ -154,7 +154,7 @@ secret_collection_new_for_dbus_path_sync (SecretService *service,
proxy = G_DBUS_PROXY (service); proxy = G_DBUS_PROXY (service);
return g_initable_new (SECRET_SERVICE_GET_CLASS (service)->collection_gtype, return g_initable_new (secret_service_get_collection_gtype (service),
cancellable, error, cancellable, error,
"g-flags", G_DBUS_CALL_FLAGS_NONE, "g-flags", G_DBUS_CALL_FLAGS_NONE,
"g-interface-info", _secret_gen_collection_interface_info (), "g-interface-info", _secret_gen_collection_interface_info (),
@ -199,7 +199,7 @@ secret_item_new_for_dbus_path (SecretService *service,
proxy = G_DBUS_PROXY (service); proxy = G_DBUS_PROXY (service);
g_async_initable_new_async (SECRET_SERVICE_GET_CLASS (service)->item_gtype, g_async_initable_new_async (secret_service_get_item_gtype (service),
G_PRIORITY_DEFAULT, cancellable, callback, user_data, G_PRIORITY_DEFAULT, cancellable, callback, user_data,
"g-flags", G_DBUS_CALL_FLAGS_NONE, "g-flags", G_DBUS_CALL_FLAGS_NONE,
"g-interface-info", _secret_gen_item_interface_info (), "g-interface-info", _secret_gen_item_interface_info (),
@ -276,7 +276,7 @@ secret_item_new_for_dbus_path_sync (SecretService *service,
proxy = G_DBUS_PROXY (service); proxy = G_DBUS_PROXY (service);
return g_initable_new (SECRET_SERVICE_GET_CLASS (service)->item_gtype, return g_initable_new (secret_service_get_item_gtype (service),
cancellable, error, cancellable, error,
"g-flags", G_DBUS_CALL_FLAGS_NONE, "g-flags", G_DBUS_CALL_FLAGS_NONE,
"g-interface-info", _secret_gen_item_interface_info (), "g-interface-info", _secret_gen_item_interface_info (),

View File

@ -496,6 +496,24 @@ secret_service_signal (GDBusProxy *proxy,
g_variant_unref (paths); g_variant_unref (paths);
} }
static GType
secret_service_real_get_collection_gtype (SecretService *self)
{
SecretServiceClass *klass;
klass = SECRET_SERVICE_GET_CLASS (self);
return klass->collection_gtype;
}
static GType
secret_service_real_get_item_gtype (SecretService *self)
{
SecretServiceClass *klass;
klass = SECRET_SERVICE_GET_CLASS (self);
return klass->item_gtype;
}
static void static void
secret_service_class_init (SecretServiceClass *klass) secret_service_class_init (SecretServiceClass *klass)
{ {
@ -516,6 +534,8 @@ secret_service_class_init (SecretServiceClass *klass)
klass->item_gtype = SECRET_TYPE_ITEM; klass->item_gtype = SECRET_TYPE_ITEM;
klass->collection_gtype = SECRET_TYPE_COLLECTION; klass->collection_gtype = SECRET_TYPE_COLLECTION;
klass->get_item_gtype = secret_service_real_get_item_gtype;
klass->get_collection_gtype = secret_service_real_get_collection_gtype;
/** /**
* SecretService:flags: * SecretService:flags:
@ -1729,3 +1749,59 @@ secret_service_prompt_finish (SecretService *self,
return (klass->prompt_finish) (self, result, return_type, error); return (klass->prompt_finish) (self, result, return_type, error);
} }
/**
* secret_service_get_collection_gtype:
* @self: the secret service
*
* Get the GObject type for collections instantiated by this service.
* This will always be either #SecretCollection or derived from it.
*
* Returns: the gobject type for collections
*/
GType
secret_service_get_collection_gtype (SecretService *self)
{
SecretServiceClass *klass;
GType type;
g_return_val_if_fail (SECRET_IS_SERVICE (self), 0);
klass = SECRET_SERVICE_GET_CLASS (self);
g_return_val_if_fail (klass->get_collection_gtype != NULL,
SECRET_TYPE_COLLECTION);
type = (klass->get_collection_gtype) (self);
g_return_val_if_fail (g_type_is_a (type, SECRET_TYPE_COLLECTION),
SECRET_TYPE_COLLECTION);
return type;
}
/**
* secret_service_get_item_gtype:
* @self: the collection
*
* Get the GObject type for items instantiated by this collection.
* This will always be either #SecretItem or derived from it.
*
* Returns: the gobject type for items
*/
GType
secret_service_get_item_gtype (SecretService *self)
{
SecretServiceClass *klass;
GType type;
g_return_val_if_fail (SECRET_IS_SERVICE (self), 0);
klass = SECRET_SERVICE_GET_CLASS (self);
g_return_val_if_fail (klass->get_item_gtype != NULL,
SECRET_TYPE_ITEM);
type = (klass->get_item_gtype) (self);
g_return_val_if_fail (g_type_is_a (type, SECRET_TYPE_ITEM),
SECRET_TYPE_ITEM);
return type;
}

View File

@ -84,12 +84,20 @@ struct _SecretServiceClass {
const GVariantType *return_type, const GVariantType *return_type,
GError **error); GError **error);
GType (* get_collection_gtype) (SecretService *self);
GType (* get_item_gtype) (SecretService *self);
/*< private >*/ /*< private >*/
gpointer padding[16]; gpointer padding[14];
}; };
GType secret_service_get_type (void) G_GNUC_CONST; GType secret_service_get_type (void) G_GNUC_CONST;
GType secret_service_get_collection_gtype (SecretService *self);
GType secret_service_get_item_gtype (SecretService *self);
void secret_service_get (SecretServiceFlags flags, void secret_service_get (SecretServiceFlags flags,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,