[network-manager-applet/jklimes/team: 7/7] editor: change JSON TeamPort config widget from GEntry to GTextView



commit 97a493418b3f6d0703a641692f29dde5b3aeabf6
Author: Jiří Klimeš <jklimes redhat com>
Date:   Thu Oct 3 17:35:26 2013 +0200

    editor: change JSON TeamPort config widget from GEntry to GTextView
    
    and allow importing the config from file.
    
    libteam configuration is a string that can be quite long. Simple GEntry is
    is not much appropriate for it. This commit uses GTextView instead, so that
    structured multiline JSON config can be typed. In addition to that, the file
    chooser button allows selecting a file whose content will be imported as the
    config text.

 src/connection-editor/ce-page-team-port.ui |   46 ++++++++++++++-
 src/connection-editor/page-team-port.c     |   87 +++++++++++++++++++++++----
 2 files changed, 117 insertions(+), 16 deletions(-)
---
diff --git a/src/connection-editor/ce-page-team-port.ui b/src/connection-editor/ce-page-team-port.ui
index 6f71d47..f88a8d1 100644
--- a/src/connection-editor/ce-page-team-port.ui
+++ b/src/connection-editor/ce-page-team-port.ui
@@ -7,6 +7,7 @@
     <property name="border_width">12</property>
     <property name="column_spacing">12</property>
     <property name="row_spacing">6</property>
+    <property name="column_homogeneous">True</property>
     <child>
       <object class="GtkLabel" id="team_port_json_config_label">
         <property name="visible">True</property>
@@ -22,16 +23,55 @@
       </packing>
     </child>
     <child>
-      <object class="GtkEntry" id="team_port_json_config">
+      <object class="GtkScrolledWindow" id="scrolledwindow1">
         <property name="visible">True</property>
         <property name="can_focus">True</property>
-        <property name="invisible_char">●</property>
+        <property name="shadow_type">in</property>
+        <property name="min_content_height">100</property>
+        <child>
+          <object class="GtkTextView" id="team_port_json_config">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="hexpand">True</property>
+          </object>
+        </child>
+      </object>
+      <packing>
+        <property name="left_attach">0</property>
+        <property name="top_attach">1</property>
+        <property name="width">2</property>
+        <property name="height">1</property>
+      </packing>
+    </child>
+    <child>
+      <object class="GtkCheckButton" id="team_port_json_file_checkbox">
+        <property name="label" translatable="yes">_Load config from file</property>
+        <property name="visible">True</property>
+        <property name="can_focus">True</property>
+        <property name="receives_default">False</property>
+        <property name="use_underline">True</property>
+        <property name="xalign">0</property>
+        <property name="draw_indicator">True</property>
+      </object>
+      <packing>
+        <property name="left_attach">0</property>
+        <property name="top_attach">2</property>
+        <property name="width">1</property>
+        <property name="height">1</property>
+      </packing>
+    </child>
+    <child>
+      <object class="GtkFileChooserButton" id="team_port_json_file_button">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
         <property name="hexpand">True</property>
       </object>
       <packing>
         <property name="left_attach">1</property>
-        <property name="top_attach">0</property>
+        <property name="right_attach">2</property>
+        <property name="top_attach">2</property>
       </packing>
     </child>
+
   </object>
 </interface>
diff --git a/src/connection-editor/page-team-port.c b/src/connection-editor/page-team-port.c
index ca7936c..9f4a6a9 100644
--- a/src/connection-editor/page-team-port.c
+++ b/src/connection-editor/page-team-port.c
@@ -37,8 +37,9 @@ G_DEFINE_TYPE (CEPageTeamPort, ce_page_team_port, CE_TYPE_PAGE)
 typedef struct {
        NMSettingTeamPort *setting;
 
-       GtkEntry *json_config;
-
+       GtkTextView *json_config_widget;
+       GtkToggleButton *json_file_checkbox;
+       GtkFileChooserButton *json_file_chooser_button;
 } CEPageTeamPortPrivate;
 
 static void
@@ -49,13 +50,61 @@ team_port_private_init (CEPageTeamPort *self)
 
        builder = CE_PAGE (self)->builder;
 
-       priv->json_config = GTK_ENTRY (gtk_builder_get_object (builder, "team_port_json_config"));
+       priv->json_config_widget = GTK_TEXT_VIEW (gtk_builder_get_object (builder, "team_port_json_config"));
+       priv->json_file_checkbox = GTK_TOGGLE_BUTTON (gtk_builder_get_object (builder, 
"team_port_json_file_checkbox"));
+       priv->json_file_chooser_button = GTK_FILE_CHOOSER_BUTTON (gtk_builder_get_object (builder, 
"team_port_json_file_button"));
 }
 
 static void
-stuff_changed (GtkWidget *w, gpointer user_data)
+json_config_changed (GObject *object, CEPageTeamPort *self)
 {
-       ce_page_changed (CE_PAGE (user_data));
+       CEPageTeamPortPrivate *priv = CE_PAGE_TEAM_PORT_GET_PRIVATE (self);
+
+       gtk_toggle_button_set_active (priv->json_file_checkbox, FALSE);
+       gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->json_file_chooser_button));
+       gtk_widget_set_sensitive (GTK_WIDGET (priv->json_file_chooser_button), FALSE);
+
+       ce_page_changed (CE_PAGE (self));
+}
+
+static void
+json_file_checkbox_toggled_cb (GtkToggleButton *widget, CEPageTeamPort *self)
+{
+       CEPageTeamPortPrivate *priv = CE_PAGE_TEAM_PORT_GET_PRIVATE (self);
+       gboolean active;
+
+       active = gtk_toggle_button_get_active (widget);
+       gtk_widget_set_sensitive (GTK_WIDGET (priv->json_file_chooser_button), active);
+
+       ce_page_changed (CE_PAGE (self));
+}
+
+static void
+json_file_set_cb (GtkWidget *chooser, CEPageTeamPort *self)
+{
+       CEPageTeamPortPrivate *priv = CE_PAGE_TEAM_PORT_GET_PRIVATE (self);
+       GtkTextBuffer *buffer;
+       char *filename;
+       char *buf = NULL;
+       gsize buf_len;
+
+       /* Put the file content into JSON config text view. */
+       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (priv->json_file_chooser_button));
+       if (filename) {
+               // FIXME: do a cleverer file validity check
+               g_file_get_contents (filename, &buf, &buf_len, NULL);
+               if (buf_len > 100000) {
+                       g_free (buf);
+                       buf = g_strdup (_("Error: file doesn't contain a valid JSON configuration"));
+               }
+       }
+       buffer = gtk_text_view_get_buffer (priv->json_config_widget);
+       g_signal_handlers_block_by_func (buffer, G_CALLBACK (json_config_changed), NULL);
+       gtk_text_buffer_set_text (buffer, buf ? buf : "", -1);
+       g_signal_handlers_unblock_by_func (buffer, G_CALLBACK (json_config_changed), NULL);
+
+       g_free (filename);
+       g_free (buf);
 }
 
 static void
@@ -63,23 +112,28 @@ populate_ui (CEPageTeamPort *self)
 {
        CEPageTeamPortPrivate *priv = CE_PAGE_TEAM_PORT_GET_PRIVATE (self);
        NMSettingTeamPort *s_port = priv->setting;
+       GtkTextBuffer *buffer;
        const char *json_config;
 
+       buffer = gtk_text_view_get_buffer (priv->json_config_widget);
        json_config = nm_setting_team_port_get_config (s_port);
-       gtk_entry_set_text (priv->json_config, json_config ? json_config : "");
+       gtk_text_buffer_set_text (buffer, json_config ? json_config : "", -1);
+
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->json_file_checkbox), FALSE);
+       gtk_widget_set_sensitive (GTK_WIDGET (priv->json_file_chooser_button), FALSE);
+
+       g_signal_connect (buffer, "changed", G_CALLBACK (json_config_changed), self);
+       g_signal_connect (priv->json_file_checkbox, "toggled", G_CALLBACK (json_file_checkbox_toggled_cb), 
self);
+       g_signal_connect (priv->json_file_chooser_button, "file-set", G_CALLBACK (json_file_set_cb), self);
 }
 
 static void
 finish_setup (CEPageTeamPort *self, gpointer unused, GError *error, gpointer user_data)
 {
-       CEPageTeamPortPrivate *priv = CE_PAGE_TEAM_PORT_GET_PRIVATE (self);
-
        if (error)
                return;
 
        populate_ui (self);
-
-       g_signal_connect (priv->json_config, "changed", G_CALLBACK (stuff_changed), self);
 }
 
 CEPage *
@@ -127,14 +181,21 @@ static void
 ui_to_setting (CEPageTeamPort *self)
 {
        CEPageTeamPortPrivate *priv = CE_PAGE_TEAM_PORT_GET_PRIVATE (self);
-       const char *json_config;
+       GtkTextBuffer *buffer;
+       GtkTextIter start, end;
+       char *json_config;
+
+       buffer = gtk_text_view_get_buffer (priv->json_config_widget);
+       gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
+       gtk_text_buffer_get_iter_at_offset (buffer, &end, -1);
+       json_config = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
 
-       json_config = gtk_entry_get_text (priv->json_config);
-       if (!g_strcmp0(json_config, ""))
+       if (g_strcmp0 (json_config, "") == 0)
                json_config = NULL;
        g_object_set (priv->setting,
                      NM_SETTING_TEAM_PORT_CONFIG, json_config,
                      NULL);
+       g_free (json_config);
 }
 
 static gboolean


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