* Reworked attachments mime type detection and body detection, to work
[modest] / src / widgets / modest-attachments-view.c
index 392263d..6a17f99 100644 (file)
@@ -46,7 +46,7 @@ static GObjectClass *parent_class = NULL;
 /* signals */
 enum {
        ACTIVATE_SIGNAL,
-       SELECTION_CHANGED_SIGNAL,
+       DELETE_SIGNAL,
        LAST_SIGNAL
 };
 
@@ -76,7 +76,6 @@ static void set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView
 static void select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2);
 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
                           guint info, gpointer userdata);
-static void clipboard_clear (GtkClipboard *clipboard, gpointer userdata);
 static void own_clipboard (ModestAttachmentsView *atts_view);
 
 static guint signals[LAST_SIGNAL] = {0};
@@ -101,13 +100,14 @@ modest_attachments_view_new (TnyMsg *msg)
        return GTK_WIDGET (self);
 }
 
+
 void
 modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, TnyMsg *msg)
 {
        ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
        TnyList *parts;
        TnyIterator *iter;
-       const gchar *msg_content_type = NULL;
+       gchar *msg_content_type = NULL;
        
        if (msg == priv->msg) return;
 
@@ -129,10 +129,39 @@ modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, Tn
 
        /* If the top mime part is a multipart/related, we don't show the attachments, as they're
         * embedded images in body */
-       msg_content_type = tny_mime_part_get_content_type (TNY_MIME_PART (priv->msg));
+       msg_content_type = modest_tny_mime_part_get_content_type (TNY_MIME_PART (priv->msg));
        if ((msg_content_type != NULL) && !strcasecmp (msg_content_type, "multipart/related")) {
-               gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
-               return;
+               gchar *header_content_type;
+               gboolean application_multipart = FALSE;
+
+               g_free (msg_content_type);
+
+               header_content_type = modest_tny_mime_part_get_headers_content_type (TNY_MIME_PART (priv->msg));
+               
+               if ((header_content_type != NULL) && 
+                   !strstr (header_content_type, "application/")) {
+                       application_multipart = TRUE;
+               }
+               g_free (header_content_type);
+
+               if (application_multipart) {
+                       gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
+                       return;
+               }
+       } else {
+               gboolean direct_attach;
+
+               direct_attach = (!g_str_has_prefix (msg_content_type, "message/rfc822") && 
+                                !g_str_has_prefix (msg_content_type, "multipart") && 
+                                !g_str_has_prefix (msg_content_type, "text/"));
+
+               g_free (msg_content_type);
+
+               if (direct_attach) {
+                       modest_attachments_view_add_attachment (attachments_view, TNY_MIME_PART (msg), TRUE, 0);
+                       gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
+                       return;
+               }
        }
 
        parts = TNY_LIST (tny_simple_list_new ());
@@ -145,7 +174,7 @@ modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, Tn
                part = TNY_MIME_PART (tny_iterator_get_current (iter));
 
                if (part && (modest_tny_mime_part_is_attachment_for_modest (part))) 
-                       modest_attachments_view_add_attachment (attachments_view, part);
+                       modest_attachments_view_add_attachment (attachments_view, part, TRUE, 0);
 
                if (part)
                        g_object_unref (part);
@@ -161,7 +190,8 @@ modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, Tn
 }
 
 void
-modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part)
+modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part,
+                                       gboolean detect_size, guint64 size)
 {
        GtkWidget *att_view = NULL;
        ModestAttachmentsViewPrivate *priv = NULL;
@@ -171,7 +201,9 @@ modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view,
 
        priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
 
-       att_view = modest_attachment_view_new (part);
+       att_view = modest_attachment_view_new (part, detect_size);
+       if (!detect_size)
+               modest_attachment_view_set_size (MODEST_ATTACHMENT_VIEW (att_view), size);
        gtk_box_pack_end (GTK_BOX (priv->box), att_view, FALSE, FALSE, 0);
        gtk_widget_show_all (att_view);
 }
@@ -321,16 +353,16 @@ modest_attachments_view_class_init (ModestAttachmentsViewClass *klass)
                              NULL, NULL,
                              g_cclosure_marshal_VOID__OBJECT,
                              G_TYPE_NONE, 1, G_TYPE_OBJECT);
-
-       signals[SELECTION_CHANGED_SIGNAL] =
-               g_signal_new ("selection-changed",
+       
+       signals[DELETE_SIGNAL] =
+               g_signal_new ("delete",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
-                             G_STRUCT_OFFSET (ModestAttachmentsViewClass, selection_changed),
+                             G_STRUCT_OFFSET(ModestAttachmentsViewClass, delete),
                              NULL, NULL,
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE, 0);
-       
+
        return;
 }
 
@@ -387,9 +419,15 @@ button_press_event (GtkWidget *widget,
                                        g_object_unref (mime_part);
                                }
                        } else {
-                               set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
-                               priv->rubber_start = att_view;
-                               gtk_grab_add (widget);
+                               TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
+
+                               /* Do not select purged attachments */
+                               if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
+                                       set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
+                                       priv->rubber_start = att_view;
+                                       gtk_grab_add (widget);
+                               }
+                               g_object_unref (mime_part);
                        }
                }
        }
@@ -443,6 +481,46 @@ motion_notify_event (GtkWidget *widget,
        return TRUE;
 }
 
+static GList*
+find_prev_or_next_not_purged (GList *list, gboolean prev, gboolean include_this)
+{
+       GList *tmp = NULL;
+       gboolean is_valid;
+
+       if (!include_this) {
+               if (prev) {
+                       tmp = g_list_previous (list);
+               } else {
+                       tmp = g_list_next (list);
+               }
+       } else {
+               tmp = list;
+       }
+
+       if (!tmp)
+               return NULL;
+
+       do {
+               ModestAttachmentView *att_view = (ModestAttachmentView *) tmp->data;
+               TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
+               
+               /* Do not select purged attachments */
+               if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
+                       is_valid = TRUE;
+               } else {
+                       if (prev)
+                               tmp = g_list_previous (tmp);
+                       else
+                               tmp = g_list_next (tmp);
+                       is_valid = FALSE;
+               }
+               g_object_unref (mime_part);
+       } while (!is_valid && tmp);
+
+       return tmp;
+}
+
+
 static gboolean
 key_press_event (GtkWidget *widget,
                 GdkEventKey *event,
@@ -465,15 +543,23 @@ key_press_event (GtkWidget *widget,
        if (event->keyval == GDK_Up) {
                ModestAttachmentView *current_sel = NULL;
                gboolean move_out = FALSE;
-               GList * box_children, *new_sel;
+               GList * box_children, *new_sel, *first_child;
 
                box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
-               if (box_children == NULL)
-                       move_out = TRUE;
-               else if ((priv->selected != NULL)&&(priv->selected->data != box_children->data))
-                       current_sel = (ModestAttachmentView *) priv->selected->data;
-               else
+               if (box_children == NULL) {
                        move_out = TRUE;
+               } else { 
+                       first_child = box_children;
+                       first_child = find_prev_or_next_not_purged (box_children, FALSE, TRUE);
+                       if (priv->selected != NULL && first_child != NULL) {
+                               if (priv->selected->data != first_child->data)
+                                       current_sel = (ModestAttachmentView *) priv->selected->data;
+                               else
+                                       move_out = TRUE;
+                       } else {
+                               move_out = TRUE;
+                       }
+               }
 
                if (move_out) {
                        GtkWidget *toplevel = NULL;
@@ -484,7 +570,10 @@ key_press_event (GtkWidget *widget,
                        unselect_all (atts_view);
                } else {
                        new_sel = g_list_find (box_children, (gpointer) current_sel);
-                       new_sel = g_list_previous (new_sel);
+                       new_sel = find_prev_or_next_not_purged (new_sel, TRUE, FALSE);
+                       /* We assume that we detected properly that
+                          there is a not purge attachment so we don't
+                          need to check NULL */
                        set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
                }
                g_list_free (box_children);
@@ -502,7 +591,8 @@ key_press_event (GtkWidget *widget,
                        move_out = TRUE;
                } else {
                        last_child = g_list_last (box_children);
-                       if (priv->selected != NULL) {
+                       last_child = find_prev_or_next_not_purged (last_child, TRUE, TRUE);
+                       if (priv->selected != NULL && last_child != NULL) {
                                GList *last_selected = g_list_last (priv->selected);
                                if (last_selected->data != last_child->data)
                                        current_sel = (ModestAttachmentView *) last_selected->data;
@@ -522,13 +612,18 @@ key_press_event (GtkWidget *widget,
                        unselect_all (atts_view);
                } else {
                        new_sel = g_list_find (box_children, (gpointer) current_sel);
-                       new_sel = g_list_next (new_sel);
+                       new_sel = find_prev_or_next_not_purged (new_sel, FALSE, FALSE);
                        set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
                }
                g_list_free (box_children);
                return TRUE;
        }
 
+       if (event->keyval == GDK_BackSpace) {
+               g_signal_emit (G_OBJECT (widget), signals[DELETE_SIGNAL], 0);
+               return TRUE;
+       }
+
        /* Activates selected item */
        if (g_list_length (priv->selected) == 1) {
                ModestAttachmentView *att_view = (ModestAttachmentView *) priv->selected->data;
@@ -623,8 +718,6 @@ set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view)
                g_object_unref (part);
        
        own_clipboard (atts_view);
-
-       g_signal_emit (G_OBJECT (atts_view), signals[SELECTION_CHANGED_SIGNAL], 0);
 }
 
 static void 
@@ -670,8 +763,6 @@ select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, Mode
        g_list_free (children);
        
        own_clipboard (atts_view);
-
-       g_signal_emit (G_OBJECT (atts_view), signals[SELECTION_CHANGED_SIGNAL], 0);
 }
 
 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
@@ -697,13 +788,6 @@ static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_
        }
 }
 
-static void clipboard_clear (GtkClipboard *clipboard, gpointer userdata)
-{
-       ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
-
-       unselect_all (atts_view);
-}
-
 TnyList *
 modest_attachments_view_get_selection (ModestAttachmentsView *atts_view)
 {
@@ -762,16 +846,20 @@ modest_attachments_view_select_all (ModestAttachmentsView *atts_view)
        g_list_free (priv->selected);
        priv->selected = NULL;
 
-
        for (node = children; node != NULL; node = g_list_next (node)) {
-               gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
-               priv->selected = g_list_append (priv->selected, node->data);
+               ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
+               TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
+
+               /* Do not select purged attachments */
+               if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
+                       gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
+                       priv->selected = g_list_append (priv->selected, node->data);
+               }
+               g_object_unref (mime_part);
        }
        g_list_free (children);
 
        own_clipboard (atts_view);
-
-       g_signal_emit (G_OBJECT (atts_view), signals[SELECTION_CHANGED_SIGNAL], 0);
 }
 
 gboolean
@@ -791,6 +879,43 @@ modest_attachments_view_has_attachments (ModestAttachmentsView *atts_view)
        return result;
 }
 
+void
+modest_attachments_view_get_sizes (ModestAttachmentsView *attachments_view,
+                                  gint *attachments_count,
+                                  guint64 *attachments_size)
+{
+       ModestAttachmentsViewPrivate *priv;
+       GList *children, *node;
+
+       g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
+       g_return_if_fail (attachments_count != NULL && attachments_size != NULL);
+
+       *attachments_count = 0;
+       *attachments_size = 0;
+
+       priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
+
+       children = gtk_container_get_children (GTK_CONTAINER (priv->box));
+       for (node = children; node != NULL; node = g_list_next (node)) {
+               GtkWidget *att_view = (GtkWidget *) node->data;
+               TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
+
+               if (!tny_mime_part_is_purged (part)) {
+                       guint64 size;
+                       (*attachments_count) ++;
+                       size = modest_attachment_view_get_size (MODEST_ATTACHMENT_VIEW (att_view));
+                       if (size == 0) {
+                               /* we do a random estimation of the size of an attachment */
+                               size = 32768;
+                       }
+                       *attachments_size += size;
+                       
+               }
+               g_object_unref (part);
+       }
+       g_list_free (children);
+}
+
 static void
 own_clipboard (ModestAttachmentsView *atts_view)
 {
@@ -800,7 +925,7 @@ own_clipboard (ModestAttachmentsView *atts_view)
 
        gtk_clipboard_set_with_owner (gtk_widget_get_clipboard (GTK_WIDGET (atts_view), GDK_SELECTION_PRIMARY),
                                      targets, G_N_ELEMENTS (targets),
-                                     clipboard_get, clipboard_clear, G_OBJECT(atts_view));
+                                     clipboard_get, NULL, G_OBJECT(atts_view));
                              
 }
 
@@ -810,8 +935,6 @@ focus_out_event (GtkWidget *widget, GdkEventFocus *event, ModestAttachmentsView
        if (!gtk_widget_is_focus (widget))
                unselect_all (atts_view);
 
-       g_signal_emit (G_OBJECT (atts_view), signals[SELECTION_CHANGED_SIGNAL], 0);
-
        return FALSE;
 }