* Partially fixes NB#81238, fixes some problems reported by coverity
[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                         }
423                 }
424         }
425         return TRUE;
426
427 }
428
429 static gboolean
430 button_release_event (GtkWidget *widget,
431                       GdkEventButton *event,
432                       ModestAttachmentsView *atts_view)
433 {
434         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
435         if (widget == gtk_grab_get_current ()) {
436                 GtkWidget *att_view = NULL;
437
438                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
439                                                    (gint) event->x_root, (gint) event->y_root);
440
441                 if (att_view != NULL) {
442                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
443                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
444                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
445                                       MODEST_ATTACHMENT_VIEW (att_view));
446                 }
447                 priv->rubber_start = NULL;
448                 gtk_grab_remove (widget);
449         }
450         return TRUE;
451 }
452
453 static gboolean
454 motion_notify_event (GtkWidget *widget,
455                      GdkEventMotion *event,
456                      ModestAttachmentsView *atts_view)
457 {
458         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
459         if (gtk_grab_get_current () == widget) {
460                 GtkWidget *att_view = NULL;
461
462                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
463                                                    (gint) event->x_root, (gint) event->y_root);
464
465                 if (att_view != NULL) {
466                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
467                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
468                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
469                                       MODEST_ATTACHMENT_VIEW (att_view));
470                 }
471         }
472         return TRUE;
473 }
474
475 static GList*
476 find_prev_or_next_not_purged (GList *list, gboolean prev, gboolean include_this)
477 {
478         GList *tmp = NULL;
479         gboolean is_valid;
480
481         if (!include_this) {
482                 if (prev) {
483                         tmp = g_list_previous (list);
484                 } else {
485                         tmp = g_list_next (list);
486                 }
487         } else {
488                 tmp = list;
489         }
490
491         if (!tmp)
492                 return NULL;
493
494         do {
495                 ModestAttachmentView *att_view = (ModestAttachmentView *) tmp->data;
496                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
497                 
498                 /* Do not select purged attachments */
499                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
500                         is_valid = TRUE;
501                 } else {
502                         if (prev)
503                                 tmp = g_list_previous (tmp);
504                         else
505                                 tmp = g_list_next (tmp);
506                         is_valid = FALSE;
507                 }
508         } while (!is_valid && tmp);
509
510         return tmp;
511 }
512
513
514 static gboolean
515 key_press_event (GtkWidget *widget,
516                  GdkEventKey *event,
517                  ModestAttachmentsView *atts_view)
518 {
519         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
520
521         /* If grabbed (for example rubber banding), escape leaves the rubberbanding mode */
522         if (gtk_grab_get_current () == widget) {
523                 if (event->keyval == GDK_Escape) {
524                         set_selected (MODEST_ATTACHMENTS_VIEW (widget),
525                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start));
526                         priv->rubber_start = NULL;
527                         gtk_grab_remove (widget);
528                         return TRUE;
529                 } 
530                 return FALSE;
531         }
532
533         if (event->keyval == GDK_Up) {
534                 ModestAttachmentView *current_sel = NULL;
535                 gboolean move_out = FALSE;
536                 GList * box_children, *new_sel, *first_child;
537
538                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
539                 if (box_children == NULL) {
540                         move_out = TRUE;
541                 } else { 
542                         first_child = box_children;
543                         first_child = find_prev_or_next_not_purged (box_children, FALSE, TRUE);
544                         if (priv->selected != NULL && first_child != NULL) {
545                                 if (priv->selected->data != first_child->data)
546                                         current_sel = (ModestAttachmentView *) priv->selected->data;
547                                 else
548                                         move_out = TRUE;
549                         } else {
550                                 move_out = TRUE;
551                         }
552                 }
553
554                 if (move_out) {
555                         GtkWidget *toplevel = NULL;
556                         /* move cursor outside */
557                         toplevel = gtk_widget_get_toplevel (widget);
558                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
559                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_UP);
560                         unselect_all (atts_view);
561                 } else {
562                         new_sel = g_list_find (box_children, (gpointer) current_sel);
563                         new_sel = find_prev_or_next_not_purged (new_sel, TRUE, FALSE);
564                         /* We assume that we detected properly that
565                            there is a not purge attachment so we don't
566                            need to check NULL */
567                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
568                 }
569                 g_list_free (box_children);
570                 return TRUE;
571         }
572
573         if (event->keyval == GDK_Down) {
574                 ModestAttachmentView *current_sel = NULL;
575                 gboolean move_out = FALSE;
576                 GList * box_children, *new_sel, *last_child = NULL;
577
578                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
579
580                 if (box_children == NULL) {
581                         move_out = TRUE;
582                 } else {
583                         last_child = g_list_last (box_children);
584                         last_child = find_prev_or_next_not_purged (last_child, TRUE, TRUE);
585                         if (priv->selected != NULL && last_child != NULL) {
586                                 GList *last_selected = g_list_last (priv->selected);
587                                 if (last_selected->data != last_child->data)
588                                         current_sel = (ModestAttachmentView *) last_selected->data;
589                                 else
590                                         move_out = TRUE;
591                         } else {
592                                 move_out = TRUE;
593                         }
594                 }
595
596                 if (move_out) {
597                         GtkWidget *toplevel = NULL;
598                         /* move cursor outside */
599                         toplevel = gtk_widget_get_toplevel (widget);
600                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
601                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_DOWN);
602                         unselect_all (atts_view);
603                 } else {
604                         new_sel = g_list_find (box_children, (gpointer) current_sel);
605                         new_sel = find_prev_or_next_not_purged (new_sel, FALSE, FALSE);
606                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
607                 }
608                 g_list_free (box_children);
609                 return TRUE;
610         }
611
612         /* Activates selected item */
613         if (g_list_length (priv->selected) == 1) {
614                 ModestAttachmentView *att_view = (ModestAttachmentView *) priv->selected->data;
615                 if ((event->keyval == GDK_Return)) {
616                         TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
617                         if (TNY_IS_MIME_PART (mime_part)) {
618                                 g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
619                                 g_object_unref (mime_part);
620                         }
621                         return TRUE;
622                 }
623         }
624
625         return FALSE;
626 }
627
628
629 static GtkWidget *
630 get_att_view_at_coords (ModestAttachmentsView *atts_view,
631                         gdouble x, gdouble y)
632 {
633         ModestAttachmentsViewPrivate *priv = NULL;
634         GList *att_view_list, *node;
635         GtkWidget *result = NULL;
636
637         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
638         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
639         
640         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
641                 GtkWidget *att_view = (GtkWidget *) node->data;
642                 gint pos_x, pos_y, w, h, int_x, int_y;
643                 gint widget_x, widget_y;
644
645                 gdk_window_get_origin (att_view->window, &widget_x, &widget_y);
646
647                 pos_x = widget_x;
648                 pos_y = widget_y;
649                 w = att_view->allocation.width;
650                 h = att_view->allocation.height;
651
652                 int_x = (gint) x - GTK_WIDGET (atts_view)->allocation.x;
653                 int_y = (gint) y - GTK_WIDGET (atts_view)->allocation.y;
654
655                 if ((x >= pos_x) && (x <= (pos_x + w)) && (y >= pos_y) && (y <= (pos_y + h))) {
656                         result = att_view;
657                         break;
658                 }
659         }
660
661         g_list_free (att_view_list);
662         return result;
663 }
664
665 static void
666 unselect_all (ModestAttachmentsView *atts_view)
667 {
668         ModestAttachmentsViewPrivate *priv = NULL;
669         GList *att_view_list, *node;
670
671         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
672         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
673         
674         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
675                 GtkWidget *att_view = (GtkWidget *) node->data;
676
677                 if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED)
678                         gtk_widget_set_state (att_view, GTK_STATE_NORMAL);
679         }
680
681         g_list_free (priv->selected);
682         priv->selected = NULL;
683
684         g_list_free (att_view_list);
685 }
686
687 static void 
688 set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view)
689 {
690         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
691         TnyMimePart *part;
692
693         unselect_all (atts_view);
694         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
695         
696         g_list_free (priv->selected);
697         priv->selected = NULL;
698         if (TNY_IS_MIME_PART (part) && !tny_mime_part_is_purged (part)) {
699                 gtk_widget_set_state (GTK_WIDGET (att_view), GTK_STATE_SELECTED);
700                 priv->selected = g_list_append (priv->selected, att_view);
701         }
702         if (part)
703                 g_object_unref (part);
704         
705         own_clipboard (atts_view);
706 }
707
708 static void 
709 select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2)
710 {
711         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
712         GList *children = NULL;
713         GList *node = NULL;
714         gboolean selecting = FALSE;
715         TnyMimePart *part;
716
717         unselect_all (atts_view);
718
719         if (att1 == att2) {
720                 set_selected (atts_view, att1);
721                 return;
722         }
723
724         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
725         g_list_free (priv->selected);
726         priv->selected = NULL;
727
728
729         for (node = children; node != NULL; node = g_list_next (node)) {
730                 if ((node->data == att1) || (node->data == att2)) {
731                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
732                         if (!tny_mime_part_is_purged (part)) {
733                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
734                                 priv->selected = g_list_append (priv->selected, node->data);
735                         }
736                         g_object_unref (part);
737                         selecting = !selecting;
738                 } else if (selecting) {
739                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
740                         if (!tny_mime_part_is_purged (part)) {
741                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
742                                 priv->selected = g_list_append (priv->selected, node->data);
743                         }
744                         g_object_unref (part);
745                 }
746                         
747         }
748         g_list_free (children);
749         
750         own_clipboard (atts_view);
751 }
752
753 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
754                            guint info, gpointer userdata)
755 {
756         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
757         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
758
759         if ((priv->selected != NULL)&&(priv->selected->next == NULL)) {
760                 if (info == MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX) {
761                         /* MODEST_ATTACHMENT requested. As the content id is not filled in all the case, we'll
762                          * use an internal index. This index is simply the index of the attachment in the vbox */
763                         GList *box_children = NULL;
764                         gint index;
765                         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
766                         index = g_list_index (box_children, priv->selected);
767                         if (index >= 0) {
768                                 gchar *index_str = g_strdup_printf("%d", index);
769                                 gtk_selection_data_set_text (selection_data, index_str, -1);
770                                 g_free (index_str);
771                         }
772                 }
773         }
774 }
775
776 static void clipboard_clear (GtkClipboard *clipboard, gpointer userdata)
777 {
778         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
779
780         unselect_all (atts_view);
781 }
782
783 TnyList *
784 modest_attachments_view_get_selection (ModestAttachmentsView *atts_view)
785 {
786         ModestAttachmentsViewPrivate *priv;
787         TnyList *selection;
788         GList *node;
789
790         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
791         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
792
793         selection = tny_simple_list_new ();
794         for (node = priv->selected; node != NULL; node = g_list_next (node)) {
795                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
796                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
797                 tny_list_append (selection, (GObject *) part);
798                 g_object_unref (part);
799         }
800         
801         return selection;
802 }
803
804 TnyList *
805 modest_attachments_view_get_attachments (ModestAttachmentsView *atts_view)
806 {
807         ModestAttachmentsViewPrivate *priv;
808         TnyList *att_list;
809         GList *children, *node= NULL;
810
811         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
812         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
813
814         att_list = TNY_LIST (tny_simple_list_new ());
815
816         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
817         for (node = children; node != NULL; node = g_list_next (node)) {
818                 GtkWidget *att_view = GTK_WIDGET (node->data);
819                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
820                 tny_list_append (att_list, (GObject *) mime_part);
821                 g_object_unref (mime_part);
822         }
823         g_list_free (children);
824         return att_list;
825
826 }
827
828 void
829 modest_attachments_view_select_all (ModestAttachmentsView *atts_view)
830 {
831         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
832         GList *children = NULL;
833         GList *node = NULL;
834
835         unselect_all (atts_view);
836
837         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
838         g_list_free (priv->selected);
839         priv->selected = NULL;
840
841         for (node = children; node != NULL; node = g_list_next (node)) {
842                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
843                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
844
845                 /* Do not select purged attachments */
846                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
847                         gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
848                         priv->selected = g_list_append (priv->selected, node->data);
849                 }
850         }
851         g_list_free (children);
852
853         own_clipboard (atts_view);
854 }
855
856 gboolean
857 modest_attachments_view_has_attachments (ModestAttachmentsView *atts_view)
858 {
859         ModestAttachmentsViewPrivate *priv;
860         GList *children;
861         gboolean result;
862
863         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), FALSE);
864         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
865
866         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
867         result = (children != NULL);
868         g_list_free (children);
869
870         return result;
871 }
872
873 void
874 modest_attachments_view_get_sizes (ModestAttachmentsView *attachments_view,
875                                    gint *attachments_count,
876                                    guint64 *attachments_size)
877 {
878         ModestAttachmentsViewPrivate *priv;
879         GList *children, *node;
880
881         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
882         g_return_if_fail (attachments_count != NULL && attachments_size != NULL);
883
884         *attachments_count = 0;
885         *attachments_size = 0;
886
887         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
888
889         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
890         for (node = children; node != NULL; node = g_list_next (node)) {
891                 GtkWidget *att_view = (GtkWidget *) node->data;
892                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
893
894                 if (!tny_mime_part_is_purged (part)) {
895                         guint64 size;
896                         (*attachments_count) ++;
897                         size = modest_attachment_view_get_size (MODEST_ATTACHMENT_VIEW (att_view));
898                         if (size == 0) {
899                                 /* we do a random estimation of the size of an attachment */
900                                 size = 32768;
901                         }
902                         *attachments_size += size;
903                         
904                 }
905                 g_object_unref (part);
906         }
907         g_list_free (children);
908 }
909
910 static void
911 own_clipboard (ModestAttachmentsView *atts_view)
912 {
913         GtkTargetEntry targets[] = {
914                 {MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE, 0, MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX},
915         };
916
917         gtk_clipboard_set_with_owner (gtk_widget_get_clipboard (GTK_WIDGET (atts_view), GDK_SELECTION_PRIMARY),
918                                       targets, G_N_ELEMENTS (targets),
919                                       clipboard_get, clipboard_clear, G_OBJECT(atts_view));
920                               
921 }
922
923 static gboolean 
924 focus_out_event (GtkWidget *widget, GdkEventFocus *event, ModestAttachmentsView *atts_view)
925 {
926         if (!gtk_widget_is_focus (widget))
927                 unselect_all (atts_view);
928
929         return FALSE;
930 }
931
932 static gboolean 
933 focus (GtkWidget *widget, GtkDirectionType direction, ModestAttachmentsView *atts_view)
934 {
935         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
936         GList *children = NULL;
937         GtkWidget *toplevel = NULL;
938
939         toplevel = gtk_widget_get_toplevel (widget);
940         if (!gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)))
941                 return FALSE;
942
943         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
944         if (children != NULL) {
945                 set_selected (atts_view, MODEST_ATTACHMENT_VIEW (children->data));
946         }
947         g_list_free (children);
948
949         return FALSE;
950 }