From fe090fd4e131861c6b1ad5c7d677b95bf82b2ee9 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Tue, 31 Oct 2017 00:10:19 -0400 Subject: src/view-source.[ch]: New files --- diff --git a/src/local.mk b/src/local.mk index 2218f1a..12b66d0 100644 --- a/src/local.mk +++ b/src/local.mk @@ -11,6 +11,7 @@ marquee_SOURCES += \ %reldir%/tab-label.c \ %reldir%/tab-page.c \ %reldir%/tree.c \ + %reldir%/view-source.c \ %reldir%/web-settings.c \ %reldir%/web-view.c \ %reldir%/window.c diff --git a/src/view-source.c b/src/view-source.c new file mode 100644 index 0000000..5d4372b --- /dev/null +++ b/src/view-source.c @@ -0,0 +1,118 @@ +/* + * view-source: URI 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 "view-source.h" + +#include + +#include +#include + +#include "application.h" +#include "string-utils.h" + +typedef struct { + WebKitURISchemeRequest *request; + gulong handler_id; +} DataSignalData; + +const gchar *document_begin = + "\n" + "\n" + "\n" + "\n" + "Source of %s\n" + "\n" + "\n" + "
\n"
+				"\n";
+const gchar *document_end =
+				"\n"
+			"
\n" + "\n" + "\n"; + +static void +respond(WebKitURISchemeRequest *request, guchar *data, gsize length) +{ + GInputStream *stream; + + stream = g_memory_input_stream_new_from_data( + g_strdup_printf(document_begin, + gtk_widget_get_default_direction() == + GTK_TEXT_DIR_RTL ? "rtl" : "ltr", + "TODO"), -1, g_free); + g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(stream), + data, length, g_free); + g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(stream), + document_end, -1, NULL); + + webkit_uri_scheme_request_finish(request, stream, -1, "text/html"); + g_object_unref(stream); +} + +static void +data_cb(MqWebView *web_view, GParamSpec G_GNUC_UNUSED *param_spec, + DataSignalData *data_signal_data) +{ + guchar *data; + gsize length; + + data = mq_web_view_get_data(web_view, &length); + respond(data_signal_data->request, data, length); + g_signal_handler_disconnect(web_view, data_signal_data->handler_id); +} + +void +mq_view_source_request(WebKitURISchemeRequest *request, + MqApplication *application) +{ + const gchar *path; + gint64 origin_tab_id; + MqTabPage *origin_tab; + MqWebView *origin_web_view; + guchar *data; + gsize length; + DataSignalData *data_signal_data; + + path = webkit_uri_scheme_request_get_path(request); + /* TODO: Handle "view-source:URI" URIs. */ + g_assert(g_str_has_prefix(path, "origin-tab=")); + + /* Parse origin tab ID out of path. */ + path += strlen("origin-tab="); + origin_tab_id = atoi64(path); + + /* Look up origin tab. */ + origin_tab = mq_application_get_tab(application, origin_tab_id); + origin_web_view = mq_tab_page_get_web_view(origin_tab); + + /* Get origin tab's data. */ + data = mq_web_view_get_data(origin_web_view, &length); + if (data) { + respond(request, data, length); + } else { + data_signal_data = g_new(DataSignalData, 1); + data_signal_data->request = request; + data_signal_data->handler_id = g_signal_connect(origin_web_view, + "notify::data", G_CALLBACK(data_cb), data_signal_data); + } +} diff --git a/src/view-source.h b/src/view-source.h new file mode 100644 index 0000000..ba50d3d --- /dev/null +++ b/src/view-source.h @@ -0,0 +1,34 @@ +/* + * view-source: URI 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 . + */ + +#ifndef MQ_VIEW_SOURCE_H +#define MQ_VIEW_SOURCE_H + +#include +#include + +#include "application.h" + +void +mq_view_source_request(WebKitURISchemeRequest *request, + MqApplication *application); + +#endif /* MQ_VIEW_SOURCE_H */ -- cgit v0.9.1