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