Attachment size detection uses disposition size if available.
[modest] / src / widgets / modest-attachment-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 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif /*HAVE_CONFIG_H*/
33
34 #include <glib/gi18n.h>
35
36 #include <string.h>
37 #include <modest-attachment-view.h>
38 #include <modest-platform.h>
39 #include <modest-text-utils.h>
40 #include <modest-tny-mime-part.h>
41 #include <tny-msg.h>
42 #include <modest-mail-operation.h>
43 #include <modest-mail-operation-queue.h>
44 #include <modest-runtime.h>
45 #include <modest-count-stream.h>
46
47 #define GET_SIZE_BUFFER_SIZE 128
48
49 static GObjectClass *parent_class = NULL;
50
51 typedef struct _ModestAttachmentViewPrivate ModestAttachmentViewPrivate;
52
53 struct _ModestAttachmentViewPrivate
54 {
55         TnyMimePart *mime_part;
56
57         GtkWidget *icon;
58         GtkWidget *filename_view;
59         GtkWidget *size_view;
60
61         gboolean detect_size;
62         TnyStream *get_size_stream;
63         guint64 size;
64
65         PangoLayout *layout_full_filename;
66         gboolean is_purged;
67
68 };
69
70 #ifdef MODEST_TOOLKIT_HILDON2
71 #define UNKNOWN_FILE_ICON "filemanager_unknown_file"
72 #else
73 #define UNKNOWN_FILE_ICON "qgn_list_gene_unknown_file"
74 #endif
75
76 #define MODEST_ATTACHMENT_VIEW_GET_PRIVATE(o)   \
77         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ATTACHMENT_VIEW, ModestAttachmentViewPrivate))
78
79 /* TnyMimePartView functions */
80 static TnyMimePart *modest_attachment_view_get_part (TnyMimePartView *self);
81 static TnyMimePart *modest_attachment_view_get_part_default (TnyMimePartView *self);
82 static void modest_attachment_view_set_part (TnyMimePartView *self, TnyMimePart *mime_part);
83 static void modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mime_part);
84 static void modest_attachment_view_clear (TnyMimePartView *self);
85 static void modest_attachment_view_clear_default (TnyMimePartView *self);
86
87 /* Gtk events */
88 static void size_allocate (GtkWidget *widget, GtkAllocation *allocation);
89
90 /* GObject and GInterface management */
91 static void modest_attachment_view_instance_init (GTypeInstance *instance, gpointer g_class);
92 static void modest_attachment_view_finalize (GObject *object);
93 static void modest_attachment_view_class_init (ModestAttachmentViewClass *klass);
94 static void tny_mime_part_view_init (gpointer g, gpointer iface_data);
95
96
97
98 static void update_filename_request (ModestAttachmentView *self);
99
100 static void update_size_label (ModestAttachmentView *self)
101 {
102         ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
103         gchar *size_str;
104         gchar *label_text;
105
106         size_str = modest_text_utils_get_display_size (priv->size);
107         label_text = g_strdup_printf (" (%s)", size_str);
108         g_free (size_str);
109         gtk_label_set_text (GTK_LABEL (priv->size_view), label_text);
110         g_free (label_text);
111 }
112
113 static gboolean
114 idle_get_mime_part_size_cb (gpointer userdata)
115 {
116         ModestAttachmentView *view = (ModestAttachmentView *) userdata;
117         gdk_threads_enter ();
118
119         if (GTK_WIDGET_VISIBLE (view)) {
120                 update_size_label (view);
121         }
122
123         gdk_threads_leave ();
124
125         g_object_unref (view);
126
127         return FALSE;
128 }
129
130 static gpointer
131 get_mime_part_size_thread (gpointer thr_user_data)
132 {
133         ModestAttachmentView *view =  (ModestAttachmentView *) thr_user_data;
134         ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (view);
135         gsize total = 0;
136         gssize result = 0;
137
138         result = tny_mime_part_decode_to_stream (priv->mime_part, priv->get_size_stream, NULL);
139         total = modest_count_stream_get_count(MODEST_COUNT_STREAM (priv->get_size_stream));
140         if (total == 0) {
141                 modest_count_stream_reset_count(MODEST_COUNT_STREAM (priv->get_size_stream));
142                 result = tny_mime_part_write_to_stream (priv->mime_part, priv->get_size_stream, NULL);
143                 total = modest_count_stream_get_count(MODEST_COUNT_STREAM (priv->get_size_stream));
144         }
145         
146         /* if there was an error, don't set the size (this is pretty uncommon) */
147         if (result < 0) {
148                 g_warning ("%s: error while writing mime part to stream\n", __FUNCTION__);
149         } else {
150                 priv->size = (guint64)total;
151                 g_idle_add (idle_get_mime_part_size_cb, g_object_ref (view));
152         }
153         g_object_unref (view);
154
155         return NULL;
156 }
157
158 void
159 modest_attachment_view_set_detect_size (ModestAttachmentView *self, gboolean detect_size)
160 {
161         ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
162
163         priv->detect_size = detect_size;
164         
165 }
166
167 void
168 modest_attachment_view_set_size (ModestAttachmentView *self, guint64 size)
169 {
170         ModestAttachmentViewPrivate *priv;
171
172         g_return_if_fail (MODEST_IS_ATTACHMENT_VIEW (self));
173         priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
174
175         if (!priv->detect_size) {
176                 priv->size = size;
177                 update_size_label (self);
178         } else {
179                 g_assert ("Shouldn't set the size of the attachment view if detect size is enabled");
180         }
181 }
182
183 guint64
184 modest_attachment_view_get_size (ModestAttachmentView *self)
185 {
186         ModestAttachmentViewPrivate *priv;
187
188         g_return_val_if_fail (MODEST_IS_ATTACHMENT_VIEW (self), 0);
189         priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
190
191         return priv->size;
192 }
193
194 static TnyMimePart *
195 modest_attachment_view_get_part (TnyMimePartView *self)
196 {
197         return MODEST_ATTACHMENT_VIEW_GET_CLASS (self)->get_part_func (self);
198 }
199
200 static TnyMimePart *
201 modest_attachment_view_get_part_default (TnyMimePartView *self)
202 {
203         ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
204
205         if (priv->mime_part)
206                 return TNY_MIME_PART (g_object_ref (priv->mime_part));
207         else
208                 return NULL;
209 }
210
211 static void
212 update_filename_request (ModestAttachmentView *self)
213 {
214         ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
215         /* gint width, height; */
216         
217         pango_layout_set_text (PANGO_LAYOUT (priv->layout_full_filename), 
218                                gtk_label_get_text (GTK_LABEL (priv->filename_view)), -1);
219
220
221 }
222
223 static void
224 modest_attachment_view_set_part (TnyMimePartView *self, TnyMimePart *mime_part)
225 {
226         MODEST_ATTACHMENT_VIEW_GET_CLASS (self)->set_part_func (self, mime_part);
227         return;
228 }
229
230
231 static void
232 modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mime_part)
233 {
234         ModestAttachmentViewPrivate *priv = NULL;
235         gchar *filename = NULL;
236         gchar *file_icon_name = NULL;
237         gboolean show_size = FALSE;
238         
239         g_return_if_fail (TNY_IS_MIME_PART_VIEW (self));
240         g_return_if_fail (TNY_IS_MIME_PART (mime_part));
241         priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
242
243         if (priv->mime_part != NULL) {
244                 g_object_unref (priv->mime_part);
245         }
246
247         priv->mime_part = g_object_ref (mime_part);
248
249         priv->size = 0;
250         priv->is_purged = tny_mime_part_is_purged (mime_part);
251
252         if (TNY_IS_MSG (mime_part)) {
253                 TnyHeader *header = tny_msg_get_header (TNY_MSG (mime_part));
254                 if (TNY_IS_HEADER (header)) {
255                         filename = g_strdup (tny_mime_part_get_filename (mime_part));
256                         if (!filename)
257                                 filename = tny_header_dup_subject (header);
258                         if (filename == NULL || filename[0] == '\0') {
259                                 if (filename)
260                                         g_free (filename);
261                                 filename = g_strdup (_("mail_va_no_subject"));
262                         }
263                         if (priv->is_purged) {
264                                 file_icon_name = modest_platform_get_file_icon_name (NULL, NULL, NULL);
265                         } else {
266                                 gchar *header_content_type;
267                                 header_content_type = modest_tny_mime_part_get_content_type (mime_part);
268                                 if ((g_str_has_prefix (header_content_type, "message/rfc822") ||
269                                      g_str_has_prefix (header_content_type, "multipart/") ||
270                                      g_str_has_prefix (header_content_type, "text/"))) {
271                                         file_icon_name = 
272                                                 modest_platform_get_file_icon_name (
273                                                         NULL, tny_mime_part_get_content_type (mime_part), NULL);
274                                 } else {
275                                         file_icon_name = 
276                                                 modest_platform_get_file_icon_name (
277                                                         NULL, header_content_type, NULL);
278                                 }
279                                 g_free (header_content_type);
280                         }
281                         g_object_unref (header);
282                 }
283         } else {
284                 filename = g_strdup (tny_mime_part_get_filename (mime_part));
285                 if (priv->is_purged) {
286                         file_icon_name = modest_platform_get_file_icon_name (NULL, NULL, NULL);
287                 } else {
288                         file_icon_name = modest_platform_get_file_icon_name (
289                                 filename, modest_tny_mime_part_get_content_type (mime_part), NULL);
290                         show_size = TRUE;
291                 }
292         }
293
294         if (file_icon_name) {
295                 gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon), file_icon_name, GTK_ICON_SIZE_MENU);
296                 g_free (file_icon_name);
297         } else {
298                 gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon), UNKNOWN_FILE_ICON, GTK_ICON_SIZE_MENU);
299         }
300
301         if (priv->is_purged) {
302                 gchar * label_str = g_markup_printf_escaped(
303                         "<span style='italic' foreground='grey'>%s</span>",
304                         filename);
305                 gtk_label_set_markup (GTK_LABEL (priv->filename_view), label_str);
306                 g_free (label_str);
307         } else {
308                 gtk_label_set_text (GTK_LABEL (priv->filename_view), filename);
309         }
310         g_free (filename);
311         update_filename_request (MODEST_ATTACHMENT_VIEW (self));
312
313         gtk_label_set_text (GTK_LABEL (priv->size_view), "");
314
315         if (show_size && priv->detect_size) {
316                 gchar *disposition;
317
318                 disposition = modest_tny_mime_part_get_header_value (mime_part, "Content-Disposition");
319                 if (disposition) {
320                         const gchar *size_tmp;
321                         size_tmp = strstr (disposition, "size=");
322                         if (size_tmp) size_tmp += strlen("size=");
323                         if (size_tmp) {
324                                 gchar *disposition_value;
325                                 const gchar *size_end;
326                                 size_end = strstr (size_tmp, ";");
327                                 if (size_end == NULL) {
328                                         disposition_value = g_strdup (size_tmp);
329                                 } else {
330                                         disposition_value = g_strndup (size_tmp, size_end - size_tmp);
331                                 }
332                                 if (disposition_value && disposition_value[0] != '\0') {
333                                         priv->size = atoll (disposition_value);
334                                         if (priv->size != 0) {
335                                                 show_size = FALSE;
336                                                 update_size_label (MODEST_ATTACHMENT_VIEW (self));
337                                         }
338                                 }
339                                 g_free (disposition_value);
340                         }
341                         
342                         g_free (disposition);
343                 }
344         }
345
346         if (show_size && priv->detect_size) {
347                 g_object_ref (self);
348                 if (!priv->get_size_stream)
349                         priv->get_size_stream = modest_count_stream_new ();
350                 g_thread_create (get_mime_part_size_thread, self, FALSE, NULL);
351         }
352
353         gtk_widget_queue_draw (GTK_WIDGET (self));
354 }
355
356 static void
357 modest_attachment_view_clear (TnyMimePartView *self)
358 {
359         MODEST_ATTACHMENT_VIEW_GET_CLASS (self)->clear_func (self);
360         return;
361 }
362
363 static void
364 modest_attachment_view_clear_default (TnyMimePartView *self)
365 {
366         ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
367
368         if (priv->mime_part != NULL) {
369                 g_object_unref (priv->mime_part);
370                 priv->mime_part = NULL;
371         }
372
373         if (priv->get_size_stream)
374                 modest_count_stream_reset_count(MODEST_COUNT_STREAM (priv->get_size_stream));
375
376         priv->size = 0;
377
378         gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon), 
379                                       UNKNOWN_FILE_ICON,
380                                       GTK_ICON_SIZE_MENU);
381         gtk_label_set_text (GTK_LABEL (priv->filename_view), "");
382         update_filename_request (MODEST_ATTACHMENT_VIEW(self));
383         gtk_label_set_text (GTK_LABEL (priv->size_view), " ");
384
385         gtk_widget_queue_draw (GTK_WIDGET (self));
386 }
387
388
389 /**
390  * modest_attachment_view_new:
391  * @mime_part: a #TnyMimePart
392  *
393  * Constructor for attachment view widget.
394  *
395  * Return value: a new #ModestAttachmentView instance implemented for Gtk+
396  **/
397 GtkWidget*
398 modest_attachment_view_new (TnyMimePart *mime_part, gboolean detect_size)
399 {
400         ModestAttachmentView *self = g_object_new (MODEST_TYPE_ATTACHMENT_VIEW, 
401                                                    NULL);
402
403         modest_attachment_view_set_detect_size (self, detect_size);
404
405         modest_attachment_view_set_part (TNY_MIME_PART_VIEW (self), mime_part);
406
407         return GTK_WIDGET (self);
408 }
409
410 static void
411 modest_attachment_view_instance_init (GTypeInstance *instance, gpointer g_class)
412 {
413         ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (instance);
414         PangoContext *context;
415         GtkWidget *box = NULL;
416
417 #ifdef MODEST_TOOLKIT_HILDON2
418         PangoAttrList *attr_list;
419         attr_list = pango_attr_list_new ();
420         pango_attr_list_insert (attr_list, pango_attr_underline_new (PANGO_UNDERLINE_SINGLE));
421 #endif
422
423         priv->mime_part = NULL;
424         priv->icon = gtk_image_new ();
425         priv->filename_view = gtk_label_new ("");
426         gtk_label_set_line_wrap (GTK_LABEL (priv->filename_view), FALSE);
427         gtk_label_set_ellipsize (GTK_LABEL (priv->filename_view), PANGO_ELLIPSIZE_END);
428         gtk_label_set_single_line_mode (GTK_LABEL (priv->filename_view), TRUE);
429         gtk_label_set_selectable (GTK_LABEL (priv->filename_view), FALSE);
430         priv->size_view = gtk_label_new (" ");
431         gtk_label_set_line_wrap (GTK_LABEL (priv->size_view), FALSE);
432         gtk_label_set_selectable (GTK_LABEL (priv->size_view), FALSE);
433         gtk_misc_set_alignment (GTK_MISC (priv->size_view), 0.0, 0.5);
434         gtk_misc_set_alignment (GTK_MISC (priv->filename_view), 0.0, 0.5);
435
436 #ifdef MODEST_TOOLKIT_HILDON2
437         gtk_label_set_attributes (GTK_LABEL (priv->filename_view), attr_list);
438         gtk_label_set_attributes (GTK_LABEL (priv->size_view), attr_list);
439 #endif
440
441         priv->get_size_stream = NULL;
442         priv->size = 0;
443         priv->detect_size = TRUE;
444
445         box = gtk_hbox_new (FALSE, 0);
446         gtk_box_pack_start (GTK_BOX (box), priv->icon, FALSE, FALSE, 0);
447         gtk_box_pack_start (GTK_BOX (box), priv->filename_view, TRUE, TRUE, 0);
448         gtk_box_pack_start (GTK_BOX (box), priv->size_view, FALSE, FALSE, 0);
449         gtk_container_add (GTK_CONTAINER (instance), box);
450
451 /*      gtk_widget_get_style */
452 /*      gtk_widget_modify_bg (instance, GTK_STATE_SELECTED, selection_color); */
453
454         context = gtk_widget_get_pango_context (priv->filename_view);
455         priv->layout_full_filename = pango_layout_new (context);
456         
457         pango_layout_set_ellipsize (priv->layout_full_filename, PANGO_ELLIPSIZE_NONE);
458
459         gtk_event_box_set_above_child (GTK_EVENT_BOX (instance), FALSE);
460         gtk_event_box_set_visible_window (GTK_EVENT_BOX (instance), TRUE);
461         gtk_widget_set_events (GTK_WIDGET (instance), 0);
462
463 #ifdef MODEST_TOOLKIT_HILDON2
464         pango_attr_list_unref (attr_list);
465 #endif
466
467         GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET (instance), GTK_CAN_FOCUS);
468
469         return;
470 }
471
472 static void
473 modest_attachment_view_finalize (GObject *object)
474 {
475         ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (object);
476
477         if (priv->get_size_stream != NULL) {
478                 g_object_unref (priv->get_size_stream);
479                 priv->get_size_stream = NULL;
480         }
481
482         if (G_LIKELY (priv->mime_part)) {
483                 g_object_unref (G_OBJECT (priv->mime_part));
484                 priv->mime_part = NULL;
485         }
486
487         (*parent_class->finalize) (object);
488
489         return;
490 }
491
492 static void
493 size_allocate (GtkWidget *widget, GtkAllocation *allocation)
494 {
495         ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (widget);
496         gint width, width_diff;
497
498         GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
499         pango_layout_set_font_description (priv->layout_full_filename, pango_context_get_font_description(pango_layout_get_context (gtk_label_get_layout (GTK_LABEL (priv->filename_view)))));
500
501         pango_layout_get_pixel_size (priv->layout_full_filename, &width, NULL);
502         width_diff = priv->filename_view->allocation.width - width;
503         if (width_diff > 0) {
504                 GtkAllocation filename_alloc, filesize_alloc;
505                 filename_alloc = priv->filename_view->allocation;
506                 filesize_alloc = priv->size_view->allocation;
507                 filename_alloc.width -= width_diff;
508                 filesize_alloc.width += width_diff;
509                 filesize_alloc.x -= width_diff;
510                 gtk_widget_size_allocate (priv->filename_view, &filename_alloc);
511                 gtk_widget_size_allocate (priv->size_view, &filesize_alloc);
512         }
513         
514 }
515
516
517 static void 
518 modest_attachment_view_class_init (ModestAttachmentViewClass *klass)
519 {
520         GObjectClass *object_class;
521         GtkWidgetClass *widget_class;
522
523         parent_class = g_type_class_peek_parent (klass);
524         object_class = (GObjectClass*) klass;
525         widget_class = GTK_WIDGET_CLASS (klass);
526
527         object_class->finalize = modest_attachment_view_finalize;
528
529         klass->get_part_func = modest_attachment_view_get_part_default;
530         klass->set_part_func = modest_attachment_view_set_part_default;
531         klass->clear_func = modest_attachment_view_clear_default;
532
533         widget_class->size_allocate = size_allocate;
534
535         g_type_class_add_private (object_class, sizeof (ModestAttachmentViewPrivate));
536
537         return;
538 }
539
540 static void
541 tny_mime_part_view_init (gpointer g, gpointer iface_data)
542 {
543         TnyMimePartViewIface *klass = (TnyMimePartViewIface *)g;
544
545         klass->get_part = modest_attachment_view_get_part;
546         klass->set_part = modest_attachment_view_set_part;
547         klass->clear = modest_attachment_view_clear;
548
549         return;
550 }
551
552 GType 
553 modest_attachment_view_get_type (void)
554 {
555         static GType type = 0;
556
557         if (G_UNLIKELY(type == 0))
558         {
559                 static const GTypeInfo info = 
560                 {
561                   sizeof (ModestAttachmentViewClass),
562                   NULL,   /* base_init */
563                   NULL,   /* base_finalize */
564                   (GClassInitFunc) modest_attachment_view_class_init,   /* class_init */
565                   NULL,   /* class_finalize */
566                   NULL,   /* class_data */
567                   sizeof (ModestAttachmentView),
568                   0,      /* n_preallocs */
569                   modest_attachment_view_instance_init    /* instance_init */
570                 };
571
572                 static const GInterfaceInfo tny_mime_part_view_info =
573                 {
574                         (GInterfaceInitFunc) tny_mime_part_view_init, /* interface_init */
575                         NULL,        /* interface_finalize */
576                         NULL         /* interface_data */
577                 };
578
579                 type = g_type_register_static (GTK_TYPE_EVENT_BOX,
580                         "ModestAttachmentView",
581                         &info, 0);
582
583                 g_type_add_interface_static (type, TNY_TYPE_MIME_PART_VIEW,
584                                              &tny_mime_part_view_info);
585
586         }
587
588         return type;
589 }