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