* src/maemo/modest-msg-edit-window.c:
[modest] / src / maemo / modest-msg-view-window.c
index 4eb7754..bc8dcf2 100644 (file)
@@ -38,6 +38,7 @@
 #include <modest-msg-view-window.h>
 #include <modest-attachments-view.h>
 #include <modest-main-window-ui.h>
+#include "modest-msg-view-window-ui-dimming.h"
 #include <modest-widget-memory.h>
 #include <modest-runtime.h>
 #include <modest-window-priv.h>
@@ -48,6 +49,7 @@
 #include "modest-defs.h"
 #include "modest-hildon-includes.h"
 #include <gtkhtml/gtkhtml-search.h>
+#include "modest-ui-dimming-manager.h"
 #include <gdk/gdkkeysyms.h>
 
 #define DEFAULT_FOLDER "MyDocs/.documents"
@@ -96,7 +98,7 @@ static void         on_queue_changed                     (ModestMailOperationQue
                                                          ModestMailOperationQueueNotification type,
                                                          ModestMsgViewWindow *self);
 
-static void view_msg_cb (const GObject *obj, const TnyMsg *msg, gpointer user_data);
+static void view_msg_cb (ModestMailOperation *mail_op, TnyHeader *header, TnyMsg *msg, gpointer user_data);
 
 static void set_toolbar_mode (ModestMsgViewWindow *self, 
                              ModestToolBarModes mode);
@@ -416,20 +418,20 @@ modest_msg_view_window_finalize (GObject *obj)
        ModestMsgViewWindowPrivate *priv;
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (obj);
-       if (priv->header_model != NULL) {
-               g_object_unref (priv->header_model);
-               priv->header_model = NULL;
-       }
        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;
        }
-
-       /* disconnet operations queue observer */
        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;
        }
+       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);
@@ -480,24 +482,31 @@ modest_msg_view_window_new_with_header_model (TnyMsg *msg, const gchar *account_
 ModestWindow *
 modest_msg_view_window_new (TnyMsg *msg, const gchar *account_name)
 {
-       GObject *obj;
-       ModestMsgViewWindowPrivate *priv;
-       ModestWindowPrivate *parent_priv;
-       GtkActionGroup *action_group;
+       ModestMsgViewWindow *self = NULL;
+       GObject *obj = NULL;
+       ModestMsgViewWindowPrivate *priv = NULL;
+       ModestWindowPrivate *parent_priv = NULL;
+       ModestDimmingRulesGroup *rules_group = NULL;
+       GtkActionGroup *action_group = NULL;
        GError *error = NULL;
        GdkPixbuf *window_icon = NULL;
-       GtkAction *action;
+       GtkAction *action = NULL;
 
        g_return_val_if_fail (msg, NULL);
        
        obj = g_object_new(MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
        parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
-       
+       self = MODEST_MSG_VIEW_WINDOW (obj);
+
        parent_priv->ui_manager = gtk_ui_manager_new();
+       parent_priv->ui_dimming_manager = modest_ui_dimming_manager_new();
+
        action_group = gtk_action_group_new ("ModestMsgViewWindowActions");
        gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
 
+       rules_group = modest_dimming_rules_group_new ("ModestCommonDimmingRules");
+
        /* Add common actions */
        gtk_action_group_add_actions (action_group,
                                      modest_action_entries,
@@ -531,6 +540,16 @@ modest_msg_view_window_new (TnyMsg *msg, const gchar *account_name)
        }
        /* ****** */
 
+       /* Add common dimming rules */
+       modest_dimming_rules_group_add_rules (rules_group, 
+                                             modest_msg_view_dimming_entries,
+                                             G_N_ELEMENTS (modest_msg_view_dimming_entries),
+                                             self);
+
+       /* Insert dimming rules group for this window */
+       modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, rules_group);
+       g_object_unref (rules_group);
+
        /* Add accelerators */
        gtk_window_add_accel_group (GTK_WINDOW (obj), 
                                    gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
@@ -590,6 +609,29 @@ modest_msg_view_window_new (TnyMsg *msg, const gchar *account_name)
 
 
 
+TnyHeader*
+modest_msg_view_window_get_header (ModestMsgViewWindow *self)
+{
+       ModestMsgViewWindowPrivate *priv= NULL; 
+       TnyHeader *header = NULL;
+       GtkTreeIter iter;
+
+       g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), NULL);
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
+
+       /* Get current message iter */
+       gtk_tree_model_get_iter (priv->header_model, 
+                                &iter, 
+                                gtk_tree_row_reference_get_path (priv->row_reference));
+
+       /* Get current message header */
+       gtk_tree_model_get (priv->header_model, &iter, 
+                           TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, 
+                           &header, -1);
+
+       return header;
+}
+
 TnyMsg*
 modest_msg_view_window_get_message (ModestMsgViewWindow *self)
 {
@@ -622,6 +664,8 @@ modest_msg_view_window_get_message_uid (ModestMsgViewWindow *self)
                retval = tny_header_get_uid (header);
                g_object_unref (header);
        }
+       g_object_unref (msg);
+
        return retval;
 }
 
@@ -927,7 +971,7 @@ modest_msg_view_window_select_next_message (ModestMsgViewWindow *window)
                                tny_header_set_flags (header, flags | TNY_HEADER_FLAG_SEEN);
 
                        /* New mail operation */
-                       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(window));
+                       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_RECEIVE, G_OBJECT(window));
                        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
                        modest_mail_operation_get_msg (mail_op, header, view_msg_cb, NULL);
                        g_object_unref (mail_op);
@@ -938,6 +982,53 @@ modest_msg_view_window_select_next_message (ModestMsgViewWindow *window)
        return FALSE;           
 }
 
+gboolean 
+modest_msg_view_window_select_first_message (ModestMsgViewWindow *self)
+{
+       ModestMsgViewWindowPrivate *priv = NULL;
+       ModestMailOperation *mail_op = NULL;
+       TnyHeader *header = NULL;
+       TnyHeaderFlags flags;
+       GtkTreePath *path = NULL;
+       GtkTreeIter iter;
+
+       g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), FALSE);
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
+
+       path = gtk_tree_path_new_from_string ("0");
+
+       /* Update the row reference */
+       /* Get first message */
+       gtk_tree_model_get_iter (priv->header_model, &iter, path);
+       gtk_tree_model_get (priv->header_model, &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)
+               return modest_msg_view_window_select_next_message (self);
+       
+       /* Update the row reference */
+       gtk_tree_row_reference_free (priv->row_reference);
+       priv->row_reference = gtk_tree_row_reference_new (priv->header_model, path);
+       gtk_tree_path_free (path);
+
+       /* Mark as read */
+       flags = tny_header_get_flags (header);
+       if (!(flags & TNY_HEADER_FLAG_SEEN))
+               tny_header_set_flags (header, flags | TNY_HEADER_FLAG_SEEN);
+       
+       /* New mail operation */
+       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_RECEIVE, G_OBJECT(self));
+       modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
+       modest_mail_operation_get_msg (mail_op, header, view_msg_cb, NULL);
+       g_object_unref (mail_op);
+       
+       /* Free */
+/*     g_object_unref (header); */
+
+       return TRUE;
+}
 gboolean        
 modest_msg_view_window_select_previous_message (ModestMsgViewWindow *window)
 {
@@ -974,7 +1065,7 @@ modest_msg_view_window_select_previous_message (ModestMsgViewWindow *window)
                                tny_header_set_flags (header, flags | TNY_HEADER_FLAG_SEEN);
 
                        /* New mail operation */
-                       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(window));
+                       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_RECEIVE, G_OBJECT(window));
                        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
                        modest_mail_operation_get_msg (mail_op, header, view_msg_cb, NULL);             
 
@@ -986,20 +1077,24 @@ modest_msg_view_window_select_previous_message (ModestMsgViewWindow *window)
 }
 
 static void
-view_msg_cb(const GObject *obj, const TnyMsg *msg, gpointer user_data)
+view_msg_cb (ModestMailOperation *mail_op, 
+            TnyHeader *header, 
+            TnyMsg *msg, 
+            gpointer user_data)
 {
        ModestMsgViewWindow *self = NULL;
        ModestMsgViewWindowPrivate *priv = NULL;
-       TnyMsg *new_msg = NULL;
-       g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (obj));
+
        g_return_if_fail (TNY_IS_MSG (msg));
-       self = MODEST_MSG_VIEW_WINDOW (obj);
+
+       /* Get the window */ 
+       self = (ModestMsgViewWindow *) modest_mail_operation_get_source (mail_op);
+       g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self));
+
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
 
        /* Set new message */
-       new_msg = g_object_ref (G_OBJECT(msg));
-       modest_msg_view_set_message (MODEST_MSG_VIEW (priv->msg_view), new_msg);
+       modest_msg_view_set_message (MODEST_MSG_VIEW (priv->msg_view), msg);
        modest_msg_view_window_update_dimmed (self);
        modest_msg_view_window_update_priority (self);
        gtk_widget_grab_focus (priv->msg_view);
@@ -1032,6 +1127,7 @@ modest_msg_view_window_get_folder_type (ModestMsgViewWindow *window)
 
                        g_object_unref (folder);
                }
+               g_object_unref (msg);
        }
 
        return folder_type;
@@ -1336,7 +1432,7 @@ on_queue_changed (ModestMailOperationQueue *queue,
 {
        GSList *tmp;
        ModestMsgViewWindowPrivate *priv;
-       ModestMailOperationId op_id;
+       ModestMailOperationTypeOperation op_type;
        ModestToolBarModes mode;
        
        g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self));
@@ -1347,10 +1443,10 @@ on_queue_changed (ModestMailOperationQueue *queue,
            return;
 
        /* Get toolbar mode from operation id*/
-       op_id = modest_mail_operation_get_id (mail_op);
-       switch (op_id) {
-       case MODEST_MAIL_OPERATION_ID_SEND:
-       case MODEST_MAIL_OPERATION_ID_RECEIVE:
+       op_type = modest_mail_operation_get_type_operation (mail_op);
+       switch (op_type) {
+       case MODEST_MAIL_OPERATION_TYPE_SEND:
+       case MODEST_MAIL_OPERATION_TYPE_RECEIVE:
                mode = TOOLBAR_MODE_TRANSFER;
                break;
        default:
@@ -1364,9 +1460,7 @@ on_queue_changed (ModestMailOperationQueue *queue,
        case MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED:
                if (mode == TOOLBAR_MODE_TRANSFER) {
                        /* Enable transfer toolbar mode */
-                       priv->progress_bar_timeout = g_timeout_add (2000, 
-                                                                   (GSourceFunc) set_toolbar_transfer_mode, 
-                                                                   self);
+                       set_toolbar_transfer_mode(self);
                        while (tmp) {
                                modest_progress_object_add_operation (MODEST_PROGRESS_OBJECT (tmp->data),
                                                                      mail_op);
@@ -1386,12 +1480,7 @@ on_queue_changed (ModestMailOperationQueue *queue,
 
                        /* If no more operations are being observed, NORMAL mode is enabled again */
                        if (observers_empty (self)) {
-                               if (priv->progress_bar_timeout > 0) {
-                                       g_source_remove (priv->progress_bar_timeout);
-                                       priv->progress_bar_timeout = 0;
-                               }
-                               else 
-                                       set_toolbar_mode (self, TOOLBAR_MODE_NORMAL);
+                               set_toolbar_mode (self, TOOLBAR_MODE_NORMAL);
                        }
                }
                break;