Introduce Midori.URI to unitfy all URI logic

sokoke_hostname_from_uri, sokoke_search_uri,
sokoke_hostname_to_ascii and sokoke_unescape_uri_string
are merged into Midori.URI.
midori-core.h is the official API header for Vala now.
This commit is contained in:
Christian Dywan 2011-10-19 09:53:18 +02:00
parent 3f77b9f300
commit 77428fe44c
12 changed files with 171 additions and 260 deletions

View file

@ -13,6 +13,7 @@
#include "katze-utils.h" #include "katze-utils.h"
#include "katze-array.h" #include "katze-array.h"
#include "midori-core.h"
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
@ -1518,12 +1519,7 @@ katze_uri_entry_changed_cb (GtkWidget* entry,
GtkWidget* other_widget) GtkWidget* other_widget)
{ {
const gchar* uri = gtk_entry_get_text (GTK_ENTRY (entry)); const gchar* uri = gtk_entry_get_text (GTK_ENTRY (entry));
gboolean valid = g_str_has_prefix (uri, "http://") gboolean valid = midori_uri_is_location (uri);
|| g_str_has_prefix (uri, "https://")
|| g_str_has_prefix (uri, "file://")
|| g_str_has_prefix (uri, "data:")
|| g_str_has_prefix (uri, "about:")
|| g_str_has_prefix (uri, "javascript:");
if (*uri && !valid) if (*uri && !valid)
{ {
GdkColor bg_color = { 0 }; GdkColor bg_color = { 0 };
@ -1540,7 +1536,7 @@ katze_uri_entry_changed_cb (GtkWidget* entry,
} }
if (other_widget != NULL) if (other_widget != NULL)
gtk_widget_set_sensitive (other_widget, *uri && valid); gtk_widget_set_sensitive (other_widget, valid);
} }
/** /**

126
katze/midori-uri.vala Normal file
View file

@ -0,0 +1,126 @@
/*
Copyright (C) 2011 Christian Dywan <christian@twotoasts.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See the file COPYING for the full license text.
*/
namespace GLib {
extern static string hostname_to_unicode (string hostname);
extern static string hostname_to_ascii (string hostname);
}
namespace Midori {
public class URI : Object {
public static string parse (string? uri, out string path) {
/* path may be null.
If there's no hostname, the original URI is returned */
if (uri == null)
return uri;
unowned string? hostname = uri.chr (-1, '/');
if (hostname == null || hostname[1] != '/')
return uri;
hostname = hostname.offset (2);
if (&path != null) {
if ((path = hostname.chr (-1, '/')) != null)
return hostname.split ("/")[0];
}
return hostname;
}
public static string to_ascii (string uri) {
/* Convert hostname to ASCII. */
string? proto = null;
if (uri.chr (-1, '/') != null && uri.chr (-1, ':') != null)
proto = uri.split ("://")[0];
string? path = null;
string hostname = parse (uri, out path);
string encoded = hostname_to_ascii (hostname);
if (encoded != null) {
return (proto ?? "")
+ (proto != null ? "://" : "")
+ encoded + path;
}
return uri;
}
public static string unescape (string uri) {
/* Unescape, pass through + and %20 */
if (uri.chr (-1, '%') != null || uri.chr (-1, ' ') != null) {
/* Preserve %20 for pasting URLs into other windows */
string? unescaped = GLib.Uri.unescape_string (uri, "+");
if (unescaped == null)
return uri;
return unescaped.replace (" ", "%20");
}
return uri;
}
public static string format_for_display (string? uri) {
/* Percent-decode and decode puniycode for user display */
if (uri != null && uri.has_prefix ("http://")) {
string unescaped = unescape (uri);
if (unescaped == null)
return uri;
else if (!unescaped.validate ())
return uri;
string path;
string hostname = parse (unescaped, out path);
string decoded = hostname_to_unicode (hostname);
if (decoded != null)
return "http://" + decoded + path;
return unescaped;
}
return uri;
}
public static string for_search (string? uri, string keywords) {
/* Take a search engine URI and insert specified keywords.
Keywords are percent-encoded. If the uri contains a %s
the keywords are inserted there, otherwise appended. */
if (uri == null)
return keywords;
string escaped = GLib.Uri.escape_string (keywords, ":/", true);
if (uri.str ("%s") != null)
return uri.printf (escaped);
return uri + escaped;
}
public static bool is_blank (string? uri) {
return !(uri != null && uri != "" && !uri.has_prefix ("about:"));
}
public static bool is_resource (string? uri) {
return uri != null
&& (uri.has_prefix ("http://")
|| (uri.has_prefix ("data:") && uri.chr (-1, ';') != null)
|| uri.has_prefix ("https://"));
}
public static bool is_location (string? uri) {
/* file:// is not considered a location for security reasons */
return uri != null
&& ((uri.str ("://") != null && uri.chr (-1, ' ') == null)
|| uri.has_prefix ("about:")
|| (uri.has_prefix ("data:") && uri.chr (-1, ';') != null)
|| (uri.has_prefix ("geo:") && uri.chr (-1, ',') != null)
|| uri.has_prefix ("javascript:"));
}
public static bool is_email (string? uri) {
return uri != null
&& (uri.chr (-1, '@') != null || uri.has_prefix ("mailto:"))
/* :// and @ together would mean login credentials */
&& uri.str ("://") == null;
}
public static bool is_ip_address (string? uri) {
/* Quick check for IPv4 or IPv6, no validation.
FIXME: Schemes are not handled
hostname_is_ip_address () is not used because
we'd have to separate the path from the URI first. */
return uri != null && uri[0].isdigit ()
&& (uri.chr (4, '.') != null || uri.chr (4, ':') != null);
}
public static bool is_valid (string? uri) {
return uri != null
&& uri.chr (-1, ' ') == null
&& (URI.is_location (uri) || uri.chr (-1, '.') != null);
}
}
}

View file

@ -20,6 +20,7 @@
#include "midori-extensions.h" #include "midori-extensions.h"
#include "midori-history.h" #include "midori-history.h"
#include "midori-transfers.h" #include "midori-transfers.h"
#include <midori/midori-core.h>
#include "sokoke.h" #include "sokoke.h"
@ -1406,7 +1407,7 @@ midori_load_session (gpointer data)
item = katze_array_get_nth_item (_session, 0); item = katze_array_get_nth_item (_session, 0);
} }
midori_browser_set_current_page (browser, current); midori_browser_set_current_page (browser, current);
if (!g_strcmp0 (katze_item_get_uri (item), "")) if (midori_uri_is_blank (katze_item_get_uri (item)))
midori_browser_activate_action (browser, "Location"); midori_browser_activate_action (browser, "Location");
g_object_unref (settings); g_object_unref (settings);
@ -1528,9 +1529,9 @@ midori_prepare_uri (const gchar *uri)
uri_ready = sokoke_magic_uri (uri); uri_ready = sokoke_magic_uri (uri);
if (uri_ready) if (uri_ready)
return sokoke_uri_to_ascii (uri_ready); return midori_uri_to_ascii (uri_ready);
return sokoke_uri_to_ascii (uri); return midori_uri_to_ascii (uri);
} }
#ifdef HAVE_SIGNAL_H #ifdef HAVE_SIGNAL_H

View file

@ -25,6 +25,7 @@
#include "midori-findbar.h" #include "midori-findbar.h"
#include "midori-transferbar.h" #include "midori-transferbar.h"
#include "midori-platform.h" #include "midori-platform.h"
#include "midori-core.h"
#include "gtkiconentry.h" #include "gtkiconentry.h"
#include "marshal.h" #include "marshal.h"
@ -382,7 +383,7 @@ _midori_browser_set_statusbar_text (MidoriBrowser* browser,
gboolean is_location = widget && GTK_IS_ENTRY (widget) gboolean is_location = widget && GTK_IS_ENTRY (widget)
&& GTK_IS_ALIGNMENT (gtk_widget_get_parent (widget)); && GTK_IS_ALIGNMENT (gtk_widget_get_parent (widget));
katze_assign (browser->statusbar_text, sokoke_format_uri_for_display (text)); katze_assign (browser->statusbar_text, midori_uri_format_for_display (text));
if (!browser->show_statusbar && !is_location) if (!browser->show_statusbar && !is_location)
{ {
@ -611,7 +612,7 @@ midori_view_notify_title_cb (GtkWidget* widget,
proxy = midori_view_get_proxy_item (view); proxy = midori_view_get_proxy_item (view);
proxy_uri = katze_item_get_uri (proxy); proxy_uri = katze_item_get_uri (proxy);
if (proxy_uri && *proxy_uri && proxy_uri[1] && if (proxy_uri && *proxy_uri && proxy_uri[1] &&
!g_str_has_prefix (proxy_uri, "about:") && !midori_uri_is_blank (proxy_uri) &&
(katze_item_get_meta_integer (proxy, "history-step") == -1)) (katze_item_get_meta_integer (proxy, "history-step") == -1))
{ {
if (!katze_item_get_meta_boolean (proxy, "dont-write-history")) if (!katze_item_get_meta_boolean (proxy, "dont-write-history"))
@ -621,7 +622,7 @@ midori_view_notify_title_cb (GtkWidget* widget,
} }
} }
else if (katze_item_get_name (proxy) && else if (katze_item_get_name (proxy) &&
!g_str_has_prefix (proxy_uri, "about:") && !midori_uri_is_blank (proxy_uri) &&
(katze_item_get_meta_integer (proxy, "history-step") == 1)) (katze_item_get_meta_integer (proxy, "history-step") == 1))
{ {
midori_browser_update_history_title (browser, proxy); midori_browser_update_history_title (browser, proxy);
@ -3788,7 +3789,7 @@ _action_location_submit_uri (GtkAction* action,
keywords = stripped_uri; keywords = stripped_uri;
search_uri = browser->location_entry_search; search_uri = browser->location_entry_search;
} }
new_uri = sokoke_search_uri (search_uri, keywords); new_uri = midori_uri_for_search (search_uri, keywords);
if (browser->history != NULL) if (browser->history != NULL)
{ {
@ -3937,7 +3938,7 @@ _action_search_submit (GtkAction* action,
else /* The location entry search is our fallback */ else /* The location entry search is our fallback */
url = browser->location_entry_search; url = browser->location_entry_search;
search = sokoke_search_uri (url, keywords); search = midori_uri_for_search (url, keywords);
if (new_tab) if (new_tab)
midori_browser_add_uri (browser, search); midori_browser_add_uri (browser, search);
@ -3967,7 +3968,7 @@ _action_search_activate (GtkAction* action,
/* Load default search engine in current tab */ /* Load default search engine in current tab */
uri = browser->location_entry_search; uri = browser->location_entry_search;
search = sokoke_search_uri (uri ? uri : "", ""); search = midori_uri_for_search (uri ? uri : "", "");
midori_browser_set_current_uri (browser, search); midori_browser_set_current_uri (browser, search);
gtk_widget_grab_focus (midori_browser_get_current_tab (browser)); gtk_widget_grab_focus (midori_browser_get_current_tab (browser));
g_free (search); g_free (search);
@ -7140,7 +7141,7 @@ midori_browser_add_item (MidoriBrowser* browser,
/* Blank pages should not be delayed */ /* Blank pages should not be delayed */
if (katze_item_get_meta_integer (item, "delay") > 0 if (katze_item_get_meta_integer (item, "delay") > 0
&& strcmp (uri, "about:blank") != 0 && !midori_uri_is_blank (uri)
&& strncmp (uri, "pause:", 6) != 0) && strncmp (uri, "pause:", 6) != 0)
{ {
gchar* new_uri = g_strdup_printf ("pause:%s", uri); gchar* new_uri = g_strdup_printf ("pause:%s", uri);

View file

@ -12,13 +12,13 @@
#include "midori-locationaction.h" #include "midori-locationaction.h"
#include "gtk3-compat.h"
#include "gtkiconentry.h" #include "gtkiconentry.h"
#include "marshal.h" #include "marshal.h"
#include "sokoke.h" #include "sokoke.h"
#include "midori-browser.h" #include "midori-browser.h"
#include "midori-searchaction.h" #include "midori-searchaction.h"
#include "midori-platform.h" #include "midori-platform.h"
#include <midori/midori-core.h>
#include <string.h> #include <string.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
@ -508,7 +508,7 @@ midori_location_action_popup_timeout_cb (gpointer data)
gchar* title; gchar* title;
GdkPixbuf* icon; GdkPixbuf* icon;
uri = sokoke_search_uri (katze_item_get_uri (item), action->key); uri = midori_uri_for_search (katze_item_get_uri (item), action->key);
title = g_strdup_printf (_("Search with %s"), katze_item_get_name (item)); title = g_strdup_printf (_("Search with %s"), katze_item_get_name (item));
icon = midori_search_action_get_icon (item, action->treeview, NULL, FALSE); icon = midori_search_action_get_icon (item, action->treeview, NULL, FALSE);
gtk_list_store_insert_with_values (store, NULL, matches + i, gtk_list_store_insert_with_values (store, NULL, matches + i,
@ -1079,7 +1079,7 @@ midori_location_entry_render_text_cb (GtkCellLayout* layout,
keys = g_strsplit_set (key, " %", -1); keys = g_strsplit_set (key, " %", -1);
g_free (key); g_free (key);
uri_temp = sokoke_uri_unescape_string (uri_escaped); uri_temp = midori_uri_unescape (uri_escaped);
g_free (uri_escaped); g_free (uri_escaped);
uri = g_strescape (uri_temp, NULL); uri = g_strescape (uri_temp, NULL);
g_free (uri_temp); g_free (uri_temp);

View file

@ -16,6 +16,7 @@
#include "marshal.h" #include "marshal.h"
#include "sokoke.h" #include "sokoke.h"
#include "midori-platform.h" #include "midori-platform.h"
#include "midori-core.h"
#include <string.h> #include <string.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
@ -407,11 +408,11 @@ midori_search_action_get_icon (KatzeItem* item,
const gchar** icon_name, const gchar** icon_name,
gboolean in_entry) gboolean in_entry)
{ {
const gchar* icon; const gchar* icon = katze_item_get_uri (item);
GdkScreen* screen; GdkScreen* screen;
GtkIconTheme* icon_theme; GtkIconTheme* icon_theme;
if ((icon = katze_item_get_uri (item)) && (g_strstr_len (icon, 8, "://"))) if (midori_uri_is_resource (icon))
return katze_load_cached_icon (icon, widget); return katze_load_cached_icon (icon, widget);
if (icon_name == NULL) if (icon_name == NULL)

View file

@ -20,6 +20,7 @@
#include "midori-browser.h" #include "midori-browser.h"
#include "midori-searchaction.h" #include "midori-searchaction.h"
#include "midori-platform.h" #include "midori-platform.h"
#include "midori-core.h"
#include "marshal.h" #include "marshal.h"
#include "sokoke.h" #include "sokoke.h"
@ -1064,7 +1065,7 @@ webkit_web_view_load_committed_cb (WebKitWebView* web_view,
if (g_strcmp0 (uri, katze_item_get_uri (view->item))) if (g_strcmp0 (uri, katze_item_get_uri (view->item)))
{ {
katze_assign (view->uri, sokoke_format_uri_for_display (uri)); katze_assign (view->uri, midori_uri_format_for_display (uri));
katze_item_set_uri (view->item, uri); katze_item_set_uri (view->item, uri);
} }
@ -1362,7 +1363,7 @@ midori_view_web_view_database_quota_exceeded_cb (WebKitWebView* web_view,
MidoriView* view) MidoriView* view)
{ {
const gchar* uri = webkit_web_frame_get_uri (web_frame); const gchar* uri = webkit_web_frame_get_uri (web_frame);
const gchar* hostname = sokoke_hostname_from_uri (uri, NULL); const gchar* hostname = midori_uri_parse (uri, NULL);
gchar* message = g_strdup_printf (_("%s wants to save an HTML5 database."), gchar* message = g_strdup_printf (_("%s wants to save an HTML5 database."),
hostname && *hostname ? hostname : uri); hostname && *hostname ? hostname : uri);
midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION, message, midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION, message,
@ -1391,7 +1392,7 @@ midori_view_web_view_geolocation_decision_cb (WebKitWebView* w
MidoriView* view) MidoriView* view)
{ {
const gchar* uri = webkit_web_frame_get_uri (web_frame); const gchar* uri = webkit_web_frame_get_uri (web_frame);
const gchar* hostname = sokoke_hostname_from_uri (uri, NULL); const gchar* hostname = midori_uri_parse (uri, NULL);
gchar* message = g_strdup_printf (_("%s wants to know your location."), gchar* message = g_strdup_printf (_("%s wants to know your location."),
hostname && *hostname ? hostname : uri); hostname && *hostname ? hostname : uri);
midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION, midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION,
@ -1774,12 +1775,12 @@ gtk_widget_button_press_event_cb (WebKitWebView* web_view,
{ {
gchar* search = katze_object_get_string ( gchar* search = katze_object_get_string (
view->settings, "location-entry-search"); view->settings, "location-entry-search");
new_uri = sokoke_search_uri (search, uri); new_uri = midori_uri_for_search (search, uri);
g_free (search); g_free (search);
} }
katze_assign (uri, new_uri); katze_assign (uri, new_uri);
} }
else if (!strstr (uri, "://")) else if (!midori_uri_is_location (uri))
{ {
g_free (uri); g_free (uri);
return FALSE; return FALSE;
@ -1935,7 +1936,7 @@ gtk_widget_key_press_event_cb (WebKitWebView* web_view,
view->find_links, event->keyval == GDK_KEY_Return view->find_links, event->keyval == GDK_KEY_Return
); );
result = sokoke_js_script_eval (js_context, script, NULL); result = sokoke_js_script_eval (js_context, script, NULL);
if (result && strstr (result, "://")) if (midori_uri_is_location (result))
{ {
if (MIDORI_MOD_NEW_TAB (event->state)) if (MIDORI_MOD_NEW_TAB (event->state))
{ {
@ -2157,7 +2158,7 @@ midori_web_view_menu_search_web_activate_cb (GtkWidget* widget,
else else
g_object_get (view->settings, "location-entry-search", g_object_get (view->settings, "location-entry-search",
&search, NULL); &search, NULL);
uri = sokoke_search_uri (search, view->selected_text); uri = midori_uri_for_search (search, view->selected_text);
g_free (search); g_free (search);
g_signal_emit (view, signals[NEW_TAB], 0, uri, g_signal_emit (view, signals[NEW_TAB], 0, uri,
@ -2489,10 +2490,9 @@ midori_view_populate_popup (MidoriView* view,
G_CALLBACK (midori_web_view_menu_search_web_activate_cb), widget); G_CALLBACK (midori_web_view_menu_search_web_activate_cb), widget);
g_strstrip (view->selected_text); g_strstrip (view->selected_text);
if (view->selected_text && !strchr (view->selected_text, ' ') if (midori_uri_is_valid (view->selected_text))
&& (strchr (view->selected_text, '.') || g_strstr_len (view->selected_text, 9, "://")))
{ {
if (strchr (view->selected_text, '@')) if (midori_uri_is_email (view->selected_text))
{ {
gchar* text = g_strdup_printf (_("Send a message to %s"), view->selected_text); gchar* text = g_strdup_printf (_("Send a message to %s"), view->selected_text);
menuitem = midori_view_insert_menu_item (menu_shell, -1, menuitem = midori_view_insert_menu_item (menu_shell, -1,
@ -3866,7 +3866,7 @@ midori_view_set_uri (MidoriView* view,
} }
/* This is not prefectly elegant, but creating /* This is not prefectly elegant, but creating
special pages inline is the simplest solution. */ special pages inline is the simplest solution. */
else if (g_str_has_prefix (uri, "error:") || g_str_has_prefix (uri, "about:")) else if (g_str_has_prefix (uri, "error:") || midori_uri_is_blank (uri))
{ {
data = NULL; data = NULL;
if (!strncmp (uri, "error:nodocs ", 13)) if (!strncmp (uri, "error:nodocs ", 13))
@ -4009,7 +4009,7 @@ midori_view_set_uri (MidoriView* view,
} }
else else
{ {
katze_assign (view->uri, sokoke_format_uri_for_display (uri)); katze_assign (view->uri, midori_uri_format_for_display (uri));
katze_item_set_uri (view->item, uri); katze_item_set_uri (view->item, uri);
katze_item_set_meta_integer (view->item, "delay", -1); katze_item_set_meta_integer (view->item, "delay", -1);
g_object_notify (G_OBJECT (view), "uri"); g_object_notify (G_OBJECT (view), "uri");
@ -4027,12 +4027,9 @@ midori_view_set_uri (MidoriView* view,
gboolean gboolean
midori_view_is_blank (MidoriView* view) midori_view_is_blank (MidoriView* view)
{ {
const gchar* uri;
g_return_val_if_fail (MIDORI_IS_VIEW (view), TRUE); g_return_val_if_fail (MIDORI_IS_VIEW (view), TRUE);
uri = midori_view_get_display_uri (view); return midori_uri_is_blank (midori_view_get_display_uri (view));
return uri[0] == '\0' || g_str_has_prefix (uri, "about:");
} }
/** /**

View file

@ -25,6 +25,7 @@
#include "midori-viewable.h" #include "midori-viewable.h"
#include "midori-websettings.h" #include "midori-websettings.h"
#include "midori-platform.h" #include "midori-platform.h"
#include <midori/midori-core.h> /* Vala API */
/* For convenience, include localization header */ /* For convenience, include localization header */
#include <glib/gi18n-lib.h> #include <glib/gi18n-lib.h>

View file

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2007-2009 Christian Dywan <christian@twotoasts.de> Copyright (C) 2007-2011 Christian Dywan <christian@twotoasts.de>
Copyright (C) 2009 Dale Whittaker <dayul@users.sf.net> Copyright (C) 2009 Dale Whittaker <dayul@users.sf.net>
Copyright (C) 2009 Alexander Butenko <a.butenka@gmail.com> Copyright (C) 2009 Alexander Butenko <a.butenka@gmail.com>
@ -18,6 +18,7 @@
#endif #endif
#include "midori-stock.h" #include "midori-stock.h"
#include "midori-core.h"
#if HAVE_UNISTD_H #if HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
@ -570,124 +571,6 @@ sokoke_spawn_app (const gchar* uri,
g_free (command); g_free (command);
} }
/**
* sokoke_hostname_from_uri:
* @uri: an URI string
* @path: location of a string, or %NULL
*
* Returns the hostname of the specified URI.
*
* If there is a path, it is stored in @path.
*
* Return value: a newly allocated hostname
**/
gchar*
sokoke_hostname_from_uri (const gchar* uri,
gchar** path)
{
gchar* hostname;
if ((hostname = strchr (uri, '/')))
{
gchar* pathname;
if (hostname[1] == '/')
hostname += 2;
if ((pathname = strchr (hostname, '/')))
{
if (path != NULL)
*path = pathname;
return g_strndup (hostname, pathname - hostname);
}
else
return g_strdup (hostname);
}
return g_strdup (uri);
}
/**
* sokoke_uri_to_ascii:
* @uri: an URI string
*
* The specified URI is parsed and the hostname
* part of it is encoded if it is not ASCII.
*
* If no IDN support is available at compile time,
* the URI will be returned unaltered.
*
* Return value: a newly allocated URI
**/
gchar*
sokoke_uri_to_ascii (const gchar* uri)
{
gchar* proto = NULL;
gchar* path = NULL;
gchar* hostname;
gchar* encoded;
if (strchr (uri, '/') && (proto = strchr (uri, ':')))
{
gulong offset;
gchar* buffer;
offset = g_utf8_pointer_to_offset (uri, proto);
buffer = g_malloc0 (offset + 1);
g_utf8_strncpy (buffer, uri, offset);
proto = buffer;
}
hostname = sokoke_hostname_from_uri (uri, &path);
encoded = g_hostname_to_ascii (hostname);
if (encoded)
{
gchar* res = g_strconcat (proto ? proto : "", proto ? "://" : "",
encoded, path, NULL);
g_free (encoded);
return res;
}
g_free (hostname);
return g_strdup (uri);
}
static gchar*
sokoke_idn_to_punycode (gchar* uri)
{
return uri;
}
/**
* sokoke_search_uri:
* @uri: a search URI with or without %s
* @keywords: keywords
*
* Takes a search engine URI and inserts the specified
* keywords. The @keywords are percent encoded. If the
* search URI contains a %s they keywords are inserted
* in that place, otherwise appended to the URI.
*
* Return value: a newly allocated search URI
**/
gchar* sokoke_search_uri (const gchar* uri,
const gchar* keywords)
{
gchar* escaped;
gchar* search;
g_return_val_if_fail (keywords != NULL, NULL);
if (!uri)
return g_strdup (keywords);
escaped = g_uri_escape_string (keywords, ":/", TRUE);
if (strstr (uri, "%s"))
search = g_strdup_printf (uri, escaped);
else
search = g_strconcat (uri, escaped, NULL);
g_free (escaped);
return search;
}
static void static void
sokoke_resolve_hostname_cb (SoupAddress *address, sokoke_resolve_hostname_cb (SoupAddress *address,
guint status, guint status,
@ -763,13 +646,6 @@ sokoke_magic_uri (const gchar* uri)
g_return_val_if_fail (uri, NULL); g_return_val_if_fail (uri, NULL);
/* Just return if it's a javascript: or mailto: uri */
if (!strncmp (uri, "javascript:", 11)
|| !strncmp (uri, "mailto:", 7)
|| sokoke_external_uri (uri)
|| !strncmp (uri, "data:", 5)
|| !strncmp (uri, "about:", 6))
return g_strdup (uri);
/* Add file:// if we have a local path */ /* Add file:// if we have a local path */
if (g_path_is_absolute (uri)) if (g_path_is_absolute (uri))
return g_strconcat ("file://", uri, NULL); return g_strconcat ("file://", uri, NULL);
@ -800,12 +676,9 @@ sokoke_magic_uri (const gchar* uri)
g_free (longitude); g_free (longitude);
return geo; return geo;
} }
/* Do we have a protocol? */ if (midori_uri_is_location (uri) || sokoke_external_uri (uri))
if (g_strstr_len (uri, 8, "://")) return g_strdup (uri);
return sokoke_idn_to_punycode (g_strdup (uri)); if (midori_uri_is_ip_address (uri))
/* Do we have an IP address? */
if (g_ascii_isdigit (uri[0]) && g_strstr_len (uri, 4, "."))
return g_strconcat ("http://", uri, NULL); return g_strconcat ("http://", uri, NULL);
search = NULL; search = NULL;
if (!strchr (uri, ' ') && if (!strchr (uri, ' ') &&
@ -833,75 +706,6 @@ sokoke_magic_uri (const gchar* uri)
return NULL; return NULL;
} }
/**
* sokoke_uri_unescape_string:
* @uri: an URI string
*
* Unescape @uri if needed, and pass through '+' and '%20'.
*
* Return value: a newly allocated URI
**/
gchar*
sokoke_uri_unescape_string (const gchar* uri)
{
if (strchr (uri,'%') || strchr (uri, ' '))
{
/* Preserve %20 for pasting URLs into other windows */
gchar* unescaped = g_uri_unescape_string (uri, "+");
if (!unescaped)
return g_strdup (uri);
gchar* spaced = sokoke_replace_variables (unescaped, " ", "%20", NULL);
g_free (unescaped);
return spaced;
}
return g_strdup (uri);
}
/**
* sokoke_format_uri_for_display:
* @uri: an URI string
*
* Formats an URI for display, for instance by converting
* percent encoded characters and by decoding punycode.
*
* Return value: a newly allocated URI
**/
gchar*
sokoke_format_uri_for_display (const gchar* uri)
{
if (uri && g_str_has_prefix (uri, "http://"))
{
gchar* unescaped = sokoke_uri_unescape_string (uri);
gchar* path = NULL;
gchar* hostname;
gchar* decoded;
if (!unescaped)
return g_strdup (uri);
else if (!g_utf8_validate (unescaped, -1, NULL))
{
g_free (unescaped);
return g_strdup (uri);
}
hostname = sokoke_hostname_from_uri (unescaped, &path);
decoded = g_hostname_to_unicode (hostname);
if (decoded)
{
gchar* result = g_strconcat ("http://", decoded, path, NULL);
g_free (unescaped);
g_free (decoded);
g_free (hostname);
return result;
}
g_free (hostname);
return unescaped;
}
return g_strdup (uri);
}
void void
sokoke_combo_box_add_strings (GtkComboBox* combobox, sokoke_combo_box_add_strings (GtkComboBox* combobox,
const gchar* label_first, ...) const gchar* label_first, ...)

View file

@ -49,28 +49,12 @@ void
sokoke_spawn_app (const gchar* uri, sokoke_spawn_app (const gchar* uri,
gboolean inherit_config); gboolean inherit_config);
gchar* sokoke_search_uri (const gchar* uri,
const gchar* keywords);
gchar*
sokoke_hostname_from_uri (const gchar* uri,
gchar** path);
gchar*
sokoke_uri_to_ascii (const gchar* uri);
gboolean gboolean
sokoke_external_uri (const gchar* uri); sokoke_external_uri (const gchar* uri);
gchar* gchar*
sokoke_magic_uri (const gchar* uri); sokoke_magic_uri (const gchar* uri);
gchar*
sokoke_uri_unescape_string (const gchar* uri);
gchar*
sokoke_format_uri_for_display (const gchar* uri);
void void
sokoke_combo_box_add_strings (GtkComboBox* combobox, sokoke_combo_box_add_strings (GtkComboBox* combobox,
const gchar* label_first, const gchar* label_first,

View file

@ -15,8 +15,7 @@
#include "midori-extension.h" #include "midori-extension.h"
#include "midori-stock.h" #include "midori-stock.h"
#include "midori-viewable.h" #include "midori-viewable.h"
#include "midori-core.h"
#include "midori-extensions-column.c"
#include "sokoke.h" #include "sokoke.h"
#include <glib/gi18n.h> #include <glib/gi18n.h>

View file

@ -15,6 +15,7 @@
#endif #endif
#include "sokoke.h" #include "sokoke.h"
#include <midori/midori-core.h>
#define SM "http://www.searchmash.com/search/" #define SM "http://www.searchmash.com/search/"
@ -76,7 +77,7 @@ test_input (const gchar* input,
} }
g_strfreev (parts); g_strfreev (parts);
uri = keywords ? sokoke_search_uri (search_uri, keywords) : NULL; uri = keywords ? midori_uri_for_search (search_uri, keywords) : NULL;
g_free (keywords); g_free (keywords);
} }
@ -134,7 +135,7 @@ magic_uri_idn (void)
for (i = 0; i < G_N_ELEMENTS (items); i++) for (i = 0; i < G_N_ELEMENTS (items); i++)
{ {
gchar* result = sokoke_uri_to_ascii (items[i].before); gchar* result = midori_uri_to_ascii (items[i].before);
const gchar* after = items[i].after ? items[i].after : items[i].before; const gchar* after = items[i].after ? items[i].after : items[i].before;
sokoke_assert_str_equal (items[i].before, result, after); sokoke_assert_str_equal (items[i].before, result, after);
g_free (result); g_free (result);
@ -236,7 +237,7 @@ magic_uri_format (void)
for (i = 0; i < G_N_ELEMENTS (items); i++) for (i = 0; i < G_N_ELEMENTS (items); i++)
{ {
gchar* result = sokoke_format_uri_for_display (items[i].before); gchar* result = midori_uri_format_for_display (items[i].before);
const gchar* after = items[i].after ? items[i].after : items[i].before; const gchar* after = items[i].after ? items[i].after : items[i].before;
sokoke_assert_str_equal (items[i].before, result, after); sokoke_assert_str_equal (items[i].before, result, after);
g_free (result); g_free (result);