[evolution/wip-webkit2] EWebView, EmailDisplay - Register custom URI schemes in *_class_init functions
- From: Tomas Popela <tpopela src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution/wip-webkit2] EWebView, EmailDisplay - Register custom URI schemes in *_class_init functions
- Date: Tue, 16 Sep 2014 09:43:25 +0000 (UTC)
commit 0641739d90e5ce580491c83c63bd0af31275e92b
Author: Tomas Popela <tpopela redhat com>
Date: Tue Sep 16 11:42:33 2014 +0200
EWebView, EmailDisplay - Register custom URI schemes in *_class_init functions
e-util/e-web-view.c | 414 ++++++++++++++++++++++++------------------------
mail/e-mail-display.c | 29 ++++
2 files changed, 236 insertions(+), 207 deletions(-)
---
diff --git a/e-util/e-web-view.c b/e-util/e-web-view.c
index 3a0bce6..b5385f9 100644
--- a/e-util/e-web-view.c
+++ b/e-util/e-web-view.c
@@ -932,6 +932,195 @@ web_view_initialize (WebKitWebView *web_view)
NULL, NULL, GTK_WIDGET (web_view));
}
+static void
+web_view_process_uri_scheme_finished_cb (EWebView *web_view,
+ GAsyncResult *result,
+ WebKitURISchemeRequest *request)
+{
+ GError *error = NULL;
+
+ if (!g_task_propagate_boolean (G_TASK (result), &error)) {
+ if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
+ g_warning ("URI %s cannot be processed: %s",
+ webkit_uri_scheme_request_get_uri (request),
+ error ? error->message : "Unknown error");
+ }
+ g_object_unref (request);
+ if (error)
+ g_error_free (error);
+ }
+}
+
+static void
+web_view_process_file_uri_scheme_request (GTask *task,
+ gpointer source_object,
+ gpointer task_data,
+ GCancellable *cancellable)
+{
+ gboolean ret_val = FALSE;
+ const gchar *uri;
+ gchar *content = NULL;
+ gchar *content_type = NULL;
+ gchar *filename = NULL;
+ GInputStream *stream;
+ gsize length = 0;
+ GError *error = NULL;
+ WebKitURISchemeRequest *request = WEBKIT_URI_SCHEME_REQUEST (task_data);
+
+ uri = webkit_uri_scheme_request_get_uri (request);
+
+ filename = g_filename_from_uri (strstr (uri, "file"), NULL, &error);
+ if (!filename)
+ goto out;
+
+ if (!g_file_get_contents (filename, &content, &length, &error))
+ goto out;
+
+ content_type = g_content_type_guess (filename, NULL, 0, NULL);
+
+ stream = g_memory_input_stream_new_from_data (content, length, g_free);
+
+ webkit_uri_scheme_request_finish (request, stream, length, content_type);
+
+ ret_val = TRUE;
+ out:
+ g_free (content_type);
+ g_free (content);
+ g_free (filename);
+
+ if (ret_val)
+ g_object_unref (request);
+
+ if (error)
+ g_task_return_error (task, error);
+ else
+ g_task_return_boolean (task, ret_val);
+}
+
+static void
+web_view_file_uri_scheme_appeared_cb (WebKitURISchemeRequest *request)
+{
+ EWebView *web_view;
+ GTask *task;
+
+ printf ("%s\n", __FUNCTION__);
+ web_view = E_WEB_VIEW (webkit_uri_scheme_request_get_web_view (request));
+
+ task = g_task_new (
+ web_view, NULL,
+ (GAsyncReadyCallback) web_view_process_uri_scheme_finished_cb,
+ request);
+
+ g_task_set_task_data (task, g_object_ref (request), NULL);
+ g_task_run_in_thread (task, web_view_process_file_uri_scheme_request);
+
+ g_object_unref (task);
+}
+
+static void
+web_view_gtk_stock_uri_scheme_appeared_cb (WebKitURISchemeRequest *request)
+{
+ SoupURI *uri;
+ GHashTable *query = NULL;
+ GtkStyleContext *context;
+ GtkWidgetPath *path;
+ GtkIconSet *icon_set;
+ gssize size = GTK_ICON_SIZE_BUTTON;
+ gchar *a_size;
+ gchar *buffer = NULL;
+ gchar *content_type = NULL;
+ gsize buff_len = 0;
+ GError *local_error = NULL;
+
+ uri = soup_uri_new (webkit_uri_scheme_request_get_uri (request));
+
+ if (uri && uri->query)
+ query = soup_form_decode (uri->query);
+
+ if (query) {
+ a_size = g_hash_table_lookup (query, "size");
+ if (a_size != NULL)
+ size = atoi (a_size);
+ g_hash_table_destroy (query);
+ }
+
+ /* Try style context first */
+ context = gtk_style_context_new ();
+ path = gtk_widget_path_new ();
+ gtk_widget_path_append_type (path, GTK_TYPE_WINDOW);
+ gtk_widget_path_append_type (path, GTK_TYPE_BUTTON);
+ gtk_style_context_set_path (context, path);
+ gtk_widget_path_free (path);
+
+ icon_set = gtk_style_context_lookup_icon_set (context, uri->host);
+ if (icon_set != NULL) {
+ GdkPixbuf *pixbuf;
+
+ pixbuf = gtk_icon_set_render_icon_pixbuf (
+ icon_set, context, size);
+ gdk_pixbuf_save_to_buffer (
+ pixbuf, &buffer, &buff_len,
+ "png", &local_error, NULL);
+ g_object_unref (pixbuf);
+
+ /* Fallback to icon theme */
+ } else {
+ GtkIconTheme *icon_theme;
+ GtkIconInfo *icon_info;
+ const gchar *filename;
+
+ icon_theme = gtk_icon_theme_get_default ();
+
+ icon_info = gtk_icon_theme_lookup_icon (
+ icon_theme, uri->host, size,
+ GTK_ICON_LOOKUP_USE_BUILTIN);
+
+ filename = gtk_icon_info_get_filename (icon_info);
+ if (filename != NULL) {
+ g_file_get_contents (
+ filename, &buffer, &buff_len, &local_error);
+ content_type =
+ g_content_type_guess (filename, NULL, 0, NULL);
+
+ } else {
+ GdkPixbuf *pixbuf;
+
+ pixbuf = gtk_icon_info_get_builtin_pixbuf (icon_info);
+ if (pixbuf != NULL) {
+ gdk_pixbuf_save_to_buffer (
+ pixbuf, &buffer, &buff_len,
+ "png", &local_error, NULL);
+ g_object_unref (pixbuf);
+ }
+ }
+
+ gtk_icon_info_free (icon_info);
+ }
+
+ /* Sanity check */
+ g_return_if_fail (
+ ((buffer != NULL) && (local_error == NULL)) ||
+ ((buffer == NULL) && (local_error != NULL)));
+
+ if (!content_type)
+ content_type = g_strdup ("image/png");
+
+ if (buffer != NULL) {
+ GInputStream *stream;
+
+ stream = g_memory_input_stream_new_from_data (
+ buffer, buff_len, (GDestroyNotify) g_free);
+
+ webkit_uri_scheme_request_finish (
+ request, stream, buff_len, content_type);
+
+ g_object_unref (stream);
+ }
+
+ g_object_unref (context);
+ g_free (content_type);
+}
+
static void
web_view_constructed (GObject *object)
@@ -1552,9 +1741,27 @@ e_web_view_class_init (EWebViewClass *class)
{
GObjectClass *object_class;
GtkWidgetClass *widget_class;
+ WebKitWebContext *web_context = webkit_web_context_get_default ();
g_type_class_add_private (class, sizeof (EWebViewPrivate));
+ webkit_web_context_set_cache_model (
+ web_context, WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
+
+ webkit_web_context_register_uri_scheme (
+ web_context,
+ "evo-file",
+ (WebKitURISchemeRequestCallback) web_view_file_uri_scheme_appeared_cb,
+ NULL,
+ NULL);
+
+ webkit_web_context_register_uri_scheme (
+ web_context,
+ "gtk-stock",
+ (WebKitURISchemeRequestCallback) web_view_gtk_stock_uri_scheme_appeared_cb,
+ NULL,
+ NULL);
+
object_class = G_OBJECT_CLASS (class);
object_class->constructor = web_view_constructor;
object_class->set_property = web_view_set_property;
@@ -1775,194 +1982,6 @@ e_web_view_selectable_init (ESelectableInterface *interface)
}
static void
-web_view_process_uri_scheme_finished_cb (EWebView *web_view,
- GAsyncResult *result,
- WebKitURISchemeRequest *request)
-{
- GError *error = NULL;
-
- if (!g_task_propagate_boolean (G_TASK (result), &error)) {
- if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
- g_warning ("URI %s cannot be processed: %s",
- webkit_uri_scheme_request_get_uri (request),
- error ? error->message : "Unknown error");
- }
- g_object_unref (request);
- if (error)
- g_error_free (error);
- }
-}
-
-static void
-web_view_process_file_uri_scheme_request (GTask *task,
- gpointer source_object,
- gpointer task_data,
- GCancellable *cancellable)
-{
- gboolean ret_val = FALSE;
- const gchar *uri;
- gchar *content = NULL;
- gchar *content_type = NULL;
- gchar *filename = NULL;
- GInputStream *stream;
- gsize length = 0;
- GError *error = NULL;
- WebKitURISchemeRequest *request = WEBKIT_URI_SCHEME_REQUEST (task_data);
-
- uri = webkit_uri_scheme_request_get_uri (request);
-
- filename = g_filename_from_uri (strstr (uri, "file"), NULL, &error);
- if (!filename)
- goto out;
-
- if (!g_file_get_contents (filename, &content, &length, &error))
- goto out;
-
- content_type = g_content_type_guess (filename, NULL, 0, NULL);
-
- stream = g_memory_input_stream_new_from_data (content, length, g_free);
-
- webkit_uri_scheme_request_finish (request, stream, length, content_type);
-
- ret_val = TRUE;
- out:
- g_free (content_type);
- g_free (content);
- g_free (filename);
-
- if (ret_val)
- g_object_unref (request);
-
- if (error)
- g_task_return_error (task, error);
- else
- g_task_return_boolean (task, ret_val);
-}
-
-static void
-web_view_file_uri_scheme_appeared_cb (WebKitURISchemeRequest *request)
-{
- EWebView *web_view;
- GTask *task;
-
- web_view = E_WEB_VIEW (webkit_uri_scheme_request_get_web_view (request));
-
- task = g_task_new (
- web_view, NULL,
- (GAsyncReadyCallback) web_view_process_uri_scheme_finished_cb,
- request);
-
- g_task_set_task_data (task, g_object_ref (request), NULL);
- g_task_run_in_thread (task, web_view_process_file_uri_scheme_request);
-
- g_object_unref (task);
-}
-
-static void
-web_view_gtk_stock_uri_scheme_appeared_cb (WebKitURISchemeRequest *request)
-{
- SoupURI *uri;
- GHashTable *query = NULL;
- GtkStyleContext *context;
- GtkWidgetPath *path;
- GtkIconSet *icon_set;
- gssize size = GTK_ICON_SIZE_BUTTON;
- gchar *a_size;
- gchar *buffer = NULL;
- gchar *content_type = NULL;
- gsize buff_len = 0;
- GError *local_error = NULL;
-
- uri = soup_uri_new (webkit_uri_scheme_request_get_uri (request));
-
- if (uri && uri->query)
- query = soup_form_decode (uri->query);
-
- if (query) {
- a_size = g_hash_table_lookup (query, "size");
- if (a_size != NULL)
- size = atoi (a_size);
- g_hash_table_destroy (query);
- }
-
- /* Try style context first */
- context = gtk_style_context_new ();
- path = gtk_widget_path_new ();
- gtk_widget_path_append_type (path, GTK_TYPE_WINDOW);
- gtk_widget_path_append_type (path, GTK_TYPE_BUTTON);
- gtk_style_context_set_path (context, path);
- gtk_widget_path_free (path);
-
- icon_set = gtk_style_context_lookup_icon_set (context, uri->host);
- if (icon_set != NULL) {
- GdkPixbuf *pixbuf;
-
- pixbuf = gtk_icon_set_render_icon_pixbuf (
- icon_set, context, size);
- gdk_pixbuf_save_to_buffer (
- pixbuf, &buffer, &buff_len,
- "png", &local_error, NULL);
- g_object_unref (pixbuf);
-
- /* Fallback to icon theme */
- } else {
- GtkIconTheme *icon_theme;
- GtkIconInfo *icon_info;
- const gchar *filename;
-
- icon_theme = gtk_icon_theme_get_default ();
-
- icon_info = gtk_icon_theme_lookup_icon (
- icon_theme, uri->host, size,
- GTK_ICON_LOOKUP_USE_BUILTIN);
-
- filename = gtk_icon_info_get_filename (icon_info);
- if (filename != NULL) {
- g_file_get_contents (
- filename, &buffer, &buff_len, &local_error);
- content_type =
- g_content_type_guess (filename, NULL, 0, NULL);
-
- } else {
- GdkPixbuf *pixbuf;
-
- pixbuf = gtk_icon_info_get_builtin_pixbuf (icon_info);
- if (pixbuf != NULL) {
- gdk_pixbuf_save_to_buffer (
- pixbuf, &buffer, &buff_len,
- "png", &local_error, NULL);
- g_object_unref (pixbuf);
- }
- }
-
- gtk_icon_info_free (icon_info);
- }
-
- /* Sanity check */
- g_return_if_fail (
- ((buffer != NULL) && (local_error == NULL)) ||
- ((buffer == NULL) && (local_error != NULL)));
-
- if (!content_type)
- content_type = g_strdup ("image/png");
-
- if (buffer != NULL) {
- GInputStream *stream;
-
- stream = g_memory_input_stream_new_from_data (
- buffer, buff_len, (GDestroyNotify) g_free);
-
- webkit_uri_scheme_request_finish (
- request, stream, buff_len, content_type);
-
- g_object_unref (stream);
- }
-
- g_object_unref (context);
- g_free (content_type);
-}
-
-static void
web_view_update_fonts (EWebView *web_view)
{
e_web_view_update_fonts (web_view);
@@ -2208,25 +2227,6 @@ initialize_web_extensions_cb (WebKitWebContext *web_context,
/* Set the web extensions dir before the process is launched */
webkit_web_context_set_web_extensions_directory (
web_context, EVOLUTION_WEB_EXTENSIONS_DIR);
-
- webkit_web_context_set_cache_model (
- web_context, WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
-
- webkit_web_context_register_uri_scheme (
- web_context,
- "evo-file",
- (WebKitURISchemeRequestCallback) web_view_file_uri_scheme_appeared_cb,
- NULL,
- NULL);
-
- webkit_web_context_register_uri_scheme (
- web_context,
- "gtk-stock",
- (WebKitURISchemeRequestCallback) web_view_gtk_stock_uri_scheme_appeared_cb,
- NULL,
- NULL);
-
- g_signal_emit (view, signals[REGISTER_URI_HANDLERS], 0, web_context);
}
void
diff --git a/mail/e-mail-display.c b/mail/e-mail-display.c
index db2dc28..0dafb81 100644
--- a/mail/e-mail-display.c
+++ b/mail/e-mail-display.c
@@ -2103,9 +2103,38 @@ e_mail_display_class_init (EMailDisplayClass *class)
GObjectClass *object_class;
EWebViewClass *web_view_class;
GtkWidgetClass *widget_class;
+ WebKitWebContext *web_context = webkit_web_context_get_default ();
g_type_class_add_private (class, sizeof (EMailDisplayPrivate));
+ webkit_web_context_register_uri_scheme (
+ web_context,
+ "evo-http",
+ (WebKitURISchemeRequestCallback) mail_display_http_uri_scheme_appeared_cb,
+ NULL,
+ NULL);
+
+ webkit_web_context_register_uri_scheme (
+ web_context,
+ "evo-https",
+ (WebKitURISchemeRequestCallback) mail_display_http_uri_scheme_appeared_cb,
+ NULL,
+ NULL);
+
+ webkit_web_context_register_uri_scheme (
+ web_context,
+ "mail",
+ (WebKitURISchemeRequestCallback) mail_display_mail_uri_scheme_appeared_cb,
+ NULL,
+ NULL);
+
+ webkit_web_context_register_uri_scheme (
+ web_context,
+ "cid",
+ (WebKitURISchemeRequestCallback) mail_display_cid_uri_scheme_appeared_cb,
+ NULL,
+ NULL);
+
object_class = G_OBJECT_CLASS (class);
object_class->constructed = mail_display_constructed;
object_class->set_property = mail_display_set_property;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]