+
+ if (!gtk_window_has_toplevel_focus (window)) {
+ GList *toplevels, *node;
+
+ toplevels = gtk_window_list_toplevels ();
+ for (node = toplevels; node != NULL; node = g_list_next (node)) {
+ if (gtk_window_has_toplevel_focus (GTK_WINDOW (node->data))) {
+ if (GTK_IS_DIALOG (node->data)) {
+ gtk_window_set_transient_for (window, GTK_WINDOW (node->data));
+ }
+ break;
+ }
+ }
+ }
+}
+
+static void
+close_all_but_first (gpointer data)
+{
+ gint num_windows, i;
+ gboolean retval;
+ HildonWindowStack *stack;
+
+ stack = hildon_window_stack_get_default ();
+ g_return_if_fail (stack);
+
+ num_windows = hildon_window_stack_size (stack);
+
+ for (i = 0; i < (num_windows - 1); i++) {
+ GtkWidget *current_top;
+
+ /* Close window */
+ current_top = hildon_window_stack_peek (stack);
+ g_signal_emit_by_name (G_OBJECT (current_top), "delete-event", NULL, &retval);
+ }
+}
+
+static gboolean
+on_idle_close_all_but_first (gpointer data)
+{
+ gdk_threads_enter ();
+ close_all_but_first (data);
+ gdk_threads_leave ();
+
+ return FALSE;
+}
+
+static void
+on_account_removed (TnyAccountStore *acc_store,
+ TnyAccount *account,
+ gpointer user_data)
+{
+ HildonWindowStack *stack;
+ ModestWindow *current_top;
+
+ /* Ignore transport account removals */
+ if (TNY_IS_TRANSPORT_ACCOUNT (account))
+ return;
+
+ stack = hildon_window_stack_get_default ();
+ current_top = (ModestWindow *) hildon_window_stack_peek (stack);
+
+ /* if we're showing the header view of the currently deleted
+ account, or the outbox and we deleted the last account,
+ then close the window */
+ if (current_top &&
+ (MODEST_IS_HEADER_WINDOW (current_top) ||
+ MODEST_IS_FOLDER_WINDOW (current_top))) {
+ const gchar *acc_name;
+
+ acc_name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
+
+ /* We emit it in an idle, because sometimes this
+ function could called when the account settings
+ dialog is about to close but still there. That
+ modal dialog would otherwise, prevent the
+ windows from being closed */
+ if (!strcmp (acc_name, modest_window_get_active_account (current_top)))
+ g_idle_add (on_idle_close_all_but_first, NULL);
+ }
+}
+
+static ModestWindow *
+modest_hildon2_window_mgr_show_initial_window (ModestWindowMgr *self)
+{
+ ModestWindow *initial_window = NULL;
+ ModestHildon2WindowMgrPrivate *priv;
+
+ priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
+
+ /* Connect to the account store "account-removed" signal". We
+ do this here because in the init the singletons are still
+ not initialized properly */
+ if (!g_signal_handler_is_connected (modest_runtime_get_account_store (),
+ priv->accounts_handler)) {
+ priv->accounts_handler = g_signal_connect (modest_runtime_get_account_store (),
+ "account-removed",
+ G_CALLBACK (on_account_removed),
+ self);
+ }
+
+ /* Return accounts window */
+ initial_window = MODEST_WINDOW (modest_accounts_window_new ());
+ modest_window_mgr_register_window (self, initial_window, NULL);
+
+ return initial_window;
+}
+
+
+static ModestWindow *
+modest_hildon2_window_mgr_get_current_top (ModestWindowMgr *self)
+{
+ HildonWindowStack *stack;
+ stack = hildon_window_stack_get_default ();
+ return (ModestWindow *) hildon_window_stack_peek (stack);
+}
+
+static gint
+find_folder_window (gconstpointer a,
+ gconstpointer b)
+{
+ return (MODEST_IS_FOLDER_WINDOW (a)) ? 0 : 1;
+}
+
+ModestWindow *
+modest_hildon2_window_mgr_get_folder_window (ModestHildon2WindowMgr *self)
+{
+ ModestHildon2WindowMgrPrivate *priv;
+ GList *window;
+
+ g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
+
+ priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
+
+ window = g_list_find_custom (priv->window_list,
+ NULL,
+ find_folder_window);
+
+ return (window != NULL) ? MODEST_WINDOW (window->data) : NULL;