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