Use midori_uri_parse in Addons to handle installation

This commit is contained in:
Christian Dywan 2011-10-28 23:53:14 +02:00
parent 5244bda781
commit 4d872505fb

View file

@ -105,14 +105,13 @@ addons_install_response (GtkWidget* infobar,
const gchar* uri = midori_view_get_display_uri (view); const gchar* uri = midori_view_get_display_uri (view);
if (uri && *uri) if (uri && *uri)
{ {
gchar** split_uri; gchar* hostname, *path;
gchar* path, *filename, *hostname, *dest_path, *temp_uri, *folder_path; gchar* dest_uri, *filename, *dest_path, *temp_uri, *folder_path;
const gchar* folder; const gchar* folder;
WebKitNetworkRequest* request; WebKitNetworkRequest* request;
WebKitDownload* download; WebKitDownload* download;
split_uri = g_strsplit (uri, "/", -1); hostname = midori_uri_parse (uri, &path);
hostname = split_uri[2];
temp_uri = NULL; temp_uri = NULL;
filename = NULL; filename = NULL;
folder = NULL; folder = NULL;
@ -121,49 +120,50 @@ addons_install_response (GtkWidget* infobar,
folder = "scripts"; folder = "scripts";
else if (g_str_has_suffix (uri, ".user.css")) else if (g_str_has_suffix (uri, ".user.css"))
folder = "styles"; folder = "styles";
else if (!g_strcmp0 (hostname, "userscripts.org")) else if (!strcmp (hostname, "userscripts.org"))
{ {
gchar* script_id; /* http://userscripts.org/scripts/ACTION/SCRIPT_ID/NAME */
const gchar* js_script; gchar* subpage = strchr (strchr (path + 1, '/') + 1, '/');
WebKitWebView* web_view; if (subpage && subpage[0] == '/' && g_ascii_isdigit (subpage[1]))
WebKitWebFrame* web_frame;
web_view = WEBKIT_WEB_VIEW (midori_view_get_web_view (view));
web_frame = webkit_web_view_get_main_frame (web_view);
js_script = "document.getElementById('heading').childNodes[3].childNodes[1].textContent";
if (WEBKIT_IS_WEB_FRAME (web_frame))
{ {
JSContextRef js_context = webkit_web_frame_get_global_context (web_frame);
gchar* value = sokoke_js_script_eval (js_context, js_script, NULL);
if (value && *value)
filename = g_strdup_printf ("%s.user.js", value);
g_free (value);
}
folder = "scripts";
script_id = split_uri[5];
/* rewrite uri to get source js */
temp_uri = g_strdup_printf ("http://%s/scripts/source/%s.user.js",
hostname, script_id);
uri = temp_uri;
}
else if (!g_strcmp0 (hostname, "userstyles.org"))
{
gchar* subpage = split_uri[4];
folder = "styles";
if ((subpage && *subpage) && g_ascii_isdigit (subpage[0]))
{
gchar* style_id;
const gchar* js_script; const gchar* js_script;
WebKitWebView* web_view; WebKitWebView* web_view;
WebKitWebFrame* web_frame; WebKitWebFrame* web_frame;
js_script = "document.getElementById('heading').childNodes[3].childNodes[1].textContent";
web_view = WEBKIT_WEB_VIEW (midori_view_get_web_view (view)); web_view = WEBKIT_WEB_VIEW (midori_view_get_web_view (view));
web_frame = webkit_web_view_get_main_frame (web_view); web_frame = webkit_web_view_get_main_frame (web_view);
if (WEBKIT_IS_WEB_FRAME (web_frame))
{
JSContextRef js_context = webkit_web_frame_get_global_context (web_frame);
gchar* value = sokoke_js_script_eval (js_context, js_script, NULL);
if (value && *value)
filename = g_strdup_printf ("%s.user.js", value);
g_free (value);
}
/* rewrite uri to get source js */
temp_uri = g_strdup_printf ("http://%s/scripts/source/%s.user.js",
hostname, subpage + 1);
uri = temp_uri;
folder = "scripts";
}
}
else if (!strcmp (hostname, "userstyles.org"))
{
/* http://userstyles.org/styles/STYLE_ID/NAME */
gchar* subpage = strchr (path + 1, '/');
if (subpage && subpage[0] == '/' && g_ascii_isdigit (subpage[1]))
{
const gchar* js_script;
WebKitWebView* web_view;
WebKitWebFrame* web_frame;
js_script = "document.getElementById('stylish-description').innerHTML;"; js_script = "document.getElementById('stylish-description').innerHTML;";
web_view = WEBKIT_WEB_VIEW (midori_view_get_web_view (view));
web_frame = webkit_web_view_get_main_frame (web_view);
if (WEBKIT_IS_WEB_FRAME (web_frame)) if (WEBKIT_IS_WEB_FRAME (web_frame))
{ {
JSContextRef js_context = webkit_web_frame_get_global_context (web_frame); JSContextRef js_context = webkit_web_frame_get_global_context (web_frame);
@ -173,9 +173,9 @@ addons_install_response (GtkWidget* infobar,
g_free (value); g_free (value);
} }
/* rewrite uri to get css */ /* rewrite uri to get css */
style_id = split_uri[4]; temp_uri = g_strdup_printf ("http://%s/styles/%s.css", hostname, subpage + 1);
temp_uri = g_strdup_printf ("http://%s/styles/%s.css", hostname, style_id);
uri = temp_uri; uri = temp_uri;
folder = "styles";
} }
} }
@ -186,22 +186,22 @@ addons_install_response (GtkWidget* infobar,
if (!g_file_test (folder_path, G_FILE_TEST_EXISTS)) if (!g_file_test (folder_path, G_FILE_TEST_EXISTS))
katze_mkdir_with_parents (folder_path, 0700); katze_mkdir_with_parents (folder_path, 0700);
path = g_build_path (G_DIR_SEPARATOR_S, folder_path, filename, NULL); dest_path = g_build_path (G_DIR_SEPARATOR_S, folder_path, filename, NULL);
request = webkit_network_request_new (uri); request = webkit_network_request_new (uri);
download = webkit_download_new (request); download = webkit_download_new (request);
g_object_unref (request); g_object_unref (request);
dest_path = g_filename_to_uri (path, NULL, NULL); dest_uri = g_filename_to_uri (dest_path, NULL, NULL);
webkit_download_set_destination_uri (download, dest_path); webkit_download_set_destination_uri (download, dest_uri);
webkit_download_start (download); webkit_download_start (download);
g_free (filename); g_free (filename);
g_free (path); g_free (dest_uri);
g_free (temp_uri); g_free (temp_uri);
g_free (dest_path); g_free (dest_path);
g_free (folder_path); g_free (folder_path);
g_strfreev (split_uri); g_free (hostname);
} }
} }
gtk_widget_destroy (GTK_WIDGET (infobar)); gtk_widget_destroy (GTK_WIDGET (infobar));
@ -255,27 +255,26 @@ addons_notify_load_status_cb (MidoriView* view,
addons_uri_install (view, ADDONS_USER_SCRIPTS); addons_uri_install (view, ADDONS_USER_SCRIPTS);
else if (g_str_has_suffix (uri, ".user.css")) else if (g_str_has_suffix (uri, ".user.css"))
addons_uri_install (view, ADDONS_USER_STYLES); addons_uri_install (view, ADDONS_USER_STYLES);
else if (g_str_has_prefix (uri, "http://userscripts.org/scripts/")) else
{ {
gchar** split_uri = g_strsplit (uri, "/", -1); gchar* path;
gchar* subpage = split_uri[4]; gchar* hostname = midori_uri_parse (uri, &path);
if (!strcmp (hostname, "userscripts.org")
/* userscripts.org script main (with desc) and "source view" pages */ && (g_str_has_prefix (path, "/scripts/show/")
if (!g_strcmp0 (subpage, "show") || !g_strcmp0 (subpage, "review")) || g_str_has_prefix (path, "/scripts/review/")))
{
/* Main (with desc) and "source view" pages */
addons_uri_install (view, ADDONS_USER_SCRIPTS); addons_uri_install (view, ADDONS_USER_SCRIPTS);
}
g_strfreev (split_uri); else if (!strcmp (hostname, "userstyles.org")
} && g_str_has_prefix (path, "/styles/"))
else if (g_str_has_prefix (uri, "http://userstyles.org/styles/")) {
{ gchar* subpage = strchr (path + 1, '/');
gchar** split_uri = g_strsplit (uri, "/", -1); /* Main page with style description */
gchar* subpage = split_uri[4]; if (subpage && subpage[0] == '/' && g_ascii_isdigit (subpage[1]))
addons_uri_install (view, ADDONS_USER_STYLES);
/* userstyles.org style main page with style description */ }
if ((subpage && *subpage) && g_ascii_isdigit (subpage[0])) g_free (hostname);
addons_uri_install (view, ADDONS_USER_STYLES);
g_strfreev (split_uri);
} }
} }
} }