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