* Fixes NB#85060, show the password dialog whenever the password is not defined...
[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
34 /* 'private'/'protected' functions */
35 static void modest_singletons_class_init (ModestSingletonsClass *klass);
36 static void modest_singletons_init       (ModestSingletons *obj);
37 static void modest_singletons_finalize   (GObject *obj);
38
39 typedef struct _ModestSingletonsPrivate ModestSingletonsPrivate;
40 struct _ModestSingletonsPrivate {
41         ModestConf                *conf;
42         ModestAccountMgr          *account_mgr;
43         ModestEmailClipboard      *email_clipboard;
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->cache_mgr       = NULL;
102         priv->mail_op_queue   = NULL;
103         priv->platform_fact   = NULL;
104         priv->device          = NULL;
105         priv->window_mgr      = NULL;
106         
107         priv->conf           = modest_conf_new ();
108         if (!priv->conf) {
109                 g_printerr ("modest: cannot create modest conf instance\n");
110                 return;
111         }
112
113         priv->account_mgr    = modest_account_mgr_new (priv->conf);
114         if (!priv->account_mgr) {
115                 g_printerr ("modest: cannot create modest account mgr instance\n");
116                 return;
117         }
118
119         priv->email_clipboard    = modest_email_clipboard_new ();
120         if (!priv->email_clipboard) {
121                 g_printerr ("modest: cannot create modest email clipboard instance\n");
122                 return;
123         }
124
125         priv->platform_fact  = modest_tny_platform_factory_get_instance ();
126         if (!priv->platform_fact) {
127                 g_printerr ("modest: cannot create platform factory instance\n");
128                 return;
129         }
130
131         priv->device  = tny_platform_factory_new_device (priv->platform_fact);
132         if (!priv->device) {
133                 g_printerr ("modest: cannot create tny device instance\n");
134                 return;
135         }
136         
137         priv->cache_mgr     = modest_cache_mgr_new ();
138         if (!priv->cache_mgr) {
139                 g_printerr ("modest: cannot create modest cache mgr instance\n");
140                 return;
141         }
142
143         priv->mail_op_queue  = modest_mail_operation_queue_new ();
144         if (!priv->mail_op_queue) {
145                 g_printerr ("modest: cannot create modest mail operation queue instance\n");
146                 return;
147         }
148
149         priv->window_mgr = modest_window_mgr_new ();
150         if (!priv->window_mgr) {
151                 g_printerr ("modest: cannot create modest window manager instance\n");
152                 return;
153         }
154 }
155
156 static void
157 modest_singletons_finalize (GObject *obj)
158 {
159         ModestSingletonsPrivate *priv;
160                 
161         priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
162         
163         if (priv->window_mgr) {
164                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->window_mgr,"");
165                 g_object_unref (G_OBJECT(priv->window_mgr));
166                 priv->window_mgr = NULL;
167         }
168         
169         if (priv->mail_op_queue) {
170                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->mail_op_queue,"");
171                 g_object_unref (G_OBJECT(priv->mail_op_queue));
172                 priv->mail_op_queue = NULL;
173         }
174
175         if (priv->cache_mgr) {
176                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->cache_mgr,"");
177                 g_object_unref (G_OBJECT(priv->cache_mgr));
178                 priv->cache_mgr = NULL;
179         }
180
181         if (priv->device) {
182                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->device,"");
183                 g_object_unref (G_OBJECT(priv->device));
184                 priv->device = NULL;
185         }
186
187         if (priv->platform_fact) {
188                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->platform_fact,"");
189                 g_object_unref (G_OBJECT(priv->platform_fact));
190                 priv->platform_fact = NULL;
191         }
192         
193         if (priv->email_clipboard) {
194                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->email_clipboard,"");
195                 g_object_unref (G_OBJECT(priv->email_clipboard));
196                 priv->email_clipboard = NULL;
197         }
198
199         /* It is important that the account manager is uninitialized after
200          * the mail op queue is uninitialized because the mail op queue
201          * cancells any mail operations which in turn access the account
202          * manager (see modest_mail_operation_notify_end()). */
203         if (priv->account_mgr) {
204                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->account_mgr,"");
205                 g_object_unref (G_OBJECT(priv->account_mgr));
206                 priv->account_mgr = NULL;
207         }
208         
209         if (priv->conf) {
210                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->conf,"");
211                 g_object_unref (G_OBJECT(priv->conf));
212                 priv->conf = NULL;
213         }
214         
215         G_OBJECT_CLASS(parent_class)->finalize (obj);
216 }
217
218 ModestSingletons*
219 modest_singletons_new (void)
220 {
221         ModestSingletonsPrivate *priv;
222         ModestSingletons *self;
223         static gboolean invoked = FALSE;
224
225         if (invoked) {
226                 g_printerr ("%s: modest: modest_singletons_new may only be called once, aborting...\n",
227                             __FUNCTION__);
228                 abort();
229                 return NULL;
230         }
231         
232         self = MODEST_SINGLETONS(g_object_new(MODEST_TYPE_SINGLETONS, NULL));
233         priv = MODEST_SINGLETONS_GET_PRIVATE(self);
234         
235         /* widget_factory will still be NULL, as it is initialized lazily */
236         if (!(priv->conf && priv->account_mgr && priv->email_clipboard && 
237               priv->cache_mgr && priv->mail_op_queue && priv->device && priv->platform_fact)) {
238                 g_printerr ("modest: failed to create singletons object\n");
239                 g_object_unref (G_OBJECT(self));
240                 self = NULL;
241         }
242
243         invoked = TRUE;
244         return self;
245 }
246
247
248 ModestConf*
249 modest_singletons_get_conf (ModestSingletons *self)
250 {
251         g_return_val_if_fail (self, NULL);
252         return MODEST_SINGLETONS_GET_PRIVATE(self)->conf;
253 }
254
255 ModestAccountMgr*
256 modest_singletons_get_account_mgr (ModestSingletons *self)
257 {
258         g_return_val_if_fail (self, NULL);
259         return MODEST_SINGLETONS_GET_PRIVATE(self)->account_mgr;
260 }
261
262 ModestEmailClipboard*
263 modest_singletons_get_email_clipboard (ModestSingletons *self)
264 {
265         g_return_val_if_fail (self, NULL);
266         return MODEST_SINGLETONS_GET_PRIVATE(self)->email_clipboard;
267 }
268
269 ModestCacheMgr*
270 modest_singletons_get_cache_mgr (ModestSingletons *self)
271 {
272         g_return_val_if_fail (self, NULL);
273         return MODEST_SINGLETONS_GET_PRIVATE(self)->cache_mgr;
274 }
275
276 ModestMailOperationQueue*
277 modest_singletons_get_mail_operation_queue (ModestSingletons *self)
278 {
279         g_return_val_if_fail (self, NULL);
280         return MODEST_SINGLETONS_GET_PRIVATE(self)->mail_op_queue;
281 }
282
283 TnyDevice*
284 modest_singletons_get_device (ModestSingletons *self)
285 {
286         g_return_val_if_fail (self, NULL);
287         return MODEST_SINGLETONS_GET_PRIVATE(self)->device;
288 }
289
290
291 TnyPlatformFactory*
292 modest_singletons_get_platform_factory (ModestSingletons *self)
293 {
294         g_return_val_if_fail (self, NULL);
295         return MODEST_SINGLETONS_GET_PRIVATE(self)->platform_fact;
296 }
297
298 ModestWindowMgr* 
299 modest_singletons_get_window_mgr (ModestSingletons *self)
300 {
301         g_return_val_if_fail (self, NULL);
302         return MODEST_SINGLETONS_GET_PRIVATE(self)->window_mgr;
303 }