[network-manager-applet/lr/import: 4/5] streamline import



commit 97cf9ee189e229c475ade4a7ac226fa41b3b9335
Author: Lubomir Rintel <lkundrak v3 sk>
Date:   Thu Oct 20 18:46:50 2016 +0200

    streamline import

 src/connection-editor/page-vpn.c |  148 ++++++++++++++++----------------------
 1 files changed, 62 insertions(+), 86 deletions(-)
---
diff --git a/src/connection-editor/page-vpn.c b/src/connection-editor/page-vpn.c
index 91bd47d..1c021c2 100644
--- a/src/connection-editor/page-vpn.c
+++ b/src/connection-editor/page-vpn.c
@@ -191,61 +191,6 @@ typedef struct {
        gpointer user_data;
 } NewVpnInfo;
 
-static void
-import_cb (NMConnection *connection, gpointer user_data)
-{
-       NewVpnInfo *info = (NewVpnInfo *) user_data;
-       NMSettingConnection *s_con;
-       NMSettingVpn *s_vpn;
-       const char *service_type;
-       char *s;
-       GError *error = NULL;
-
-       /* Basic sanity checks of the connection */
-       s_con = nm_connection_get_setting_connection (connection);
-       if (!s_con) {
-               s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());
-               nm_connection_add_setting (connection, NM_SETTING (s_con));
-       }
-
-       s = (char *) nm_setting_connection_get_id (s_con);
-       if (!s) {
-               const GPtrArray *connections;
-
-               connections = nm_client_get_connections (info->client);
-               s = ce_page_get_next_available_name (connections, _("VPN connection %d"));
-               g_object_set (s_con, NM_SETTING_CONNECTION_ID, s, NULL);
-               g_free (s);
-       }
-
-       s = (char *) nm_setting_connection_get_connection_type (s_con);
-       if (!s || strcmp (s, NM_SETTING_VPN_SETTING_NAME))
-               g_object_set (s_con, NM_SETTING_CONNECTION_TYPE, NM_SETTING_VPN_SETTING_NAME, NULL);
-
-       s = (char *) nm_setting_connection_get_uuid (s_con);
-       if (!s) {
-               s = nm_utils_uuid_generate ();
-               g_object_set (s_con, NM_SETTING_CONNECTION_UUID, s, NULL);
-               g_free (s);
-       }
-
-       s_vpn = nm_connection_get_setting_vpn (connection);
-       service_type = s_vpn ? nm_setting_vpn_get_service_type (s_vpn) : NULL;
-
-       if (!service_type || !strlen (service_type)) {
-               g_object_unref (connection);
-               connection = NULL;
-
-               error = g_error_new_literal (NMA_ERROR, NMA_ERROR_GENERIC,
-                                            _("The VPN plugin failed to import the VPN connection 
correctly\n\nError: no VPN service type."));
-       }
-
-       info->result_func (connection, FALSE, error, info->user_data);
-       g_clear_error (&error);
-       g_object_unref (info->client);
-       g_slice_free (NewVpnInfo, info);
-}
-
 typedef void (*VpnImportSuccessCallback) (NMConnection *connection, gpointer user_data);
 
 typedef struct {
@@ -257,7 +202,11 @@ static void
 import_vpn_from_file_cb (GtkWidget *dialog, gint response, gpointer user_data)
 {
        char *filename = NULL;
-       ActionInfo *info = (ActionInfo *) user_data;
+       NewVpnInfo *info = (NewVpnInfo *) user_data;
+       NMSettingConnection *s_con;
+       NMSettingVpn *s_vpn;
+       const char *service_type;
+       char *s;
        GSList *iter;
        NMConnection *connection = NULL;
        GError *error = NULL;
@@ -279,9 +228,48 @@ import_vpn_from_file_cb (GtkWidget *dialog, gint response, gpointer user_data)
                connection = nm_vpn_editor_plugin_import (plugin, filename, &error);
        }
 
-       if (connection)
-               info->callback (connection, info->user_data);
-       else {
+       if (connection) {
+               /* Basic sanity checks of the connection */
+               s_con = nm_connection_get_setting_connection (connection);
+               if (!s_con) {
+                       s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());
+                       nm_connection_add_setting (connection, NM_SETTING (s_con));
+               }
+       
+               s = (char *) nm_setting_connection_get_id (s_con);
+               if (!s) {
+                       const GPtrArray *connections;
+       
+                       connections = nm_client_get_connections (info->client);
+                       s = ce_page_get_next_available_name (connections, _("VPN connection %d"));
+                       g_object_set (s_con, NM_SETTING_CONNECTION_ID, s, NULL);
+                       g_free (s);
+               }
+       
+               s = (char *) nm_setting_connection_get_connection_type (s_con);
+               if (!s || strcmp (s, NM_SETTING_VPN_SETTING_NAME))
+                       g_object_set (s_con, NM_SETTING_CONNECTION_TYPE, NM_SETTING_VPN_SETTING_NAME, NULL);
+       
+               s = (char *) nm_setting_connection_get_uuid (s_con);
+               if (!s) {
+                       s = nm_utils_uuid_generate ();
+                       g_object_set (s_con, NM_SETTING_CONNECTION_UUID, s, NULL);
+                       g_free (s);
+               }
+       
+               s_vpn = nm_connection_get_setting_vpn (connection);
+               service_type = s_vpn ? nm_setting_vpn_get_service_type (s_vpn) : NULL;
+       
+               if (!service_type || !strlen (service_type)) {
+                       g_object_unref (connection);
+                       connection = NULL;
+       
+                       error = g_error_new_literal (NMA_ERROR, NMA_ERROR_GENERIC,
+                                                    _("The VPN plugin failed to import the VPN connection 
correctly\n\nError: no VPN service type."));
+               }
+       
+               info->result_func (connection, FALSE, error, info->user_data);
+       } else {
                GtkWidget *err_dialog;
                char *bname = g_path_get_basename (filename);
 
@@ -302,20 +290,30 @@ import_vpn_from_file_cb (GtkWidget *dialog, gint response, gpointer user_data)
 
        g_clear_error (&error);
        g_free (filename);
-
 out:
        gtk_widget_hide (dialog);
        gtk_widget_destroy (dialog);
-       g_free (info);
+       g_object_unref (info->client);
+       g_slice_free (NewVpnInfo, info);
 }
 
-static void
-vpn_import (VpnImportSuccessCallback callback, gpointer user_data)
+void
+vpn_connection_import (GtkWindow *parent,
+                       const char *detail,
+                       gpointer detail_data,
+                       NMClient *client,
+                       PageNewConnectionResultFunc result_func,
+                       gpointer user_data)
 {
+       NewVpnInfo *info;
        GtkWidget *dialog;
-       ActionInfo *info;
        const char *home_folder;
 
+       info = g_slice_new (NewVpnInfo);
+       info->result_func = result_func;
+       info->client = g_object_ref (client);
+       info->user_data = user_data;
+
        dialog = gtk_file_chooser_dialog_new (_("Select file to import"),
                                              NULL,
                                              GTK_FILE_CHOOSER_ACTION_OPEN,
@@ -325,33 +323,11 @@ vpn_import (VpnImportSuccessCallback callback, gpointer user_data)
        home_folder = g_get_home_dir ();
        gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), home_folder);
 
-       info = g_malloc0 (sizeof (ActionInfo));
-       info->callback = callback;
-       info->user_data = user_data;
-
        g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (import_vpn_from_file_cb), info);
        gtk_widget_show_all (dialog);
        gtk_window_present (GTK_WINDOW (dialog));
 }
 
-
-void
-vpn_connection_import (GtkWindow *parent,
-                       const char *detail,
-                       gpointer detail_data,
-                       NMClient *client,
-                       PageNewConnectionResultFunc result_func,
-                       gpointer user_data)
-{
-       NewVpnInfo *info;
-
-       info = g_slice_new (NewVpnInfo);
-       info->result_func = result_func;
-       info->client = g_object_ref (client);
-       info->user_data = user_data;
-       vpn_import (import_cb, info);
-}
-
 #define NEW_VPN_CONNECTION_PRIMARY_LABEL _("Choose a VPN Connection Type")
 #define NEW_VPN_CONNECTION_SECONDARY_LABEL _("Select the type of VPN you wish to use for the new connection. 
If the type of VPN connection you wish to create does not appear in the list, you may not have the correct 
VPN plugin installed.")
 


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