From f47eb4f209fab2502884f9be3f1d306140c3e3b6 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 22 Apr 2016 09:12:54 +0200 Subject: libnm/vpn: add nm_vpn_plugin_info_new_search_file() --- libnm-core/nm-vpn-plugin-info.c | 87 ++++++++++++++++++++++++++++++++++------- libnm-core/nm-vpn-plugin-info.h | 4 ++ libnm/libnm.ver | 1 + 3 files changed, 77 insertions(+), 15 deletions(-) diff --git a/libnm-core/nm-vpn-plugin-info.c b/libnm-core/nm-vpn-plugin-info.c index bd2d46e09d..9ffa04706a 100644 --- a/libnm-core/nm-vpn-plugin-info.c +++ b/libnm-core/nm-vpn-plugin-info.c @@ -189,6 +189,23 @@ _sort_files (LoadDirInfo *a, LoadDirInfo *b) nm_vpn_plugin_info_get_filename (b->plugin_info)); } +#define DEFINE_DEFAULT_DIR_LIST(dir) \ + const char *dir[] = { \ + /* We load plugins from NM_VPN_PLUGIN_DIR *and* DEFAULT_DIR*, with + * preference to the former. + * + * load user directory with highest priority. */ \ + _nm_vpn_plugin_info_get_default_dir_user (), \ + \ + /* lib directory has higher priority then etc. The reason is that + * etc is deprecated and used by old plugins. We expect newer plugins + * to install their file in lib, where they have higher priority. + * + * Optimally, there are no duplicates anyway, so it doesn't really matter. */ \ + _nm_vpn_plugin_info_get_default_dir_lib (), \ + _nm_vpn_plugin_info_get_default_dir_etc (), \ + } + /** * _nm_vpn_plugin_info_get_default_dir_etc: * @@ -316,21 +333,7 @@ nm_vpn_plugin_info_list_load () gint64 uid; GSList *list = NULL; GSList *infos, *info; - const char *dir[] = { - /* We load plugins from NM_VPN_PLUGIN_DIR *and* DEFAULT_DIR*, with - * preference to the former. - * - * load user directory with highest priority. */ - _nm_vpn_plugin_info_get_default_dir_user (), - - /* lib directory has higher priority then etc. The reason is that - * etc is deprecated and used by old plugins. We expect newer plugins - * to install their file in lib, where they have higher priority. - * - * Optimally, there are no duplicates anyway, so it doesn't really matter. */ - _nm_vpn_plugin_info_get_default_dir_lib (), - _nm_vpn_plugin_info_get_default_dir_etc (), - }; + DEFINE_DEFAULT_DIR_LIST (dir); uid = getuid (); @@ -349,6 +352,60 @@ nm_vpn_plugin_info_list_load () return list; } +/** + * nm_vpn_plugin_info_new_search_file: + * @name: (allow-none): the name to search for. Either @name or @service + * must be present. + * @service: (allow-none): the service to search for. Either @name or + * @service must be present. + * + * This has the same effect as doing a full nm_vpn_plugin_info_list_load() + * followed by a search for the first matching VPN plugin info that has the + * given @name and/or @service. + * + * Returns: (transfer full): a newly created instance of plugin info + * or %NULL if no matching value was found. + * + * Since: 1.4 + */ +NMVpnPluginInfo * +nm_vpn_plugin_info_new_search_file (const char *name, const char *service) +{ + int i; + gint64 uid; + NMVpnPluginInfo *plugin_info = NULL; + GSList *infos, *info; + DEFINE_DEFAULT_DIR_LIST (dir); + + if (!name && !service) + g_return_val_if_reached (NULL); + + uid = getuid (); + + for (i = 0; !plugin_info && i < G_N_ELEMENTS (dir); i++) { + if ( !dir[i] + || _nm_utils_strv_find_first ((char **) dir, i, dir[i]) >= 0) + continue; + + /* We still must load the entire directory while searching for the matching + * plugin-info. The reason is that reading the directory has no stable + * order and we can only sort them after reading the entire directory -- + * which _nm_vpn_plugin_info_list_load_dir() does. */ + infos = _nm_vpn_plugin_info_list_load_dir (dir[i], TRUE, uid, NULL, NULL); + + for (info = infos; info; info = info->next) { + if ( (!name || nm_streq (nm_vpn_plugin_info_get_name (info->data), name)) + && (!service || nm_streq (nm_vpn_plugin_info_get_service (info->data), service))) { + plugin_info = g_object_ref (info->data); + break; + } + } + + g_slist_free_full (infos, g_object_unref); + } + return plugin_info; +} + /*********************************************************************/ static gboolean diff --git a/libnm-core/nm-vpn-plugin-info.h b/libnm-core/nm-vpn-plugin-info.h index 05ed5c792d..9844625cfc 100644 --- a/libnm-core/nm-vpn-plugin-info.h +++ b/libnm-core/nm-vpn-plugin-info.h @@ -70,6 +70,10 @@ NMVpnPluginInfo *nm_vpn_plugin_info_new_with_data (const char *filename, GKeyFile *keyfile, GError **error); +NM_AVAILABLE_IN_1_4 +NMVpnPluginInfo *nm_vpn_plugin_info_new_search_file (const char *name, + const char *service); + NM_AVAILABLE_IN_1_2 const char *nm_vpn_plugin_info_get_name (NMVpnPluginInfo *self); NM_AVAILABLE_IN_1_2 diff --git a/libnm/libnm.ver b/libnm/libnm.ver index c7f3316e5f..e40a8d8e2e 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -1064,5 +1064,6 @@ global: nm_vpn_editor_plugin_load; nm_vpn_plugin_info_get_auth_dialog; nm_vpn_plugin_info_get_service; + nm_vpn_plugin_info_new_search_file; nm_vpn_plugin_info_supports_hints; } libnm_1_2_0; -- cgit v1.2.1