f968dfb77875512cf85973920bdb5b6095f817d8
[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 };
77
78 #define MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
79                                                          MODEST_TYPE_TNY_PLATFORM_FACTORY, \
80                                                          ModestTnyPlatformFactoryPrivate))
81 /* globals */
82 static GObjectClass *parent_class = NULL;
83 static ModestTnyPlatformFactory *singleton = NULL;
84
85 /* uncomment the following if you have defined any signals */
86 /* static guint signals[LAST_SIGNAL] = {0}; */
87
88 GType
89 modest_tny_platform_factory_get_type (void)
90 {
91         static GType my_type = 0;
92         if (!my_type) {
93                 static const GTypeInfo my_info = {
94                         sizeof(ModestTnyPlatformFactoryClass),
95                         NULL,           /* base init */
96                         NULL,           /* base finalize */
97                         (GClassInitFunc) modest_tny_platform_factory_class_init,
98                         NULL,           /* class finalize */
99                         NULL,           /* class data */
100                         sizeof(ModestTnyPlatformFactory),
101                         1,              /* n_preallocs */
102                         (GInstanceInitFunc) modest_tny_platform_factory_init,
103                         NULL
104                 };
105
106                 static const GInterfaceInfo tny_platform_factory_info = {
107                         (GInterfaceInitFunc) tny_platform_factory_init, /* interface_init */
108                         NULL,         /* interface_finalize */
109                         NULL          /* interface_data */
110                 };
111
112                 my_type = g_type_register_static (G_TYPE_OBJECT,
113                                                   "ModestTnyPlatformFactory",
114                                                   &my_info, 0);
115
116                 g_type_add_interface_static (my_type, TNY_TYPE_PLATFORM_FACTORY, 
117                                              &tny_platform_factory_info);
118         }
119         return my_type;
120 }
121
122 static void
123 modest_tny_platform_factory_class_init (ModestTnyPlatformFactoryClass *klass)
124 {
125         GObjectClass *gobject_class;
126
127         gobject_class = (GObjectClass*) klass;
128         parent_class            = g_type_class_peek_parent (klass);
129
130         gobject_class->finalize = modest_tny_platform_factory_finalize;
131         gobject_class->constructor = modest_tny_platform_factory_constructor;
132
133         g_type_class_add_private (gobject_class, sizeof(ModestTnyPlatformFactoryPrivate));
134 }
135
136 static void
137 modest_tny_platform_factory_init (ModestTnyPlatformFactory *obj)
138 {
139         ModestTnyPlatformFactoryPrivate *priv;
140         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(obj);
141
142         priv->account_mgr   = NULL;
143         priv->conf          = NULL;
144         priv->account_store = NULL;
145         priv->mail_op_queue = NULL;
146 }
147
148 static GObject*
149 modest_tny_platform_factory_constructor (GType type, guint n_construct_params,
150                                          GObjectConstructParam *construct_params)
151 {
152         GObject *object;
153
154         if (!singleton) {
155                 object = G_OBJECT_CLASS (parent_class)->constructor (type,
156                                 n_construct_params, construct_params);
157
158                 singleton = MODEST_TNY_PLATFORM_FACTORY (object);
159         } else {
160                 object = G_OBJECT (singleton);
161                 g_object_freeze_notify (G_OBJECT (singleton));
162         }
163
164         return object;
165 }
166
167 static void
168 modest_tny_platform_factory_finalize (GObject *obj)
169 {
170         ModestTnyPlatformFactoryPrivate *priv;
171
172         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(obj);
173
174         if (priv->account_mgr)
175                 g_object_unref (priv->account_mgr);
176
177         if (priv->conf)
178                 g_object_unref (priv->conf);
179
180         if (priv->account_store)
181                 g_object_unref (priv->account_store);
182
183         if (priv->mail_op_queue)
184                 g_object_unref (priv->mail_op_queue);
185         
186         G_OBJECT_CLASS(parent_class)->finalize (obj);
187 }
188
189 static void
190 tny_platform_factory_init (gpointer g, gpointer iface_data)
191 {
192         TnyPlatformFactoryIface *klass = (TnyPlatformFactoryIface *)g;
193
194         klass->new_account_store_func = modest_tny_platform_factory_new_account_store;
195         klass->new_device_func        = modest_tny_platform_factory_new_device;
196         klass->new_msg_view_func      = modest_tny_platform_factory_new_msg_view;
197         klass->new_msg_func           = modest_tny_platform_factory_new_msg;
198         klass->new_mime_part_func     = modest_tny_platform_factory_new_mime_part;
199         klass->new_header_func        = modest_tny_platform_factory_new_header;
200
201         return;
202 }
203
204 TnyPlatformFactory *
205 modest_tny_platform_factory_get_instance (void)
206 {
207         ModestTnyPlatformFactory *self = g_object_new (MODEST_TYPE_TNY_PLATFORM_FACTORY, NULL);
208
209         return TNY_PLATFORM_FACTORY (self);
210 }
211
212 static TnyAccountStore *
213 modest_tny_platform_factory_new_account_store (TnyPlatformFactory *self)
214 {
215         ModestTnyPlatformFactoryPrivate *priv;
216
217         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(self);
218
219         if (!priv->account_store) {
220                 if (!priv->account_mgr)
221                         modest_tny_platform_factory_get_modest_account_mgr_instance (self);
222
223                 priv->account_store = modest_tny_account_store_new (priv->account_mgr);
224         }
225
226         return TNY_ACCOUNT_STORE (priv->account_store);
227 }
228
229 static TnyDevice *
230 modest_tny_platform_factory_new_device (TnyPlatformFactory *self)
231 {
232 /* MODES_PLATFORM_ID: 1 ==> gtk, 2==> maemo */
233 #if MODEST_PLATFORM_ID==1   
234         return TNY_DEVICE (tny_gnome_device_new ());
235 #elif MODEST_PLATFORM_ID==2
236         return TNY_DEVICE (tny_maemo_device_new ());
237 #else
238         return NULL;
239 #endif /* MODEST_PLATFORM */
240 }
241
242 static TnyMsgView*
243 modest_tny_platform_factory_new_msg_view (TnyPlatformFactory *self)
244 {
245         /* TODO */
246         return NULL;
247 }
248
249 static TnyMsg*
250 modest_tny_platform_factory_new_msg (TnyPlatformFactory *self)
251 {
252         return tny_camel_msg_new ();
253 }
254
255
256 static TnyMimePart*
257 modest_tny_platform_factory_new_mime_part (TnyPlatformFactory *self)
258 {
259         return tny_camel_mime_part_new ();
260 }
261
262
263 static TnyHeader*
264 modest_tny_platform_factory_new_header (TnyPlatformFactory *self)
265 {
266         return tny_camel_header_new ();
267 }
268
269
270 ModestAccountMgr *
271 modest_tny_platform_factory_get_modest_account_mgr_instance (TnyPlatformFactory *fact)
272 {
273         ModestTnyPlatformFactoryPrivate *priv;
274
275         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact);
276
277         if (!priv->account_mgr) {
278                 if (!priv->conf)
279                         modest_tny_platform_factory_get_modest_conf_instance (fact);
280
281                 priv->account_mgr = modest_account_mgr_new (priv->conf);
282         }
283
284         return priv->account_mgr;
285 }
286
287 ModestConf *
288 modest_tny_platform_factory_get_modest_conf_instance (TnyPlatformFactory *fact)
289 {
290         ModestTnyPlatformFactoryPrivate *priv;
291
292         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact);
293
294         if (!priv->conf)
295                 priv->conf = modest_conf_new ();
296
297         return priv->conf;
298 }
299
300 ModestMailOperationQueue*   
301 modest_tny_platform_factory_get_modest_mail_operation_queue_instance (TnyPlatformFactory *fact)
302 {
303         ModestTnyPlatformFactoryPrivate *priv;
304
305         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact);
306
307         if (!priv->mail_op_queue)
308                 priv->mail_op_queue = modest_mail_operation_queue_new ();
309
310         return priv->mail_op_queue;
311 }