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