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