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