* src/widgets/modest-attachments-view.c:
[modest] / src / widgets / modest-attachments-view.c
1 /* Copyright (c) 2007, 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
44 static GObjectClass *parent_class = NULL;
45
46 /* signals */
47 enum {
48         ACTIVATE_SIGNAL,
49         LAST_SIGNAL
50 };
51
52 typedef struct _ModestAttachmentsViewPrivate ModestAttachmentsViewPrivate;
53
54 struct _ModestAttachmentsViewPrivate
55 {
56         TnyMsg *msg;
57         GtkWidget *box;
58         GList *selected;
59         GtkWidget *rubber_start;
60 };
61
62 #define MODEST_ATTACHMENTS_VIEW_GET_PRIVATE(o)  \
63         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ATTACHMENTS_VIEW, ModestAttachmentsViewPrivate))
64
65 static gboolean button_press_event (GtkWidget *widget, GdkEventButton *event, ModestAttachmentsView *atts_view);
66 static gboolean motion_notify_event (GtkWidget *widget, GdkEventMotion *event, ModestAttachmentsView *atts_view);
67 static gboolean button_release_event (GtkWidget *widget, GdkEventButton *event, ModestAttachmentsView *atts_view);
68 static gboolean key_press_event (GtkWidget *widget, GdkEventKey *event, ModestAttachmentsView *atts_view);
69 static gboolean focus_out_event (GtkWidget *widget, GdkEventFocus *event, ModestAttachmentsView *atts_view);
70 static gboolean focus (GtkWidget *widget, GtkDirectionType direction, ModestAttachmentsView *atts_view);
71 static GtkWidget *get_att_view_at_coords (ModestAttachmentsView *atts_view,
72                                           gdouble x, gdouble y);
73 static void unselect_all (ModestAttachmentsView *atts_view);
74 static void set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view);
75 static void select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2);
76 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
77                            guint info, gpointer userdata);
78 static void clipboard_clear (GtkClipboard *clipboard, gpointer userdata);
79 static void own_clipboard (ModestAttachmentsView *atts_view);
80
81 static guint signals[LAST_SIGNAL] = {0};
82
83 /**
84  * modest_attachments_view_new:
85  * @msg: a #TnyMsg
86  *
87  * Constructor for attachments view widget.
88  *
89  * Return value: a new #ModestAttachmentsView instance implemented for Gtk+
90  **/
91 GtkWidget*
92 modest_attachments_view_new (TnyMsg *msg)
93 {
94         ModestAttachmentsView *self = g_object_new (MODEST_TYPE_ATTACHMENTS_VIEW, 
95                                                     "resize-mode", GTK_RESIZE_PARENT,
96                                                     NULL);
97
98         modest_attachments_view_set_message (self, msg);
99
100         return GTK_WIDGET (self);
101 }
102
103 void
104 modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, TnyMsg *msg)
105 {
106         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
107         TnyList *parts;
108         TnyIterator *iter;
109         const gchar *msg_content_type = NULL;
110         
111         if (msg == priv->msg) return;
112
113         if (priv->msg)
114                 g_object_unref (priv->msg);
115         if (msg)
116                 g_object_ref (G_OBJECT(msg));
117         
118         priv->msg = msg;
119
120         g_list_free (priv->selected);
121         priv->selected = NULL;
122
123         gtk_container_foreach (GTK_CONTAINER (priv->box), (GtkCallback) gtk_widget_destroy, NULL);
124         
125         if (priv->msg == NULL) {
126                 return;
127         }
128
129         /* If the top mime part is a multipart/related, we don't show the attachments, as they're
130          * embedded images in body */
131         msg_content_type = tny_mime_part_get_content_type (TNY_MIME_PART (priv->msg));
132         if ((msg_content_type != NULL) && !strcasecmp (msg_content_type, "multipart/related")) {
133                 gchar *header_content_type;
134                 gchar *header_content_type_lower;
135                 gboolean application_multipart = FALSE;
136                 header_content_type = modest_tny_mime_part_get_header_value (TNY_MIME_PART (priv->msg), "Content-Type");
137                 header_content_type = g_strstrip (header_content_type);
138                 header_content_type_lower = (header_content_type ) ?
139                         g_ascii_strdown (header_content_type, -1) : NULL;
140                 
141                 if ((header_content_type_lower != NULL) && 
142                     !strstr (header_content_type_lower, "application/")) {
143                         application_multipart = TRUE;
144                         g_free (header_content_type_lower);
145                 }
146                 g_free (header_content_type);
147
148                 if (application_multipart) {
149                         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
150                         return;
151                 }
152         } else {
153                 gchar *lower;
154                 gboolean direct_attach;
155
156                 lower = g_ascii_strdown (msg_content_type, -1);
157                 direct_attach = (!g_str_has_prefix (lower, "message/rfc822") && 
158                                  !g_str_has_prefix (lower, "multipart") && 
159                                  !g_str_has_prefix (lower, "text/"));
160                 g_free (lower);
161                 if (direct_attach) {
162                         modest_attachments_view_add_attachment (attachments_view, TNY_MIME_PART (msg), TRUE, 0);
163                         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
164                         return;
165                 }
166         }
167
168         parts = TNY_LIST (tny_simple_list_new ());
169         tny_mime_part_get_parts (TNY_MIME_PART (priv->msg), parts);
170         iter = tny_list_create_iterator (parts);
171
172         while (!tny_iterator_is_done (iter)) {
173                 TnyMimePart *part;
174
175                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
176
177                 if (part && (modest_tny_mime_part_is_attachment_for_modest (part))) 
178                         modest_attachments_view_add_attachment (attachments_view, part, TRUE, 0);
179
180                 if (part)
181                         g_object_unref (part);
182
183                 tny_iterator_next (iter);
184         }
185         g_object_unref (iter);
186         g_object_unref (parts);
187         
188
189         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
190
191 }
192
193 void
194 modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part,
195                                         gboolean detect_size, guint64 size)
196 {
197         GtkWidget *att_view = NULL;
198         ModestAttachmentsViewPrivate *priv = NULL;
199
200         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
201         g_return_if_fail (TNY_IS_MIME_PART (part));
202
203         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
204
205         att_view = modest_attachment_view_new (part, detect_size);
206         if (!detect_size)
207                 modest_attachment_view_set_size (MODEST_ATTACHMENT_VIEW (att_view), size);
208         gtk_box_pack_end (GTK_BOX (priv->box), att_view, FALSE, FALSE, 0);
209         gtk_widget_show_all (att_view);
210 }
211
212 void
213 modest_attachments_view_remove_attachment (ModestAttachmentsView *atts_view, TnyMimePart *mime_part)
214 {
215         ModestAttachmentsViewPrivate *priv = NULL;
216         GList *box_children = NULL, *node = NULL;
217         ModestAttachmentView *found_att_view = NULL;
218
219         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
220         g_return_if_fail (TNY_IS_MIME_PART (mime_part));
221
222         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
223         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
224
225         for (node = box_children; node != NULL; node = g_list_next (node)) {
226                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
227                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
228
229                 if (mime_part == cur_mime_part)
230                         found_att_view = att_view;
231
232                 g_object_unref (cur_mime_part);
233
234                 if (found_att_view != NULL)
235                         break;
236         }
237
238         if (found_att_view) {
239                 GList *node = NULL;
240                 GtkWidget *next_widget = NULL;
241                 GList *box_children = NULL;
242
243                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
244                 node = g_list_find (box_children, found_att_view);
245                 if (node->next)
246                         next_widget = node->next->data;
247
248                 g_list_free (box_children);
249
250                 node = g_list_find (priv->selected, found_att_view);
251                 if (node != NULL) {
252                         priv->selected = g_list_delete_link (priv->selected, node);
253                         gtk_widget_destroy (GTK_WIDGET (found_att_view));
254                         if ((priv->selected == NULL) && (next_widget != NULL))
255                                 set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), 
256                                               MODEST_ATTACHMENT_VIEW (next_widget));
257                 }
258                 own_clipboard (atts_view);
259         }
260
261 }
262
263 void
264 modest_attachments_view_remove_attachment_by_id (ModestAttachmentsView *atts_view, const gchar *att_id)
265 {
266         ModestAttachmentsViewPrivate *priv = NULL;
267         GList *box_children = NULL, *node = NULL;
268
269         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
270         g_return_if_fail (att_id != NULL);
271
272         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
273         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
274
275         for (node = box_children; node != NULL; node = g_list_next (node)) {
276                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
277                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
278                 const gchar *mime_part_id = NULL;
279
280                 mime_part_id = tny_mime_part_get_content_id (cur_mime_part);
281                 if ((mime_part_id != NULL) && (strcmp (mime_part_id, att_id) == 0)) {
282                         gtk_widget_destroy (GTK_WIDGET (att_view));
283                         priv->selected = g_list_remove (priv->selected, att_view);
284                 }
285
286                 g_object_unref (cur_mime_part);
287         }
288
289         own_clipboard (atts_view);
290
291 }
292
293 static void
294 modest_attachments_view_instance_init (GTypeInstance *instance, gpointer g_class)
295 {
296         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (instance);
297
298         priv->msg = NULL;
299         priv->box = gtk_vbox_new (FALSE, 0);
300         priv->rubber_start = NULL;
301         priv->selected = NULL;
302
303         gtk_container_add (GTK_CONTAINER (instance), priv->box);
304         gtk_event_box_set_above_child (GTK_EVENT_BOX (instance), TRUE);
305
306         g_signal_connect (G_OBJECT (instance), "button-press-event", G_CALLBACK (button_press_event), instance);
307         g_signal_connect (G_OBJECT (instance), "button-release-event", G_CALLBACK (button_release_event), instance);
308         g_signal_connect (G_OBJECT (instance), "motion-notify-event", G_CALLBACK (motion_notify_event), instance);
309         g_signal_connect (G_OBJECT (instance), "key-press-event", G_CALLBACK (key_press_event), instance);
310         g_signal_connect (G_OBJECT (instance), "focus-out-event", G_CALLBACK (focus_out_event), instance);
311         g_signal_connect (G_OBJECT (instance), "focus", G_CALLBACK (focus), instance);
312
313         GTK_WIDGET_SET_FLAGS (instance, GTK_CAN_FOCUS);
314
315         return;
316 }
317
318 static void
319 modest_attachments_view_finalize (GObject *object)
320 {
321         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (object);
322
323         if (priv->msg) {
324                 g_object_unref (priv->msg);
325                 priv->msg = NULL;
326         }
327
328         (*parent_class->finalize) (object);
329
330         return;
331 }
332
333 static void 
334 modest_attachments_view_class_init (ModestAttachmentsViewClass *klass)
335 {
336         GObjectClass *object_class;
337         GtkWidgetClass *widget_class;
338
339         parent_class = g_type_class_peek_parent (klass);
340         object_class = (GObjectClass*) klass;
341         widget_class = GTK_WIDGET_CLASS (klass);
342
343         object_class->finalize = modest_attachments_view_finalize;
344
345         klass->activate = NULL;
346
347         g_type_class_add_private (object_class, sizeof (ModestAttachmentsViewPrivate));
348
349         signals[ACTIVATE_SIGNAL] =
350                 g_signal_new ("activate",
351                               G_TYPE_FROM_CLASS (object_class),
352                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
353                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, activate),
354                               NULL, NULL,
355                               g_cclosure_marshal_VOID__OBJECT,
356                               G_TYPE_NONE, 1, G_TYPE_OBJECT);
357         
358         return;
359 }
360
361 GType 
362 modest_attachments_view_get_type (void)
363 {
364         static GType type = 0;
365
366         if (G_UNLIKELY(type == 0))
367         {
368                 static const GTypeInfo info = 
369                 {
370                   sizeof (ModestAttachmentsViewClass),
371                   NULL,   /* base_init */
372                   NULL,   /* base_finalize */
373                   (GClassInitFunc) modest_attachments_view_class_init,   /* class_init */
374                   NULL,   /* class_finalize */
375                   NULL,   /* class_data */
376                   sizeof (ModestAttachmentsView),
377                   0,      /* n_preallocs */
378                   modest_attachments_view_instance_init    /* instance_init */
379                 };
380
381                 type = g_type_register_static (GTK_TYPE_EVENT_BOX,
382                         "ModestAttachmentsView",
383                         &info, 0);
384
385         }
386
387         return type;
388 }
389
390 /* buttons signal events */
391 static gboolean
392 button_press_event (GtkWidget *widget, 
393                     GdkEventButton *event,
394                     ModestAttachmentsView *atts_view)
395 {
396         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
397         if (!GTK_WIDGET_HAS_FOCUS (widget))
398                 gtk_widget_grab_focus (widget);
399
400         if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
401                 GtkWidget *att_view = NULL;
402
403                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
404                                                    (gint) event->x_root, (gint) event->y_root);
405
406                 if (att_view != NULL) {
407                         if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED && (g_list_length (priv->selected) < 2)) {
408                                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
409                                 if (TNY_IS_MIME_PART (mime_part)) {
410                                         g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
411                                         g_object_unref (mime_part);
412                                 }
413                         } else {
414                                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
415
416                                 /* Do not select purged attachments */
417                                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
418                                         set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
419                                         priv->rubber_start = att_view;
420                                         gtk_grab_add (widget);
421                                 }
422                                 g_object_unref (mime_part);
423                         }
424                 }
425         }
426         return TRUE;
427
428 }
429
430 static gboolean
431 button_release_event (GtkWidget *widget,
432                       GdkEventButton *event,
433                       ModestAttachmentsView *atts_view)
434 {
435         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
436         if (widget == gtk_grab_get_current ()) {
437                 GtkWidget *att_view = NULL;
438
439                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
440                                                    (gint) event->x_root, (gint) event->y_root);
441
442                 if (att_view != NULL) {
443                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
444                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
445                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
446                                       MODEST_ATTACHMENT_VIEW (att_view));
447                 }
448                 priv->rubber_start = NULL;
449                 gtk_grab_remove (widget);
450         }
451         return TRUE;
452 }
453
454 static gboolean
455 motion_notify_event (GtkWidget *widget,
456                      GdkEventMotion *event,
457                      ModestAttachmentsView *atts_view)
458 {
459         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
460         if (gtk_grab_get_current () == widget) {
461                 GtkWidget *att_view = NULL;
462
463                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
464                                                    (gint) event->x_root, (gint) event->y_root);
465
466                 if (att_view != NULL) {
467                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
468                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
469                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
470                                       MODEST_ATTACHMENT_VIEW (att_view));
471                 }
472         }
473         return TRUE;
474 }
475
476 static GList*
477 find_prev_or_next_not_purged (GList *list, gboolean prev, gboolean include_this)
478 {
479         GList *tmp = NULL;
480         gboolean is_valid;
481
482         if (!include_this) {
483                 if (prev) {
484                         tmp = g_list_previous (list);
485                 } else {
486                         tmp = g_list_next (list);
487                 }
488         } else {
489                 tmp = list;
490         }
491
492         if (!tmp)
493                 return NULL;
494
495         do {
496                 ModestAttachmentView *att_view = (ModestAttachmentView *) tmp->data;
497                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
498                 
499                 /* Do not select purged attachments */
500                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
501                         is_valid = TRUE;
502                 } else {
503                         if (prev)
504                                 tmp = g_list_previous (tmp);
505                         else
506                                 tmp = g_list_next (tmp);
507                         is_valid = FALSE;
508                 }
509                 g_object_unref (mime_part);
510         } while (!is_valid && tmp);
511
512         return tmp;
513 }
514
515
516 static gboolean
517 key_press_event (GtkWidget *widget,
518                  GdkEventKey *event,
519                  ModestAttachmentsView *atts_view)
520 {
521         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
522
523         /* If grabbed (for example rubber banding), escape leaves the rubberbanding mode */
524         if (gtk_grab_get_current () == widget) {
525                 if (event->keyval == GDK_Escape) {
526                         set_selected (MODEST_ATTACHMENTS_VIEW (widget),
527                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start));
528                         priv->rubber_start = NULL;
529                         gtk_grab_remove (widget);
530                         return TRUE;
531                 } 
532                 return FALSE;
533         }
534
535         if (event->keyval == GDK_Up) {
536                 ModestAttachmentView *current_sel = NULL;
537                 gboolean move_out = FALSE;
538                 GList * box_children, *new_sel, *first_child;
539
540                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
541                 if (box_children == NULL) {
542                         move_out = TRUE;
543                 } else { 
544                         first_child = box_children;
545                         first_child = find_prev_or_next_not_purged (box_children, FALSE, TRUE);
546                         if (priv->selected != NULL && first_child != NULL) {
547                                 if (priv->selected->data != first_child->data)
548                                         current_sel = (ModestAttachmentView *) priv->selected->data;
549                                 else
550                                         move_out = TRUE;
551                         } else {
552                                 move_out = TRUE;
553                         }
554                 }
555
556                 if (move_out) {
557                         GtkWidget *toplevel = NULL;
558                         /* move cursor outside */
559                         toplevel = gtk_widget_get_toplevel (widget);
560                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
561                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_UP);
562                         unselect_all (atts_view);
563                 } else {
564                         new_sel = g_list_find (box_children, (gpointer) current_sel);
565                         new_sel = find_prev_or_next_not_purged (new_sel, TRUE, FALSE);
566                         /* We assume that we detected properly that
567                            there is a not purge attachment so we don't
568                            need to check NULL */
569                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
570                 }
571                 g_list_free (box_children);
572                 return TRUE;
573         }
574
575         if (event->keyval == GDK_Down) {
576                 ModestAttachmentView *current_sel = NULL;
577                 gboolean move_out = FALSE;
578                 GList * box_children, *new_sel, *last_child = NULL;
579
580                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
581
582                 if (box_children == NULL) {
583                         move_out = TRUE;
584                 } else {
585                         last_child = g_list_last (box_children);
586                         last_child = find_prev_or_next_not_purged (last_child, TRUE, TRUE);
587                         if (priv->selected != NULL && last_child != NULL) {
588                                 GList *last_selected = g_list_last (priv->selected);
589                                 if (last_selected->data != last_child->data)
590                                         current_sel = (ModestAttachmentView *) last_selected->data;
591                                 else
592                                         move_out = TRUE;
593                         } else {
594                                 move_out = TRUE;
595                         }
596                 }
597
598                 if (move_out) {
599                         GtkWidget *toplevel = NULL;
600                         /* move cursor outside */
601                         toplevel = gtk_widget_get_toplevel (widget);
602                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
603                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_DOWN);
604                         unselect_all (atts_view);
605                 } else {
606                         new_sel = g_list_find (box_children, (gpointer) current_sel);
607                         new_sel = find_prev_or_next_not_purged (new_sel, FALSE, FALSE);
608                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
609                 }
610                 g_list_free (box_children);
611                 return TRUE;
612         }
613
614         /* Activates selected item */
615         if (g_list_length (priv->selected) == 1) {
616                 ModestAttachmentView *att_view = (ModestAttachmentView *) priv->selected->data;
617                 if ((event->keyval == GDK_Return)) {
618                         TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
619                         if (TNY_IS_MIME_PART (mime_part)) {
620                                 g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
621                                 g_object_unref (mime_part);
622                         }
623                         return TRUE;
624                 }
625         }
626
627         return FALSE;
628 }
629
630
631 static GtkWidget *
632 get_att_view_at_coords (ModestAttachmentsView *atts_view,
633                         gdouble x, gdouble y)
634 {
635         ModestAttachmentsViewPrivate *priv = NULL;
636         GList *att_view_list, *node;
637         GtkWidget *result = NULL;
638
639         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
640         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
641         
642         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
643                 GtkWidget *att_view = (GtkWidget *) node->data;
644                 gint pos_x, pos_y, w, h, int_x, int_y;
645                 gint widget_x, widget_y;
646
647                 gdk_window_get_origin (att_view->window, &widget_x, &widget_y);
648
649                 pos_x = widget_x;
650                 pos_y = widget_y;
651                 w = att_view->allocation.width;
652                 h = att_view->allocation.height;
653
654                 int_x = (gint) x - GTK_WIDGET (atts_view)->allocation.x;
655                 int_y = (gint) y - GTK_WIDGET (atts_view)->allocation.y;
656
657                 if ((x >= pos_x) && (x <= (pos_x + w)) && (y >= pos_y) && (y <= (pos_y + h))) {
658                         result = att_view;
659                         break;
660                 }
661         }
662
663         g_list_free (att_view_list);
664         return result;
665 }
666
667 static void
668 unselect_all (ModestAttachmentsView *atts_view)
669 {
670         ModestAttachmentsViewPrivate *priv = NULL;
671         GList *att_view_list, *node;
672
673         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
674         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
675         
676         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
677                 GtkWidget *att_view = (GtkWidget *) node->data;
678
679                 if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED)
680                         gtk_widget_set_state (att_view, GTK_STATE_NORMAL);
681         }
682
683         g_list_free (priv->selected);
684         priv->selected = NULL;
685
686         g_list_free (att_view_list);
687 }
688
689 static void 
690 set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view)
691 {
692         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
693         TnyMimePart *part;
694
695         unselect_all (atts_view);
696         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
697         
698         g_list_free (priv->selected);
699         priv->selected = NULL;
700         if (TNY_IS_MIME_PART (part) && !tny_mime_part_is_purged (part)) {
701                 gtk_widget_set_state (GTK_WIDGET (att_view), GTK_STATE_SELECTED);
702                 priv->selected = g_list_append (priv->selected, att_view);
703         }
704         if (part)
705                 g_object_unref (part);
706         
707         own_clipboard (atts_view);
708 }
709
710 static void 
711 select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2)
712 {
713         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
714         GList *children = NULL;
715         GList *node = NULL;
716         gboolean selecting = FALSE;
717         TnyMimePart *part;
718
719         unselect_all (atts_view);
720
721         if (att1 == att2) {
722                 set_selected (atts_view, att1);
723                 return;
724         }
725
726         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
727         g_list_free (priv->selected);
728         priv->selected = NULL;
729
730
731         for (node = children; node != NULL; node = g_list_next (node)) {
732                 if ((node->data == att1) || (node->data == att2)) {
733                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
734                         if (!tny_mime_part_is_purged (part)) {
735                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
736                                 priv->selected = g_list_append (priv->selected, node->data);
737                         }
738                         g_object_unref (part);
739                         selecting = !selecting;
740                 } else if (selecting) {
741                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
742                         if (!tny_mime_part_is_purged (part)) {
743                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
744                                 priv->selected = g_list_append (priv->selected, node->data);
745                         }
746                         g_object_unref (part);
747                 }
748                         
749         }
750         g_list_free (children);
751         
752         own_clipboard (atts_view);
753 }
754
755 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
756                            guint info, gpointer userdata)
757 {
758         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
759         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
760
761         if ((priv->selected != NULL)&&(priv->selected->next == NULL)) {
762                 if (info == MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX) {
763                         /* MODEST_ATTACHMENT requested. As the content id is not filled in all the case, we'll
764                          * use an internal index. This index is simply the index of the attachment in the vbox */
765                         GList *box_children = NULL;
766                         gint index;
767                         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
768                         index = g_list_index (box_children, priv->selected);
769                         if (index >= 0) {
770                                 gchar *index_str = g_strdup_printf("%d", index);
771                                 gtk_selection_data_set_text (selection_data, index_str, -1);
772                                 g_free (index_str);
773                         }
774                 }
775         }
776 }
777
778 static void clipboard_clear (GtkClipboard *clipboard, gpointer userdata)
779 {
780         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
781
782         unselect_all (atts_view);
783 }
784
785 TnyList *
786 modest_attachments_view_get_selection (ModestAttachmentsView *atts_view)
787 {
788         ModestAttachmentsViewPrivate *priv;
789         TnyList *selection;
790         GList *node;
791
792         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
793         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
794
795         selection = tny_simple_list_new ();
796         for (node = priv->selected; node != NULL; node = g_list_next (node)) {
797                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
798                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
799                 tny_list_append (selection, (GObject *) part);
800                 g_object_unref (part);
801         }
802         
803         return selection;
804 }
805
806 TnyList *
807 modest_attachments_view_get_attachments (ModestAttachmentsView *atts_view)
808 {
809         ModestAttachmentsViewPrivate *priv;
810         TnyList *att_list;
811         GList *children, *node= NULL;
812
813         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
814         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
815
816         att_list = TNY_LIST (tny_simple_list_new ());
817
818         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
819         for (node = children; node != NULL; node = g_list_next (node)) {
820                 GtkWidget *att_view = GTK_WIDGET (node->data);
821                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
822                 tny_list_append (att_list, (GObject *) mime_part);
823                 g_object_unref (mime_part);
824         }
825         g_list_free (children);
826         return att_list;
827
828 }
829
830 void
831 modest_attachments_view_select_all (ModestAttachmentsView *atts_view)
832 {
833         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
834         GList *children = NULL;
835         GList *node = NULL;
836
837         unselect_all (atts_view);
838
839         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
840         g_list_free (priv->selected);
841         priv->selected = NULL;
842
843         for (node = children; node != NULL; node = g_list_next (node)) {
844                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
845                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
846
847                 /* Do not select purged attachments */
848                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
849                         gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
850                         priv->selected = g_list_append (priv->selected, node->data);
851                 }
852                 g_object_unref (mime_part);
853         }
854         g_list_free (children);
855
856         own_clipboard (atts_view);
857 }
858
859 gboolean
860 modest_attachments_view_has_attachments (ModestAttachmentsView *atts_view)
861 {
862         ModestAttachmentsViewPrivate *priv;
863         GList *children;
864         gboolean result;
865
866         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), FALSE);
867         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
868
869         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
870         result = (children != NULL);
871         g_list_free (children);
872
873         return result;
874 }
875
876 void
877 modest_attachments_view_get_sizes (ModestAttachmentsView *attachments_view,
878                                    gint *attachments_count,
879                                    guint64 *attachments_size)
880 {
881         ModestAttachmentsViewPrivate *priv;
882         GList *children, *node;
883
884         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
885         g_return_if_fail (attachments_count != NULL && attachments_size != NULL);
886
887         *attachments_count = 0;
888         *attachments_size = 0;
889
890         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
891
892         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
893         for (node = children; node != NULL; node = g_list_next (node)) {
894                 GtkWidget *att_view = (GtkWidget *) node->data;
895                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
896
897                 if (!tny_mime_part_is_purged (part)) {
898                         guint64 size;
899                         (*attachments_count) ++;
900                         size = modest_attachment_view_get_size (MODEST_ATTACHMENT_VIEW (att_view));
901                         if (size == 0) {
902                                 /* we do a random estimation of the size of an attachment */
903                                 size = 32768;
904                         }
905                         *attachments_size += size;
906                         
907                 }
908                 g_object_unref (part);
909         }
910         g_list_free (children);
911 }
912
913 static void
914 own_clipboard (ModestAttachmentsView *atts_view)
915 {
916         GtkTargetEntry targets[] = {
917                 {MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE, 0, MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX},
918         };
919
920         gtk_clipboard_set_with_owner (gtk_widget_get_clipboard (GTK_WIDGET (atts_view), GDK_SELECTION_PRIMARY),
921                                       targets, G_N_ELEMENTS (targets),
922                                       clipboard_get, clipboard_clear, G_OBJECT(atts_view));
923                               
924 }
925
926 static gboolean 
927 focus_out_event (GtkWidget *widget, GdkEventFocus *event, ModestAttachmentsView *atts_view)
928 {
929         if (!gtk_widget_is_focus (widget))
930                 unselect_all (atts_view);
931
932         return FALSE;
933 }
934
935 static gboolean 
936 focus (GtkWidget *widget, GtkDirectionType direction, ModestAttachmentsView *atts_view)
937 {
938         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
939         GList *children = NULL;
940         GtkWidget *toplevel = NULL;
941
942         toplevel = gtk_widget_get_toplevel (widget);
943         if (!gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)))
944                 return FALSE;
945
946         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
947         if (children != NULL) {
948                 set_selected (atts_view, MODEST_ATTACHMENT_VIEW (children->data));
949         }
950         g_list_free (children);
951
952         return FALSE;
953 }