pidgin/ljfisher-ssl-client-auth

Update the webview to use the new image sources.

2012-06-15, Elliott Sales de Andrade
177359654a3d
Update the webview to use the new image sources.

Instead of doing ugly HTML parsing, just let WebKit do it. Since the
prpls now use src attributes, it correctly searches for things, which
we can capture and override when it's from the imgstore.
--- a/pidgin/gtkwebview.c Fri Jun 15 02:55:02 2012 -0400
+++ b/pidgin/gtkwebview.c Fri Jun 15 03:06:35 2012 -0400
@@ -53,7 +53,6 @@
*****************************************************************************/
typedef struct _GtkWebViewPriv {
- GHashTable *images; /**< a map from id to temporary file for the image */
gboolean empty; /**< whether anything has been appended **/
/* JS execute queue */
@@ -84,97 +83,44 @@
* Helpers
*****************************************************************************/
-static const char *
-get_image_src_from_id(GtkWebViewPriv *priv, int id)
-{
- char *src;
- PurpleStoredImage *img;
-
- if (priv->images) {
- /* Check for already loaded image */
- src = (char *)g_hash_table_lookup(priv->images, GINT_TO_POINTER(id));
- if (src)
- return src;
- } else {
- priv->images = g_hash_table_new_full(g_direct_hash, g_direct_equal,
- NULL, g_free);
- }
-
- /* Find image in store */
- img = purple_imgstore_find_by_id(id);
-
- src = (char *)purple_imgstore_get_filename(img);
- if (src) {
- src = g_strdup_printf("file://%s", src);
- } else {
- char *tmp;
- tmp = purple_base64_encode(purple_imgstore_get_data(img),
- purple_imgstore_get_size(img));
- src = g_strdup_printf("data:base64,%s", tmp);
- g_free(tmp);
- }
-
- g_hash_table_insert(priv->images, GINT_TO_POINTER(id), src);
-
- return src;
-}
-
-/*
- * Replace all <img id=""> tags with <img src="">. I hoped to never
- * write any HTML parsing code, but I'm forced to do this, until
- * purple changes the way it works.
- */
-static char *
-replace_img_id_with_src(GtkWebViewPriv *priv, const char *html)
+static void
+webview_resource_loading(WebKitWebView *webview,
+ WebKitWebFrame *frame,
+ WebKitWebResource *resource,
+ WebKitNetworkRequest *request,
+ WebKitNetworkResponse *response,
+ gpointer user_data)
{
- GString *buffer = g_string_new(NULL);
- const char *cur = html;
- char *id;
- int nid;
+ const gchar *uri;
- while (*cur) {
- const char *img = strstr(cur, "<img");
- if (!img) {
- g_string_append(buffer, cur);
- break;
- } else
- g_string_append_len(buffer, cur, img - cur);
+ uri = webkit_network_request_get_uri(request);
+ if (purple_str_has_prefix(uri, PURPLE_STORED_IMAGE_PROTOCOL)) {
+ int id;
+ PurpleStoredImage *img;
+ const char *filename;
- cur = strstr(img, "/>");
- if (!cur)
- cur = strstr(img, ">");
+ uri += sizeof(PURPLE_STORED_IMAGE_PROTOCOL) - 1;
+ id = strtoul(uri, NULL, 10);
- if (!cur) { /* invalid html? */
- g_string_printf(buffer, "%s", html);
- break;
- }
+ img = purple_imgstore_find_by_id(id);
+ if (!img)
+ return;
- if (strstr(img, "src=") || !strstr(img, "id=")) {
- g_string_printf(buffer, "%s", html);
- break;
+ filename = purple_imgstore_get_filename(img);
+ if (filename && g_path_is_absolute(filename)) {
+ char *tmp = g_strdup_printf("file://%s", filename);
+ webkit_network_request_set_uri(request, tmp);
+ g_free(tmp);
+ } else {
+ char *b64 = purple_base64_encode(purple_imgstore_get_data(img),
+ purple_imgstore_get_size(img));
+ const char *type = purple_imgstore_get_extension(img);
+ char *tmp = g_strdup_printf("data:image/%s;base64,%s", type, b64);
+ webkit_network_request_set_uri(request, tmp);
+ g_free(b64);
+ g_free(tmp);
}
-
- /*
- * if this is valid HTML, then I can be sure that it
- * has an id= and does not have an src=, since
- * '=' cannot appear in parameters.
- */
-
- id = strstr(img, "id=") + 3;
-
- /* *id can't be \0, since a ">" appears after this */
- if (isdigit(*id))
- nid = atoi(id);
- else
- nid = atoi(id + 1);
-
- /* let's dump this, tag and then dump the src information */
- g_string_append_len(buffer, img, cur - img);
-
- g_string_append_printf(buffer, " src='%s' ", get_image_src_from_id(priv, nid));
}
-
- return g_string_free(buffer, FALSE);
}
static gboolean
@@ -441,9 +387,6 @@
g_free(temp);
g_queue_free(priv->js_queue);
- if (priv->images)
- g_hash_table_unref(priv->images);
-
G_OBJECT_CLASS(parent_class)->finalize(G_OBJECT(webview));
}
@@ -535,6 +478,9 @@
g_signal_connect(webview, "load-finished",
G_CALLBACK(webview_load_finished), NULL);
+
+ g_signal_connect(G_OBJECT(webview), "resource-request-starting",
+ G_CALLBACK(webview_resource_loading), NULL);
}
GType
@@ -612,18 +558,8 @@
void
gtk_webview_load_html_string(GtkWebView *webview, const char *html)
{
- GtkWebViewPriv *priv = GTK_WEBVIEW_GET_PRIVATE(webview);
- char *html_imged;
-
- if (priv->images) {
- g_hash_table_unref(priv->images);
- priv->images = NULL;
- }
-
- html_imged = replace_img_id_with_src(priv, html);
- webkit_web_view_load_string(WEBKIT_WEB_VIEW(webview), html_imged, NULL,
- NULL, "file:///");
- g_free(html_imged);
+ webkit_web_view_load_string(WEBKIT_WEB_VIEW(webview), html, NULL, NULL,
+ "file:///");
}
void
@@ -1067,7 +1003,8 @@
char *img;
dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webview));
- img = g_strdup_printf("<img src='%s'/>", get_image_src_from_id(priv, id));
+ img = g_strdup_printf("<img src='" PURPLE_STORED_IMAGE_PROTOCOL "%d'/>",
+ id);
priv->edit.block_changed = TRUE;
webkit_dom_document_exec_command(dom, "insertHTML", FALSE, img);
--- a/pidgin/gtkwebview.h Fri Jun 15 02:55:02 2012 -0400
+++ b/pidgin/gtkwebview.h Fri Jun 15 03:06:35 2012 -0400
@@ -114,9 +114,7 @@
void gtk_webview_append_html(GtkWebView *webview, const char *markup);
/**
- * Rather than use webkit_webview_load_string, this routine
- * parses and displays the \<img id=?\> tags that make use of the
- * Pidgin imgstore.
+ * Requests loading of the given content.
*
* @param webview The GtkWebView object
* @param html The HTML content to load