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