[network-manager-applet/th/validation-error-bgo754832: 6/6] c-e: show tooltip with validation error



commit bd20efeb915a6f2959e061f75a84abe23fea39e6
Author: Thomas Haller <thaller redhat com>
Date:   Thu Sep 10 16:38:13 2015 +0200

    c-e: show tooltip with validation error

 src/connection-editor/ce-polkit-button.c     |   25 ++++++++---
 src/connection-editor/ce-polkit-button.h     |    2 +-
 src/connection-editor/nm-connection-editor.c |   59 ++++++++++++++++----------
 src/connection-editor/nm-connection-editor.h |    2 +
 src/connection-editor/nm-connection-list.c   |    4 +-
 5 files changed, 60 insertions(+), 32 deletions(-)
---
diff --git a/src/connection-editor/ce-polkit-button.c b/src/connection-editor/ce-polkit-button.c
index 71eb614..2269eb5 100644
--- a/src/connection-editor/ce-polkit-button.c
+++ b/src/connection-editor/ce-polkit-button.c
@@ -34,7 +34,7 @@ G_DEFINE_TYPE (CEPolkitButton, ce_polkit_button, GTK_TYPE_BUTTON)
 typedef struct {
        char *tooltip;
        char *auth_tooltip;
-       gboolean master_sensitive;
+       char *validation_error;
 
        GtkWidget *stock;
        GtkWidget *auth;
@@ -65,7 +65,9 @@ update_button (CEPolkitButton *self)
 
        gtk_widget_set_sensitive (GTK_WIDGET (self), actionable);
 
-       if (priv->permission_result == NM_CLIENT_PERMISSION_RESULT_AUTH)
+       if (priv->validation_error)
+               gtk_widget_set_tooltip_text (GTK_WIDGET (self), priv->validation_error);
+       else if (priv->permission_result == NM_CLIENT_PERMISSION_RESULT_AUTH)
                gtk_widget_set_tooltip_text (GTK_WIDGET (self), priv->auth_tooltip);
        else if (priv->permission_result == NM_CLIENT_PERMISSION_RESULT_YES)
                gtk_widget_set_tooltip_text (GTK_WIDGET (self), priv->tooltip);
@@ -90,16 +92,24 @@ update_and_emit (CEPolkitButton *self, gboolean old_actionable)
 }
 
 void
-ce_polkit_button_set_master_sensitive (CEPolkitButton *self, gboolean sensitive)
+ce_polkit_button_set_validation_error (CEPolkitButton *self, const char *validation_error)
 {
+       CEPolkitButtonPrivate *priv;
        gboolean old_actionable;
 
        g_return_if_fail (self != NULL);
        g_return_if_fail (CE_IS_POLKIT_BUTTON (self));
 
-       old_actionable = ce_polkit_button_get_actionable (self);
-       CE_POLKIT_BUTTON_GET_PRIVATE (self)->master_sensitive = sensitive;
-       update_and_emit (self, old_actionable);
+       priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
+
+       if (g_strcmp0 (validation_error, priv->validation_error) != 0) {
+               old_actionable = ce_polkit_button_get_actionable (self);
+
+               g_free (priv->validation_error);
+               priv->validation_error = g_strdup (validation_error);
+
+               update_and_emit (self, old_actionable);
+       }
 }
 
 gboolean
@@ -112,7 +122,7 @@ ce_polkit_button_get_actionable (CEPolkitButton *self)
 
        priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
 
-       return    priv->master_sensitive
+       return    !priv->validation_error
               && ce_polkit_button_get_authorized (self);
 }
 
@@ -216,6 +226,7 @@ finalize (GObject *object)
 
        g_free (priv->tooltip);
        g_free (priv->auth_tooltip);
+       g_free (priv->validation_error);
 
        G_OBJECT_CLASS (ce_polkit_button_parent_class)->finalize (object);
 }
diff --git a/src/connection-editor/ce-polkit-button.h b/src/connection-editor/ce-polkit-button.h
index da2f3e3..a81963b 100644
--- a/src/connection-editor/ce-polkit-button.h
+++ b/src/connection-editor/ce-polkit-button.h
@@ -56,7 +56,7 @@ GtkWidget *ce_polkit_button_new (const char *label,
                                  NMClient *client,
                                  NMClientPermission permission);
 
-void ce_polkit_button_set_master_sensitive (CEPolkitButton *button, gboolean sensitive);
+void ce_polkit_button_set_validation_error (CEPolkitButton *self, const char *validation_error);
 
 gboolean ce_polkit_button_get_actionable (CEPolkitButton *button);
 
diff --git a/src/connection-editor/nm-connection-editor.c b/src/connection-editor/nm-connection-editor.c
index b4b0711..9c7a1ef 100644
--- a/src/connection-editor/nm-connection-editor.c
+++ b/src/connection-editor/nm-connection-editor.c
@@ -108,7 +108,7 @@ nm_connection_editor_update_title (NMConnectionEditor *editor)
 }
 
 static gboolean
-ui_to_setting (NMConnectionEditor *editor)
+ui_to_setting (NMConnectionEditor *editor, GError **error)
 {
        NMSettingConnection *s_con;
        GtkWidget *widget;
@@ -123,8 +123,10 @@ ui_to_setting (NMConnectionEditor *editor)
        g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_ID, name, NULL);
        nm_connection_editor_update_title (editor);
 
-       if (!name || !strlen (name))
+       if (!name || !strlen (name)) {
+               g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("Missing connection name"));
                return FALSE;
+       }
 
        return TRUE;
 }
@@ -180,43 +182,54 @@ static void
 connection_editor_validate (NMConnectionEditor *editor)
 {
        NMSettingConnection *s_con;
-       gboolean valid = FALSE, printed = FALSE;
        GSList *iter;
+       char *validation_error = NULL;
+       GError *error = NULL;
 
-       if (!editor_is_initialized (editor))
+       if (!editor_is_initialized (editor)) {
+               validation_error = g_strdup (_("Editor initializing..."));
                goto done;
+       }
 
        s_con = nm_connection_get_setting_connection (editor->connection);
        g_assert (s_con);
-       if (nm_setting_connection_get_read_only (s_con))
+       if (nm_setting_connection_get_read_only (s_con)) {
+               validation_error = g_strdup (_("Connection cannot be modified"));
                goto done;
+       }
 
-       if (!ui_to_setting (editor))
+       if (!ui_to_setting (editor, &error)) {
+               validation_error = g_strdup (error->message);
+               g_clear_error (&error);
                goto done;
+       }
 
-       valid = TRUE;
        for (iter = editor->pages; iter; iter = g_slist_next (iter)) {
-               GError *error = NULL;
-
                if (!ce_page_validate (CE_PAGE (iter->data), editor->connection, &error)) {
-                       valid = FALSE;
-
-                       /* FIXME: use the error to indicate which UI widgets are invalid */
-                       if (!printed) {
-                               printed = TRUE;
-                               if (error) {
-                                       g_warning ("Invalid setting %s: %s", CE_PAGE (iter->data)->title, 
error->message);
-                               } else
-                                       g_warning ("Invalid setting %s", CE_PAGE (iter->data)->title);
+                       if (!validation_error) {
+                               validation_error = g_strdup_printf (_("Invalid setting %s: %s"),
+                                                                   CE_PAGE (iter->data)->title,
+                                                                   error->message);
                        }
                        g_clear_error (&error);
                }
        }
 
 done:
-       ce_polkit_button_set_master_sensitive (CE_POLKIT_BUTTON (editor->ok_button), valid);
-       gtk_widget_set_sensitive (editor->export_button, valid);
+       if (g_strcmp0 (validation_error, editor->last_validation_error) != 0) {
+               if (editor->last_validation_error && !validation_error)
+                       g_message ("Connection validates and can be saved");
+               else if (validation_error)
+                       g_message ("Cannot save connection due to error: %s", validation_error);
+               g_free (editor->last_validation_error);
+               editor->last_validation_error = g_strdup (validation_error);
+       }
+       ce_polkit_button_set_validation_error (CE_POLKIT_BUTTON (editor->ok_button), validation_error);
+       gtk_widget_set_sensitive (editor->export_button, !!validation_error);
+
        update_sensitivity (editor);
+
+       g_free (validation_error);
 }
 
 static void
@@ -229,8 +242,8 @@ ok_button_actionable_cb (GtkWidget *button,
 
 static void
 permissions_changed_cb (NMClient *client,
-                           NMClientPermission permission,
-                           NMClientPermissionResult result,                       
+                        NMClientPermission permission,
+                        NMClientPermissionResult result,
                         NMConnectionEditor *editor)
 {
        if (permission != NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM)
@@ -346,6 +359,8 @@ dispose (GObject *object)
        g_signal_handler_disconnect (editor->client, editor->permission_id);
        g_object_unref (editor->client);
 
+       g_clear_pointer (&editor->last_validation_error, g_free);
+
 out:
        G_OBJECT_CLASS (nm_connection_editor_parent_class)->dispose (object);
 }
diff --git a/src/connection-editor/nm-connection-editor.h b/src/connection-editor/nm-connection-editor.h
index 7c953b8..259e8dc 100644
--- a/src/connection-editor/nm-connection-editor.h
+++ b/src/connection-editor/nm-connection-editor.h
@@ -64,6 +64,8 @@ typedef struct {
        gboolean busy;
        gboolean init_run;
        guint validate_id;
+
+       char *last_validation_error;
 } NMConnectionEditor;
 
 typedef struct {
diff --git a/src/connection-editor/nm-connection-list.c b/src/connection-editor/nm-connection-list.c
index 1149433..d961b1f 100644
--- a/src/connection-editor/nm-connection-list.c
+++ b/src/connection-editor/nm-connection-list.c
@@ -389,12 +389,12 @@ pk_button_selection_changed_cb (GtkTreeSelection *selection, gpointer user_data)
                if (connection) {
                        s_con = nm_connection_get_setting_connection (NM_CONNECTION (connection));
                        g_assert (s_con);
-       
+
                        sensitive = !nm_setting_connection_get_read_only (s_con);
                }
        }
 
-       ce_polkit_button_set_master_sensitive (button, sensitive);
+       ce_polkit_button_set_validation_error (button, sensitive ? NULL : _("Connection cannot be modified"));
 }
 
 static void


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