X-Git-Url: http://git.maemo.org/git/?p=modest;a=blobdiff_plain;f=src%2Fwidgets%2Fmodest-attachments-view.c;h=6a17f99dc53fd0cc1cffe6825f3e0497928f9665;hp=22d3b05b159d8a3a60517ec530eb16247e6c8297;hb=d2ea95049c43f1fc3089109866757e45139d0d9a;hpb=ab02b629212f025c460314340353972a97481448 diff --git a/src/widgets/modest-attachments-view.c b/src/widgets/modest-attachments-view.c index 22d3b05..6a17f99 100644 --- a/src/widgets/modest-attachments-view.c +++ b/src/widgets/modest-attachments-view.c @@ -29,8 +29,6 @@ #include -//#include - #include #include #include @@ -41,12 +39,14 @@ #include #include #include +#include static GObjectClass *parent_class = NULL; /* signals */ enum { ACTIVATE_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,13 +129,41 @@ 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 ()); tny_mime_part_get_parts (TNY_MIME_PART (priv->msg), parts); iter = tny_list_create_iterator (parts); @@ -144,22 +172,26 @@ modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, Tn TnyMimePart *part; part = TNY_MIME_PART (tny_iterator_get_current (iter)); - if (part && (tny_mime_part_is_attachment (part) || TNY_IS_MSG (part))) { - modest_attachments_view_add_attachment (attachments_view, part); - } + + if (part && (modest_tny_mime_part_is_attachment_for_modest (part))) + modest_attachments_view_add_attachment (attachments_view, part, TRUE, 0); if (part) g_object_unref (part); tny_iterator_next (iter); } + g_object_unref (iter); + g_object_unref (parts); + gtk_widget_queue_draw (GTK_WIDGET (attachments_view)); } 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; @@ -169,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); } @@ -320,6 +354,15 @@ modest_attachments_view_class_init (ModestAttachmentsViewClass *klass) g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT); + signals[DELETE_SIGNAL] = + g_signal_new ("delete", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET(ModestAttachmentsViewClass, delete), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + return; } @@ -376,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); } } } @@ -432,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, @@ -454,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; @@ -473,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); @@ -491,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; @@ -511,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; @@ -682,49 +788,47 @@ 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); -} - -GList * +TnyList * modest_attachments_view_get_selection (ModestAttachmentsView *atts_view) { ModestAttachmentsViewPrivate *priv; - GList *selection, *node; + TnyList *selection; + GList *node; g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL); priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view); - selection = NULL; + selection = tny_simple_list_new (); for (node = priv->selected; node != NULL; node = g_list_next (node)) { ModestAttachmentView *att_view = (ModestAttachmentView *) node->data; TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view)); - selection = g_list_append (selection, part); + tny_list_append (selection, (GObject *) part); + g_object_unref (part); } return selection; } -GList * +TnyList * modest_attachments_view_get_attachments (ModestAttachmentsView *atts_view) { ModestAttachmentsViewPrivate *priv; - GList *children, *node, *att_list = NULL; + TnyList *att_list; + GList *children, *node= NULL; g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL); priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view); + att_list = TNY_LIST (tny_simple_list_new ()); + children = gtk_container_get_children (GTK_CONTAINER (priv->box)); for (node = children; node != NULL; node = g_list_next (node)) { GtkWidget *att_view = GTK_WIDGET (node->data); TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view)); - att_list = g_list_prepend (att_list, mime_part); + tny_list_append (att_list, (GObject *) mime_part); + g_object_unref (mime_part); } g_list_free (children); - att_list = g_list_reverse (att_list); return att_list; } @@ -742,10 +846,16 @@ 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); @@ -769,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) { @@ -778,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)); }