1fcf4c33f684335193c6fb65acd579869115815b
[modest] / src / widgets / modest-attachments-view.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <config.h>
31
32 #include <string.h>
33 #include <gtk/gtk.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <tny-list.h>
36 #include <tny-simple-list.h>
37
38 #include <modest-platform.h>
39 #include <modest-runtime.h>
40 #include <modest-attachment-view.h>
41 #include <modest-attachments-view.h>
42 #include <modest-tny-mime-part.h>
43
44 static GObjectClass *parent_class = NULL;
45
46 /* signals */
47 enum {
48         ACTIVATE_SIGNAL,
49         LAST_SIGNAL
50 };
51
52 typedef struct _ModestAttachmentsViewPrivate ModestAttachmentsViewPrivate;
53
54 struct _ModestAttachmentsViewPrivate
55 {
56         TnyMsg *msg;
57         GtkWidget *box;
58         GList *selected;
59         GtkWidget *rubber_start;
60 };
61
62 #define MODEST_ATTACHMENTS_VIEW_GET_PRIVATE(o)  \
63         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ATTACHMENTS_VIEW, ModestAttachmentsViewPrivate))
64
65 static gboolean button_press_event (GtkWidget *widget, GdkEventButton *event, ModestAttachmentsView *atts_view);
66 static gboolean motion_notify_event (GtkWidget *widget, GdkEventMotion *event, ModestAttachmentsView *atts_view);
67 static gboolean button_release_event (GtkWidget *widget, GdkEventButton *event, ModestAttachmentsView *atts_view);
68 static gboolean key_press_event (GtkWidget *widget, GdkEventKey *event, ModestAttachmentsView *atts_view);
69 static gboolean focus_out_event (GtkWidget *widget, GdkEventFocus *event, ModestAttachmentsView *atts_view);
70 static gboolean focus (GtkWidget *widget, GtkDirectionType direction, ModestAttachmentsView *atts_view);
71 static GtkWidget *get_att_view_at_coords (ModestAttachmentsView *atts_view,
72                                           gdouble x, gdouble y);
73 static void unselect_all (ModestAttachmentsView *atts_view);
74 static void set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view);
75 static void select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2);
76 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
77                            guint info, gpointer userdata);
78 static void clipboard_clear (GtkClipboard *clipboard, gpointer userdata);
79 static void own_clipboard (ModestAttachmentsView *atts_view);
80
81 static guint signals[LAST_SIGNAL] = {0};
82
83 /**
84  * modest_attachments_view_new:
85  * @msg: a #TnyMsg
86  *
87  * Constructor for attachments view widget.
88  *
89  * Return value: a new #ModestAttachmentsView instance implemented for Gtk+
90  **/
91 GtkWidget*
92 modest_attachments_view_new (TnyMsg *msg)
93 {
94         ModestAttachmentsView *self = g_object_new (MODEST_TYPE_ATTACHMENTS_VIEW, 
95                                                     "resize-mode", GTK_RESIZE_PARENT,
96                                                     NULL);
97
98         modest_attachments_view_set_message (self, msg);
99
100         return GTK_WIDGET (self);
101 }
102
103 void
104 modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, TnyMsg *msg)
105 {
106         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
107         TnyList *parts;
108         TnyIterator *iter;
109         const gchar *msg_content_type = NULL;
110         
111         if (msg == priv->msg) return;
112
113         if (priv->msg)
114                 g_object_unref (priv->msg);
115         if (msg)
116                 g_object_ref (G_OBJECT(msg));
117         
118         priv->msg = msg;
119
120         g_list_free (priv->selected);
121         priv->selected = NULL;
122
123         gtk_container_foreach (GTK_CONTAINER (priv->box), (GtkCallback) gtk_widget_destroy, NULL);
124         
125         if (priv->msg == NULL) {
126                 return;
127         }
128
129         /* If the top mime part is a multipart/related, we don't show the attachments, as they're
130          * embedded images in body */
131         msg_content_type = tny_mime_part_get_content_type (TNY_MIME_PART (priv->msg));
132         if ((msg_content_type != NULL) && !strcasecmp (msg_content_type, "multipart/related")) {
133                 gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
134                 return;
135         }
136
137         parts = TNY_LIST (tny_simple_list_new ());
138         tny_mime_part_get_parts (TNY_MIME_PART (priv->msg), parts);
139         iter = tny_list_create_iterator (parts);
140
141         while (!tny_iterator_is_done (iter)) {
142                 TnyMimePart *part;
143
144                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
145
146                 if (part && (modest_tny_mime_part_is_attachment_for_modest (part))) 
147                         modest_attachments_view_add_attachment (attachments_view, part);
148
149                 if (part)
150                         g_object_unref (part);
151
152                 tny_iterator_next (iter);
153         }
154
155         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
156
157 }
158
159 void
160 modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part)
161 {
162         GtkWidget *att_view = NULL;
163         ModestAttachmentsViewPrivate *priv = NULL;
164
165         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
166         g_return_if_fail (TNY_IS_MIME_PART (part));
167
168         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
169
170         att_view = modest_attachment_view_new (part);
171         gtk_box_pack_end (GTK_BOX (priv->box), att_view, FALSE, FALSE, 0);
172         gtk_widget_show_all (att_view);
173 }
174
175 void
176 modest_attachments_view_remove_attachment (ModestAttachmentsView *atts_view, TnyMimePart *mime_part)
177 {
178         ModestAttachmentsViewPrivate *priv = NULL;
179         GList *box_children = NULL, *node = NULL;
180         ModestAttachmentView *found_att_view = NULL;
181
182         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
183         g_return_if_fail (TNY_IS_MIME_PART (mime_part));
184
185         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
186         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
187
188         for (node = box_children; node != NULL; node = g_list_next (node)) {
189                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
190                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
191
192                 if (mime_part == cur_mime_part)
193                         found_att_view = att_view;
194
195                 g_object_unref (cur_mime_part);
196
197                 if (found_att_view != NULL)
198                         break;
199         }
200
201         if (found_att_view) {
202                 GList *node = NULL;
203                 GtkWidget *next_widget = NULL;
204                 GList *box_children = NULL;
205
206                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
207                 node = g_list_find (box_children, found_att_view);
208                 if (node->next)
209                         next_widget = node->next->data;
210
211                 g_list_free (box_children);
212
213                 node = g_list_find (priv->selected, found_att_view);
214                 if (node != NULL) {
215                         priv->selected = g_list_delete_link (priv->selected, node);
216                         gtk_widget_destroy (GTK_WIDGET (found_att_view));
217                         if ((priv->selected == NULL) && (next_widget != NULL))
218                                 set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), 
219                                               MODEST_ATTACHMENT_VIEW (next_widget));
220                 }
221                 own_clipboard (atts_view);
222         }
223
224 }
225
226 void
227 modest_attachments_view_remove_attachment_by_id (ModestAttachmentsView *atts_view, const gchar *att_id)
228 {
229         ModestAttachmentsViewPrivate *priv = NULL;
230         GList *box_children = NULL, *node = NULL;
231
232         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
233         g_return_if_fail (att_id != NULL);
234
235         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
236         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
237
238         for (node = box_children; node != NULL; node = g_list_next (node)) {
239                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
240                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
241                 const gchar *mime_part_id = NULL;
242
243                 mime_part_id = tny_mime_part_get_content_id (cur_mime_part);
244                 if ((mime_part_id != NULL) && (strcmp (mime_part_id, att_id) == 0)) {
245                         gtk_widget_destroy (GTK_WIDGET (att_view));
246                         priv->selected = g_list_remove (priv->selected, att_view);
247                 }
248
249                 g_object_unref (cur_mime_part);
250         }
251
252         own_clipboard (atts_view);
253
254 }
255
256 static void
257 modest_attachments_view_instance_init (GTypeInstance *instance, gpointer g_class)
258 {
259         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (instance);
260
261         priv->msg = NULL;
262         priv->box = gtk_vbox_new (FALSE, 0);
263         priv->rubber_start = NULL;
264         priv->selected = NULL;
265
266         gtk_container_add (GTK_CONTAINER (instance), priv->box);
267         gtk_event_box_set_above_child (GTK_EVENT_BOX (instance), TRUE);
268
269         g_signal_connect (G_OBJECT (instance), "button-press-event", G_CALLBACK (button_press_event), instance);
270         g_signal_connect (G_OBJECT (instance), "button-release-event", G_CALLBACK (button_release_event), instance);
271         g_signal_connect (G_OBJECT (instance), "motion-notify-event", G_CALLBACK (motion_notify_event), instance);
272         g_signal_connect (G_OBJECT (instance), "key-press-event", G_CALLBACK (key_press_event), instance);
273         g_signal_connect (G_OBJECT (instance), "focus-out-event", G_CALLBACK (focus_out_event), instance);
274         g_signal_connect (G_OBJECT (instance), "focus", G_CALLBACK (focus), instance);
275
276         GTK_WIDGET_SET_FLAGS (instance, GTK_CAN_FOCUS);
277
278         return;
279 }
280
281 static void
282 modest_attachments_view_finalize (GObject *object)
283 {
284         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (object);
285
286         if (priv->msg) {
287                 g_object_unref (priv->msg);
288                 priv->msg = NULL;
289         }
290
291         (*parent_class->finalize) (object);
292
293         return;
294 }
295
296 static void 
297 modest_attachments_view_class_init (ModestAttachmentsViewClass *klass)
298 {
299         GObjectClass *object_class;
300         GtkWidgetClass *widget_class;
301
302         parent_class = g_type_class_peek_parent (klass);
303         object_class = (GObjectClass*) klass;
304         widget_class = GTK_WIDGET_CLASS (klass);
305
306         object_class->finalize = modest_attachments_view_finalize;
307
308         klass->activate = NULL;
309
310         g_type_class_add_private (object_class, sizeof (ModestAttachmentsViewPrivate));
311
312         signals[ACTIVATE_SIGNAL] =
313                 g_signal_new ("activate",
314                               G_TYPE_FROM_CLASS (object_class),
315                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
316                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, activate),
317                               NULL, NULL,
318                               g_cclosure_marshal_VOID__OBJECT,
319                               G_TYPE_NONE, 1, G_TYPE_OBJECT);
320         
321         return;
322 }
323
324 GType 
325 modest_attachments_view_get_type (void)
326 {
327         static GType type = 0;
328
329         if (G_UNLIKELY(type == 0))
330         {
331                 static const GTypeInfo info = 
332                 {
333                   sizeof (ModestAttachmentsViewClass),
334                   NULL,   /* base_init */
335                   NULL,   /* base_finalize */
336                   (GClassInitFunc) modest_attachments_view_class_init,   /* class_init */
337                   NULL,   /* class_finalize */
338                   NULL,   /* class_data */
339                   sizeof (ModestAttachmentsView),
340                   0,      /* n_preallocs */
341                   modest_attachments_view_instance_init    /* instance_init */
342                 };
343
344                 type = g_type_register_static (GTK_TYPE_EVENT_BOX,
345                         "ModestAttachmentsView",
346                         &info, 0);
347
348         }
349
350         return type;
351 }
352
353 /* buttons signal events */
354 static gboolean
355 button_press_event (GtkWidget *widget, 
356                     GdkEventButton *event,
357                     ModestAttachmentsView *atts_view)
358 {
359         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
360         if (!GTK_WIDGET_HAS_FOCUS (widget))
361                 gtk_widget_grab_focus (widget);
362
363         if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
364                 GtkWidget *att_view = NULL;
365
366                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
367                                                    (gint) event->x_root, (gint) event->y_root);
368
369                 if (att_view != NULL) {
370                         if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED && (g_list_length (priv->selected) < 2)) {
371                                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
372                                 if (TNY_IS_MIME_PART (mime_part)) {
373                                         g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
374                                         g_object_unref (mime_part);
375                                 }
376                         } else {
377                                 set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
378                                 priv->rubber_start = att_view;
379                                 gtk_grab_add (widget);
380                         }
381                 }
382         }
383         return TRUE;
384
385 }
386
387 static gboolean
388 button_release_event (GtkWidget *widget,
389                       GdkEventButton *event,
390                       ModestAttachmentsView *atts_view)
391 {
392         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
393         if (widget == gtk_grab_get_current ()) {
394                 GtkWidget *att_view = NULL;
395
396                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
397                                                    (gint) event->x_root, (gint) event->y_root);
398
399                 if (att_view != NULL) {
400                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
401                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
402                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
403                                       MODEST_ATTACHMENT_VIEW (att_view));
404                 }
405                 priv->rubber_start = NULL;
406                 gtk_grab_remove (widget);
407         }
408         return TRUE;
409 }
410
411 static gboolean
412 motion_notify_event (GtkWidget *widget,
413                      GdkEventMotion *event,
414                      ModestAttachmentsView *atts_view)
415 {
416         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
417         if (gtk_grab_get_current () == widget) {
418                 GtkWidget *att_view = NULL;
419
420                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
421                                                    (gint) event->x_root, (gint) event->y_root);
422
423                 if (att_view != NULL) {
424                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
425                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
426                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
427                                       MODEST_ATTACHMENT_VIEW (att_view));
428                 }
429         }
430         return TRUE;
431 }
432
433 static gboolean
434 key_press_event (GtkWidget *widget,
435                  GdkEventKey *event,
436                  ModestAttachmentsView *atts_view)
437 {
438         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
439
440         /* If grabbed (for example rubber banding), escape leaves the rubberbanding mode */
441         if (gtk_grab_get_current () == widget) {
442                 if (event->keyval == GDK_Escape) {
443                         set_selected (MODEST_ATTACHMENTS_VIEW (widget),
444                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start));
445                         priv->rubber_start = NULL;
446                         gtk_grab_remove (widget);
447                         return TRUE;
448                 } 
449                 return FALSE;
450         }
451
452         if (event->keyval == GDK_Up) {
453                 ModestAttachmentView *current_sel = NULL;
454                 gboolean move_out = FALSE;
455                 GList * box_children, *new_sel;
456
457                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
458                 if (box_children == NULL)
459                         move_out = TRUE;
460                 else if ((priv->selected != NULL)&&(priv->selected->data != box_children->data))
461                         current_sel = (ModestAttachmentView *) priv->selected->data;
462                 else
463                         move_out = TRUE;
464
465                 if (move_out) {
466                         GtkWidget *toplevel = NULL;
467                         /* move cursor outside */
468                         toplevel = gtk_widget_get_toplevel (widget);
469                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
470                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_UP);
471                         unselect_all (atts_view);
472                 } else {
473                         new_sel = g_list_find (box_children, (gpointer) current_sel);
474                         new_sel = g_list_previous (new_sel);
475                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
476                 }
477                 g_list_free (box_children);
478                 return TRUE;
479         }
480
481         if (event->keyval == GDK_Down) {
482                 ModestAttachmentView *current_sel = NULL;
483                 gboolean move_out = FALSE;
484                 GList * box_children, *new_sel, *last_child = NULL;
485
486                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
487
488                 if (box_children == NULL) {
489                         move_out = TRUE;
490                 } else {
491                         last_child = g_list_last (box_children);
492                         if (priv->selected != NULL) {
493                                 GList *last_selected = g_list_last (priv->selected);
494                                 if (last_selected->data != last_child->data)
495                                         current_sel = (ModestAttachmentView *) last_selected->data;
496                                 else
497                                         move_out = TRUE;
498                         } else {
499                                 move_out = TRUE;
500                         }
501                 }
502
503                 if (move_out) {
504                         GtkWidget *toplevel = NULL;
505                         /* move cursor outside */
506                         toplevel = gtk_widget_get_toplevel (widget);
507                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
508                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_DOWN);
509                         unselect_all (atts_view);
510                 } else {
511                         new_sel = g_list_find (box_children, (gpointer) current_sel);
512                         new_sel = g_list_next (new_sel);
513                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
514                 }
515                 g_list_free (box_children);
516                 return TRUE;
517         }
518
519         /* Activates selected item */
520         if (g_list_length (priv->selected) == 1) {
521                 ModestAttachmentView *att_view = (ModestAttachmentView *) priv->selected->data;
522                 if ((event->keyval == GDK_Return)) {
523                         TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
524                         if (TNY_IS_MIME_PART (mime_part)) {
525                                 g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
526                                 g_object_unref (mime_part);
527                         }
528                         return TRUE;
529                 }
530         }
531
532         return FALSE;
533 }
534
535
536 static GtkWidget *
537 get_att_view_at_coords (ModestAttachmentsView *atts_view,
538                         gdouble x, gdouble y)
539 {
540         ModestAttachmentsViewPrivate *priv = NULL;
541         GList *att_view_list, *node;
542         GtkWidget *result = NULL;
543
544         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
545         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
546         
547         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
548                 GtkWidget *att_view = (GtkWidget *) node->data;
549                 gint pos_x, pos_y, w, h, int_x, int_y;
550                 gint widget_x, widget_y;
551
552                 gdk_window_get_origin (att_view->window, &widget_x, &widget_y);
553
554                 pos_x = widget_x;
555                 pos_y = widget_y;
556                 w = att_view->allocation.width;
557                 h = att_view->allocation.height;
558
559                 int_x = (gint) x - GTK_WIDGET (atts_view)->allocation.x;
560                 int_y = (gint) y - GTK_WIDGET (atts_view)->allocation.y;
561
562                 if ((x >= pos_x) && (x <= (pos_x + w)) && (y >= pos_y) && (y <= (pos_y + h))) {
563                         result = att_view;
564                         break;
565                 }
566         }
567
568         g_list_free (att_view_list);
569         return result;
570 }
571
572 static void
573 unselect_all (ModestAttachmentsView *atts_view)
574 {
575         ModestAttachmentsViewPrivate *priv = NULL;
576         GList *att_view_list, *node;
577
578         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
579         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
580         
581         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
582                 GtkWidget *att_view = (GtkWidget *) node->data;
583
584                 if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED)
585                         gtk_widget_set_state (att_view, GTK_STATE_NORMAL);
586         }
587
588         g_list_free (priv->selected);
589         priv->selected = NULL;
590
591         g_list_free (att_view_list);
592 }
593
594 static void 
595 set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view)
596 {
597         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
598         TnyMimePart *part;
599
600         unselect_all (atts_view);
601         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
602         
603         g_list_free (priv->selected);
604         priv->selected = NULL;
605         if (TNY_IS_MIME_PART (part) && !tny_mime_part_is_purged (part)) {
606                 gtk_widget_set_state (GTK_WIDGET (att_view), GTK_STATE_SELECTED);
607                 priv->selected = g_list_append (priv->selected, att_view);
608         }
609         if (part)
610                 g_object_unref (part);
611         
612         own_clipboard (atts_view);
613 }
614
615 static void 
616 select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2)
617 {
618         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
619         GList *children = NULL;
620         GList *node = NULL;
621         gboolean selecting = FALSE;
622         TnyMimePart *part;
623
624         unselect_all (atts_view);
625
626         if (att1 == att2) {
627                 set_selected (atts_view, att1);
628                 return;
629         }
630
631         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
632         g_list_free (priv->selected);
633         priv->selected = NULL;
634
635
636         for (node = children; node != NULL; node = g_list_next (node)) {
637                 if ((node->data == att1) || (node->data == att2)) {
638                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
639                         if (!tny_mime_part_is_purged (part)) {
640                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
641                                 priv->selected = g_list_append (priv->selected, node->data);
642                         }
643                         g_object_unref (part);
644                         selecting = !selecting;
645                 } else if (selecting) {
646                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
647                         if (!tny_mime_part_is_purged (part)) {
648                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
649                                 priv->selected = g_list_append (priv->selected, node->data);
650                         }
651                         g_object_unref (part);
652                 }
653                         
654         }
655         g_list_free (children);
656         
657         own_clipboard (atts_view);
658 }
659
660 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
661                            guint info, gpointer userdata)
662 {
663         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
664         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
665
666         if ((priv->selected != NULL)&&(priv->selected->next == NULL)) {
667                 if (info == MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX) {
668                         /* MODEST_ATTACHMENT requested. As the content id is not filled in all the case, we'll
669                          * use an internal index. This index is simply the index of the attachment in the vbox */
670                         GList *box_children = NULL;
671                         gint index;
672                         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
673                         index = g_list_index (box_children, priv->selected);
674                         if (index >= 0) {
675                                 gchar *index_str = g_strdup_printf("%d", index);
676                                 gtk_selection_data_set_text (selection_data, index_str, -1);
677                                 g_free (index_str);
678                         }
679                 }
680         }
681 }
682
683 static void clipboard_clear (GtkClipboard *clipboard, gpointer userdata)
684 {
685         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
686
687         unselect_all (atts_view);
688 }
689
690 GList *
691 modest_attachments_view_get_selection (ModestAttachmentsView *atts_view)
692 {
693         ModestAttachmentsViewPrivate *priv;
694         GList *selection, *node;
695
696         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
697         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
698
699         selection = NULL;
700         for (node = priv->selected; node != NULL; node = g_list_next (node)) {
701                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
702                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
703                 selection = g_list_append (selection, part);
704         }
705         
706         return selection;
707 }
708
709 GList *
710 modest_attachments_view_get_attachments (ModestAttachmentsView *atts_view)
711 {
712         ModestAttachmentsViewPrivate *priv;
713         GList *children, *node, *att_list = NULL;
714
715         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
716         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
717
718         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
719         for (node = children; node != NULL; node = g_list_next (node)) {
720                 GtkWidget *att_view = GTK_WIDGET (node->data);
721                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
722                 att_list = g_list_prepend (att_list, mime_part);
723         }
724         g_list_free (children);
725         att_list = g_list_reverse (att_list);
726         return att_list;
727
728 }
729
730 void
731 modest_attachments_view_select_all (ModestAttachmentsView *atts_view)
732 {
733         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
734         GList *children = NULL;
735         GList *node = NULL;
736
737         unselect_all (atts_view);
738
739         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
740         g_list_free (priv->selected);
741         priv->selected = NULL;
742
743
744         for (node = children; node != NULL; node = g_list_next (node)) {
745                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
746                 priv->selected = g_list_append (priv->selected, node->data);
747         }
748         g_list_free (children);
749
750         own_clipboard (atts_view);
751 }
752
753 gboolean
754 modest_attachments_view_has_attachments (ModestAttachmentsView *atts_view)
755 {
756         ModestAttachmentsViewPrivate *priv;
757         GList *children;
758         gboolean result;
759
760         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), FALSE);
761         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
762
763         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
764         result = (children != NULL);
765         g_list_free (children);
766
767         return result;
768 }
769
770 static void
771 own_clipboard (ModestAttachmentsView *atts_view)
772 {
773         GtkTargetEntry targets[] = {
774                 {MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE, 0, MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX},
775         };
776
777         gtk_clipboard_set_with_owner (gtk_widget_get_clipboard (GTK_WIDGET (atts_view), GDK_SELECTION_PRIMARY),
778                                       targets, G_N_ELEMENTS (targets),
779                                       clipboard_get, clipboard_clear, G_OBJECT(atts_view));
780                               
781 }
782
783 static gboolean 
784 focus_out_event (GtkWidget *widget, GdkEventFocus *event, ModestAttachmentsView *atts_view)
785 {
786         if (!gtk_widget_is_focus (widget))
787                 unselect_all (atts_view);
788
789         return FALSE;
790 }
791
792 static gboolean 
793 focus (GtkWidget *widget, GtkDirectionType direction, ModestAttachmentsView *atts_view)
794 {
795         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
796         GList *children = NULL;
797         GtkWidget *toplevel = NULL;
798
799         toplevel = gtk_widget_get_toplevel (widget);
800         if (!gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)))
801                 return FALSE;
802
803         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
804         if (children != NULL) {
805                 set_selected (atts_view, MODEST_ATTACHMENT_VIEW (children->data));
806         }
807         g_list_free (children);
808
809         return FALSE;
810 }