* Use the correct translation string instead of a fixed one when
[modest] / src / maemo / modest-msg-view-window.c
index 87ea173..14deee8 100644 (file)
@@ -53,6 +53,7 @@
 #include <gtkhtml/gtkhtml-search.h>
 #include "modest-ui-dimming-manager.h"
 #include <gdk/gdkkeysyms.h>
+#include <modest-tny-account.h>
 
 #define DEFAULT_FOLDER "MyDocs/.documents"
 
@@ -66,8 +67,9 @@ static void  modest_msg_view_window_find_toolbar_close (GtkWidget *widget,
 static void  modest_msg_view_window_find_toolbar_search (GtkWidget *widget,
                                                        ModestMsgViewWindow *obj);
 
-static void  modest_msg_view_window_set_zoom (ModestWindow *window,
-                                             gdouble zoom);
+static void modest_msg_view_window_disconnect_signals (ModestWindow *self);
+static void modest_msg_view_window_set_zoom (ModestWindow *window,
+                                            gdouble zoom);
 static gdouble modest_msg_view_window_get_zoom (ModestWindow *window);
 static gboolean modest_msg_view_window_zoom_minus (ModestWindow *window);
 static gboolean modest_msg_view_window_zoom_plus (ModestWindow *window);
@@ -88,23 +90,30 @@ static void modest_msg_view_window_clipboard_owner_change (GtkClipboard *clipboa
                                                           GdkEvent *event,
                                                           ModestMsgViewWindow *window);
 
-static void cancel_progressbar (GtkToolButton *toolbutton,
-                               ModestMsgViewWindow *self);
+static void cancel_progressbar  (GtkToolButton *toolbutton,
+                                ModestMsgViewWindow *self);
 
-static void         on_queue_changed                     (ModestMailOperationQueue *queue,
-                                                         ModestMailOperation *mail_op,
-                                                         ModestMailOperationQueueNotification type,
-                                                         ModestMsgViewWindow *self);
+static void on_queue_changed    (ModestMailOperationQueue *queue,
+                                ModestMailOperation *mail_op,
+                                ModestMailOperationQueueNotification type,
+                                ModestMsgViewWindow *self);
 
-static void view_msg_cb (ModestMailOperation *mail_op, TnyHeader *header, TnyMsg *msg, gpointer user_data);
+static void on_account_removed  (TnyAccountStore *account_store, 
+                                TnyAccount *account,
+                                gpointer user_data);
 
-static void set_toolbar_mode (ModestMsgViewWindow *self, 
-                             ModestToolBarModes mode);
+static void view_msg_cb         (ModestMailOperation *mail_op, 
+                                TnyHeader *header, 
+                                TnyMsg *msg, 
+                                gpointer user_data);
 
-static gboolean set_toolbar_transfer_mode     (ModestMsgViewWindow *self); 
+static void set_toolbar_mode    (ModestMsgViewWindow *self, 
+                                ModestToolBarModes mode);
 
 static void update_window_title (ModestMsgViewWindow *window);
 
+static gboolean set_toolbar_transfer_mode     (ModestMsgViewWindow *self); 
+
 
 /* list my signals */
 enum {
@@ -148,12 +157,17 @@ struct _ModestMsgViewWindowPrivate {
        /* Optimized view enabled */
        gboolean optimized_view;
 
+       /* A reference to the @model of the header view 
+        * to allow selecting previous/next messages,
+        * if the message is currently selected in the header view.
+        */
        GtkTreeModel *header_model;
        GtkTreeRowReference *row_reference;
        GtkTreeRowReference *next_row_reference;
 
        guint clipboard_change_handler;
        guint queue_change_handler;
+       guint account_removed_handler;
 
        guint progress_bar_timeout;
 
@@ -226,6 +240,7 @@ modest_msg_view_window_class_init (ModestMsgViewWindowClass *klass)
        modest_window_class->zoom_minus_func = modest_msg_view_window_zoom_minus;
        modest_window_class->zoom_plus_func = modest_msg_view_window_zoom_plus;
        modest_window_class->show_toolbar_func = modest_msg_view_window_show_toolbar;
+       modest_window_class->disconnect_signals_func = modest_msg_view_window_disconnect_signals;
 
        g_type_class_add_private (gobject_class, sizeof(ModestMsgViewWindowPrivate));
 
@@ -250,6 +265,8 @@ modest_msg_view_window_init (ModestMsgViewWindow *obj)
        priv->msg_view      = NULL;
        priv->header_model  = NULL;
        priv->clipboard_change_handler = 0;
+       priv->queue_change_handler = 0;
+       priv->account_removed_handler = 0;
        priv->current_toolbar_mode = TOOLBAR_MODE_NORMAL;
 
        priv->optimized_view  = FALSE;
@@ -420,16 +437,37 @@ init_window (ModestMsgViewWindow *obj, TnyMsg *msg)
        gtk_container_add   (GTK_CONTAINER(obj), main_vbox);
 
        priv->find_toolbar = hildon_find_toolbar_new (NULL);
+       hildon_window_add_toolbar (HILDON_WINDOW (obj), GTK_TOOLBAR (priv->find_toolbar));
        gtk_widget_set_no_show_all (priv->find_toolbar, TRUE);
        g_signal_connect (G_OBJECT (priv->find_toolbar), "close", G_CALLBACK (modest_msg_view_window_find_toolbar_close), obj);
        g_signal_connect (G_OBJECT (priv->find_toolbar), "search", G_CALLBACK (modest_msg_view_window_find_toolbar_search), obj);
        
        priv->clipboard_change_handler = g_signal_connect (G_OBJECT (gtk_clipboard_get (GDK_SELECTION_PRIMARY)), "owner-change", G_CALLBACK (modest_msg_view_window_clipboard_owner_change), obj);
        gtk_widget_show_all (GTK_WIDGET(main_vbox));
-       gtk_box_pack_end (GTK_BOX (main_vbox), priv->find_toolbar, FALSE, FALSE, 0);
+}
 
-}      
+static void
+modest_msg_view_window_disconnect_signals (ModestWindow *self)
+{
+       ModestMsgViewWindowPrivate *priv;
+
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
 
+       if (g_signal_handler_is_connected (gtk_clipboard_get (GDK_SELECTION_PRIMARY),
+                                          priv->clipboard_change_handler)) 
+               g_signal_handler_disconnect (gtk_clipboard_get (GDK_SELECTION_PRIMARY), 
+                                            priv->clipboard_change_handler);
+
+       if (g_signal_handler_is_connected (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
+                                          priv->queue_change_handler))
+               g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
+                                            priv->queue_change_handler);
+
+       if (g_signal_handler_is_connected (G_OBJECT (modest_runtime_get_account_store ()), 
+                                          priv->account_removed_handler))
+               g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_account_store ()), 
+                                            priv->account_removed_handler);
+}      
 
 static void
 modest_msg_view_window_finalize (GObject *obj)
@@ -437,21 +475,16 @@ modest_msg_view_window_finalize (GObject *obj)
        ModestMsgViewWindowPrivate *priv;
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (obj);
-       if (priv->clipboard_change_handler > 0) {
-               g_signal_handler_disconnect (gtk_clipboard_get (GDK_SELECTION_PRIMARY), priv->clipboard_change_handler);
-               priv->clipboard_change_handler = 0;
-       }
-       if (priv->queue_change_handler > 0) {
-               g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()), priv->queue_change_handler);
-               priv->queue_change_handler = 0;
-       }
+
+       /* Sanity check: shouldn't be needed, the window mgr should
+          call this function before */
+       modest_msg_view_window_disconnect_signals (MODEST_WINDOW (obj));
+
        if (priv->header_model != NULL) {
                g_object_unref (priv->header_model);
                priv->header_model = NULL;
        }
 
-       /* disconnet operations queue observer */
-       
        if (priv->progress_bar_timeout > 0) {
                g_source_remove (priv->progress_bar_timeout);
                priv->progress_bar_timeout = 0;
@@ -513,7 +546,7 @@ select_next_valid_row (GtkTreeModel *model,
 
 ModestWindow *
 modest_msg_view_window_new_with_header_model (TnyMsg *msg, 
-                                             const gchar *account_name,
+                                             const gchar *modest_account_name,
                                              const gchar *msg_uid,
                                              GtkTreeModel *model, 
                                              GtkTreeRowReference *row_reference)
@@ -521,7 +554,7 @@ modest_msg_view_window_new_with_header_model (TnyMsg *msg,
        ModestMsgViewWindow *window = NULL;
        ModestMsgViewWindowPrivate *priv = NULL;
 
-       window = MODEST_MSG_VIEW_WINDOW(modest_msg_view_window_new (msg, account_name, msg_uid));
+       window = MODEST_MSG_VIEW_WINDOW(modest_msg_view_window_new (msg, modest_account_name, msg_uid));
        g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window), NULL);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
@@ -543,7 +576,7 @@ modest_msg_view_window_new_with_header_model (TnyMsg *msg,
 
 ModestWindow *
 modest_msg_view_window_new (TnyMsg *msg, 
-                           const gchar *account_name,
+                           const gchar *modest_account_name,
                            const gchar *msg_uid)
 {
        ModestMsgViewWindow *self = NULL;
@@ -665,7 +698,13 @@ modest_msg_view_window_new (TnyMsg *msg,
                                                       G_CALLBACK (on_queue_changed),
                                                       obj);
 
-       modest_window_set_active_account (MODEST_WINDOW(obj), account_name);
+       /* Account manager */
+       priv->account_removed_handler = g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),
+                                                         "account_removed",
+                                                         G_CALLBACK(on_account_removed),
+                                                         obj);
+
+       modest_window_set_active_account (MODEST_WINDOW(obj), modest_account_name);
 
        priv->last_search = NULL;
 
@@ -704,7 +743,9 @@ modest_msg_view_window_get_header (ModestMsgViewWindow *self)
        g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), NULL);
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
 
-       /* Message is not obtained from a treemodel (Attachment ?) */
+       /* If the message ws not obtained from a treemodel,
+        * for instance if it was opened directly by the search UI:
+        */
        if (priv->header_model == NULL) {
                msg = modest_msg_view_window_get_message (self);
                header = tny_msg_get_header (msg);
@@ -712,7 +753,11 @@ modest_msg_view_window_get_header (ModestMsgViewWindow *self)
                return header;
        }
 
-       /* Get current message iter */
+       /* Get iter of the currently selected message in the header view: */
+       /* TODO: Why not just give this window a ref of the TnyHeader or TnyMessage,
+        * instead of sometimes retrieving it from the header view?
+        * Then we wouldn't be dependent on the message actually still being selected 
+        * in the header view. murrayc. */
        path = gtk_tree_row_reference_get_path (priv->row_reference);
        g_return_val_if_fail (path != NULL, NULL);
        gtk_tree_model_get_iter (priv->header_model, 
@@ -755,28 +800,6 @@ modest_msg_view_window_get_message_uid (ModestMsgViewWindow *self)
        return (const gchar*) priv->msg_uid;
 }
 
-static void
-toggle_action_set_active_block_notify (GtkToggleAction *action,
-                                      gboolean value)
-{
-       GSList *proxies = NULL;
-
-       for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
-            proxies != NULL; proxies = g_slist_next (proxies)) {
-               GtkWidget *widget = (GtkWidget *) proxies->data;
-               gtk_action_block_activate_from (GTK_ACTION (action), widget);
-       }
-
-       gtk_toggle_action_set_active (action, value);
-
-       for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
-            proxies != NULL; proxies = g_slist_next (proxies)) {
-               GtkWidget *widget = (GtkWidget *) proxies->data;
-               gtk_action_unblock_activate_from (GTK_ACTION (action), widget);
-       }
-}
-
-
 static void 
 modest_msg_view_window_toggle_find_toolbar (GtkToggleAction *toggle,
                                            gpointer data)
@@ -798,9 +821,9 @@ modest_msg_view_window_toggle_find_toolbar (GtkToggleAction *toggle,
 
        /* update the toggle buttons status */
        action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/FindInMessage");
-       toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), is_active);
+       modest_maemo_toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), is_active);
        action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ToolsMenu/ToolsFindInMessageMenu");
-       toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), is_active);
+       modest_maemo_toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), is_active);
        
 }
 
@@ -823,6 +846,11 @@ modest_msg_view_window_find_toolbar_search (GtkWidget *widget,
        gchar *current_search;
        ModestMsgViewWindowPrivate *priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (obj);
 
+       if (modest_msg_view_get_message_is_empty (MODEST_MSG_VIEW (priv->msg_view))) {
+               hildon_banner_show_information (NULL, NULL, _("mail_ib_nothing_to_find"));
+               return;
+       }
+
        g_object_get (G_OBJECT (widget), "prefix", &current_search, NULL);
 
        if ((current_search == NULL) || (strcmp (current_search, "") == 0)) {
@@ -997,7 +1025,7 @@ modest_msg_view_window_last_message_selected (ModestMsgViewWindow *window)
                                break;
                        gtk_tree_model_get (priv->header_model, &tmp_iter, TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
                                            &header, -1);
-                       if (!(tny_header_get_flags(header)&TNY_HEADER_FLAG_DELETED)) {
+                       if (!(tny_header_get_flags(header) & TNY_HEADER_FLAG_DELETED)) {
                                has_next = TRUE;
                                break;
                        }       
@@ -1091,11 +1119,25 @@ static gboolean
 message_reader (ModestMsgViewWindow *window,
                ModestMsgViewWindowPrivate *priv,
                TnyHeader *header,
-               GtkTreeIter iter)
+               GtkTreePath *path)
 {
        ModestMailOperation *mail_op = NULL;
-       GtkTreePath *path = NULL;
        ModestMailOperationTypeOperation op_type;
+       gboolean already_showing = FALSE;
+       ModestWindow *msg_window = NULL;
+       ModestWindowMgr *mgr;
+
+       g_return_val_if_fail (path != NULL, FALSE);
+
+       mgr = modest_runtime_get_window_mgr ();
+       already_showing = modest_window_mgr_find_registered_header (mgr, header, &msg_window);
+       if (already_showing && (msg_window != MODEST_WINDOW (window))) {
+               gboolean retval;
+               if (msg_window)
+                       gtk_window_present (GTK_WINDOW (msg_window));
+               g_signal_emit_by_name (G_OBJECT (window), "delete-event", NULL, &retval);
+               return TRUE;
+       }
 
        /* Msg download completed */
        if (tny_header_get_flags (header) & TNY_HEADER_FLAG_CACHED) {
@@ -1126,9 +1168,6 @@ message_reader (ModestMsgViewWindow *window,
                }
        }
 
-       /* Get the path, will be freed by the callback */
-       path = gtk_tree_model_get_path (priv->header_model, &iter);
-
        /* New mail operation */
        mail_op = modest_mail_operation_new_with_error_handling (op_type, 
                                                                 G_OBJECT(window),
@@ -1157,8 +1196,21 @@ modest_msg_view_window_select_next_message (ModestMsgViewWindow *window)
        g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window), FALSE);
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
 
-       path = gtk_tree_row_reference_get_path (priv->next_row_reference);
-       if (path == NULL) 
+       /* Update the next row reference if it's not valid. This could
+          happen if for example the header which it was pointing to,
+          was deleted. The best place to do it is in the row-deleted
+          handler but the tinymail model do not work like the glib
+          tree models and reports the deletion when the row is still
+          there */
+       if (!gtk_tree_row_reference_valid (priv->next_row_reference)) {
+               if (gtk_tree_row_reference_valid (priv->row_reference)) {
+                       priv->next_row_reference = gtk_tree_row_reference_copy (priv->row_reference);
+                       select_next_valid_row (priv->header_model, &(priv->next_row_reference), FALSE);
+               }
+       }
+       if (priv->next_row_reference)
+               path = gtk_tree_row_reference_get_path (priv->next_row_reference);
+       if (path == NULL)
                return FALSE;
 
        gtk_tree_model_get_iter (priv->header_model,
@@ -1170,12 +1222,13 @@ modest_msg_view_window_select_next_message (ModestMsgViewWindow *window)
                            &header, -1);
        
        /* Read the message & show it */
-       if (!message_reader (window, priv, header, tmp_iter))
+       if (!message_reader (window, priv, header, path)) {
                retval = FALSE;
+               gtk_tree_path_free (path);
+       }
 
        /* Free */
        g_object_unref (header);
-       gtk_tree_path_free (path);
 
        return retval;          
 }
@@ -1186,6 +1239,7 @@ modest_msg_view_window_select_first_message (ModestMsgViewWindow *self)
        ModestMsgViewWindowPrivate *priv = NULL;
        TnyHeader *header = NULL;
        GtkTreeIter iter;
+       GtkTreePath *path;
 
        g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), FALSE);
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
@@ -1199,15 +1253,16 @@ modest_msg_view_window_select_first_message (ModestMsgViewWindow *self)
                            &iter, 
                            TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
                            &header, -1);
-       
        g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
        if (tny_header_get_flags (header) & TNY_HEADER_FLAG_DELETED) {
                g_object_unref (header);
                return modest_msg_view_window_select_next_message (self);
        }
        
+       path = gtk_tree_model_get_path (priv->header_model, &iter);
+       
        /* Read the message & show it */
-       message_reader (self, priv, header, iter);
+       message_reader (self, priv, header, path);
        
        /* Free */
        g_object_unref (header);
@@ -1245,7 +1300,7 @@ modest_msg_view_window_select_previous_message (ModestMsgViewWindow *window)
                }
 
                /* Read the message & show it */
-               if (!message_reader (window, priv, header, iter)) {
+               if (!message_reader (window, priv, header, path)) {
                        g_object_unref (header);
                        break;
                }
@@ -1510,6 +1565,9 @@ modest_msg_view_window_clipboard_owner_change (GtkClipboard *clipboard,
        gchar *selection;
        GtkWidget *focused;
 
+       if (!GTK_WIDGET_VISIBLE (window))
+               return;
+
        parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
        selection = gtk_clipboard_wait_for_text (clipboard);
 
@@ -1580,6 +1638,25 @@ observers_empty (ModestMsgViewWindow *self)
        return is_empty;
 }
 
+static void
+on_account_removed (TnyAccountStore *account_store, 
+                   TnyAccount *account,
+                   gpointer user_data)
+{
+       /* Do nothing if it's a transport account, because we only
+          show the messages of a store account */
+       if (tny_account_get_account_type(account) == TNY_ACCOUNT_TYPE_STORE) {
+               const gchar *parent_acc = NULL;
+               const gchar *our_acc = NULL;
+
+               our_acc = modest_window_get_active_account (MODEST_WINDOW (user_data));
+               parent_acc = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
+
+               /* Close this window if I'm showing a message of the removed account */
+               if (strcmp (parent_acc, our_acc) == 0)
+                       modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (user_data));
+       }
+}
 
 static void
 on_queue_changed (ModestMailOperationQueue *queue,
@@ -1744,6 +1821,8 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart
                        if (!account)
                                account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
                        msg_win = modest_msg_view_window_new (TNY_MSG (mime_part), account, NULL);
+                       modest_window_set_zoom (MODEST_WINDOW (msg_win), 
+                                               modest_window_get_zoom (MODEST_WINDOW (window)));
                        modest_window_mgr_register_window (mgr, msg_win);
                        gtk_window_set_transient_for (GTK_WINDOW (msg_win), GTK_WINDOW (window));
                        gtk_widget_show_all (GTK_WIDGET (msg_win));
@@ -1899,7 +1978,7 @@ modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, GList *mim
                        canceled = TRUE;
                }
        } else {
-               save_multiple_str = g_strdup_printf (_("FIXME: %d attachments"), 
+               save_multiple_str = g_strdup_printf (_FM("sfil_va_number_of_objects_attachments"), 
                                                     g_list_length (mime_parts));
        }