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