* Fixed NB#62792, the editor is closed if the account is removed
[modest] / src / dbus_api / modest-dbus-callbacks.c
index 9b3837a..962a675 100644 (file)
@@ -55,6 +55,8 @@
 #include <tny-simple-list.h>
 #include <tny-merge-folder.h>
 
+#include <modest-text-utils.h>
+
 typedef struct 
 {
        gchar *to;
@@ -193,7 +195,7 @@ check_and_offer_account_creation()
        
        if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
                printf ("DEBUG1: %s\n", __FUNCTION__);
-               const gboolean created = modest_run_account_setup_wizard (NULL);
+               const gboolean created = modest_ui_actions_run_account_setup_wizard (NULL);
                printf ("DEBUG1: %s\n", __FUNCTION__);
                if (!created) {
                        g_debug ("modest: %s: no account exists even after offering, "
@@ -207,7 +209,6 @@ check_and_offer_account_creation()
        return result;
 }
 
-
 static gboolean
 on_idle_mail_to(gpointer user_data)
 {
@@ -279,35 +280,21 @@ on_idle_mail_to(gpointer user_data)
                                
                        if (!msg) {
                                g_printerr ("modest: failed to create message\n");
-                       } else
-                       {
-                               /* Add the message to a folder and show its UI for editing: */
-                               TnyFolder *folder = modest_tny_account_get_special_folder (account,
-                                                                       TNY_FOLDER_TYPE_DRAFTS);
-                               if (!folder) {
-                                       g_printerr ("modest: failed to find Drafts folder\n");
-                               } else {
-                       
-                                       tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
-
-                                       /* This is a GDK lock because we are an idle callback and
-                                        * the code below is or does Gtk+ code */
-
-                                       gdk_threads_enter (); /* CHECKED */
-
-                                       ModestWindow *win = modest_msg_edit_window_new (msg, account_name, FALSE);
-                                       modest_window_mgr_register_window (modest_runtime_get_window_mgr (), win);
-                                       gtk_widget_show_all (GTK_WIDGET (win));
-
-                                       gdk_threads_leave (); /* CHECKED */
+                       } else {                        
+                               ModestWindow *win;
+
+                               /* This is a GDK lock because we are an idle callback and
+                                * the code below is or does Gtk+ code */
+                               gdk_threads_enter ();
+                               win = modest_msg_edit_window_new (msg, account_name, FALSE);
+                               modest_window_mgr_register_window (modest_runtime_get_window_mgr (), win);
+                               gtk_widget_show_all (GTK_WIDGET (win));
+                               gdk_threads_leave ();
                                
-                                       g_object_unref (G_OBJECT(folder));
-                                       g_object_unref (win);
-                               }
-                       
-                               g_object_unref (G_OBJECT(msg));
+                               g_object_unref (win);
                        }
                        
+                       g_object_unref (G_OBJECT(msg));                 
                        g_object_unref (G_OBJECT(account));
                }
        }
@@ -324,7 +311,8 @@ on_idle_mail_to(gpointer user_data)
        return FALSE; /* Do not call this callback again. */
 }
 
-static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
+static gint 
+on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
 {
        if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
        return OSSO_ERROR;
@@ -841,7 +829,7 @@ on_idle_delete_message (gpointer user_data)
         * so they would appear with a strike-through until then):
         */
        ModestHeaderView *header_view = MODEST_HEADER_VIEW(modest_main_window_get_child_widget (
-               MODEST_MAIN_WINDOW(win), MODEST_WIDGET_TYPE_HEADER_VIEW));
+               MODEST_MAIN_WINDOW(win), MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW));
        if (header_view && MODEST_IS_HEADER_VIEW (header_view))
                modest_header_view_refilter (header_view);
        
@@ -933,7 +921,7 @@ on_idle_open_default_inbox(gpointer user_data)
 
        /* Get the folder view */
        GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
-                                                          MODEST_WIDGET_TYPE_FOLDER_VIEW);
+                                                          MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
        modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
        
        gdk_threads_leave (); /* CHECKED */
@@ -1407,12 +1395,47 @@ add_folders_to_list (TnyFolderStore *folder_store, GList** list)
        g_object_unref (G_OBJECT (iter));
 }
 
+
+/* return >1 for a special folder, 0 for a user-folder */
+static gint
+get_rank (const gchar *folder)
+{
+       if (strcmp (folder, "INBOX") == 0)
+               return 1;
+       if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_SENT)) == 0)
+               return 2;
+       if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
+               return 3;
+       if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
+               return 4;
+       return 0;
+}
+
+static gint
+folder_name_compare_func (const gchar* folder1, const gchar* folder2)
+{
+       gint r1 = get_rank (folder1);
+       gint r2 = get_rank (folder2);
+
+       if (r1 > 0 && r2 > 0)
+               return r1 - r2;
+       if (r1 > 0 && r2 == 0)
+               return -1;
+       if (r1 == 0 && r2 > 0)
+               return 1;
+       else
+               return  modest_text_utils_utf8_strcmp (folder1, folder2, TRUE);
+}
+
+/* FIXME: */
+/*   - we're still missing the outbox */
+/*   - we need to take care of localization (urgh) */
+/*   - what about 'All mail folders'? */
 static void
 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
 {
        DBusMessage  *reply = NULL;
        
-
        /* Get the TnyStoreAccount so we can get the folders: */
        ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
        gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
@@ -1455,6 +1478,11 @@ on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
        g_object_unref (account_local);
        account_local = NULL;
 
+       /* specs require us to sort the folder names, although
+        * this is really not the place to do that...
+        */
+       folder_names = g_list_sort (folder_names,
+                                   (GCompareFunc)folder_name_compare_func);
 
        /* Put the result in a DBus reply: */
        reply = dbus_message_new_method_return (message);