Fixes leak 25/26
[modest] / src / widgets / modest-attachments-view.c
index 392263d..5617a65 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007, Nokia Corporation
+/* Copyright (c) 2007, 2009, Nokia Corporation
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -46,7 +46,7 @@ static GObjectClass *parent_class = NULL;
 /* signals */
 enum {
        ACTIVATE_SIGNAL,
-       SELECTION_CHANGED_SIGNAL,
+       DELETE_SIGNAL,
        LAST_SIGNAL
 };
 
@@ -58,6 +58,7 @@ struct _ModestAttachmentsViewPrivate
        GtkWidget *box;
        GList *selected;
        GtkWidget *rubber_start;
+       ModestAttachmentsViewStyle style;
 };
 
 #define MODEST_ATTACHMENTS_VIEW_GET_PRIVATE(o) \
@@ -76,7 +77,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 +101,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 +130,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 +175,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 +191,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,9 +202,12 @@ modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view,
 
        priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
 
-       att_view = modest_attachment_view_new (part);
-       gtk_box_pack_end (GTK_BOX (priv->box), att_view, FALSE, FALSE, 0);
+       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_start (GTK_BOX (priv->box), att_view, FALSE, FALSE, 0);
        gtk_widget_show_all (att_view);
+       gtk_widget_queue_resize (GTK_WIDGET (attachments_view));
 }
 
 void
@@ -209,22 +243,24 @@ modest_attachments_view_remove_attachment (ModestAttachmentsView *atts_view, Tny
 
                box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
                node = g_list_find (box_children, found_att_view);
-               if (node->next)
+               if (node && node->next)
                        next_widget = node->next->data;
 
                g_list_free (box_children);
+               gtk_widget_destroy (GTK_WIDGET (found_att_view));
 
                node = g_list_find (priv->selected, found_att_view);
-               if (node != NULL) {
+               if (node) {
                        priv->selected = g_list_delete_link (priv->selected, node);
-                       gtk_widget_destroy (GTK_WIDGET (found_att_view));
                        if ((priv->selected == NULL) && (next_widget != NULL))
                                set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), 
                                              MODEST_ATTACHMENT_VIEW (next_widget));
                }
                own_clipboard (atts_view);
+
        }
 
+       gtk_widget_queue_resize (GTK_WIDGET (atts_view));
 }
 
 void
@@ -255,6 +291,7 @@ modest_attachments_view_remove_attachment_by_id (ModestAttachmentsView *atts_vie
 
        own_clipboard (atts_view);
 
+       gtk_widget_queue_resize (GTK_WIDGET (atts_view));
 }
 
 static void
@@ -266,6 +303,7 @@ modest_attachments_view_instance_init (GTypeInstance *instance, gpointer g_class
        priv->box = gtk_vbox_new (FALSE, 0);
        priv->rubber_start = NULL;
        priv->selected = NULL;
+       priv->style = MODEST_ATTACHMENTS_VIEW_STYLE_SELECTABLE;
 
        gtk_container_add (GTK_CONTAINER (instance), priv->box);
        gtk_event_box_set_above_child (GTK_EVENT_BOX (instance), TRUE);
@@ -321,16 +359,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;
 }
 
@@ -370,7 +408,8 @@ button_press_event (GtkWidget *widget,
                    ModestAttachmentsView *atts_view)
 {
        ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
-       if (!GTK_WIDGET_HAS_FOCUS (widget))
+       if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_SELECTABLE && 
+           !GTK_WIDGET_HAS_FOCUS (widget))
                gtk_widget_grab_focus (widget);
 
        if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
@@ -380,16 +419,25 @@ button_press_event (GtkWidget *widget,
                                                   (gint) event->x_root, (gint) event->y_root);
 
                if (att_view != NULL) {
-                       if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED && (g_list_length (priv->selected) < 2)) {
+                       if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_NO_FOCUS) {
+                               unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
+                       } else if ((priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_LINKS) ||
+                           (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED && (g_list_length (priv->selected) < 2))) {
                                TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
                                if (TNY_IS_MIME_PART (mime_part)) {
                                        g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
                                        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 +491,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,
@@ -450,6 +538,11 @@ key_press_event (GtkWidget *widget,
 {
        ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
 
+       if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_NO_FOCUS) {
+               unselect_all (atts_view);
+               return FALSE;
+       }
+
        /* If grabbed (for example rubber banding), escape leaves the rubberbanding mode */
        if (gtk_grab_get_current () == widget) {
                if (event->keyval == GDK_Escape) {
@@ -465,15 +558,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 +585,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 +606,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 +627,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 +733,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 +778,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 +803,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)
 {
@@ -758,20 +857,27 @@ modest_attachments_view_select_all (ModestAttachmentsView *atts_view)
 
        unselect_all (atts_view);
 
+       if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_LINKS)
+               return;
+
        children = gtk_container_get_children (GTK_CONTAINER (priv->box));
        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 +897,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 +943,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 +953,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;
 }
 
@@ -826,11 +967,34 @@ focus (GtkWidget *widget, GtkDirectionType direction, ModestAttachmentsView *att
        if (!gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)))
                return FALSE;
 
-       children = gtk_container_get_children (GTK_CONTAINER (priv->box));
-       if (children != NULL) {
-               set_selected (atts_view, MODEST_ATTACHMENT_VIEW (children->data));
+       if (priv->style != MODEST_ATTACHMENTS_VIEW_STYLE_NO_FOCUS) {
+               children = gtk_container_get_children (GTK_CONTAINER (priv->box));
+               if (children != NULL) {
+                       set_selected (atts_view, MODEST_ATTACHMENT_VIEW (children->data));
+               }
+               g_list_free (children);
        }
-       g_list_free (children);
 
        return FALSE;
 }
+
+void 
+modest_attachments_view_set_style (ModestAttachmentsView *self,
+                                  ModestAttachmentsViewStyle style)
+{
+       ModestAttachmentsViewPrivate *priv;
+
+       g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (self));
+       priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (self);
+
+       if (priv->style != style) {
+               priv->style = style;
+               gtk_widget_queue_draw (GTK_WIDGET (self));
+               if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_SELECTABLE) {
+                       GTK_WIDGET_SET_FLAGS (self, GTK_CAN_FOCUS);
+               } else {
+                       GTK_WIDGET_UNSET_FLAGS (self, GTK_CAN_FOCUS);
+               }
+
+       }
+}