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"
31 #include "modest-runtime.h"
32 #include "modest-debug.h"
33 #include <tny-fs-stream-cache.h>
35 /* 'private'/'protected' functions */
36 static void modest_singletons_class_init (ModestSingletonsClass *klass);
37 static void modest_singletons_init (ModestSingletons *obj);
38 static void modest_singletons_finalize (GObject *obj);
40 typedef struct _ModestSingletonsPrivate ModestSingletonsPrivate;
41 struct _ModestSingletonsPrivate {
43 ModestAccountMgr *account_mgr;
44 ModestEmailClipboard *email_clipboard;
45 ModestCacheMgr *cache_mgr;
46 ModestMailOperationQueue *mail_op_queue;
47 TnyPlatformFactory *platform_fact;
49 ModestWindowMgr *window_mgr;
50 TnyStreamCache *images_cache;
52 #define MODEST_SINGLETONS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
53 MODEST_TYPE_SINGLETONS, \
54 ModestSingletonsPrivate))
56 static GObjectClass *parent_class = NULL;
59 modest_singletons_get_type (void)
61 static GType my_type = 0;
63 static const GTypeInfo my_info = {
64 sizeof(ModestSingletonsClass),
66 NULL, /* base finalize */
67 (GClassInitFunc) modest_singletons_class_init,
68 NULL, /* class finalize */
69 NULL, /* class data */
70 sizeof(ModestSingletons),
72 (GInstanceInitFunc) modest_singletons_init,
75 my_type = g_type_register_static (G_TYPE_OBJECT,
83 modest_singletons_class_init (ModestSingletonsClass *klass)
85 GObjectClass *gobject_class;
86 gobject_class = (GObjectClass*) klass;
88 parent_class = g_type_class_peek_parent (klass);
89 gobject_class->finalize = modest_singletons_finalize;
91 g_type_class_add_private (gobject_class, sizeof(ModestSingletonsPrivate));
95 modest_singletons_init (ModestSingletons *obj)
97 ModestSingletonsPrivate *priv;
98 gchar *images_cache_path;
99 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
102 priv->account_mgr = NULL;
103 priv->email_clipboard = NULL;
104 priv->cache_mgr = NULL;
105 priv->mail_op_queue = NULL;
106 priv->platform_fact = NULL;
108 priv->window_mgr = NULL;
109 priv->images_cache = NULL;
111 priv->conf = modest_conf_new ();
113 g_printerr ("modest: cannot create modest conf instance\n");
117 priv->account_mgr = modest_account_mgr_new (priv->conf);
118 if (!priv->account_mgr) {
119 g_printerr ("modest: cannot create modest account mgr instance\n");
123 priv->email_clipboard = modest_email_clipboard_new ();
124 if (!priv->email_clipboard) {
125 g_printerr ("modest: cannot create modest email clipboard instance\n");
129 priv->platform_fact = modest_tny_platform_factory_get_instance ();
130 if (!priv->platform_fact) {
131 g_printerr ("modest: cannot create platform factory instance\n");
135 priv->device = tny_platform_factory_new_device (priv->platform_fact);
137 g_printerr ("modest: cannot create tny device instance\n");
141 priv->cache_mgr = modest_cache_mgr_new ();
142 if (!priv->cache_mgr) {
143 g_printerr ("modest: cannot create modest cache mgr instance\n");
147 priv->mail_op_queue = modest_mail_operation_queue_new ();
148 if (!priv->mail_op_queue) {
149 g_printerr ("modest: cannot create modest mail operation queue instance\n");
153 priv->window_mgr = modest_window_mgr_new ();
154 if (!priv->window_mgr) {
155 g_printerr ("modest: cannot create modest window manager instance\n");
159 images_cache_path = g_build_filename (g_get_home_dir (), MODEST_DIR, MODEST_IMAGES_CACHE_DIR, NULL);
160 priv->images_cache = tny_fs_stream_cache_new (images_cache_path, MODEST_IMAGES_CACHE_SIZE);
161 g_free (images_cache_path);
162 if (!priv->images_cache) {
163 g_printerr ("modest: cannot create images cache instance\n");
169 modest_singletons_finalize (GObject *obj)
171 ModestSingletonsPrivate *priv;
173 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
175 if (priv->images_cache) {
176 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF (priv->images_cache, "");
177 g_object_unref (G_OBJECT (priv->images_cache));
178 priv->images_cache = NULL;
181 if (priv->window_mgr) {
182 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->window_mgr,"");
183 g_object_unref (G_OBJECT(priv->window_mgr));
184 priv->window_mgr = NULL;
187 if (priv->mail_op_queue) {
188 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->mail_op_queue,"");
189 g_object_unref (G_OBJECT(priv->mail_op_queue));
190 priv->mail_op_queue = NULL;
193 if (priv->cache_mgr) {
194 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->cache_mgr,"");
195 g_object_unref (G_OBJECT(priv->cache_mgr));
196 priv->cache_mgr = NULL;
200 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->device,"");
201 g_object_unref (G_OBJECT(priv->device));
205 if (priv->platform_fact) {
206 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->platform_fact,"");
207 g_object_unref (G_OBJECT(priv->platform_fact));
208 priv->platform_fact = NULL;
211 if (priv->email_clipboard) {
212 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->email_clipboard,"");
213 g_object_unref (G_OBJECT(priv->email_clipboard));
214 priv->email_clipboard = NULL;
217 /* It is important that the account manager is uninitialized after
218 * the mail op queue is uninitialized because the mail op queue
219 * cancells any mail operations which in turn access the account
220 * manager (see modest_mail_operation_notify_end()). */
221 if (priv->account_mgr) {
222 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->account_mgr,"");
223 g_object_unref (G_OBJECT(priv->account_mgr));
224 priv->account_mgr = NULL;
228 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->conf,"");
229 g_object_unref (G_OBJECT(priv->conf));
233 G_OBJECT_CLASS(parent_class)->finalize (obj);
237 modest_singletons_new (void)
239 ModestSingletonsPrivate *priv;
240 ModestSingletons *self;
241 static gboolean invoked = FALSE;
244 g_printerr ("%s: modest: modest_singletons_new may only be called once, aborting...\n",
250 self = MODEST_SINGLETONS(g_object_new(MODEST_TYPE_SINGLETONS, NULL));
251 priv = MODEST_SINGLETONS_GET_PRIVATE(self);
253 /* widget_factory will still be NULL, as it is initialized lazily */
254 if (!(priv->conf && priv->account_mgr && priv->email_clipboard &&
255 priv->cache_mgr && priv->mail_op_queue && priv->device && priv->platform_fact)) {
256 g_printerr ("modest: failed to create singletons object\n");
257 g_object_unref (G_OBJECT(self));
267 modest_singletons_get_conf (ModestSingletons *self)
269 g_return_val_if_fail (self, NULL);
270 return MODEST_SINGLETONS_GET_PRIVATE(self)->conf;
274 modest_singletons_get_account_mgr (ModestSingletons *self)
276 g_return_val_if_fail (self, NULL);
277 return MODEST_SINGLETONS_GET_PRIVATE(self)->account_mgr;
280 ModestEmailClipboard*
281 modest_singletons_get_email_clipboard (ModestSingletons *self)
283 g_return_val_if_fail (self, NULL);
284 return MODEST_SINGLETONS_GET_PRIVATE(self)->email_clipboard;
288 modest_singletons_get_cache_mgr (ModestSingletons *self)
290 g_return_val_if_fail (self, NULL);
291 return MODEST_SINGLETONS_GET_PRIVATE(self)->cache_mgr;
294 ModestMailOperationQueue*
295 modest_singletons_get_mail_operation_queue (ModestSingletons *self)
297 g_return_val_if_fail (self, NULL);
298 return MODEST_SINGLETONS_GET_PRIVATE(self)->mail_op_queue;
302 modest_singletons_get_device (ModestSingletons *self)
304 g_return_val_if_fail (self, NULL);
305 return MODEST_SINGLETONS_GET_PRIVATE(self)->device;
310 modest_singletons_get_platform_factory (ModestSingletons *self)
312 g_return_val_if_fail (self, NULL);
313 return MODEST_SINGLETONS_GET_PRIVATE(self)->platform_fact;
317 modest_singletons_get_window_mgr (ModestSingletons *self)
319 g_return_val_if_fail (self, NULL);
320 return MODEST_SINGLETONS_GET_PRIVATE(self)->window_mgr;
324 modest_singletons_get_images_cache (ModestSingletons *self)
326 g_return_val_if_fail (self, NULL);
327 return MODEST_SINGLETONS_GET_PRIVATE(self)->images_cache;