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>
46 static GObjectClass *parent_class = NULL;
48 typedef struct _ModestAttachmentViewPriv ModestAttachmentViewPriv;
50 struct _ModestAttachmentViewPriv
52 TnyMimePart *mime_part;
55 GtkWidget *filename_view;
58 guint get_size_idle_id;
59 TnyStream *get_size_stream;
62 PangoLayout *layout_full_filename;
67 #define UNKNOWN_FILE_ICON "qgn_list_gene_unknown_file"
69 #define MODEST_ATTACHMENT_VIEW_GET_PRIVATE(o) \
70 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ATTACHMENT_VIEW, ModestAttachmentViewPriv))
72 /* TnyMimePartView functions */
73 static TnyMimePart *modest_attachment_view_get_part (TnyMimePartView *self);
74 static TnyMimePart *modest_attachment_view_get_part_default (TnyMimePartView *self);
75 static void modest_attachment_view_set_part (TnyMimePartView *self, TnyMimePart *mime_part);
76 static void modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mime_part);
77 static void modest_attachment_view_clear (TnyMimePartView *self);
78 static void modest_attachment_view_clear_default (TnyMimePartView *self);
81 static void size_allocate (GtkWidget *widget, GtkAllocation *allocation);
83 /* GObject and GInterface management */
84 static void modest_attachment_view_instance_init (GTypeInstance *instance, gpointer g_class);
85 static void modest_attachment_view_finalize (GObject *object);
86 static void modest_attachment_view_class_init (ModestAttachmentViewClass *klass);
87 static void tny_mime_part_view_init (gpointer g, gpointer iface_data);
91 static void update_filename_request (ModestAttachmentView *self);
93 static void get_mime_part_size_cb (ModestMailOperation *mail_op,
99 get_mime_part_size_cb (ModestMailOperation *mail_op,
103 ModestAttachmentView *att_view = MODEST_ATTACHMENT_VIEW (userdata);
104 ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (att_view);
108 if (GTK_WIDGET_VISIBLE (att_view)) {
109 size_str = modest_text_utils_get_display_size (size);
110 label_text = g_strdup_printf (" (%s)", size_str);
112 gtk_label_set_text (GTK_LABEL (priv->size_view), label_text);
120 modest_attachment_view_get_part (TnyMimePartView *self)
122 return MODEST_ATTACHMENT_VIEW_GET_CLASS (self)->get_part_func (self);
126 modest_attachment_view_get_part_default (TnyMimePartView *self)
128 ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
131 return TNY_MIME_PART (g_object_ref (priv->mime_part));
137 update_filename_request (ModestAttachmentView *self)
139 ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
140 /* gint width, height; */
142 pango_layout_set_text (PANGO_LAYOUT (priv->layout_full_filename),
143 gtk_label_get_text (GTK_LABEL (priv->filename_view)), -1);
149 modest_attachment_view_set_part (TnyMimePartView *self, TnyMimePart *mime_part)
151 MODEST_ATTACHMENT_VIEW_GET_CLASS (self)->set_part_func (self, mime_part);
157 modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mime_part)
159 ModestAttachmentViewPriv *priv = NULL;
160 gchar *filename = NULL;
161 gchar *file_icon_name = NULL;
162 gboolean show_size = FALSE;
164 g_return_if_fail (TNY_IS_MIME_PART_VIEW (self));
165 g_return_if_fail (TNY_IS_MIME_PART (mime_part));
166 priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
168 if (priv->mime_part != NULL) {
169 g_object_unref (priv->mime_part);
172 priv->mime_part = mime_part;
175 priv->is_purged = tny_mime_part_is_purged (mime_part);
177 if (priv->is_purged) {
178 filename = g_strdup( tny_mime_part_get_filename (mime_part));
179 if (filename == NULL)
180 filename = g_strdup ("");
181 file_icon_name = modest_platform_get_file_icon_name (NULL, NULL, NULL);
182 } else if (TNY_IS_MSG (mime_part)) {
183 TnyHeader *header = tny_msg_get_header (TNY_MSG (mime_part));
184 if (TNY_IS_HEADER (header)) {
185 filename = g_strdup (tny_header_get_subject (header));
186 if (filename == NULL)
187 filename = g_strdup (_("mail_va_no_subject"));
188 file_icon_name = modest_platform_get_file_icon_name (NULL, tny_mime_part_get_content_type (mime_part), NULL);
189 g_object_unref (header);
192 filename = g_strdup (tny_mime_part_get_filename (mime_part));
193 file_icon_name = modest_platform_get_file_icon_name (filename,
194 tny_mime_part_get_content_type (mime_part),
199 if (file_icon_name) {
200 gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon), file_icon_name, GTK_ICON_SIZE_MENU);
201 g_free (file_icon_name);
203 gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon), UNKNOWN_FILE_ICON, GTK_ICON_SIZE_MENU);
206 if (priv->is_purged) {
207 gchar * label_str = g_markup_printf_escaped(
208 "<span style='italic' foreground='grey'>%s</span>",
210 gtk_label_set_markup (GTK_LABEL (priv->filename_view), label_str);
213 gtk_label_set_text (GTK_LABEL (priv->filename_view), filename);
216 update_filename_request (MODEST_ATTACHMENT_VIEW (self));
218 gtk_label_set_text (GTK_LABEL (priv->size_view), "");
221 ModestMailOperation *mail_op =
222 modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_INFO, G_OBJECT (self));
223 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
224 modest_mail_operation_get_mime_part_size (mail_op, mime_part, get_mime_part_size_cb,
226 g_object_unref (mail_op);
229 gtk_widget_queue_draw (GTK_WIDGET (self));
233 modest_attachment_view_clear (TnyMimePartView *self)
235 MODEST_ATTACHMENT_VIEW_GET_CLASS (self)->clear_func (self);
240 modest_attachment_view_clear_default (TnyMimePartView *self)
242 ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
244 if (priv->mime_part != NULL) {
245 g_object_unref (priv->mime_part);
246 priv->mime_part = NULL;
249 if (priv->get_size_idle_id != 0) {
250 g_source_remove (priv->get_size_idle_id);
251 priv->get_size_idle_id = 0;
254 if (priv->get_size_stream != NULL) {
255 g_object_unref (priv->get_size_stream);
256 priv->get_size_stream = NULL;
261 gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
264 gtk_label_set_text (GTK_LABEL (priv->filename_view), "");
265 update_filename_request (MODEST_ATTACHMENT_VIEW(self));
266 gtk_label_set_text (GTK_LABEL (priv->size_view), " ");
268 gtk_widget_queue_draw (GTK_WIDGET (self));
273 * modest_attachment_view_new:
274 * @mime_part: a #TnyMimePart
276 * Constructor for attachment view widget.
278 * Return value: a new #ModestAttachmentView instance implemented for Gtk+
281 modest_attachment_view_new (TnyMimePart *mime_part)
283 ModestAttachmentView *self = g_object_new (MODEST_TYPE_ATTACHMENT_VIEW,
286 modest_attachment_view_set_part (TNY_MIME_PART_VIEW (self), mime_part);
288 return GTK_WIDGET (self);
292 modest_attachment_view_instance_init (GTypeInstance *instance, gpointer g_class)
294 ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (instance);
295 PangoContext *context;
296 GtkWidget *box = NULL;
298 priv->mime_part = NULL;
299 priv->icon = gtk_image_new ();
300 priv->filename_view = gtk_label_new ("");
301 gtk_label_set_line_wrap (GTK_LABEL (priv->filename_view), FALSE);
302 gtk_label_set_ellipsize (GTK_LABEL (priv->filename_view), PANGO_ELLIPSIZE_END);
303 gtk_label_set_single_line_mode (GTK_LABEL (priv->filename_view), TRUE);
304 gtk_label_set_selectable (GTK_LABEL (priv->filename_view), FALSE);
305 priv->size_view = gtk_label_new (" ");
306 gtk_label_set_line_wrap (GTK_LABEL (priv->size_view), FALSE);
307 gtk_label_set_selectable (GTK_LABEL (priv->size_view), FALSE);
308 gtk_misc_set_alignment (GTK_MISC (priv->size_view), 0.0, 0.5);
309 gtk_misc_set_alignment (GTK_MISC (priv->filename_view), 0.0, 0.5);
311 priv->get_size_idle_id = 0;
312 priv->get_size_stream = NULL;
315 box = gtk_hbox_new (FALSE, 0);
316 gtk_box_pack_start (GTK_BOX (box), priv->icon, FALSE, FALSE, 0);
317 gtk_box_pack_start (GTK_BOX (box), priv->filename_view, TRUE, TRUE, 0);
318 gtk_box_pack_start (GTK_BOX (box), priv->size_view, FALSE, FALSE, 0);
319 gtk_container_add (GTK_CONTAINER (instance), box);
321 /* gtk_widget_get_style */
322 /* gtk_widget_modify_bg (instance, GTK_STATE_SELECTED, selection_color); */
324 context = gtk_widget_get_pango_context (priv->filename_view);
325 priv->layout_full_filename = pango_layout_new (context);
327 pango_layout_set_ellipsize (priv->layout_full_filename, PANGO_ELLIPSIZE_NONE);
329 gtk_event_box_set_above_child (GTK_EVENT_BOX (instance), FALSE);
330 gtk_event_box_set_visible_window (GTK_EVENT_BOX (instance), TRUE);
331 gtk_widget_set_events (GTK_WIDGET (instance), 0);
333 GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET (instance), GTK_CAN_FOCUS);
339 modest_attachment_view_finalize (GObject *object)
341 ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (object);
343 if (priv->get_size_idle_id) {
344 g_source_remove (priv->get_size_idle_id);
345 priv->get_size_idle_id = 0;
348 if (priv->get_size_stream != NULL) {
349 g_object_unref (priv->get_size_stream);
350 priv->get_size_stream = NULL;
353 if (G_LIKELY (priv->mime_part)) {
354 g_object_unref (G_OBJECT (priv->mime_part));
355 priv->mime_part = NULL;
358 (*parent_class->finalize) (object);
364 size_allocate (GtkWidget *widget, GtkAllocation *allocation)
366 ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (widget);
367 gint width, width_diff;
369 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
370 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)))));
372 pango_layout_get_pixel_size (priv->layout_full_filename, &width, NULL);
373 width_diff = priv->filename_view->allocation.width - width;
374 if (width_diff > 0) {
375 GtkAllocation filename_alloc, filesize_alloc;
376 filename_alloc = priv->filename_view->allocation;
377 filesize_alloc = priv->size_view->allocation;
378 filename_alloc.width -= width_diff;
379 filesize_alloc.width += width_diff;
380 filesize_alloc.x -= width_diff;
381 gtk_widget_size_allocate (priv->filename_view, &filename_alloc);
382 gtk_widget_size_allocate (priv->size_view, &filesize_alloc);
389 modest_attachment_view_class_init (ModestAttachmentViewClass *klass)
391 GObjectClass *object_class;
392 GtkWidgetClass *widget_class;
394 parent_class = g_type_class_peek_parent (klass);
395 object_class = (GObjectClass*) klass;
396 widget_class = GTK_WIDGET_CLASS (klass);
398 object_class->finalize = modest_attachment_view_finalize;
400 klass->get_part_func = modest_attachment_view_get_part_default;
401 klass->set_part_func = modest_attachment_view_set_part_default;
402 klass->clear_func = modest_attachment_view_clear_default;
404 widget_class->size_allocate = size_allocate;
406 g_type_class_add_private (object_class, sizeof (ModestAttachmentViewPriv));
412 tny_mime_part_view_init (gpointer g, gpointer iface_data)
414 TnyMimePartViewIface *klass = (TnyMimePartViewIface *)g;
416 klass->get_part_func = modest_attachment_view_get_part;
417 klass->set_part_func = modest_attachment_view_set_part;
418 klass->clear_func = modest_attachment_view_clear;
424 modest_attachment_view_get_type (void)
426 static GType type = 0;
428 if (G_UNLIKELY(type == 0))
430 static const GTypeInfo info =
432 sizeof (ModestAttachmentViewClass),
433 NULL, /* base_init */
434 NULL, /* base_finalize */
435 (GClassInitFunc) modest_attachment_view_class_init, /* class_init */
436 NULL, /* class_finalize */
437 NULL, /* class_data */
438 sizeof (ModestAttachmentView),
440 modest_attachment_view_instance_init /* instance_init */
443 static const GInterfaceInfo tny_mime_part_view_info =
445 (GInterfaceInitFunc) tny_mime_part_view_init, /* interface_init */
446 NULL, /* interface_finalize */
447 NULL /* interface_data */
450 type = g_type_register_static (GTK_TYPE_EVENT_BOX,
451 "ModestAttachmentView",
454 g_type_add_interface_static (type, TNY_TYPE_MIME_PART_VIEW,
455 &tny_mime_part_view_info);