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