669f75760e48e87a167a54b9e88c06b6918f7fdb
[modest] / src / modest-tny-platform-factory.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 /* modest-tny-platform-factory.c */
31 #include <config.h>
32
33 /* MODES_PLATFORM_ID: 1 ==> gtk, 2==> maemo */
34 #if MODEST_PLATFORM_ID==1   
35 #include <tny-gnome-device.h>
36 #elif MODEST_PLATFORM_ID==2
37 #include <tny-maemo-device.h>
38 #endif
39
40 #include "modest-tny-platform-factory.h"
41 #include "modest-tny-account-store.h"
42
43 /* 'private'/'protected' functions */
44 static void modest_tny_platform_factory_class_init (ModestTnyPlatformFactoryClass *klass);
45 static void modest_tny_platform_factory_init       (ModestTnyPlatformFactory *obj);
46 static void modest_tny_platform_factory_finalize   (GObject *obj);
47 static GObject *modest_tny_platform_factory_constructor (GType type, guint n_construct_params,
48                                                          GObjectConstructParam *construct_params);
49 static void tny_platform_factory_init (gpointer g, gpointer iface_data);
50 static TnyAccountStore *modest_tny_platform_factory_new_account_store (TnyPlatformFactory *self);
51 static TnyDevice *modest_tny_platform_factory_new_device (TnyPlatformFactory *self);
52 static TnyMsgView *modest_tny_platform_factory_new_msg_view (TnyPlatformFactory *self);
53 /* list my signals  */
54 enum {
55         /* MY_SIGNAL_1, */
56         /* MY_SIGNAL_2, */
57         LAST_SIGNAL
58 };
59
60 typedef struct _ModestTnyPlatformFactoryPrivate ModestTnyPlatformFactoryPrivate;
61 struct _ModestTnyPlatformFactoryPrivate {
62         ModestTnyAccountStore *account_store;
63         ModestConf *conf;
64         ModestAccountMgr *account_mgr;
65 };
66 #define MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
67                                                          MODEST_TYPE_TNY_PLATFORM_FACTORY, \
68                                                          ModestTnyPlatformFactoryPrivate))
69 /* globals */
70 static GObjectClass *parent_class = NULL;
71 static ModestTnyPlatformFactory *singleton = NULL;
72
73 /* uncomment the following if you have defined any signals */
74 /* static guint signals[LAST_SIGNAL] = {0}; */
75
76 GType
77 modest_tny_platform_factory_get_type (void)
78 {
79         static GType my_type = 0;
80         if (!my_type) {
81                 static const GTypeInfo my_info = {
82                         sizeof(ModestTnyPlatformFactoryClass),
83                         NULL,           /* base init */
84                         NULL,           /* base finalize */
85                         (GClassInitFunc) modest_tny_platform_factory_class_init,
86                         NULL,           /* class finalize */
87                         NULL,           /* class data */
88                         sizeof(ModestTnyPlatformFactory),
89                         1,              /* n_preallocs */
90                         (GInstanceInitFunc) modest_tny_platform_factory_init,
91                         NULL
92                 };
93
94                 static const GInterfaceInfo tny_platform_factory_info = {
95                         (GInterfaceInitFunc) tny_platform_factory_init, /* interface_init */
96                         NULL,         /* interface_finalize */
97                         NULL          /* interface_data */
98                 };
99
100                 my_type = g_type_register_static (G_TYPE_OBJECT,
101                                                   "ModestTnyPlatformFactory",
102                                                   &my_info, 0);
103
104                 g_type_add_interface_static (my_type, TNY_TYPE_PLATFORM_FACTORY, 
105                                              &tny_platform_factory_info);
106         }
107         return my_type;
108 }
109
110 static void
111 modest_tny_platform_factory_class_init (ModestTnyPlatformFactoryClass *klass)
112 {
113         GObjectClass *gobject_class;
114
115         gobject_class = (GObjectClass*) klass;
116         parent_class            = g_type_class_peek_parent (klass);
117
118         gobject_class->finalize = modest_tny_platform_factory_finalize;
119         gobject_class->constructor = modest_tny_platform_factory_constructor;
120
121         g_type_class_add_private (gobject_class, sizeof(ModestTnyPlatformFactoryPrivate));
122 }
123
124 static void
125 modest_tny_platform_factory_init (ModestTnyPlatformFactory *obj)
126 {
127         ModestTnyPlatformFactoryPrivate *priv;
128         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(obj);
129
130         priv->account_mgr   = NULL;
131         priv->conf          = NULL;
132         priv->account_store = NULL;
133 }
134
135 static GObject*
136 modest_tny_platform_factory_constructor (GType type, guint n_construct_params,
137                                          GObjectConstructParam *construct_params)
138 {
139         GObject *object;
140
141         if (!singleton) {
142                 object = G_OBJECT_CLASS (parent_class)->constructor (type,
143                                 n_construct_params, construct_params);
144
145                 singleton = MODEST_TNY_PLATFORM_FACTORY (object);
146         } else {
147                 object = G_OBJECT (singleton);
148                 g_object_freeze_notify (G_OBJECT (singleton));
149         }
150
151         return object;
152 }
153
154 static void
155 modest_tny_platform_factory_finalize (GObject *obj)
156 {
157         ModestTnyPlatformFactoryPrivate *priv;
158
159         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(obj);
160
161         if (priv->account_mgr) {
162                 g_object_unref (priv->account_mgr);
163                 priv->account_mgr = NULL;
164         }
165
166         if (priv->conf) {
167                 g_object_unref (priv->conf);
168                 priv->conf = NULL;
169         }
170
171         if (priv->account_store) {
172                 g_object_unref (priv->account_store);
173                 priv->account_store = NULL;
174         }
175         
176         G_OBJECT_CLASS(parent_class)->finalize (obj);
177 }
178
179 static void
180 tny_platform_factory_init (gpointer g, gpointer iface_data)
181 {
182         TnyPlatformFactoryIface *klass = (TnyPlatformFactoryIface *)g;
183
184         klass->new_account_store_func = modest_tny_platform_factory_new_account_store;
185         klass->new_device_func = modest_tny_platform_factory_new_device;
186         klass->new_msg_view_func = modest_tny_platform_factory_new_msg_view;
187
188         return;
189 }
190
191 TnyPlatformFactory *
192 modest_tny_platform_factory_get_instance (void)
193 {
194         ModestTnyPlatformFactory *self = g_object_new (MODEST_TYPE_TNY_PLATFORM_FACTORY, NULL);
195
196         return TNY_PLATFORM_FACTORY (self);
197 }
198
199 static TnyAccountStore *
200 modest_tny_platform_factory_new_account_store (TnyPlatformFactory *self)
201 {
202         ModestTnyPlatformFactoryPrivate *priv;
203
204         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(self);
205
206         if (!priv->account_store) {
207                 if (!priv->account_mgr)
208                         modest_tny_platform_factory_get_modest_account_mgr_instance (self);
209
210                 priv->account_store = modest_tny_account_store_new (priv->account_mgr);
211         }
212
213         return TNY_ACCOUNT_STORE (priv->account_store);
214 }
215
216 static TnyDevice *
217 modest_tny_platform_factory_new_device (TnyPlatformFactory *self)
218 {
219 /* MODES_PLATFORM_ID: 1 ==> gtk, 2==> maemo */
220 #if MODEST_PLATFORM_ID==1   
221         return TNY_DEVICE (tny_gnome_device_new ());
222 #elif MODEST_PLATFORM_ID==2
223         return TNY_DEVICE (tny_maemo_device_new ());
224 #else
225         return NULL;
226 #endif /* MODEST_PLATFORM */
227 }
228
229 static TnyMsgView*
230 modest_tny_platform_factory_new_msg_view (TnyPlatformFactory *self)
231 {
232         /* TODO */
233         return NULL;
234 }
235
236 ModestAccountMgr *
237 modest_tny_platform_factory_get_modest_account_mgr_instance (TnyPlatformFactory *fact)
238 {
239         ModestTnyPlatformFactoryPrivate *priv;
240
241         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact);
242
243         if (!priv->account_mgr) {
244                 if (!priv->conf)
245                         modest_tny_platform_factory_get_modest_conf_instance (fact);
246
247                 priv->account_mgr = modest_account_mgr_new (priv->conf);
248         }
249
250         return priv->account_mgr;
251 }
252
253 ModestConf *
254 modest_tny_platform_factory_get_modest_conf_instance (TnyPlatformFactory *fact)
255 {
256         ModestTnyPlatformFactoryPrivate *priv;
257
258         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact);
259
260         if (!priv->conf)
261                 priv->conf = modest_conf_new ();
262
263         return priv->conf;
264 }