1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include "modest-singletons.h"
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);
37 typedef struct _ModestSingletonsPrivate ModestSingletonsPrivate;
38 struct _ModestSingletonsPrivate {
40 ModestAccountMgr *account_mgr;
41 ModestTnyAccountStore *account_store;
42 ModestCacheMgr *cache_mgr;
43 ModestMailOperationQueue *mail_op_queue;
44 ModestWidgetFactory *widget_factory;
46 #define MODEST_SINGLETONS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
47 MODEST_TYPE_SINGLETONS, \
48 ModestSingletonsPrivate))
50 static GObjectClass *parent_class = NULL;
53 modest_singletons_get_type (void)
55 static GType my_type = 0;
57 static const GTypeInfo my_info = {
58 sizeof(ModestSingletonsClass),
60 NULL, /* base finalize */
61 (GClassInitFunc) modest_singletons_class_init,
62 NULL, /* class finalize */
63 NULL, /* class data */
64 sizeof(ModestSingletons),
66 (GInstanceInitFunc) modest_singletons_init,
69 my_type = g_type_register_static (G_TYPE_OBJECT,
77 modest_singletons_class_init (ModestSingletonsClass *klass)
79 GObjectClass *gobject_class;
80 gobject_class = (GObjectClass*) klass;
82 parent_class = g_type_class_peek_parent (klass);
83 gobject_class->finalize = modest_singletons_finalize;
85 g_type_class_add_private (gobject_class, sizeof(ModestSingletonsPrivate));
89 modest_singletons_init (ModestSingletons *obj)
91 ModestSingletonsPrivate *priv;
92 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
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;
101 priv->conf = modest_conf_new ();
103 g_printerr ("modest: cannot create modest conf instance\n");
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");
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");
119 priv->cache_mgr = modest_cache_mgr_new ();
120 if (!priv->cache_mgr) {
121 g_printerr ("modest: cannot create modest cache mgr instance\n");
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");
131 /* don't initialize widget_factory here, but do it lazily, so we can
132 * instaniatie modest-singletons before gtk_init
138 check_object_is_dead (GObject *obj, gchar *name)
140 if (G_IS_OBJECT(obj))
141 g_printerr ("modest: %s is still alive\n", name);
145 modest_singletons_finalize (GObject *obj)
147 ModestSingletonsPrivate *priv;
148 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
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;
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;
164 if (priv->account_mgr) {
165 g_object_unref (G_OBJECT(priv->account_mgr));
166 check_object_is_dead ((GObject*)priv->account_mgr,
167 "priv->account_mgr");
168 priv->account_mgr = NULL;
172 g_object_unref (G_OBJECT(priv->conf));
173 check_object_is_dead ((GObject*)priv->conf,
178 if (priv->cache_mgr) {
179 g_object_unref (G_OBJECT(priv->cache_mgr));
180 check_object_is_dead ((GObject*)priv->cache_mgr,
182 priv->cache_mgr = NULL;
185 G_OBJECT_CLASS(parent_class)->finalize (obj);
189 modest_singletons_new (void)
191 ModestSingletonsPrivate *priv;
192 ModestSingletons *self;
194 self = MODEST_SINGLETONS(g_object_new(MODEST_TYPE_SINGLETONS, NULL));
195 priv = MODEST_SINGLETONS_GET_PRIVATE(self);
197 /* widget_factory will still be NULL, as it is initialized lazily */
198 if (!(priv->conf && priv->account_mgr && priv->account_store &&
199 priv->cache_mgr && priv->mail_op_queue)) {
200 g_printerr ("modest: failed to create singletons instance\n");
201 g_object_unref (G_OBJECT(self));
210 modest_singletons_get_conf (ModestSingletons *self)
212 g_return_val_if_fail (self, NULL);
213 return MODEST_SINGLETONS_GET_PRIVATE(self)->conf;
217 modest_singletons_get_account_mgr (ModestSingletons *self)
219 g_return_val_if_fail (self, NULL);
220 return MODEST_SINGLETONS_GET_PRIVATE(self)->account_mgr;
223 ModestTnyAccountStore*
224 modest_singletons_get_account_store (ModestSingletons *self)
226 g_return_val_if_fail (self, NULL);
227 return MODEST_SINGLETONS_GET_PRIVATE(self)->account_store;
231 modest_singletons_get_cache_mgr (ModestSingletons *self)
233 g_return_val_if_fail (self, NULL);
234 return MODEST_SINGLETONS_GET_PRIVATE(self)->cache_mgr;
238 ModestMailOperationQueue*
239 modest_singletons_get_mail_operation_queue (ModestSingletons *self)
241 g_return_val_if_fail (self, NULL);
242 return MODEST_SINGLETONS_GET_PRIVATE(self)->mail_op_queue;
247 modest_singletons_get_widget_factory (ModestSingletons *self)
249 ModestSingletonsPrivate *priv;
251 g_return_val_if_fail (self, NULL);
253 priv = MODEST_SINGLETONS_GET_PRIVATE(self);
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");
261 return priv->widget_factory;