Fixes NB#137624, crash when creating a new folder in offline mode
[modest] / src / modest-ui-actions.c
index d4353b3..8e4185b 100644 (file)
@@ -700,12 +700,8 @@ modest_ui_actions_on_add_to_contacts (GtkAction *action, ModestWindow *win)
        clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
        selection = gtk_clipboard_wait_for_text (clipboard);
 
-       /* Question: why is the clipboard being used here?
-        * It doesn't really make a lot of sense. */
-
-       if (selection)
-       {
-               modest_address_book_add_address (selection);
+       if (selection) {
+               modest_address_book_add_address (selection, (GtkWindow *) win);
                g_free (selection);
        }
 }
@@ -994,19 +990,27 @@ modest_ui_actions_msg_retrieval_check (ModestMailOperation *mail_op,
                              error->code == TNY_SERVICE_ERROR_MESSAGE_NOT_AVAILABLE)) {
                        gchar *subject, *msg, *format = NULL;
                        TnyAccount *account;
-                       
-                       subject = header?tny_header_dup_subject (header):NULL;
+
+                       subject = (header) ? tny_header_dup_subject (header) : NULL;
                        if (!subject)
                                subject = g_strdup (_("mail_va_no_subject"));
 
                        account = modest_mail_operation_get_account (mail_op);
                        if (account) {
-                               ModestProtocol *protocol;
-                               ModestProtocolType proto;
-                               proto = modest_tny_account_get_protocol_type (account);
-                               protocol = modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (), proto);
-                               if (protocol)
-                                 format = modest_protocol_get_translation (protocol, MODEST_PROTOCOL_TRANSLATION_MSG_NOT_AVAILABLE, subject);
+                               ModestProtocolType proto = modest_tny_account_get_protocol_type (account);
+                               ModestProtocol *protocol = modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (), proto);
+
+                               if (protocol) {
+                                       if (tny_account_get_connection_status (account) ==
+                                           TNY_CONNECTION_STATUS_CONNECTED) {
+                                               format = modest_protocol_get_translation (protocol,
+                                                                                         MODEST_PROTOCOL_TRANSLATION_MSG_NOT_AVAILABLE,
+                                                                                         subject);
+                                       } else {
+                                               format = g_strdup_printf (_("mail_ib_backend_server_invalid"),
+                                                                         tny_account_get_hostname (account));
+                                       }
+                               }
                                g_object_unref (account);
                        }
 
@@ -1540,7 +1544,7 @@ open_msg_from_header (TnyHeader *header, GtkTreeRowReference *rowref, ModestWind
        OpenMsgHelper *helper;
        ModestWindow *window;
 
-       g_return_if_fail (header != NULL && rowref != NULL);
+       g_return_if_fail (header != NULL && rowref != NULL && gtk_tree_row_reference_valid (rowref));
 
        mgr = modest_runtime_get_window_mgr ();
 
@@ -1924,7 +1928,7 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
        ReplyForwardHelper *rf_helper = NULL;
        guint reply_forward_type;
 
-       g_return_if_fail (MODEST_IS_WINDOW(win));
+       g_return_if_fail (win && MODEST_IS_WINDOW(win));
 
        /* we check for low-mem; in that case, show a warning, and don't allow
         * reply/forward (because it could potentially require a lot of memory */
@@ -2044,7 +2048,7 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
                        if (folder)
                                g_object_unref (folder);
                } else {
-                       reply_forward_cb (NULL, header, FALSE, NULL, NULL, rf_helper);
+                       reply_forward_cb (NULL, header, FALSE, NULL, NULL, NULL);
                }
                /* Frees */
                g_object_unref (header_list);
@@ -2147,18 +2151,50 @@ modest_ui_actions_on_sort (GtkAction *action,
        modest_utils_run_sort_dialog (GTK_WINDOW (window), MODEST_SORT_HEADERS);
 }
 
+static gboolean
+idle_refresh_folder (gpointer source)
+{
+       ModestHeaderView *header_view = NULL;
+
+       /* If the window still exists */
+       if (!GTK_IS_WIDGET (source) ||
+           !GTK_WIDGET_VISIBLE (source))
+               return FALSE;
+
+       /* Refresh the current view */
+#ifdef MODEST_TOOLKIT_HILDON2
+       if (MODEST_IS_HEADER_WINDOW (source))
+               header_view = modest_header_window_get_header_view ((ModestHeaderWindow *) source);
+#else
+       if (MODEST_IS_MAIN_WINDOW (source))
+               header_view = modest_main_window_get_child_widget ((ModestMainWindow *) source,
+                                                                  MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
+#endif
+       if (header_view) {
+               TnyFolder *folder = modest_header_view_get_folder (header_view);
+               if (folder) {
+                       /* We must clear first, because otherwise set_folder will ignore
+                          the change as the folders are the same */
+                       modest_header_view_clear (header_view);
+                       modest_header_view_set_folder (header_view, folder, TRUE,
+                                                      (ModestWindow *) source, NULL, NULL);
+                       g_object_unref (folder);
+               }
+       }
+
+       return FALSE;
+}
+
 static void
-new_messages_arrived (ModestMailOperation *self,
-                     TnyList *new_headers,
-                     gpointer user_data)
+update_account_cb (ModestMailOperation *self,
+                  TnyList *new_headers,
+                  gpointer user_data)
 {
        GObject *source;
        gboolean show_visual_notifications;
 
        source = modest_mail_operation_get_source (self);
        show_visual_notifications = (source) ? FALSE : TRUE;
-       if (source)
-               g_object_unref (source);
 
        /* Notify new messages have been downloaded. If the
           send&receive was invoked by the user then do not show any
@@ -2179,7 +2215,12 @@ new_messages_arrived (ModestMailOperation *self,
                        flags = tny_header_get_flags (header);
 
                        if (!(flags & TNY_HEADER_FLAG_SEEN)) {
-                               tny_list_append (actually_new_list, G_OBJECT (header));
+                               /* Messages are ordered from most
+                                  recent to oldest. But we want to
+                                  show notifications starting from
+                                  the oldest message. That's why we
+                                  reverse the list */
+                               tny_list_prepend (actually_new_list, G_OBJECT (header));
                        }
                        g_object_unref (header);
                }
@@ -2201,29 +2242,16 @@ new_messages_arrived (ModestMailOperation *self,
                g_object_unref (actually_new_list);
        }
 
-}
-
-gboolean
-retrieve_all_messages_cb (GObject *source,
-                         guint num_msgs,
-                         guint retrieve_limit)
-{
-       GtkWindow *window;
-       gchar *msg;
-       gint response;
-
-       window = GTK_WINDOW (source);
-       msg = g_strdup_printf (_("mail_nc_msg_count_limit_exceeded"),
-                              num_msgs, retrieve_limit);
-
-       /* Ask the user if they want to retrieve all the messages */
-       response =
-               modest_platform_run_confirmation_dialog_with_buttons (window, msg,
-                                                                     _("mcen_bd_get_all"),
-                                                                     _("mcen_bd_newest_only"));
-       /* Free and return */
-       g_free (msg);
-       return (response == GTK_RESPONSE_ACCEPT) ? TRUE : FALSE;
+       if (source) {
+               /* Refresh the current folder in an idle. We do this
+                  in order to avoid refresh cancelations if the
+                  currently viewed folder is the inbox */
+               g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
+                                idle_refresh_folder,
+                                g_object_ref (source),
+                                g_object_unref);
+               g_object_unref (source);
+       }
 }
 
 typedef struct {
@@ -2271,8 +2299,7 @@ do_send_receive_performer (gboolean canceled,
 
        /* Send & receive. */
        modest_mail_operation_update_account (info->mail_op, info->account_name, info->poke_status, info->interactive,
-                                             (info->win) ? retrieve_all_messages_cb : NULL,
-                                             new_messages_arrived, info->win);
+                                             update_account_cb, info->win);
 
  clean:
        /* Frees */
@@ -3462,7 +3489,7 @@ do_create_folder_performer (gboolean canceled,
 
                /* This happens if we have selected the outbox folder
                   as the parent */
-               if (err->code == TNY_SERVICE_ERROR_UNKNOWN &&
+               if (err && err->code == TNY_SERVICE_ERROR_UNKNOWN &&
                    TNY_IS_MERGE_FOLDER (helper->parent)) {
                        /* Show an error and retry */
                        modest_platform_information_banner ((GtkWidget *) parent_window,
@@ -3837,8 +3864,8 @@ on_delete_folder_cb (gboolean canceled,
 
        modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
 
-       g_object_unref (G_OBJECT (mail_op));
-       g_object_unref (G_OBJECT (info->folder));
+       g_object_unref (mail_op);
+       g_object_unref (info->folder);
        g_free (info);
 }
 
@@ -3888,22 +3915,23 @@ delete_folder (ModestWindow *window, gboolean move_to_trash)
        g_free (message);
 
        if (response == GTK_RESPONSE_OK) {
-               DeleteFolderInfo *info;
+               TnyAccount *account = NULL;
+               DeleteFolderInfo *info = NULL;
                info = g_new0(DeleteFolderInfo, 1);
-               info->folder = folder;
+               info->folder = g_object_ref (folder);
                info->move_to_trash = move_to_trash;
-               g_object_ref (G_OBJECT (info->folder));
-               TnyAccount *account = tny_folder_get_account (TNY_FOLDER (folder));
+
+               account = tny_folder_get_account (TNY_FOLDER (folder));
                modest_platform_connect_if_remote_and_perform (GTK_WINDOW (window),
                                                               TRUE,
                                                               TNY_FOLDER_STORE (account),
                                                               on_delete_folder_cb, info);
                g_object_unref (account);
+               g_object_unref (folder);
                return TRUE;
        } else {
                return FALSE;
        }
-       g_object_unref (G_OBJECT (folder));
 }
 
 void
@@ -4816,6 +4844,16 @@ modest_ui_actions_on_details (GtkAction *action,
 }
 
 void
+modest_ui_actions_on_limit_error (GtkAction *action,
+                                 ModestWindow *win)
+{
+       g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win));
+
+       modest_platform_information_banner ((GtkWidget *) win, NULL, _CS("ckdg_ib_maximum_characters_reached"));
+
+}
+
+void
 modest_ui_actions_on_toggle_show_cc (GtkToggleAction *toggle,
                                     ModestMsgEditWindow *window)
 {
@@ -5072,7 +5110,6 @@ on_move_to_dialog_response (GtkDialog *dialog,
        parent_win = (GtkWidget *) helper->win;
        folder_view = MODEST_FOLDER_VIEW (g_object_get_data (G_OBJECT (dialog),
                                                             MODEST_MOVE_TO_DIALOG_FOLDER_VIEW));
-
        switch (response) {
                TnyFolderStore *dst_folder;
                TnyFolderStore *selected;
@@ -5190,8 +5227,8 @@ create_move_to_dialog (GtkWindow *win,
 
                modest_folder_view_set_style (MODEST_FOLDER_VIEW (tree_view),
                                              MODEST_FOLDER_VIEW_STYLE_SHOW_ALL);
-               modest_folder_view_update_model (MODEST_FOLDER_VIEW (tree_view),
-                                                TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
+               /* modest_folder_view_update_model (MODEST_FOLDER_VIEW (tree_view), */
+               /*                               TNY_ACCOUNT_STORE (modest_runtime_get_account_store ())); */
 
                active_account_name = modest_window_get_active_account (MODEST_WINDOW (win));
                mgr = modest_runtime_get_account_mgr ();
@@ -5664,20 +5701,20 @@ xfer_messages_error_handler (ModestMailOperation *mail_op,
 
        win = modest_mail_operation_get_source (mail_op);
        error = modest_mail_operation_get_error (mail_op);
-       account = modest_mail_operation_get_account (mail_op);
 
-       if (error && modest_tny_account_store_is_disk_full_error (modest_runtime_get_account_store(),
-                                                                 (GError *) error, account)) {
-               gchar *msg = g_strdup_printf (_KR("cerm_device_memory_full"), "");
-               modest_platform_information_banner ((GtkWidget *) win, NULL, msg);
-               g_free (msg);
-       } else {
+       /* We cannot get the account from the mail op as that is the
+          source account and for checking memory full conditions we
+          need the destination one */
+       account = TNY_ACCOUNT (user_data);
+
+       if (error &&
+           !modest_tny_account_store_check_disk_full_error (modest_runtime_get_account_store(),
+                                                            (GtkWidget *) win, (GError*) error,
+                                                            account, _KR("cerm_memory_card_full"))) {
                modest_platform_run_information_dialog ((GtkWindow *) win,
                                                        _("mail_in_ui_folder_move_target_error"),
                                                        FALSE);
        }
-       if (account)
-               g_object_unref (account);
        if (win)
                g_object_unref (win);
 }
@@ -5722,8 +5759,7 @@ xfer_messages_performer  (gboolean canceled,
        /* tinymail will return NULL for local folders it seems */
        dst_forbids_message_add = modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
                                                                                  modest_tny_account_get_protocol_type (dst_account),
-                                                                                 MODEST_PROTOCOL_REGISTRY_STORE_FORBID_MESSAGE_ADD);
-       g_object_unref (dst_account);
+                                                                                 MODEST_PROTOCOL_REGISTRY_STORE_FORBID_INCOMING_XFERS);
 
        if (dst_forbids_message_add) {
                modest_platform_information_banner (GTK_WIDGET (win),
@@ -5755,7 +5791,8 @@ xfer_messages_performer  (gboolean canceled,
        /* Perform the mail operation */
        mail_op = modest_mail_operation_new_with_error_handling (G_OBJECT(win),
                                                                 xfer_messages_error_handler,
-                                                                movehelper, NULL);
+                                                                g_object_ref (dst_account),
+                                                                g_object_unref);
        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
                                         mail_op);
 
@@ -5768,6 +5805,8 @@ xfer_messages_performer  (gboolean canceled,
 
        g_object_unref (G_OBJECT (mail_op));
  end:
+       if (dst_account)
+               g_object_unref (dst_account);
        g_object_unref (helper->dst_folder);
        g_object_unref (helper->headers);
        g_slice_free (XferMsgsHelper, helper);