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