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 <tny-fs-stream-cache.h>
36 /* 'private'/'protected' functions */
37 static void modest_singletons_class_init (ModestSingletonsClass *klass);
38 static void modest_singletons_init (ModestSingletons *obj);
39 static void modest_singletons_finalize (GObject *obj);
41 typedef struct _ModestSingletonsPrivate ModestSingletonsPrivate;
42 struct _ModestSingletonsPrivate {
44 ModestAccountMgr *account_mgr;
45 ModestEmailClipboard *email_clipboard;
46 ModestCacheMgr *cache_mgr;
47 ModestMailOperationQueue *mail_op_queue;
48 TnyPlatformFactory *platform_fact;
50 ModestWindowMgr *window_mgr;
52 TnyStreamCache *images_cache;
54 ModestProtocolRegistry *protocol_registry;
55 ModestPluginFactory *plugin_factory;
56 TnyStreamCache *images_cache;
57 >>>>>>> .merge-right.r5668
59 #define MODEST_SINGLETONS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
60 MODEST_TYPE_SINGLETONS, \
61 ModestSingletonsPrivate))
63 static GObjectClass *parent_class = NULL;
66 modest_singletons_get_type (void)
68 static GType my_type = 0;
70 static const GTypeInfo my_info = {
71 sizeof(ModestSingletonsClass),
73 NULL, /* base finalize */
74 (GClassInitFunc) modest_singletons_class_init,
75 NULL, /* class finalize */
76 NULL, /* class data */
77 sizeof(ModestSingletons),
79 (GInstanceInitFunc) modest_singletons_init,
82 my_type = g_type_register_static (G_TYPE_OBJECT,
90 modest_singletons_class_init (ModestSingletonsClass *klass)
92 GObjectClass *gobject_class;
93 gobject_class = (GObjectClass*) klass;
95 parent_class = g_type_class_peek_parent (klass);
96 gobject_class->finalize = modest_singletons_finalize;
98 g_type_class_add_private (gobject_class, sizeof(ModestSingletonsPrivate));
102 modest_singletons_init (ModestSingletons *obj)
104 ModestSingletonsPrivate *priv;
105 gchar *images_cache_path;
106 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
109 priv->account_mgr = NULL;
110 priv->email_clipboard = NULL;
111 priv->cache_mgr = NULL;
112 priv->mail_op_queue = NULL;
113 priv->platform_fact = NULL;
115 priv->window_mgr = NULL;
117 priv->images_cache = NULL;
119 priv->protocol_registry = NULL;
120 priv->plugin_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;
129 >>>>>>> .merge-right.r5668
131 priv->conf = modest_conf_new ();
133 g_printerr ("modest: cannot create modest conf instance\n");
137 priv->account_mgr = modest_account_mgr_new (priv->conf);
138 if (!priv->account_mgr) {
139 g_printerr ("modest: cannot create modest account mgr instance\n");
143 priv->email_clipboard = modest_email_clipboard_new ();
144 if (!priv->email_clipboard) {
145 g_printerr ("modest: cannot create modest email clipboard instance\n");
149 priv->platform_fact = modest_tny_platform_factory_get_instance ();
150 if (!priv->platform_fact) {
151 g_printerr ("modest: cannot create platform factory instance\n");
155 priv->device = tny_platform_factory_new_device (priv->platform_fact);
157 g_printerr ("modest: cannot create tny device instance\n");
161 priv->cache_mgr = modest_cache_mgr_new ();
162 if (!priv->cache_mgr) {
163 g_printerr ("modest: cannot create modest cache mgr instance\n");
167 priv->mail_op_queue = modest_mail_operation_queue_new ();
168 if (!priv->mail_op_queue) {
169 g_printerr ("modest: cannot create modest mail operation queue instance\n");
173 priv->window_mgr = modest_window_mgr_new ();
174 if (!priv->window_mgr) {
175 g_printerr ("modest: cannot create modest window manager instance\n");
180 images_cache_path = g_build_filename (g_get_home_dir (), MODEST_DIR, MODEST_IMAGES_CACHE_DIR, NULL);
181 priv->images_cache = tny_fs_stream_cache_new (images_cache_path, MODEST_IMAGES_CACHE_SIZE);
182 g_free (images_cache_path);
183 if (!priv->images_cache) {
184 g_printerr ("modest: cannot create images cache instance\n");
189 priv->plugin_factory = modest_plugin_factory_new ();
190 if (!priv->plugin_factory) {
191 g_printerr ("modest: cannot create modest mail plugin factory instance\n");
195 images_cache_path = g_build_filename (g_get_home_dir (), MODEST_DIR, MODEST_IMAGES_CACHE_DIR, NULL);
196 priv->images_cache = tny_fs_stream_cache_new (images_cache_path, MODEST_IMAGES_CACHE_SIZE);
197 g_free (images_cache_path);
198 if (!priv->images_cache) {
199 g_printerr ("modest: cannot create images cache instance\n");
203 >>>>>>> .merge-right.r5668
207 modest_singletons_finalize (GObject *obj)
209 ModestSingletonsPrivate *priv;
211 priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
213 if (priv->images_cache) {
214 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF (priv->images_cache, "");
215 g_object_unref (G_OBJECT (priv->images_cache));
216 priv->images_cache = NULL;
219 if (priv->window_mgr) {
220 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->window_mgr,"");
221 g_object_unref (G_OBJECT(priv->window_mgr));
222 priv->window_mgr = NULL;
225 if (priv->mail_op_queue) {
226 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->mail_op_queue,"");
227 g_object_unref (G_OBJECT(priv->mail_op_queue));
228 priv->mail_op_queue = NULL;
231 if (priv->cache_mgr) {
232 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->cache_mgr,"");
233 g_object_unref (G_OBJECT(priv->cache_mgr));
234 priv->cache_mgr = NULL;
238 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->device,"");
239 g_object_unref (G_OBJECT(priv->device));
243 if (priv->platform_fact) {
244 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->platform_fact,"");
245 g_object_unref (G_OBJECT(priv->platform_fact));
246 priv->platform_fact = NULL;
249 if (priv->email_clipboard) {
250 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->email_clipboard,"");
251 g_object_unref (G_OBJECT(priv->email_clipboard));
252 priv->email_clipboard = NULL;
255 /* It is important that the account manager is uninitialized after
256 * the mail op queue is uninitialized because the mail op queue
257 * cancells any mail operations which in turn access the account
258 * manager (see modest_mail_operation_notify_end()). */
259 if (priv->account_mgr) {
260 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->account_mgr,"");
261 g_object_unref (G_OBJECT(priv->account_mgr));
262 priv->account_mgr = NULL;
266 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(priv->conf,"");
267 g_object_unref (G_OBJECT(priv->conf));
271 G_OBJECT_CLASS(parent_class)->finalize (obj);
275 modest_singletons_new (void)
277 ModestSingletonsPrivate *priv;
278 ModestSingletons *self;
279 static gboolean invoked = FALSE;
282 g_printerr ("%s: modest: modest_singletons_new may only be called once, aborting...\n",
288 self = MODEST_SINGLETONS(g_object_new(MODEST_TYPE_SINGLETONS, NULL));
289 priv = MODEST_SINGLETONS_GET_PRIVATE(self);
291 /* widget_factory will still be NULL, as it is initialized lazily */
292 if (!(priv->conf && priv->account_mgr && priv->email_clipboard &&
293 priv->cache_mgr && priv->mail_op_queue && priv->device &&
294 priv->platform_fact && priv->plugin_factory)) {
295 g_printerr ("modest: failed to create singletons object\n");
296 g_object_unref (G_OBJECT(self));
306 modest_singletons_get_conf (ModestSingletons *self)
308 g_return_val_if_fail (self, NULL);
309 return MODEST_SINGLETONS_GET_PRIVATE(self)->conf;
313 modest_singletons_get_account_mgr (ModestSingletons *self)
315 g_return_val_if_fail (self, NULL);
316 return MODEST_SINGLETONS_GET_PRIVATE(self)->account_mgr;
319 ModestEmailClipboard*
320 modest_singletons_get_email_clipboard (ModestSingletons *self)
322 g_return_val_if_fail (self, NULL);
323 return MODEST_SINGLETONS_GET_PRIVATE(self)->email_clipboard;
327 modest_singletons_get_cache_mgr (ModestSingletons *self)
329 g_return_val_if_fail (self, NULL);
330 return MODEST_SINGLETONS_GET_PRIVATE(self)->cache_mgr;
333 ModestMailOperationQueue*
334 modest_singletons_get_mail_operation_queue (ModestSingletons *self)
336 g_return_val_if_fail (self, NULL);
337 return MODEST_SINGLETONS_GET_PRIVATE(self)->mail_op_queue;
341 modest_singletons_get_device (ModestSingletons *self)
343 g_return_val_if_fail (self, NULL);
344 return MODEST_SINGLETONS_GET_PRIVATE(self)->device;
349 modest_singletons_get_platform_factory (ModestSingletons *self)
351 g_return_val_if_fail (self, NULL);
352 return MODEST_SINGLETONS_GET_PRIVATE(self)->platform_fact;
356 modest_singletons_get_window_mgr (ModestSingletons *self)
358 g_return_val_if_fail (self, NULL);
359 return MODEST_SINGLETONS_GET_PRIVATE(self)->window_mgr;
364 modest_singletons_get_images_cache (ModestSingletons *self)
366 g_return_val_if_fail (self, NULL);
367 return MODEST_SINGLETONS_GET_PRIVATE(self)->images_cache;
371 ModestProtocolRegistry*
372 modest_singletons_get_protocol_registry (ModestSingletons *self)
374 g_return_val_if_fail (self, NULL);
375 return MODEST_SINGLETONS_GET_PRIVATE(self)->protocol_registry;
379 modest_singletons_get_images_cache (ModestSingletons *self)
381 g_return_val_if_fail (self, NULL);
382 return MODEST_SINGLETONS_GET_PRIVATE(self)->images_cache;
385 ModestPluginFactory *
386 modest_singletons_get_plugin_factory (ModestSingletons *self)
388 g_return_val_if_fail (self, NULL);
390 return MODEST_SINGLETONS_GET_PRIVATE (self)->plugin_factory;
392 >>>>>>> .merge-right.r5668