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



commit 129b501948dde2f70eef5d48d3c304f958748b2e
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                      |   32 +++--------
 src/connection-editor/ip4-routes-dialog.c |   85 +++++++++++------------------
 src/connection-editor/ip6-routes-dialog.c |   85 +++++++++++------------------
 src/connection-editor/page-ip4.c          |   43 ++++++---------
 src/connection-editor/page-ip6.c          |   51 +++++++-----------
 src/connection-editor/page-mobile.c       |   78 +++++++--------------------
 src/libnm-gtk/nm-mobile-wizard.c          |   41 ++++----------
 src/utils/utils.c                         |   81 +++++++++++++++++++++++++++
 src/utils/utils.h                         |   18 ++++++
 src/wireless-security/ws-wep-key.c        |   47 +++++-----------
 10 files changed, 253 insertions(+), 308 deletions(-)
---
diff --git a/src/applet-dialogs.c b/src/applet-dialogs.c
index 5d990f6..23c5dd6 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,17 @@ show_toggled_cb (GtkWidget *button, gpointer user_data)
 }
 
 static void
-mpd_entry_filter (GtkEntry *entry,
-                  const char *text,
+mpd_entry_filter (GtkEditable *entry,
+                  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 (GTK_EDITABLE (entry),
+                                             text, length, position, user_data,
+                                             utils_char_is_ascii_digit,
+                                             mpd_entry_filter,
+                                             NULL, NULL);
 }
 
 const char *
diff --git a/src/connection-editor/ip4-routes-dialog.c b/src/connection-editor/ip4-routes-dialog.c
index 0897751..2fea022 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,38 +357,27 @@ 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 *entry,
+                      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 was_filtered;
+       char *result = NULL;
 
-       result = g_malloc0 (length + 1);
+       was_filtered = utils_filter_editable_on_insert_text (GTK_EDITABLE (entry),
+                                                            text, length, position, user_data,
+                                                            utils_char_is_ascii_ip4_address,
+                                                            ip_address_filter_cb,
+                                                            &result, NULL);
 
-       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 (was_filtered) {
                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);
+               last_edited = result;
+       } else
+               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
@@ -542,36 +532,27 @@ 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 *entry,
+                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 was_filtered;
+       char *result = NULL;
 
-       for (i = 0; i < length; i++) {
-               if ((text[i] >= '0' && text[i] <= '9'))
-                       result[count++] = text[i];
-       }
+       was_filtered = utils_filter_editable_on_insert_text (GTK_EDITABLE (entry),
+                                                            text, length, position, user_data,
+                                                            utils_char_is_ascii_digit,
+                                                            uint_filter_cb,
+                                                            &result, NULL);
 
-       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 (was_filtered) {
                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);
+               last_edited = result;
+       } else
+               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
diff --git a/src/connection-editor/ip6-routes-dialog.c b/src/connection-editor/ip6-routes-dialog.c
index 56525ae..590e415 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,38 +308,27 @@ 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 *entry,
+                      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 was_filtered;
+       char *result = NULL;
 
-       result = g_malloc0 (length + 1);
+       was_filtered = utils_filter_editable_on_insert_text (GTK_EDITABLE (entry),
+                                                            text, length, position, user_data,
+                                                            utils_char_is_ascii_ip6_address,
+                                                            ip_address_filter_cb,
+                                                            &result, NULL);
 
-       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 (was_filtered) {
                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);
+               last_edited = result;
+       } else
+               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
@@ -483,36 +473,27 @@ 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 *entry,
+                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 was_filtered;
+       char *result = NULL;
 
-       for (i = 0; i < length; i++) {
-               if ((text[i] >= '0' && text[i] <= '9'))
-                       result[count++] = text[i];
-       }
+       was_filtered = utils_filter_editable_on_insert_text (GTK_EDITABLE (entry),
+                                                            text, length, position, user_data,
+                                                            utils_char_is_ascii_digit,
+                                                            uint_filter_cb,
+                                                            &result, NULL);
 
-       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 (was_filtered) {
                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);
+               last_edited = result;
+       } else
+               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
diff --git a/src/connection-editor/page-ip4.c b/src/connection-editor/page-ip4.c
index 2bc1536..0d3f2b5 100644
--- a/src/connection-editor/page-ip4.c
+++ b/src/connection-editor/page-ip4.c
@@ -614,39 +614,28 @@ 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 *entry,
+                      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 was_filtered;
+       char *result = NULL;
 
-       result = g_malloc0 (length + 1);
+       was_filtered = utils_filter_editable_on_insert_text (GTK_EDITABLE (entry),
+                                                            text, length, position, user_data,
+                                                            utils_char_is_ascii_ip4_address,
+                                                            ip_address_filter_cb,
+                                                            &result, NULL);
 
-       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 (was_filtered) {
                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);
+               priv->last_edited = result;
+       } else
+               g_free (result);
 }
 
 static void
diff --git a/src/connection-editor/page-ip6.c b/src/connection-editor/page-ip6.c
index 30c2497..9b1a11b 100644
--- a/src/connection-editor/page-ip6.c
+++ b/src/connection-editor/page-ip6.c
@@ -587,48 +587,35 @@ 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 *entry,
+                      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 was_filtered;
+       char *result = NULL;
 
-       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];
-       }
+       column = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (entry), "column"));
 
-       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);
-               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);
-       }
+       was_filtered = utils_filter_editable_on_insert_text (GTK_EDITABLE (entry),
+                                                            text, length, position, user_data,
+                                                            column == COL_PREFIX ? utils_char_is_ascii_digit 
: utils_char_is_ascii_ip6_address,
+                                                            ip_address_filter_cb,
+                                                            &result, NULL);
 
-       g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
-       g_free (result);
+       if (was_filtered) {
+               g_free (priv->last_edited);
+               priv->last_edited = result;
+       } else
+               g_free (result);
 }
 
 static void
diff --git a/src/connection-editor/page-mobile.c b/src/connection-editor/page-mobile.c
index b0ba724..b3ba828 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,31 @@ 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 *entry,
+                      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 (GTK_EDITABLE (entry),
+                                             text, length, position, user_data,
+                                             utils_char_is_ascii_digit,
+                                             network_id_filter_cb,
+                                             NULL, NULL);
 }
 
 static void
-apn_filter_cb (GtkEntry *   entry,
-               const gchar *text,
-               gint         length,
-               gint *       position,
-               gpointer     user_data)
+apn_filter_cb (GtkEditable *entry,
+               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 (GTK_EDITABLE (entry),
+                                             text, length, position, user_data,
+                                             utils_char_is_ascii_apn,
+                                             apn_filter_cb,
+                                             NULL, NULL);
 }
 
 static void
diff --git a/src/libnm-gtk/nm-mobile-wizard.c b/src/libnm-gtk/nm-mobile-wizard.c
index 84bb377..6ff9484 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,17 @@ 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 *entry,
+               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 (GTK_EDITABLE (entry),
+                                             text, length, position, user_data,
+                                             utils_char_is_ascii_apn,
+                                             apn_filter_cb,
+                                             NULL, NULL);
 }
 
 static void
diff --git a/src/utils/utils.c b/src/utils/utils.c
index 00f8596..3940d80 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -209,3 +209,84 @@ utils_show_error_dialog (const char *title,
        }
 }
 
+
+
+gboolean
+utils_char_is_ascii (char character)
+{
+       return character >= 0 && character <= 0x7F;
+}
+
+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 == '-';
+}
+
+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,
+                                      gchar **result_text,
+                                      gint *result_length)
+{
+       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");
+
+       if (result_text)
+               *result_text = result;
+       else
+               g_free (result);
+
+       if (result_length)
+               *result_length = count;
+
+       return count != length;
+}
+
diff --git a/src/utils/utils.h b/src/utils/utils.h
index 0da159a..9167e96 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -59,5 +59,23 @@ typedef enum  {
        NMA_ERROR_GENERIC
 } NMAError;
 
+
+gboolean utils_char_is_ascii (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,
+                                               gchar **result_text,
+                                               gint *result_length);
+
 #endif /* UTILS_H */
 
diff --git a/src/wireless-security/ws-wep-key.c b/src/wireless-security/ws-wep-key.c
index 17ec002..8d35361 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 (key[i]))
                                        return FALSE;
                        }
                } else {
@@ -173,41 +174,21 @@ 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 *entry,
+                     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 (GTK_EDITABLE (entry),
+                                                     text, length, position, data,
+                                                     utils_char_is_ascii,
+                                                     wep_entry_filter_cb,
+                                                     NULL, NULL);
        }
-
-       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]