Merged the new plugin system from branch plugin-system.
[modest] / src / modest-singletons.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 #include "modest-singletons.h"
31 #include "modest-runtime.h"
32 #include "modest-defs.h"
33 #include "modest-debug.h"
34 #include <tny-fs-stream-cache.h>
35
36 /* 'private'/'protected' functions */
37 static void modest_singletons_class_init (ModestSingletonsClass *klass);
38 static void modest_singletons_init       (ModestSingletons *obj);
39 static void modest_singletons_finalize   (GObject *obj);
40
41 typedef struct _ModestSingletonsPrivate ModestSingletonsPrivate;
42 struct _ModestSingletonsPrivate {
43         ModestConf                *conf;
44         ModestAccountMgr          *account_mgr;
45         ModestEmailClipboard      *email_clipboard;
46         ModestCacheMgr            *cache_mgr;   
47         ModestMailOperationQueue  *mail_op_queue;
48         TnyPlatformFactory        *platform_fact;
49         TnyDevice                 *device;
50         ModestWindowMgr           *window_mgr;
51 <<<<<<< .working
52         TnyStreamCache            *images_cache;
53 =======
54         ModestProtocolRegistry    *protocol_registry;
55         ModestPluginFactory   *plugin_factory;
56         TnyStreamCache            *images_cache;
57 >>>>>>> .merge-right.r5668
58 };
59 #define MODEST_SINGLETONS_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
60                                                MODEST_TYPE_SINGLETONS, \
61                                                ModestSingletonsPrivate))
62 /* globals */
63 static GObjectClass *parent_class = NULL;
64
65 GType
66 modest_singletons_get_type (void)
67 {
68         static GType my_type = 0;
69         if (!my_type) {
70                 static const GTypeInfo my_info = {
71                         sizeof(ModestSingletonsClass),
72                         NULL,           /* base init */
73                         NULL,           /* base finalize */
74                         (GClassInitFunc) modest_singletons_class_init,
75                         NULL,           /* class finalize */
76                         NULL,           /* class data */
77                         sizeof(ModestSingletons),
78                         1,              /* n_preallocs */
79                         (GInstanceInitFunc) modest_singletons_init,
80                         NULL
81                 };
82                 my_type = g_type_register_static (G_TYPE_OBJECT,
83                                                   "ModestSingletons",
84                                                   &my_info, 0);
85         }
86         return my_type;
87 }
88
89 static void
90 modest_singletons_class_init (ModestSingletonsClass *klass)
91 {
92         GObjectClass *gobject_class;
93         gobject_class = (GObjectClass*) klass;
94
95         parent_class            = g_type_class_peek_parent (klass);
96         gobject_class->finalize = modest_singletons_finalize;
97
98         g_type_class_add_private (gobject_class, sizeof(ModestSingletonsPrivate));
99 }
100
101 static void
102 modest_singletons_init (ModestSingletons *obj)
103 {
104         ModestSingletonsPrivate *priv;
105         gchar *images_cache_path;
106         priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
107
108         priv->conf            = NULL;
109         priv->account_mgr     = NULL;
110         priv->email_clipboard = NULL;
111         priv->cache_mgr       = NULL;
112         priv->mail_op_queue   = NULL;
113         priv->platform_fact   = NULL;
114         priv->device          = NULL;
115         priv->window_mgr      = NULL;
116 <<<<<<< .working
117         priv->images_cache    = NULL;
118 =======
119         priv->protocol_registry = NULL;
120         priv->plugin_factory = NULL;
121
122         priv->protocol_registry = modest_protocol_registry_new ();
123         if (!priv->protocol_registry) {
124                 g_printerr ("modest:cannot create protocol registry instance\n");
125                 return;
126         }
127         modest_protocol_registry_set_to_default (priv->protocol_registry);
128         priv->images_cache    = NULL;
129 >>>>>>> .merge-right.r5668
130         
131         priv->conf           = modest_conf_new ();
132         if (!priv->conf) {
133                 g_printerr ("modest: cannot create modest conf instance\n");
134                 return;
135         }
136
137         priv->account_mgr    = modest_account_mgr_new (priv->conf);
138         if (!priv->account_mgr) {
139                 g_printerr ("modest: cannot create modest account mgr instance\n");
140                 return;
141         }
142
143         priv->email_clipboard    = modest_email_clipboard_new ();
144         if (!priv->email_clipboard) {
145                 g_printerr ("modest: cannot create modest email clipboard instance\n");
146                 return;
147         }
148
149         priv->platform_fact  = modest_tny_platform_factory_get_instance ();
150         if (!priv->platform_fact) {
151                 g_printerr ("modest: cannot create platform factory instance\n");
152                 return;
153         }
154
155         priv->device  = tny_platform_factory_new_device (priv->platform_fact);
156         if (!priv->device) {
157                 g_printerr ("modest: cannot create tny device instance\n");
158                 return;
159         }
160         
161         priv->cache_mgr     = modest_cache_mgr_new ();
162         if (!priv->cache_mgr) {
163                 g_printerr ("modest: cannot create modest cache mgr instance\n");
164                 return;
165         }
166
167         priv->mail_op_queue  = modest_mail_operation_queue_new ();
168         if (!priv->mail_op_queue) {
169                 g_printerr ("modest: cannot create modest mail operation queue instance\n");
170                 return;
171         }
172
173         priv->window_mgr = modest_window_mgr_new ();
174         if (!priv->window_mgr) {
175                 g_printerr ("modest: cannot create modest window manager instance\n");
176                 return;
177         }
178 <<<<<<< .working
179
180         images_cache_path = g_build_filename (g_get_home_dir (), MODEST_DIR, MODEST_IMAGES_CACHE_DIR, NULL);
181         priv->images_cache = tny_fs_stream_cache_new (images_cache_path, MODEST_IMAGES_CACHE_SIZE);
182         g_free (images_cache_path);
183         if (!priv->images_cache) {
184                 g_printerr ("modest: cannot create images cache instance\n");
185                 return;
186         }
187 =======
188
189         priv->plugin_factory = modest_plugin_factory_new ();
190         if (!priv->plugin_factory) {
191                 g_printerr ("modest: cannot create modest mail plugin factory instance\n");
192                 return;
193         }
194
195         images_cache_path = g_build_filename (g_get_home_dir (), MODEST_DIR, MODEST_IMAGES_CACHE_DIR, NULL);
196         priv->images_cache = tny_fs_stream_cache_new (images_cache_path, MODEST_IMAGES_CACHE_SIZE);
197         g_free (images_cache_path);
198         if (!priv->images_cache) {
199                 g_printerr ("modest: cannot create images cache instance\n");
200                 return;
201         }
202
203 >>>>>>> .merge-right.r5668
204 }
205
206 static void
207 modest_singletons_finalize (GObject *obj)
208 {
209         ModestSingletonsPrivate *priv;
210                 
211         priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
212
213         if (priv->images_cache) {
214                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF (priv->images_cache, "");
215                 g_object_unref (G_OBJECT (priv->images_cache));
216                 priv->images_cache = NULL;
217         }
218         
219         if (priv->window_mgr) {
220                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->window_mgr,"");
221                 g_object_unref (G_OBJECT(priv->window_mgr));
222                 priv->window_mgr = NULL;
223         }
224         
225         if (priv->mail_op_queue) {
226                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->mail_op_queue,"");
227                 g_object_unref (G_OBJECT(priv->mail_op_queue));
228                 priv->mail_op_queue = NULL;
229         }
230
231         if (priv->cache_mgr) {
232                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->cache_mgr,"");
233                 g_object_unref (G_OBJECT(priv->cache_mgr));
234                 priv->cache_mgr = NULL;
235         }
236
237         if (priv->device) {
238                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->device,"");
239                 g_object_unref (G_OBJECT(priv->device));
240                 priv->device = NULL;
241         }
242
243         if (priv->platform_fact) {
244                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->platform_fact,"");
245                 g_object_unref (G_OBJECT(priv->platform_fact));
246                 priv->platform_fact = NULL;
247         }
248         
249         if (priv->email_clipboard) {
250                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->email_clipboard,"");
251                 g_object_unref (G_OBJECT(priv->email_clipboard));
252                 priv->email_clipboard = NULL;
253         }
254
255         /* It is important that the account manager is uninitialized after
256          * the mail op queue is uninitialized because the mail op queue
257          * cancells any mail operations which in turn access the account
258          * manager (see modest_mail_operation_notify_end()). */
259         if (priv->account_mgr) {
260                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->account_mgr,"");
261                 g_object_unref (G_OBJECT(priv->account_mgr));
262                 priv->account_mgr = NULL;
263         }
264         
265         if (priv->conf) {
266                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->conf,"");
267                 g_object_unref (G_OBJECT(priv->conf));
268                 priv->conf = NULL;
269         }
270         
271         G_OBJECT_CLASS(parent_class)->finalize (obj);
272 }
273
274 ModestSingletons*
275 modest_singletons_new (void)
276 {
277         ModestSingletonsPrivate *priv;
278         ModestSingletons *self;
279         static gboolean invoked = FALSE;
280
281         if (invoked) {
282                 g_printerr ("%s: modest: modest_singletons_new may only be called once, aborting...\n",
283                             __FUNCTION__);
284                 abort();
285                 return NULL;
286         }
287         
288         self = MODEST_SINGLETONS(g_object_new(MODEST_TYPE_SINGLETONS, NULL));
289         priv = MODEST_SINGLETONS_GET_PRIVATE(self);
290         
291         /* widget_factory will still be NULL, as it is initialized lazily */
292         if (!(priv->conf && priv->account_mgr && priv->email_clipboard && 
293               priv->cache_mgr && priv->mail_op_queue && priv->device && 
294               priv->platform_fact && priv->plugin_factory)) {
295                 g_printerr ("modest: failed to create singletons object\n");
296                 g_object_unref (G_OBJECT(self));
297                 self = NULL;
298         }
299
300         invoked = TRUE;
301         return self;
302 }
303
304
305 ModestConf*
306 modest_singletons_get_conf (ModestSingletons *self)
307 {
308         g_return_val_if_fail (self, NULL);
309         return MODEST_SINGLETONS_GET_PRIVATE(self)->conf;
310 }
311
312 ModestAccountMgr*
313 modest_singletons_get_account_mgr (ModestSingletons *self)
314 {
315         g_return_val_if_fail (self, NULL);
316         return MODEST_SINGLETONS_GET_PRIVATE(self)->account_mgr;
317 }
318
319 ModestEmailClipboard*
320 modest_singletons_get_email_clipboard (ModestSingletons *self)
321 {
322         g_return_val_if_fail (self, NULL);
323         return MODEST_SINGLETONS_GET_PRIVATE(self)->email_clipboard;
324 }
325
326 ModestCacheMgr*
327 modest_singletons_get_cache_mgr (ModestSingletons *self)
328 {
329         g_return_val_if_fail (self, NULL);
330         return MODEST_SINGLETONS_GET_PRIVATE(self)->cache_mgr;
331 }
332
333 ModestMailOperationQueue*
334 modest_singletons_get_mail_operation_queue (ModestSingletons *self)
335 {
336         g_return_val_if_fail (self, NULL);
337         return MODEST_SINGLETONS_GET_PRIVATE(self)->mail_op_queue;
338 }
339
340 TnyDevice*
341 modest_singletons_get_device (ModestSingletons *self)
342 {
343         g_return_val_if_fail (self, NULL);
344         return MODEST_SINGLETONS_GET_PRIVATE(self)->device;
345 }
346
347
348 TnyPlatformFactory*
349 modest_singletons_get_platform_factory (ModestSingletons *self)
350 {
351         g_return_val_if_fail (self, NULL);
352         return MODEST_SINGLETONS_GET_PRIVATE(self)->platform_fact;
353 }
354
355 ModestWindowMgr* 
356 modest_singletons_get_window_mgr (ModestSingletons *self)
357 {
358         g_return_val_if_fail (self, NULL);
359         return MODEST_SINGLETONS_GET_PRIVATE(self)->window_mgr;
360 }
361 <<<<<<< .working
362
363 TnyStreamCache* 
364 modest_singletons_get_images_cache (ModestSingletons *self)
365 {
366         g_return_val_if_fail (self, NULL);
367         return MODEST_SINGLETONS_GET_PRIVATE(self)->images_cache;
368 }
369 =======
370
371 ModestProtocolRegistry* 
372 modest_singletons_get_protocol_registry (ModestSingletons *self)
373 {
374         g_return_val_if_fail (self, NULL);
375         return MODEST_SINGLETONS_GET_PRIVATE(self)->protocol_registry;
376 }
377
378 TnyStreamCache* 
379 modest_singletons_get_images_cache (ModestSingletons *self)
380 {
381         g_return_val_if_fail (self, NULL);
382         return MODEST_SINGLETONS_GET_PRIVATE(self)->images_cache;
383 }
384
385 ModestPluginFactory *
386 modest_singletons_get_plugin_factory (ModestSingletons *self)
387 {
388         g_return_val_if_fail (self, NULL);
389
390         return MODEST_SINGLETONS_GET_PRIVATE (self)->plugin_factory;
391 }
392 >>>>>>> .merge-right.r5668