1 /*
2 * e-mail-formatter-audio.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 <camel/camel.h>
23
24 #include "e-mail-formatter-extension.h"
25 #include "e-mail-formatter.h"
26 #include "e-mail-part-audio.h"
27
28 /* This is currently disabled, because the WebKit player requires javascript,
29 which is disabled in Evolution. */
30
31 typedef EMailFormatterExtension EMailFormatterAudio;
32 typedef EMailFormatterExtensionClass EMailFormatterAudioClass;
33
34 GType e_mail_formatter_audio_get_type (void);
35
36 G_DEFINE_TYPE (
37 EMailFormatterAudio,
38 e_mail_formatter_audio,
39 E_TYPE_MAIL_FORMATTER_EXTENSION)
40
41 static const gchar *formatter_mime_types[] = {
42 "application/vnd.evolution.audio",
43 "audio/ac3",
44 "audio/x-ac3",
45 "audio/basic",
46 "audio/mpeg",
47 "audio/x-mpeg",
48 "audio/mpeg3",
49 "audio/x-mpeg3",
50 "audio/mp3",
51 "audio/x-mp3",
52 "audio/mp4",
53 "audio/flac",
54 "audio/x-flac",
55 "audio/mod",
56 "audio/x-mod",
57 "audio/x-wav",
58 "audio/microsoft-wav",
59 "audio/x-wma",
60 "audio/x-ms-wma",
61 "audio/ogg",
62 "audio/x-vorbis+ogg",
63 "application/ogg",
64 "application/x-ogg",
65 NULL
66 };
67
68 static gboolean
mail_formatter_audio_format(EMailFormatterExtension * extension,EMailFormatter * formatter,EMailFormatterContext * context,EMailPart * part,GOutputStream * stream,GCancellable * cancellable)69 mail_formatter_audio_format (EMailFormatterExtension *extension,
70 EMailFormatter *formatter,
71 EMailFormatterContext *context,
72 EMailPart *part,
73 GOutputStream *stream,
74 GCancellable *cancellable)
75 {
76 CamelMimePart *mime_part;
77 CamelDataWrapper *content;
78 CamelTransferEncoding encoding;
79 GOutputStream *mem_stream;
80 const gchar *mime_type;
81 gchar *html;
82 GError *local_error = NULL;
83
84 mime_part = e_mail_part_ref_mime_part (part);
85 encoding = camel_mime_part_get_encoding (mime_part);
86 content = camel_medium_get_content (CAMEL_MEDIUM (mime_part));
87
88 mime_type = e_mail_part_get_mime_type (part);
89 if (mime_type == NULL)
90 mime_type = "audio/*";
91
92 mem_stream = g_memory_output_stream_new_resizable ();
93
94 if (encoding == CAMEL_TRANSFER_ENCODING_BASE64) {
95 const gchar *data;
96
97 camel_data_wrapper_write_to_output_stream_sync (
98 content, mem_stream, cancellable, &local_error);
99
100 data = g_memory_output_stream_get_data (
101 G_MEMORY_OUTPUT_STREAM (mem_stream));
102
103 html = g_strdup_printf (
104 "<audio controls>"
105 "<source src=\"data:%s;base64,%s\"/>"
106 "</audio>",
107 mime_type, data);
108
109 } else {
110 const guchar *data;
111 gchar *base64;
112 gsize size;
113
114 camel_data_wrapper_decode_to_output_stream_sync (
115 content, mem_stream, cancellable, &local_error);
116
117 data = g_memory_output_stream_get_data (
118 G_MEMORY_OUTPUT_STREAM (mem_stream));
119 size = g_memory_output_stream_get_data_size (
120 G_MEMORY_OUTPUT_STREAM (mem_stream));
121
122 base64 = g_base64_encode (data, size);
123 html = g_strdup_printf (
124 "<audio controls>"
125 "<source src=\"data:%s;base64,%s\"/>"
126 "</audio>",
127 mime_type, base64);
128 g_free (base64);
129 }
130
131 /* XXX Should show the error message in the UI somehow. */
132 if (local_error != NULL) {
133 g_warning ("%s: %s", G_STRFUNC, local_error->message);
134 g_error_free (local_error);
135 }
136
137 g_output_stream_write_all (
138 stream, html, strlen (html), NULL, cancellable, NULL);
139
140 g_free (html);
141
142 g_object_unref (mime_part);
143 g_object_unref (mem_stream);
144
145 return TRUE;
146 }
147
148 static void
e_mail_formatter_audio_class_init(EMailFormatterExtensionClass * class)149 e_mail_formatter_audio_class_init (EMailFormatterExtensionClass *class)
150 {
151 class->display_name = _("Audio Player");
152 class->description = _("Play the attachment in embedded audio player");
153 class->mime_types = formatter_mime_types;
154 class->priority = G_PRIORITY_LOW;
155 class->format = mail_formatter_audio_format;
156 }
157
158 static void
e_mail_formatter_audio_init(EMailFormatterExtension * extension)159 e_mail_formatter_audio_init (EMailFormatterExtension *extension)
160 {
161 }
162
163