aboutsummaryrefslogtreecommitdiffstats
path: root/modules/prefer-plain/e-mail-display-popup-prefer-plain.c
blob: f9d8a9ddd69dbf07f4d67911abc9476ad21739e4 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * 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; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 */

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

#include "e-mail-display-popup-prefer-plain.h"
#include "mail/e-mail-display-popup-extension.h"
#include "mail/e-mail-display.h"
#include <shell/e-shell.h>
#include <shell/e-shell-window.h>
#include "mail/e-mail-browser.h"

#include <libebackend/libebackend.h>

#include <glib/gi18n-lib.h>

#define d(x)

typedef struct _EMailDisplayPopupPreferPlain EMailDisplayPopupPreferPlain;
typedef struct _EMailDisplayPopupPreferPlainClass EMailDisplayPopupPreferPlainClass;

struct _EMailDisplayPopupPreferPlain {
    EExtension parent;

    WebKitDOMDocument *document;
    gchar *text_plain_id;
    gchar *text_html_id;

    GtkActionGroup *action_group;

};

struct _EMailDisplayPopupPreferPlainClass {
    EExtensionClass parent_class;
};

#define E_TYPE_MAIL_DISPLAY_POPUP_PREFER_PLAIN \
    (e_mail_display_popup_prefer_plain_get_type ())
#define E_MAIL_DISPLAY_POPUP_PREFER_PLAIN(obj) \
    (G_TYPE_CHECK_INSTANCE_CAST \
    ((obj), E_TYPE_MAIL_DISPLAY_POPUP_PREFER_PLAIN, EMailDisplayPopupPreferPlain))

GType e_mail_display_popup_prefer_plain_get_type (void);
static void e_mail_display_popup_extension_interface_init (EMailDisplayPopupExtensionInterface *iface);

G_DEFINE_DYNAMIC_TYPE_EXTENDED (
    EMailDisplayPopupPreferPlain,
    e_mail_display_popup_prefer_plain,
    E_TYPE_EXTENSION,
    0,
    G_IMPLEMENT_INTERFACE_DYNAMIC (
        E_TYPE_MAIL_DISPLAY_POPUP_EXTENSION,
        e_mail_display_popup_extension_interface_init));

static const gchar *ui_webview =
"<ui>"
"  <popup name='context'>"
"    <placeholder name='custom-actions-2'>"
"      <separator/>"
"      <menuitem action='show-plain-text-part'/>"
"      <menuitem action='show-text-html-part'/>"
"      <separator/>"
"    </placeholder>"
"  </popup>"
"</ui>";

static const gchar *ui_reader =
"<ui>"
"  <popup name='mail-preview-popup'>"
"    <placeholder name='mail-preview-popup-actions'>"
"      <separator/>"
"      <menuitem action='show-plain-text-part'/>"
"      <menuitem action='show-text-html-part'/>"
"      <separator/>"
"    </placeholder>"
"  </popup>"
"</ui>";

static void
toggle_part (GtkAction *action,
             EMailDisplayPopupExtension *extension)
{
    EMailDisplayPopupPreferPlain *pp_extension = (EMailDisplayPopupPreferPlain *) extension;
    WebKitDOMDocument *doc = pp_extension->document;
    WebKitDOMDOMWindow *window;
    WebKitDOMElement *frame_element;
    SoupURI *soup_uri;
    GHashTable *query;
    gchar *uri;

    uri = webkit_dom_document_get_document_uri (doc);
    soup_uri = soup_uri_new (uri);
    g_free (uri);

    query = soup_form_decode (soup_uri->query);
    g_hash_table_replace (
        query, g_strdup ("part_id"),
        pp_extension->text_html_id ?
            pp_extension->text_html_id :
            pp_extension->text_plain_id);
    g_hash_table_replace (
        query, g_strdup ("mime_type"),
        pp_extension->text_html_id ?
            (gpointer) "text/html" :
            (gpointer) "text/plain");

    soup_uri_set_query_from_form (soup_uri, query);
    g_hash_table_destroy (query);

    uri = soup_uri_to_string (soup_uri, FALSE);
    soup_uri_free (soup_uri);

    /* Get frame's window and from the window the actual <iframe> element */
    window = webkit_dom_document_get_default_view (doc);
    frame_element = webkit_dom_dom_window_get_frame_element (window);
    webkit_dom_html_iframe_element_set_src (
        WEBKIT_DOM_HTML_IFRAME_ELEMENT (frame_element), uri);

    g_free (uri);
}

GtkActionEntry entries[] = {

    { "show-plain-text-part",
       NULL,
       N_("Display plain text version"),
       NULL,
       N_("Display plain text version of multipart/alternative message"),
       NULL
    },

    { "show-text-html-part",
      NULL,
      N_("Display HTML version"),
      NULL,
      N_("Display HTML version of multipart/alternative message"),
      NULL
    }
};

const gint ID_LEN = G_N_ELEMENTS (".alternative-prefer-plain.");

static void
set_text_plain_id (EMailDisplayPopupPreferPlain *extension,
                   const gchar *id)
{
    g_free (extension->text_plain_id);
    extension->text_plain_id = g_strdup (id);
}

static void
set_text_html_id (EMailDisplayPopupPreferPlain *extension,
                  const gchar *id)
{
    g_free (extension->text_html_id);
    extension->text_html_id = g_strdup (id);
}

static GtkActionGroup *
create_group (EMailDisplayPopupExtension *extension)
{
    EExtensible *extensible;
    EWebView *web_view;
    GtkUIManager *ui_manager;
    GtkActionGroup *group;
    GtkAction *action;
    EShell *shell;
    GtkWindow *shell_window;

    extensible = e_extension_get_extensible (E_EXTENSION (extension));
    web_view = E_WEB_VIEW (extensible);

    group = gtk_action_group_new ("prefer-plain");
    gtk_action_group_add_actions (group, entries, G_N_ELEMENTS (entries), NULL);

    ui_manager = e_web_view_get_ui_manager (web_view);
    gtk_ui_manager_insert_action_group (ui_manager, group, 0);
    gtk_ui_manager_add_ui_from_string (ui_manager, ui_webview, -1, NULL);

    action = gtk_action_group_get_action (group, "show-plain-text-part");
    g_signal_connect (
        action, "activate",
        G_CALLBACK (toggle_part), extension);

    action = gtk_action_group_get_action (group, "show-text-html-part");
    g_signal_connect (
        action, "activate",
        G_CALLBACK (toggle_part), extension);

    shell = e_shell_get_default ();
    shell_window = e_shell_get_active_window (shell);
    if (E_IS_SHELL_WINDOW (shell_window)) {
        ui_manager = e_shell_window_get_ui_manager (E_SHELL_WINDOW (shell_window));
    } else if (E_IS_MAIL_BROWSER (shell_window)) {
        ui_manager = e_mail_browser_get_ui_manager (E_MAIL_BROWSER (shell_window));
    } else {
        return NULL;
    }

    gtk_ui_manager_insert_action_group (ui_manager, group, 0);
    gtk_ui_manager_add_ui_from_string (ui_manager, ui_reader, -1, NULL);

    return group;
}

static void
mail_display_popup_prefer_plain_update_actions (EMailDisplayPopupExtension *extension,
                                                WebKitHitTestResult *context)
{
    EMailDisplay *display;
    GtkAction *action;
    WebKitDOMNode *node;
    gchar *uri, *part_id, *pos, *prefix;
    SoupURI *soup_uri;
    GHashTable *query;
    EMailPartList *part_list;
    gboolean is_text_plain;
    const gchar *action_name;
    EMailDisplayPopupPreferPlain *pp_extension;
    GQueue queue = G_QUEUE_INIT;
    GList *head, *link;

    display = E_MAIL_DISPLAY (e_extension_get_extensible (
            E_EXTENSION (extension)));

    pp_extension = E_MAIL_DISPLAY_POPUP_PREFER_PLAIN (extension);

    if (!pp_extension->action_group)
        pp_extension->action_group = create_group (extension);

    g_object_get (context, "inner-node", &node, NULL);

    if (!node) {
        gtk_action_group_set_visible (pp_extension->action_group, FALSE);
        return;
    }

    pp_extension->document = webkit_dom_node_get_owner_document (node);
    uri = webkit_dom_document_get_document_uri (pp_extension->document);

    soup_uri = soup_uri_new (uri);
    if (!soup_uri || !soup_uri->query) {
        gtk_action_group_set_visible (pp_extension->action_group, FALSE);
        if (soup_uri)
            soup_uri_free (soup_uri);
        g_free (uri);
        return;
    }

    query = soup_form_decode (soup_uri->query);
    part_id = g_hash_table_lookup (query, "part_id");
    if (part_id == NULL) {
        gtk_action_group_set_visible (pp_extension->action_group, FALSE);
        g_hash_table_destroy (query);
        soup_uri_free (soup_uri);
        g_free (uri);
        return;
    }

    pos = strstr (part_id, ".alternative-prefer-plain.");
    if (!pos) {
        gtk_action_group_set_visible (pp_extension->action_group, FALSE);
        g_hash_table_destroy (query);
        soup_uri_free (soup_uri);
        g_free (uri);
        return;
    }

    /* Don't display the actions on any other than text/plain or text/html parts */
    if (!strstr (pos, "plain_text") && !strstr (pos, "text_html")) {
        gtk_action_group_set_visible (pp_extension->action_group, FALSE);
        g_hash_table_destroy (query);
        soup_uri_free (soup_uri);
        g_free (uri);
        return;
    }

    /* Check whether the displayed part is text_plain */
    is_text_plain = (strstr (pos + ID_LEN, "plain_text") != NULL);

    /* It is! Hide the menu action */
    if (is_text_plain) {
        action = gtk_action_group_get_action (
            pp_extension->action_group, "show-plain-text-part");
        gtk_action_set_visible (action, FALSE);
    } else {
        action = gtk_action_group_get_action (
            pp_extension->action_group, "show-text-html-part");
        gtk_action_set_visible (action, FALSE);
    }

    /* Now check whether HTML version exists, if it does enable the action */
    prefix = g_strndup (part_id, (pos - part_id) + ID_LEN - 1);

    action_name = NULL;
    part_list = e_mail_display_get_part_list (display);
    e_mail_part_list_queue_parts (part_list, NULL, &queue);
    head = g_queue_peek_head_link (&queue);

    for (link = head; link != NULL; link = g_list_next (link)) {
        EMailPart *p = link->data;

        if (e_mail_part_id_has_prefix (p, prefix) &&
            (e_mail_part_id_has_substr (p, "text_html") ||
             e_mail_part_id_has_substr (p, "plain_text"))) {
            const gchar *p_id;

            p_id = e_mail_part_get_id (p);

            pos = strstr (p_id, ".alternative-prefer-plain.");

            if (is_text_plain) {
                if (strstr (pos + ID_LEN, "text_html") != NULL) {
                    action_name = "show-text-html-part";
                    set_text_html_id (pp_extension, p_id);
                    set_text_plain_id (pp_extension, NULL);
                    break;
                }
            } else {
                if (strstr (pos + ID_LEN, "plain_text") != NULL) {
                    action_name = "show-plain-text-part";
                    set_text_html_id (pp_extension, NULL);
                    set_text_plain_id (pp_extension, p_id);
                    break;
                }
            }
        }
    }

    while (!g_queue_is_empty (&queue))
        g_object_unref (g_queue_pop_head (&queue));

    if (action_name) {
        action = gtk_action_group_get_action (
            pp_extension->action_group, action_name);
        gtk_action_group_set_visible (pp_extension->action_group, TRUE);
        gtk_action_set_visible (action, TRUE);
    } else {
        gtk_action_group_set_visible (pp_extension->action_group, FALSE);
    }

    g_free (prefix);
    g_hash_table_destroy (query);
    soup_uri_free (soup_uri);
    g_free (uri);
}

void
e_mail_display_popup_prefer_plain_type_register (GTypeModule *type_module)
{
    e_mail_display_popup_prefer_plain_register_type (type_module);
}

static void
e_mail_display_popup_prefer_plain_dispose (GObject *object)
{
    EMailDisplayPopupPreferPlain *extension;

    extension = E_MAIL_DISPLAY_POPUP_PREFER_PLAIN (object);

    if (extension->action_group != NULL) {
        g_object_unref (extension->action_group);
        extension->action_group = NULL;
    }

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_mail_display_popup_prefer_plain_parent_class)->
        dispose (object);
}

static void
e_mail_display_popup_prefer_plain_finalize (GObject *object)
{
    EMailDisplayPopupPreferPlain *extension;

    extension = E_MAIL_DISPLAY_POPUP_PREFER_PLAIN (object);

    g_free (extension->text_html_id);
    g_free (extension->text_plain_id);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_mail_display_popup_prefer_plain_parent_class)->
        finalize (object);
}

static void
e_mail_display_popup_prefer_plain_class_init (EMailDisplayPopupPreferPlainClass *class)
{
    EExtensionClass *extension_class;
    GObjectClass *object_class;

    extension_class = E_EXTENSION_CLASS (class);
    extension_class->extensible_type = E_TYPE_MAIL_DISPLAY;

    object_class = G_OBJECT_CLASS (class);
    object_class->dispose = e_mail_display_popup_prefer_plain_dispose;
    object_class->finalize = e_mail_display_popup_prefer_plain_finalize;
}

static void
e_mail_display_popup_extension_interface_init (EMailDisplayPopupExtensionInterface *iface)
{
    iface->update_actions = mail_display_popup_prefer_plain_update_actions;
}

void
e_mail_display_popup_prefer_plain_class_finalize (EMailDisplayPopupPreferPlainClass *class)
{

}

static void
e_mail_display_popup_prefer_plain_init (EMailDisplayPopupPreferPlain *extension)
{
    extension->action_group = NULL;
    extension->text_html_id = NULL;
    extension->text_plain_id = NULL;
    extension->document = NULL;
}