* fix for compile breakage
[modest] / src / modest-ui-actions.c
index ccc4c83..57989ae 100644 (file)
@@ -93,8 +93,9 @@ guint reply_forward_type;
 
 typedef struct _HeaderActivatedHelper {
        GtkTreeModel *model;
-       GtkTreeIter iter;
+       GtkTreeRowReference *row_reference;
        TnyFolder *folder;
+       TnyHeader *header;
 } HeaderActivatedHelper;
 
 /*
@@ -174,6 +175,36 @@ get_selected_headers (ModestWindow *win)
 }
 
 static void
+headers_action_mark_as_read (TnyHeader *header,
+                            ModestWindow *win,
+                            gpointer user_data)
+{
+       TnyHeaderFlags flags;
+
+       g_return_if_fail (TNY_IS_HEADER(header));
+
+       flags = tny_header_get_flags (header);
+       if (flags & TNY_HEADER_FLAG_SEEN) return;
+       tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
+}
+
+static void
+headers_action_mark_as_unread (TnyHeader *header,
+                              ModestWindow *win,
+                              gpointer user_data)
+{
+       TnyHeaderFlags flags;
+
+       g_return_if_fail (TNY_IS_HEADER(header));
+
+       flags = tny_header_get_flags (header);
+       if (flags & TNY_HEADER_FLAG_SEEN)  {
+               tny_header_unset_flags (header, TNY_HEADER_FLAG_SEEN);
+       }
+}
+
+
+static void
 headers_action_delete (TnyHeader *header,
                       ModestWindow *win,
                       gpointer user_data)
@@ -422,9 +453,17 @@ open_msg_func (const GObject *obj, const TnyMsg *msg, gpointer user_data)
        
        g_return_if_fail (MODEST_IS_WINDOW(obj));
        g_return_if_fail (user_data != NULL);
+
+       /* TODO: Show an error? (review the specs) */
+       if (!msg)
+               return;
+
        parent_win = MODEST_WINDOW(obj);
        helper = (HeaderActivatedHelper *) user_data;
 
+       /* Mark header as read */
+       headers_action_mark_as_read (helper->header, MODEST_WINDOW(parent_win), NULL);
+
        /* Get account */
        account =  g_strdup(modest_window_get_active_account(MODEST_WINDOW(parent_win)));
        if (!account)
@@ -440,7 +479,7 @@ open_msg_func (const GObject *obj, const TnyMsg *msg, gpointer user_data)
                break;
        default:
                if (helper->model != NULL)
-                       win = modest_msg_view_window_new_with_header_model ((TnyMsg *) msg, account, helper->model, helper->iter);
+                       win = modest_msg_view_window_new_with_header_model ((TnyMsg *) msg, account, helper->model, helper->row_reference);
                else
                        win = modest_msg_view_window_new ((TnyMsg *) msg, account);
        }
@@ -457,6 +496,8 @@ open_msg_func (const GObject *obj, const TnyMsg *msg, gpointer user_data)
        g_free(account);
 /*     g_object_unref (G_OBJECT(msg)); */
        g_object_unref (G_OBJECT(helper->folder));
+       g_object_unref (G_OBJECT(helper->header));
+       gtk_tree_row_reference_free (helper->row_reference);
        g_slice_free (HeaderActivatedHelper, helper);
 }
 
@@ -700,72 +741,6 @@ modest_ui_actions_on_sort (GtkAction *action,
        }
 }
 
-
-static gboolean
-action_send (const gchar* account_name)
-{
-       TnyAccount *tny_account;
-       ModestTnySendQueue *send_queue;
-
-       g_return_val_if_fail (account_name, FALSE);
-
-       /* Get the transport account according to the open connection, 
-        * because the account might specify connection-specific SMTP servers.
-        */
-       tny_account = 
-               modest_tny_account_store_get_transport_account_for_open_connection (modest_runtime_get_account_store(),
-                                                                    account_name);
-       if (!tny_account) {
-               g_printerr ("modest: cannot get tny transport account for %s\n", account_name);
-               return FALSE;
-       }
-       
-       send_queue = modest_tny_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT(tny_account));
-       if (!send_queue) {
-               g_object_unref (G_OBJECT(tny_account));
-               g_printerr ("modest: cannot get send queue for %s\n", account_name);
-               return FALSE;
-       }
-       
-       modest_tny_send_queue_flush (send_queue);
-
-       g_object_unref (G_OBJECT(send_queue));
-       g_object_unref (G_OBJECT(tny_account));
-
-       return TRUE;
-}
-
-
-static gboolean
-action_receive (const gchar* account_name, 
-               ModestWindow *win)
-{
-       TnyAccount *tny_account;
-       ModestMailOperation *mail_op;
-
-       g_return_val_if_fail (account_name, FALSE);
-
-       tny_account = 
-               modest_tny_account_store_get_tny_account_by_account (modest_runtime_get_account_store(),
-                                                                    account_name,
-                                                                    TNY_ACCOUNT_TYPE_STORE);
-       if (!tny_account) {
-               g_printerr ("modest: cannot get tny store account for %s\n", account_name);
-               return FALSE;
-       }
-
-       /* Create the mail operation */
-       /* TODO: The spec wants us to first do any pending deletions, before receiving. */
-       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win));
-       modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
-       modest_mail_operation_update_account (mail_op, TNY_STORE_ACCOUNT(tny_account));
-
-       g_object_unref (G_OBJECT(tny_account));
-       g_object_unref (G_OBJECT (mail_op));
-               
-       return TRUE;
-}
-
 /** Check that an appropriate connection is open.
  */
 gboolean check_for_connection (const gchar *account_name)
@@ -822,10 +797,13 @@ modest_ui_actions_do_send_receive (const gchar *account_name, ModestWindow *win)
                 * for SMTP we should send,
                 * first receiving, then sending:
                 */
-               if (!action_receive(acc_name, win))
-                       g_printerr ("modest: failed to receive\n");
-               if (!action_send(acc_name))
-                       g_printerr ("modest: failed to send\n");
+               /* Create the mail operation */
+               /* TODO: The spec wants us to first do any pending deletions, before receiving. */
+               ModestMailOperation *mail_op;
+               mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win));
+               modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
+               modest_mail_operation_update_account (mail_op, acc_name);
+               g_object_unref (G_OBJECT (mail_op));
        }
        /* Free */
        g_free (acc_name);
@@ -938,7 +916,6 @@ modest_ui_actions_on_header_activated (ModestHeaderView *header_view,
        ModestWindowMgr *mgr = NULL;
        ModestWindow *win = NULL;
        GtkTreeModel *model = NULL;
-       GtkTreeIter iter;
        GtkTreeSelection *sel = NULL;
        GList *sel_list = NULL;
        
@@ -955,17 +932,17 @@ modest_ui_actions_on_header_activated (ModestHeaderView *header_view,
        /* Build helper */
        helper = g_slice_new0 (HeaderActivatedHelper);
        helper->folder = tny_header_get_folder (header);
+       helper->header = g_object_ref(header);
        helper->model = NULL;
 
-       /* Get headers tree model and selected iter to build message view */
+       /* Get headers tree model and selected row reference to build message view */
        sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
        sel_list = gtk_tree_selection_get_selected_rows (sel, &model);
        if (sel_list != NULL) {
-               gtk_tree_model_get_iter (model, &iter, (GtkTreePath *) sel_list->data);
                
                /* Fill helpers */
                helper->model = model;
-               helper->iter = iter;
+               helper->row_reference = gtk_tree_row_reference_new (model, (GtkTreePath *) sel_list->data);
 
                g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL);
                g_list_free (sel_list);
@@ -980,6 +957,26 @@ modest_ui_actions_on_header_activated (ModestHeaderView *header_view,
        g_object_unref (mail_op);
 }
 
+static void
+set_active_account_from_tny_account (TnyAccount *account,
+                                    ModestWindow *window)
+{
+       TnyAccount *modest_server_account;
+       const gchar *server_acc_name;
+       gchar *modest_acc_name;
+
+       server_acc_name = tny_account_get_id (account);
+       /* We need the TnyAccount provided by the
+          account store because that is the one that
+          knows the name of the Modest account */
+       modest_server_account = 
+               modest_tny_account_store_get_tny_account_by_id  (modest_runtime_get_account_store (), 
+                                                                server_acc_name);
+       modest_acc_name = (gchar *) g_object_get_data (G_OBJECT (modest_server_account), "modest_account");
+       modest_window_set_active_account (window, modest_acc_name);
+       g_object_unref (modest_server_account);
+}
+
 void 
 modest_ui_actions_on_folder_selection_changed (ModestFolderView *folder_view,
                                               TnyFolderStore *folder_store, 
@@ -988,6 +985,7 @@ modest_ui_actions_on_folder_selection_changed (ModestFolderView *folder_view,
 {
        ModestConf *conf;
        GtkWidget *header_view;
+       TnyAccount *account;
        
        g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
 
@@ -1001,6 +999,12 @@ modest_ui_actions_on_folder_selection_changed (ModestFolderView *folder_view,
        if (TNY_IS_FOLDER (folder_store)) {
 
                if (selected) {
+                       /* Update the active account */
+                       account = tny_folder_get_account (TNY_FOLDER (folder_store));
+                       set_active_account_from_tny_account (account, MODEST_WINDOW (main_window));
+                       g_object_unref (account);
+
+                       /* Set folder on header view */
                        modest_main_window_set_contents_style (main_window, 
                                                               MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS);
                        modest_header_view_set_folder (MODEST_HEADER_VIEW(header_view),
@@ -1008,11 +1012,16 @@ modest_ui_actions_on_folder_selection_changed (ModestFolderView *folder_view,
                        modest_widget_memory_restore (conf, G_OBJECT(header_view),
                                                      MODEST_CONF_HEADER_VIEW_KEY);
                } else {
+                       /* Update the active account */
+                       modest_window_set_active_account (MODEST_WINDOW (main_window), NULL);
+                       /* Do not show folder */
                        modest_widget_memory_save (conf, G_OBJECT (header_view), MODEST_CONF_HEADER_VIEW_KEY);
                        modest_header_view_set_folder (MODEST_HEADER_VIEW(header_view), NULL);
                }
        } else if (TNY_IS_ACCOUNT (folder_store)) {
-
+               /* Update active account */
+               set_active_account_from_tny_account (TNY_ACCOUNT (folder_store), MODEST_WINDOW (main_window));
+               /* Show account details */
                modest_main_window_set_contents_style (main_window, MODEST_MAIN_WINDOW_CONTENTS_STYLE_DETAILS);
        }
 }
@@ -1464,7 +1473,7 @@ modest_ui_actions_on_new_folder (GtkAction *action, ModestMainWindow *main_windo
 /* /\*                                         suggested_name = X; *\/ */
 /*                                     /\* Show error to the user *\/ */
 /*                                     modest_platform_run_information_dialog (GTK_WINDOW (main_window), */
-/*                                                                             MODEST_INFORMATION_CREATE_FOLDER); */
+/*                                                                             _("mail_in_ui_folder_create_error")); */
 /*                             } */
                                g_object_unref (mail_op);
                        }
@@ -1532,6 +1541,13 @@ delete_folder (ModestMainWindow *main_window, gboolean move_to_trash)
 
        folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
 
+       /* Show an error if it's an account */
+       if (!TNY_IS_FOLDER (folder)) {
+               modest_platform_run_information_dialog (GTK_WINDOW (main_window),
+                                                       _("mail_in_ui_folder_delete_error"));
+               return ;
+       }
+
        /* Ask the user */      
        message =  g_strdup_printf (_("mcen_nc_delete_folder_text"), 
                                    tny_folder_get_name (TNY_FOLDER (folder)));
@@ -1549,7 +1565,7 @@ delete_folder (ModestMainWindow *main_window, gboolean move_to_trash)
                /* Show error if happened */
                if (modest_mail_operation_get_error (mail_op))
                        modest_platform_run_information_dialog (GTK_WINDOW (main_window),
-                                                               MODEST_INFORMATION_DELETE_FOLDER);
+                                                               _("mail_in_ui_folder_delete_error"));
 
                g_object_unref (G_OBJECT (mail_op));
        }
@@ -1838,6 +1854,26 @@ modest_ui_actions_on_select_all (GtkAction *action,
 }
 
 void
+modest_ui_actions_on_mark_as_read (GtkAction *action,
+                                  ModestWindow *window)
+{      
+       g_return_if_fail (MODEST_IS_WINDOW(window));
+               
+       /* Mark each header as read */
+       do_headers_action (window, headers_action_mark_as_read, NULL);
+}
+
+void
+modest_ui_actions_on_mark_as_unread (GtkAction *action,
+                                    ModestWindow *window)
+{      
+       g_return_if_fail (MODEST_IS_WINDOW(window));
+               
+       /* Mark each header as read */
+       do_headers_action (window, headers_action_mark_as_unread, NULL);
+}
+
+void
 modest_ui_actions_on_change_zoom (GtkRadioAction *action,
                                  GtkRadioAction *selected,
                                  ModestWindow *window)
@@ -2469,17 +2505,12 @@ modest_ui_actions_on_settings (GtkAction *action,
                               ModestWindow *win)
 {
        GtkWidget *dialog;
-       gint response;
 
        dialog = modest_platform_get_global_settings_dialog ();
        gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (win));
        gtk_widget_show (dialog);
 
-       response = gtk_dialog_run (GTK_DIALOG (dialog));
-
-       if (response == GTK_RESPONSE_ACCEPT) {
-               g_message ("YES");
-       }
+       gtk_dialog_run (GTK_DIALOG (dialog));
 
        gtk_widget_destroy (dialog);
 }