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