Validate URIs and render entry red if invalid

Fixes: https://bugs.launchpad.net/midori/+bug/710855
This commit is contained in:
Christian Dywan 2011-05-15 16:45:14 +02:00
parent 6756f35763
commit f97395dab1
5 changed files with 70 additions and 21 deletions

View file

@ -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));

View file

@ -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,6 +744,9 @@ katze_property_proxy (gpointer object,
{
gchar* notify_property;
if (_hint == I_("address"))
widget = katze_uri_entry_new (NULL);
else
widget = gtk_entry_new ();
g_object_get (object, property, &string, NULL);
if (!string)
@ -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;
}

View file

@ -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__ */

View file

@ -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);

View file

@ -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"))
{