1 /* Copyright (c) 2007, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
32 #endif /*HAVE_CONFIG_H*/
34 #include <glib/gi18n.h>
37 #include <modest-attachment-view.h>
38 #include <modest-platform.h>
39 #include <modest-text-utils.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>
47 #define GET_SIZE_BUFFER_SIZE 128
49 static GObjectClass *parent_class = NULL;
51 typedef struct _ModestAttachmentViewPrivate ModestAttachmentViewPrivate;
53 struct _ModestAttachmentViewPrivate
55 TnyMimePart *mime_part;
58 GtkWidget *filename_view;
61 guint get_size_idle_id;
62 TnyStream *get_size_stream;
65 PangoLayout *layout_full_filename;
70 #define UNKNOWN_FILE_ICON "qgn_list_gene_unknown_file"
72 #define MODEST_ATTACHMENT_VIEW_GET_PRIVATE(o) \
73 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ATTACHMENT_VIEW, ModestAttachmentViewPrivate))
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);
84 static void size_allocate (GtkWidget *widget, GtkAllocation *allocation);
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);
94 static void update_filename_request (ModestAttachmentView *self);
98 idle_get_mime_part_size_cb (gpointer userdata)
100 ModestAttachmentView *view = (ModestAttachmentView *) userdata;
101 ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (view);
104 gdk_threads_enter ();
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);
110 gtk_label_set_text (GTK_LABEL (priv->size_view), label_text);
114 gdk_threads_leave ();
116 g_object_unref (view);
122 get_mime_part_size_thread (gpointer thr_user_data)
124 ModestAttachmentView *view = (ModestAttachmentView *) thr_user_data;
125 ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (view);
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));
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));
137 priv->size = (guint64)total;
139 g_idle_add (idle_get_mime_part_size_cb, g_object_ref (view));
141 g_object_unref (stream);
142 g_object_unref (view);
148 modest_attachment_view_get_part (TnyMimePartView *self)
150 return MODEST_ATTACHMENT_VIEW_GET_CLASS (self)->get_part_func (self);
154 modest_attachment_view_get_part_default (TnyMimePartView *self)
156 ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
159 return TNY_MIME_PART (g_object_ref (priv->mime_part));
165 update_filename_request (ModestAttachmentView *self)
167 ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
168 /* gint width, height; */
170 pango_layout_set_text (PANGO_LAYOUT (priv->layout_full_filename),
171 gtk_label_get_text (GTK_LABEL (priv->filename_view)), -1);
177 modest_attachment_view_set_part (TnyMimePartView *self, TnyMimePart *mime_part)
179 MODEST_ATTACHMENT_VIEW_GET_CLASS (self)->set_part_func (self, mime_part);
185 modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mime_part)
187 ModestAttachmentViewPrivate *priv = NULL;
188 gchar *filename = NULL;
189 gchar *file_icon_name = NULL;
190 gboolean show_size = FALSE;
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);
196 if (priv->mime_part != NULL) {
197 g_object_unref (priv->mime_part);
200 priv->mime_part = mime_part;
203 priv->is_purged = tny_mime_part_is_purged (mime_part);
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"));
212 file_icon_name = modest_platform_get_file_icon_name (NULL, NULL, NULL);
215 modest_platform_get_file_icon_name (
216 NULL, tny_mime_part_get_content_type (mime_part), NULL);
217 g_object_unref (header);
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);
224 file_icon_name = modest_platform_get_file_icon_name (
225 filename, tny_mime_part_get_content_type (mime_part), NULL);
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);
234 gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon), UNKNOWN_FILE_ICON, GTK_ICON_SIZE_MENU);
237 if (priv->is_purged) {
238 gchar * label_str = g_markup_printf_escaped(
239 "<span style='italic' foreground='grey'>%s</span>",
241 gtk_label_set_markup (GTK_LABEL (priv->filename_view), label_str);
244 gtk_label_set_text (GTK_LABEL (priv->filename_view), filename);
247 update_filename_request (MODEST_ATTACHMENT_VIEW (self));
249 gtk_label_set_text (GTK_LABEL (priv->size_view), "");
252 tny_camel_mem_stream_get_type ();
254 g_thread_create (get_mime_part_size_thread, self, FALSE, NULL);
257 gtk_widget_queue_draw (GTK_WIDGET (self));
261 modest_attachment_view_clear (TnyMimePartView *self)
263 MODEST_ATTACHMENT_VIEW_GET_CLASS (self)->clear_func (self);
268 modest_attachment_view_clear_default (TnyMimePartView *self)
270 ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
272 if (priv->mime_part != NULL) {
273 g_object_unref (priv->mime_part);
274 priv->mime_part = NULL;
277 if (priv->get_size_idle_id != 0) {
278 g_source_remove (priv->get_size_idle_id);
279 priv->get_size_idle_id = 0;
282 if (priv->get_size_stream != NULL) {
283 g_object_unref (priv->get_size_stream);
284 priv->get_size_stream = NULL;
289 gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
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), " ");
296 gtk_widget_queue_draw (GTK_WIDGET (self));
301 * modest_attachment_view_new:
302 * @mime_part: a #TnyMimePart
304 * Constructor for attachment view widget.
306 * Return value: a new #ModestAttachmentView instance implemented for Gtk+
309 modest_attachment_view_new (TnyMimePart *mime_part)
311 ModestAttachmentView *self = g_object_new (MODEST_TYPE_ATTACHMENT_VIEW,
314 modest_attachment_view_set_part (TNY_MIME_PART_VIEW (self), mime_part);
316 return GTK_WIDGET (self);
320 modest_attachment_view_instance_init (GTypeInstance *instance, gpointer g_class)
322 ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (instance);
323 PangoContext *context;
324 GtkWidget *box = NULL;
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);
339 priv->get_size_idle_id = 0;
340 priv->get_size_stream = NULL;
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);
349 /* gtk_widget_get_style */
350 /* gtk_widget_modify_bg (instance, GTK_STATE_SELECTED, selection_color); */
352 context = gtk_widget_get_pango_context (priv->filename_view);
353 priv->layout_full_filename = pango_layout_new (context);
355 pango_layout_set_ellipsize (priv->layout_full_filename, PANGO_ELLIPSIZE_NONE);
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);
361 GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET (instance), GTK_CAN_FOCUS);
367 modest_attachment_view_finalize (GObject *object)
369 ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (object);
371 if (priv->get_size_idle_id) {
372 g_source_remove (priv->get_size_idle_id);
373 priv->get_size_idle_id = 0;
376 if (priv->get_size_stream != NULL) {
377 g_object_unref (priv->get_size_stream);
378 priv->get_size_stream = NULL;
381 if (G_LIKELY (priv->mime_part)) {
382 g_object_unref (G_OBJECT (priv->mime_part));
383 priv->mime_part = NULL;
386 (*parent_class->finalize) (object);
392 size_allocate (GtkWidget *widget, GtkAllocation *allocation)
394 ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (widget);
395 gint width, width_diff;
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)))));
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);
417 modest_attachment_view_class_init (ModestAttachmentViewClass *klass)
419 GObjectClass *object_class;
420 GtkWidgetClass *widget_class;
422 parent_class = g_type_class_peek_parent (klass);
423 object_class = (GObjectClass*) klass;
424 widget_class = GTK_WIDGET_CLASS (klass);
426 object_class->finalize = modest_attachment_view_finalize;
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;
432 widget_class->size_allocate = size_allocate;
434 g_type_class_add_private (object_class, sizeof (ModestAttachmentViewPrivate));
440 tny_mime_part_view_init (gpointer g, gpointer iface_data)
442 TnyMimePartViewIface *klass = (TnyMimePartViewIface *)g;
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;
452 modest_attachment_view_get_type (void)
454 static GType type = 0;
456 if (G_UNLIKELY(type == 0))
458 static const GTypeInfo info =
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),
468 modest_attachment_view_instance_init /* instance_init */
471 static const GInterfaceInfo tny_mime_part_view_info =
473 (GInterfaceInitFunc) tny_mime_part_view_init, /* interface_init */
474 NULL, /* interface_finalize */
475 NULL /* interface_data */
478 type = g_type_register_static (GTK_TYPE_EVENT_BOX,
479 "ModestAttachmentView",
482 g_type_add_interface_static (type, TNY_TYPE_MIME_PART_VIEW,
483 &tny_mime_part_view_info);