* remove my debug g_warnings from modest_tny_account_store
[modest] / src / maemo / modest-msg-view-window.c
index aad0670..14e9e1b 100644 (file)
@@ -105,6 +105,11 @@ static void set_toolbar_mode (ModestMsgViewWindow *self,
 
 static gboolean set_toolbar_transfer_mode     (ModestMsgViewWindow *self); 
 
+static void view_attachment_insensitive_press (GtkWidget *widget, ModestMsgViewWindow *window);
+static void save_attachment_insensitive_press (GtkWidget *widget, ModestMsgViewWindow *window);
+
+static void update_window_title (ModestMsgViewWindow *window);
+
 /* list my signals */
 enum {
        /* MY_SIGNAL_1, */
@@ -142,6 +147,7 @@ struct _ModestMsgViewWindowPrivate {
        GtkWidget   *cancel_toolitem;
        GtkWidget   *prev_toolitem;
        GtkWidget   *next_toolitem;
+       ModestToolBarModes current_toolbar_mode;
 
        /* Optimized view enabled */
        gboolean optimized_view;
@@ -153,6 +159,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), \
@@ -236,9 +244,11 @@ modest_msg_view_window_init (ModestMsgViewWindow *obj)
        priv->msg_view      = NULL;
        priv->header_model  = NULL;
        priv->clipboard_change_handler = 0;
+       priv->current_toolbar_mode = TOOLBAR_MODE_NORMAL;
 
        priv->optimized_view  = FALSE;
        priv->progress_bar_timeout = 0;
+       priv->msg_uid = NULL;
 }
 
 
@@ -274,6 +284,9 @@ set_toolbar_mode (ModestMsgViewWindow *self,
        parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
                        
+       /* Sets current toolbar mode */
+       priv->current_toolbar_mode = mode;
+
        switch (mode) {
        case TOOLBAR_MODE_NORMAL:
                widget = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarMessageNew");
@@ -443,6 +456,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);
 }
 
@@ -456,13 +474,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);
@@ -480,17 +501,21 @@ 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)
 {
        ModestMsgViewWindow *self = NULL;
        GObject *obj = NULL;
        ModestMsgViewWindowPrivate *priv = NULL;
        ModestWindowPrivate *parent_priv = NULL;
-       ModestDimmingRulesGroup *rules_group = NULL;
+       ModestDimmingRulesGroup *menu_rules_group = NULL;
+       ModestDimmingRulesGroup *toolbar_rules_group = NULL;
        GtkActionGroup *action_group = NULL;
        GError *error = NULL;
        GdkPixbuf *window_icon = NULL;
        GtkAction *action = NULL;
+       GtkWidget *widget = NULL;
 
        g_return_val_if_fail (msg, NULL);
        
@@ -499,13 +524,16 @@ modest_msg_view_window_new (TnyMsg *msg, const gchar *account_name)
        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);
 
-       rules_group = modest_dimming_rules_group_new ("ModestCommonDimmingRules");
+       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,
@@ -541,14 +569,20 @@ 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),
+       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, rules_group);
-       g_object_unref (rules_group);
+       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), 
@@ -606,12 +640,30 @@ modest_msg_view_window_new (TnyMsg *msg, const gchar *account_name)
        action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ToolsMenu/ToolsAddToContactsMenu");
        gtk_action_set_sensitive (action, FALSE);
 
+       /* insensitive handlers */
+       widget = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar/AttachmentsMenu/ViewAttachmentMenu");
+       g_signal_connect (G_OBJECT (widget), "insensitive-press", G_CALLBACK (view_attachment_insensitive_press), self);
+       widget = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar/AttachmentsMenu/SaveAttachmentMenu");
+       g_signal_connect (G_OBJECT (widget), "insensitive-press", G_CALLBACK (save_attachment_insensitive_press), self);
+
        gtk_widget_grab_focus (priv->msg_view);
 
+       update_window_title (MODEST_MSG_VIEW_WINDOW (obj));
+
        return MODEST_WINDOW(obj);
 }
 
 
+gboolean 
+modest_msg_view_window_toolbar_on_transfer_mode     (ModestMsgViewWindow *self)
+{
+       ModestMsgViewWindowPrivate *priv= NULL; 
+
+       g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), FALSE);
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
+
+       return priv->current_toolbar_mode == TOOLBAR_MODE_TRANSFER;
+}
 
 TnyHeader*
 modest_msg_view_window_get_header (ModestMsgViewWindow *self)
@@ -654,23 +706,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 
@@ -682,6 +719,7 @@ modest_msg_view_window_toggle_find_toolbar (GtkToggleAction *toggle,
 
        if (gtk_toggle_action_get_active (toggle)) {
                gtk_widget_show (priv->find_toolbar);
+               hildon_find_toolbar_highlight_entry (HILDON_FIND_TOOLBAR (priv->find_toolbar), TRUE);
        } else {
                gtk_widget_hide (priv->find_toolbar);
        }
@@ -974,6 +1012,8 @@ modest_msg_view_window_select_next_message (ModestMsgViewWindow *window)
                        if (!(flags & TNY_HEADER_FLAG_SEEN))
                                tny_header_set_flags (header, flags | TNY_HEADER_FLAG_SEEN);
 
+                       /* Msg download initied */
+
                        /* New mail operation */
                        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);
@@ -1101,6 +1141,7 @@ view_msg_cb (ModestMailOperation *mail_op,
        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);
+       update_window_title (MODEST_MSG_VIEW_WINDOW (self));
        gtk_widget_grab_focus (priv->msg_view);
 }
 
@@ -1523,11 +1564,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 */
@@ -1536,18 +1590,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));
                }
@@ -1692,3 +1745,74 @@ modest_msg_view_window_remove_attachments (ModestMsgViewWindow *window, GList *m
 {
 /*     g_message ("not implemented %s", __FUNCTION__); */
 }
+
+static void 
+view_attachment_insensitive_press (GtkWidget *widget, ModestMsgViewWindow *window)
+{
+       GList *attachments = NULL;
+       ModestMsgViewWindowPrivate *priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+
+       attachments = modest_msg_view_get_selected_attachments (MODEST_MSG_VIEW (priv->msg_view));
+       if (g_list_length (attachments) > 1)
+               hildon_banner_show_information (NULL, NULL, _("mcen_ib_unable_to_display_more"));
+
+       g_list_free (attachments);
+
+}
+
+static void 
+save_attachment_insensitive_press (GtkWidget *widget, ModestMsgViewWindow *window)
+{
+       GList *attachments = NULL;
+       ModestMsgViewWindowPrivate *priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       gboolean selected_messages = FALSE;
+       gboolean nested_attachments = FALSE;
+       gint n_selected = 0;
+       GList *node = NULL;
+
+       attachments = modest_msg_view_get_selected_attachments (MODEST_MSG_VIEW (priv->msg_view));
+       n_selected = g_list_length (attachments);
+       for (node = attachments; node != NULL; node = g_list_next (node)) {
+               TnyMimePart *mime_part = TNY_MIME_PART (node->data);
+               TnyList *nested_list = tny_simple_list_new ();
+               if (!tny_mime_part_is_attachment (mime_part)) {
+                       selected_messages = TRUE;
+                       break;
+               }
+               tny_mime_part_get_parts (mime_part, nested_list);
+               if (tny_list_get_length (nested_list) > 0)
+                       nested_attachments = TRUE;
+               g_object_unref (nested_list);
+       }
+       g_list_free (attachments);
+       if (selected_messages) {
+               hildon_banner_show_information (NULL, NULL, _("mcen_ib_unable_to_save_attach_mail"));
+       } else if (nested_attachments) {
+               hildon_banner_show_information (NULL, NULL, _("FIXME:unable to save attachments with nested elements"));
+       } else if (n_selected == 0) {
+               hildon_banner_show_information (NULL, NULL, _("FIXME:no attachment selected"));
+       }
+
+       return ;
+               
+}
+
+static void
+update_window_title (ModestMsgViewWindow *window)
+{
+       ModestMsgViewWindowPrivate *priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       TnyMsg *msg = NULL;
+       TnyHeader *header = NULL;
+       const gchar *subject = NULL;
+
+       msg = modest_msg_view_get_message (MODEST_MSG_VIEW (priv->msg_view));
+       if (msg != NULL) {
+               header = tny_msg_get_header (msg);
+               subject = tny_header_get_subject (header);
+       }
+
+       if ((subject == NULL)||(subject[0] == '\0'))
+               subject = _("mail_va_no_subject");
+
+       gtk_window_set_title (GTK_WINDOW (window), subject);
+}