* Fixes NB#57580
[modest] / src / modest-singletons.c
index 61d0b08..9c0ede2 100644 (file)
@@ -44,6 +44,7 @@ struct _ModestSingletonsPrivate {
        ModestMailOperationQueue  *mail_op_queue;
        TnyPlatformFactory        *platform_fact;
        TnyDevice                 *device;
+       ModestWindowMgr           *window_mgr;
 };
 #define MODEST_SINGLETONS_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                MODEST_TYPE_SINGLETONS, \
@@ -100,6 +101,7 @@ modest_singletons_init (ModestSingletons *obj)
        priv->mail_op_queue  = NULL;
        priv->platform_fact  = NULL;
        priv->device         = NULL;
+       priv->window_mgr     = NULL;
        
        priv->conf           = modest_conf_new ();
        if (!priv->conf) {
@@ -141,75 +143,70 @@ modest_singletons_init (ModestSingletons *obj)
        if (!priv->mail_op_queue) {
                g_printerr ("modest: cannot create modest mail operation queue instance\n");
                return;
-       }       
-}
-
+       }
 
-static void
-check_object_is_dead (GObject *obj, gchar *name)
-{
-       if (G_IS_OBJECT(obj))
-               g_warning ("BUG: %s still has %d ref(s)\n", name, obj->ref_count);
+       priv->window_mgr = modest_window_mgr_new ();
+       if (!priv->window_mgr) {
+               g_printerr ("modest: cannot create modest window manager instance\n");
+               return;
+       }
 }
 
 static void
 modest_singletons_finalize (GObject *obj)
 {
        ModestSingletonsPrivate *priv;
-       gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
                
        priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
 
        if (priv->account_store) {
+               modest_runtime_verify_object_last_ref(priv->account_store,"");
                g_object_unref (G_OBJECT(priv->account_store));
-               if (debug)
-                       check_object_is_dead ((GObject*)priv->account_store,
-                                             "priv->account_store");
                priv->account_store = NULL;
        }
 
        if (priv->account_mgr) {
+               modest_runtime_verify_object_last_ref(priv->account_mgr,"");
                g_object_unref (G_OBJECT(priv->account_mgr));
-               if (debug)
-                       check_object_is_dead ((GObject*)priv->account_mgr,
-                                             "priv->account_mgr");
                priv->account_mgr = NULL;
        }
 
        if (priv->conf) {
+               modest_runtime_verify_object_last_ref(priv->conf,"");
                g_object_unref (G_OBJECT(priv->conf));
-               if (debug)
-                       check_object_is_dead ((GObject*)priv->conf,
-                                             "priv->conf");
                priv->conf = NULL;
        }
 
        if (priv->cache_mgr) {
+               modest_runtime_verify_object_last_ref(priv->cache_mgr,"");
                g_object_unref (G_OBJECT(priv->cache_mgr));
-               if (debug)
-                       check_object_is_dead ((GObject*)priv->cache_mgr,
-                                             "priv->cache_mgr");
                priv->cache_mgr = NULL;
        }
 
        if (priv->device) {
+               modest_runtime_verify_object_last_ref(priv->device,"");
                g_object_unref (G_OBJECT(priv->device));
-               if (debug)
-                       check_object_is_dead ((GObject*)priv->cache_mgr,
-                                             "priv->device");
                priv->device = NULL;
        }
 
        if (priv->platform_fact) {
+               modest_runtime_verify_object_last_ref(priv->platform_fact,"");
                g_object_unref (G_OBJECT(priv->platform_fact));
-               if (debug)
-                       check_object_is_dead ((GObject*)priv->cache_mgr,
-                                             "priv->platform_fact");
                priv->platform_fact = NULL;
        }
 
-
+       if (priv->mail_op_queue) {
+               modest_runtime_verify_object_last_ref(priv->mail_op_queue,"");
+               g_object_unref (G_OBJECT(priv->mail_op_queue));
+               priv->mail_op_queue = NULL;
+       }
        
+       if (priv->window_mgr) {
+               modest_runtime_verify_object_last_ref(priv->window_mgr,"");
+               g_object_unref (G_OBJECT(priv->window_mgr));
+               priv->window_mgr = NULL;
+       }
+
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
@@ -218,18 +215,26 @@ modest_singletons_new (void)
 {
        ModestSingletonsPrivate *priv;
        ModestSingletons *self;
+       static gboolean invoked = FALSE;
+
+       if (invoked) {
+               g_printerr ("modest: modest_singletons_new may only be called once\n");
+               g_assert (!invoked); /* abort */
+               return NULL; /* g_assert may be NOP */
+       }
        
        self = MODEST_SINGLETONS(g_object_new(MODEST_TYPE_SINGLETONS, NULL));
        priv = MODEST_SINGLETONS_GET_PRIVATE(self);
-
+       
        /* widget_factory will still be NULL, as it is initialized lazily */
        if (!(priv->conf && priv->account_mgr && priv->account_store &&
              priv->cache_mgr && priv->mail_op_queue && priv->device && priv->platform_fact)) {
-               g_printerr ("modest: failed to create singletons instance\n");
+               g_printerr ("modest: failed to create singletons object\n");
                g_object_unref (G_OBJECT(self));
                self = NULL;
        }
-       
+
+       invoked = TRUE;
        return self;
 }
 
@@ -286,3 +291,10 @@ modest_singletons_get_platform_factory (ModestSingletons *self)
        g_return_val_if_fail (self, NULL);
        return MODEST_SINGLETONS_GET_PRIVATE(self)->platform_fact;
 }
+
+ModestWindowMgr* 
+modest_singletons_get_window_mgr (ModestSingletons *self)
+{
+       g_return_val_if_fail (self, NULL);
+       return MODEST_SINGLETONS_GET_PRIVATE(self)->window_mgr;
+}