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