From f97395dab1f6a0461cd1c56ba8db6fbb29396903 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Sun, 15 May 2011 16:45:14 +0200 Subject: [PATCH] Validate URIs and render entry red if invalid Fixes: https://bugs.launchpad.net/midori/+bug/710855 --- extensions/adblock.c | 2 +- katze/katze-utils.c | 60 ++++++++++++++++++++++++++++++++++++- katze/katze-utils.h | 3 ++ midori/midori-browser.c | 24 ++++----------- midori/midori-preferences.c | 2 +- 5 files changed, 70 insertions(+), 21 deletions(-) diff --git a/extensions/adblock.c b/extensions/adblock.c index a6acc3e7..062a77eb 100644 --- a/extensions/adblock.c +++ b/extensions/adblock.c @@ -460,7 +460,7 @@ adblock_get_preferences_dialog (MidoriExtension* extension) g_free (description); gtk_label_set_line_wrap (GTK_LABEL (button), TRUE); gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 4); - entry = gtk_entry_new (); + entry = katze_uri_entry_new (NULL); gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 4); liststore = gtk_list_store_new (1, G_TYPE_STRING); treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (liststore)); diff --git a/katze/katze-utils.c b/katze/katze-utils.c index fab3d9bb..9cbd72fd 100644 --- a/katze/katze-utils.c +++ b/katze/katze-utils.c @@ -477,6 +477,9 @@ g_icon_to_string (GIcon *icon) * Since 0.2.9 the following hints are also supported: * "languages": the widget will be particularly suitable for choosing * multiple language codes, ie. "de,en_GB". + * Since 0.3.6 the following hints are also supported: + * "address": the widget will be particularly suitable for typing + * a valid URI or IP address and highlight errors. * * Any other values for @hint are silently ignored. * @@ -741,7 +744,10 @@ katze_property_proxy (gpointer object, { gchar* notify_property; - widget = gtk_entry_new (); + if (_hint == I_("address")) + widget = katze_uri_entry_new (NULL); + else + widget = gtk_entry_new (); g_object_get (object, property, &string, NULL); if (!string) string = g_strdup (G_PARAM_SPEC_STRING (pspec)->default_value); @@ -1559,3 +1565,55 @@ katze_load_cached_icon (const gchar* uri, GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL); } +static void +katze_uri_entry_changed_cb (GtkWidget* entry, + 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:"); + if (*uri && !valid) + { + GdkColor bg_color = { 0 }; + GdkColor fg_color = { 0 }; + gdk_color_parse ("#ef7070", &bg_color); + gdk_color_parse ("#000", &fg_color); + gtk_widget_modify_base (entry, GTK_STATE_NORMAL, &bg_color); + gtk_widget_modify_text (entry, GTK_STATE_NORMAL, &fg_color); + } + else + { + gtk_widget_modify_base (entry, GTK_STATE_NORMAL, NULL); + gtk_widget_modify_text (entry, GTK_STATE_NORMAL, NULL); + } + + if (other_widget != NULL) + gtk_widget_set_sensitive (other_widget, *uri && valid); +} + +/** + * katze_uri_entry_new: + * @other_widget: a #GtkWidget, or %NULL + * + * Creates an entry that validates the typed URI. + * + * If @other_widget is given, it will become insensitive if + * the input is not a valid URI. + * + * Returns: a #GtkEntry + * + * Since: 0.3.6 + */ +GtkWidget* +katze_uri_entry_new (GtkWidget* other_widget) +{ + GtkWidget* entry = gtk_entry_new (); + g_signal_connect (entry, "changed", + G_CALLBACK (katze_uri_entry_changed_cb), other_widget); + return entry; +} + diff --git a/katze/katze-utils.h b/katze/katze-utils.h index 5382173f..eb67a1ff 100644 --- a/katze/katze-utils.h +++ b/katze/katze-utils.h @@ -139,6 +139,9 @@ GdkPixbuf* katze_load_cached_icon (const gchar* uri, GtkWidget* widget); +GtkWidget* +katze_uri_entry_new (GtkWidget* other_widget); + G_END_DECLS #endif /* __KATZE_UTILS_H__ */ diff --git a/midori/midori-browser.c b/midori/midori-browser.c index 1fcbdde0..13998260 100644 --- a/midori/midori-browser.c +++ b/midori/midori-browser.c @@ -670,19 +670,6 @@ midori_view_notify_statusbar_text_cb (GtkWidget* view, } } -static void -midori_browser_edit_bookmark_uri_changed_cb (GtkEntry* entry, - GtkDialog* dialog) -{ - const gchar* uri = gtk_entry_get_text (entry); - gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_ACCEPT, - uri && (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, "javascript:"))); -} - static void midori_browser_edit_bookmark_title_changed_cb (GtkEntry* entry, GtkDialog* dialog) @@ -803,7 +790,12 @@ midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser, label = gtk_label_new_with_mnemonic (_("_Address:")); gtk_size_group_add_widget (sizegroup, label); gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); - entry_uri = gtk_entry_new (); + entry_uri = katze_uri_entry_new ( + #if GTK_CHECK_VERSION (2, 20, 0) + gtk_dialog_get_widget_for_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT)); + #else + NULL); + #endif #if HAVE_HILDON HildonGtkInputMode mode = hildon_gtk_entry_get_input_mode (GTK_ENTRY (entry_uri)); mode &= ~HILDON_GTK_INPUT_MODE_AUTOCAP; @@ -811,10 +803,6 @@ midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser, #endif gtk_entry_set_activates_default (GTK_ENTRY (entry_uri), TRUE); gtk_entry_set_text (GTK_ENTRY (entry_uri), katze_item_get_uri (bookmark)); - midori_browser_edit_bookmark_uri_changed_cb (GTK_ENTRY (entry_uri), - GTK_DIALOG (dialog)); - g_signal_connect (entry_uri, "changed", - G_CALLBACK (midori_browser_edit_bookmark_uri_changed_cb), dialog); gtk_box_pack_start (GTK_BOX (hbox), entry_uri, TRUE, TRUE, 0); gtk_container_add (GTK_CONTAINER (content_area), hbox); gtk_widget_show_all (hbox); diff --git a/midori/midori-preferences.c b/midori/midori-preferences.c index 1b255b75..429fe505 100644 --- a/midori/midori-preferences.c +++ b/midori/midori-preferences.c @@ -299,7 +299,7 @@ midori_preferences_set_settings (MidoriPreferences* preferences, SPANNED_ADD (button); label = katze_property_label (settings, "homepage"); INDENTED_ADD (label); - entry = katze_property_proxy (settings, "homepage", NULL); + entry = katze_property_proxy (settings, "homepage", "address"); SPANNED_ADD (entry); if (parent && katze_object_has_property (parent, "uri")) {