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