* check for a valid foldername
[modest] / src / widgets / modest-window-mgr.c
index 9a8c103..f87fd13 100644 (file)
@@ -36,7 +36,7 @@
 #include "widgets/modest-main-window.h"
 #include "widgets/modest-msg-edit-window.h"
 #include "widgets/modest-msg-view-window.h"
-/* include other impl specific header files */
+
 
 /* 'private'/'protected' functions */
 static void modest_window_mgr_class_init (ModestWindowMgrClass *klass);
@@ -66,6 +66,8 @@ struct _ModestWindowMgrPrivate {
        GSList       *preregistered_uids;
        GHashTable   *destroy_handlers;
        GHashTable   *viewer_handlers;
+       
+       guint        closing_time;
 };
 #define MODEST_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                MODEST_TYPE_WINDOW_MGR, \
@@ -130,6 +132,8 @@ modest_window_mgr_init (ModestWindowMgr *obj)
        priv->show_toolbars_fullscreen = FALSE;
        priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);   
        priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
+
+       priv->closing_time = 0;
 }
 
 static void
@@ -283,11 +287,15 @@ compare_msguids (ModestWindow *win,
 {
        const gchar *msg_uid;
 
-       if (!MODEST_IS_MSG_VIEW_WINDOW (win))
+       if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
                return 1;
 
        /* Get message uid from msg window */
-       msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
+       if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
+               msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
+       } else {
+               msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
+       }
        
        if (msg_uid && uid &&!strcmp (msg_uid, uid))
                return 0;
@@ -325,7 +333,8 @@ modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *head
        if (item) {
                has_window = TRUE;
                if (win) {
-                       if (!MODEST_IS_MSG_VIEW_WINDOW(item->data))
+                       if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
+                           (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
                                g_debug ("not a valid window!");
                        else {
                                g_debug ("found a window");
@@ -385,6 +394,19 @@ modest_window_mgr_register_window (ModestWindowMgr *self,
                        remove_uid (priv->preregistered_uids,
                                    modest_msg_view_window_get_message_uid
                                    (MODEST_MSG_VIEW_WINDOW (window)));
+       } else if (MODEST_IS_MSG_EDIT_WINDOW(window)) {
+               const gchar *uid = modest_msg_edit_window_get_message_uid
+                       (MODEST_MSG_EDIT_WINDOW (window));
+
+               g_debug ("registering window for %s", uid);
+                               
+               if (!has_uid (priv->preregistered_uids, uid)) 
+                       g_debug ("weird: no uid for window (%s)", uid);
+
+               priv->preregistered_uids = 
+                       remove_uid (priv->preregistered_uids,
+                                   modest_msg_edit_window_get_message_uid
+                                   (MODEST_MSG_EDIT_WINDOW (window)));
        }
        
        /* Add to list. Keep a reference to the window */
@@ -500,6 +522,61 @@ disconnect_msg_changed (gpointer key,
 }
 
 
+
+/* interval before retrying to close the application */
+#define CLOSING_RETRY_INTERVAL 3000 
+/* interval before cancel whatever is left in the queue, and closing anyway */
+#define MAX_WAIT_FOR_CLOSING 30 * 1000 
+
+static gboolean
+on_quit_maybe (ModestWindowMgr *self)
+{
+       ModestWindowMgrPrivate *priv;
+       guint queue_num;
+       
+       priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
+
+       /* it seems, in the meantime some windows were
+        * created. in that case, stop  'on_quit_maybe' */
+       if (priv->window_list) {
+               priv->closing_time = 0;
+               return FALSE;
+       }
+
+       if (priv->closing_time >= MAX_WAIT_FOR_CLOSING) {
+               /* we waited long enough: cancel all remaining operations */
+               g_debug ("%s: we waited long enough (%ds), cancelling queue and quiting",
+                        __FUNCTION__, priv->closing_time/1000);
+               /* FIXME: below gives me a lot of:
+                * GLIB CRITICAL ** default - modest_mail_operation_cancel:
+                *                     assertion `priv->account' failed
+                * which means there is no account for the given operation
+                * so, we're not trying to be nice, we're just quiting.
+                */
+               //modest_mail_operation_queue_cancel_all
+               //      (modest_runtime_get_mail_operation_queue());
+       } else {
+       
+               /* if there is anything left in our operation queue,
+                * wait another round
+                */
+               queue_num = modest_mail_operation_queue_num_elements
+                       (modest_runtime_get_mail_operation_queue()); 
+               if  (queue_num > 0) {
+                       g_debug ("%s: waiting, there are still %d operation(s) queued",
+                                __FUNCTION__, queue_num);
+                       priv->closing_time += CLOSING_RETRY_INTERVAL;
+                       return TRUE;
+               }
+       }
+       
+       /* so: no windows left, nothing in the queue: quit */
+       priv->closing_time = 0;
+       gtk_main_quit ();
+       return FALSE;
+}
+
+
 void 
 modest_window_mgr_unregister_window (ModestWindowMgr *self, 
                                     ModestWindow *window)
@@ -554,27 +631,27 @@ modest_window_mgr_unregister_window (ModestWindowMgr *self,
 
        /* Disconnect all the window signals */
        modest_window_disconnect_signals (window);
-
+       
        /* Destroy the window */
        gtk_widget_destroy (win->data);
-
+       
        /* If there are no more windows registered then exit program */
        if (priv->window_list == NULL) {
+               
                ModestConf *conf = modest_runtime_get_conf ();
-
                /* Save show toolbar status */
                modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, 
                                      priv->show_toolbars_fullscreen, NULL);
                modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR, 
                                      priv->show_toolbars, NULL);
 
-               /* Quit main loop */
-               /* FIXME: do we ever need to do this here? */
-               if (gtk_main_level() > 0)
-                       gtk_main_quit ();
+               g_timeout_add (CLOSING_RETRY_INTERVAL,
+                              (GSourceFunc)on_quit_maybe, self);
        }
 }
 
+
+
 void
 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
                                       gboolean on)
@@ -665,11 +742,16 @@ ModestWindow*
 modest_window_mgr_get_main_window (ModestWindowMgr *self)
 {
        ModestWindowMgrPrivate *priv;
-
+       
        g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
-
        priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
 
+       /* create the main window, if it hasn't been created yet */
+       if (!priv->main_window) {
+               g_debug ("%s: creating main window\n", __FUNCTION__);
+               modest_window_mgr_register_window (self, modest_main_window_new ());
+       }
+       
        return priv->main_window;
 }