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