root/trunk/libtinymail-maemo/tny-maemo-platform-factory.c

Revision 3666 (checked in by jdapena, 6 months ago)

* Use GOnce registering all types in tinymail to make

registering thread-safe.

Line 
1 /* libtinymail-camel - The Tiny Mail base library for Camel
2  * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with self library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #include <config.h>
20
21 #include <tny-maemo-platform-factory.h>
22
23 #include <tny-maemo-account-store.h>
24 #include <tny-maemo-device.h>
25 #include <tny-gtk-msg-view.h>
26 #include <tny-gtk-password-dialog.h>
27 #include <tny-camel-mime-part.h>
28 #include <tny-camel-msg.h>
29
30 static GObjectClass *parent_class = NULL;
31
32 static void
33 tny_maemo_platform_factory_instance_init (GTypeInstance *instance, gpointer g_class)
34 {
35         return;
36 }
37
38
39 static TnyMsg*
40 tny_maemo_platform_factory_new_msg (TnyPlatformFactory *self)
41 {
42         return tny_camel_msg_new ();
43 }
44
45
46 static TnyMimePart*
47 tny_maemo_platform_factory_new_mime_part (TnyPlatformFactory *self)
48 {
49         return tny_camel_mime_part_new ();
50 }
51
52 static TnyPasswordGetter*
53 tny_maemo_platform_factory_new_password_getter (TnyPlatformFactory *self)
54 {
55         return TNY_PASSWORD_GETTER (tny_gtk_password_dialog_new ());
56 }
57
58 static TnyAccountStore*
59 tny_maemo_platform_factory_new_account_store (TnyPlatformFactory *self)
60 {
61         return TNY_ACCOUNT_STORE (tny_maemo_account_store_new ());
62 }
63
64 static TnyDevice*
65 tny_maemo_platform_factory_new_device (TnyPlatformFactory *self)
66 {
67         return TNY_DEVICE (tny_maemo_device_new ());
68 }
69
70 static TnyMsgView*
71 tny_maemo_platform_factory_new_msg_view (TnyPlatformFactory *self)
72 {
73         return tny_gtk_msg_view_new ();
74 }
75
76 /**
77  * tny_maemo_platform_factory_get_instance:
78  *
79  *
80  * Return value: The #TnyPlatformFactory singleton instance implemented for Maemo
81  **/
82 TnyPlatformFactory*
83 tny_maemo_platform_factory_get_instance (void)
84 {
85         TnyMaemoPlatformFactory *self = g_object_new (TNY_TYPE_MAEMO_PLATFORM_FACTORY, NULL);
86
87         return TNY_PLATFORM_FACTORY (self);
88 }
89
90
91 static void
92 tny_maemo_platform_factory_finalize (GObject *object)
93 {
94         (*parent_class->finalize) (object);
95
96         return;
97 }
98
99
100 static void
101 tny_platform_factory_init (gpointer g, gpointer iface_data)
102 {
103         TnyPlatformFactoryIface *klass = (TnyPlatformFactoryIface *)g;
104
105         klass->new_account_store= tny_maemo_platform_factory_new_account_store;
106         klass->new_device= tny_maemo_platform_factory_new_device;
107         klass->new_msg_view= tny_maemo_platform_factory_new_msg_view;
108         klass->new_msg= tny_maemo_platform_factory_new_msg;
109         klass->new_mime_part= tny_maemo_platform_factory_new_mime_part;
110         klass->new_password_getter= tny_maemo_platform_factory_new_password_getter;
111
112         return;
113 }
114
115
116 static TnyMaemoPlatformFactory *the_singleton = NULL;
117
118
119 static GObject*
120 tny_maemo_platform_factory_constructor (GType type, guint n_construct_params,
121                         GObjectConstructParam *construct_params)
122 {
123         GObject *object;
124
125         /* TODO: potential problem: singleton without lock */
126
127         if (G_UNLIKELY (!the_singleton))
128         {
129                 object = G_OBJECT_CLASS (parent_class)->constructor (type,
130                                 n_construct_params, construct_params);
131
132                 the_singleton = TNY_MAEMO_PLATFORM_FACTORY (object);
133         }
134         else
135         {
136                 /* refdbg killed bug!
137                 object = g_object_ref (G_OBJECT (the_singleton)); */
138
139                 object = G_OBJECT (the_singleton);
140                 g_object_freeze_notify (G_OBJECT(the_singleton));
141         }
142
143         return object;
144 }
145
146 static void
147 tny_maemo_platform_factory_class_init (TnyMaemoPlatformFactoryClass *class)
148 {
149         GObjectClass *object_class;
150
151         parent_class = g_type_class_peek_parent (class);
152         object_class = (GObjectClass*) class;
153
154         object_class->finalize = tny_maemo_platform_factory_finalize;
155         object_class->constructor = tny_maemo_platform_factory_constructor;
156
157         return;
158 }
159
160 static gpointer
161 tny_maemo_platform_factory_register_type (gpointer notused)
162 {
163         GType type = 0;
164
165         static const GTypeInfo info =
166                 {
167                         sizeof (TnyMaemoPlatformFactoryClass),
168                         NULL,   /* base_init */
169                         NULL,   /* base_finalize */
170                         (GClassInitFunc) tny_maemo_platform_factory_class_init,   /* class_init */
171                         NULL,   /* class_finalize */
172                         NULL,   /* class_data */
173                         sizeof (TnyMaemoPlatformFactory),
174                         0,      /* n_preallocs */
175                         tny_maemo_platform_factory_instance_init    /* instance_init */
176                 };
177        
178         static const GInterfaceInfo tny_platform_factory_info =
179                 {
180                         (GInterfaceInitFunc) tny_platform_factory_init, /* interface_init */
181                         NULL,         /* interface_finalize */
182                         NULL          /* interface_data */
183                 };
184        
185         type = g_type_register_static (G_TYPE_OBJECT,
186                                        "TnyMaemoPlatformFactory",
187                                        &info, 0);
188        
189         g_type_add_interface_static (type, TNY_TYPE_PLATFORM_FACTORY,
190                                      &tny_platform_factory_info);
191        
192         return GUINT_TO_POINTER (type);
193 }
194
195 GType
196 tny_maemo_platform_factory_get_type (void)
197 {
198         static GOnce once = G_ONCE_INIT;
199         g_once (&once, tny_maemo_platform_factory_register_type, NULL);
200         return GPOINTER_TO_UINT (once.retval);
201 }
Note: See TracBrowser for help on using the browser.