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
|
/*
* 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 <inttypes.h>
#include <string.h>
#include <glib.h>
#include <webkit2/webkit2.h>
#include "../application.h"
#include "../config/config.h"
#include "../config/settings.h"
#include "../tab-page.h"
#include "../utils/string.h"
#include "../web-view.h"
static MqSettings *settings = NULL;
static gboolean
match_uri(const gchar *uri)
{
return uri && g_str_has_prefix(uri, "view-source:");
}
static void
init_settings(MqConfig *config)
{
if (settings) {
return;
}
settings = mq_settings_new();
mq_settings_set_web_context(settings, webkit_web_context_get_default());
mq_settings_override_bool(settings, "permissions.javascript.enable",
TRUE);
mq_settings_connect_config(settings, config);
}
static void
initialize(MqWebView *web_view, MqWebViewScheme *scheme,
const gchar *uri)
{
gchar *query_str;
GHashTable *query;
init_settings(mq_web_view_get_config(web_view));
webkit_web_view_set_settings(WEBKIT_WEB_VIEW(web_view),
WEBKIT_SETTINGS(settings));
query_str = g_strdup(uri + strlen("view-source:"));
query = mq_parse_query_string(query_str);
scheme->view_source.origin_tab_id = mq_atoi64(g_hash_table_lookup(query,
"origin-tab"));
scheme->view_source.uri = g_strdup(g_hash_table_lookup(query, "uri"));
g_hash_table_unref(query);
g_free(query_str);
}
static void
finalize(MqWebViewScheme *scheme)
{
memset(&scheme->view_source, 0, sizeof(scheme->view_source));
}
static gchar *
rewrite_uri(MqWebView G_GNUC_UNUSED *web_view, MqWebViewScheme *scheme,
const gchar G_GNUC_UNUSED *uri)
{
/* TODO: Handle reloads. */
return g_strdup_printf("view-source:origin-tab=%" PRId64 "&uri=%s",
scheme->view_source.origin_tab_id, scheme->view_source.uri);
}
static gchar *
display_uri(MqWebView G_GNUC_UNUSED *web_view, MqWebViewScheme *scheme,
const gchar G_GNUC_UNUSED *uri)
{
return g_strdup_printf("view-source:%s", scheme->view_source.uri);
}
#define WKCMA(ACTION) \
WEBKIT_CONTEXT_MENU_ACTION_##ACTION
#define ITEM_DECLS_NO_CUSTOM \
WebKitContextMenuItem *menu_item;
#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;
default:
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->view_source.hit_test_result) {
g_object_unref(scheme->view_source.hit_test_result);
}
scheme->view_source.hit_test_result = hit_test_result;
g_object_ref(scheme->view_source.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 = {
.match_uri = match_uri,
.initialize = initialize,
.finalize = finalize,
.rewrite_uri = rewrite_uri,
.display_uri = display_uri,
.context_menu = context_menu,
.save_file = save_file,
};
|