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