summaryrefslogtreecommitdiffstats
path: root/src/web-view-schemes/view-source.c
blob: e7a1ecbd1b3b4f538491c42879fec8e2e7373808 (plain)
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
/*
 * Web view members and methods for "view-source" scheme
 *
 * Copyright (C) 2017  Patrick McDermott
 *
 * This file is part of Marquee.
 *
 * Marquee is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Marquee 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 General Public License
 * along with Marquee.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "schemes.h"

#include <glib.h>
#include <webkit2/webkit2.h>

#include "../application.h"
#include "../config.h"
#include "../notebook.h"
#include "../tab-page.h"
#include "../web-view.h"

static gchar *
rewrite_uri(MqWebViewScheme G_GNUC_UNUSED *scheme, const gchar *uri)
{
	return g_strdup(uri);
}

static gchar *
display_uri(MqWebViewScheme G_GNUC_UNUSED *scheme, const gchar *uri)
{
	return g_strdup(uri);
}

#define WKCMA(ACTION) \
	WEBKIT_CONTEXT_MENU_ACTION_##ACTION

#define ITEM_DECLS \
	GtkAction             *action; \
	WebKitContextMenuItem *menu_item;
#define ITEM_DECLS_NO_CUSTOM \
	WebKitContextMenuItem *menu_item;
#define NEW_CUSTOM_ITEM(NAME, LABEL) \
	G_STMT_START { \
		/* Don't blame me; blame WebKitGTK+ for using GtkAction. */ \
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
		action = gtk_action_new(#NAME, (LABEL), NULL, NULL); \
		G_GNUC_END_IGNORE_DEPRECATIONS \
		g_signal_connect(action, "activate", \
			G_CALLBACK(menu_##NAME##_activate_cb), web_view); \
		menu_item = webkit_context_menu_item_new(action); \
		webkit_context_menu_append(context_menu, menu_item); \
	} G_STMT_END
#define NEW_STOCK_ITEM(STOCK) \
	G_STMT_START { \
		menu_item = webkit_context_menu_item_new_from_stock_action( \
			WEBKIT_CONTEXT_MENU_ACTION_##STOCK); \
		webkit_context_menu_append(context_menu, menu_item); \
	} G_STMT_END
#define NEW_SEPARATOR_ITEM() \
	G_STMT_START { \
		webkit_context_menu_append(context_menu, \
			webkit_context_menu_item_new_separator()); \
	} G_STMT_END
#define RESTORE_ITEMS(ITEMS) \
	G_STMT_START { \
		for (; ITEMS; ITEMS = ITEMS->next) { \
			webkit_context_menu_append(context_menu, ITEMS->data); \
			g_object_unref(ITEMS->data); \
		} \
	} G_STMT_END

static void
context_menu_document_cb(WebKitContextMenu *context_menu, GList *nav_items,
	MqWebView G_GNUC_UNUSED *web_view)
{
	ITEM_DECLS_NO_CUSTOM

	RESTORE_ITEMS(nav_items);    /* _Back, _Forward, _Stop, _Reload */
	NEW_SEPARATOR_ITEM();        /* --- */
	NEW_STOCK_ITEM(SELECT_ALL);  /* Select _All */
	NEW_SEPARATOR_ITEM();        /* --- */
}

#define PRESERVE_ITEM(ITEMS) \
	G_STMT_START { \
		g_object_ref(items->data); \
		ITEMS = g_list_prepend(ITEMS, items->data); \
	} G_STMT_END

static gboolean
context_menu(MqWebView *web_view, MqWebViewScheme G_GNUC_UNUSED *scheme,
	WebKitContextMenu *context_menu, GdkEvent G_GNUC_UNUSED *event,
	WebKitHitTestResult *hit_test_result)
{
	GList                      *items;
	GList                      *nav_items;
	GList                      *edit_items;
	gboolean                    is_selection;
	WebKitContextMenuAction     stock_action;
	WebKitHitTestResultContext  context;
	gboolean                    context_handled;
	WebKitContextMenuItem      *menu_item;

	/* Get more hints about the context, since WebKit doesn't describe
	 * context very well in hit test results.  Also, preserve menu items
	 * that aren't easy to reproduce (e.g. the Unicode menu and spelling
	 * guesses). */
	items = webkit_context_menu_get_items(context_menu);
	nav_items          = NULL;
	edit_items         = NULL;
	is_selection = FALSE;
	for (; items; items = items->next) {
		stock_action = webkit_context_menu_item_get_stock_action(
			items->data);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
		switch (stock_action) {
			case WKCMA(GO_BACK):
			case WKCMA(GO_FORWARD):
			case WKCMA(STOP):
			case WKCMA(RELOAD):
				PRESERVE_ITEM(nav_items);
				break;
			case WKCMA(COPY):
				PRESERVE_ITEM(edit_items);
				is_selection = TRUE;
				break;
		}
#pragma GCC diagnostic pop
	}
	nav_items  = g_list_reverse(nav_items);
	edit_items = g_list_reverse(edit_items);

	/* Clear the menu. */
	webkit_context_menu_remove_all(context_menu);

	/* Get the reported context (which isn't very descriptive) and save the
	 * hit test result for use by action callbacks. */
	context = webkit_hit_test_result_get_context(hit_test_result);
	if (scheme->normal.hit_test_result) {
		g_object_unref(scheme->normal.hit_test_result);
	}
	scheme->normal.hit_test_result = hit_test_result;
	g_object_ref(scheme->normal.hit_test_result);
	context_handled = FALSE;

	/* Build the context menu. */
	if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT) {
		if (is_selection) {
			RESTORE_ITEMS(edit_items);  /* _Copy */
			context_handled = TRUE;
		} else {
			context_menu_document_cb(context_menu, nav_items,
				web_view);
			context_handled = TRUE;
		}
		if (context_handled) {
			NEW_SEPARATOR_ITEM();
		}
		NEW_STOCK_ITEM(INSPECT_ELEMENT);  /* Inspect Element */
	}

	/* Propagate the event further and show the context menu. */
	return FALSE;
}

static void
save_file(MqWebView *web_view, MqWebViewScheme *scheme)
{
	/* TODO: Stub */
	mq_web_view_get_scheme(web_view);
	*scheme = *scheme;
}

MqWebViewSchemeMethods mq_web_view_view_source_scheme_methods = {
	.rewrite_uri  = rewrite_uri,
	.display_uri  = display_uri,
	.context_menu = context_menu,
	.save_file    = save_file,
};