clients: move clients/tui/vpn-helpers.c to clients/common/nm-vpn-helpers.c

The file has not been used up to now. But it is going to be used by both
nmtui and nmcli later.

(cherry picked from commit b8d6bd1a98)
This commit is contained in:
Jiří Klimeš 2015-11-23 17:47:00 +01:00
parent d1be467d38
commit 50867ad4e4
4 changed files with 107 additions and 77 deletions

View file

@ -13,19 +13,14 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2013 Red Hat, Inc.
* Copyright 2013 - 2015 Red Hat, Inc.
*/
/**
* SECTION:vpn-helpers
* SECTION:nm-vpn-helpers
* @short_description: VPN-related utilities
*
* This is copied directly from libnm-gtk and should probably
* eventually move into libnm.
*
* It is also currently unused in nmtui.
*
* FIXME.
* Some functions should probably eventually move into libnm.
*/
#include "config.h"
@ -33,60 +28,83 @@
#include <string.h>
#include <glib.h>
#include <gmodule.h>
#include <glib/gi18n.h>
#include <nm-connection.h>
#include <nm-setting-connection.h>
#include <nm-setting-vpn.h>
#include <NetworkManager.h>
#include "nm-vpn-editor-plugin.h"
#include "vpn-helpers.h"
#include "nm-vpn-helpers.h"
#define NM_VPN_API_SUBJECT_TO_CHANGE
#include "nm-vpn-plugin-ui-interface.h"
#define VPN_NAME_FILES_DIR SYSCONFDIR"/NetworkManager/VPN"
#define VPN_NAME_FILES_DIR NMCONFDIR "/VPN"
#define DEFAULT_DIR_LIB NMLIBDIR"/VPN"
static GHashTable *plugins = NULL;
static gboolean plugins_loaded = FALSE;
static GHashTable *plugins_hash = NULL;
static GSList *plugins_list = NULL;
G_DEFINE_QUARK (NMA_ERROR, nma_error)
#define NMA_ERROR nma_error_quark ()
#define NMA_ERROR_GENERIC 0
NMVpnPluginUiInterface *
vpn_get_plugin_by_service (const char *service)
GQuark nm_vpn_error_quark (void);
G_DEFINE_QUARK (NM_VPN_ERROR, nm_vpn_error)
#define NM_VPN_ERROR nm_vpn_error_quark ()
#define NM_VPN_ERROR_GENERIC 0
NMVpnEditorPlugin *
nm_vpn_get_plugin_by_service (const char *service)
{
NMVpnEditorPlugin *plugin;
const char *str;
char *tmp = NULL;
g_return_val_if_fail (service != NULL, NULL);
return g_hash_table_lookup (plugins, service);
if (G_UNLIKELY (!plugins_loaded))
nm_vpn_get_plugins (NULL);
if (!plugins_hash)
return NULL;
if (g_str_has_prefix (service, NM_DBUS_SERVICE))
str = service;
else
str = tmp = g_strdup_printf ("%s.%s", NM_DBUS_SERVICE, service);
plugin = g_hash_table_lookup (plugins_hash, str);
g_free (tmp);
return plugin;
}
GHashTable *
vpn_get_plugins (GError **error)
GSList *
nm_vpn_get_plugins (GError **error)
{
GDir *dir;
const char *f;
GHashTableIter iter;
NMVpnEditorPlugin *plugin;
if (error)
g_return_val_if_fail (*error == NULL, NULL);
if (plugins)
return plugins;
if (G_LIKELY (plugins_loaded))
return plugins_list;
plugins_loaded = TRUE;
dir = g_dir_open (VPN_NAME_FILES_DIR, 0, NULL);
if (!dir) {
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "Couldn't read VPN .name files directory " VPN_NAME_FILES_DIR ".");
g_set_error (error, NM_VPN_ERROR, NM_VPN_ERROR_GENERIC, "Couldn't read VPN .name files directory " VPN_NAME_FILES_DIR ".");
return NULL;
}
plugins = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free, (GDestroyNotify) g_object_unref);
plugins_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free, (GDestroyNotify) g_object_unref);
while ((f = g_dir_read_name (dir))) {
char *path = NULL, *service = NULL;
char *so_path = NULL, *so_name = NULL;
GKeyFile *keyfile = NULL;
GModule *module;
NMVpnPluginUiFactory factory = NULL;
GModule *module = NULL;
NMVpnEditorPluginFactory factory = NULL;
if (!g_str_has_suffix (f, ".name"))
continue;
@ -101,27 +119,32 @@ vpn_get_plugins (GError **error)
if (!service)
goto next;
so_path = g_key_file_get_string (keyfile, "GNOME", "properties", NULL);
so_path = g_key_file_get_string (keyfile, "libnm", "plugin", NULL);
if (!so_path)
goto next;
/* Remove any path and extension components, then reconstruct path
* to the SO in LIBDIR
*/
so_name = g_path_get_basename (so_path);
g_free (so_path);
so_path = g_strdup_printf ("%s/NetworkManager/%s", LIBDIR, so_name);
g_free (so_name);
if (g_path_is_absolute (so_path))
module = g_module_open (so_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
module = g_module_open (so_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
if (!module) {
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "Cannot load the VPN plugin which provides the "
"service '%s'.", service);
goto next;
/* Remove any path and extension components, then reconstruct path
* to the SO in LIBDIR
*/
so_name = g_path_get_basename (so_path);
g_free (so_path);
so_path = g_strdup_printf ("%s/NetworkManager/%s", NMLIBDIR, so_name);
g_free (so_name);
module = g_module_open (so_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
if (!module) {
g_clear_error (error);
g_set_error (error, NM_VPN_ERROR, NM_VPN_ERROR_GENERIC, "Cannot load the VPN plugin which provides the "
"service '%s'.", service);
goto next;
}
}
if (g_module_symbol (module, "nm_vpn_plugin_ui_factory", (gpointer) &factory)) {
NMVpnPluginUiInterface *plugin;
if (g_module_symbol (module, "nm_vpn_editor_plugin_factory", (gpointer) &factory)) {
GError *factory_error = NULL;
gboolean success = FALSE;
@ -131,33 +154,37 @@ vpn_get_plugins (GError **error)
/* Validate plugin properties */
g_object_get (G_OBJECT (plugin),
NM_VPN_PLUGIN_UI_INTERFACE_NAME, &plug_name,
NM_VPN_PLUGIN_UI_INTERFACE_SERVICE, &plug_service,
NM_VPN_EDITOR_PLUGIN_NAME, &plug_name,
NM_VPN_EDITOR_PLUGIN_SERVICE, &plug_service,
NULL);
if (!plug_name || !strlen (plug_name)) {
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "cannot load VPN plugin in '%s': missing plugin name",
g_clear_error (error);
g_set_error (error, NM_VPN_ERROR, NM_VPN_ERROR_GENERIC, "cannot load VPN plugin in '%s': missing plugin name",
g_module_name (module));
} else if (!plug_service || strcmp (plug_service, service)) {
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "cannot load VPN plugin in '%s': invalid service name",
g_clear_error (error);
g_set_error (error, NM_VPN_ERROR, NM_VPN_ERROR_GENERIC, "cannot load VPN plugin in '%s': invalid service name",
g_module_name (module));
} else {
/* Success! */
g_object_set_data_full (G_OBJECT (plugin), "gmodule", module,
(GDestroyNotify) g_module_close);
g_hash_table_insert (plugins, g_strdup (service), plugin);
g_hash_table_insert (plugins_hash, g_strdup (service), plugin);
success = TRUE;
}
g_free (plug_name);
g_free (plug_service);
} else {
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "cannot load VPN plugin in '%s': %s",
g_clear_error (error);
g_set_error (error, NM_VPN_ERROR, NM_VPN_ERROR_GENERIC, "cannot load VPN plugin in '%s': %s",
g_module_name (module), g_module_error ());
}
if (!success)
g_module_close (module);
} else {
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "cannot locate nm_vpn_plugin_ui_factory() in '%s': %s",
g_clear_error (error);
g_set_error (error, NM_VPN_ERROR, NM_VPN_ERROR_GENERIC, "cannot locate nm_vpn_editor_plugin_factory() in '%s': %s",
g_module_name (module), g_module_error ());
g_module_close (module);
}
@ -170,7 +197,12 @@ vpn_get_plugins (GError **error)
}
g_dir_close (dir);
return plugins;
/* Copy hash to list */
g_hash_table_iter_init (&iter, plugins_hash);
while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &plugin))
plugins_list = g_slist_prepend (plugins_list, plugin);
return plugins_list;
}
#if 0
@ -405,11 +437,11 @@ vpn_export (NMConnection *connection)
#endif
gboolean
vpn_supports_ipv6 (NMConnection *connection)
nm_vpn_supports_ipv6 (NMConnection *connection)
{
NMSettingVpn *s_vpn;
const char *service_type;
NMVpnPluginUiInterface *plugin;
NMVpnEditorPlugin *plugin;
guint32 capabilities;
s_vpn = nm_connection_get_setting_vpn (connection);
@ -418,9 +450,9 @@ vpn_supports_ipv6 (NMConnection *connection)
service_type = nm_setting_vpn_get_service_type (s_vpn);
g_return_val_if_fail (service_type != NULL, FALSE);
plugin = vpn_get_plugin_by_service (service_type);
plugin = nm_vpn_get_plugin_by_service (service_type);
g_return_val_if_fail (plugin != NULL, FALSE);
capabilities = nm_vpn_plugin_ui_interface_get_capabilities (plugin);
return (capabilities & NM_VPN_PLUGIN_UI_CAPABILITY_IPV6) != 0;
capabilities = nm_vpn_editor_plugin_get_capabilities (plugin);
return (capabilities & NM_VPN_EDITOR_PLUGIN_CAPABILITY_IPV6) != 0;
}

View file

@ -13,27 +13,26 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2013 Red Hat, Inc.
* Copyright 2013 - 2015 Red Hat, Inc.
*/
#ifndef _VPN_HELPERS_H_
#define _VPN_HELPERS_H_
#ifndef __NM_VPN_HELPERS_H__
#define __NM_VPN_HELPERS_H__
#include <glib.h>
#include <nm-connection.h>
#define NM_VPN_API_SUBJECT_TO_CHANGE
#include <nm-vpn-plugin-ui-interface.h>
#include <nm-vpn-editor-plugin.h>
GHashTable *vpn_get_plugins (GError **error);
GSList *nm_vpn_get_plugins (GError **error);
NMVpnPluginUiInterface *vpn_get_plugin_by_service (const char *service);
NMVpnEditorPlugin *nm_vpn_get_plugin_by_service (const char *service);
typedef void (*VpnImportSuccessCallback) (NMConnection *connection, gpointer user_data);
void vpn_import (VpnImportSuccessCallback callback, gpointer user_data);
void nm_vpn_import (VpnImportSuccessCallback callback, gpointer user_data);
void vpn_export (NMConnection *connection);
void nm_vpn_export (NMConnection *connection);
gboolean vpn_supports_ipv6 (NMConnection *connection);
gboolean nm_vpn_supports_ipv6 (NMConnection *connection);
#endif /* _VPN_HELPERS_H_ */
#endif /* __NM_VPN_HELPERS_H__ */

View file

@ -32,20 +32,20 @@
#include "nm-editor-utils.h"
#if 0
#include "vpn-helpers.h"
#include "nm-vpn-helpers.h"
static GSList *vpn_plugins;
static gint
sort_vpn_plugins (gconstpointer a, gconstpointer b)
{
NMVpnPluginUiInterface *aa = NM_VPN_PLUGIN_UI_INTERFACE (a);
NMVpnPluginUiInterface *bb = NM_VPN_PLUGIN_UI_INTERFACE (b);
NMVpnEditorPlugin *aa = NM_VPN_EDITOR_PLUGIN (a);
NMVpnEditorPlugin *bb = NM_VPN_EDITOR_PLUGIN (b);
char *aa_desc = NULL, *bb_desc = NULL;
int ret;
g_object_get (aa, NM_VPN_PLUGIN_UI_INTERFACE_NAME, &aa_desc, NULL);
g_object_get (bb, NM_VPN_PLUGIN_UI_INTERFACE_NAME, &bb_desc, NULL);
g_object_get (aa, NM_VPN_EDITOR_PLUGIN_NAME, &aa_desc, NULL);
g_object_get (bb, NM_VPN_EDITOR_PLUGIN_NAME, &bb_desc, NULL);
ret = g_strcmp0 (aa_desc, bb_desc);
@ -224,7 +224,7 @@ nm_editor_utils_get_connection_type_list (void)
#if 0
/* Add "VPN" only if there are plugins */
vpn_plugins_hash = vpn_get_plugins (NULL);
vpn_plugins_hash = nm_vpn_get_plugins (NULL);
have_vpn_plugins = vpn_plugins_hash && g_hash_table_size (vpn_plugins_hash);
if (have_vpn_plugins) {
GHashTableIter iter;

View file

@ -1,4 +1,3 @@
clients/tui/vpn-helpers.c
examples/python/NetworkManager.py
examples/python/systray/eggtrayicon.c
policy/org.freedesktop.NetworkManager.policy.in