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