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



commit 744f1be253976f62fa33a90e3365ccbb6cf94e97
Author: Jiří Klimeš <jklimes redhat com>
Date:   Tue Sep 17 16:43:54 2013 +0200

    editor: change JSON Team config widget from GEntry to GTextView
    
    and allow importing the config from file.
    
    libteam configuration is a string that can be quite long and 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.ui |   49 ++++++++++++++++--
 src/connection-editor/page-team.c     |   88 ++++++++++++++++++++++++++++-----
 2 files changed, 119 insertions(+), 18 deletions(-)
---
diff --git a/src/connection-editor/ce-page-team.ui b/src/connection-editor/ce-page-team.ui
index 135d043..58ec93e 100644
--- a/src/connection-editor/ce-page-team.ui
+++ b/src/connection-editor/ce-page-team.ui
@@ -14,7 +14,7 @@
     <property name="can_focus">False</property>
     <property name="border_width">12</property>
     <property name="column_spacing">12</property>
-    <property name="row_spacing">6</property>
+    <property name="row_spacing">8</property>
     <child>
       <object class="GtkLabel" id="master_connections_label">
         <property name="visible">True</property>
@@ -41,18 +41,57 @@
       </object>
       <packing>
         <property name="left_attach">0</property>
-        <property name="top_attach">8</property>
+        <property name="top_attach">3</property>
+        <property name="width">2</property>
+        <property name="height">1</property>
       </packing>
     </child>
     <child>
-      <object class="GtkEntry" id="team_json_config">
+      <object class="GtkScrolledWindow" id="scrolledwindow2">
         <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_json_config">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+          </object>
+        </child>
+      </object>
+      <packing>
+        <property name="left_attach">0</property>
+        <property name="top_attach">4</property>
+        <property name="width">2</property>
+        <property name="height">1</property>
+      </packing>
+    </child>
+    <child>
+      <object class="GtkCheckButton" id="team_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">5</property>
+        <property name="width">1</property>
+        <property name="height">1</property>
+      </packing>
+    </child>
+    <child>
+      <object class="GtkFileChooserButton" id="team_json_file_button">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
       </object>
       <packing>
         <property name="left_attach">1</property>
-        <property name="top_attach">8</property>
+        <property name="right_attach">2</property>
+        <property name="top_attach">5</property>
       </packing>
     </child>
     <child>
diff --git a/src/connection-editor/page-team.c b/src/connection-editor/page-team.c
index 8229440..2e1ac21 100644
--- a/src/connection-editor/page-team.c
+++ b/src/connection-editor/page-team.c
@@ -39,8 +39,10 @@ typedef struct {
 
        GtkWindow *toplevel;
 
-       GtkEntry *json_config;
        GtkWidget *json_config_label;
+       GtkTextView *json_config_widget;
+       GtkToggleButton *json_file_checkbox;
+       GtkFileChooserButton *json_file_chooser_button;
 } CEPageTeamPrivate;
 
 static void
@@ -51,17 +53,65 @@ team_private_init (CEPageTeam *self)
 
        builder = CE_PAGE (self)->builder;
 
-       priv->json_config = GTK_ENTRY (gtk_builder_get_object (builder, "team_json_config"));
        priv->json_config_label = GTK_WIDGET (gtk_builder_get_object (builder, "team_json_config_label"));
+       priv->json_config_widget = GTK_TEXT_VIEW (gtk_builder_get_object (builder, "team_json_config"));
+       priv->json_file_checkbox = GTK_TOGGLE_BUTTON (gtk_builder_get_object (builder, 
"team_json_file_checkbox"));
+       priv->json_file_chooser_button = GTK_FILE_CHOOSER_BUTTON (gtk_builder_get_object (builder, 
"team_json_file_button"));
 
-       priv->toplevel = GTK_WINDOW (gtk_widget_get_ancestor (GTK_WIDGET (priv->json_config),
+       priv->toplevel = GTK_WINDOW (gtk_widget_get_ancestor (GTK_WIDGET (priv->json_config_widget),
                                                              GTK_TYPE_WINDOW));
 }
 
 static void
-stuff_changed (GtkWidget *w, gpointer user_data)
+json_config_changed (GObject *object, CEPageTeam *self)
 {
-       ce_page_changed (CE_PAGE (user_data));
+       CEPageTeamPrivate *priv = CE_PAGE_TEAM_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, CEPageTeam *self)
+{
+       CEPageTeamPrivate *priv = CE_PAGE_TEAM_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, CEPageTeam *self)
+{
+       CEPageTeamPrivate *priv = CE_PAGE_TEAM_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
@@ -69,10 +119,19 @@ populate_ui (CEPageTeam *self)
 {
        CEPageTeamPrivate *priv = CE_PAGE_TEAM_GET_PRIVATE (self);
        NMSettingTeam *s_team = priv->setting;
+       GtkTextBuffer *buffer;
        const char *json_config;
 
+       buffer = gtk_text_view_get_buffer (priv->json_config_widget);
        json_config = nm_setting_team_get_config (s_team);
-       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
@@ -115,14 +174,10 @@ add_slave (CEPageMaster *master, NewConnectionResultFunc result_func)
 static void
 finish_setup (CEPageTeam *self, gpointer unused, GError *error, gpointer user_data)
 {
-       CEPageTeamPrivate *priv = CE_PAGE_TEAM_GET_PRIVATE (self);
-
        if (error)
                return;
 
        populate_ui (self);
-
-       g_signal_connect (priv->json_config, "changed", G_CALLBACK (stuff_changed), self);
 }
 
 CEPage *
@@ -168,14 +223,21 @@ static void
 ui_to_setting (CEPageTeam *self)
 {
        CEPageTeamPrivate *priv = CE_PAGE_TEAM_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_CONFIG, json_config,
                      NULL);
+       g_free (json_config);
 }
 
 static gboolean


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