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