+
+static gboolean
+idle_top_modal (gpointer data)
+{
+ ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
+ ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
+ GtkWindow *topmost;
+
+ /* Get the top modal */
+ g_mutex_lock (priv->queue_lock);
+ topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
+ g_mutex_unlock (priv->queue_lock);
+
+ /* Show it */
+ if (topmost)
+ gtk_window_present (topmost);
+
+ return FALSE;
+}
+
+static void
+remove_modal_from_queue (GtkWidget *widget,
+ ModestWindowMgr *self)
+{
+ ModestWindowMgrPrivate *priv;
+ GList *item = NULL;
+
+ priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
+
+ /* Remove from queue. We don't use remove, because we want to
+ exit if the widget does not belong to the queue */
+ g_mutex_lock (priv->queue_lock);
+ item = g_queue_find (priv->modal_windows, widget);
+ if (!item) {
+ g_warning ("Trying to remove a modal window that is not registered");
+ g_mutex_unlock (priv->queue_lock);
+ return;
+ }
+ g_queue_unlink (priv->modal_windows, item);
+ g_mutex_unlock (priv->queue_lock);
+
+ /* Disconnect handler */
+ priv->modal_handler_uids =
+ modest_signal_mgr_disconnect (priv->modal_handler_uids,
+ G_OBJECT (widget),
+ GTK_IS_DIALOG (widget) ?
+ "response" :
+ "destroy-event");
+
+ /* Schedule the next one for being shown */
+ g_idle_add (idle_top_modal, self);
+}
+
+static gboolean
+on_modal_window_close (GtkWidget *widget,
+ GdkEvent *event,
+ gpointer user_data)
+{
+ ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
+
+ /* Remove modal window from queue */
+ remove_modal_from_queue (widget, self);
+
+ /* Continue */
+ return FALSE;
+}
+
+static void
+on_modal_dialog_close (GtkDialog *dialog,
+ gint arg1,
+ gpointer user_data)
+{
+ ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
+
+ /* Remove modal window from queue */
+ remove_modal_from_queue (GTK_WIDGET (dialog), self);
+}
+
+gint
+modest_window_mgr_num_windows (ModestWindowMgr *self)
+{
+ ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
+ gint num_windows = 0;
+
+ if (priv->window_list)
+ num_windows = g_list_length (priv->window_list);
+
+ return num_windows;
+}