* modest-tny-platform-factory.[ch]:
[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 #include <tny-camel-header.h>
34 #include <tny-camel-mime-part.h>
35 #include <tny-camel-msg.h>
36
37 /* MODES_PLATFORM_ID: 1 ==> gtk, 2==> maemo */
38 #if MODEST_PLATFORM_ID==1   
39 #include <tny-gnome-device.h>
40 #elif MODEST_PLATFORM_ID==2
41 #include <tny-maemo-device.h>
42 #endif
43
44 #include "modest-tny-platform-factory.h"
45 #include "modest-tny-account-store.h"
46
47 /* 'private'/'protected' functions */
48 static void modest_tny_platform_factory_class_init (ModestTnyPlatformFactoryClass *klass);
49 static void modest_tny_platform_factory_init       (ModestTnyPlatformFactory *obj);
50 static void modest_tny_platform_factory_finalize   (GObject *obj);
51 static GObject *modest_tny_platform_factory_constructor (GType type, guint n_construct_params,
52                                                          GObjectConstructParam *construct_params);
53 static void tny_platform_factory_init (gpointer g, gpointer iface_data);
54
55 static TnyAccountStore* modest_tny_platform_factory_new_account_store (TnyPlatformFactory *self);
56 static TnyDevice*       modest_tny_platform_factory_new_device        (TnyPlatformFactory *self);
57 static TnyMsgView*      modest_tny_platform_factory_new_msg_view      (TnyPlatformFactory *self);
58 static TnyMsg*          modest_tny_platform_factory_new_msg           (TnyPlatformFactory *self);
59 static TnyMimePart*     modest_tny_platform_factory_new_mime_part     (TnyPlatformFactory *self);
60 static TnyHeader*       modest_tny_platform_factory_new_header        (TnyPlatformFactory *self);
61
62
63 /* list my signals  */
64 enum {
65         /* MY_SIGNAL_1, */
66         /* MY_SIGNAL_2, */
67         LAST_SIGNAL
68 };
69
70 typedef struct _ModestTnyPlatformFactoryPrivate ModestTnyPlatformFactoryPrivate;
71 struct _ModestTnyPlatformFactoryPrivate {
72         ModestTnyAccountStore    *account_store;
73         ModestConf               *conf;
74         ModestAccountMgr         *account_mgr;
75         ModestMailOperationQueue *mail_op_queue;
76         ModestCacheMgr           *cache_mgr;
77 };
78
79 #define MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
80                                                          MODEST_TYPE_TNY_PLATFORM_FACTORY, \
81                                                          ModestTnyPlatformFactoryPrivate))
82 /* globals */
83 static GObjectClass *parent_class = NULL;
84 static ModestTnyPlatformFactory *singleton = NULL;
85
86 /* uncomment the following if you have defined any signals */
87 /* static guint signals[LAST_SIGNAL] = {0}; */
88
89 GType
90 modest_tny_platform_factory_get_type (void)
91 {
92         static GType my_type = 0;
93         if (!my_type) {
94                 static const GTypeInfo my_info = {
95                         sizeof(ModestTnyPlatformFactoryClass),
96                         NULL,           /* base init */
97                         NULL,           /* base finalize */
98                         (GClassInitFunc) modest_tny_platform_factory_class_init,
99                         NULL,           /* class finalize */
100                         NULL,           /* class data */
101                         sizeof(ModestTnyPlatformFactory),
102                         1,              /* n_preallocs */
103                         (GInstanceInitFunc) modest_tny_platform_factory_init,
104                         NULL
105                 };
106
107                 static const GInterfaceInfo tny_platform_factory_info = {
108                         (GInterfaceInitFunc) tny_platform_factory_init, /* interface_init */
109                         NULL,         /* interface_finalize */
110                         NULL          /* interface_data */
111                 };
112
113                 my_type = g_type_register_static (G_TYPE_OBJECT,
114                                                   "ModestTnyPlatformFactory",
115                                                   &my_info, 0);
116
117                 g_type_add_interface_static (my_type, TNY_TYPE_PLATFORM_FACTORY, 
118                                              &tny_platform_factory_info);
119         }
120         return my_type;
121 }
122
123 static void
124 modest_tny_platform_factory_class_init (ModestTnyPlatformFactoryClass *klass)
125 {
126         GObjectClass *gobject_class;
127
128         gobject_class = (GObjectClass*) klass;
129         parent_class            = g_type_class_peek_parent (klass);
130
131         gobject_class->finalize = modest_tny_platform_factory_finalize;
132         gobject_class->constructor = modest_tny_platform_factory_constructor;
133
134         g_type_class_add_private (gobject_class, sizeof(ModestTnyPlatformFactoryPrivate));
135 }
136
137 static void
138 modest_tny_platform_factory_init (ModestTnyPlatformFactory *obj)
139 {
140         ModestTnyPlatformFactoryPrivate *priv;
141         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(obj);
142
143         priv->account_mgr   = NULL;
144         priv->conf          = NULL;
145         priv->account_store = NULL;
146         priv->mail_op_queue = NULL;
147         priv->cache_mgr     = NULL;
148 }
149
150 static GObject*
151 modest_tny_platform_factory_constructor (GType type, guint n_construct_params,
152                                          GObjectConstructParam *construct_params)
153 {
154         GObject *object;
155
156         if (!singleton) {
157                 object = G_OBJECT_CLASS (parent_class)->constructor (type,
158                                 n_construct_params, construct_params);
159
160                 singleton = MODEST_TNY_PLATFORM_FACTORY (object);
161         } else {
162                 object = G_OBJECT (singleton);
163                 g_object_freeze_notify (G_OBJECT (singleton));
164         }
165
166         return object;
167 }
168
169 static void
170 modest_tny_platform_factory_finalize (GObject *obj)
171 {
172         ModestTnyPlatformFactoryPrivate *priv;
173
174         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(obj);
175
176         if (priv->account_mgr)
177                 g_object_unref (G_OBJECT(priv->account_mgr));
178
179         if (priv->conf)
180                 g_object_unref (G_OBJECT(priv->conf));
181
182         if (priv->account_store)
183                 g_object_unref (G_OBJECT(priv->account_store));
184
185         if (priv->mail_op_queue)
186                 g_object_unref (G_OBJECT(priv->mail_op_queue));
187
188         if (priv->cache_mgr)
189                 g_object_unref (G_OBJECT(priv->cache_mgr));
190         
191         G_OBJECT_CLASS(parent_class)->finalize (obj);
192 }
193
194 static void
195 tny_platform_factory_init (gpointer g, gpointer iface_data)
196 {
197         TnyPlatformFactoryIface *klass = (TnyPlatformFactoryIface *)g;
198
199         klass->new_account_store_func = modest_tny_platform_factory_new_account_store;
200         klass->new_device_func        = modest_tny_platform_factory_new_device;
201         klass->new_msg_view_func      = modest_tny_platform_factory_new_msg_view;
202         klass->new_msg_func           = modest_tny_platform_factory_new_msg;
203         klass->new_mime_part_func     = modest_tny_platform_factory_new_mime_part;
204         klass->new_header_func        = modest_tny_platform_factory_new_header;
205
206         return;
207 }
208
209 TnyPlatformFactory *
210 modest_tny_platform_factory_get_instance (void)
211 {
212         ModestTnyPlatformFactory *self = g_object_new (MODEST_TYPE_TNY_PLATFORM_FACTORY, NULL);
213
214         return TNY_PLATFORM_FACTORY (self);
215 }
216
217 static TnyAccountStore *
218 modest_tny_platform_factory_new_account_store (TnyPlatformFactory *self)
219 {
220         ModestTnyPlatformFactoryPrivate *priv;
221
222         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(self);
223
224         if (!priv->account_store) {
225                 if (!priv->account_mgr)
226                         modest_tny_platform_factory_get_account_mgr_instance (MODEST_TNY_PLATFORM_FACTORY (self));
227
228                 priv->account_store = modest_tny_account_store_new (priv->account_mgr);
229         }
230
231         return TNY_ACCOUNT_STORE (priv->account_store);
232 }
233
234 static TnyDevice *
235 modest_tny_platform_factory_new_device (TnyPlatformFactory *self)
236 {
237 /* MODES_PLATFORM_ID: 1 ==> gtk, 2==> maemo */
238 #if MODEST_PLATFORM_ID==1   
239         return TNY_DEVICE (tny_gnome_device_new ());
240 #elif MODEST_PLATFORM_ID==2
241         return TNY_DEVICE (tny_maemo_device_new ());
242 #else
243         g_return_val_if_reached (NULL);
244 #endif /* MODEST_PLATFORM */
245 }
246
247 static TnyMsgView*
248 modest_tny_platform_factory_new_msg_view (TnyPlatformFactory *self)
249 {
250         /* TODO */
251         return NULL;
252 }
253
254 static TnyMsg*
255 modest_tny_platform_factory_new_msg (TnyPlatformFactory *self)
256 {
257         return tny_camel_msg_new ();
258 }
259
260
261 static TnyMimePart*
262 modest_tny_platform_factory_new_mime_part (TnyPlatformFactory *self)
263 {
264         return tny_camel_mime_part_new ();
265 }
266
267
268 static TnyHeader*
269 modest_tny_platform_factory_new_header (TnyPlatformFactory *self)
270 {
271         return tny_camel_header_new ();
272 }
273
274
275 ModestAccountMgr *
276 modest_tny_platform_factory_get_account_mgr_instance (ModestTnyPlatformFactory *fact)
277 {
278         ModestTnyPlatformFactoryPrivate *priv;
279
280         g_return_val_if_fail (fact, NULL);
281         
282         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact);
283
284         if (!priv->account_mgr) {
285                 if (!priv->conf)
286                         modest_tny_platform_factory_get_conf_instance (fact);
287                 priv->account_mgr = modest_account_mgr_new (priv->conf);
288         }
289
290         return priv->account_mgr;
291 }
292
293 ModestConf *
294 modest_tny_platform_factory_get_conf_instance (ModestTnyPlatformFactory *fact)
295 {
296         ModestTnyPlatformFactoryPrivate *priv;
297         
298         g_return_val_if_fail (fact, NULL);
299         
300         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact);
301
302         if (!priv->conf)
303                 priv->conf = modest_conf_new ();
304
305         return priv->conf;
306 }
307
308 ModestMailOperationQueue*   
309 modest_tny_platform_factory_get_mail_operation_queue_instance (ModestTnyPlatformFactory *fact)
310 {
311         ModestTnyPlatformFactoryPrivate *priv;
312
313         g_return_val_if_fail (fact, NULL);
314         
315         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact);
316
317         if (!priv->mail_op_queue)
318                 priv->mail_op_queue = modest_mail_operation_queue_new ();
319
320         return priv->mail_op_queue;
321 }
322
323
324
325 ModestCacheMgr*
326 modest_tny_platform_factory_get_cache_mgr_instance (ModestTnyPlatformFactory *fact)
327 {
328         ModestTnyPlatformFactoryPrivate *priv;
329
330         g_return_val_if_fail (fact, NULL);
331         
332         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact);
333
334         if (!priv->cache_mgr)
335                 priv->cache_mgr = modest_cache_mgr_new ();
336                 
337         return priv->cache_mgr;
338 }