=== modified file 'webkit.c' --- webkit.c 2008-11-30 13:05:46 +0000 +++ webkit.c 2009-03-14 14:11:46 +0000 @@ -25,10 +25,10 @@ /* System headers */ #include #include +#include /* Webkit headers */ -#include -#include +#include /* Purple headers */ #include @@ -41,6 +41,10 @@ #include #include +/* some unix specific stuff */ +#include +#include + /* Own includes */ #include "webkit.h" #include "prefs.h" @@ -501,17 +505,26 @@ } -static WebKitNavigationResponse -webkit_navigation_requested_cb(WebKitWebView *web_view, WebKitWebFrame *frame, - WebKitNetworkRequest *request) +static gboolean +webkit_navigation_requested_cb(WebKitWebView *web_view, + WebKitWebFrame *frame, + WebKitNetworkRequest *request, + WebKitWebNavigationAction *action, + WebKitWebPolicyDecision *pd, + PidginWebkit *px) { purple_debug_info("WebKit","webkit_navigation_requested_cb\n"); /* TODO: Adium does not open file: URIs and allows WebKit to handle any navigation of type 'Other' */ - purple_notify_uri(NULL, webkit_network_request_get_uri(request)); - return WEBKIT_NAVIGATION_RESPONSE_IGNORE; + const gchar *uri = webkit_network_request_get_uri (request); + if (strstr (uri, "/tmp/webkit-pidgin-")) { + return FALSE; + } else { + purple_notify_uri(NULL, webkit_network_request_get_uri(request)); + return TRUE; + } } static void pidgin_webkit_prefs_style_cb(const char *name, PurplePrefType type, @@ -644,6 +657,69 @@ " with this contact\n"); } +/** + * Returns a filename for a temporary file given the purple conversation. + * The returned string must be freed! + * Example: + * "chat-with-user1.html" + */ +char* get_filename_for_template(PurpleConversation *conv) +{ + + char *name = g_uri_escape_string + (conv->name, G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO, TRUE); + char *tmp = g_strconcat(name , ".html", NULL); + g_free(name); + return tmp; +} + +/** + * Returns a directory for a temporary file given the purple conversation. + * The returned string must be freed! + * Example: + * "/tmp/webkit-pidgin-myusername" + */ +char* get_dir_for_tempfile(PurpleConversation *conv) +{ + return g_strconcat("/tmp/webkit-pidgin-", getenv("USER"), NULL); +} + +/** + * Returns the full-path for a temporary file given the purple conversation. + * The returned string must be freed! + * Example: + * "/tmp/webkit-pidgin-myusername/chat-with-user1.html" + */ +char* get_full_path_for_tempfile(PurpleConversation *conv) +{ + char *name = get_filename_for_template(conv); + char *dir = get_dir_for_tempfile(conv); + char *path = g_strconcat(dir, "/", name, NULL); + g_free(name); + g_free(dir); + return path; +} + +/** + * Returns a file-uri for a temporary file given the purple conversation. + * The returned string must be freed! + * Example: + * "file:///tmp/webkit-pidgin-myusername/chat-with-user1.html" + */ +char *get_file_uri_for_tempfile(PurpleConversation *conv) +{ + char *name = get_filename_for_template(conv); + char *dir = get_dir_for_tempfile(conv); + char *escaped = g_uri_escape_string + (name, G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO, TRUE); + char *result = g_strconcat("file://", dir, "/", escaped, NULL); + + g_free(name); + g_free(dir); + g_free(escaped); + return result; +} + GtkWidget *get_webkit(PurpleConversation *conv) { PidginWebkit *gx; @@ -677,7 +753,7 @@ template_html_len + header_html_len, header, footer); - g_signal_connect(G_OBJECT(webkit), "navigation-requested", + g_signal_connect(G_OBJECT(webkit), "navigation-policy-decision-requested", G_CALLBACK(webkit_navigation_requested_cb), gx); g_signal_connect(G_OBJECT(webkit), "load-started", G_CALLBACK(webkit_load_started_cb), gx); @@ -689,8 +765,19 @@ pendscript = g_new0(pending_script, 1); g_hash_table_insert(pending_scripts, webkit, pendscript); - webkit_web_view_load_string(WEBKIT_WEB_VIEW(webkit), template, - "text/html", "UTF-8", template_path); + char *dir = get_dir_for_tempfile(conv); + mkdir(dir, 0755); + g_free(dir); + + char *filename = get_full_path_for_tempfile(conv); + FILE *fp = fopen(filename, "w"); + fwrite(template, strlen(template), sizeof (char), fp); + fclose(fp); + g_free(filename); + + char *url = get_file_uri_for_tempfile(conv); + webkit_web_view_open(WEBKIT_WEB_VIEW(webkit), url); + g_free(url); gx->webkit = webkit;