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-defs.h"
33 #include "modest-debug.h"
34 #include "widgets/modest-hildon1-window-mgr.h"
35 #include <tny-fs-stream-cache.h>
37 /* 'private'/'protected' functions */
38 static void modest_singletons_class_init (ModestSingletonsClass *klass);
39 static void modest_singletons_init (ModestSingletons *obj);
40 static void modest_singletons_finalize (GObject *obj);
42 typedef struct _ModestSingletonsPrivate ModestSingletonsPrivate;
43 struct _ModestSingletonsPrivate {
45 ModestAccountMgr *account_mgr;
46 ModestEmailClipboard *email_clipboard;
47 ModestCacheMgr *cache_mgr;
48 ModestMailOperationQueue *mail_op_queue;
49 TnyPlatformFactory *platform_fact;
51 ModestWindowMgr *window_mgr;
52 ModestProtocolRegistry *protocol_registry;
53 ModestPluginFactory *plugin_factory;
54 TnyStreamCache *images_cache;
56 #define MODEST_SINGLETONS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
57 MODEST_TYPE_SINGLETONS, \
58 ModestSingletonsPrivate))
60 static GObjectClass *parent_class = NULL;
63 modest_singletons_get_type (void)
65 static GType my_type = 0;
67 static const GTypeInfo my_info = {
68 sizeof(ModestSingletonsClass),
70 NULL, /* base finalize */
71 (GClassInitFunc) modest_singletons_class_init,
72 NULL, /* class finalize */
73 NULL, /* class data */
74 sizeof(ModestSingletons),
76 (GInstanceInitFunc) modest_singletons_init,
79 my_type = g_type_register_static (G_TYPE_OBJECT,
87 modest_singletons_class_init (ModestSingletonsClass *klass)
89 GObjectClass *gobject_class;
90 gobject_class = (GObjectClass*) klass;
92 parent_class = g_type_class_peek_parent (klass);
93 gobject_class->finalize = modest_singletons_finalize;
95 g_type_class_add_private (gobject_class, sizeof(ModestSingletonsPrivate));
99 modest_singletons_init (ModestSingletons *obj)
101 ModestSingletonsPrivate *priv;
102 gchar *images_cache_path;
103 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
106 priv->account_mgr = NULL;
107 priv->email_clipboard = NULL;
108 priv->cache_mgr = NULL;
109 priv->mail_op_queue = NULL;
110 priv->platform_fact = NULL;
112 priv->window_mgr = NULL;
113 priv->protocol_registry = NULL;
114 priv->plugin_factory = NULL;
116 priv->protocol_registry = modest_protocol_registry_new ();
117 if (!priv->protocol_registry) {
118 g_printerr ("modest:cannot create protocol registry instance\n");
121 modest_protocol_registry_set_to_default (priv->protocol_registry);
122 priv->images_cache = NULL;
124 priv->conf = modest_conf_new ();
126 g_printerr ("modest: cannot create modest conf instance\n");
130 priv->account_mgr = modest_account_mgr_new (priv->conf);
131 if (!priv->account_mgr) {
132 g_printerr ("modest: cannot create modest account mgr instance\n");
136 priv->email_clipboard = modest_email_clipboard_new ();
137 if (!priv->email_clipboard) {
138 g_printerr ("modest: cannot create modest email clipboard instance\n");
142 priv->platform_fact = modest_tny_platform_factory_get_instance ();
143 if (!priv->platform_fact) {
144 g_printerr ("modest: cannot create platform factory instance\n");
148 priv->device = tny_platform_factory_new_device (priv->platform_fact);
150 g_printerr ("modest: cannot create tny device instance\n");
154 priv->cache_mgr = modest_cache_mgr_new ();
155 if (!priv->cache_mgr) {
156 g_printerr ("modest: cannot create modest cache mgr instance\n");
160 priv->mail_op_queue = modest_mail_operation_queue_new ();
161 if (!priv->mail_op_queue) {
162 g_printerr ("modest: cannot create modest mail operation queue instance\n");
166 priv->window_mgr = modest_hildon1_window_mgr_new ();
167 if (!priv->window_mgr) {
168 g_printerr ("modest: cannot create modest window manager instance\n");
172 priv->plugin_factory = modest_plugin_factory_new ();
173 if (!priv->plugin_factory) {
174 g_printerr ("modest: cannot create modest mail plugin factory instance\n");
178 images_cache_path = g_build_filename (g_get_home_dir (), MODEST_DIR, MODEST_IMAGES_CACHE_DIR, NULL);
179 priv->images_cache = tny_fs_stream_cache_new (images_cache_path, MODEST_IMAGES_CACHE_SIZE);
180 g_free (images_cache_path);
181 if (!priv->images_cache) {
182 g_printerr ("modest: cannot create images cache instance\n");
189 modest_singletons_finalize (GObject *obj)
191 ModestSingletonsPrivate *priv;
193 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
195 if (priv->images_cache) {
196 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF (priv->images_cache, "");
197 g_object_unref (G_OBJECT (priv->images_cache));
198 priv->images_cache = NULL;
201 if (priv->window_mgr) {
202 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->window_mgr,"");
203 g_object_unref (G_OBJECT(priv->window_mgr));
204 priv->window_mgr = NULL;
207 if (priv->mail_op_queue) {
208 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->mail_op_queue,"");
209 g_object_unref (G_OBJECT(priv->mail_op_queue));
210 priv->mail_op_queue = NULL;
213 if (priv->cache_mgr) {
214 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->cache_mgr,"");
215 g_object_unref (G_OBJECT(priv->cache_mgr));
216 priv->cache_mgr = NULL;
220 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->device,"");
221 g_object_unref (G_OBJECT(priv->device));
225 if (priv->platform_fact) {
226 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->platform_fact,"");
227 g_object_unref (G_OBJECT(priv->platform_fact));
228 priv->platform_fact = NULL;
231 if (priv->email_clipboard) {
232 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->email_clipboard,"");
233 g_object_unref (G_OBJECT(priv->email_clipboard));
234 priv->email_clipboard = NULL;
237 /* It is important that the account manager is uninitialized after
238 * the mail op queue is uninitialized because the mail op queue
239 * cancells any mail operations which in turn access the account
240 * manager (see modest_mail_operation_notify_end()). */
241 if (priv->account_mgr) {
242 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->account_mgr,"");
243 g_object_unref (G_OBJECT(priv->account_mgr));
244 priv->account_mgr = NULL;
248 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->conf,"");
249 g_object_unref (G_OBJECT(priv->conf));
253 G_OBJECT_CLASS(parent_class)->finalize (obj);
257 modest_singletons_new (void)
259 ModestSingletonsPrivate *priv;
260 ModestSingletons *self;
261 static gboolean invoked = FALSE;
264 g_printerr ("%s: modest: modest_singletons_new may only be called once, aborting...\n",
270 self = MODEST_SINGLETONS(g_object_new(MODEST_TYPE_SINGLETONS, NULL));
271 priv = MODEST_SINGLETONS_GET_PRIVATE(self);
273 /* widget_factory will still be NULL, as it is initialized lazily */
274 if (!(priv->conf && priv->account_mgr && priv->email_clipboard &&
275 priv->cache_mgr && priv->mail_op_queue && priv->device &&
276 priv->platform_fact && priv->plugin_factory)) {
277 g_printerr ("modest: failed to create singletons object\n");
278 g_object_unref (G_OBJECT(self));
288 modest_singletons_get_conf (ModestSingletons *self)
290 g_return_val_if_fail (self, NULL);
291 return MODEST_SINGLETONS_GET_PRIVATE(self)->conf;
295 modest_singletons_get_account_mgr (ModestSingletons *self)
297 g_return_val_if_fail (self, NULL);
298 return MODEST_SINGLETONS_GET_PRIVATE(self)->account_mgr;
301 ModestEmailClipboard*
302 modest_singletons_get_email_clipboard (ModestSingletons *self)
304 g_return_val_if_fail (self, NULL);
305 return MODEST_SINGLETONS_GET_PRIVATE(self)->email_clipboard;
309 modest_singletons_get_cache_mgr (ModestSingletons *self)
311 g_return_val_if_fail (self, NULL);
312 return MODEST_SINGLETONS_GET_PRIVATE(self)->cache_mgr;
315 ModestMailOperationQueue*
316 modest_singletons_get_mail_operation_queue (ModestSingletons *self)
318 g_return_val_if_fail (self, NULL);
319 return MODEST_SINGLETONS_GET_PRIVATE(self)->mail_op_queue;
323 modest_singletons_get_device (ModestSingletons *self)
325 g_return_val_if_fail (self, NULL);
326 return MODEST_SINGLETONS_GET_PRIVATE(self)->device;
331 modest_singletons_get_platform_factory (ModestSingletons *self)
333 g_return_val_if_fail (self, NULL);
334 return MODEST_SINGLETONS_GET_PRIVATE(self)->platform_fact;
338 modest_singletons_get_window_mgr (ModestSingletons *self)
340 g_return_val_if_fail (self, NULL);
341 return MODEST_SINGLETONS_GET_PRIVATE(self)->window_mgr;
344 ModestProtocolRegistry*
345 modest_singletons_get_protocol_registry (ModestSingletons *self)
347 g_return_val_if_fail (self, NULL);
348 return MODEST_SINGLETONS_GET_PRIVATE(self)->protocol_registry;
352 modest_singletons_get_images_cache (ModestSingletons *self)
354 g_return_val_if_fail (self, NULL);
355 return MODEST_SINGLETONS_GET_PRIVATE(self)->images_cache;
358 ModestPluginFactory *
359 modest_singletons_get_plugin_factory (ModestSingletons *self)
361 g_return_val_if_fail (self, NULL);
363 return MODEST_SINGLETONS_GET_PRIVATE (self)->plugin_factory;