[network-manager-applet/bg/wireguard: 10/11] editor: rework assignment of text values to spin buttons



commit b4f4d37f121059c319dbdf186c79beb58e97e67a
Author: Beniamino Galvani <bgalvani redhat com>
Date:   Mon Mar 9 19:22:12 2020 +0100

    editor: rework assignment of text values to spin buttons
    
    Remove duplicate code and use a common function to set the mapping
    between a numeric value and a text one. I think we should not
    encourage the use of arbitrary text but instead allow the most common
    ones. Therefore, add also 'off'.

 src/connection-editor/ce-page.c | 96 ++++++++++++++++++-----------------------
 src/connection-editor/ce-page.h |  4 +-
 2 files changed, 43 insertions(+), 57 deletions(-)
---
diff --git a/src/connection-editor/ce-page.c b/src/connection-editor/ce-page.c
index 44a3868e..1e523e47 100644
--- a/src/connection-editor/ce-page.c
+++ b/src/connection-editor/ce-page.c
@@ -35,98 +35,86 @@ enum {
 
 static guint signals[LAST_SIGNAL] = { 0 };
 
+typedef struct {
+       int value;
+       const char *text;
+} SpinMapping;
+
 static gboolean
-spin_output_with_default_string (GtkSpinButton *spin,
-                                 int defvalue,
-                                 const char *defstring)
+spin_output_with_mapping (GtkSpinButton *spin,
+                          const SpinMapping *mapping)
 {
        int val;
        gchar *buf = NULL;
 
        val = gtk_spin_button_get_value_as_int (spin);
-       if (val == defvalue)
-               buf = g_strdup (defstring);
+       if (val == mapping->value)
+               buf = g_strdup (mapping->text);
        else
                buf = g_strdup_printf ("%d", val);
 
-       if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin))))
+       if (!nm_streq (buf, gtk_entry_get_text (GTK_ENTRY (spin))))
                gtk_entry_set_text (GTK_ENTRY (spin), buf);
 
        g_free (buf);
        return TRUE;
 }
 
-static gboolean
-ce_spin_output_with_automatic (GtkSpinButton *spin, gpointer user_data)
-{
-       return spin_output_with_default_string (spin,
-                                               GPOINTER_TO_INT (user_data),
-                                               _("automatic"));
-}
-
-static gboolean
-ce_spin_output_with_default (GtkSpinButton *spin, gpointer user_data)
-{
-       return spin_output_with_default_string (spin,
-                                               GPOINTER_TO_INT (user_data),
-                                               _("default"));
-}
-
 static gint
-spin_input_with_default_string (GtkSpinButton *spin,
-                                int defvalue,
-                                gdouble *new_val,
-                                const char *defstring)
+spin_input_with_mapping (GtkSpinButton *spin,
+                         gdouble *new_val,
+                         const SpinMapping *mapping)
 {
        const gchar *buf;
 
        buf = gtk_entry_get_text (GTK_ENTRY (spin));
-       if (strcmp (buf, defstring) == 0) {
-               *new_val = defvalue;
+       if (nm_streq (buf, mapping->text)) {
+               *new_val = mapping->value;
                return TRUE;
        }
 
        return FALSE;
 }
 
-static gint
-ce_spin_input_with_automatic (GtkSpinButton *spin, gdouble *new_val, gpointer user_data)
+static void
+spin_set_mapping (GtkSpinButton *spin, int value, const char *text)
 {
-       return spin_input_with_default_string (spin,
-                                              GPOINTER_TO_INT (user_data),
-                                              new_val,
-                                              _("automatic"));
-}
+       SpinMapping *mapping;
 
-static gint
-ce_spin_input_with_default (GtkSpinButton *spin, gdouble *new_val, gpointer user_data)
-{
-       return spin_input_with_default_string (spin,
-                                              GPOINTER_TO_INT (user_data),
-                                              new_val,
-                                              _("default"));
+       g_return_if_fail (!g_object_get_data (G_OBJECT (spin), "mapping"));
+
+       mapping = g_new (SpinMapping, 1);
+       *mapping = (SpinMapping) {
+               .value = value,
+               .text = text,
+       };
+
+       g_object_set_data_full (G_OBJECT (spin), "mapping", mapping, g_free);
+
+       g_signal_connect (spin, "output",
+                         G_CALLBACK (spin_output_with_mapping),
+                         mapping);
+       g_signal_connect (spin, "input",
+                         G_CALLBACK (spin_input_with_mapping),
+                         mapping);
 }
 
 void
 ce_spin_automatic_val (GtkSpinButton *spin, int defvalue)
 {
-       g_signal_connect (spin, "output",
-                         G_CALLBACK (ce_spin_output_with_automatic),
-                         GINT_TO_POINTER (defvalue));
-       g_signal_connect (spin, "input",
-                         G_CALLBACK (ce_spin_input_with_automatic),
-                         GINT_TO_POINTER (defvalue));
+       spin_set_mapping (spin, defvalue, _("automatic"));
 }
 
 void
 ce_spin_default_val (GtkSpinButton *spin, int defvalue)
 {
-       g_signal_connect (spin, "output",
-                         G_CALLBACK (ce_spin_output_with_default),
-                         GINT_TO_POINTER (defvalue));
-       g_signal_connect (spin, "input",
-                         G_CALLBACK (ce_spin_input_with_default),
-                         GINT_TO_POINTER (defvalue));
+       spin_set_mapping (spin, defvalue, _("default"));
+}
+
+void
+ce_spin_off_val (GtkSpinButton *spin, int defvalue)
+{
+       spin_set_mapping (spin, defvalue, _("off"));
 }
 
 int
diff --git a/src/connection-editor/ce-page.h b/src/connection-editor/ce-page.h
index 6c1bb012..54df6c65 100644
--- a/src/connection-editor/ce-page.h
+++ b/src/connection-editor/ce-page.h
@@ -84,7 +84,6 @@ typedef struct {
        gboolean    (*inter_page_change)  (CEPage *self);
 } CEPageClass;
 
-
 typedef CEPage* (*CEPageNewFunc)(NMConnectionEditor *editor,
                                  NMConnection *connection,
                                  GtkWindow *parent,
@@ -145,7 +144,7 @@ NMConnectionEditor *ce_page_new_editor (CEPage *self,
 
 void ce_spin_automatic_val (GtkSpinButton *spin, int defvalue);
 void ce_spin_default_val (GtkSpinButton *spin, int defvalue);
-
+void ce_spin_off_val (GtkSpinButton *spin, int defvalue);
 int ce_get_property_default (NMSetting *setting, const char *property_name);
 
 void ce_page_complete_init (CEPage *self,
@@ -187,6 +186,5 @@ CEPage *ce_page_new (GType page_type,
                      const char *widget_name,
                      const char *title);
 
-
 #endif  /* __CE_PAGE_H__ */
 


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