* src/maemo/modest-msg-view-window.c:
[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                 priv->selected = g_list_remove (priv->selected, found_att_view);
190                 gtk_widget_destroy (GTK_WIDGET (found_att_view));
191                 own_clipboard (atts_view);
192         }
193
194 }
195
196 void
197 modest_attachments_view_remove_attachment_by_id (ModestAttachmentsView *atts_view, const gchar *att_id)
198 {
199         ModestAttachmentsViewPrivate *priv = NULL;
200         GList *box_children = NULL, *node = NULL;
201
202         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
203         g_return_if_fail (att_id != NULL);
204
205         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
206         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
207
208         for (node = box_children; node != NULL; node = g_list_next (node)) {
209                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
210                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
211                 const gchar *mime_part_id = NULL;
212
213                 mime_part_id = tny_mime_part_get_content_id (cur_mime_part);
214                 if ((mime_part_id != NULL) && (strcmp (mime_part_id, att_id) == 0)) {
215                         gtk_widget_destroy (GTK_WIDGET (att_view));
216                         priv->selected = g_list_remove (priv->selected, att_view);
217                 }
218
219                 g_object_unref (cur_mime_part);
220         }
221
222         own_clipboard (atts_view);
223
224 }
225
226 static void
227 modest_attachments_view_instance_init (GTypeInstance *instance, gpointer g_class)
228 {
229         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (instance);
230
231         priv->msg = NULL;
232         priv->box = gtk_vbox_new (FALSE, 0);
233         priv->rubber_start = NULL;
234         priv->selected = NULL;
235
236         gtk_container_add (GTK_CONTAINER (instance), priv->box);
237         gtk_event_box_set_above_child (GTK_EVENT_BOX (instance), TRUE);
238
239         g_signal_connect (G_OBJECT (instance), "button-press-event", G_CALLBACK (button_press_event), instance);
240         g_signal_connect (G_OBJECT (instance), "button-release-event", G_CALLBACK (button_release_event), instance);
241         g_signal_connect (G_OBJECT (instance), "motion-notify-event", G_CALLBACK (motion_notify_event), instance);
242         g_signal_connect (G_OBJECT (instance), "key-press-event", G_CALLBACK (key_press_event), instance);
243
244         GTK_WIDGET_SET_FLAGS (instance, GTK_CAN_FOCUS);
245
246         return;
247 }
248
249 static void
250 modest_attachments_view_finalize (GObject *object)
251 {
252         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (object);
253
254         if (priv->msg) {
255                 g_object_unref (priv->msg);
256                 priv->msg = NULL;
257         }
258
259         (*parent_class->finalize) (object);
260
261         return;
262 }
263
264 static void 
265 modest_attachments_view_class_init (ModestAttachmentsViewClass *klass)
266 {
267         GObjectClass *object_class;
268         GtkWidgetClass *widget_class;
269
270         parent_class = g_type_class_peek_parent (klass);
271         object_class = (GObjectClass*) klass;
272         widget_class = GTK_WIDGET_CLASS (klass);
273
274         object_class->finalize = modest_attachments_view_finalize;
275
276         klass->activate = NULL;
277
278         g_type_class_add_private (object_class, sizeof (ModestAttachmentsViewPrivate));
279
280         signals[ACTIVATE_SIGNAL] =
281                 g_signal_new ("activate",
282                               G_TYPE_FROM_CLASS (object_class),
283                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
284                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, activate),
285                               NULL, NULL,
286                               g_cclosure_marshal_VOID__OBJECT,
287                               G_TYPE_NONE, 1, G_TYPE_OBJECT);
288         
289         return;
290 }
291
292 GType 
293 modest_attachments_view_get_type (void)
294 {
295         static GType type = 0;
296
297         if (G_UNLIKELY(type == 0))
298         {
299                 static const GTypeInfo info = 
300                 {
301                   sizeof (ModestAttachmentsViewClass),
302                   NULL,   /* base_init */
303                   NULL,   /* base_finalize */
304                   (GClassInitFunc) modest_attachments_view_class_init,   /* class_init */
305                   NULL,   /* class_finalize */
306                   NULL,   /* class_data */
307                   sizeof (ModestAttachmentsView),
308                   0,      /* n_preallocs */
309                   modest_attachments_view_instance_init    /* instance_init */
310                 };
311
312                 type = g_type_register_static (GTK_TYPE_EVENT_BOX,
313                         "ModestAttachmentsView",
314                         &info, 0);
315
316         }
317
318         return type;
319 }
320
321 /* buttons signal events */
322 static gboolean
323 button_press_event (GtkWidget *widget, 
324                     GdkEventButton *event,
325                     ModestAttachmentsView *atts_view)
326 {
327         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
328         if (!GTK_WIDGET_HAS_FOCUS (widget))
329                 gtk_widget_grab_focus (widget);
330
331         if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
332                 GtkWidget *att_view = NULL;
333
334                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
335                                                    (gint) event->x_root, (gint) event->y_root);
336
337                 if (att_view != NULL) {
338                         if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED && (g_list_length (priv->selected) < 2)) {
339                                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
340                                 if (TNY_IS_MIME_PART (mime_part)) {
341                                         g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
342                                         g_object_unref (mime_part);
343                                 }
344                         } else {
345                                 set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
346                                 priv->rubber_start = att_view;
347                                 gtk_grab_add (widget);
348                         }
349                 }
350         }
351         return TRUE;
352
353 }
354
355 static gboolean
356 button_release_event (GtkWidget *widget,
357                       GdkEventButton *event,
358                       ModestAttachmentsView *atts_view)
359 {
360         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
361         if (widget == gtk_grab_get_current ()) {
362                 GtkWidget *att_view = NULL;
363
364                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
365                                                    (gint) event->x_root, (gint) event->y_root);
366
367                 if (att_view != NULL) {
368                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
369                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
370                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
371                                       MODEST_ATTACHMENT_VIEW (att_view));
372                 }
373                 priv->rubber_start = NULL;
374                 gtk_grab_remove (widget);
375         }
376         return TRUE;
377 }
378
379 static gboolean
380 motion_notify_event (GtkWidget *widget,
381                      GdkEventMotion *event,
382                      ModestAttachmentsView *atts_view)
383 {
384         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
385         if (gtk_grab_get_current () == widget) {
386                 GtkWidget *att_view = NULL;
387
388                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
389                                                    (gint) event->x_root, (gint) event->y_root);
390
391                 if (att_view != NULL) {
392                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
393                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
394                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
395                                       MODEST_ATTACHMENT_VIEW (att_view));
396                 }
397         }
398         return TRUE;
399 }
400
401 static gboolean
402 key_press_event (GtkWidget *widget,
403                  GdkEventKey *event,
404                  ModestAttachmentsView *atts_view)
405 {
406         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
407
408         /* If grabbed (for example rubber banding), escape leaves the rubberbanding mode */
409         if (gtk_grab_get_current () == widget) {
410                 if (event->keyval == GDK_Escape) {
411                         set_selected (MODEST_ATTACHMENTS_VIEW (widget),
412                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start));
413                         priv->rubber_start = NULL;
414                         gtk_grab_remove (widget);
415                         return TRUE;
416                 } 
417                 return FALSE;
418         }
419
420         if (event->keyval == GDK_Up) {
421                 ModestAttachmentView *current_sel = NULL;
422                 gboolean move_out = FALSE;
423                 GList * box_children, *new_sel;
424
425                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
426                 if (box_children == NULL)
427                         move_out = TRUE;
428                 else if ((priv->selected != NULL)&&(priv->selected->data != box_children->data))
429                         current_sel = (ModestAttachmentView *) priv->selected->data;
430                 else
431                         move_out = TRUE;
432
433                 if (move_out) {
434                         GtkWidget *toplevel = NULL;
435                         /* move cursor outside */
436                         toplevel = gtk_widget_get_toplevel (widget);
437                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
438                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_UP);
439                         unselect_all (atts_view);
440                 } else {
441                         new_sel = g_list_find (box_children, (gpointer) current_sel);
442                         new_sel = g_list_previous (new_sel);
443                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
444                 }
445                 g_list_free (box_children);
446                 return TRUE;
447         }
448
449         if (event->keyval == GDK_Down) {
450                 ModestAttachmentView *current_sel = NULL;
451                 gboolean move_out = FALSE;
452                 GList * box_children, *new_sel, *last_child = NULL;
453
454                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
455
456                 if (box_children == NULL) {
457                         move_out = TRUE;
458                 } else {
459                         last_child = g_list_last (box_children);
460                         if (priv->selected != NULL) {
461                                 GList *last_selected = g_list_last (priv->selected);
462                                 if (last_selected->data != last_child->data)
463                                         current_sel = (ModestAttachmentView *) last_selected->data;
464                                 else
465                                         move_out = TRUE;
466                         } else {
467                                 move_out = TRUE;
468                         }
469                 }
470
471                 if (move_out) {
472                         GtkWidget *toplevel = NULL;
473                         /* move cursor outside */
474                         toplevel = gtk_widget_get_toplevel (widget);
475                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
476                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_DOWN);
477                         unselect_all (atts_view);
478                 } else {
479                         new_sel = g_list_find (box_children, (gpointer) current_sel);
480                         new_sel = g_list_next (new_sel);
481                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
482                 }
483                 g_list_free (box_children);
484                 return TRUE;
485         }
486
487         /* Activates selected item */
488         if (g_list_length (priv->selected) == 1) {
489                 ModestAttachmentView *att_view = (ModestAttachmentView *) priv->selected->data;
490                 if ((event->keyval == GDK_Return)) {
491                         TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
492                         if (TNY_IS_MIME_PART (mime_part)) {
493                                 g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
494                                 g_object_unref (mime_part);
495                         }
496                         return TRUE;
497                 }
498         }
499
500         return FALSE;
501 }
502
503
504 static GtkWidget *
505 get_att_view_at_coords (ModestAttachmentsView *atts_view,
506                         gdouble x, gdouble y)
507 {
508         ModestAttachmentsViewPrivate *priv = NULL;
509         GList *att_view_list, *node;
510         GtkWidget *result = NULL;
511
512         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
513         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
514         
515         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
516                 GtkWidget *att_view = (GtkWidget *) node->data;
517                 gint pos_x, pos_y, w, h, int_x, int_y;
518                 gint widget_x, widget_y;
519
520                 gdk_window_get_origin (att_view->window, &widget_x, &widget_y);
521
522                 pos_x = widget_x;
523                 pos_y = widget_y;
524                 w = att_view->allocation.width;
525                 h = att_view->allocation.height;
526
527                 int_x = (gint) x - GTK_WIDGET (atts_view)->allocation.x;
528                 int_y = (gint) y - GTK_WIDGET (atts_view)->allocation.y;
529
530                 if ((x >= pos_x) && (x <= (pos_x + w)) && (y >= pos_y) && (y <= (pos_y + h))) {
531                         result = att_view;
532                         break;
533                 }
534         }
535
536         g_list_free (att_view_list);
537         return result;
538 }
539
540 static void
541 unselect_all (ModestAttachmentsView *atts_view)
542 {
543         ModestAttachmentsViewPrivate *priv = NULL;
544         GList *att_view_list, *node;
545
546         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
547         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
548         
549         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
550                 GtkWidget *att_view = (GtkWidget *) node->data;
551
552                 if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED)
553                         gtk_widget_set_state (att_view, GTK_STATE_NORMAL);
554         }
555
556         g_list_free (priv->selected);
557         priv->selected = NULL;
558
559         g_list_free (att_view_list);
560 }
561
562 static void 
563 set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view)
564 {
565         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
566
567         unselect_all (atts_view);
568         gtk_widget_set_state (GTK_WIDGET (att_view), GTK_STATE_SELECTED);
569         g_list_free (priv->selected);
570         priv->selected = NULL;
571         priv->selected = g_list_append (priv->selected, att_view);
572         
573         own_clipboard (atts_view);
574 }
575
576 static void 
577 select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2)
578 {
579         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
580         GList *children = NULL;
581         GList *node = NULL;
582         gboolean selecting = FALSE;
583
584         unselect_all (atts_view);
585
586         if (att1 == att2) {
587                 set_selected (atts_view, att1);
588                 return;
589         }
590
591         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
592         g_list_free (priv->selected);
593         priv->selected = NULL;
594
595
596         for (node = children; node != NULL; node = g_list_next (node)) {
597                 if ((node->data == att1) || (node->data == att2)) {
598                         gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
599                         priv->selected = g_list_append (priv->selected, node->data);
600                         selecting = !selecting;
601                 } else if (selecting) {
602                         gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
603                         priv->selected = g_list_append (priv->selected, node->data);
604                 }
605                         
606         }
607         g_list_free (children);
608         
609         own_clipboard (atts_view);
610 }
611
612 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
613                            guint info, gpointer userdata)
614 {
615         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
616         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
617
618         if ((priv->selected != NULL)&&(priv->selected->next == NULL)) {
619                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (priv->selected->data));
620                 if (info != MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX) {
621                         if (TNY_IS_MSG (mime_part)) {
622                                 TnyHeader *header = tny_msg_get_header (TNY_MSG (mime_part));
623                                 if (TNY_IS_HEADER (header)) {
624                                         const gchar *subject = NULL;
625                                         subject = tny_header_get_subject (header);
626                                         if ((subject == NULL) || (subject[0] == '\0'))
627                                                 subject = _("mail_va_no_subject");
628                                         gtk_selection_data_set_text (selection_data, subject, -1);
629                                         g_object_unref (header);
630                                 }
631                         } else {
632                                 gtk_selection_data_set_text (selection_data, tny_mime_part_get_filename (mime_part), -1);
633                         }
634                 } else {
635                         /* MODEST_ATTACHMENT requested. As the content id is not filled in all the case, we'll
636                          * use an internal index. This index is simply the index of the attachment in the vbox */
637                         GList *box_children = NULL;
638                         gint index;
639                         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
640                         index = g_list_index (box_children, priv->selected);
641                         if (index >= 0) {
642                                 gchar *index_str = g_strdup_printf("%d", index);
643                                 gtk_selection_data_set_text (selection_data, index_str, -1);
644                                 g_free (index_str);
645                         }
646                 }
647         }
648 }
649
650 static void clipboard_clear (GtkClipboard *clipboard, gpointer userdata)
651 {
652         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
653
654         unselect_all (atts_view);
655 }
656
657 GList *
658 modest_attachments_view_get_selection (ModestAttachmentsView *atts_view)
659 {
660         ModestAttachmentsViewPrivate *priv;
661         GList *selection, *node;
662
663         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
664         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
665
666         selection = NULL;
667         for (node = priv->selected; node != NULL; node = g_list_next (node)) {
668                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
669                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
670                 selection = g_list_append (selection, part);
671         }
672         
673         return selection;
674 }
675
676 GList *
677 modest_attachments_view_get_attachments (ModestAttachmentsView *atts_view)
678 {
679         ModestAttachmentsViewPrivate *priv;
680         GList *children, *node, *att_list = NULL;
681
682         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
683         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
684
685         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
686         for (node = children; node != NULL; node = g_list_next (node)) {
687                 GtkWidget *att_view = GTK_WIDGET (node->data);
688                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
689                 att_list = g_list_prepend (att_list, mime_part);
690         }
691         g_list_free (children);
692         att_list = g_list_reverse (att_list);
693         return att_list;
694
695 }
696
697 void
698 modest_attachments_view_select_all (ModestAttachmentsView *atts_view)
699 {
700         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
701         GList *children = NULL;
702         GList *node = NULL;
703
704         unselect_all (atts_view);
705
706         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
707         g_list_free (priv->selected);
708         priv->selected = NULL;
709
710
711         for (node = children; node != NULL; node = g_list_next (node)) {
712                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
713                 priv->selected = g_list_append (priv->selected, node->data);
714         }
715         g_list_free (children);
716
717         own_clipboard (atts_view);
718 }
719
720 gboolean
721 modest_attachments_view_has_attachments (ModestAttachmentsView *atts_view)
722 {
723         ModestAttachmentsViewPrivate *priv;
724         GList *children;
725         gboolean result;
726
727         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), FALSE);
728         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
729
730         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
731         result = (children != NULL);
732         g_list_free (children);
733
734         return result;
735 }
736
737 static void
738 own_clipboard (ModestAttachmentsView *atts_view)
739 {
740         GtkTargetEntry targets[] = {
741                 {"TEXT", 0, 0},
742                 {"UTF8_STRING", 0, 1},
743                 {"COMPOUND_TEXT", 0, 2},
744                 {"STRING", 0, 3},
745                 {MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE, 0, MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX},
746         };
747
748         gtk_clipboard_set_with_owner (gtk_widget_get_clipboard (GTK_WIDGET (atts_view), GDK_SELECTION_PRIMARY),
749                                       targets, G_N_ELEMENTS (targets),
750                                       clipboard_get, clipboard_clear, G_OBJECT(atts_view));
751                               
752 }