#include "katze-utils.h"
#include "katze-array.h"
+#include "midori-core.h"
#include <glib/gstdio.h>
#include <glib/gi18n.h>
GtkWidget* other_widget)
{
const gchar* uri = gtk_entry_get_text (GTK_ENTRY (entry));
- gboolean valid = g_str_has_prefix (uri, "http://")
- || 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:");
+ gboolean valid = midori_uri_is_location (uri);
if (*uri && !valid)
{
GdkColor bg_color = { 0 };
}
if (other_widget != NULL)
- gtk_widget_set_sensitive (other_widget, *uri && valid);
+ gtk_widget_set_sensitive (other_widget, valid);
}
/**
--- /dev/null
+/*
+ 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);
+ }
+ }
+}
#include "midori-extensions.h"
#include "midori-history.h"
#include "midori-transfers.h"
+#include <midori/midori-core.h>
#include "sokoke.h"
item = katze_array_get_nth_item (_session, 0);
}
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");
g_object_unref (settings);
uri_ready = sokoke_magic_uri (uri);
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
#include "midori-findbar.h"
#include "midori-transferbar.h"
#include "midori-platform.h"
+#include "midori-core.h"
#include "gtkiconentry.h"
#include "marshal.h"
gboolean is_location = widget && GTK_IS_ENTRY (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)
{
proxy = midori_view_get_proxy_item (view);
proxy_uri = katze_item_get_uri (proxy);
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))
{
if (!katze_item_get_meta_boolean (proxy, "dont-write-history"))
}
}
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))
{
midori_browser_update_history_title (browser, proxy);
keywords = stripped_uri;
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)
{
else /* The location entry search is our fallback */
url = browser->location_entry_search;
- search = sokoke_search_uri (url, keywords);
+ search = midori_uri_for_search (url, keywords);
if (new_tab)
midori_browser_add_uri (browser, search);
/* Load default search engine in current tab */
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);
gtk_widget_grab_focus (midori_browser_get_current_tab (browser));
g_free (search);
/* Blank pages should not be delayed */
if (katze_item_get_meta_integer (item, "delay") > 0
- && strcmp (uri, "about:blank") != 0
+ && !midori_uri_is_blank (uri)
&& strncmp (uri, "pause:", 6) != 0)
{
gchar* new_uri = g_strdup_printf ("pause:%s", uri);
#include "midori-locationaction.h"
-#include "gtk3-compat.h"
#include "gtkiconentry.h"
#include "marshal.h"
#include "sokoke.h"
#include "midori-browser.h"
#include "midori-searchaction.h"
#include "midori-platform.h"
+#include <midori/midori-core.h>
#include <string.h>
#include <glib/gi18n.h>
gchar* title;
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));
icon = midori_search_action_get_icon (item, action->treeview, NULL, FALSE);
gtk_list_store_insert_with_values (store, NULL, matches + i,
keys = g_strsplit_set (key, " %", -1);
g_free (key);
- uri_temp = sokoke_uri_unescape_string (uri_escaped);
+ uri_temp = midori_uri_unescape (uri_escaped);
g_free (uri_escaped);
uri = g_strescape (uri_temp, NULL);
g_free (uri_temp);
#include "marshal.h"
#include "sokoke.h"
#include "midori-platform.h"
+#include "midori-core.h"
#include <string.h>
#include <glib/gi18n.h>
const gchar** icon_name,
gboolean in_entry)
{
- const gchar* icon;
+ const gchar* icon = katze_item_get_uri (item);
GdkScreen* screen;
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);
if (icon_name == NULL)
#include "midori-browser.h"
#include "midori-searchaction.h"
#include "midori-platform.h"
+#include "midori-core.h"
#include "marshal.h"
#include "sokoke.h"
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);
}
MidoriView* view)
{
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."),
hostname && *hostname ? hostname : uri);
midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION, message,
MidoriView* view)
{
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."),
hostname && *hostname ? hostname : uri);
midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION,
{
gchar* search = katze_object_get_string (
view->settings, "location-entry-search");
- new_uri = sokoke_search_uri (search, uri);
+ new_uri = midori_uri_for_search (search, uri);
g_free (search);
}
katze_assign (uri, new_uri);
}
- else if (!strstr (uri, "://"))
+ else if (!midori_uri_is_location (uri))
{
g_free (uri);
return FALSE;
view->find_links, event->keyval == GDK_KEY_Return
);
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))
{
else
g_object_get (view->settings, "location-entry-search",
&search, NULL);
- uri = sokoke_search_uri (search, view->selected_text);
+ uri = midori_uri_for_search (search, view->selected_text);
g_free (search);
g_signal_emit (view, signals[NEW_TAB], 0, uri,
G_CALLBACK (midori_web_view_menu_search_web_activate_cb), widget);
g_strstrip (view->selected_text);
- if (view->selected_text && !strchr (view->selected_text, ' ')
- && (strchr (view->selected_text, '.') || g_strstr_len (view->selected_text, 9, "://")))
+ if (midori_uri_is_valid (view->selected_text))
{
- 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);
menuitem = midori_view_insert_menu_item (menu_shell, -1,
}
/* This is not prefectly elegant, but creating
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;
if (!strncmp (uri, "error:nodocs ", 13))
}
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_meta_integer (view->item, "delay", -1);
g_object_notify (G_OBJECT (view), "uri");
gboolean
midori_view_is_blank (MidoriView* view)
{
- const gchar* uri;
-
g_return_val_if_fail (MIDORI_IS_VIEW (view), TRUE);
- uri = midori_view_get_display_uri (view);
- return uri[0] == '\0' || g_str_has_prefix (uri, "about:");
+ return midori_uri_is_blank (midori_view_get_display_uri (view));
}
/**
#include "midori-viewable.h"
#include "midori-websettings.h"
#include "midori-platform.h"
+#include <midori/midori-core.h> /* Vala API */
/* For convenience, include localization header */
#include <glib/gi18n-lib.h>
/*
- 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 Alexander Butenko <a.butenka@gmail.com>
#endif
#include "midori-stock.h"
+#include "midori-core.h"
#if HAVE_UNISTD_H
#include <unistd.h>
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
sokoke_resolve_hostname_cb (SoupAddress *address,
guint status,
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 */
if (g_path_is_absolute (uri))
return g_strconcat ("file://", uri, NULL);
g_free (longitude);
return geo;
}
- /* Do we have a protocol? */
- if (g_strstr_len (uri, 8, "://"))
- return sokoke_idn_to_punycode (g_strdup (uri));
-
- /* Do we have an IP address? */
- if (g_ascii_isdigit (uri[0]) && g_strstr_len (uri, 4, "."))
+ if (midori_uri_is_location (uri) || sokoke_external_uri (uri))
+ return g_strdup (uri);
+ if (midori_uri_is_ip_address (uri))
return g_strconcat ("http://", uri, NULL);
search = NULL;
if (!strchr (uri, ' ') &&
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
sokoke_combo_box_add_strings (GtkComboBox* combobox,
const gchar* label_first, ...)
sokoke_spawn_app (const gchar* uri,
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
sokoke_external_uri (const gchar* uri);
gchar*
sokoke_magic_uri (const gchar* uri);
-gchar*
-sokoke_uri_unescape_string (const gchar* uri);
-
-gchar*
-sokoke_format_uri_for_display (const gchar* uri);
-
void
sokoke_combo_box_add_strings (GtkComboBox* combobox,
const gchar* label_first,
#include "midori-extension.h"
#include "midori-stock.h"
#include "midori-viewable.h"
-
-#include "midori-extensions-column.c"
+#include "midori-core.h"
#include "sokoke.h"
#include <glib/gi18n.h>
#endif
#include "sokoke.h"
+#include <midori/midori-core.h>
#define SM "http://www.searchmash.com/search/"
}
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);
}
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;
sokoke_assert_str_equal (items[i].before, result, after);
g_free (result);
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;
sokoke_assert_str_equal (items[i].before, result, after);
g_free (result);