[network-manager-applet: 2/4] editor: refactor filtering of characters for GtkEntry



commit c3d8011ef52a22321feecd6dd3ba23b0b364c970
Author: Thomas Haller <thaller redhat com>
Date:   Fri Sep 27 19:56:58 2013 +0200

    editor: refactor filtering of characters for GtkEntry
    
    Some GtkEntry only allow certain characters. This code was duplicated
    over several places. Refactor them and add a function
    utils_filter_editable_on_insert_text to implement the filtering.
    
    This also fixes some minor hitches:
    
    - at some places, the is<type> functions from ctype.h were used. These
      functions behave differently depending on the locale, but we ~really~
      want to check for ASCII characters (in the UTF-8) string.
    
    - at several places, the allocated memory for `result` did not include
      the terminating zero caracter. This was not a real bug, because
      gtk_editable_insert_text was called with the `count` parameter and
      nowhere the terminating zero was actually needed.
    
    - adjust the signature of the *_filter_cb functions to the insert-text
      signal.
    
    Signed-off-by: Thomas Haller <thaller redhat com>

 src/applet-dialogs.c                      |   31 +++---------
 src/connection-editor/ip4-routes-dialog.c |   71 +++++++++------------------
 src/connection-editor/ip6-routes-dialog.c |   71 +++++++++------------------
 src/connection-editor/page-ip4.c          |   36 ++++---------
 src/connection-editor/page-ip6.c          |   40 +++++----------
 src/connection-editor/page-mobile.c       |   76 +++++++----------------------
 src/libnm-gtk/nm-mobile-wizard.c          |   40 ++++-----------
 src/utils/utils.c                         |   77 +++++++++++++++++++++++++++++
 src/utils/utils.h                         |   16 ++++++
 src/wireless-security/ws-wep-key.c        |   46 +++++------------
 10 files changed, 210 insertions(+), 294 deletions(-)
---
diff --git a/src/applet-dialogs.c b/src/applet-dialogs.c
index 5d990f6..e084c27 100644
--- a/src/applet-dialogs.c
+++ b/src/applet-dialogs.c
@@ -25,7 +25,6 @@
 #include <netinet/in.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
-#include <ctype.h>
 
 #include <nm-device-ethernet.h>
 #include <nm-device-wifi.h>
@@ -46,6 +45,7 @@
 #include <glib/gi18n.h>
 
 #include "applet-dialogs.h"
+#include "utils.h"
 
 
 static void
@@ -1078,33 +1078,16 @@ show_toggled_cb (GtkWidget *button, gpointer user_data)
 }
 
 static void
-mpd_entry_filter (GtkEntry *entry,
-                  const char *text,
+mpd_entry_filter (GtkEditable *editable,
+                  gchar *text,
                   gint length,
                   gint *position,
                   gpointer user_data)
 {
-       GtkEditable *editable = GTK_EDITABLE (entry);
-       int i, count = 0;
-       gchar *result = g_malloc0 (length);
-
-       /* Digits only */
-       for (i = 0; i < length; i++) {
-               if (isdigit (text[i]))
-                       result[count++] = text[i];
-       }
-
-       if (count > 0) {
-               g_signal_handlers_block_by_func (G_OBJECT (editable),
-                                                G_CALLBACK (mpd_entry_filter),
-                                                user_data);
-               gtk_editable_insert_text (editable, result, count, position);
-               g_signal_handlers_unblock_by_func (G_OBJECT (editable),
-                                                  G_CALLBACK (mpd_entry_filter),
-                                                  user_data);
-       }
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
+       utils_filter_editable_on_insert_text (editable,
+                                             text, length, position, user_data,
+                                             utils_char_is_ascii_digit,
+                                             mpd_entry_filter);
 }
 
 const char *
diff --git a/src/connection-editor/ip4-routes-dialog.c b/src/connection-editor/ip4-routes-dialog.c
index 0897751..7650c2b 100644
--- a/src/connection-editor/ip4-routes-dialog.c
+++ b/src/connection-editor/ip4-routes-dialog.c
@@ -36,6 +36,7 @@
 #include <nm-utils.h>
 
 #include "ip4-routes-dialog.h"
+#include "utils.h"
 
 #define COL_ADDRESS 0
 #define COL_PREFIX  1
@@ -356,39 +357,25 @@ cell_edited (GtkCellRendererText *cell,
 }
 
 static void
-ip_address_filter_cb (GtkEntry *   entry,
-                      const gchar *text,
-                      gint         length,
-                      gint *       position,
-                      gpointer     user_data)
+ip_address_filter_cb (GtkEditable *editable,
+                      gchar *text,
+                      gint length,
+                      gint *position,
+                      gpointer user_data)
 {
        GtkWidget *ok_button = user_data;
-       GtkEditable *editable = GTK_EDITABLE (entry);
-       int i, count = 0;
-       gchar *result;
+       gboolean changed;
 
-       result = g_malloc0 (length + 1);
+       changed = utils_filter_editable_on_insert_text (editable,
+                                                       text, length, position, user_data,
+                                                       utils_char_is_ascii_ip4_address,
+                                                       ip_address_filter_cb);
 
-       for (i = 0; i < length; i++) {
-               if ((text[i] >= '0' && text[i] <= '9') || (text[i] == '.'))
-                       result[count++] = text[i];
-       }
-
-       if (count > 0) {
-               g_signal_handlers_block_by_func (G_OBJECT (editable),
-                                                G_CALLBACK (ip_address_filter_cb),
-                                                user_data);
-               gtk_editable_insert_text (editable, result, count, position);
+       if (changed) {
                g_free (last_edited);
                last_edited = gtk_editable_get_chars (editable, 0, -1);
-               g_signal_handlers_unblock_by_func (G_OBJECT (editable),
-                                                  G_CALLBACK (ip_address_filter_cb),
-                                                  user_data);
        }
 
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
-
        /* Desensitize the OK button during input to simplify input validation.
         * All routes will be validated on focus-out, which will then re-enable
         * the OK button if the routes are valid.
@@ -542,37 +529,25 @@ ip4_cell_editing_started (GtkCellRenderer *cell,
 }
 
 static void
-uint_filter_cb (GtkEntry *   entry,
-                const gchar *text,
-                gint         length,
-                gint *       position,
-                gpointer     user_data)
+uint_filter_cb (GtkEditable *editable,
+                gchar *text,
+                gint length,
+                gint *position,
+                gpointer user_data)
 {
        GtkWidget *ok_button = user_data;
-       GtkEditable *editable = GTK_EDITABLE (entry);
-       int i, count = 0;
-       gchar *result = g_new (gchar, length);
+       gboolean changed;
 
-       for (i = 0; i < length; i++) {
-               if ((text[i] >= '0' && text[i] <= '9'))
-                       result[count++] = text[i];
-       }
+       changed = utils_filter_editable_on_insert_text (editable,
+                                                       text, length, position, user_data,
+                                                       utils_char_is_ascii_digit,
+                                                       uint_filter_cb);
 
-       if (count > 0) {
-               g_signal_handlers_block_by_func (G_OBJECT (editable),
-                                                G_CALLBACK (uint_filter_cb),
-                                                user_data);
-               gtk_editable_insert_text (editable, result, count, position);
+       if (changed) {
                g_free (last_edited);
                last_edited = gtk_editable_get_chars (editable, 0, -1);
-               g_signal_handlers_unblock_by_func (G_OBJECT (editable),
-                                                  G_CALLBACK (uint_filter_cb),
-                                                  user_data);
        }
 
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
-
        /* Desensitize the OK button during input to simplify input validation.
         * All routes will be validated on focus-out, which will then re-enable
         * the OK button if the routes are valid.
diff --git a/src/connection-editor/ip6-routes-dialog.c b/src/connection-editor/ip6-routes-dialog.c
index 56525ae..74a7129 100644
--- a/src/connection-editor/ip6-routes-dialog.c
+++ b/src/connection-editor/ip6-routes-dialog.c
@@ -36,6 +36,7 @@
 #include <nm-utils.h>
 
 #include "ip6-routes-dialog.h"
+#include "utils.h"
 
 #define COL_ADDRESS 0
 #define COL_PREFIX  1
@@ -307,39 +308,25 @@ cell_edited (GtkCellRendererText *cell,
 }
 
 static void
-ip_address_filter_cb (GtkEntry *   entry,
-                      const gchar *text,
-                      gint         length,
-                      gint *       position,
-                      gpointer     user_data)
+ip_address_filter_cb (GtkEditable *editable,
+                      gchar *text,
+                      gint length,
+                      gint *position,
+                      gpointer user_data)
 {
        GtkWidget *ok_button = user_data;
-       GtkEditable *editable = GTK_EDITABLE (entry);
-       int i, count = 0;
-       gchar *result;
+       gboolean changed;
 
-       result = g_malloc0 (length + 1);
+       changed = utils_filter_editable_on_insert_text (editable,
+                                                       text, length, position, user_data,
+                                                       utils_char_is_ascii_ip6_address,
+                                                       ip_address_filter_cb);
 
-       for (i = 0; i < length; i++) {
-               if (g_ascii_isxdigit(text[i]) || (text[i] == ':'))
-                       result[count++] = text[i];
-       }
-
-       if (count > 0) {
-               g_signal_handlers_block_by_func (G_OBJECT (editable),
-                                                G_CALLBACK (ip_address_filter_cb),
-                                                user_data);
-               gtk_editable_insert_text (editable, result, count, position);
+       if (changed) {
                g_free (last_edited);
                last_edited = gtk_editable_get_chars (editable, 0, -1);
-               g_signal_handlers_unblock_by_func (G_OBJECT (editable),
-                                                  G_CALLBACK (ip_address_filter_cb),
-                                                  user_data);
        }
 
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
-
        /* Desensitize the OK button during input to simplify input validation.
         * All routes will be validated on focus-out, which will then re-enable
         * the OK button if the routes are valid.
@@ -483,37 +470,25 @@ ip6_cell_editing_started (GtkCellRenderer *cell,
 }
 
 static void
-uint_filter_cb (GtkEntry *   entry,
-                const gchar *text,
-                gint         length,
-                gint *       position,
-                gpointer     user_data)
+uint_filter_cb (GtkEditable *editable,
+                gchar *text,
+                gint length,
+                gint *position,
+                gpointer user_data)
 {
        GtkWidget *ok_button = user_data;
-       GtkEditable *editable = GTK_EDITABLE (entry);
-       int i, count = 0;
-       gchar *result = g_new (gchar, length);
+       gboolean changed;
 
-       for (i = 0; i < length; i++) {
-               if ((text[i] >= '0' && text[i] <= '9'))
-                       result[count++] = text[i];
-       }
+       changed = utils_filter_editable_on_insert_text (editable,
+                                                       text, length, position, user_data,
+                                                       utils_char_is_ascii_digit,
+                                                       uint_filter_cb);
 
-       if (count > 0) {
-               g_signal_handlers_block_by_func (G_OBJECT (editable),
-                                                G_CALLBACK (uint_filter_cb),
-                                                user_data);
-               gtk_editable_insert_text (editable, result, count, position);
+       if (changed) {
                g_free (last_edited);
                last_edited = gtk_editable_get_chars (editable, 0, -1);
-               g_signal_handlers_unblock_by_func (G_OBJECT (editable),
-                                                  G_CALLBACK (uint_filter_cb),
-                                                  user_data);
        }
 
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
-
        /* Desensitize the OK button during input to simplify input validation.
         * All routes will be validated on focus-out, which will then re-enable
         * the OK button if the routes are valid.
diff --git a/src/connection-editor/page-ip4.c b/src/connection-editor/page-ip4.c
index 2bc1536..e4371ec 100644
--- a/src/connection-editor/page-ip4.c
+++ b/src/connection-editor/page-ip4.c
@@ -614,39 +614,25 @@ cell_edited (GtkCellRendererText *cell,
 }
 
 static void
-ip_address_filter_cb (GtkEntry *   entry,
-                      const gchar *text,
-                      gint         length,
-                      gint *       position,
-                      gpointer     user_data)
+ip_address_filter_cb (GtkEditable *editable,
+                      gchar *text,
+                      gint length,
+                      gint *position,
+                      gpointer user_data)
 {
        CEPageIP4 *self = CE_PAGE_IP4 (user_data);
        CEPageIP4Private *priv = CE_PAGE_IP4_GET_PRIVATE (self);
-       GtkEditable *editable = GTK_EDITABLE (entry);
-       int i, count = 0;
-       gchar *result;
+       gboolean changed;
 
-       result = g_malloc0 (length + 1);
+       changed = utils_filter_editable_on_insert_text (editable,
+                                                       text, length, position, user_data,
+                                                       utils_char_is_ascii_ip4_address,
+                                                       ip_address_filter_cb);
 
-       for (i = 0; i < length; i++) {
-               if ((text[i] >= '0' && text[i] <= '9') || (text[i] == '.'))
-                       result[count++] = text[i];
-       }
-
-       if (count > 0) {
-               g_signal_handlers_block_by_func (G_OBJECT (editable),
-                                                G_CALLBACK (ip_address_filter_cb),
-                                                user_data);
-               gtk_editable_insert_text (editable, result, count, position);
+       if (changed) {
                g_free (priv->last_edited);
                priv->last_edited = gtk_editable_get_chars (editable, 0, -1);
-               g_signal_handlers_unblock_by_func (G_OBJECT (editable),
-                                                  G_CALLBACK (ip_address_filter_cb),
-                                                  user_data);
        }
-
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
 }
 
 static void
diff --git a/src/connection-editor/page-ip6.c b/src/connection-editor/page-ip6.c
index 30c2497..306d765 100644
--- a/src/connection-editor/page-ip6.c
+++ b/src/connection-editor/page-ip6.c
@@ -587,48 +587,32 @@ cell_edited (GtkCellRendererText *cell,
        ce_page_changed (CE_PAGE (self));
 }
 
+
 static void
-ip_address_filter_cb (GtkEntry *   entry,
-                      const gchar *text,
-                      gint         length,
-                      gint *       position,
-                      gpointer     user_data)
+ip_address_filter_cb (GtkEditable *editable,
+                      gchar *text,
+                      gint length,
+                      gint *position,
+                      gpointer user_data)
 {
        CEPageIP6 *self = CE_PAGE_IP6 (user_data);
        CEPageIP6Private *priv = CE_PAGE_IP6_GET_PRIVATE (self);
-       GtkEditable *editable = GTK_EDITABLE (entry);
-       gboolean numeric = FALSE;
-       int i, count = 0;
-       gchar *result;
        guint column;
+       gboolean changed;
 
-       result = g_malloc0 (length + 1);
 
        /* The prefix column only allows numbers, no ':' */
        column = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (editable), "column"));
-       if (column == COL_PREFIX)
-               numeric = TRUE;
 
-       for (i = 0; i < length; i++) {
-               if ((numeric && g_ascii_isdigit (text[i])) ||
-                       (!numeric && (g_ascii_isxdigit(text[i]) || (text[i] == ':'))))
-                       result[count++] = text[i];
-       }
+       changed = utils_filter_editable_on_insert_text (editable,
+                                                       text, length, position, user_data,
+                                                       column == COL_PREFIX ? utils_char_is_ascii_digit : 
utils_char_is_ascii_ip6_address,
+                                                       ip_address_filter_cb);
 
-       if (count > 0) {
-               g_signal_handlers_block_by_func (G_OBJECT (editable),
-                                                G_CALLBACK (ip_address_filter_cb),
-                                                user_data);
-               gtk_editable_insert_text (editable, result, count, position);
+       if (changed) {
                g_free (priv->last_edited);
                priv->last_edited = gtk_editable_get_chars (editable, 0, -1);
-               g_signal_handlers_unblock_by_func (G_OBJECT (editable),
-                                                  G_CALLBACK (ip_address_filter_cb),
-                                                  user_data);
        }
-
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
 }
 
 static void
diff --git a/src/connection-editor/page-mobile.c b/src/connection-editor/page-mobile.c
index b0ba724..5742426 100644
--- a/src/connection-editor/page-mobile.c
+++ b/src/connection-editor/page-mobile.c
@@ -23,7 +23,6 @@
 #include "config.h"
 
 #include <string.h>
-#include <ctype.h>
 
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
@@ -267,68 +266,29 @@ apn_button_clicked (GtkButton *button, gpointer user_data)
 }
 
 static void
-network_id_filter_cb (GtkEntry *   entry,
-                      const gchar *text,
-                      gint         length,
-                      gint *       position,
-                      gpointer     user_data)
+network_id_filter_cb (GtkEditable *editable,
+                      gchar *text,
+                      gint length,
+                      gint *position,
+                      gpointer user_data)
 {
-       GtkEditable *editable = GTK_EDITABLE (entry);
-       int i, count = 0;
-       gchar *result;
-
-       result = g_malloc0 (length + 1);
-
-       for (i = 0; i < length; i++) {
-               if (isdigit (text[i]))
-                       result[count++] = text[i];
-       }
-
-       if (count > 0) {
-               g_signal_handlers_block_by_func (G_OBJECT (editable),
-                                                G_CALLBACK (network_id_filter_cb),
-                                                user_data);
-               gtk_editable_insert_text (editable, result, count, position);
-               g_signal_handlers_unblock_by_func (G_OBJECT (editable),
-                                                  G_CALLBACK (network_id_filter_cb),
-                                                  user_data);
-       }
-
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
+       utils_filter_editable_on_insert_text (editable,
+                                             text, length, position, user_data,
+                                             utils_char_is_ascii_digit,
+                                             network_id_filter_cb);
 }
 
 static void
-apn_filter_cb (GtkEntry *   entry,
-               const gchar *text,
-               gint         length,
-               gint *       position,
-               gpointer     user_data)
+apn_filter_cb (GtkEditable *editable,
+               gchar *text,
+               gint length,
+               gint *position,
+               gpointer user_data)
 {
-       GtkEditable *editable = GTK_EDITABLE (entry);
-       int i, count = 0;
-       gchar *result = g_new0 (gchar, length);
-
-       for (i = 0; i < length; i++) {
-               if (   isalnum (text[i])
-                   || (text[i] == '.')
-                   || (text[i] == '_')
-                   || (text[i] == '-'))
-                       result[count++] = text[i];
-       }
-
-       if (count > 0) {
-               g_signal_handlers_block_by_func (G_OBJECT (editable),
-                                                G_CALLBACK (apn_filter_cb),
-                                                user_data);
-               gtk_editable_insert_text (editable, result, count, position);
-               g_signal_handlers_unblock_by_func (G_OBJECT (editable),
-                                                  G_CALLBACK (apn_filter_cb),
-                                                  user_data);
-       }
-
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
+       utils_filter_editable_on_insert_text (editable,
+                                             text, length, position, user_data,
+                                             utils_char_is_ascii_apn,
+                                             apn_filter_cb);
 }
 
 static void
diff --git a/src/libnm-gtk/nm-mobile-wizard.c b/src/libnm-gtk/nm-mobile-wizard.c
index 84bb377..9f586a0 100644
--- a/src/libnm-gtk/nm-mobile-wizard.c
+++ b/src/libnm-gtk/nm-mobile-wizard.c
@@ -23,7 +23,6 @@
 #include "config.h"
 
 #include <stdlib.h>
-#include <ctype.h>
 
 #include <glib.h>
 #include <glib/gi18n-lib.h>
@@ -39,6 +38,7 @@
 #include "nm-mobile-wizard.h"
 #include "nm-mobile-providers.h"
 #include "nm-ui-utils.h"
+#include "utils.h"
 
 #define DEVICE_TAG "device"
 #define TYPE_TAG "setting-type"
@@ -437,36 +437,16 @@ plan_row_separator_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
 }
 
 static void
-apn_filter_cb (GtkEntry *   entry,
-               const gchar *text,
-               gint         length,
-               gint *       position,
-               gpointer     user_data)
+apn_filter_cb (GtkEditable *editable,
+               gchar *text,
+               gint length,
+               gint *position,
+               gpointer user_data)
 {
-       GtkEditable *editable = GTK_EDITABLE (entry);
-       int i, count = 0;
-       gchar *result = g_new0 (gchar, length);
-
-       for (i = 0; i < length; i++) {
-               if (   isalnum (text[i])
-                   || (text[i] == '.')
-                   || (text[i] == '_')
-                   || (text[i] == '-'))
-                       result[count++] = text[i];
-       }
-
-       if (count > 0) {
-               g_signal_handlers_block_by_func (G_OBJECT (editable),
-                                                G_CALLBACK (apn_filter_cb),
-                                                user_data);
-               gtk_editable_insert_text (editable, result, count, position);
-               g_signal_handlers_unblock_by_func (G_OBJECT (editable),
-                                                  G_CALLBACK (apn_filter_cb),
-                                                  user_data);
-       }
-
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
+       utils_filter_editable_on_insert_text (editable,
+                                             text, length, position, user_data,
+                                             utils_char_is_ascii_apn,
+                                             apn_filter_cb);
 }
 
 static void
diff --git a/src/utils/utils.c b/src/utils/utils.c
index 00f8596..298ffe5 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -209,3 +209,80 @@ utils_show_error_dialog (const char *title,
        }
 }
 
+
+gboolean
+utils_char_is_ascii_print (char character)
+{
+       return g_ascii_isprint (character);
+}
+
+gboolean
+utils_char_is_ascii_digit (char character)
+{
+       return g_ascii_isdigit (character);
+}
+
+gboolean
+utils_char_is_ascii_ip4_address (char character)
+{
+       return g_ascii_isdigit (character) || character == '.';
+}
+
+gboolean
+utils_char_is_ascii_ip6_address (char character)
+{
+       return g_ascii_isxdigit (character) || character == ':';
+}
+
+gboolean
+utils_char_is_ascii_apn (char character)
+{
+       return g_ascii_isalnum (character)
+              || character == '.'
+              || character == '_'
+              || character == '-';
+}
+
+/**
+ * Filters the characters from a text that was just input into GtkEditable.
+ * Returns FALSE, if after filtering no characters were left. TRUE means,
+ * that valid characters were added and the content of the GtkEditable changed.
+ **/
+gboolean
+utils_filter_editable_on_insert_text (GtkEditable *editable,
+                                      const gchar *text,
+                                      gint length,
+                                      gint *position,
+                                      void *user_data,
+                                      UtilsFilterGtkEditableFunc validate_character,
+                                      gpointer block_func)
+{
+       int i, count = 0;
+       gchar *result = g_new (gchar, length+1);
+
+       for (i = 0; i < length; i++) {
+               if (validate_character (text[i]))
+                       result[count++] = text[i];
+       }
+       result[count] = 0;
+
+       if (count > 0) {
+               if (block_func) {
+                       g_signal_handlers_block_by_func (G_OBJECT (editable),
+                                                        G_CALLBACK (block_func),
+                                                        user_data);
+               }
+               gtk_editable_insert_text (editable, result, count, position);
+               if (block_func) {
+                       g_signal_handlers_unblock_by_func (G_OBJECT (editable),
+                                                          G_CALLBACK (block_func),
+                                                          user_data);
+               }
+       }
+       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
+
+       g_free (result);
+
+       return count > 0;
+}
+
diff --git a/src/utils/utils.h b/src/utils/utils.h
index 0da159a..8ad3e55 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -59,5 +59,21 @@ typedef enum  {
        NMA_ERROR_GENERIC
 } NMAError;
 
+
+gboolean utils_char_is_ascii_print (char character);
+gboolean utils_char_is_ascii_digit (char character);
+gboolean utils_char_is_ascii_ip4_address (char character);
+gboolean utils_char_is_ascii_ip6_address (char character);
+gboolean utils_char_is_ascii_apn (char character);
+
+typedef gboolean (*UtilsFilterGtkEditableFunc) (char character);
+gboolean utils_filter_editable_on_insert_text (GtkEditable *editable,
+                                               const gchar *text,
+                                               gint length,
+                                               gint *position,
+                                               void *user_data,
+                                               UtilsFilterGtkEditableFunc validate_character,
+                                               gpointer block_func);
+
 #endif /* UTILS_H */
 
diff --git a/src/wireless-security/ws-wep-key.c b/src/wireless-security/ws-wep-key.c
index 17ec002..37dbceb 100644
--- a/src/wireless-security/ws-wep-key.c
+++ b/src/wireless-security/ws-wep-key.c
@@ -20,13 +20,14 @@
  * (C) Copyright 2007 - 2010 Red Hat, Inc.
  */
 
-#include <ctype.h>
 #include <string.h>
+#include <glib.h>
 
 #include <nm-setting-wireless.h>
 #include <nm-setting-wireless-security.h>
 
 #include "wireless-security.h"
+#include "utils.h"
 
 struct _WirelessSecurityWEPKey {
        WirelessSecurity parent;
@@ -104,12 +105,12 @@ validate (WirelessSecurity *parent, const GByteArray *ssid)
        if (sec->type == NM_WEP_KEY_TYPE_KEY) {
                if ((strlen (key) == 10) || (strlen (key) == 26)) {
                        for (i = 0; i < strlen (key); i++) {
-                               if (!isxdigit (key[i]))
+                               if (!g_ascii_isxdigit (key[i]))
                                        return FALSE;
                        }
                } else if ((strlen (key) == 5) || (strlen (key) == 13)) {
                        for (i = 0; i < strlen (key); i++) {
-                               if (!isascii (key[i]))
+                               if (!utils_char_is_ascii_print (key[i]))
                                        return FALSE;
                        }
                } else {
@@ -173,41 +174,20 @@ fill_connection (WirelessSecurity *parent, NMConnection *connection)
 }
 
 static void
-wep_entry_filter_cb (GtkEntry *   entry,
-                     const gchar *text,
-                     gint         length,
-                     gint *       position,
-                     gpointer     data)
+wep_entry_filter_cb (GtkEditable *editable,
+                     gchar *text,
+                     gint length,
+                     gint *position,
+                     gpointer data)
 {
        WirelessSecurityWEPKey *sec = (WirelessSecurityWEPKey *) data;
-       GtkEditable *editable = GTK_EDITABLE (entry);
-       int i, count = 0;
-       gchar *result;
-
-       result = g_malloc0 (length + 1);
 
        if (sec->type == NM_WEP_KEY_TYPE_KEY) {
-               for (i = 0; i < length; i++) {
-                       if (isxdigit(text[i]) || isascii(text[i]))
-                               result[count++] = text[i];
-               }
-       } else if (sec->type == NM_WEP_KEY_TYPE_PASSPHRASE) {
-               for (i = 0; i < length; i++)
-                       result[count++] = text[i];
+               utils_filter_editable_on_insert_text (editable,
+                                                     text, length, position, data,
+                                                     utils_char_is_ascii_print,
+                                                     wep_entry_filter_cb);
        }
-
-       if (count > 0) {
-               g_signal_handlers_block_by_func (G_OBJECT (editable),
-                                                    G_CALLBACK (wep_entry_filter_cb),
-                                                    data);
-               gtk_editable_insert_text (editable, result, count, position);
-               g_signal_handlers_unblock_by_func (G_OBJECT (editable),
-                                                      G_CALLBACK (wep_entry_filter_cb),
-                                                      data);
-       }
-
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]