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