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