* modest-folder-view.c: change sorting for remote folders: make Inbox be the first...
[modest] / src / modest-ui-actions.c
index e1ae608..be1d2a0 100644 (file)
@@ -121,6 +121,9 @@ static void     reply_forward_cb       (ModestMailOperation *mail_op,
 
 static void     reply_forward          (ReplyForwardAction action, ModestWindow *win);
 
+static void     folder_refreshed_cb    (ModestMailOperation *mail_op, 
+                                       TnyFolder *folder, 
+                                       gpointer user_data);
 
 static void     _on_send_receive_progress_changed (ModestMailOperation  *mail_op, 
                                                   ModestMailOperationState *state,
@@ -255,7 +258,7 @@ headers_action_delete (TnyHeader *header,
 }
 
 void
-modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win)
+modest_ui_actions_on_delete_message (GtkAction *action, ModestWindow *win)
 {
        TnyList *header_list = NULL;
        TnyIterator *iter = NULL;
@@ -364,6 +367,38 @@ modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win)
 }
 
 
+
+
+/* delete either message or folder, based on what has focus */
+void
+modest_ui_actions_on_delete_message_or_folder (GtkAction *action, ModestWindow *win)
+{
+       g_return_if_fail (MODEST_IS_WINDOW(win));
+       
+       /* Check first if the header view has the focus */
+       if (MODEST_IS_MAIN_WINDOW (win)) {
+               GtkWidget *w;
+
+               w = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
+                                                        MODEST_WIDGET_TYPE_HEADER_VIEW);
+               if (gtk_widget_is_focus (w)) {
+                       modest_ui_actions_on_delete_message (action, win);
+                       return;
+               }
+               
+               w = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
+                                                        MODEST_WIDGET_TYPE_FOLDER_VIEW);
+               if (gtk_widget_is_focus (w)) {
+                       modest_ui_actions_on_delete_folder (action, MODEST_MAIN_WINDOW(win));
+                       return;
+               }
+       }
+       g_warning ("BUG: delete toolbar button pressed with neither headerview nor folderview "
+                  "having focus");
+}
+
+
+
 void
 modest_ui_actions_on_quit (GtkAction *action, ModestWindow *win)
 {
@@ -1339,6 +1374,76 @@ modest_ui_actions_do_send_receive (const gchar *account_name, ModestWindow *win)
        g_free (acc_name);
 }
 
+
+static void
+modest_ui_actions_do_cancel_send (const gchar *account_name,  
+                                 ModestWindow *win)
+{
+       TnyTransportAccount *transport_account;
+       TnySendQueue *send_queue = NULL;
+       GError *error = NULL;
+
+       /* Get transport account */
+       transport_account =
+               TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_server_account
+                                     (modest_runtime_get_account_store(),
+                                      account_name,
+                                      TNY_ACCOUNT_TYPE_TRANSPORT));
+       if (!transport_account) {
+               g_printerr ("modest: no transport account found for '%s'\n", account_name);
+               goto frees;
+       }
+
+       /* Get send queue*/
+       send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
+       if (!TNY_IS_SEND_QUEUE(send_queue)) {
+               g_set_error (&error, MODEST_MAIL_OPERATION_ERROR,
+                            MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
+                            "modest: could not find send queue for account\n");
+       } else {
+               /* Keeep messages in outbox folder */
+               tny_send_queue_cancel (send_queue, FALSE, &error);
+       }       
+
+ frees:
+       if (transport_account != NULL) 
+               g_object_unref (G_OBJECT (transport_account));
+}
+
+static void
+modest_ui_actions_cancel_send_all (ModestWindow *win) 
+{
+       GSList *account_names, *iter;
+
+       account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(), 
+                                                         TRUE);
+
+       iter = account_names;
+       while (iter) {                  
+               modest_ui_actions_do_cancel_send ((const char*) iter->data, win);
+               iter = g_slist_next (iter);
+       }
+
+       modest_account_mgr_free_account_names (account_names);
+       account_names = NULL;
+}
+
+void
+modest_ui_actions_cancel_send (GtkAction *action,  ModestWindow *win)
+
+{
+       /* Check if accounts exist */
+       gboolean accounts_exist = 
+               modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE);
+       
+       /* If not, allow the user to create an account before trying to send/receive. */
+       if (!accounts_exist)
+               modest_ui_actions_on_accounts (NULL, win);
+       
+       /* Cancel all sending operaitons */     
+       modest_ui_actions_cancel_send_all (win);
+}
+
 /*
  * Refreshes all accounts. This function will be used by automatic
  * updates
@@ -1374,6 +1479,38 @@ modest_ui_actions_on_send_receive (GtkAction *action,  ModestWindow *win)
        /* If not, allow the user to create an account before trying to send/receive. */
        if (!accounts_exist)
                modest_ui_actions_on_accounts (NULL, win);
+
+       /* Refresh currently selected folder. Note that if we only
+          want to retrive the headers, then the refresh only will
+          invoke a poke_status over all folders, i.e., only the
+          total/unread count will be updated */
+       if (MODEST_IS_MAIN_WINDOW (win)) {
+               GtkWidget *header_view, *folder_view;
+               TnyFolderStore *folder_store;
+
+               /* Get folder and header view */
+               folder_view = 
+                       modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win), 
+                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
+
+               folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
+
+               if (TNY_IS_FOLDER (folder_store)) {
+                       header_view = 
+                               modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
+                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
+               
+                       /* We do not need to set the contents style
+                          because it hasn't changed. We also do not
+                          need to save the widget status. Just force
+                          a refresh */
+                       modest_header_view_set_folder (MODEST_HEADER_VIEW(header_view),
+                                                      TNY_FOLDER (folder_store),
+                                                      folder_refreshed_cb,
+                                                      MODEST_MAIN_WINDOW (win));
+               }
+               g_object_unref (folder_store);
+       }
        
        /* Refresh the active account */
        modest_ui_actions_do_send_receive (NULL, win);
@@ -1989,6 +2126,25 @@ modest_ui_actions_on_remove_attachments (GtkAction *action,
        modest_msg_edit_window_remove_attachments (window, NULL);
 }
 
+static void
+modest_ui_actions_new_folder_error_handler (ModestMailOperation *mail_op,
+                                            gpointer user_data)
+{
+       ModestMainWindow *window = MODEST_MAIN_WINDOW (user_data);
+
+       /* TODO: Note that folder creation might go wrong due to other
+        * failures such as when the parent folder is non-writable. We can
+        * query a GError* with modest_mail_operation_get_error(), but the
+        * the error code (from tinymail) does not give us a clue about what
+        * has gone wrong. We might use the error->message but it might come
+        * from camel and not be suitable to show to the user directly. */
+       modest_platform_information_banner (GTK_WIDGET (window), NULL,
+                                           _CS("ckdg_ib_folder_already_exists"));
+
+/*     modest_platform_information_banner (GTK_WIDGET (window), NULL,
+                                           modest_mail_operation_get_error (mail_op)->message);*/
+}
+
 void 
 modest_ui_actions_on_new_folder (GtkAction *action, ModestMainWindow *main_window)
 {
@@ -2016,14 +2172,20 @@ modest_ui_actions_on_new_folder (GtkAction *action, ModestMainWindow *main_windo
                                                                        suggested_name,
                                                                        &folder_name);
 
+                       g_free (suggested_name);
+                       suggested_name = NULL;
+
                        if (result == GTK_RESPONSE_REJECT) {
                                finished = TRUE;
                        } else {
                                ModestMailOperation *mail_op;
                                TnyFolder *new_folder = NULL;
 
-                               mail_op  = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_INFO, 
-                                                                     G_OBJECT(main_window));
+                               mail_op  = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_INFO,
+                                                                                         G_OBJECT(main_window),
+                                                                                         modest_ui_actions_new_folder_error_handler,
+                                                                                         main_window);
+
                                modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
                                                                 mail_op);
                                new_folder = modest_mail_operation_create_folder (mail_op,
@@ -2035,7 +2197,8 @@ modest_ui_actions_on_new_folder (GtkAction *action, ModestMainWindow *main_windo
                                }
                                g_object_unref (mail_op);
                        }
-                       g_free (folder_name);
+
+                       suggested_name = folder_name;
                        folder_name = NULL;
                }