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