Request motion events after processing the first one in attachments view
[modest] / src / widgets / modest-attachments-view.c
1 /* Copyright (c) 2007, 2009, 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 #include <modest-tny-msg.h>
44
45 static GObjectClass *parent_class = NULL;
46
47 /* signals */
48 enum {
49         ACTIVATE_SIGNAL,
50         DELETE_SIGNAL,
51         LAST_SIGNAL
52 };
53
54 typedef struct _ModestAttachmentsViewPrivate ModestAttachmentsViewPrivate;
55
56 struct _ModestAttachmentsViewPrivate
57 {
58         TnyMsg *msg;
59         GtkWidget *box;
60         GList *selected;
61         GtkWidget *rubber_start;
62         GtkWidget *press_att_view;
63         ModestAttachmentsViewStyle style;
64 };
65
66 #define MODEST_ATTACHMENTS_VIEW_GET_PRIVATE(o)  \
67         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ATTACHMENTS_VIEW, ModestAttachmentsViewPrivate))
68
69 static gboolean button_press_event (GtkWidget *widget, GdkEventButton *event, ModestAttachmentsView *atts_view);
70 static gboolean motion_notify_event (GtkWidget *widget, GdkEventMotion *event, ModestAttachmentsView *atts_view);
71 static gboolean button_release_event (GtkWidget *widget, GdkEventButton *event, ModestAttachmentsView *atts_view);
72 static gboolean key_press_event (GtkWidget *widget, GdkEventKey *event, ModestAttachmentsView *atts_view);
73 static gboolean focus_out_event (GtkWidget *widget, GdkEventFocus *event, ModestAttachmentsView *atts_view);
74 static gboolean focus (GtkWidget *widget, GtkDirectionType direction, ModestAttachmentsView *atts_view);
75 static GtkWidget *get_att_view_at_coords (ModestAttachmentsView *atts_view,
76                                           gdouble x, gdouble y);
77 static void unselect_all (ModestAttachmentsView *atts_view);
78 static void set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view);
79 static void select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2);
80 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
81                            guint info, gpointer userdata);
82 static void own_clipboard (ModestAttachmentsView *atts_view);
83
84 static guint signals[LAST_SIGNAL] = {0};
85
86 /**
87  * modest_attachments_view_new:
88  * @msg: a #TnyMsg
89  *
90  * Constructor for attachments view widget.
91  *
92  * Return value: a new #ModestAttachmentsView instance implemented for Gtk+
93  **/
94 GtkWidget*
95 modest_attachments_view_new (TnyMsg *msg)
96 {
97         ModestAttachmentsView *self = g_object_new (MODEST_TYPE_ATTACHMENTS_VIEW, 
98                                                     "resize-mode", GTK_RESIZE_PARENT,
99                                                     NULL);
100
101         modest_attachments_view_set_message (self, msg);
102
103         return GTK_WIDGET (self);
104 }
105
106
107 void
108 modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, TnyMsg *msg)
109 {
110         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
111         TnyList *parts;
112         TnyIterator *iter;
113         gchar *msg_content_type = NULL;
114         TnyMimePart *part_to_check;
115         
116         if (msg == priv->msg) return;
117
118         if (priv->msg)
119                 g_object_unref (priv->msg);
120         if (msg)
121                 g_object_ref (G_OBJECT(msg));
122         
123         priv->msg = msg;
124
125         g_list_free (priv->selected);
126         priv->selected = NULL;
127
128         gtk_container_foreach (GTK_CONTAINER (priv->box), (GtkCallback) gtk_widget_destroy, NULL);
129         
130         if (priv->msg == NULL) {
131                 return;
132         }
133
134         part_to_check = modest_tny_msg_get_attachments_parent (TNY_MSG (msg));
135
136         msg_content_type = modest_tny_mime_part_get_content_type (TNY_MIME_PART (part_to_check));
137
138         /* If the top mime part is a multipart/related, we don't show the attachments, as they're
139          * embedded images in body */
140         if ((msg_content_type != NULL) && !strcasecmp (msg_content_type, "multipart/related")) {
141                 gchar *header_content_type;
142                 gboolean application_multipart = FALSE;
143
144                 g_free (msg_content_type);
145
146                 header_content_type = modest_tny_mime_part_get_headers_content_type (TNY_MIME_PART (part_to_check));
147                 
148                 if ((header_content_type != NULL) && 
149                     !strstr (header_content_type, "application/")) {
150                         application_multipart = TRUE;
151                 }
152                 g_free (header_content_type);
153
154                 if (application_multipart) {
155                         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
156                         g_object_unref (part_to_check);
157                         return;
158                 }
159         } else {
160                 gboolean direct_attach;
161
162                 direct_attach = (!g_str_has_prefix (msg_content_type, "message/rfc822") && 
163                                  !g_str_has_prefix (msg_content_type, "multipart") && 
164                                  !g_str_has_prefix (msg_content_type, "text/"));
165
166                 g_free (msg_content_type);
167
168                 if (direct_attach) {
169                         modest_attachments_view_add_attachment (attachments_view, TNY_MIME_PART (part_to_check), TRUE, 0);
170                         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
171                         g_object_unref (part_to_check);
172                         return;
173                 }
174         }
175
176         parts = TNY_LIST (tny_simple_list_new ());
177         tny_mime_part_get_parts (TNY_MIME_PART (part_to_check), parts);
178         iter = tny_list_create_iterator (parts);
179
180         while (!tny_iterator_is_done (iter)) {
181                 TnyMimePart *part;
182
183                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
184
185                 if (part && (modest_tny_mime_part_is_attachment_for_modest (part))) 
186                         modest_attachments_view_add_attachment (attachments_view, part, TRUE, 0);
187
188                 if (part)
189                         g_object_unref (part);
190
191                 tny_iterator_next (iter);
192         }
193         g_object_unref (iter);
194         g_object_unref (parts);
195         g_object_unref (part_to_check);
196         
197
198         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
199
200 }
201
202 void
203 modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part,
204                                         gboolean detect_size, guint64 size)
205 {
206         GtkWidget *att_view = NULL;
207         ModestAttachmentsViewPrivate *priv = NULL;
208
209         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
210         g_return_if_fail (TNY_IS_MIME_PART (part));
211
212         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
213
214         att_view = modest_attachment_view_new (part, detect_size);
215         if (!detect_size)
216                 modest_attachment_view_set_size (MODEST_ATTACHMENT_VIEW (att_view), size);
217         gtk_box_pack_start (GTK_BOX (priv->box), att_view, FALSE, FALSE, 0);
218         gtk_widget_show_all (att_view);
219         gtk_widget_queue_resize (GTK_WIDGET (attachments_view));
220 }
221
222 void
223 modest_attachments_view_remove_attachment (ModestAttachmentsView *atts_view, TnyMimePart *mime_part)
224 {
225         ModestAttachmentsViewPrivate *priv = NULL;
226         GList *box_children = NULL, *node = NULL;
227         ModestAttachmentView *found_att_view = NULL;
228
229         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
230         g_return_if_fail (TNY_IS_MIME_PART (mime_part));
231
232         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
233         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
234
235         for (node = box_children; node != NULL; node = g_list_next (node)) {
236                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
237                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
238
239                 if (mime_part == cur_mime_part)
240                         found_att_view = att_view;
241
242                 g_object_unref (cur_mime_part);
243
244                 if (found_att_view != NULL)
245                         break;
246         }
247
248         if (found_att_view) {
249                 GList *node = NULL;
250                 GtkWidget *next_widget = NULL;
251                 GList *box_children = NULL;
252
253                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
254                 node = g_list_find (box_children, found_att_view);
255                 if (node && node->next)
256                         next_widget = node->next->data;
257
258                 g_list_free (box_children);
259                 gtk_widget_destroy (GTK_WIDGET (found_att_view));
260
261                 node = g_list_find (priv->selected, found_att_view);
262                 if (node) {
263                         priv->selected = g_list_delete_link (priv->selected, node);
264                         if ((priv->selected == NULL) && (next_widget != NULL))
265                                 set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), 
266                                               MODEST_ATTACHMENT_VIEW (next_widget));
267                 }
268                 own_clipboard (atts_view);
269
270         }
271
272         gtk_widget_queue_resize (GTK_WIDGET (atts_view));
273 }
274
275 void
276 modest_attachments_view_remove_attachment_by_id (ModestAttachmentsView *atts_view, const gchar *att_id)
277 {
278         ModestAttachmentsViewPrivate *priv = NULL;
279         GList *box_children = NULL, *node = NULL;
280
281         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
282         g_return_if_fail (att_id != NULL);
283
284         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
285         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
286
287         for (node = box_children; node != NULL; node = g_list_next (node)) {
288                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
289                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
290                 const gchar *mime_part_id = NULL;
291
292                 mime_part_id = tny_mime_part_get_content_id (cur_mime_part);
293                 if ((mime_part_id != NULL) && (strcmp (mime_part_id, att_id) == 0)) {
294                         gtk_widget_destroy (GTK_WIDGET (att_view));
295                         priv->selected = g_list_remove (priv->selected, att_view);
296                 }
297
298                 g_object_unref (cur_mime_part);
299         }
300
301         own_clipboard (atts_view);
302
303         gtk_widget_queue_resize (GTK_WIDGET (atts_view));
304 }
305
306 static void
307 modest_attachments_view_instance_init (GTypeInstance *instance, gpointer g_class)
308 {
309         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (instance);
310
311         priv->msg = NULL;
312         priv->box = gtk_vbox_new (FALSE, 0);
313         priv->rubber_start = NULL;
314         priv->press_att_view = NULL;
315         priv->selected = NULL;
316         priv->style = MODEST_ATTACHMENTS_VIEW_STYLE_SELECTABLE;
317
318         gtk_container_add (GTK_CONTAINER (instance), priv->box);
319         gtk_event_box_set_above_child (GTK_EVENT_BOX (instance), TRUE);
320
321         g_signal_connect (G_OBJECT (instance), "button-press-event", G_CALLBACK (button_press_event), instance);
322         g_signal_connect (G_OBJECT (instance), "button-release-event", G_CALLBACK (button_release_event), instance);
323         g_signal_connect (G_OBJECT (instance), "motion-notify-event", G_CALLBACK (motion_notify_event), instance);
324         g_signal_connect (G_OBJECT (instance), "key-press-event", G_CALLBACK (key_press_event), instance);
325         g_signal_connect (G_OBJECT (instance), "focus-out-event", G_CALLBACK (focus_out_event), instance);
326         g_signal_connect (G_OBJECT (instance), "focus", G_CALLBACK (focus), instance);
327
328         GTK_WIDGET_SET_FLAGS (instance, GTK_CAN_FOCUS);
329
330         return;
331 }
332
333 static void
334 modest_attachments_view_finalize (GObject *object)
335 {
336         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (object);
337
338         if (priv->msg) {
339                 g_object_unref (priv->msg);
340                 priv->msg = NULL;
341         }
342
343         (*parent_class->finalize) (object);
344
345         return;
346 }
347
348 static void 
349 modest_attachments_view_class_init (ModestAttachmentsViewClass *klass)
350 {
351         GObjectClass *object_class;
352         GtkWidgetClass *widget_class;
353
354         parent_class = g_type_class_peek_parent (klass);
355         object_class = (GObjectClass*) klass;
356         widget_class = GTK_WIDGET_CLASS (klass);
357
358         object_class->finalize = modest_attachments_view_finalize;
359
360         klass->activate = NULL;
361
362         g_type_class_add_private (object_class, sizeof (ModestAttachmentsViewPrivate));
363
364         signals[ACTIVATE_SIGNAL] =
365                 g_signal_new ("activate",
366                               G_TYPE_FROM_CLASS (object_class),
367                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
368                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, activate),
369                               NULL, NULL,
370                               g_cclosure_marshal_VOID__OBJECT,
371                               G_TYPE_NONE, 1, G_TYPE_OBJECT);
372         
373         signals[DELETE_SIGNAL] =
374                 g_signal_new ("delete",
375                               G_TYPE_FROM_CLASS (object_class),
376                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
377                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, delete),
378                               NULL, NULL,
379                               g_cclosure_marshal_VOID__VOID,
380                               G_TYPE_NONE, 0);
381
382         return;
383 }
384
385 GType 
386 modest_attachments_view_get_type (void)
387 {
388         static GType type = 0;
389
390         if (G_UNLIKELY(type == 0))
391         {
392                 static const GTypeInfo info = 
393                 {
394                   sizeof (ModestAttachmentsViewClass),
395                   NULL,   /* base_init */
396                   NULL,   /* base_finalize */
397                   (GClassInitFunc) modest_attachments_view_class_init,   /* class_init */
398                   NULL,   /* class_finalize */
399                   NULL,   /* class_data */
400                   sizeof (ModestAttachmentsView),
401                   0,      /* n_preallocs */
402                   modest_attachments_view_instance_init    /* instance_init */
403                 };
404
405                 type = g_type_register_static (GTK_TYPE_EVENT_BOX,
406                         "ModestAttachmentsView",
407                         &info, 0);
408
409         }
410
411         return type;
412 }
413
414 /* buttons signal events */
415 static gboolean
416 button_press_event (GtkWidget *widget, 
417                     GdkEventButton *event,
418                     ModestAttachmentsView *atts_view)
419 {
420         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
421         if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_SELECTABLE && 
422             !GTK_WIDGET_HAS_FOCUS (widget))
423                 gtk_widget_grab_focus (widget);
424
425         if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
426                 GtkWidget *att_view = NULL;
427
428                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
429                                                    (gint) event->x_root, (gint) event->y_root);
430
431                 if (att_view != NULL) {
432                         if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_NO_FOCUS) {
433                                 unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
434                         } else if ((priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_LINKS) ||
435                             (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED && (g_list_length (priv->selected) < 2))) {
436                                 priv->press_att_view = att_view;
437                                 set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
438                                 gtk_grab_add (widget);
439                         } else {
440                                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
441
442                                 /* Do not select purged attachments */
443                                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
444                                         set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
445                                         priv->rubber_start = att_view;
446                                         gtk_grab_add (widget);
447                                 }
448                                 g_object_unref (mime_part);
449                         }
450                 }
451         }
452         return TRUE;
453
454 }
455
456 static gboolean
457 button_release_event (GtkWidget *widget,
458                       GdkEventButton *event,
459                       ModestAttachmentsView *atts_view)
460 {
461         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
462         if (widget == gtk_grab_get_current ()) {
463                 GtkWidget *att_view = NULL;
464
465                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
466                                                    (gint) event->x_root, (gint) event->y_root);
467
468                 if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_LINKS) {
469                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
470                         if (att_view == priv->press_att_view) {
471                                 TnyMimePart *mime_part;
472                                 mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
473                                 g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
474                                 g_object_unref (mime_part);
475                         }
476                         priv->press_att_view = NULL;
477                 } else {
478
479                         if (att_view != NULL) {
480                                 unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
481                                 select_range (MODEST_ATTACHMENTS_VIEW (widget), 
482                                               MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
483                                               MODEST_ATTACHMENT_VIEW (att_view));
484                         }
485                         priv->rubber_start = NULL;
486                 }
487                 gtk_grab_remove (widget);
488         }
489         return TRUE;
490 }
491
492 static gboolean
493 motion_notify_event (GtkWidget *widget,
494                      GdkEventMotion *event,
495                      ModestAttachmentsView *atts_view)
496 {
497         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
498         if (gtk_grab_get_current () == widget) {
499                 GtkWidget *att_view = NULL;
500
501                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
502                                                    (gint) event->x_root, (gint) event->y_root);
503                 if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_LINKS) {
504                         if (att_view == priv->press_att_view) {
505                                 if (priv->selected == NULL)
506                                 set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
507                         } else {
508                                 if (priv->selected) {
509                                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
510                                 }
511                         }
512                 } else {
513
514                         if (att_view != NULL) {
515                                 unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
516                                 select_range (MODEST_ATTACHMENTS_VIEW (widget), 
517                                               MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
518                                               MODEST_ATTACHMENT_VIEW (att_view));
519                         }
520                 }
521                 gdk_event_request_motions (event);
522         }
523         return TRUE;
524 }
525
526 static GList*
527 find_prev_or_next_not_purged (GList *list, gboolean prev, gboolean include_this)
528 {
529         GList *tmp = NULL;
530         gboolean is_valid;
531
532         if (!include_this) {
533                 if (prev) {
534                         tmp = g_list_previous (list);
535                 } else {
536                         tmp = g_list_next (list);
537                 }
538         } else {
539                 tmp = list;
540         }
541
542         if (!tmp)
543                 return NULL;
544
545         do {
546                 ModestAttachmentView *att_view = (ModestAttachmentView *) tmp->data;
547                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
548                 
549                 /* Do not select purged attachments */
550                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
551                         is_valid = TRUE;
552                 } else {
553                         if (prev)
554                                 tmp = g_list_previous (tmp);
555                         else
556                                 tmp = g_list_next (tmp);
557                         is_valid = FALSE;
558                 }
559                 g_object_unref (mime_part);
560         } while (!is_valid && tmp);
561
562         return tmp;
563 }
564
565
566 static gboolean
567 key_press_event (GtkWidget *widget,
568                  GdkEventKey *event,
569                  ModestAttachmentsView *atts_view)
570 {
571         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
572
573         if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_NO_FOCUS) {
574                 unselect_all (atts_view);
575                 return FALSE;
576         }
577
578         /* If grabbed (for example rubber banding), escape leaves the rubberbanding mode */
579         if (gtk_grab_get_current () == widget) {
580                 if (event->keyval == GDK_Escape) {
581                         set_selected (MODEST_ATTACHMENTS_VIEW (widget),
582                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start));
583                         priv->rubber_start = NULL;
584                         gtk_grab_remove (widget);
585                         return TRUE;
586                 } 
587                 return FALSE;
588         }
589
590         if (event->keyval == GDK_Up) {
591                 ModestAttachmentView *current_sel = NULL;
592                 gboolean move_out = FALSE;
593                 GList * box_children, *new_sel, *first_child;
594
595                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
596                 if (box_children == NULL) {
597                         move_out = TRUE;
598                 } else { 
599                         first_child = box_children;
600                         first_child = find_prev_or_next_not_purged (box_children, FALSE, TRUE);
601                         if (priv->selected != NULL && first_child != NULL) {
602                                 if (priv->selected->data != first_child->data)
603                                         current_sel = (ModestAttachmentView *) priv->selected->data;
604                                 else
605                                         move_out = TRUE;
606                         } else {
607                                 move_out = TRUE;
608                         }
609                 }
610
611                 if (move_out) {
612                         GtkWidget *toplevel = NULL;
613                         /* move cursor outside */
614                         toplevel = gtk_widget_get_toplevel (widget);
615                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
616                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_UP);
617                         unselect_all (atts_view);
618                 } else {
619                         new_sel = g_list_find (box_children, (gpointer) current_sel);
620                         new_sel = find_prev_or_next_not_purged (new_sel, TRUE, FALSE);
621                         /* We assume that we detected properly that
622                            there is a not purge attachment so we don't
623                            need to check NULL */
624                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
625                 }
626                 g_list_free (box_children);
627                 return TRUE;
628         }
629
630         if (event->keyval == GDK_Down) {
631                 ModestAttachmentView *current_sel = NULL;
632                 gboolean move_out = FALSE;
633                 GList * box_children, *new_sel, *last_child = NULL;
634
635                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
636
637                 if (box_children == NULL) {
638                         move_out = TRUE;
639                 } else {
640                         last_child = g_list_last (box_children);
641                         last_child = find_prev_or_next_not_purged (last_child, TRUE, TRUE);
642                         if (priv->selected != NULL && last_child != NULL) {
643                                 GList *last_selected = g_list_last (priv->selected);
644                                 if (last_selected->data != last_child->data)
645                                         current_sel = (ModestAttachmentView *) last_selected->data;
646                                 else
647                                         move_out = TRUE;
648                         } else {
649                                 move_out = TRUE;
650                         }
651                 }
652
653                 if (move_out) {
654                         GtkWidget *toplevel = NULL;
655                         /* move cursor outside */
656                         toplevel = gtk_widget_get_toplevel (widget);
657                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
658                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_DOWN);
659                         unselect_all (atts_view);
660                 } else {
661                         new_sel = g_list_find (box_children, (gpointer) current_sel);
662                         new_sel = find_prev_or_next_not_purged (new_sel, FALSE, FALSE);
663                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
664                 }
665                 g_list_free (box_children);
666                 return TRUE;
667         }
668
669         if (event->keyval == GDK_BackSpace) {
670                 g_signal_emit (G_OBJECT (widget), signals[DELETE_SIGNAL], 0);
671                 return TRUE;
672         }
673
674         /* Activates selected item */
675         if (g_list_length (priv->selected) == 1) {
676                 ModestAttachmentView *att_view = (ModestAttachmentView *) priv->selected->data;
677                 if ((event->keyval == GDK_Return)) {
678                         TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
679                         if (TNY_IS_MIME_PART (mime_part)) {
680                                 g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
681                                 g_object_unref (mime_part);
682                         }
683                         return TRUE;
684                 }
685         }
686
687         return FALSE;
688 }
689
690
691 static GtkWidget *
692 get_att_view_at_coords (ModestAttachmentsView *atts_view,
693                         gdouble x, gdouble y)
694 {
695         ModestAttachmentsViewPrivate *priv = NULL;
696         GList *att_view_list, *node;
697         GtkWidget *result = NULL;
698
699         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
700         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
701         
702         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
703                 GtkWidget *att_view = (GtkWidget *) node->data;
704                 gint pos_x, pos_y, w, h, int_x, int_y;
705                 gint widget_x, widget_y;
706
707                 gdk_window_get_origin (att_view->window, &widget_x, &widget_y);
708
709                 pos_x = widget_x;
710                 pos_y = widget_y;
711                 w = att_view->allocation.width;
712                 h = att_view->allocation.height;
713
714                 int_x = (gint) x - GTK_WIDGET (atts_view)->allocation.x;
715                 int_y = (gint) y - GTK_WIDGET (atts_view)->allocation.y;
716
717                 if ((x >= pos_x) && (x <= (pos_x + w)) && (y >= pos_y) && (y <= (pos_y + h))) {
718                         result = att_view;
719                         break;
720                 }
721         }
722
723         g_list_free (att_view_list);
724         return result;
725 }
726
727 static void
728 unselect_all (ModestAttachmentsView *atts_view)
729 {
730         ModestAttachmentsViewPrivate *priv = NULL;
731         GList *att_view_list, *node;
732
733         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
734         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
735         
736         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
737                 GtkWidget *att_view = (GtkWidget *) node->data;
738
739                 if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED)
740                         gtk_widget_set_state (att_view, GTK_STATE_NORMAL);
741         }
742
743         g_list_free (priv->selected);
744         priv->selected = NULL;
745
746         g_list_free (att_view_list);
747 }
748
749 static void 
750 set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view)
751 {
752         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
753         TnyMimePart *part;
754
755         unselect_all (atts_view);
756         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
757         
758         g_list_free (priv->selected);
759         priv->selected = NULL;
760         if (TNY_IS_MIME_PART (part) && !tny_mime_part_is_purged (part)) {
761                 gtk_widget_set_state (GTK_WIDGET (att_view), GTK_STATE_SELECTED);
762                 priv->selected = g_list_append (priv->selected, att_view);
763         }
764         if (part)
765                 g_object_unref (part);
766         
767         own_clipboard (atts_view);
768 }
769
770 static void 
771 select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2)
772 {
773         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
774         GList *children = NULL;
775         GList *node = NULL;
776         gboolean selecting = FALSE;
777         TnyMimePart *part;
778
779         unselect_all (atts_view);
780
781         if (att1 == att2) {
782                 set_selected (atts_view, att1);
783                 return;
784         }
785
786         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
787         g_list_free (priv->selected);
788         priv->selected = NULL;
789
790
791         for (node = children; node != NULL; node = g_list_next (node)) {
792                 if ((node->data == att1) || (node->data == att2)) {
793                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
794                         if (!tny_mime_part_is_purged (part)) {
795                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
796                                 priv->selected = g_list_append (priv->selected, node->data);
797                         }
798                         g_object_unref (part);
799                         selecting = !selecting;
800                 } else if (selecting) {
801                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
802                         if (!tny_mime_part_is_purged (part)) {
803                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
804                                 priv->selected = g_list_append (priv->selected, node->data);
805                         }
806                         g_object_unref (part);
807                 }
808                         
809         }
810         g_list_free (children);
811         
812         own_clipboard (atts_view);
813 }
814
815 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
816                            guint info, gpointer userdata)
817 {
818         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
819         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
820
821         if ((priv->selected != NULL)&&(priv->selected->next == NULL)) {
822                 if (info == MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX) {
823                         /* MODEST_ATTACHMENT requested. As the content id is not filled in all the case, we'll
824                          * use an internal index. This index is simply the index of the attachment in the vbox */
825                         GList *box_children = NULL;
826                         gint index;
827                         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
828                         index = g_list_index (box_children, priv->selected);
829                         if (index >= 0) {
830                                 gchar *index_str = g_strdup_printf("%d", index);
831                                 gtk_selection_data_set_text (selection_data, index_str, -1);
832                                 g_free (index_str);
833                         }
834                 }
835         }
836 }
837
838 TnyList *
839 modest_attachments_view_get_selection (ModestAttachmentsView *atts_view)
840 {
841         ModestAttachmentsViewPrivate *priv;
842         TnyList *selection;
843         GList *node;
844
845         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
846         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
847
848         selection = tny_simple_list_new ();
849         for (node = priv->selected; node != NULL; node = g_list_next (node)) {
850                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
851                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
852                 tny_list_append (selection, (GObject *) part);
853                 g_object_unref (part);
854         }
855         
856         return selection;
857 }
858
859 TnyList *
860 modest_attachments_view_get_attachments (ModestAttachmentsView *atts_view)
861 {
862         ModestAttachmentsViewPrivate *priv;
863         TnyList *att_list;
864         GList *children, *node= NULL;
865
866         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
867         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
868
869         att_list = TNY_LIST (tny_simple_list_new ());
870
871         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
872         for (node = children; node != NULL; node = g_list_next (node)) {
873                 GtkWidget *att_view = GTK_WIDGET (node->data);
874                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
875                 tny_list_append (att_list, (GObject *) mime_part);
876                 g_object_unref (mime_part);
877         }
878         g_list_free (children);
879         return att_list;
880
881 }
882
883 void
884 modest_attachments_view_select_all (ModestAttachmentsView *atts_view)
885 {
886         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
887         GList *children = NULL;
888         GList *node = NULL;
889
890         unselect_all (atts_view);
891
892         if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_LINKS)
893                 return;
894
895         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
896         g_list_free (priv->selected);
897         priv->selected = NULL;
898
899         for (node = children; node != NULL; node = g_list_next (node)) {
900                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
901                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
902
903                 /* Do not select purged attachments */
904                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
905                         gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
906                         priv->selected = g_list_append (priv->selected, node->data);
907                 }
908                 g_object_unref (mime_part);
909         }
910         g_list_free (children);
911
912         own_clipboard (atts_view);
913 }
914
915 gboolean
916 modest_attachments_view_has_attachments (ModestAttachmentsView *atts_view)
917 {
918         ModestAttachmentsViewPrivate *priv;
919         GList *children;
920         gboolean result;
921
922         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), FALSE);
923         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
924
925         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
926         result = (children != NULL);
927         g_list_free (children);
928
929         return result;
930 }
931
932 void
933 modest_attachments_view_get_sizes (ModestAttachmentsView *attachments_view,
934                                    gint *attachments_count,
935                                    guint64 *attachments_size)
936 {
937         ModestAttachmentsViewPrivate *priv;
938         GList *children, *node;
939
940         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
941         g_return_if_fail (attachments_count != NULL && attachments_size != NULL);
942
943         *attachments_count = 0;
944         *attachments_size = 0;
945
946         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
947
948         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
949         for (node = children; node != NULL; node = g_list_next (node)) {
950                 GtkWidget *att_view = (GtkWidget *) node->data;
951                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
952
953                 if (!tny_mime_part_is_purged (part)) {
954                         guint64 size;
955                         (*attachments_count) ++;
956                         size = modest_attachment_view_get_size (MODEST_ATTACHMENT_VIEW (att_view));
957                         if (size == 0) {
958                                 /* we do a random estimation of the size of an attachment */
959                                 size = 32768;
960                         }
961                         *attachments_size += size;
962                 }
963                 g_object_unref (part);
964         }
965         g_list_free (children);
966 }
967
968 static void
969 dummy_clear_func (GtkClipboard *clipboard,
970                   gpointer user_data_or_owner)
971 {
972         /* Do nothing */
973 }
974
975 static void
976 own_clipboard (ModestAttachmentsView *atts_view)
977 {
978         GtkTargetEntry targets[] = {
979                 {MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE, 0, MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX},
980         };
981
982         gtk_clipboard_set_with_owner (gtk_widget_get_clipboard (GTK_WIDGET (atts_view), GDK_SELECTION_PRIMARY),
983                                       targets, G_N_ELEMENTS (targets),
984                                       clipboard_get, dummy_clear_func, G_OBJECT(atts_view));
985 }
986
987 static gboolean 
988 focus_out_event (GtkWidget *widget, GdkEventFocus *event, ModestAttachmentsView *atts_view)
989 {
990         if (!gtk_widget_is_focus (widget))
991                 unselect_all (atts_view);
992
993         return FALSE;
994 }
995
996 static gboolean 
997 focus (GtkWidget *widget, GtkDirectionType direction, ModestAttachmentsView *atts_view)
998 {
999         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
1000         GList *children = NULL;
1001         GtkWidget *toplevel = NULL;
1002
1003         toplevel = gtk_widget_get_toplevel (widget);
1004         if (!gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)))
1005                 return FALSE;
1006
1007         if (priv->style != MODEST_ATTACHMENTS_VIEW_STYLE_NO_FOCUS) {
1008                 children = gtk_container_get_children (GTK_CONTAINER (priv->box));
1009                 if (children != NULL) {
1010                         set_selected (atts_view, MODEST_ATTACHMENT_VIEW (children->data));
1011                 }
1012                 g_list_free (children);
1013         }
1014
1015         return FALSE;
1016 }
1017
1018 void 
1019 modest_attachments_view_set_style (ModestAttachmentsView *self,
1020                                    ModestAttachmentsViewStyle style)
1021 {
1022         ModestAttachmentsViewPrivate *priv;
1023
1024         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (self));
1025         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (self);
1026
1027         if (priv->style != style) {
1028                 priv->style = style;
1029                 gtk_widget_queue_draw (GTK_WIDGET (self));
1030                 if (priv->style == MODEST_ATTACHMENTS_VIEW_STYLE_SELECTABLE) {
1031                         GTK_WIDGET_SET_FLAGS (self, GTK_CAN_FOCUS);
1032                 } else {
1033                         GTK_WIDGET_UNSET_FLAGS (self, GTK_CAN_FOCUS);
1034                 }
1035
1036         }
1037 }
1038
1039 guint
1040 modest_attachments_view_get_num_attachments (ModestAttachmentsView *atts_view)
1041 {
1042         ModestAttachmentsViewPrivate *priv;
1043         GList *children;
1044         gint result;
1045
1046         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), 0);
1047         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
1048
1049         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
1050         result = g_list_length (children);
1051         g_list_free (children);
1052
1053         return result;
1054 }