[network-manager-applet/th/vpn-service-info-bgo767197: 8/8] c-e: support multiple "Add VPN connection" entires



commit d70ba56b118593737ec0221a76b6d6e67613ba12
Author: Thomas Haller <thaller redhat com>
Date:   Wed Jun 8 13:23:49 2016 +0200

    c-e: support multiple "Add VPN connection" entires
    
    For one given service-type, the UI shall be able to possibly
    show multiple entires in the list for creating a new connection.
    
    This works by asking the plugin whether it supports multiple
    "add-details". And if so, create an list entry for each add-detail.
    
    When adding a VPN connection, the editor create a NMConnection
    with setting vpn.service-type and setting the add-detail to a
    key as instructed by the plugin.
    
    For example, nm-openvpn supports multiple "vpn.connection-type" such as
    "tls", "static-key", "password", "password-tls". With this change, it could
    generate an entry for each type in nm-c-e's connection-add list. In this
    case, the "add-detail" would be one of the connection-types and
    "add-detail-key" would be "connection-type".
    
    This functionality is not available via
      $ nm-connection-editor -c -t vpn:<SERVICE_TYPE>:<ADD_DETAIL>

 shared/nm-vpn-editor-plugin-call.h         |   62 +++++++++++++++++++
 src/connection-editor/ce-new-connection.ui |    4 +
 src/connection-editor/connection-helpers.c |   91 ++++++++++++++++++++++-----
 src/connection-editor/page-vpn.c           |    5 ++
 src/connection-editor/page-vpn.h           |    5 ++
 5 files changed, 150 insertions(+), 17 deletions(-)
---
diff --git a/shared/nm-vpn-editor-plugin-call.h b/shared/nm-vpn-editor-plugin-call.h
index f38490b..8a372d3 100644
--- a/shared/nm-vpn-editor-plugin-call.h
+++ b/shared/nm-vpn-editor-plugin-call.h
@@ -65,4 +65,66 @@ nm_vpn_editor_plugin_get_service_info (NMVpnEditorPlugin *plugin,
        return TRUE;
 }
 
+#ifndef NM_VPN_EDITOR_PLUGIN_CALL_GET_SERVICE_ADD_DETAILS
+#define NM_VPN_EDITOR_PLUGIN_CALL_GET_SERVICE_ADD_DETAILS "get-service-add-details"
+#endif
+
+static inline char **
+nm_vpn_editor_plugin_get_service_add_details (NMVpnEditorPlugin *plugin,
+                                              const char *service_name)
+{
+       char **details = NULL;
+
+       g_return_val_if_fail (NM_IS_VPN_EDITOR_PLUGIN (plugin), FALSE);
+       g_return_val_if_fail (service_name, FALSE);
+
+       if (!nm_vpn_editor_plugin_call (plugin,
+                                       NM_VPN_EDITOR_PLUGIN_CALL_GET_SERVICE_ADD_DETAILS,
+                                       NULL,
+                                       service_name,
+                                       &details))
+               g_clear_pointer (&details, g_strfreev);
+       else if (!details)
+               return g_new0 (char *, 1);
+       return details;
+}
+
+#ifndef NM_VPN_EDITOR_PLUGIN_CALL_GET_SERVICE_ADD_DETAIL
+#define NM_VPN_EDITOR_PLUGIN_CALL_GET_SERVICE_ADD_DETAIL "get-service-add-detail"
+#endif
+
+static inline gboolean
+nm_vpn_editor_plugin_get_service_add_detail (NMVpnEditorPlugin *plugin,
+                                             const char *service_type,
+                                             const char *add_detail,
+                                             char **out_pretty_name,
+                                             char **out_description,
+                                             char **out_add_detail_key,
+                                             guint *out_flags)
+{
+       gs_free char *pretty_name_local = NULL;
+       gs_free char *description_local = NULL;
+       gs_free char *add_detail_key_local = NULL;
+       guint flags_local = 0;
+
+       g_return_val_if_fail (NM_IS_VPN_EDITOR_PLUGIN (plugin), FALSE);
+       g_return_val_if_fail (service_type, FALSE);
+       g_return_val_if_fail (add_detail, FALSE);
+
+       if (!nm_vpn_editor_plugin_call (plugin,
+                                       NM_VPN_EDITOR_PLUGIN_CALL_GET_SERVICE_ADD_DETAIL,
+                                       NULL,
+                                       service_type,
+                                       add_detail,
+                                       &pretty_name_local,
+                                       &description_local,
+                                       &add_detail_key_local,
+                                       out_flags ?: &flags_local))
+               return FALSE;
+       NM_SET_OUT (out_pretty_name, g_steal_pointer (&pretty_name_local));
+       NM_SET_OUT (out_description, g_steal_pointer (&description_local));
+       NM_SET_OUT (out_add_detail_key, g_steal_pointer (&add_detail_key_local));
+       return TRUE;
+}
+
 #endif /* __NM_VPN_EDITOR_PLUGIN_CALL_H__ */
diff --git a/src/connection-editor/ce-new-connection.ui b/src/connection-editor/ce-new-connection.ui
index c9dfa54..bd641f5 100644
--- a/src/connection-editor/ce-new-connection.ui
+++ b/src/connection-editor/ce-new-connection.ui
@@ -16,6 +16,10 @@
       <column type="GObject"/>
       <!-- column-name vpn_service_type, COL_VPN_SERVICE_TYPE -->
       <column type="gchararray"/>
+      <!-- column-name vpn_service_type, COL_VPN_ADD_DETAIL -->
+      <column type="gchararray"/>
+      <!-- column-name vpn_service_type, COL_VPN_ADD_DETAIL_KEY -->
+      <column type="gchararray"/>
     </columns>
   </object>
   <object class="GtkDialog" id="new_connection_type_dialog">
diff --git a/src/connection-editor/connection-helpers.c b/src/connection-editor/connection-helpers.c
index 8fa378b..748e4ec 100644
--- a/src/connection-editor/connection-helpers.c
+++ b/src/connection-editor/connection-helpers.c
@@ -43,6 +43,8 @@
 #define COL_DESCRIPTION 3
 #define COL_VPN_PLUGIN 4
 #define COL_VPN_SERVICE_TYPE 5
+#define COL_VPN_ADD_DETAIL 6
+#define COL_VPN_ADD_DETAIL_KEY 7
 
 static gint
 sort_types (gconstpointer a, gconstpointer b)
@@ -273,6 +275,9 @@ set_up_connection_type_combo (GtkComboBox *combo,
                        gs_free char *pretty_name = NULL;
                        gs_free char *description = NULL;
                        guint flags;
+                       gs_strfreev char **add_details_free = NULL;
+                       char **add_details;
+                       const char *i_add_detail;
 
                        if (!nm_vpn_editor_plugin_get_service_info (plugin, service_type, NULL, &pretty_name, 
&description, &flags)) {
                                if (!is_alias)
@@ -288,21 +293,61 @@ set_up_connection_type_combo (GtkComboBox *combo,
                        if (!NM_FLAGS_HAS (flags, NM_VPN_EDITOR_PLUGIN_SERVICE_FLAGS_CAN_ADD))
                                goto next;
 
-                       if (show_headers)
-                               markup = g_markup_printf_escaped ("    %s", pretty_name);
-                       else
-                               markup = g_markup_escape_text (pretty_name, -1);
-
-                       gtk_list_store_append (model, &iter);
-                       gtk_list_store_set (model, &iter,
-                                           COL_MARKUP, markup,
-                                           COL_SENSITIVE, TRUE,
-                                           COL_NEW_FUNC, list[vpn_index].new_connection_func,
-                                           COL_DESCRIPTION, description,
-                                           COL_VPN_PLUGIN, plugin,
-                                           COL_VPN_SERVICE_TYPE, service_type,
-                                           -1);
-                       g_free (markup);
+                       add_details_free = nm_vpn_editor_plugin_get_service_add_details (plugin, 
service_type);
+                       add_details = add_details_free;
+                       i_add_detail = add_details ? add_details[0] : NULL;
+                       do {
+                               const char *i_pretty_name, *i_description;
+                               gs_free char *i_pretty_name_free = NULL;
+                               gs_free char *i_description_free = NULL;
+                               gs_free char *i_add_detail_key = NULL;
+
+                               if (i_add_detail) {
+                                       if (!nm_vpn_editor_plugin_get_service_add_detail (plugin, 
service_type, i_add_detail,
+                                                                                         
&i_pretty_name_free, &i_description_free,
+                                                                                         &i_add_detail_key, 
NULL))
+                                               goto i_next;
+                                       if (!i_add_detail_key) {
+                                               /* currently, the only way for a plugin to know the 
add-detail of a new connection
+                                                * is by returning an "add-detail-key", which then is set in 
the VPN connection.
+                                                *
+                                                * We could imaging other schemes, to notify the plugin which 
connection is to be
+                                                * created. This one is sufficient for now, so as is, a 
plugin must return an
+                                                * "add-detail-key", otherwise it cannot work. */
+                                               goto i_next;
+                                       }
+                                       if (!i_pretty_name_free)
+                                               goto i_next;
+                                       i_pretty_name = i_pretty_name_free;
+                                       i_description = i_description_free;
+                               } else {
+                                       i_pretty_name = pretty_name;
+                                       i_description = description;
+                               }
+
+                               if (show_headers)
+                                       markup = g_markup_printf_escaped ("    %s", i_pretty_name);
+                               else
+                                       markup = g_markup_escape_text (i_pretty_name, -1);
+
+                               gtk_list_store_append (model, &iter);
+                               gtk_list_store_set (model, &iter,
+                                                   COL_MARKUP, markup,
+                                                   COL_SENSITIVE, TRUE,
+                                                   COL_NEW_FUNC, list[vpn_index].new_connection_func,
+                                                   COL_DESCRIPTION, i_description,
+                                                   COL_VPN_PLUGIN, plugin,
+                                                   COL_VPN_SERVICE_TYPE, service_type,
+                                                   COL_VPN_ADD_DETAIL, i_add_detail,
+                                                   COL_VPN_ADD_DETAIL_KEY, i_add_detail_key,
+                                                   -1);
+                               g_free (markup);
+
+i_next:
+                               if (!add_details)
+                                       break;
+                               i_add_detail = (++add_details)[0];
+                       } while (i_add_detail);
 
 next:
                        if (!aliases || !aliases[0])
@@ -426,8 +471,12 @@ new_connection_dialog_full (GtkWindow *parent_window,
        int response;
        PageNewConnectionFunc new_func = NULL;
        gs_free char *vpn_service_type = NULL;
+       gs_free char *vpn_add_detail = NULL;
+       gs_free char *vpn_add_detail_key = NULL;
        const char *detail = NULL;
+       gpointer detail_data = NULL;
        GError *error = NULL;
+       CEPageVpnDetailData vpn_data;
 
        /* load GUI */
        gui = gtk_builder_new ();
@@ -462,16 +511,24 @@ new_connection_dialog_full (GtkWindow *parent_window,
                gtk_tree_model_get (gtk_combo_box_get_model (combo), &iter,
                                    COL_NEW_FUNC, &new_func,
                                    COL_VPN_SERVICE_TYPE, &vpn_service_type,
+                                   COL_VPN_ADD_DETAIL, &vpn_add_detail,
+                                   COL_VPN_ADD_DETAIL_KEY, &vpn_add_detail_key,
                                    -1);
-               if (vpn_service_type)
+               if (vpn_service_type) {
+                       memset (&vpn_data, 0, sizeof (vpn_data));
+                       vpn_data.add_detail = vpn_add_detail;
+                       vpn_data.add_detail_key = vpn_add_detail_key;
+
                        detail = vpn_service_type;
+                       detail_data = &vpn_data;
+               }
        }
 
        gtk_widget_destroy (GTK_WIDGET (type_dialog));
        g_object_unref (gui);
 
        if (new_func)
-               new_connection_of_type (parent_window, detail, NULL, client, new_func, result_func, 
user_data);
+               new_connection_of_type (parent_window, detail, detail_data, client, new_func, result_func, 
user_data);
        else
                result_func (NULL, user_data);
 }
diff --git a/src/connection-editor/page-vpn.c b/src/connection-editor/page-vpn.c
index 4b4a45d..a53602d 100644
--- a/src/connection-editor/page-vpn.c
+++ b/src/connection-editor/page-vpn.c
@@ -290,6 +290,7 @@ vpn_connection_new (GtkWindow *parent,
        NMSetting *s_vpn;
        const char *service_type;
        gs_free char *service_type_free = NULL;
+       const CEPageVpnDetailData *data = detail_data;
 
        if (!detail) {
                NewVpnInfo *info;
@@ -319,6 +320,10 @@ vpn_connection_new (GtkWindow *parent,
                                             user_data);
        s_vpn = nm_setting_vpn_new ();
        g_object_set (s_vpn, NM_SETTING_VPN_SERVICE_TYPE, service_type, NULL);
+
+       if (data && data->add_detail_key)
+               nm_setting_vpn_add_data_item ((NMSettingVpn *) s_vpn, data->add_detail_key, data->add_detail);
+
        nm_connection_add_setting (connection, s_vpn);
 
        (*result_func) (connection, FALSE, NULL, user_data);
diff --git a/src/connection-editor/page-vpn.h b/src/connection-editor/page-vpn.h
index e8dddf0..5d65fe6 100644
--- a/src/connection-editor/page-vpn.h
+++ b/src/connection-editor/page-vpn.h
@@ -43,6 +43,11 @@ typedef struct {
        CEPageClass parent;
 } CEPageVpnClass;
 
+typedef struct {
+       char *add_detail;
+       char *add_detail_key;
+} CEPageVpnDetailData;
+
 GType ce_page_vpn_get_type (void);
 
 CEPage *ce_page_vpn_new (NMConnectionEditor *editor,


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