* Added a first implementation for new messages notifications
[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 <glib/gi18n-lib.h>
33
34 #include <string.h>
35 #include <gtk/gtk.h>
36 #include <gdk/gdkkeysyms.h>
37 #include <tny-list.h>
38 #include <tny-simple-list.h>
39
40 #include <modest-platform.h>
41 #include <modest-runtime.h>
42 #include <modest-attachment-view.h>
43 #include <modest-attachments-view.h>
44
45 static GObjectClass *parent_class = NULL;
46
47 /* signals */
48 enum {
49         ACTIVATE_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 GtkWidget *get_att_view_at_coords (ModestAttachmentsView *atts_view,
71                                           gdouble x, gdouble y);
72 static void unselect_all (ModestAttachmentsView *atts_view);
73 static void set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view);
74 static void select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2);
75 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
76                            guint info, gpointer userdata);
77 static void clipboard_clear (GtkClipboard *clipboard, gpointer userdata);
78 static void own_clipboard (ModestAttachmentsView *atts_view);
79
80 static guint signals[LAST_SIGNAL] = {0};
81
82 /**
83  * modest_attachments_view_new:
84  * @msg: a #TnyMsg
85  *
86  * Constructor for attachments view widget.
87  *
88  * Return value: a new #ModestAttachmentsView instance implemented for Gtk+
89  **/
90 GtkWidget*
91 modest_attachments_view_new (TnyMsg *msg)
92 {
93         ModestAttachmentsView *self = g_object_new (MODEST_TYPE_ATTACHMENTS_VIEW, 
94                                                     "resize-mode", GTK_RESIZE_PARENT,
95                                                     NULL);
96
97         modest_attachments_view_set_message (self, msg);
98
99         return GTK_WIDGET (self);
100 }
101
102 void
103 modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, TnyMsg *msg)
104 {
105         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
106         TnyList *parts;
107         TnyIterator *iter;
108         
109         if (msg == priv->msg) return;
110
111         if (priv->msg) 
112                 g_object_unref (priv->msg);
113         if (msg)
114                 g_object_ref (G_OBJECT(msg));
115         
116         priv->msg = msg;
117
118         g_list_free (priv->selected);
119         priv->selected = NULL;
120
121         gtk_container_foreach (GTK_CONTAINER (priv->box), (GtkCallback) gtk_widget_destroy, NULL);
122         
123         if (priv->msg == NULL) {
124                 return;
125         }
126
127         parts = TNY_LIST (tny_simple_list_new ());
128         tny_mime_part_get_parts (TNY_MIME_PART (priv->msg), parts);
129         iter = tny_list_create_iterator (parts);
130
131         while (!tny_iterator_is_done (iter)) {
132                 TnyMimePart *part;
133
134                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
135                 if (tny_mime_part_is_attachment (part) || TNY_IS_MSG (part)) {
136                         modest_attachments_view_add_attachment (attachments_view, part);
137                 }
138                 g_object_unref (part);
139                 tny_iterator_next (iter);
140         }
141
142         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
143
144 }
145
146 void
147 modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part)
148 {
149         GtkWidget *att_view = NULL;
150         ModestAttachmentsViewPrivate *priv = NULL;
151
152         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
153         g_return_if_fail (TNY_IS_MIME_PART (part));
154
155         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
156
157         att_view = modest_attachment_view_new (part);
158         gtk_box_pack_end (GTK_BOX (priv->box), att_view, FALSE, FALSE, 0);
159         gtk_widget_show_all (att_view);
160 }
161
162 void
163 modest_attachments_view_remove_attachment (ModestAttachmentsView *atts_view, TnyMimePart *mime_part)
164 {
165         ModestAttachmentsViewPrivate *priv = NULL;
166         GList *box_children = NULL, *node = NULL;
167         ModestAttachmentView *found_att_view = NULL;
168
169         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
170         g_return_if_fail (TNY_IS_MIME_PART (mime_part));
171
172         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
173         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
174
175         for (node = box_children; node != NULL; node = g_list_next (node)) {
176                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
177                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
178
179                 if (mime_part == cur_mime_part)
180                         found_att_view = att_view;
181
182                 g_object_unref (cur_mime_part);
183
184                 if (found_att_view != NULL)
185                         break;
186         }
187
188         if (found_att_view) {
189                 gtk_widget_destroy (GTK_WIDGET (found_att_view));
190         }
191
192 }
193
194 void
195 modest_attachments_view_remove_attachment_by_id (ModestAttachmentsView *atts_view, const gchar *att_id)
196 {
197         ModestAttachmentsViewPrivate *priv = NULL;
198         GList *box_children = NULL, *node = NULL;
199
200         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
201         g_return_if_fail (att_id != NULL);
202
203         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
204         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
205
206         for (node = box_children; node != NULL; node = g_list_next (node)) {
207                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
208                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
209                 const gchar *mime_part_id = NULL;
210
211                 mime_part_id = tny_mime_part_get_content_id (cur_mime_part);
212                 if ((mime_part_id != NULL) && (strcmp (mime_part_id, att_id) == 0))
213                         gtk_widget_destroy (GTK_WIDGET (att_view));
214
215                 g_object_unref (cur_mime_part);
216         }
217
218 }
219
220 static void
221 modest_attachments_view_instance_init (GTypeInstance *instance, gpointer g_class)
222 {
223         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (instance);
224
225         priv->msg = NULL;
226         priv->box = gtk_vbox_new (FALSE, 0);
227         priv->rubber_start = NULL;
228         priv->selected = NULL;
229
230         gtk_container_add (GTK_CONTAINER (instance), priv->box);
231         gtk_event_box_set_above_child (GTK_EVENT_BOX (instance), TRUE);
232
233         g_signal_connect (G_OBJECT (instance), "button-press-event", G_CALLBACK (button_press_event), instance);
234         g_signal_connect (G_OBJECT (instance), "button-release-event", G_CALLBACK (button_release_event), instance);
235         g_signal_connect (G_OBJECT (instance), "motion-notify-event", G_CALLBACK (motion_notify_event), instance);
236         g_signal_connect (G_OBJECT (instance), "key-press-event", G_CALLBACK (key_press_event), instance);
237
238         GTK_WIDGET_SET_FLAGS (instance, GTK_CAN_FOCUS);
239
240         return;
241 }
242
243 static void
244 modest_attachments_view_finalize (GObject *object)
245 {
246         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (object);
247
248         if (priv->msg) {
249                 g_object_unref (priv->msg);
250                 priv->msg = NULL;
251         }
252
253         (*parent_class->finalize) (object);
254
255         return;
256 }
257
258 static void 
259 modest_attachments_view_class_init (ModestAttachmentsViewClass *klass)
260 {
261         GObjectClass *object_class;
262         GtkWidgetClass *widget_class;
263
264         parent_class = g_type_class_peek_parent (klass);
265         object_class = (GObjectClass*) klass;
266         widget_class = GTK_WIDGET_CLASS (klass);
267
268         object_class->finalize = modest_attachments_view_finalize;
269
270         klass->activate = NULL;
271
272         g_type_class_add_private (object_class, sizeof (ModestAttachmentsViewPrivate));
273
274         signals[ACTIVATE_SIGNAL] =
275                 g_signal_new ("activate",
276                               G_TYPE_FROM_CLASS (object_class),
277                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
278                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, activate),
279                               NULL, NULL,
280                               g_cclosure_marshal_VOID__OBJECT,
281                               G_TYPE_NONE, 1, G_TYPE_OBJECT);
282         
283         return;
284 }
285
286 GType 
287 modest_attachments_view_get_type (void)
288 {
289         static GType type = 0;
290
291         if (G_UNLIKELY(type == 0))
292         {
293                 static const GTypeInfo info = 
294                 {
295                   sizeof (ModestAttachmentsViewClass),
296                   NULL,   /* base_init */
297                   NULL,   /* base_finalize */
298                   (GClassInitFunc) modest_attachments_view_class_init,   /* class_init */
299                   NULL,   /* class_finalize */
300                   NULL,   /* class_data */
301                   sizeof (ModestAttachmentsView),
302                   0,      /* n_preallocs */
303                   modest_attachments_view_instance_init    /* instance_init */
304                 };
305
306                 type = g_type_register_static (GTK_TYPE_EVENT_BOX,
307                         "ModestAttachmentsView",
308                         &info, 0);
309
310         }
311
312         return type;
313 }
314
315 /* buttons signal events */
316 static gboolean
317 button_press_event (GtkWidget *widget, 
318                     GdkEventButton *event,
319                     ModestAttachmentsView *atts_view)
320 {
321         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
322         if (!GTK_WIDGET_HAS_FOCUS (widget))
323                 gtk_widget_grab_focus (widget);
324
325         if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
326                 GtkWidget *att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
327                                                               event->x, event->y);
328
329                 if (att_view != NULL) {
330                         if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED && (g_list_length (priv->selected) < 2)) {
331                                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
332                                 if (TNY_IS_MIME_PART (mime_part)) {
333                                         g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
334                                         g_object_unref (mime_part);
335                                 }
336                         } else {
337                                 set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
338                                 priv->rubber_start = att_view;
339                                 gtk_grab_add (widget);
340                         }
341                 }
342         }
343         return TRUE;
344
345 }
346
347 static gboolean
348 button_release_event (GtkWidget *widget,
349                       GdkEventButton *event,
350                       ModestAttachmentsView *atts_view)
351 {
352         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
353         if (widget == gtk_grab_get_current ()) {
354                 GtkWidget *att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
355                                                               event->x, event->y);
356
357                 if (att_view != NULL) {
358                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
359                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
360                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
361                                       MODEST_ATTACHMENT_VIEW (att_view));
362                 }
363                 priv->rubber_start = NULL;
364                 
365                 gtk_grab_remove (widget);
366         }
367         return TRUE;
368 }
369
370 static gboolean
371 motion_notify_event (GtkWidget *widget,
372                      GdkEventMotion *event,
373                      ModestAttachmentsView *atts_view)
374 {
375         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
376         if (gtk_grab_get_current () == widget) {
377                 GtkWidget *att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
378                                                               event->x, event->y);
379
380                 if (att_view != NULL) {
381                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
382                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
383                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
384                                       MODEST_ATTACHMENT_VIEW (att_view));
385                 }
386         }
387         return TRUE;
388 }
389
390 static gboolean
391 key_press_event (GtkWidget *widget,
392                  GdkEventKey *event,
393                  ModestAttachmentsView *atts_view)
394 {
395         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
396
397         /* If grabbed (for example rubber banding), escape leaves the rubberbanding mode */
398         if (gtk_grab_get_current () == widget) {
399                 if (event->keyval == GDK_Escape) {
400                         set_selected (MODEST_ATTACHMENTS_VIEW (widget),
401                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start));
402                         priv->rubber_start = NULL;
403                         gtk_grab_remove (widget);
404                         return TRUE;
405                 } 
406                 return FALSE;
407         }
408
409         if (event->keyval == GDK_Up) {
410                 ModestAttachmentView *current_sel = NULL;
411                 gboolean move_out = FALSE;
412                 GList * box_children, *new_sel;
413
414                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
415                 if (box_children == NULL)
416                         move_out = TRUE;
417                 else if ((priv->selected != NULL)&&(priv->selected->data != box_children->data))
418                         current_sel = (ModestAttachmentView *) priv->selected->data;
419                 else
420                         move_out = TRUE;
421
422                 if (move_out) {
423                         GtkWidget *toplevel = NULL;
424                         /* move cursor outside */
425                         toplevel = gtk_widget_get_toplevel (widget);
426                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
427                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_UP);
428                         unselect_all (atts_view);
429                 } else {
430                         new_sel = g_list_find (box_children, (gpointer) current_sel);
431                         new_sel = g_list_previous (new_sel);
432                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
433                 }
434                 g_list_free (box_children);
435                 return TRUE;
436         }
437
438         if (event->keyval == GDK_Down) {
439                 ModestAttachmentView *current_sel = NULL;
440                 gboolean move_out = FALSE;
441                 GList * box_children, *new_sel, *last_child = NULL;
442
443                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
444
445                 if (box_children == NULL) {
446                         move_out = TRUE;
447                 } else {
448                         last_child = g_list_last (box_children);
449                         if (priv->selected != NULL) {
450                                 GList *last_selected = g_list_last (priv->selected);
451                                 if (last_selected->data != last_child->data)
452                                         current_sel = (ModestAttachmentView *) last_selected->data;
453                                 else
454                                         move_out = TRUE;
455                         } else {
456                                 move_out = TRUE;
457                         }
458                 }
459
460                 if (move_out) {
461                         GtkWidget *toplevel = NULL;
462                         /* move cursor outside */
463                         toplevel = gtk_widget_get_toplevel (widget);
464                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
465                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_DOWN);
466                         unselect_all (atts_view);
467                 } else {
468                         new_sel = g_list_find (box_children, (gpointer) current_sel);
469                         new_sel = g_list_next (new_sel);
470                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
471                 }
472                 g_list_free (box_children);
473                 return TRUE;
474         }
475
476         /* Activates selected item */
477         if (g_list_length (priv->selected) == 1) {
478                 ModestAttachmentView *att_view = (ModestAttachmentView *) priv->selected->data;
479                 if ((event->keyval == GDK_Return)) {
480                         TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
481                         if (TNY_IS_MIME_PART (mime_part)) {
482                                 g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
483                                 g_object_unref (mime_part);
484                         }
485                         return TRUE;
486                 }
487         }
488
489         return FALSE;
490 }
491
492
493 static GtkWidget *
494 get_att_view_at_coords (ModestAttachmentsView *atts_view,
495                         gdouble x, gdouble y)
496 {
497         ModestAttachmentsViewPrivate *priv = NULL;
498         GList *att_view_list, *node;
499         GtkWidget *result = NULL;
500
501         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
502         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
503         
504         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
505                 GtkWidget *att_view = (GtkWidget *) node->data;
506                 gint pos_x, pos_y, w, h, int_x, int_y;
507
508                 pos_x = att_view->allocation.x;
509                 pos_y = att_view->allocation.y;
510                 w = att_view->allocation.width;
511                 h = att_view->allocation.height;
512
513                 int_x = (gint) x;
514                 int_y = (gint) y;
515
516                 if ((x >= pos_x) && (x <= (pos_x + w)) && (y >= pos_y) && (y <= (pos_y + h))) {
517                         result = att_view;
518                         break;
519                 }
520         }
521
522         g_list_free (att_view_list);
523         return result;
524 }
525
526 static void
527 unselect_all (ModestAttachmentsView *atts_view)
528 {
529         ModestAttachmentsViewPrivate *priv = NULL;
530         GList *att_view_list, *node;
531
532         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
533         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
534         
535         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
536                 GtkWidget *att_view = (GtkWidget *) node->data;
537
538                 if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED)
539                         gtk_widget_set_state (att_view, GTK_STATE_NORMAL);
540         }
541
542         g_list_free (priv->selected);
543         priv->selected = NULL;
544
545         g_list_free (att_view_list);
546 }
547
548 static void 
549 set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view)
550 {
551         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
552
553         unselect_all (atts_view);
554         gtk_widget_set_state (GTK_WIDGET (att_view), GTK_STATE_SELECTED);
555         g_list_free (priv->selected);
556         priv->selected = NULL;
557         priv->selected = g_list_append (priv->selected, att_view);
558         
559         own_clipboard (atts_view);
560 }
561
562 static void 
563 select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2)
564 {
565         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
566         GList *children = NULL;
567         GList *node = NULL;
568         gboolean selecting = FALSE;
569
570         unselect_all (atts_view);
571
572         if (att1 == att2) {
573                 set_selected (atts_view, att1);
574                 return;
575         }
576
577         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
578         g_list_free (priv->selected);
579         priv->selected = NULL;
580
581
582         for (node = children; node != NULL; node = g_list_next (node)) {
583                 if ((node->data == att1) || (node->data == att2)) {
584                         gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
585                         priv->selected = g_list_append (priv->selected, node->data);
586                         selecting = !selecting;
587                 } else if (selecting) {
588                         gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
589                         priv->selected = g_list_append (priv->selected, node->data);
590                 }
591                         
592         }
593         g_list_free (children);
594         
595         own_clipboard (atts_view);
596 }
597
598 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
599                            guint info, gpointer userdata)
600 {
601         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
602         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
603
604         if ((priv->selected != NULL)&&(priv->selected->next == NULL)) {
605                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (priv->selected->data));
606                 if (info != MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX) {
607                         if (TNY_IS_MSG (mime_part)) {
608                                 TnyHeader *header = tny_msg_get_header (TNY_MSG (mime_part));
609                                 if (TNY_IS_HEADER (header)) {
610                                         gtk_selection_data_set_text (selection_data, tny_header_get_subject (header), -1);
611                                         g_object_unref (header);
612                                 }
613                         } else {
614                                 gtk_selection_data_set_text (selection_data, tny_mime_part_get_filename (mime_part), -1);
615                         }
616                 } else {
617                         /* MODEST_ATTACHMENT requested. As the content id is not filled in all the case, we'll
618                          * use an internal index. This index is simply the index of the attachment in the vbox */
619                         GList *box_children = NULL;
620                         gint index;
621                         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
622                         index = g_list_index (box_children, priv->selected);
623                         if (index >= 0) {
624                                 gchar *index_str = g_strdup_printf("%d", index);
625                                 gtk_selection_data_set_text (selection_data, index_str, -1);
626                                 g_free (index_str);
627                         }
628                 }
629         }
630 }
631
632 static void clipboard_clear (GtkClipboard *clipboard, gpointer userdata)
633 {
634         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
635
636         unselect_all (atts_view);
637 }
638
639 GList *
640 modest_attachments_view_get_selection (ModestAttachmentsView *atts_view)
641 {
642         ModestAttachmentsViewPrivate *priv;
643         GList *selection, *node;
644
645         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
646         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
647
648         selection = NULL;
649         for (node = priv->selected; node != NULL; node = g_list_next (node)) {
650                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
651                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
652                 selection = g_list_append (selection, part);
653         }
654         
655         return selection;
656 }
657
658 GList *
659 modest_attachments_view_get_attachments (ModestAttachmentsView *atts_view)
660 {
661         ModestAttachmentsViewPrivate *priv;
662         GList *children, *node, *att_list = NULL;
663
664         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
665         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
666
667         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
668         for (node = children; node != NULL; node = g_list_next (node)) {
669                 GtkWidget *att_view = GTK_WIDGET (node->data);
670                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
671                 att_list = g_list_prepend (att_list, mime_part);
672         }
673         g_list_free (children);
674         att_list = g_list_reverse (att_list);
675         return att_list;
676
677 }
678
679 void
680 modest_attachments_view_select_all (ModestAttachmentsView *atts_view)
681 {
682         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
683         GList *children = NULL;
684         GList *node = NULL;
685
686         unselect_all (atts_view);
687
688         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
689         g_list_free (priv->selected);
690         priv->selected = NULL;
691
692
693         for (node = children; node != NULL; node = g_list_next (node)) {
694                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
695                 priv->selected = g_list_append (priv->selected, node->data);
696         }
697         g_list_free (children);
698
699         own_clipboard (atts_view);
700 }
701
702 static void
703 own_clipboard (ModestAttachmentsView *atts_view)
704 {
705         GtkTargetEntry targets[] = {
706                 {"TEXT", 0, 0},
707                 {"UTF8_STRING", 0, 1},
708                 {"COMPOUND_TEXT", 0, 2},
709                 {"STRING", 0, 3},
710                 {MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE, 0, MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX},
711         };
712
713         gtk_clipboard_set_with_owner (gtk_widget_get_clipboard (GTK_WIDGET (atts_view), GDK_SELECTION_PRIMARY),
714                                       targets, G_N_ELEMENTS (targets),
715                                       clipboard_get, clipboard_clear, G_OBJECT(atts_view));
716                               
717 }