2007-06-07 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / modest-msg-view-window.c
index 229cb72..35b6bbb 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"
@@ -151,6 +153,8 @@ struct _ModestMsgViewWindowPrivate {
        guint queue_change_handler;
 
        guint progress_bar_timeout;
+
+       gchar *msg_uid;
 };
 
 #define MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
@@ -237,6 +241,7 @@ modest_msg_view_window_init (ModestMsgViewWindow *obj)
 
        priv->optimized_view  = FALSE;
        priv->progress_bar_timeout = 0;
+       priv->msg_uid = NULL;
 }
 
 
@@ -416,20 +421,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);
@@ -441,6 +446,11 @@ modest_msg_view_window_finalize (GObject *obj)
                priv->row_reference = NULL;
        }
 
+       if (priv->msg_uid) {
+               g_free (priv->msg_uid);
+               priv->msg_uid = NULL;
+       }
+
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
@@ -454,13 +464,16 @@ on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMsgViewWindow *self)
 }
 
 ModestWindow *
-modest_msg_view_window_new_with_header_model (TnyMsg *msg, const gchar *account_name,
-                                             GtkTreeModel *model, GtkTreeRowReference *row_reference)
+modest_msg_view_window_new_with_header_model (TnyMsg *msg, 
+                                             const gchar *account_name,
+                                             const gchar *msg_uid,
+                                             GtkTreeModel *model, 
+                                             GtkTreeRowReference *row_reference)
 {
        ModestMsgViewWindow *window = NULL;
        ModestMsgViewWindowPrivate *priv = NULL;
 
-       window = MODEST_MSG_VIEW_WINDOW(modest_msg_view_window_new (msg, account_name));
+       window = MODEST_MSG_VIEW_WINDOW(modest_msg_view_window_new (msg, account_name, msg_uid));
        g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window), NULL);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
@@ -478,26 +491,39 @@ 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)
+modest_msg_view_window_new (TnyMsg *msg, 
+                           const gchar *account_name,
+                           const gchar *msg_uid)
 {
-       GObject *obj;
-       ModestMsgViewWindowPrivate *priv;
-       ModestWindowPrivate *parent_priv;
-       GtkActionGroup *action_group;
+       ModestMsgViewWindow *self = NULL;
+       GObject *obj = NULL;
+       ModestMsgViewWindowPrivate *priv = NULL;
+       ModestWindowPrivate *parent_priv = NULL;
+       ModestDimmingRulesGroup *menu_rules_group = NULL;
+       ModestDimmingRulesGroup *toolbar_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);
+
+       priv->msg_uid = g_strdup (msg_uid);
+
        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);
 
+       menu_rules_group = modest_dimming_rules_group_new ("ModestMenuDimmingRules");
+       toolbar_rules_group = modest_dimming_rules_group_new ("ModestToolbarDimmingRules");
+
        /* Add common actions */
        gtk_action_group_add_actions (action_group,
                                      modest_action_entries,
@@ -531,6 +557,22 @@ modest_msg_view_window_new (TnyMsg *msg, const gchar *account_name)
        }
        /* ****** */
 
+       /* Add common dimming rules */
+       modest_dimming_rules_group_add_rules (menu_rules_group, 
+                                             modest_msg_view_menu_dimming_entries,
+                                             G_N_ELEMENTS (modest_msg_view_menu_dimming_entries),
+                                             self);
+       modest_dimming_rules_group_add_rules (toolbar_rules_group, 
+                                             modest_msg_view_toolbar_dimming_entries,
+                                             G_N_ELEMENTS (modest_msg_view_toolbar_dimming_entries),
+                                             self);
+
+       /* Insert dimming rules group for this window */
+       modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, menu_rules_group);
+       modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, toolbar_rules_group);
+       g_object_unref (menu_rules_group);
+       g_object_unref (toolbar_rules_group);
+
        /* Add accelerators */
        gtk_window_add_accel_group (GTK_WINDOW (obj), 
                                    gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
@@ -583,6 +625,10 @@ modest_msg_view_window_new (TnyMsg *msg, const gchar *account_name)
        action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/EditMenu/EditCutMenu");
        gtk_action_set_sensitive (action, FALSE);
 
+       /* also set the add to contacts status to false as it depends on clipboard status */
+       action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ToolsMenu/ToolsAddToContactsMenu");
+       gtk_action_set_sensitive (action, FALSE);
+
        gtk_widget_grab_focus (priv->msg_view);
 
        return MODEST_WINDOW(obj);
@@ -631,23 +677,8 @@ modest_msg_view_window_get_message (ModestMsgViewWindow *self)
 const gchar*
 modest_msg_view_window_get_message_uid (ModestMsgViewWindow *self)
 {
-       TnyMsg *msg;
-       TnyHeader *header;
-       const gchar *retval = NULL;
-
-       msg = modest_msg_view_window_get_message (self);
-
-       if (!msg)
-               return NULL;
-
-       header = tny_msg_get_header (msg);
-       if (header) {
-               retval = tny_header_get_uid (header);
-               g_object_unref (header);
-       }
-       g_object_unref (msg);
-
-       return retval;
+       ModestMsgViewWindowPrivate *priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
+       return (const gchar*) priv->msg_uid;
 }
 
 static void 
@@ -952,7 +983,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);
@@ -999,7 +1030,7 @@ modest_msg_view_window_select_first_message (ModestMsgViewWindow *self)
                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(self));
+       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);
@@ -1046,7 +1077,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);             
 
@@ -1413,7 +1444,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));
@@ -1424,10 +1455,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:
@@ -1500,11 +1531,24 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart
 
        if (!TNY_IS_MSG (mime_part)) {
                gchar *filepath = NULL;
-               TnyFsStream *temp_stream = modest_maemo_utils_create_temp_stream (&filepath);
+               const gchar *att_filename = tny_mime_part_get_filename (mime_part);
+               gchar *extension = NULL;
+               TnyFsStream *temp_stream = NULL;
+
+               if (att_filename) {
+                       extension = g_strrstr (att_filename, ".");
+                       if (extension != NULL)
+                               extension++;
+               }
+
+               temp_stream = modest_maemo_utils_create_temp_stream (extension, &filepath);
 
                if (temp_stream) {
+                       const gchar *content_type;
+                       content_type = tny_mime_part_get_content_type (mime_part);
                        tny_mime_part_decode_to_stream (mime_part, TNY_STREAM (temp_stream));
-                       modest_platform_activate_file (filepath);
+                       
+                       modest_platform_activate_file (filepath, content_type);
                        g_object_unref (temp_stream);
                        g_free (filepath);
                        /* TODO: delete temporary file */
@@ -1513,18 +1557,17 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart
                /* message attachment */
                TnyHeader *header = NULL;
                ModestWindowMgr *mgr;
-               ModestWindow *msg_win;
+               ModestWindow *msg_win = NULL;
 
                header = tny_msg_get_header (TNY_MSG (mime_part));
                mgr = modest_runtime_get_window_mgr ();
-               /* TODO: this is not getting any uid */
-               msg_win = modest_window_mgr_find_window_by_msguid (mgr, tny_header_get_uid (header));
+               msg_win = modest_window_mgr_find_window_by_header (mgr, header);
 
                if (!msg_win) {
                        gchar *account = g_strdup (modest_window_get_active_account (MODEST_WINDOW (window)));
                        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);
+                       msg_win = modest_msg_view_window_new (TNY_MSG (mime_part), account, NULL);
                        modest_window_mgr_register_window (mgr, msg_win);
                        gtk_window_set_transient_for (GTK_WINDOW (msg_win), GTK_WINDOW (window));
                }