/*
* 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 .
*/
#include "schemes.h"
#include
#include
#include
#include
#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 /*TODO*/G_GNUC_UNUSED *web_view,
MqWebViewScheme /*TODO*/G_GNUC_UNUSED *scheme)
{
/* TODO: Stub */
}
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,
};