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 #ifdef MODEST_TOOLKIT_HILDON2
35 #include "hildon2/modest-hildon2-window-mgr.h"
37 #include "gtk/modest-gtk-window-mgr.h"
39 #include <tny-fs-stream-cache.h>
41 /* 'private'/'protected' functions */
42 static void modest_singletons_class_init (ModestSingletonsClass *klass);
43 static void modest_singletons_init (ModestSingletons *obj);
44 static void modest_singletons_finalize (GObject *obj);
46 typedef struct _ModestSingletonsPrivate ModestSingletonsPrivate;
47 struct _ModestSingletonsPrivate {
49 ModestAccountMgr *account_mgr;
50 ModestEmailClipboard *email_clipboard;
51 ModestCacheMgr *cache_mgr;
52 ModestMailOperationQueue *mail_op_queue;
53 TnyPlatformFactory *platform_fact;
55 ModestWindowMgr *window_mgr;
56 ModestProtocolRegistry *protocol_registry;
57 ModestPluginFactory *plugin_factory;
58 ModestToolkitFactory *toolkit_factory;
59 TnyStreamCache *images_cache;
61 #define MODEST_SINGLETONS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
62 MODEST_TYPE_SINGLETONS, \
63 ModestSingletonsPrivate))
65 static GObjectClass *parent_class = NULL;
68 modest_singletons_get_type (void)
70 static GType my_type = 0;
72 static const GTypeInfo my_info = {
73 sizeof(ModestSingletonsClass),
75 NULL, /* base finalize */
76 (GClassInitFunc) modest_singletons_class_init,
77 NULL, /* class finalize */
78 NULL, /* class data */
79 sizeof(ModestSingletons),
81 (GInstanceInitFunc) modest_singletons_init,
84 my_type = g_type_register_static (G_TYPE_OBJECT,
92 modest_singletons_class_init (ModestSingletonsClass *klass)
94 GObjectClass *gobject_class;
95 gobject_class = (GObjectClass*) klass;
97 parent_class = g_type_class_peek_parent (klass);
98 gobject_class->finalize = modest_singletons_finalize;
100 g_type_class_add_private (gobject_class, sizeof(ModestSingletonsPrivate));
104 modest_singletons_init (ModestSingletons *obj)
106 ModestSingletonsPrivate *priv;
107 gchar *images_cache_path;
108 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
111 priv->account_mgr = NULL;
112 priv->email_clipboard = NULL;
113 priv->cache_mgr = NULL;
114 priv->mail_op_queue = NULL;
115 priv->platform_fact = NULL;
117 priv->window_mgr = NULL;
118 priv->protocol_registry = NULL;
119 priv->plugin_factory = NULL;
120 priv->toolkit_factory = NULL;
122 priv->protocol_registry = modest_protocol_registry_new ();
123 if (!priv->protocol_registry) {
124 g_printerr ("modest:cannot create protocol registry instance\n");
127 modest_protocol_registry_set_to_default (priv->protocol_registry);
128 priv->images_cache = NULL;
130 priv->conf = modest_conf_new ();
132 g_printerr ("modest: cannot create modest conf instance\n");
136 priv->account_mgr = modest_account_mgr_new (priv->conf);
137 if (!priv->account_mgr) {
138 g_printerr ("modest: cannot create modest account mgr instance\n");
142 priv->email_clipboard = modest_email_clipboard_new ();
143 if (!priv->email_clipboard) {
144 g_printerr ("modest: cannot create modest email clipboard instance\n");
148 priv->platform_fact = modest_tny_platform_factory_get_instance ();
149 if (!priv->platform_fact) {
150 g_printerr ("modest: cannot create platform factory instance\n");
154 priv->toolkit_factory = modest_toolkit_factory_get_instance ();
155 if (!priv->toolkit_factory) {
156 g_printerr ("modest: cannot create toolkit factory instance\n");
160 priv->device = tny_platform_factory_new_device (priv->platform_fact);
162 g_printerr ("modest: cannot create tny device instance\n");
166 priv->cache_mgr = modest_cache_mgr_new ();
167 if (!priv->cache_mgr) {
168 g_printerr ("modest: cannot create modest cache mgr instance\n");
172 priv->mail_op_queue = modest_mail_operation_queue_new ();
173 if (!priv->mail_op_queue) {
174 g_printerr ("modest: cannot create modest mail operation queue instance\n");
178 #if MODEST_TOOLKIT_HILDON2
179 priv->window_mgr = modest_hildon2_window_mgr_new ();
181 priv->window_mgr = modest_gtk_window_mgr_new ();
183 if (!priv->window_mgr) {
184 g_printerr ("modest: cannot create modest window manager instance\n");
188 priv->plugin_factory = modest_plugin_factory_new ();
189 if (!priv->plugin_factory) {
190 g_printerr ("modest: cannot create modest mail plugin factory instance\n");
194 images_cache_path = g_build_filename (g_get_home_dir (), MODEST_DIR, MODEST_IMAGES_CACHE_DIR, NULL);
195 priv->images_cache = tny_fs_stream_cache_new (images_cache_path, MODEST_IMAGES_CACHE_SIZE);
196 g_free (images_cache_path);
197 if (!priv->images_cache) {
198 g_printerr ("modest: cannot create images cache instance\n");
205 modest_singletons_finalize (GObject *obj)
207 ModestSingletonsPrivate *priv;
209 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
211 if (priv->images_cache) {
212 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF (priv->images_cache, "");
213 g_object_unref (G_OBJECT (priv->images_cache));
214 priv->images_cache = NULL;
217 if (priv->window_mgr) {
218 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->window_mgr,"");
219 g_object_unref (G_OBJECT(priv->window_mgr));
220 priv->window_mgr = NULL;
223 if (priv->mail_op_queue) {
224 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->mail_op_queue,"");
225 g_object_unref (G_OBJECT(priv->mail_op_queue));
226 priv->mail_op_queue = NULL;
229 if (priv->cache_mgr) {
230 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->cache_mgr,"");
231 g_object_unref (G_OBJECT(priv->cache_mgr));
232 priv->cache_mgr = NULL;
236 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->device,"");
237 g_object_unref (G_OBJECT(priv->device));
241 if (priv->platform_fact) {
242 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->platform_fact,"");
243 g_object_unref (G_OBJECT(priv->platform_fact));
244 priv->platform_fact = NULL;
247 if (priv->email_clipboard) {
248 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->email_clipboard,"");
249 g_object_unref (G_OBJECT(priv->email_clipboard));
250 priv->email_clipboard = NULL;
253 if (priv->protocol_registry) {
254 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->protocol_registry,"");
255 g_object_unref (G_OBJECT(priv->protocol_registry));
256 priv->protocol_registry = NULL;
259 if (priv->plugin_factory) {
260 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->plugin_factory,"");
261 g_object_unref (G_OBJECT(priv->plugin_factory));
262 priv->plugin_factory = NULL;
265 /* It is important that the account manager is uninitialized after
266 * the mail op queue is uninitialized because the mail op queue
267 * cancells any mail operations which in turn access the account
268 * manager (see modest_mail_operation_notify_end()). */
269 if (priv->account_mgr) {
270 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->account_mgr,"");
271 g_object_unref (G_OBJECT(priv->account_mgr));
272 priv->account_mgr = NULL;
276 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->conf,"");
277 g_object_unref (G_OBJECT(priv->conf));
281 G_OBJECT_CLASS(parent_class)->finalize (obj);
285 modest_singletons_new (void)
287 ModestSingletonsPrivate *priv;
288 ModestSingletons *self;
289 static gboolean invoked = FALSE;
292 g_printerr ("%s: modest: modest_singletons_new may only be called once, aborting...\n",
298 self = MODEST_SINGLETONS(g_object_new(MODEST_TYPE_SINGLETONS, NULL));
299 priv = MODEST_SINGLETONS_GET_PRIVATE(self);
301 /* widget_factory will still be NULL, as it is initialized lazily */
302 if (!(priv->conf && priv->account_mgr && priv->email_clipboard &&
303 priv->cache_mgr && priv->mail_op_queue && priv->device &&
304 priv->platform_fact && priv->plugin_factory)) {
305 g_printerr ("modest: failed to create singletons object\n");
306 g_object_unref (G_OBJECT(self));
316 modest_singletons_get_conf (ModestSingletons *self)
318 g_return_val_if_fail (self, NULL);
319 return MODEST_SINGLETONS_GET_PRIVATE(self)->conf;
323 modest_singletons_get_account_mgr (ModestSingletons *self)
325 g_return_val_if_fail (self, NULL);
326 return MODEST_SINGLETONS_GET_PRIVATE(self)->account_mgr;
329 ModestEmailClipboard*
330 modest_singletons_get_email_clipboard (ModestSingletons *self)
332 g_return_val_if_fail (self, NULL);
333 return MODEST_SINGLETONS_GET_PRIVATE(self)->email_clipboard;
337 modest_singletons_get_cache_mgr (ModestSingletons *self)
339 g_return_val_if_fail (self, NULL);
340 return MODEST_SINGLETONS_GET_PRIVATE(self)->cache_mgr;
343 ModestMailOperationQueue*
344 modest_singletons_get_mail_operation_queue (ModestSingletons *self)
346 g_return_val_if_fail (self, NULL);
347 return MODEST_SINGLETONS_GET_PRIVATE(self)->mail_op_queue;
351 modest_singletons_get_device (ModestSingletons *self)
353 g_return_val_if_fail (self, NULL);
354 return MODEST_SINGLETONS_GET_PRIVATE(self)->device;
359 modest_singletons_get_platform_factory (ModestSingletons *self)
361 g_return_val_if_fail (self, NULL);
362 return MODEST_SINGLETONS_GET_PRIVATE(self)->platform_fact;
365 ModestToolkitFactory*
366 modest_singletons_get_toolkit_factory (ModestSingletons *self)
368 g_return_val_if_fail (self, NULL);
369 return MODEST_SINGLETONS_GET_PRIVATE(self)->toolkit_factory;
373 modest_singletons_get_window_mgr (ModestSingletons *self)
375 g_return_val_if_fail (self, NULL);
376 return MODEST_SINGLETONS_GET_PRIVATE(self)->window_mgr;
379 ModestProtocolRegistry*
380 modest_singletons_get_protocol_registry (ModestSingletons *self)
382 g_return_val_if_fail (self, NULL);
383 return MODEST_SINGLETONS_GET_PRIVATE(self)->protocol_registry;
387 modest_singletons_get_images_cache (ModestSingletons *self)
389 g_return_val_if_fail (self, NULL);
390 return MODEST_SINGLETONS_GET_PRIVATE(self)->images_cache;
393 ModestPluginFactory *
394 modest_singletons_get_plugin_factory (ModestSingletons *self)
396 g_return_val_if_fail (self, NULL);
398 return MODEST_SINGLETONS_GET_PRIVATE (self)->plugin_factory;