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