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