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