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;
45 #define MODEST_SINGLETONS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
46 MODEST_TYPE_SINGLETONS, \
47 ModestSingletonsPrivate))
49 static GObjectClass *parent_class = NULL;
52 modest_singletons_get_type (void)
54 static GType my_type = 0;
56 static const GTypeInfo my_info = {
57 sizeof(ModestSingletonsClass),
59 NULL, /* base finalize */
60 (GClassInitFunc) modest_singletons_class_init,
61 NULL, /* class finalize */
62 NULL, /* class data */
63 sizeof(ModestSingletons),
65 (GInstanceInitFunc) modest_singletons_init,
68 my_type = g_type_register_static (G_TYPE_OBJECT,
76 modest_singletons_class_init (ModestSingletonsClass *klass)
78 GObjectClass *gobject_class;
79 gobject_class = (GObjectClass*) klass;
81 parent_class = g_type_class_peek_parent (klass);
82 gobject_class->finalize = modest_singletons_finalize;
84 g_type_class_add_private (gobject_class, sizeof(ModestSingletonsPrivate));
88 modest_singletons_init (ModestSingletons *obj)
90 ModestSingletonsPrivate *priv;
91 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
94 priv->account_mgr = NULL;
95 priv->account_store = NULL;
96 priv->cache_mgr = NULL;
97 priv->mail_op_queue = NULL;
99 priv->conf = modest_conf_new ();
101 g_printerr ("modest: cannot create modest conf instance\n");
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");
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");
117 priv->cache_mgr = modest_cache_mgr_new ();
118 if (!priv->cache_mgr) {
119 g_printerr ("modest: cannot create modest cache mgr instance\n");
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");
132 check_object_is_dead (GObject *obj, gchar *name)
134 if (G_IS_OBJECT(obj))
135 g_printerr ("modest: %s is still alive\n", name);
139 modest_singletons_finalize (GObject *obj)
141 ModestSingletonsPrivate *priv;
142 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
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;
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;
159 g_object_unref (G_OBJECT(priv->conf));
160 check_object_is_dead ((GObject*)priv->conf,
165 if (priv->cache_mgr) {
166 g_object_unref (G_OBJECT(priv->cache_mgr));
167 check_object_is_dead ((GObject*)priv->cache_mgr,
169 priv->cache_mgr = NULL;
172 G_OBJECT_CLASS(parent_class)->finalize (obj);
176 modest_singletons_new (void)
178 ModestSingletonsPrivate *priv;
179 ModestSingletons *self;
181 self = MODEST_SINGLETONS(g_object_new(MODEST_TYPE_SINGLETONS, NULL));
182 priv = MODEST_SINGLETONS_GET_PRIVATE(self);
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));
197 modest_singletons_get_conf (ModestSingletons *self)
199 g_return_val_if_fail (self, NULL);
200 return MODEST_SINGLETONS_GET_PRIVATE(self)->conf;
204 modest_singletons_get_account_mgr (ModestSingletons *self)
206 g_return_val_if_fail (self, NULL);
207 return MODEST_SINGLETONS_GET_PRIVATE(self)->account_mgr;
210 ModestTnyAccountStore*
211 modest_singletons_get_account_store (ModestSingletons *self)
213 g_return_val_if_fail (self, NULL);
214 return MODEST_SINGLETONS_GET_PRIVATE(self)->account_store;
218 modest_singletons_get_cache_mgr (ModestSingletons *self)
220 g_return_val_if_fail (self, NULL);
221 return MODEST_SINGLETONS_GET_PRIVATE(self)->cache_mgr;
224 ModestMailOperationQueue*
225 modest_singletons_get_mail_operation_queue (ModestSingletons *self)
227 g_return_val_if_fail (self, NULL);
228 return MODEST_SINGLETONS_GET_PRIVATE(self)->mail_op_queue;