[network-manager-applet/bg/editor-route-metric-default-bgo769015: 4/4] editor: factor out common tree model helpers



commit 3c7dc617122ea4e07640c58cea05f3953b2a20ec
Author: Beniamino Galvani <bgalvani redhat com>
Date:   Tue Oct 4 11:59:13 2016 +0200

    editor: factor out common tree model helpers
    
    Move some common functions for retrieving tree model values to
    utils/util.c

 src/connection-editor/ip4-routes-dialog.c |  138 +++--------------------------
 src/connection-editor/ip6-routes-dialog.c |   55 +++---------
 src/utils/Makefile.am                     |   11 ++-
 src/utils/utils.c                         |  134 ++++++++++++++++++++++++++++
 src/utils/utils.h                         |   24 +++++
 5 files changed, 190 insertions(+), 172 deletions(-)
---
diff --git a/src/connection-editor/ip4-routes-dialog.c b/src/connection-editor/ip4-routes-dialog.c
index c93a444..edb225e 100644
--- a/src/connection-editor/ip4-routes-dialog.c
+++ b/src/connection-editor/ip4-routes-dialog.c
@@ -47,120 +47,6 @@ static char *last_edited = NULL; /* cell text */
 static char *last_path = NULL;   /* row in treeview */
 static int last_column = -1;     /* column in treeview */
 
-static gboolean
-get_one_metric (GtkTreeModel *model,
-                GtkTreeIter *iter,
-                int column,
-                gboolean fail_if_missing,
-                gint64 *out,
-                char **out_raw)
-{
-       char *item = NULL;
-       gboolean success = FALSE;
-       long int tmp_int;
-
-       gtk_tree_model_get (model, iter, column, &item, -1);
-       if (out_raw)
-               *out_raw = item;
-       if (!item || !strlen (item)) {
-               if (!out_raw)
-                       g_free (item);
-               return fail_if_missing ? FALSE : TRUE;
-       }
-
-       errno = 0;
-       tmp_int = strtol (item, NULL, 10);
-       if (errno || tmp_int < 0 || tmp_int > G_MAXUINT32)
-               goto out;
-
-       *out = (gint64) tmp_int;
-       success = TRUE;
-
-out:
-       if (!out_raw)
-               g_free (item);
-       return success;
-}
-
-static gboolean
-get_one_prefix (GtkTreeModel *model,
-                GtkTreeIter *iter,
-                int column,
-                gboolean fail_if_missing,
-                guint32 *out,
-                char **out_raw)
-{
-       char *item = NULL;
-       struct in_addr tmp_addr = { 0 };
-       gboolean success = FALSE;
-       glong tmp_prefix;
-
-       gtk_tree_model_get (model, iter, column, &item, -1);
-       if (out_raw)
-               *out_raw = item;
-       if (!item || !strlen (item)) {
-               if (!out_raw)
-                       g_free (item);
-               return fail_if_missing ? FALSE : TRUE;
-       }
-
-       errno = 0;
-
-       /* Is it a prefix? */
-       if (!strchr (item, '.')) {
-               tmp_prefix = strtol (item, NULL, 10);
-               if (!errno && tmp_prefix >= 0 && tmp_prefix <= 32) {
-                       *out = tmp_prefix;
-                       success = TRUE;
-                       goto out;
-               }
-       }
-
-       /* Is it a netmask? */
-       if (inet_pton (AF_INET, item, &tmp_addr) > 0) {
-               *out = nm_utils_ip4_netmask_to_prefix (tmp_addr.s_addr);
-               success = TRUE;
-       }
-
-out:
-       if (!out_raw)
-               g_free (item);
-       return success;
-}
-
-static gboolean
-get_one_addr (GtkTreeModel *model,
-              GtkTreeIter *iter,
-              int column,
-              gboolean fail_if_missing,
-              char **out,
-              char **out_raw)
-{
-       char *item = NULL;
-       struct in_addr tmp_addr = { 0 };
-
-       gtk_tree_model_get (model, iter, column, &item, -1);
-       if (out_raw)
-               *out_raw = item;
-       if (!item || !strlen (item)) {
-               if (!out_raw)
-                       g_free (item);
-               return fail_if_missing ? FALSE : TRUE;
-       }
-
-       if (inet_pton (AF_INET, item, &tmp_addr) == 0)
-               return FALSE;
-
-       if (tmp_addr.s_addr == 0) {
-               if (!out_raw)
-                       g_free (item);
-               return fail_if_missing ? FALSE : TRUE;
-       }
-
-       *out = item;
-       return TRUE;
-}
-
 static void
 validate (GtkWidget *dialog)
 {
@@ -186,24 +72,24 @@ validate (GtkWidget *dialog)
                gint64 metric = -1;
 
                /* Address */
-               if (!get_one_addr (model, &tree_iter, COL_ADDRESS, TRUE, &addr, NULL))
+               if (!utils_tree_model_get_address (model, &tree_iter, COL_ADDRESS, AF_INET, TRUE, &addr, 
NULL))
                        goto done;
                g_free (addr);
 
                /* Prefix */
-               if (!get_one_prefix (model, &tree_iter, COL_PREFIX, TRUE, &prefix, NULL))
+               if (!utils_tree_model_get_ip4_prefix (model, &tree_iter, COL_PREFIX, TRUE, &prefix, NULL))
                        goto done;
                /* Don't allow zero prefix for now - that's not supported in libnm-util */
                if (prefix == 0)
                        goto done;
 
                /* Next hop (optional) */
-               if (!get_one_addr (model, &tree_iter, COL_NEXT_HOP, FALSE, &next_hop, NULL))
+               if (!utils_tree_model_get_address (model, &tree_iter, COL_NEXT_HOP, AF_INET, FALSE, 
&next_hop, NULL))
                        goto done;
                g_free (next_hop);
 
                /* Metric (optional) */
-               if (!get_one_metric (model, &tree_iter, COL_METRIC, FALSE, &metric, NULL))
+               if (!utils_tree_model_get_int64 (model, &tree_iter, COL_METRIC, 0, G_MAXUINT32, FALSE, 
&metric, NULL))
                        goto done;
 
                iter_valid = gtk_tree_model_iter_next (model, &tree_iter);
@@ -696,14 +582,14 @@ cell_error_data_func (GtkTreeViewColumn *tree_column,
        gboolean invalid = FALSE;
 
        if (col == COL_ADDRESS)
-               invalid = !get_one_addr (tree_model, iter, COL_ADDRESS, TRUE, &addr, &value);
+               invalid = !utils_tree_model_get_address (tree_model, iter, COL_ADDRESS, AF_INET, TRUE, &addr, 
&value);
        else if (col == COL_PREFIX)
-               invalid =    !get_one_prefix (tree_model, iter, COL_PREFIX, TRUE, &prefix, &value)
+               invalid =    !utils_tree_model_get_ip4_prefix (tree_model, iter, COL_PREFIX, TRUE, &prefix, 
&value)
                          || prefix == 0;
        else if (col == COL_NEXT_HOP)
-               invalid = !get_one_addr (tree_model, iter, COL_NEXT_HOP, FALSE, &next_hop, &value);
+               invalid = !utils_tree_model_get_address (tree_model, iter, COL_NEXT_HOP, AF_INET, FALSE, 
&next_hop, &value);
        else if (col == COL_METRIC)
-               invalid = !get_one_metric (tree_model, iter, COL_METRIC, FALSE, &metric, &value);
+               invalid = !utils_tree_model_get_int64 (tree_model, iter, COL_METRIC, 0, G_MAXUINT32, FALSE, 
&metric, &value);
        else
                g_warn_if_reached ();
 
@@ -934,27 +820,27 @@ ip4_routes_dialog_update_setting (GtkWidget *dialog, NMSettingIPConfig *s_ip4)
                NMIPRoute *route;
 
                /* Address */
-               if (!get_one_addr (model, &tree_iter, COL_ADDRESS, TRUE, &addr, NULL)) {
+               if (!utils_tree_model_get_address (model, &tree_iter, COL_ADDRESS, AF_INET, TRUE, &addr, 
NULL)) {
                        g_warning ("%s: IPv4 address missing or invalid!", __func__);
                        goto next;
                }
 
                /* Prefix */
-               if (!get_one_prefix (model, &tree_iter, COL_PREFIX, TRUE, &prefix, NULL)) {
+               if (!utils_tree_model_get_ip4_prefix (model, &tree_iter, COL_PREFIX, TRUE, &prefix, NULL)) {
                        g_warning ("%s: IPv4 prefix/netmask missing or invalid!", __func__);
                        g_free (addr);
                        goto next;
                }
 
                /* Next hop (optional) */
-               if (!get_one_addr (model, &tree_iter, COL_NEXT_HOP, FALSE, &next_hop, NULL)) {
+               if (!utils_tree_model_get_address (model, &tree_iter, COL_NEXT_HOP, AF_INET, FALSE, 
&next_hop, NULL)) {
                        g_warning ("%s: IPv4 next hop invalid!", __func__);
                        g_free (addr);
                        goto next;
                }
 
                /* Metric (optional) */
-               if (!get_one_metric (model, &tree_iter, COL_METRIC, FALSE, &metric, NULL)) {
+               if (!utils_tree_model_get_int64 (model, &tree_iter, COL_METRIC, 0, G_MAXUINT32, FALSE, 
&metric, NULL)) {
                        g_warning ("%s: IPv4 metric invalid!", __func__);
                        g_free (addr);
                        g_free (next_hop);
diff --git a/src/connection-editor/ip6-routes-dialog.c b/src/connection-editor/ip6-routes-dialog.c
index 8e7325c..95f81ad 100644
--- a/src/connection-editor/ip6-routes-dialog.c
+++ b/src/connection-editor/ip6-routes-dialog.c
@@ -86,39 +86,6 @@ out:
        return success;
 }
 
-static gboolean
-get_one_addr (GtkTreeModel *model,
-              GtkTreeIter *iter,
-              int column,
-              gboolean fail_if_missing,
-              char **out,
-              char **out_raw)
-{
-       char *item = NULL;
-       struct in6_addr tmp_addr;
-
-       gtk_tree_model_get (model, iter, column, &item, -1);
-       if (out_raw)
-               *out_raw = item;
-       if (!item || !strlen (item)) {
-               if (!out_raw)
-                       g_free (item);
-               return fail_if_missing ? FALSE : TRUE;
-       }
-
-       if (inet_pton (AF_INET6, item, &tmp_addr) == 0)
-               return FALSE;
-
-       if (IN6_IS_ADDR_UNSPECIFIED (&tmp_addr)) {
-               if (!out_raw)
-                       g_free (item);
-               return fail_if_missing ? FALSE : TRUE;
-       }
-
-       *out = item;
-       return TRUE;
-}
-
 static void
 validate (GtkWidget *dialog)
 {
@@ -143,16 +110,16 @@ validate (GtkWidget *dialog)
                gint64 prefix = 0, metric = -1;
 
                /* Address */
-               if (!get_one_addr (model, &tree_iter, COL_ADDRESS, TRUE, &dest, NULL))
+               if (!utils_tree_model_get_address (model, &tree_iter, COL_ADDRESS, AF_INET6, TRUE, &dest, 
NULL))
                        goto done;
                g_free (dest);
 
                /* Prefix */
-               if (!get_one_int64 (model, &tree_iter, COL_PREFIX, 1, 128, TRUE, &prefix, NULL))
+               if (!utils_tree_model_get_int64 (model, &tree_iter, COL_PREFIX, 1, 128, TRUE, &prefix, NULL))
                        goto done;
 
                /* Next hop (optional) */
-               if (!get_one_addr (model, &tree_iter, COL_NEXT_HOP, FALSE, &next_hop, NULL))
+               if (!utils_tree_model_get_address (model, &tree_iter, COL_NEXT_HOP, AF_INET6, FALSE, 
&next_hop, NULL))
                        goto done;
                g_free (next_hop);
 
@@ -640,13 +607,13 @@ cell_error_data_func (GtkTreeViewColumn *tree_column,
        gboolean invalid = FALSE;
 
        if (col == COL_ADDRESS)
-               invalid = !get_one_addr (tree_model, iter, COL_ADDRESS, TRUE, &addr, &value);
+               invalid = !utils_tree_model_get_address (tree_model, iter, COL_ADDRESS, AF_INET6, TRUE, 
&addr, &value);
        else if (col == COL_PREFIX)
-               invalid = !get_one_int64 (tree_model, iter, COL_PREFIX, 1, 128, TRUE, &prefix, &value);
+               invalid = !utils_tree_model_get_int64 (tree_model, iter, COL_PREFIX, 1, 128, TRUE, &prefix, 
&value);
        else if (col == COL_NEXT_HOP)
-               invalid = !get_one_addr (tree_model, iter, COL_NEXT_HOP, FALSE, &next_hop, &value);
+               invalid = !utils_tree_model_get_address (tree_model, iter, COL_NEXT_HOP, AF_INET6, FALSE, 
&next_hop, &value);
        else if (col == COL_METRIC)
-               invalid = !get_one_int64 (tree_model, iter, COL_METRIC, 0, G_MAXUINT32, FALSE, &metric, 
&value);
+               invalid = !utils_tree_model_get_int64 (tree_model, iter, COL_METRIC, 0, G_MAXUINT32, FALSE, 
&metric, &value);
        else
                g_warn_if_reached ();
 
@@ -873,27 +840,27 @@ ip6_routes_dialog_update_setting (GtkWidget *dialog, NMSettingIPConfig *s_ip6)
                NMIPRoute *route;
 
                /* Address */
-               if (!get_one_addr (model, &tree_iter, COL_ADDRESS, TRUE, &dest, NULL)) {
+               if (!utils_tree_model_get_address (model, &tree_iter, COL_ADDRESS, AF_INET6, TRUE, &dest, 
NULL)) {
                        g_warning ("%s: IPv6 address missing or invalid!", __func__);
                        goto next;
                }
 
                /* Prefix */
-               if (!get_one_int64 (model, &tree_iter, COL_PREFIX, 1, 128, TRUE, &prefix, NULL)) {
+               if (!utils_tree_model_get_int64 (model, &tree_iter, COL_PREFIX, 1, 128, TRUE, &prefix, NULL)) 
{
                        g_warning ("%s: IPv6 prefix missing or invalid!", __func__);
                        g_free (dest);
                        goto next;
                }
 
                /* Next hop (optional) */
-               if (!get_one_addr (model, &tree_iter, COL_NEXT_HOP, FALSE, &next_hop, NULL)) {
+               if (!utils_tree_model_get_address (model, &tree_iter, COL_NEXT_HOP, AF_INET6, FALSE, 
&next_hop, NULL)) {
                        g_warning ("%s: IPv6 next hop invalid!", __func__);
                        g_free (dest);
                        goto next;
                }
 
                /* Metric (optional) */
-               if (!get_one_int64 (model, &tree_iter, COL_METRIC, 0, G_MAXUINT32, FALSE, &metric, NULL)) {
+               if (!utils_tree_model_get_int64 (model, &tree_iter, COL_METRIC, 0, G_MAXUINT32, FALSE, 
&metric, NULL)) {
                        g_warning ("%s: IPv6 metric invalid!", __func__);
                        g_free (dest);
                        g_free (next_hop);
diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am
index aac0155..22d3b0c 100644
--- a/src/utils/Makefile.am
+++ b/src/utils/Makefile.am
@@ -1,10 +1,16 @@
 SUBDIRS= . tests
 
+shared_files = \
+       $(top_srcdir)/shared/nm-utils/nm-shared-utils.c \
+       $(top_srcdir)/shared/nm-utils/nm-shared-utils.h \
+       $(NULL)
+
 noinst_LTLIBRARIES = \
        libutils-libnm-glib.la \
        libutils-libnm.la
 
 libutils_libnm_glib_la_SOURCES = \
+       $(shared_files) \
        utils.c \
        utils.h
 
@@ -12,20 +18,21 @@ libutils_libnm_glib_la_CPPFLAGS = \
        -DNETWORKMANAGER_COMPILATION=NM_NETWORKMANAGER_COMPILATION_LIB_LEGACY \
        $(GTK_CFLAGS) \
        $(LIBNM_GLIB_CFLAGS) \
-       "-I${top_srcdir}/shared/"
+       -I$(top_srcdir)/shared/
 
 libutils_libnm_glib_la_LIBADD = \
        $(GTK_LIBS) \
        $(LIBNM_GLIB_LIBS)
 
 libutils_libnm_la_SOURCES = \
+       $(shared_files) \
        utils.c \
        utils.h
 
 libutils_libnm_la_CPPFLAGS = \
        $(GTK_CFLAGS) \
        $(LIBNM_CFLAGS) \
-       "-I${top_srcdir}/shared/"
+       -I$(top_srcdir)/shared/
 
 libutils_libnm_la_LIBADD = \
        $(GTK_LIBS) \
diff --git a/src/utils/utils.c b/src/utils/utils.c
index 23e41c3..c3b806e 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -25,8 +25,13 @@
 #include "utils.h"
 
 #include <string.h>
+#include <errno.h>
+#include <arpa/inet.h>
 #include <netinet/ether.h>
 
+#include "nm-utils.h"
+#include "nm-utils/nm-shared-utils.h"
+
 /*
  * utils_ether_addr_valid
  *
@@ -385,3 +390,132 @@ widget_unset_error (GtkWidget *widget)
 
        gtk_style_context_remove_class (gtk_widget_get_style_context (widget), "error");
 }
+
+gboolean
+utils_tree_model_get_int64 (GtkTreeModel *model,
+                            GtkTreeIter *iter,
+                            int column,
+                            gint64 min_value,
+                            gint64 max_value,
+                            gboolean fail_if_missing,
+                            gint64 *out,
+                            char **out_raw)
+{
+       char *item = NULL;
+       gboolean success = FALSE;
+       gint64 val;
+
+       g_return_val_if_fail (model, FALSE);
+       g_return_val_if_fail (iter, FALSE);
+
+       gtk_tree_model_get (model, iter, column, &item, -1);
+       if (out_raw)
+               *out_raw = item;
+       if (!item || !strlen (item)) {
+               if (!out_raw)
+                       g_free (item);
+               return fail_if_missing ? FALSE : TRUE;
+       }
+
+       val = _nm_utils_ascii_str_to_int64 (item, 10, min_value, max_value, 0);
+       if (errno)
+               goto out;
+
+       *out = val;
+       success = TRUE;
+out:
+       if (!out_raw)
+               g_free (item);
+       return success;
+}
+
+gboolean
+utils_tree_model_get_address (GtkTreeModel *model,
+                              GtkTreeIter *iter,
+                              int column,
+                              int family,
+                              gboolean fail_if_missing,
+                              char **out,
+                              char **out_raw)
+{
+       char *item = NULL;
+       union {
+               struct in_addr addr4;
+               struct in6_addr addr6;
+       } tmp_addr;
+
+       g_return_val_if_fail (model, FALSE);
+       g_return_val_if_fail (iter, FALSE);
+       g_return_val_if_fail (family == AF_INET || family == AF_INET6, FALSE);
+
+       gtk_tree_model_get (model, iter, column, &item, -1);
+       if (out_raw)
+               *out_raw = item;
+       if (!item || !strlen (item)) {
+               if (!out_raw)
+                       g_free (item);
+               return fail_if_missing ? FALSE : TRUE;
+       }
+
+       if (inet_pton (family, item, &tmp_addr) == 0)
+               return FALSE;
+
+       if (   (family == AF_INET && tmp_addr.addr4.s_addr == 0)
+           || (family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED (&tmp_addr.addr6))) {
+               if (!out_raw)
+                       g_free (item);
+               return fail_if_missing ? FALSE : TRUE;
+       }
+
+       *out = item;
+       return TRUE;
+}
+
+gboolean
+utils_tree_model_get_ip4_prefix (GtkTreeModel *model,
+                                 GtkTreeIter *iter,
+                                 int column,
+                                 gboolean fail_if_missing,
+                                 guint32 *out,
+                                 char **out_raw)
+{
+       char *item = NULL;
+       struct in_addr tmp_addr = { 0 };
+       gboolean success = FALSE;
+       glong tmp_prefix;
+
+       g_return_val_if_fail (model, FALSE);
+       g_return_val_if_fail (iter, FALSE);
+
+       gtk_tree_model_get (model, iter, column, &item, -1);
+       if (out_raw)
+               *out_raw = item;
+       if (!item || !strlen (item)) {
+               if (!out_raw)
+                       g_free (item);
+               return fail_if_missing ? FALSE : TRUE;
+       }
+
+       errno = 0;
+
+       /* Is it a prefix? */
+       if (!strchr (item, '.')) {
+               tmp_prefix = strtol (item, NULL, 10);
+               if (!errno && tmp_prefix >= 0 && tmp_prefix <= 32) {
+                       *out = tmp_prefix;
+                       success = TRUE;
+                       goto out;
+               }
+       }
+
+       /* Is it a netmask? */
+       if (inet_pton (AF_INET, item, &tmp_addr) > 0) {
+               *out = nm_utils_ip4_netmask_to_prefix (tmp_addr.s_addr);
+               success = TRUE;
+       }
+
+out:
+       if (!out_raw)
+               g_free (item);
+       return success;
+}
diff --git a/src/utils/utils.h b/src/utils/utils.h
index ab90366..544833a 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -84,5 +84,29 @@ void utils_fake_return_key (GdkEventKey *event);
 void widget_set_error   (GtkWidget *widget);
 void widget_unset_error (GtkWidget *widget);
 
+gboolean utils_tree_model_get_int64 (GtkTreeModel *model,
+                                     GtkTreeIter *iter,
+                                     int column,
+                                     gint64 min_value,
+                                     gint64 max_value,
+                                     gboolean fail_if_missing,
+                                     gint64 *out,
+                                     char **out_raw);
+
+gboolean utils_tree_model_get_address (GtkTreeModel *model,
+                                       GtkTreeIter *iter,
+                                       int column,
+                                       int family,
+                                       gboolean fail_if_missing,
+                                       char **out,
+                                       char **out_raw);
+
+gboolean utils_tree_model_get_ip4_prefix (GtkTreeModel *model,
+                                          GtkTreeIter *iter,
+                                          int column,
+                                          gboolean fail_if_missing,
+                                          guint32 *out,
+                                          char **out_raw);
+
 #endif /* UTILS_H */
 


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