summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick McDermott <pj@pehjota.net>2017-10-31 00:10:19 (EDT)
committer Patrick McDermott <pj@pehjota.net>2017-10-31 00:10:19 (EDT)
commitfe090fd4e131861c6b1ad5c7d677b95bf82b2ee9 (patch)
treea2a3a3daeacca3d50d9d4f9b14278831f3208adf
parentc29fc2bad3f973b3a80eb156b1d405c1de56d1ff (diff)
downloadmarquee-fe090fd4e131861c6b1ad5c7d677b95bf82b2ee9.zip
marquee-fe090fd4e131861c6b1ad5c7d677b95bf82b2ee9.tar.gz
marquee-fe090fd4e131861c6b1ad5c7d677b95bf82b2ee9.tar.bz2
src/view-source.[ch]: New files
-rw-r--r--src/local.mk1
-rw-r--r--src/view-source.c118
-rw-r--r--src/view-source.h34
3 files changed, 153 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "view-source.h"
+
+#include <string.h>
+
+#include <glib.h>
+#include <webkit2/webkit2.h>
+
+#include "application.h"
+#include "string-utils.h"
+
+typedef struct {
+ WebKitURISchemeRequest *request;
+ gulong handler_id;
+} DataSignalData;
+
+const gchar *document_begin =
+ "<!doctype html>\n"
+ "<html dir=\"%s\">\n"
+ "<head>\n"
+ "<meta charset=\"utf-8\">\n"
+ "<title>Source of %s</title>\n"
+ "</head>\n"
+ "<body>\n"
+ "<pre>\n"
+ "<code>\n";
+const gchar *document_end =
+ "</code>\n"
+ "</pre>\n"
+ "</body>\n"
+ "</html>\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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MQ_VIEW_SOURCE_H
+#define MQ_VIEW_SOURCE_H
+
+#include <glib.h>
+#include <webkit2/webkit2.h>
+
+#include "application.h"
+
+void
+mq_view_source_request(WebKitURISchemeRequest *request,
+ MqApplication *application);
+
+#endif /* MQ_VIEW_SOURCE_H */