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
137 modest_singletons_finalize (GObject *obj)
139 ModestSingletonsPrivate *priv;
140 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
143 g_object_unref (G_OBJECT(priv->conf));
147 if (priv->account_mgr) {
148 g_object_unref (G_OBJECT(priv->account_mgr));
149 priv->account_mgr = NULL;
151 if (priv->account_store) {
152 g_object_unref (G_OBJECT(priv->account_store));
153 priv->account_store = NULL;
156 if (priv->cache_mgr) {
157 g_object_unref (G_OBJECT(priv->cache_mgr));
158 priv->cache_mgr = NULL;
161 if (priv->widget_factory) {
162 g_object_unref (G_OBJECT(priv->widget_factory));
163 priv->widget_factory = NULL;
166 G_OBJECT_CLASS(parent_class)->finalize (obj);
170 modest_singletons_new (void)
172 ModestSingletonsPrivate *priv;
173 ModestSingletons *self;
175 self = MODEST_SINGLETONS(g_object_new(MODEST_TYPE_SINGLETONS, NULL));
176 priv = MODEST_SINGLETONS_GET_PRIVATE(self);
178 /* widget_factory will still be NULL, as it is initialized lazily */
179 if (!(priv->conf && priv->account_mgr && priv->account_store &&
180 priv->cache_mgr && priv->mail_op_queue)) {
181 g_printerr ("modest: failed to create singletons instance\n");
182 g_object_unref (G_OBJECT(self));
191 modest_singletons_get_conf (ModestSingletons *self)
193 g_return_val_if_fail (self, NULL);
194 return MODEST_SINGLETONS_GET_PRIVATE(self)->conf;
198 modest_singletons_get_account_mgr (ModestSingletons *self)
200 g_return_val_if_fail (self, NULL);
201 return MODEST_SINGLETONS_GET_PRIVATE(self)->account_mgr;
204 ModestTnyAccountStore*
205 modest_singletons_get_account_store (ModestSingletons *self)
207 g_return_val_if_fail (self, NULL);
208 return MODEST_SINGLETONS_GET_PRIVATE(self)->account_store;
212 modest_singletons_get_cache_mgr (ModestSingletons *self)
214 g_return_val_if_fail (self, NULL);
215 return MODEST_SINGLETONS_GET_PRIVATE(self)->cache_mgr;
219 ModestMailOperationQueue*
220 modest_singletons_get_mail_operation_queue (ModestSingletons *self)
222 g_return_val_if_fail (self, NULL);
223 return MODEST_SINGLETONS_GET_PRIVATE(self)->mail_op_queue;
228 modest_singletons_get_widget_factory (ModestSingletons *self)
230 ModestSingletonsPrivate *priv;
232 g_return_val_if_fail (self, NULL);
234 priv = MODEST_SINGLETONS_GET_PRIVATE(self);
236 if (G_UNLIKELY(!priv->widget_factory))
237 priv->widget_factory = modest_widget_factory_new (priv->account_store);
238 if (G_UNLIKELY(!priv->widget_factory)) {
239 g_printerr ("modest: cannot create modest widget factory instance\n");
242 return priv->widget_factory;