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