1 /*
2 * image-any.c
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 #include "evolution-config.h"
19
20 #include <glib/gi18n-lib.h>
21
22 #include <e-util/e-util.h>
23
24 #include "e-mail-formatter-extension.h"
25 #include "e-mail-inline-filter.h"
26 #include "e-mail-parser-extension.h"
27 #include "e-mail-part-utils.h"
28
29 typedef EMailFormatterExtension EMailFormatterImage;
30 typedef EMailFormatterExtensionClass EMailFormatterImageClass;
31
32 GType e_mail_formatter_image_get_type (void);
33
34 G_DEFINE_TYPE (
35 EMailFormatterImage,
36 e_mail_formatter_image,
37 E_TYPE_MAIL_FORMATTER_EXTENSION)
38
39 static const gchar *formatter_mime_types[] = {
40 "image/gif",
41 "image/jpeg",
42 "image/png",
43 "image/x-png",
44 "image/x-bmp",
45 "image/bmp",
46 "image/svg",
47 "image/x-cmu-raster",
48 "image/x-ico",
49 "image/x-portable-anymap",
50 "image/x-portable-bitmap",
51 "image/x-portable-graymap",
52 "image/x-portable-pixmap",
53 "image/x-xpixmap",
54 "image/jpg",
55 "image/pjpeg",
56 "image/*",
57 NULL
58 };
59
60 static gboolean
emfe_image_format(EMailFormatterExtension * extension,EMailFormatter * formatter,EMailFormatterContext * context,EMailPart * part,GOutputStream * stream,GCancellable * cancellable)61 emfe_image_format (EMailFormatterExtension *extension,
62 EMailFormatter *formatter,
63 EMailFormatterContext *context,
64 EMailPart *part,
65 GOutputStream *stream,
66 GCancellable *cancellable)
67 {
68 CamelMimePart *mime_part;
69 CamelContentType *content_type;
70
71 if (g_cancellable_is_cancelled (cancellable))
72 return FALSE;
73
74 mime_part = e_mail_part_ref_mime_part (part);
75
76 content_type = camel_mime_part_get_content_type (mime_part);
77
78 /* Skip TIFF images, which cannot be shown inline */
79 if (content_type && (
80 camel_content_type_is (content_type, "image", "tiff") ||
81 camel_content_type_is (content_type, "image", "tif"))) {
82 g_clear_object (&mime_part);
83 return FALSE;
84 }
85
86 if (context->mode == E_MAIL_FORMATTER_MODE_RAW) {
87 CamelDataWrapper *dw;
88 GBytes *bytes;
89 GOutputStream *raw_content;
90
91 dw = camel_medium_get_content (CAMEL_MEDIUM (mime_part));
92 g_return_val_if_fail (dw, FALSE);
93
94 raw_content = g_memory_output_stream_new_resizable ();
95 camel_data_wrapper_decode_to_output_stream_sync (
96 dw, raw_content, cancellable, NULL);
97 g_output_stream_close (raw_content, NULL, NULL);
98
99 bytes = g_memory_output_stream_steal_as_bytes (
100 G_MEMORY_OUTPUT_STREAM (raw_content));
101
102 if (!e_mail_formatter_get_animate_images (formatter)) {
103 gchar *buff;
104 gsize len;
105
106 e_mail_part_animation_extract_frame (
107 bytes, &buff, &len);
108
109 g_output_stream_write_all (
110 stream, buff, len, NULL, cancellable, NULL);
111
112 g_free (buff);
113
114 } else {
115 gconstpointer data;
116 gsize size;
117
118 data = g_bytes_get_data (bytes, &size);
119
120 g_output_stream_write_all (
121 stream, data, size, NULL, cancellable, NULL);
122 }
123
124 g_bytes_unref (bytes);
125 g_object_unref (raw_content);
126 } else {
127 gchar *buffer, *uri;
128 const gchar *filename;
129
130 filename = camel_mime_part_get_filename (mime_part);
131
132 uri = e_mail_part_build_uri (
133 e_mail_part_list_get_folder (context->part_list),
134 e_mail_part_list_get_message_uid (context->part_list),
135 "part_id", G_TYPE_STRING, e_mail_part_get_id (part),
136 "mode", G_TYPE_INT, E_MAIL_FORMATTER_MODE_RAW,
137 "filename", G_TYPE_STRING, filename ? filename : "",
138 NULL);
139
140 buffer = g_strdup_printf ("<img src=\"%s\" style=\"max-width:100%%;\" />", uri);
141
142 g_output_stream_write_all (
143 stream, buffer, strlen (buffer),
144 NULL, cancellable, NULL);
145
146 g_free (buffer);
147 g_free (uri);
148 }
149
150 g_object_unref (mime_part);
151
152 return TRUE;
153 }
154
155 static void
e_mail_formatter_image_class_init(EMailFormatterExtensionClass * class)156 e_mail_formatter_image_class_init (EMailFormatterExtensionClass *class)
157 {
158 class->display_name = _("Regular Image");
159 class->description = _("Display part as an image");
160 class->mime_types = formatter_mime_types;
161 class->priority = G_PRIORITY_LOW;
162 class->format = emfe_image_format;
163 }
164
165 static void
e_mail_formatter_image_init(EMailFormatterExtension * extension)166 e_mail_formatter_image_init (EMailFormatterExtension *extension)
167 {
168 }
169