This is a manual merge of branch drop split view intro trunk.
[modest] / src / widgets / modest-attachments-view.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <config.h>
31
32 #include <string.h>
33 #include <gtk/gtk.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <tny-list.h>
36 #include <tny-simple-list.h>
37
38 #include <modest-platform.h>
39 #include <modest-runtime.h>
40 #include <modest-attachment-view.h>
41 #include <modest-attachments-view.h>
42 #include <modest-tny-mime-part.h>
43
44 static GObjectClass *parent_class = NULL;
45
46 /* signals */
47 enum {
48         ACTIVATE_SIGNAL,
49         DELETE_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 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
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         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 = modest_tny_mime_part_get_content_type (TNY_MIME_PART (priv->msg));
133         if ((msg_content_type != NULL) && !strcasecmp (msg_content_type, "multipart/related")) {
134                 gchar *header_content_type;
135                 gboolean application_multipart = FALSE;
136
137                 g_free (msg_content_type);
138
139                 header_content_type = modest_tny_mime_part_get_headers_content_type (TNY_MIME_PART (priv->msg));
140                 
141                 if ((header_content_type != NULL) && 
142                     !strstr (header_content_type, "application/")) {
143                         application_multipart = TRUE;
144                 }
145                 g_free (header_content_type);
146
147                 if (application_multipart) {
148                         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
149                         return;
150                 }
151         } else {
152                 gboolean direct_attach;
153
154                 direct_attach = (!g_str_has_prefix (msg_content_type, "message/rfc822") && 
155                                  !g_str_has_prefix (msg_content_type, "multipart") && 
156                                  !g_str_has_prefix (msg_content_type, "text/"));
157
158                 g_free (msg_content_type);
159
160                 if (direct_attach) {
161                         modest_attachments_view_add_attachment (attachments_view, TNY_MIME_PART (msg), TRUE, 0);
162                         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
163                         return;
164                 }
165         }
166
167         parts = TNY_LIST (tny_simple_list_new ());
168         tny_mime_part_get_parts (TNY_MIME_PART (priv->msg), parts);
169         iter = tny_list_create_iterator (parts);
170
171         while (!tny_iterator_is_done (iter)) {
172                 TnyMimePart *part;
173
174                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
175
176                 if (part && (modest_tny_mime_part_is_attachment_for_modest (part))) 
177                         modest_attachments_view_add_attachment (attachments_view, part, TRUE, 0);
178
179                 if (part)
180                         g_object_unref (part);
181
182                 tny_iterator_next (iter);
183         }
184         g_object_unref (iter);
185         g_object_unref (parts);
186         
187
188         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
189
190 }
191
192 void
193 modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part,
194                                         gboolean detect_size, guint64 size)
195 {
196         GtkWidget *att_view = NULL;
197         ModestAttachmentsViewPrivate *priv = NULL;
198
199         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
200         g_return_if_fail (TNY_IS_MIME_PART (part));
201
202         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
203
204         att_view = modest_attachment_view_new (part, detect_size);
205         if (!detect_size)
206                 modest_attachment_view_set_size (MODEST_ATTACHMENT_VIEW (att_view), size);
207         gtk_box_pack_end (GTK_BOX (priv->box), att_view, FALSE, FALSE, 0);
208         gtk_widget_show_all (att_view);
209         gtk_widget_queue_resize (GTK_WIDGET (attachments_view));
210 }
211
212 void
213 modest_attachments_view_remove_attachment (ModestAttachmentsView *atts_view, TnyMimePart *mime_part)
214 {
215         ModestAttachmentsViewPrivate *priv = NULL;
216         GList *box_children = NULL, *node = NULL;
217         ModestAttachmentView *found_att_view = NULL;
218
219         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
220         g_return_if_fail (TNY_IS_MIME_PART (mime_part));
221
222         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
223         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
224
225         for (node = box_children; node != NULL; node = g_list_next (node)) {
226                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
227                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
228
229                 if (mime_part == cur_mime_part)
230                         found_att_view = att_view;
231
232                 g_object_unref (cur_mime_part);
233
234                 if (found_att_view != NULL)
235                         break;
236         }
237
238         if (found_att_view) {
239                 GList *node = NULL;
240                 GtkWidget *next_widget = NULL;
241                 GList *box_children = NULL;
242
243                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
244                 node = g_list_find (box_children, found_att_view);
245                 if (node->next)
246                         next_widget = node->next->data;
247
248                 g_list_free (box_children);
249
250                 node = g_list_find (priv->selected, found_att_view);
251                 if (node != NULL) {
252                         priv->selected = g_list_delete_link (priv->selected, node);
253                         gtk_widget_destroy (GTK_WIDGET (found_att_view));
254                         if ((priv->selected == NULL) && (next_widget != NULL))
255                                 set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), 
256                                               MODEST_ATTACHMENT_VIEW (next_widget));
257                 }
258                 own_clipboard (atts_view);
259         }
260
261         gtk_widget_queue_resize (GTK_WIDGET (atts_view));
262 }
263
264 void
265 modest_attachments_view_remove_attachment_by_id (ModestAttachmentsView *atts_view, const gchar *att_id)
266 {
267         ModestAttachmentsViewPrivate *priv = NULL;
268         GList *box_children = NULL, *node = NULL;
269
270         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view));
271         g_return_if_fail (att_id != NULL);
272
273         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
274         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
275
276         for (node = box_children; node != NULL; node = g_list_next (node)) {
277                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
278                 TnyMimePart *cur_mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
279                 const gchar *mime_part_id = NULL;
280
281                 mime_part_id = tny_mime_part_get_content_id (cur_mime_part);
282                 if ((mime_part_id != NULL) && (strcmp (mime_part_id, att_id) == 0)) {
283                         gtk_widget_destroy (GTK_WIDGET (att_view));
284                         priv->selected = g_list_remove (priv->selected, att_view);
285                 }
286
287                 g_object_unref (cur_mime_part);
288         }
289
290         own_clipboard (atts_view);
291
292         gtk_widget_queue_resize (GTK_WIDGET (atts_view));
293 }
294
295 static void
296 modest_attachments_view_instance_init (GTypeInstance *instance, gpointer g_class)
297 {
298         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (instance);
299
300         priv->msg = NULL;
301         priv->box = gtk_vbox_new (FALSE, 0);
302         priv->rubber_start = NULL;
303         priv->selected = NULL;
304
305         gtk_container_add (GTK_CONTAINER (instance), priv->box);
306         gtk_event_box_set_above_child (GTK_EVENT_BOX (instance), TRUE);
307
308         g_signal_connect (G_OBJECT (instance), "button-press-event", G_CALLBACK (button_press_event), instance);
309         g_signal_connect (G_OBJECT (instance), "button-release-event", G_CALLBACK (button_release_event), instance);
310         g_signal_connect (G_OBJECT (instance), "motion-notify-event", G_CALLBACK (motion_notify_event), instance);
311         g_signal_connect (G_OBJECT (instance), "key-press-event", G_CALLBACK (key_press_event), instance);
312         g_signal_connect (G_OBJECT (instance), "focus-out-event", G_CALLBACK (focus_out_event), instance);
313         g_signal_connect (G_OBJECT (instance), "focus", G_CALLBACK (focus), instance);
314
315         GTK_WIDGET_SET_FLAGS (instance, GTK_CAN_FOCUS);
316
317         return;
318 }
319
320 static void
321 modest_attachments_view_finalize (GObject *object)
322 {
323         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (object);
324
325         if (priv->msg) {
326                 g_object_unref (priv->msg);
327                 priv->msg = NULL;
328         }
329
330         (*parent_class->finalize) (object);
331
332         return;
333 }
334
335 static void 
336 modest_attachments_view_class_init (ModestAttachmentsViewClass *klass)
337 {
338         GObjectClass *object_class;
339         GtkWidgetClass *widget_class;
340
341         parent_class = g_type_class_peek_parent (klass);
342         object_class = (GObjectClass*) klass;
343         widget_class = GTK_WIDGET_CLASS (klass);
344
345         object_class->finalize = modest_attachments_view_finalize;
346
347         klass->activate = NULL;
348
349         g_type_class_add_private (object_class, sizeof (ModestAttachmentsViewPrivate));
350
351         signals[ACTIVATE_SIGNAL] =
352                 g_signal_new ("activate",
353                               G_TYPE_FROM_CLASS (object_class),
354                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
355                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, activate),
356                               NULL, NULL,
357                               g_cclosure_marshal_VOID__OBJECT,
358                               G_TYPE_NONE, 1, G_TYPE_OBJECT);
359         
360         signals[DELETE_SIGNAL] =
361                 g_signal_new ("delete",
362                               G_TYPE_FROM_CLASS (object_class),
363                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
364                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, delete),
365                               NULL, NULL,
366                               g_cclosure_marshal_VOID__VOID,
367                               G_TYPE_NONE, 0);
368
369         return;
370 }
371
372 GType 
373 modest_attachments_view_get_type (void)
374 {
375         static GType type = 0;
376
377         if (G_UNLIKELY(type == 0))
378         {
379                 static const GTypeInfo info = 
380                 {
381                   sizeof (ModestAttachmentsViewClass),
382                   NULL,   /* base_init */
383                   NULL,   /* base_finalize */
384                   (GClassInitFunc) modest_attachments_view_class_init,   /* class_init */
385                   NULL,   /* class_finalize */
386                   NULL,   /* class_data */
387                   sizeof (ModestAttachmentsView),
388                   0,      /* n_preallocs */
389                   modest_attachments_view_instance_init    /* instance_init */
390                 };
391
392                 type = g_type_register_static (GTK_TYPE_EVENT_BOX,
393                         "ModestAttachmentsView",
394                         &info, 0);
395
396         }
397
398         return type;
399 }
400
401 /* buttons signal events */
402 static gboolean
403 button_press_event (GtkWidget *widget, 
404                     GdkEventButton *event,
405                     ModestAttachmentsView *atts_view)
406 {
407         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
408         if (!GTK_WIDGET_HAS_FOCUS (widget))
409                 gtk_widget_grab_focus (widget);
410
411         if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
412                 GtkWidget *att_view = NULL;
413
414                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
415                                                    (gint) event->x_root, (gint) event->y_root);
416
417                 if (att_view != NULL) {
418                         if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED && (g_list_length (priv->selected) < 2)) {
419                                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
420                                 if (TNY_IS_MIME_PART (mime_part)) {
421                                         g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
422                                         g_object_unref (mime_part);
423                                 }
424                         } else {
425                                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
426
427                                 /* Do not select purged attachments */
428                                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
429                                         set_selected (MODEST_ATTACHMENTS_VIEW (widget), MODEST_ATTACHMENT_VIEW (att_view));
430                                         priv->rubber_start = att_view;
431                                         gtk_grab_add (widget);
432                                 }
433                                 g_object_unref (mime_part);
434                         }
435                 }
436         }
437         return TRUE;
438
439 }
440
441 static gboolean
442 button_release_event (GtkWidget *widget,
443                       GdkEventButton *event,
444                       ModestAttachmentsView *atts_view)
445 {
446         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
447         if (widget == gtk_grab_get_current ()) {
448                 GtkWidget *att_view = NULL;
449
450                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
451                                                    (gint) event->x_root, (gint) event->y_root);
452
453                 if (att_view != NULL) {
454                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
455                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
456                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
457                                       MODEST_ATTACHMENT_VIEW (att_view));
458                 }
459                 priv->rubber_start = NULL;
460                 gtk_grab_remove (widget);
461         }
462         return TRUE;
463 }
464
465 static gboolean
466 motion_notify_event (GtkWidget *widget,
467                      GdkEventMotion *event,
468                      ModestAttachmentsView *atts_view)
469 {
470         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
471         if (gtk_grab_get_current () == widget) {
472                 GtkWidget *att_view = NULL;
473
474                 att_view = get_att_view_at_coords (MODEST_ATTACHMENTS_VIEW (widget), 
475                                                    (gint) event->x_root, (gint) event->y_root);
476
477                 if (att_view != NULL) {
478                         unselect_all (MODEST_ATTACHMENTS_VIEW (widget));
479                         select_range (MODEST_ATTACHMENTS_VIEW (widget), 
480                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start), 
481                                       MODEST_ATTACHMENT_VIEW (att_view));
482                 }
483         }
484         return TRUE;
485 }
486
487 static GList*
488 find_prev_or_next_not_purged (GList *list, gboolean prev, gboolean include_this)
489 {
490         GList *tmp = NULL;
491         gboolean is_valid;
492
493         if (!include_this) {
494                 if (prev) {
495                         tmp = g_list_previous (list);
496                 } else {
497                         tmp = g_list_next (list);
498                 }
499         } else {
500                 tmp = list;
501         }
502
503         if (!tmp)
504                 return NULL;
505
506         do {
507                 ModestAttachmentView *att_view = (ModestAttachmentView *) tmp->data;
508                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
509                 
510                 /* Do not select purged attachments */
511                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
512                         is_valid = TRUE;
513                 } else {
514                         if (prev)
515                                 tmp = g_list_previous (tmp);
516                         else
517                                 tmp = g_list_next (tmp);
518                         is_valid = FALSE;
519                 }
520                 g_object_unref (mime_part);
521         } while (!is_valid && tmp);
522
523         return tmp;
524 }
525
526
527 static gboolean
528 key_press_event (GtkWidget *widget,
529                  GdkEventKey *event,
530                  ModestAttachmentsView *atts_view)
531 {
532         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
533
534         /* If grabbed (for example rubber banding), escape leaves the rubberbanding mode */
535         if (gtk_grab_get_current () == widget) {
536                 if (event->keyval == GDK_Escape) {
537                         set_selected (MODEST_ATTACHMENTS_VIEW (widget),
538                                       MODEST_ATTACHMENT_VIEW (priv->rubber_start));
539                         priv->rubber_start = NULL;
540                         gtk_grab_remove (widget);
541                         return TRUE;
542                 } 
543                 return FALSE;
544         }
545
546         if (event->keyval == GDK_Up) {
547                 ModestAttachmentView *current_sel = NULL;
548                 gboolean move_out = FALSE;
549                 GList * box_children, *new_sel, *first_child;
550
551                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
552                 if (box_children == NULL) {
553                         move_out = TRUE;
554                 } else { 
555                         first_child = box_children;
556                         first_child = find_prev_or_next_not_purged (box_children, FALSE, TRUE);
557                         if (priv->selected != NULL && first_child != NULL) {
558                                 if (priv->selected->data != first_child->data)
559                                         current_sel = (ModestAttachmentView *) priv->selected->data;
560                                 else
561                                         move_out = TRUE;
562                         } else {
563                                 move_out = TRUE;
564                         }
565                 }
566
567                 if (move_out) {
568                         GtkWidget *toplevel = NULL;
569                         /* move cursor outside */
570                         toplevel = gtk_widget_get_toplevel (widget);
571                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
572                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_UP);
573                         unselect_all (atts_view);
574                 } else {
575                         new_sel = g_list_find (box_children, (gpointer) current_sel);
576                         new_sel = find_prev_or_next_not_purged (new_sel, TRUE, FALSE);
577                         /* We assume that we detected properly that
578                            there is a not purge attachment so we don't
579                            need to check NULL */
580                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
581                 }
582                 g_list_free (box_children);
583                 return TRUE;
584         }
585
586         if (event->keyval == GDK_Down) {
587                 ModestAttachmentView *current_sel = NULL;
588                 gboolean move_out = FALSE;
589                 GList * box_children, *new_sel, *last_child = NULL;
590
591                 box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
592
593                 if (box_children == NULL) {
594                         move_out = TRUE;
595                 } else {
596                         last_child = g_list_last (box_children);
597                         last_child = find_prev_or_next_not_purged (last_child, TRUE, TRUE);
598                         if (priv->selected != NULL && last_child != NULL) {
599                                 GList *last_selected = g_list_last (priv->selected);
600                                 if (last_selected->data != last_child->data)
601                                         current_sel = (ModestAttachmentView *) last_selected->data;
602                                 else
603                                         move_out = TRUE;
604                         } else {
605                                 move_out = TRUE;
606                         }
607                 }
608
609                 if (move_out) {
610                         GtkWidget *toplevel = NULL;
611                         /* move cursor outside */
612                         toplevel = gtk_widget_get_toplevel (widget);
613                         if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
614                                 g_signal_emit_by_name (toplevel, "move-focus", GTK_DIR_DOWN);
615                         unselect_all (atts_view);
616                 } else {
617                         new_sel = g_list_find (box_children, (gpointer) current_sel);
618                         new_sel = find_prev_or_next_not_purged (new_sel, FALSE, FALSE);
619                         set_selected (MODEST_ATTACHMENTS_VIEW (atts_view), MODEST_ATTACHMENT_VIEW (new_sel->data));
620                 }
621                 g_list_free (box_children);
622                 return TRUE;
623         }
624
625         if (event->keyval == GDK_BackSpace) {
626                 g_signal_emit (G_OBJECT (widget), signals[DELETE_SIGNAL], 0);
627                 return TRUE;
628         }
629
630         /* Activates selected item */
631         if (g_list_length (priv->selected) == 1) {
632                 ModestAttachmentView *att_view = (ModestAttachmentView *) priv->selected->data;
633                 if ((event->keyval == GDK_Return)) {
634                         TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
635                         if (TNY_IS_MIME_PART (mime_part)) {
636                                 g_signal_emit (G_OBJECT (widget), signals[ACTIVATE_SIGNAL], 0, mime_part);
637                                 g_object_unref (mime_part);
638                         }
639                         return TRUE;
640                 }
641         }
642
643         return FALSE;
644 }
645
646
647 static GtkWidget *
648 get_att_view_at_coords (ModestAttachmentsView *atts_view,
649                         gdouble x, gdouble y)
650 {
651         ModestAttachmentsViewPrivate *priv = NULL;
652         GList *att_view_list, *node;
653         GtkWidget *result = NULL;
654
655         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
656         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
657         
658         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
659                 GtkWidget *att_view = (GtkWidget *) node->data;
660                 gint pos_x, pos_y, w, h, int_x, int_y;
661                 gint widget_x, widget_y;
662
663                 gdk_window_get_origin (att_view->window, &widget_x, &widget_y);
664
665                 pos_x = widget_x;
666                 pos_y = widget_y;
667                 w = att_view->allocation.width;
668                 h = att_view->allocation.height;
669
670                 int_x = (gint) x - GTK_WIDGET (atts_view)->allocation.x;
671                 int_y = (gint) y - GTK_WIDGET (atts_view)->allocation.y;
672
673                 if ((x >= pos_x) && (x <= (pos_x + w)) && (y >= pos_y) && (y <= (pos_y + h))) {
674                         result = att_view;
675                         break;
676                 }
677         }
678
679         g_list_free (att_view_list);
680         return result;
681 }
682
683 static void
684 unselect_all (ModestAttachmentsView *atts_view)
685 {
686         ModestAttachmentsViewPrivate *priv = NULL;
687         GList *att_view_list, *node;
688
689         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
690         att_view_list = gtk_container_get_children (GTK_CONTAINER (priv->box));
691         
692         for (node = att_view_list; node != NULL; node = g_list_next (node)) {
693                 GtkWidget *att_view = (GtkWidget *) node->data;
694
695                 if (GTK_WIDGET_STATE (att_view) == GTK_STATE_SELECTED)
696                         gtk_widget_set_state (att_view, GTK_STATE_NORMAL);
697         }
698
699         g_list_free (priv->selected);
700         priv->selected = NULL;
701
702         g_list_free (att_view_list);
703 }
704
705 static void 
706 set_selected (ModestAttachmentsView *atts_view, ModestAttachmentView *att_view)
707 {
708         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
709         TnyMimePart *part;
710
711         unselect_all (atts_view);
712         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
713         
714         g_list_free (priv->selected);
715         priv->selected = NULL;
716         if (TNY_IS_MIME_PART (part) && !tny_mime_part_is_purged (part)) {
717                 gtk_widget_set_state (GTK_WIDGET (att_view), GTK_STATE_SELECTED);
718                 priv->selected = g_list_append (priv->selected, att_view);
719         }
720         if (part)
721                 g_object_unref (part);
722         
723         own_clipboard (atts_view);
724 }
725
726 static void 
727 select_range (ModestAttachmentsView *atts_view, ModestAttachmentView *att1, ModestAttachmentView *att2)
728 {
729         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
730         GList *children = NULL;
731         GList *node = NULL;
732         gboolean selecting = FALSE;
733         TnyMimePart *part;
734
735         unselect_all (atts_view);
736
737         if (att1 == att2) {
738                 set_selected (atts_view, att1);
739                 return;
740         }
741
742         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
743         g_list_free (priv->selected);
744         priv->selected = NULL;
745
746
747         for (node = children; node != NULL; node = g_list_next (node)) {
748                 if ((node->data == att1) || (node->data == att2)) {
749                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
750                         if (!tny_mime_part_is_purged (part)) {
751                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
752                                 priv->selected = g_list_append (priv->selected, node->data);
753                         }
754                         g_object_unref (part);
755                         selecting = !selecting;
756                 } else if (selecting) {
757                         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (node->data));
758                         if (!tny_mime_part_is_purged (part)) {
759                                 gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
760                                 priv->selected = g_list_append (priv->selected, node->data);
761                         }
762                         g_object_unref (part);
763                 }
764                         
765         }
766         g_list_free (children);
767         
768         own_clipboard (atts_view);
769 }
770
771 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data,
772                            guint info, gpointer userdata)
773 {
774         ModestAttachmentsView *atts_view = (ModestAttachmentsView *) userdata;
775         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
776
777         if ((priv->selected != NULL)&&(priv->selected->next == NULL)) {
778                 if (info == MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX) {
779                         /* MODEST_ATTACHMENT requested. As the content id is not filled in all the case, we'll
780                          * use an internal index. This index is simply the index of the attachment in the vbox */
781                         GList *box_children = NULL;
782                         gint index;
783                         box_children = gtk_container_get_children (GTK_CONTAINER (priv->box));
784                         index = g_list_index (box_children, priv->selected);
785                         if (index >= 0) {
786                                 gchar *index_str = g_strdup_printf("%d", index);
787                                 gtk_selection_data_set_text (selection_data, index_str, -1);
788                                 g_free (index_str);
789                         }
790                 }
791         }
792 }
793
794 TnyList *
795 modest_attachments_view_get_selection (ModestAttachmentsView *atts_view)
796 {
797         ModestAttachmentsViewPrivate *priv;
798         TnyList *selection;
799         GList *node;
800
801         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
802         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
803
804         selection = tny_simple_list_new ();
805         for (node = priv->selected; node != NULL; node = g_list_next (node)) {
806                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
807                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
808                 tny_list_append (selection, (GObject *) part);
809                 g_object_unref (part);
810         }
811         
812         return selection;
813 }
814
815 TnyList *
816 modest_attachments_view_get_attachments (ModestAttachmentsView *atts_view)
817 {
818         ModestAttachmentsViewPrivate *priv;
819         TnyList *att_list;
820         GList *children, *node= NULL;
821
822         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), NULL);
823         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
824
825         att_list = TNY_LIST (tny_simple_list_new ());
826
827         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
828         for (node = children; node != NULL; node = g_list_next (node)) {
829                 GtkWidget *att_view = GTK_WIDGET (node->data);
830                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
831                 tny_list_append (att_list, (GObject *) mime_part);
832                 g_object_unref (mime_part);
833         }
834         g_list_free (children);
835         return att_list;
836
837 }
838
839 void
840 modest_attachments_view_select_all (ModestAttachmentsView *atts_view)
841 {
842         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
843         GList *children = NULL;
844         GList *node = NULL;
845
846         unselect_all (atts_view);
847
848         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
849         g_list_free (priv->selected);
850         priv->selected = NULL;
851
852         for (node = children; node != NULL; node = g_list_next (node)) {
853                 ModestAttachmentView *att_view = (ModestAttachmentView *) node->data;
854                 TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
855
856                 /* Do not select purged attachments */
857                 if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
858                         gtk_widget_set_state (GTK_WIDGET (node->data), GTK_STATE_SELECTED);
859                         priv->selected = g_list_append (priv->selected, node->data);
860                 }
861                 g_object_unref (mime_part);
862         }
863         g_list_free (children);
864
865         own_clipboard (atts_view);
866 }
867
868 gboolean
869 modest_attachments_view_has_attachments (ModestAttachmentsView *atts_view)
870 {
871         ModestAttachmentsViewPrivate *priv;
872         GList *children;
873         gboolean result;
874
875         g_return_val_if_fail (MODEST_IS_ATTACHMENTS_VIEW (atts_view), FALSE);
876         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
877
878         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
879         result = (children != NULL);
880         g_list_free (children);
881
882         return result;
883 }
884
885 void
886 modest_attachments_view_get_sizes (ModestAttachmentsView *attachments_view,
887                                    gint *attachments_count,
888                                    guint64 *attachments_size)
889 {
890         ModestAttachmentsViewPrivate *priv;
891         GList *children, *node;
892
893         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
894         g_return_if_fail (attachments_count != NULL && attachments_size != NULL);
895
896         *attachments_count = 0;
897         *attachments_size = 0;
898
899         priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
900
901         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
902         for (node = children; node != NULL; node = g_list_next (node)) {
903                 GtkWidget *att_view = (GtkWidget *) node->data;
904                 TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
905
906                 if (!tny_mime_part_is_purged (part)) {
907                         guint64 size;
908                         (*attachments_count) ++;
909                         size = modest_attachment_view_get_size (MODEST_ATTACHMENT_VIEW (att_view));
910                         if (size == 0) {
911                                 /* we do a random estimation of the size of an attachment */
912                                 size = 32768;
913                         }
914                         *attachments_size += size;
915                         
916                 }
917                 g_object_unref (part);
918         }
919         g_list_free (children);
920 }
921
922 static void
923 own_clipboard (ModestAttachmentsView *atts_view)
924 {
925         GtkTargetEntry targets[] = {
926                 {MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE, 0, MODEST_ATTACHMENTS_VIEW_CLIPBOARD_TYPE_INDEX},
927         };
928
929         gtk_clipboard_set_with_owner (gtk_widget_get_clipboard (GTK_WIDGET (atts_view), GDK_SELECTION_PRIMARY),
930                                       targets, G_N_ELEMENTS (targets),
931                                       clipboard_get, NULL, G_OBJECT(atts_view));
932                               
933 }
934
935 static gboolean 
936 focus_out_event (GtkWidget *widget, GdkEventFocus *event, ModestAttachmentsView *atts_view)
937 {
938         if (!gtk_widget_is_focus (widget))
939                 unselect_all (atts_view);
940
941         return FALSE;
942 }
943
944 static gboolean 
945 focus (GtkWidget *widget, GtkDirectionType direction, ModestAttachmentsView *atts_view)
946 {
947         ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (atts_view);
948         GList *children = NULL;
949         GtkWidget *toplevel = NULL;
950
951         toplevel = gtk_widget_get_toplevel (widget);
952         if (!gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)))
953                 return FALSE;
954
955         children = gtk_container_get_children (GTK_CONTAINER (priv->box));
956         if (children != NULL) {
957                 set_selected (atts_view, MODEST_ATTACHMENT_VIEW (children->data));
958         }
959         g_list_free (children);
960
961         return FALSE;
962 }