aboutsummaryrefslogtreecommitdiffstats
path: root/em-format/e-mail-formatter-audio.c
blob: cf980f8e78413c5e3c3dbffe25aca5917eee78b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * e-mail-formatter-audio.c
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n-lib.h>

#include <camel/camel.h>

#include "e-mail-formatter-extension.h"
#include "e-mail-formatter.h"
#include "e-mail-part-audio.h"

#define d(x)

typedef EMailFormatterExtension EMailFormatterAudio;
typedef EMailFormatterExtensionClass EMailFormatterAudioClass;

GType e_mail_formatter_audio_get_type (void);

G_DEFINE_TYPE (
    EMailFormatterAudio,
    e_mail_formatter_audio,
    E_TYPE_MAIL_FORMATTER_EXTENSION)

static const gchar *formatter_mime_types[] = {
    "application/vnd.evolution.widget.audio",
    "audio/ac3",
    "audio/x-ac3",
    "audio/basic",
    "audio/mpeg",
    "audio/x-mpeg",
    "audio/mpeg3",
    "audio/x-mpeg3",
    "audio/mp3",
    "audio/x-mp3",
    "audio/mp4",
    "audio/flac",
    "audio/x-flac",
    "audio/mod",
    "audio/x-mod",
    "audio/x-wav",
    "audio/microsoft-wav",
    "audio/x-wma",
    "audio/x-ms-wma",
    "audio/ogg",
    "audio/x-vorbis+ogg",
    "application/ogg",
    "application/x-ogg",
    NULL
};

static gboolean
mail_formatter_audio_format (EMailFormatterExtension *extension,
                             EMailFormatter *formatter,
                             EMailFormatterContext *context,
                             EMailPart *part,
                             GOutputStream *stream,
                             GCancellable *cancellable)
{
    CamelMimePart *mime_part;
    CamelDataWrapper *content;
    CamelTransferEncoding encoding;
    GOutputStream *mem_stream;
    const gchar *mime_type;
    gchar *html;
    GError *local_error = NULL;

    mime_part = e_mail_part_ref_mime_part (part);
    encoding = camel_mime_part_get_encoding (mime_part);
    content = camel_medium_get_content (CAMEL_MEDIUM (mime_part));

    mime_type = e_mail_part_get_mime_type (part);
    if (mime_type == NULL)
        mime_type = "audio/*";

    mem_stream = g_memory_output_stream_new_resizable ();

    if (encoding == CAMEL_TRANSFER_ENCODING_BASE64) {
        const gchar *data;

        camel_data_wrapper_write_to_output_stream_sync (
            content, mem_stream, cancellable, &local_error);

        data = g_memory_output_stream_get_data (
            G_MEMORY_OUTPUT_STREAM (mem_stream));

        html = g_strdup_printf (
            "<audio controls>"
            "<source src=\"data:%s;base64,%s\"/>"
            "</audio>",
            mime_type, data);

    } else {
        const guchar *data;
        gchar *base64;
        gsize size;

        camel_data_wrapper_decode_to_output_stream_sync (
            content, mem_stream, cancellable, &local_error);

        data = g_memory_output_stream_get_data (
            G_MEMORY_OUTPUT_STREAM (mem_stream));
        size = g_memory_output_stream_get_data_size (
            G_MEMORY_OUTPUT_STREAM (mem_stream));

        base64 = g_base64_encode (data, size);
        html = g_strdup_printf (
            "<audio controls>"
            "<source src=\"data:%s;base64,%s\"/>"
            "</audio>",
            mime_type, base64);
        g_free (base64);
    }

    /* XXX Should show the error message in the UI somehow. */
    if (local_error != NULL) {
        g_warning ("%s: %s", G_STRFUNC, local_error->message);
        g_error_free (local_error);
    }

    g_output_stream_write_all (
        stream, html, strlen (html), NULL, cancellable, NULL);

    g_free (html);

    g_object_unref (mime_part);
    g_object_unref (mem_stream);

    return TRUE;
}

static void
e_mail_formatter_audio_class_init (EMailFormatterExtensionClass *class)
{
    class->display_name = _("Audio Player");
    class->description = _("Play the attachment in embedded audio player");
    class->mime_types = formatter_mime_types;
    class->priority = G_PRIORITY_LOW;
    class->format = mail_formatter_audio_format;
}

static void
e_mail_formatter_audio_init (EMailFormatterExtension *extension)
{
}