Implementing a platform factory
About
The platform factory (TnyPlatformFactory) is a factory type that returns object instances that are specific to the platform or the application software being build on top of the Tinymail framework.
Using tools/
../tools/gtypeinterface-h-files-to-c-file.pl TnyMyPlatformFactory \ ../libtinymailui/tny-platform-factory.h > \ tny-my-platform-factory.c
Will generate you something like this (tny-my-platform-factory.c)
/* Your copyright here */
#include <config.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <tny-my-platform-factory.h>
#include "tny-my-platform-factory-priv.h"
static GObjectClass *parent_class = NULL;
static TnyAccountStore*
tny_my_platform_factory_new_account_store (TnyPlatformFactory *self)
{
return tny_account_store_new ()
}
static TnyDevice*
tny_my_platform_factory_new_device (TnyPlatformFactory *self)
{
return tny_device_new ()
}
static TnyMsgView*
tny_my_platform_factory_new_msg_view (TnyPlatformFactory *self)
{
return tny_msg_view_new ()
}
static TnyMsg*
tny_my_platform_factory_new_msg (TnyPlatformFactory *self)
{
return tny_msg_new ();
}
static TnyMimePart*
tny_my_platform_factory_new_mime_part (TnyPlatformFactory *self)
{
return tny_mime_part_new ();
}
static TnyPasswordGetter*
tny_my_platform_factory_new_password_getter (TnyPlatformFactory *self)
{
return tny_password_getter_new ();
}
static void
tny_my_platform_factory_finalize (GObject *object)
{
parent_class->finalize (object);
}
static void
tny_platform_factory_init (TnyPlatformFactoryIface *klass)
{
klass->new_account_store_func = tny_my_platform_factory_new_account_store;
klass->new_device_func = tny_my_platform_factory_new_device;
klass->new_msg_view_func = tny_my_platform_factory_new_msg_view;
klass->new_msg_func = tny_my_platform_factory_new_msg;
klass->new_mime_part_func = tny_my_platform_factory_new_mime_part;
klass->new_password_getter_func = tny_my_platform_factory_new_password_getter;
}
static void
tny_my_platform_factory_class_init (TnyMyPlatformFactoryClass *klass)
{
GObjectClass *object_class;
parent_class = g_type_class_peek_parent (klass);
object_class = (GObjectClass*) klass;
object_class->finalize = tny_my_platform_factory_finalize;
}
GType
tny_my_platform_factory_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY(type == 0))
{
static const GTypeInfo info =
{
sizeof (TnyMyPlatformFactoryClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) tny_my_platform_factory_class_init, /* class_init */
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (TnyMyPlatformFactory),
0, /* n_preallocs */
tny_my_platform_factory_instance_init, /* instance_init */
NULL
};
static const GInterfaceInfo tny_platform_factory_info =
{
(GInterfaceInitFunc) tny_platform_factory_init, /* interface_init */
NULL, /* interface_finalize */
NULL /* interface_data */
}
type = g_type_register_static (G_TYPE_OBJECT,
"TnyMyPlatformFactory",
&info, 0);
g_type_add_interface_static (type, TNY_PLATFORM_FACTORY,
&tny_platform_factory_info
}
return type;
}
Making the type a singleton
+ static TnyMyPlatformFactory *the_singleton = NULL;
+ static GObject*
+ tny_my_platform_factory_constructor (GType type, guint n_construct_params,
+ GObjectConstructParam *construct_params)
+ {
+ GObject *object;
+ if (!the_singleton) {
+ object = G_OBJECT_CLASS (parent_class)->constructor (type,
+ n_construct_params, construct_params);
+ the_singleton = TNY_MY_PLATFORM_FACTORY (object);
+ } else {
+ object = g_object_ref (G_OBJECT (the_singleton)); */
+ object = G_OBJECT (the_singleton);
+ g_object_freeze_notify (G_OBJECT(the_singleton));
+ }
+ return object;
+ }
static void
tny_my_platform_factory_class_init (TnyMyPlatformFactoryClass *klass)
{
GObjectClass *object_class;
parent_class = g_type_class_peek_parent (klass);
object_class = (GObjectClass*) klass;
object_class->finalize = tny_my_platform_factory_finalize;
+ object_class->constructor = tny_my_platform_factory_constructor;
}
Implementing the six methods
tny_my_platform_factory_new_account_store (TnyAccountStore)
This one must return an instance of the account store implementation which your platform will use. Check out Implementing an account store to learn how to create your own.
#include <tny-my-account-store.h>
static TnyAccountStore*
tny_my_platform_factory_new_account_store (TnyPlatformFactory *self)
{
return tny_my_account_store_new ()
}
tny_my_platform_factory_new_device (TnyDevice)
This one must return an instance of the device implementation which your platform will use. Check out Implementing a device type to learn how to create your own.
#include <tny-my-device.h>
static TnyDevice*
tny_my_platform_factory_new_device (TnyPlatformFactory *self)
{
return tny_my_device_new ()
}
tny_my_platform_factory_new_msg_view (TnyMsgView)
This one must return an instance of the message view implementation which your platform will use. Check out Implementing a message view to learn how to create your own.
#include <tny-my-msg-view.h>
static TnyMsgView*
tny_my_platform_factory_new_msg_view (TnyPlatformFactory *self)
{
return tny_my_msg_view_new ()
}
tny_my_platform_factory_new_msg (TnyMsg)
This one must return a new message. You can for example use the TnyCamelMsg implementation.
#include <tny-camel-msg.h>
static TnyMsg*
tny_my_platform_factory_new_msg (TnyPlatformFactory *self)
{
return tny_camel_msg_new ();
}
tny_my_platform_factory_new_mime_part (TnyMimePart)
This one must return a new mime part. You can for example use the TnyCamelMimePart implementation.
#include <tny-camel-mime-part.h>
static TnyMimePart*
tny_my_platform_factory_new_mime_part (TnyPlatformFactory *self)
{
return tny_camel_mime_part_new ();
}
tny_my_platform_factory_new_password_getter (TnyPasswordGetter)
This one must return a new password getter. You can for example use the TnyGtkPasswordDialog or the TnyGnomeKeyringPasswordGetter implementation.
#include <tny-gtk-password-dialog.h>
static TnyPasswordGetter*
tny_my_platform_factory_new_password_getter (TnyPlatformFactory *self)
{
return tny_gtk_password_dialog_new ();
}
The public get_instance method
/**
* tny_my_platform_factory_get_instance:
*
*
* Return value: The #TnyPlatformFactory singleton instance implemented for my platform
**/
TnyPlatformFactory*
tny_my_platform_factory_get_instance (void)
{
TnyMyPlatformFactory *self = g_object_new (TNY_TYPE_MY_PLATFORM_FACTORY, NULL);
return TNY_PLATFORM_FACTORY (self);
}
The public .h file (tny-my-platform-factory.h)
#ifndef TNY_MY_PLATFORM_FACTORY_H
#define TNY_MY_PLATFORM_FACTORY_H
/* Your copyright here */
#include <glib.h>
#include <glib-object.h>
#include <tny-platform-factory.h>
G_BEGIN_DECLS
#define TNY_TYPE_MY_PLATFORM_FACTORY \
(tny_my_platform_factory_get_type ())
#define TNY_MY_PLATFORM_FACTORY(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), TNY_TYPE_MY_PLATFORM_FACTORY, TnyMyPlatformFactory))
#define TNY_MY_PLATFORM_FACTORY_CLASS(vtable) \
(G_TYPE_CHECK_CLASS_CAST ((vtable), TNY_TYPE_MY_PLATFORM_FACTORY, TnyMyPlatformFactoryClass))
#define TNY_IS_MY_PLATFORM_FACTORY(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), TNY_TYPE_MY_PLATFORM_FACTORY))
#define TNY_IS_MY_PLATFORM_FACTORY_CLASS(vtable) \
(G_TYPE_CHECK_CLASS_TYPE ((vtable), TNY_TYPE_MY_PLATFORM_FACTORY))
#define TNY_MY_PLATFORM_FACTORY_GET_CLASS(inst) \
(G_TYPE_INSTANCE_GET_CLASS ((inst), TNY_TYPE_MY_PLATFORM_FACTORY, TnyMyPlatformFactoryClass))
typedef struct _TnyMyPlatformFactory TnyMyPlatformFactory;
typedef struct _TnyMyPlatformFactoryClass TnyMyPlatformFactoryClass;
struct _TnyMyPlatformFactory
{
GObject parent;
};
struct _TnyMyPlatformFactoryClass
{
GObjectClass parent;
};
GType tny_my_platform_factory_get_type (void);
TnyPlatformFactory* tny_my_platform_factory_get_instance (void);
G_END_DECLS
#endif
Adjusting Makefile.am
libtinymail_my_1_0_headers = \
tny-my-account-store.h \
tny-my-password-dialog.h \
tny-my-device.h \
+ tny-my-platform-factory.h
libtinymail_my_1_0_la_SOURCES = \
$(libtinymail_my_1_0_headers) \
tny-my-account-store.c \
tny-my-device-priv.h \
tny-my-device.c \
tny-my-password-dialog.c \
+ tny-my-platform-factory.c
