Show digest attachments (and other non body text parts) in attachments view
[modest] / src / widgets / modest-attachments-view.c
1 /* Copyright (c) 2007, 2009, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <config.h>
31
32 #include <string.h>
33 #include <gtk/gtk.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <tny-list.h>
36 #include <tny-simple-list.h>
37
38 #include <modest-platform.h>
39 #include <modest-runtime.h>
40 #include <modest-attachment-view.h>
41 #include <modest-attachments-view.h>
42 #include <modest-tny-mime-part.h>
43 #include <modest-tny-msg.h>
44
45 static GObjectClass *parent_class = NULL;
46
47 /* signals */
48 enum {
49         ACTIVATE_SIGNAL,
50         DELETE_SIGNAL,
51         LAST_SIGNAL
52 };
53
54 typedef struct _ModestAttachmentsViewPrivate ModestAttachmentsViewPrivate;
55
56 struct _ModestAttachmentsViewPrivate
57 {
58         TnyMsg *msg;
59         GtkWidget *box;
60         GList *selected;
61         GtkWidget *rubber_start;
62         GtkWidget *press_att_view;
63         ModestAttachmentsViewStyle style;
64 };
65
66 #define MODEST_ATTACHMENTS_VIEW_GET_PRIVATE(o)  \
67         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ATTACHMENTS_VIEW, ModestAttachmentsViewPrivate))
68
69 static gboolean button_press_event (GtkWidget *widget, GdkEventButton *event, ModestAttachmentsView *atts_view);
70 static gboolean motion_notify_event (GtkWidget *widget, GdkEventMotion *event, ModestAttachmentsView *atts_view);
71 static gboolean button_release_event (GtkWidget *widget, GdkEventButton *event, ModestAttachmentsView *atts_view);
72 static gboolean key_press_event (GtkWidget *widget, GdkEventKey *event, ModestAttachmentsView *atts_view);
73 static gboolean focus_out_event (GtkWidget *widget, GdkEventFocus *event, ModestAttachmentsView *atts_view);
74 static gboolean focus (GtkWidget *widget, GtkDirectionType direction, ModestAttachmentsView *atts_view);
75 static GtkWidget *get_att_view_at_coords (ModestAttachmentsView *atts_view,
76                                           gdouble x, gdouble y);
77 static void unselect_all (ModestAttachmentsView *atts_view);
78 static void set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view);
79 static void select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2);
80 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
81                            guint info, gpointer userdata);
82 static void own_clipboard (ModestAttachmentsView *atts_view);
83
84 static guint signals[LAST_SIGNAL] = {0};
85
86 /**
87  * modest_attachments_view_new:
88  * @msg: a #TnyMsg
89  *
90  * Constructor for attachments view widget.
91  *
92  * Return value: a new #ModestAttachmentsView instance implemented for Gtk+
93  **/
94 GtkWidget*
95 modest_attachments_view_new (TnyMsg *msg)
96 {
97         ModestAttachmentsView *self = g_object_new (MODEST_TYPE_ATTACHMENTS_VIEW, 
98                                                     "resize-mode", GTK_RESIZE_PARENT,
99                                                     NULL);
100
101         modest_attachments_view_set_message (self, msg);
102
103         return GTK_WIDGET (self);
104 }
105
106 static void
107 add_digest_attachments (ModestAttachmentsView *attachments_view, TnyMimePart *part)
108 {
109         TnyList *parts;
110         TnyIterator *iter;
111
112         parts = TNY_LIST (tny_simple_list_new());
113         tny_mime_part_get_parts (TNY_MIME_PART (part), parts);
114
115         for (iter  = tny_list_create_iterator(parts); 
116              !tny_iterator_is_done (iter);
117              tny_iterator_next (iter)) {
118                 TnyMimePart *cur_part = TNY_MIME_PART (tny_iterator_get_current (iter));
119                 modest_attachments_view_add_attachment (attachments_view, cur_part, TRUE, 0);
120                 g_object_unref (cur_part);
121         }
122         g_object_unref (iter);
123         g_object_unref (parts);
124
125 }
126
127
128 void
129 modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, TnyMsg *msg)
130 {
131         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
132         TnyList *parts;
133         TnyIterator *iter;
134         gchar *msg_content_type = NULL;
135         TnyMimePart *part_to_check;
136         gboolean body_found;
137         gboolean is_alternate;
138         
139         if (msg == priv->msg) return;
140
141         if (priv->msg)
142                 g_object_unref (priv->msg);
143         if (msg)
144                 g_object_ref (G_OBJECT(msg));
145         
146         priv->msg = msg;
147
148         g_list_free (priv->selected);
149         priv->selected = NULL;
150
151         gtk_container_foreach (GTK_CONTAINER (priv->box), (GtkCallback) gtk_widget_destroy, NULL);
152         
153         if (priv->msg == NULL) {
154                 return;
155         }
156
157         part_to_check = modest_tny_msg_get_attachments_parent (TNY_MSG (msg));
158
159         msg_content_type = modest_tny_mime_part_get_content_type (TNY_MIME_PART (part_to_check));
160         is_alternate = !strcasecmp (msg_content_type, "multipart/alternative");
161
162         /* If the top mime part is a multipart/related, we don't show the attachments, as they're
163          * embedded images in body */
164         if ((msg_content_type != NULL) && !strcasecmp (msg_content_type, "multipart/related")) {
165                 gchar *header_content_type;
166                 gboolean application_multipart = FALSE;
167
168                 g_free (msg_content_type);
169
170                 header_content_type = modest_tny_mime_part_get_headers_content_type (TNY_MIME_PART (part_to_check));
171                 
172                 if ((header_content_type != NULL) && 
173                     !strstr (header_content_type, "application/")) {
174                         application_multipart = TRUE;
175                 }
176                 g_free (header_content_type);
177
178                 if (application_multipart) {
179                         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
180                         g_object_unref (part_to_check);
181                         return;
182                 }
183         } else {
184                 gboolean direct_attach;
185
186                 direct_attach = (!g_str_has_prefix (msg_content_type, "message/rfc822") && 
187                                  !g_str_has_prefix (msg_content_type, "multipart") && 
188                                  !g_str_has_prefix (msg_content_type, "text/"));
189
190                 g_free (msg_content_type);
191
192                 if (direct_attach) {
193                         modest_attachments_view_add_attachment (attachments_view, TNY_MIME_PART (part_to_check), TRUE, 0);
194                         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
195                         g_object_unref (part_to_check);
196                         return;
197                 }
198         }
199
200         parts = TNY_LIST (tny_simple_list_new ());
201         tny_mime_part_get_parts (TNY_MIME_PART (part_to_check), parts);
202         iter = tny_list_create_iterator (parts);
203
204         body_found = FALSE;
205         while (!tny_iterator_is_done (iter)) {
206                 TnyMimePart *part;
207                 gchar *content_type;
208
209                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
210
211                 if (part && (modest_tny_mime_part_is_attachment_for_modest (part))) {
212                         modest_attachments_view_add_attachment (attachments_view, part, TRUE, 0);
213
214                 } else if (part && !is_alternate) {
215                         content_type = g_ascii_strdown (tny_mime_part_get_content_type (part), -1);
216                         g_strstrip (content_type);
217
218                         if (g_str_has_prefix (content_type, "multipart/digest")) {
219                                 add_digest_attachments (attachments_view, part);
220                         } else if (body_found && g_str_has_prefix (content_type, "text/")) {
221                                    modest_attachments_view_add_attachment (attachments_view, part, TRUE, 0);
222                         } else if (g_str_has_prefix (content_type, "multipart/") || 
223                                    g_str_has_prefix (content_type, "text/")) {
224                                 body_found = TRUE;
225                         }
226                 }
227
228
229                 if (part)
230                         g_object_unref (part);
231
232                 tny_iterator_next (iter);
233         }
234         g_object_unref (iter);
235         g_object_unref (parts);
236         g_object_unref (part_to_check);
237         
238
239         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
240
241 }
242
243 void
244 modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part,
245                                         gboolean detect_size, guint64 size)
246 {
247         GtkWidget *att_view = NULL;
248         ModestAttachmentsViewPrivate *priv = NULL;
249
250         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
251         g_return_if_fail (TNY_IS_MIME_PART (part));
252
253         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
254
255         att_view = modest_attachment_view_new (part, detect_size);
256         if (!detect_size)
257                 modest_attachment_view_set_size (MODEST_ATTACHMENT_VIEW (att_view), size);
258         gtk_box_pack_start (GTK_BOX (priv->box), att_view, FALSE, FALSE, 0);
259         gtk_widget_show_all (att_view);
260         gtk_widget_queue_resize (GTK_WIDGET (attachments_view));
261 }
262
263 void
264 modest_attachments_view_remove_attachment (ModestAttachmentsView *atts_view, TnyMimePart *mime_part)
265 {
266         ModestAttachmentsViewPrivate *priv = NULL;
267         GList *box_children = NULL, *node = NULL;
268         ModestAttachmentView *found_att_view = NULL;
269
270         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
271         g_return_if_fail (TNY_IS_MIME_PART (mime_part));
272
273         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
274         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
275
276         for (node = box_children; node != NULL; node = g_list_next (node)) {
277                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
278                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
279
280                 if (mime_part == cur_mime_part)
281                         found_att_view = att_view;
282
283                 g_object_unref (cur_mime_part);
284
285                 if (found_att_view != NULL)
286                         break;
287         }
288
289         if (found_att_view) {
290                 GList *node = NULL;
291                 GtkWidget *next_widget = NULL;
292                 GList *box_children = NULL;
293
294                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
295                 node = g_list_find (box_children, found_att_view);
296                 if (node && node->next)
297                         next_widget = node->next->data;
298
299                 g_list_free (box_children);
300                 gtk_widget_destroy (GTK_WIDGET (found_att_view));
301
302                 node = g_list_find (priv->selected, found_att_view);
303                 if (node) {
304                         priv->selected = g_list_delete_link (priv->selected, node);
305                         if ((priv->selected == NULL) && (next_widget != NULL))
306                                 set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), 
307                                               MODEST_ATTACHMENT_VIEW (next_widget));
308                 }
309                 own_clipboard (atts_view);
310
311         }
312
313         gtk_widget_queue_resize (GTK_WIDGET (atts_view));
314 }
315
316 void
317 modest_attachments_view_remove_attachment_by_id (ModestAttachmentsView *atts_view, const gchar *att_id)
318 {
319         ModestAttachmentsViewPrivate *priv = NULL;
320         GList *box_children = NULL, *node = NULL;
321
322         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
323         g_return_if_fail (att_id != NULL);
324
325         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
326         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
327
328         for (node = box_children; node != NULL; node = g_list_next (node)) {
329                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
330                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
331                 const gchar *mime_part_id = NULL;
332
333                 mime_part_id = tny_mime_part_get_content_id (cur_mime_part);
334                 if ((mime_part_id != NULL) && (strcmp (mime_part_id, att_id) == 0)) {
335                         gtk_widget_destroy (GTK_WIDGET (att_view));
336                         priv->selected = g_list_remove (priv->selected, att_view);
337                 }
338
339                 g_object_unref (cur_mime_part);
340         }
341
342         own_clipboard (atts_view);
343
344         gtk_widget_queue_resize (GTK_WIDGET (atts_view));
345 }
346
347 static void
348 modest_attachments_view_instance_init (GTypeInstance *instance, gpointer g_class)
349 {
350         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (instance);
351
352         priv->msg = NULL;
353         priv->box = gtk_vbox_new (FALSE, 0);
354         priv->rubber_start = NULL;
355         priv->press_att_view = NULL;
356         priv->selected = NULL;
357         priv->style = MODEST_ATTACHMENTS_VIEW_STYLE_SELECTABLE;
358
359         gtk_container_add (GTK_CONTAINER (instance), priv->box);
360         gtk_event_box_set_above_child (GTK_EVENT_BOX (instance), TRUE);
361
362         g_signal_connect (G_OBJECT (instance), "button-press-event", G_CALLBACK (button_press_event), instance);
363         g_signal_connect (G_OBJECT (instance), "button-release-event", G_CALLBACK (button_release_event), instance);
364         g_signal_connect (G_OBJECT (instance), "motion-notify-event", G_CALLBACK (motion_notify_event), instance);
365         g_signal_connect (G_OBJECT (instance), "key-press-event", G_CALLBACK (key_press_event), instance);
366         g_signal_connect (G_OBJECT (instance), "focus-out-event", G_CALLBACK (focus_out_event), instance);
367         g_signal_connect (G_OBJECT (instance), "focus", G_CALLBACK (focus), instance);
368
369         GTK_WIDGET_SET_FLAGS (instance, GTK_CAN_FOCUS);
370
371         return;
372 }
373
374 static void
375 modest_attachments_view_finalize (GObject *object)
376 {
377         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (object);
378
379         if (priv->msg) {
380                 g_object_unref (priv->msg);
381                 priv->msg = NULL;
382         }
383
384         (*parent_class->finalize) (object);
385
386         return;
387 }
388
389 static void 
390 modest_attachments_view_class_init (ModestAttachmentsViewClass *klass)
391 {
392         GObjectClass *object_class;
393         GtkWidgetClass *widget_class;
394
395         parent_class = g_type_class_peek_parent (klass);
396         object_class = (GObjectClass*) klass;
397         widget_class = GTK_WIDGET_CLASS (klass);
398
399         object_class->finalize = modest_attachments_view_finalize;
400
401         klass->activate = NULL;
402
403         g_type_class_add_private (object_class, sizeof (ModestAttachmentsViewPrivate));
404
405         signals[ACTIVATE_SIGNAL] =
406                 g_signal_new ("activate",
407                               G_TYPE_FROM_CLASS (object_class),
408                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
409                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, activate),
410                               NULL, NULL,
411                               g_cclosure_marshal_VOID__OBJECT,
412                               G_TYPE_NONE, 1, G_TYPE_OBJECT);
413         
414         signals[DELETE_SIGNAL] =
415                 g_signal_new ("delete",
416                               G_TYPE_FROM_CLASS (object_class),
417                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
418                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, delete),
419                               NULL, NULL,
420                               g_cclosure_marshal_VOID__VOID,
421                               G_TYPE_NONE, 0);
422
423         return;
424 }
425
426 GType 
427 modest_attachments_view_get_type (void)
428 {
429         static GType type = 0;
430
431         if (G_UNLIKELY(type == 0))
432         {
433                 static const GTypeInfo info = 
434                 {
435                   sizeof (ModestAttachmentsViewClass),
436                   NULL,   /* base_init */
437                   NULL,   /* base_finalize */
438                   (GClassInitFunc) modest_attachments_view_class_init,   /* class_init */
439                   NULL,   /* class_finalize */
440                   NULL,   /* class_data */
441                   sizeof (ModestAttachmentsView),
442                   0,      /* n_preallocs */
443                   modest_attachments_view_instance_init    /* instance_init */
444                 };
445
446                 type = g_type_register_static (GTK_TYPE_EVENT_BOX,
447                         "ModestAttachmentsView",
448                         &info, 0);
449
450         }
451
452         return type;
453 }
454
455 /* buttons signal events */
456 static gboolean
457 button_press_event (GtkWidget *widget, 
458                     GdkEventButton *event,
459                     ModestAttachmentsView *atts_view)
460 {
461         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
462         if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_SELECTABLE && 
463             !GTK_WIDGET_HAS_FOCUS (widget))
464                 gtk_widget_grab_focus (widget);
465
466         if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
467                 GtkWidget *att_view = NULL;
468
469                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
470                                                    (gint) event->x_root, (gint) event->y_root);
471
472                 if (att_view != NULL) {
473                         if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_NO_FOCUS) {
474                                 unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
475                         } else if ((priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_LINKS) ||
476                             (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED && (g_list_length (priv->selected) < 2))) {
477                                 priv->press_att_view = att_view;
478                                 set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
479                                 gtk_grab_add (widget);
480                         } else {
481                                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
482
483                                 /* Do not select purged attachments */
484                                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
485                                         set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
486                                         priv->rubber_start = att_view;
487                                         gtk_grab_add (widget);
488                                 }
489                                 g_object_unref (mime_part);
490                         }
491                 }
492         }
493         return TRUE;
494
495 }
496
497 static gboolean
498 button_release_event (GtkWidget *widget,
499                       GdkEventButton *event,
500                       ModestAttachmentsView *atts_view)
501 {
502         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
503         if (widget == gtk_grab_get_current ()) {
504                 GtkWidget *att_view = NULL;
505
506                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
507                                                    (gint) event->x_root, (gint) event->y_root);
508
509                 if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_LINKS) {
510                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
511                         if (att_view == priv->press_att_view) {
512                                 TnyMimePart *mime_part;
513                                 mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
514                                 g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
515                                 g_object_unref (mime_part);
516                         }
517                         priv->press_att_view = NULL;
518                 } else {
519
520                         if (att_view != NULL) {
521                                 unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
522                                 select_range (MODEST_ATTACHMENTS_VIEW (widget), 
523                                               MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
524                                               MODEST_ATTACHMENT_VIEW (att_view));
525                         }
526                         priv->rubber_start = NULL;
527                 }
528                 gtk_grab_remove (widget);
529         }
530         return TRUE;
531 }
532
533 static gboolean
534 motion_notify_event (GtkWidget *widget,
535                      GdkEventMotion *event,
536                      ModestAttachmentsView *atts_view)
537 {
538         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
539         if (gtk_grab_get_current () == widget) {
540                 GtkWidget *att_view = NULL;
541
542                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
543                                                    (gint) event->x_root, (gint) event->y_root);
544                 if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_LINKS) {
545                         if (att_view == priv->press_att_view) {
546                                 if (priv->selected == NULL)
547                                 set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
548                         } else {
549                                 if (priv->selected) {
550                                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
551                                 }
552                         }
553                 } else {
554
555                         if (att_view != NULL) {
556                                 unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
557                                 select_range (MODEST_ATTACHMENTS_VIEW (widget), 
558                                               MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
559                                               MODEST_ATTACHMENT_VIEW (att_view));
560                         }
561                 }
562                 gdk_event_request_motions (event);
563         }
564         return TRUE;
565 }
566
567 static GList*
568 find_prev_or_next_not_purged (GList *list, gboolean prev, gboolean include_this)
569 {
570         GList *tmp = NULL;
571         gboolean is_valid;
572
573         if (!include_this) {
574                 if (prev) {
575                         tmp = g_list_previous (list);
576                 } else {
577                         tmp = g_list_next (list);
578                 }
579         } else {
580                 tmp = list;
581         }
582
583         if (!tmp)
584                 return NULL;
585
586         do {
587                 ModestAttachmentView *att_view = (ModestAttachmentView *) tmp->data;
588                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
589                 
590                 /* Do not select purged attachments */
591                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
592                         is_valid = TRUE;
593                 } else {
594                         if (prev)
595                                 tmp = g_list_previous (tmp);
596                         else
597                                 tmp = g_list_next (tmp);
598                         is_valid = FALSE;
599                 }
600                 g_object_unref (mime_part);
601         } while (!is_valid && tmp);
602
603         return tmp;
604 }
605
606
607 static gboolean
608 key_press_event (GtkWidget *widget,
609                  GdkEventKey *event,
610                  ModestAttachmentsView *atts_view)
611 {
612         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
613
614         if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_NO_FOCUS) {
615                 unselect_all (atts_view);
616                 return FALSE;
617         }
618
619         /* If grabbed (for example rubber banding), escape leaves the rubberbanding mode */
620         if (gtk_grab_get_current () == widget) {
621                 if (event->keyval == GDK_Escape) {
622                         set_selected (MODEST_ATTACHMENTS_VIEW (widget),
623                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start));
624                         priv->rubber_start = NULL;
625                         gtk_grab_remove (widget);
626                         return TRUE;
627                 } 
628                 return FALSE;
629         }
630
631         if (event->keyval == GDK_Up) {
632                 ModestAttachmentView *current_sel = NULL;
633                 gboolean move_out = FALSE;
634                 GList * box_children, *new_sel, *first_child;
635
636                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
637                 if (box_children == NULL) {
638                         move_out = TRUE;
639                 } else { 
640                         first_child = box_children;
641                         first_child = find_prev_or_next_not_purged (box_children, FALSE, TRUE);
642                         if (priv->selected != NULL && first_child != NULL) {
643                                 if (priv->selected->data != first_child->data)
644                                         current_sel = (ModestAttachmentView *) priv->selected->data;
645                                 else
646                                         move_out = TRUE;
647                         } else {
648                                 move_out = TRUE;
649                         }
650                 }
651
652                 if (move_out) {
653                         GtkWidget *toplevel = NULL;
654                         /* move cursor outside */
655                         toplevel = gtk_widget_get_toplevel (widget);
656                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
657                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_UP);
658                         unselect_all (atts_view);
659                 } else {
660                         new_sel = g_list_find (box_children, (gpointer) current_sel);
661                         new_sel = find_prev_or_next_not_purged (new_sel, TRUE, FALSE);
662                         /* We assume that we detected properly that
663                            there is a not purge attachment so we don't
664                            need to check NULL */
665                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
666                 }
667                 g_list_free (box_children);
668                 return TRUE;
669         }
670
671         if (event->keyval == GDK_Down) {
672                 ModestAttachmentView *current_sel = NULL;
673                 gboolean move_out = FALSE;
674                 GList * box_children, *new_sel, *last_child = NULL;
675
676                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
677
678                 if (box_children == NULL) {
679                         move_out = TRUE;
680                 } else {
681                         last_child = g_list_last (box_children);
682                         last_child = find_prev_or_next_not_purged (last_child, TRUE, TRUE);
683                         if (priv->selected != NULL && last_child != NULL) {
684                                 GList *last_selected = g_list_last (priv->selected);
685                                 if (last_selected->data != last_child->data)
686                                         current_sel = (ModestAttachmentView *) last_selected->data;
687                                 else
688                                         move_out = TRUE;
689                         } else {
690                                 move_out = TRUE;
691                         }
692                 }
693
694                 if (move_out) {
695                         GtkWidget *toplevel = NULL;
696                         /* move cursor outside */
697                         toplevel = gtk_widget_get_toplevel (widget);
698                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
699                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_DOWN);
700                         unselect_all (atts_view);
701                 } else {
702                         new_sel = g_list_find (box_children, (gpointer) current_sel);
703                         new_sel = find_prev_or_next_not_purged (new_sel, FALSE, FALSE);
704                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
705                 }
706                 g_list_free (box_children);
707                 return TRUE;
708         }
709
710         if (event->keyval == GDK_BackSpace) {
711                 g_signal_emit (G_OBJECT (widget), signals[DELETE_SIGNAL], 0);
712                 return TRUE;
713         }
714
715         /* Activates selected item */
716         if (g_list_length (priv->selected) == 1) {
717                 ModestAttachmentView *att_view = (ModestAttachmentView *) priv->selected->data;
718                 if ((event->keyval == GDK_Return)) {
719                         TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
720                         if (TNY_IS_MIME_PART (mime_part)) {
721                                 g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
722                                 g_object_unref (mime_part);
723                         }
724                         return TRUE;
725                 }
726         }
727
728         return FALSE;
729 }
730
731
732 static GtkWidget *
733 get_att_view_at_coords (ModestAttachmentsView *atts_view,
734                         gdouble x, gdouble y)
735 {
736         ModestAttachmentsViewPrivate *priv = NULL;
737         GList *att_view_list, *node;
738         GtkWidget *result = NULL;
739
740         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
741         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
742         
743         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
744                 GtkWidget *att_view = (GtkWidget *) node->data;
745                 gint pos_x, pos_y, w, h, int_x, int_y;
746                 gint widget_x, widget_y;
747
748                 gdk_window_get_origin (att_view->window, &widget_x, &widget_y);
749
750                 pos_x = widget_x;
751                 pos_y = widget_y;
752                 w = att_view->allocation.width;
753                 h = att_view->allocation.height;
754
755                 int_x = (gint) x - GTK_WIDGET (atts_view)->allocation.x;
756                 int_y = (gint) y - GTK_WIDGET (atts_view)->allocation.y;
757
758                 if ((x >= pos_x) && (x <= (pos_x + w)) && (y >= pos_y) && (y <= (pos_y + h))) {
759                         result = att_view;
760                         break;
761                 }
762         }
763
764         g_list_free (att_view_list);
765         return result;
766 }
767
768 static void
769 unselect_all (ModestAttachmentsView *atts_view)
770 {
771         ModestAttachmentsViewPrivate *priv = NULL;
772         GList *att_view_list, *node;
773
774         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
775         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
776         
777         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
778                 GtkWidget *att_view = (GtkWidget *) node->data;
779
780                 if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED)
781                         gtk_widget_set_state (att_view, GTK_STATE_NORMAL);
782         }
783
784         g_list_free (priv->selected);
785         priv->selected = NULL;
786
787         g_list_free (att_view_list);
788 }
789
790 static void 
791 set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view)
792 {
793         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
794         TnyMimePart *part;
795
796         unselect_all (atts_view);
797         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
798         
799         g_list_free (priv->selected);
800         priv->selected = NULL;
801         if (TNY_IS_MIME_PART (part) && !tny_mime_part_is_purged (part)) {
802                 gtk_widget_set_state (GTK_WIDGET (att_view), GTK_STATE_SELECTED);
803                 priv->selected = g_list_append (priv->selected, att_view);
804         }
805         if (part)
806                 g_object_unref (part);
807         
808         own_clipboard (atts_view);
809 }
810
811 static void 
812 select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2)
813 {
814         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
815         GList *children = NULL;
816         GList *node = NULL;
817         gboolean selecting = FALSE;
818         TnyMimePart *part;
819
820         unselect_all (atts_view);
821
822         if (att1 == att2) {
823                 set_selected (atts_view, att1);
824                 return;
825         }
826
827         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
828         g_list_free (priv->selected);
829         priv->selected = NULL;
830
831
832         for (node = children; node != NULL; node = g_list_next (node)) {
833                 if ((node->data == att1) || (node->data == att2)) {
834                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
835                         if (!tny_mime_part_is_purged (part)) {
836                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
837                                 priv->selected = g_list_append (priv->selected, node->data);
838                         }
839                         g_object_unref (part);
840                         selecting = !selecting;
841                 } else if (selecting) {
842                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
843                         if (!tny_mime_part_is_purged (part)) {
844                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
845                                 priv->selected = g_list_append (priv->selected, node->data);
846                         }
847                         g_object_unref (part);
848                 }
849                         
850         }
851         g_list_free (children);
852         
853         own_clipboard (atts_view);
854 }
855
856 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
857                            guint info, gpointer userdata)
858 {
859         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
860         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
861
862         if ((priv->selected != NULL)&&(priv->selected->next == NULL)) {
863                 if (info == MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX) {
864                         /* MODEST_ATTACHMENT requested. As the content id is not filled in all the case, we'll
865                          * use an internal index. This index is simply the index of the attachment in the vbox */
866                         GList *box_children = NULL;
867                         gint index;
868                         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
869                         index = g_list_index (box_children, priv->selected);
870                         if (index >= 0) {
871                                 gchar *index_str = g_strdup_printf("%d", index);
872                                 gtk_selection_data_set_text (selection_data, index_str, -1);
873                                 g_free (index_str);
874                         }
875                 }
876         }
877 }
878
879 TnyList *
880 modest_attachments_view_get_selection (ModestAttachmentsView *atts_view)
881 {
882         ModestAttachmentsViewPrivate *priv;
883         TnyList *selection;
884         GList *node;
885
886         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
887         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
888
889         selection = tny_simple_list_new ();
890         for (node = priv->selected; node != NULL; node = g_list_next (node)) {
891                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
892                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
893                 tny_list_append (selection, (GObject *) part);
894                 g_object_unref (part);
895         }
896         
897         return selection;
898 }
899
900 TnyList *
901 modest_attachments_view_get_attachments (ModestAttachmentsView *atts_view)
902 {
903         ModestAttachmentsViewPrivate *priv;
904         TnyList *att_list;
905         GList *children, *node= NULL;
906
907         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
908         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
909
910         att_list = TNY_LIST (tny_simple_list_new ());
911
912         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
913         for (node = children; node != NULL; node = g_list_next (node)) {
914                 GtkWidget *att_view = GTK_WIDGET (node->data);
915                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
916                 tny_list_append (att_list, (GObject *) mime_part);
917                 g_object_unref (mime_part);
918         }
919         g_list_free (children);
920         return att_list;
921
922 }
923
924 void
925 modest_attachments_view_select_all (ModestAttachmentsView *atts_view)
926 {
927         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
928         GList *children = NULL;
929         GList *node = NULL;
930
931         unselect_all (atts_view);
932
933         if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_LINKS)
934                 return;
935
936         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
937         g_list_free (priv->selected);
938         priv->selected = NULL;
939
940         for (node = children; node != NULL; node = g_list_next (node)) {
941                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
942                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
943
944                 /* Do not select purged attachments */
945                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
946                         gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
947                         priv->selected = g_list_append (priv->selected, node->data);
948                 }
949                 g_object_unref (mime_part);
950         }
951         g_list_free (children);
952
953         own_clipboard (atts_view);
954 }
955
956 gboolean
957 modest_attachments_view_has_attachments (ModestAttachmentsView *atts_view)
958 {
959         ModestAttachmentsViewPrivate *priv;
960         GList *children;
961         gboolean result;
962
963         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), FALSE);
964         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
965
966         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
967         result = (children != NULL);
968         g_list_free (children);
969
970         return result;
971 }
972
973 void
974 modest_attachments_view_get_sizes (ModestAttachmentsView *attachments_view,
975                                    gint *attachments_count,
976                                    guint64 *attachments_size)
977 {
978         ModestAttachmentsViewPrivate *priv;
979         GList *children, *node;
980
981         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
982         g_return_if_fail (attachments_count != NULL && attachments_size != NULL);
983
984         *attachments_count = 0;
985         *attachments_size = 0;
986
987         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
988
989         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
990         for (node = children; node != NULL; node = g_list_next (node)) {
991                 GtkWidget *att_view = (GtkWidget *) node->data;
992                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
993
994                 if (!tny_mime_part_is_purged (part)) {
995                         guint64 size;
996                         (*attachments_count) ++;
997                         size = modest_attachment_view_get_size (MODEST_ATTACHMENT_VIEW (att_view));
998                         if (size == 0) {
999                                 /* we do a random estimation of the size of an attachment */
1000                                 size = 32768;
1001                         }
1002                         *attachments_size += size;
1003                 }
1004                 g_object_unref (part);
1005         }
1006         g_list_free (children);
1007 }
1008
1009 static void
1010 dummy_clear_func (GtkClipboard *clipboard,
1011                   gpointer user_data_or_owner)
1012 {
1013         /* Do nothing */
1014 }
1015
1016 static void
1017 own_clipboard (ModestAttachmentsView *atts_view)
1018 {
1019         GtkTargetEntry targets[] = {
1020                 {MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE, 0, MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX},
1021         };
1022
1023         gtk_clipboard_set_with_owner (gtk_widget_get_clipboard (GTK_WIDGET (atts_view), GDK_SELECTION_PRIMARY),
1024                                       targets, G_N_ELEMENTS (targets),
1025                                       clipboard_get, dummy_clear_func, G_OBJECT(atts_view));
1026 }
1027
1028 static gboolean 
1029 focus_out_event (GtkWidget *widget, GdkEventFocus *event, ModestAttachmentsView *atts_view)
1030 {
1031         if (!gtk_widget_is_focus (widget))
1032                 unselect_all (atts_view);
1033
1034         return FALSE;
1035 }
1036
1037 static gboolean 
1038 focus (GtkWidget *widget, GtkDirectionType direction, ModestAttachmentsView *atts_view)
1039 {
1040         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
1041         GList *children = NULL;
1042         GtkWidget *toplevel = NULL;
1043
1044         toplevel = gtk_widget_get_toplevel (widget);
1045         if (!gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)))
1046                 return FALSE;
1047
1048         if (priv->style != MODEST_ATTACHMENTS_VIEW_STYLE_NO_FOCUS) {
1049                 children = gtk_container_get_children (GTK_CONTAINER (priv->box));
1050                 if (children != NULL) {
1051                         set_selected (atts_view, MODEST_ATTACHMENT_VIEW (children->data));
1052                 }
1053                 g_list_free (children);
1054         }
1055
1056         return FALSE;
1057 }
1058
1059 void 
1060 modest_attachments_view_set_style (ModestAttachmentsView *self,
1061                                    ModestAttachmentsViewStyle style)
1062 {
1063         ModestAttachmentsViewPrivate *priv;
1064
1065         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (self));
1066         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (self);
1067
1068         if (priv->style != style) {
1069                 priv->style = style;
1070                 gtk_widget_queue_draw (GTK_WIDGET (self));
1071                 if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_SELECTABLE) {
1072                         GTK_WIDGET_SET_FLAGS (self, GTK_CAN_FOCUS);
1073                 } else {
1074                         GTK_WIDGET_UNSET_FLAGS (self, GTK_CAN_FOCUS);
1075                 }
1076
1077         }
1078 }
1079
1080 guint
1081 modest_attachments_view_get_num_attachments (ModestAttachmentsView *atts_view)
1082 {
1083         ModestAttachmentsViewPrivate *priv;
1084         GList *children;
1085         gint result;
1086
1087         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), 0);
1088         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
1089
1090         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
1091         result = g_list_length (children);
1092         g_list_free (children);
1093
1094         return result;
1095 }