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