From db300afba179b21a282c2f2602f1ccb7d6b13e08 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 31 Mar 2017 14:45:46 +0200 Subject: [PATCH] cli: split out new file "nm-meta-setting-access.h" "nm-meta-setting-desc.h" contains static type description, vtable and (internal) accessor functions. Add accessor functions that operate on top of the type description to "nm-meta-setting-access.h". --- Makefile.am | 2 + clients/cli/connections.c | 16 +++ clients/cli/settings.c | 144 ++---------------------- clients/cli/settings.h | 1 - clients/common/nm-meta-setting-access.c | 137 ++++++++++++++++++++++ clients/common/nm-meta-setting-access.h | 42 +++++++ 6 files changed, 204 insertions(+), 138 deletions(-) create mode 100644 clients/common/nm-meta-setting-access.c create mode 100644 clients/common/nm-meta-setting-access.h diff --git a/Makefile.am b/Makefile.am index 598dfdfdde..a6fcd011c8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3132,6 +3132,8 @@ clients_cli_nmcli_SOURCES = \ clients/common/nm-client-utils.h \ clients/common/nm-meta-setting-desc.c \ clients/common/nm-meta-setting-desc.h \ + clients/common/nm-meta-setting-access.c \ + clients/common/nm-meta-setting-access.h \ \ clients/cli/agent.c \ clients/cli/agent.h \ diff --git a/clients/cli/connections.c b/clients/cli/connections.c index db146a8900..7bfa8fcf8c 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -616,6 +616,22 @@ usage_connection_export (void) "The data are directed to standard output or to a file if a name is given.\n\n")); } +static NMSetting * +nmc_setting_new_for_name (const char *name) +{ + GType stype; + NMSetting *setting = NULL; + + if (name) { + stype = nm_setting_lookup_type (name); + if (stype != G_TYPE_INVALID) { + setting = g_object_new (stype, NULL); + g_warn_if_fail (NM_IS_SETTING (setting)); + } + } + return setting; +} + /* quit main loop */ static void quit (void) diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 29fe763874..4a1a80d433 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -28,127 +28,13 @@ #include "nm-client-utils.h" #include "nm-vpn-helpers.h" +#include "nm-meta-setting-access.h" #include "utils.h" #include "common.h" /*****************************************************************************/ -static const NMMetaSettingInfoEditor * -_meta_find_setting_info_by_name (const char *setting_name) -{ - const NMMetaSettingInfo *meta_setting_info; - const NMMetaSettingInfoEditor *setting_info; - - g_return_val_if_fail (setting_name, NULL); - - meta_setting_info = nm_meta_setting_infos_by_name (setting_name); - - if (!meta_setting_info) - return NULL; - - g_return_val_if_fail (nm_streq0 (meta_setting_info->setting_name, setting_name), NULL); - - if (meta_setting_info->meta_type >= G_N_ELEMENTS (nm_meta_setting_infos_editor)) - return NULL; - - setting_info = &nm_meta_setting_infos_editor[meta_setting_info->meta_type]; - - g_return_val_if_fail (setting_info->general == meta_setting_info, NULL); - - return setting_info; -} - -static const NMMetaSettingInfoEditor * -_meta_find_setting_info_by_gtype (GType gtype) -{ - const NMMetaSettingInfo *meta_setting_info; - const NMMetaSettingInfoEditor *setting_info; - - meta_setting_info = nm_meta_setting_infos_by_gtype (gtype); - - if (!meta_setting_info) - return NULL; - - g_return_val_if_fail (meta_setting_info->get_setting_gtype, NULL); - g_return_val_if_fail (meta_setting_info->get_setting_gtype () == gtype, NULL); - - if (meta_setting_info->meta_type >= G_N_ELEMENTS (nm_meta_setting_infos_editor)) - return NULL; - - setting_info = &nm_meta_setting_infos_editor[meta_setting_info->meta_type]; - - g_return_val_if_fail (setting_info->general == meta_setting_info, NULL); - - return setting_info; -} - -static const NMMetaSettingInfoEditor * -_meta_find_setting_info_by_setting (NMSetting *setting) -{ - const NMMetaSettingInfoEditor *setting_info; - - g_return_val_if_fail (NM_IS_SETTING (setting), NULL); - - setting_info = _meta_find_setting_info_by_gtype (G_OBJECT_TYPE (setting)); - - if (!setting_info) - return NULL; - - g_return_val_if_fail (setting_info == _meta_find_setting_info_by_name (nm_setting_get_name (setting)), NULL); - - return setting_info; -} - -static const NMMetaPropertyInfo * -_meta_setting_info_find_property_info (const NMMetaSettingInfoEditor *setting_info, const char *property_name) -{ - guint i; - - g_return_val_if_fail (setting_info, NULL); - g_return_val_if_fail (property_name, NULL); - - for (i = 0; i < setting_info->properties_num; i++) { - if (nm_streq (setting_info->properties[i].property_name, property_name)) - return &setting_info->properties[i]; - } - - return NULL; -} - -static const NMMetaPropertyInfo * -_meta_find_property_info_by_name (const char *setting_name, const char *property_name, const NMMetaSettingInfoEditor **out_setting_info) -{ - const NMMetaSettingInfoEditor *setting_info; - - setting_info = _meta_find_setting_info_by_name (setting_name); - - NM_SET_OUT (out_setting_info, setting_info); - if (!setting_info) - return NULL; - return _meta_setting_info_find_property_info (setting_info, property_name); -} - -static const NMMetaPropertyInfo * -_meta_find_property_info_by_setting (NMSetting *setting, const char *property_name, const NMMetaSettingInfoEditor **out_setting_info) -{ - const NMMetaSettingInfoEditor *setting_info; - const NMMetaPropertyInfo *property_info; - - setting_info = _meta_find_setting_info_by_setting (setting); - - NM_SET_OUT (out_setting_info, setting_info); - if (!setting_info) - return NULL; - property_info = _meta_setting_info_find_property_info (setting_info, property_name); - - nm_assert (property_info == _meta_find_property_info_by_name (nm_setting_get_name (setting), property_name, NULL)); - - return property_info; -} - -/*****************************************************************************/ - static const NmcOutputField * _get_nmc_output_fields (const NMMetaSettingInfoEditor *setting_info) { @@ -175,22 +61,6 @@ _get_nmc_output_fields (const NMMetaSettingInfoEditor *setting_info) /*****************************************************************************/ -NMSetting * -nmc_setting_new_for_name (const char *name) -{ - GType stype; - NMSetting *setting = NULL; - - if (name) { - stype = nm_setting_lookup_type (name); - if (stype != G_TYPE_INVALID) { - setting = g_object_new (stype, NULL); - g_warn_if_fail (NM_IS_SETTING (setting)); - } - } - return setting; -} - static gboolean get_answer (const char *prop, const char *value) { @@ -627,7 +497,7 @@ get_property_val (NMSetting *setting, const char *prop, NMMetaAccessorGetType ge g_return_val_if_fail (NM_IS_SETTING (setting), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - if ((property_info = _meta_find_property_info_by_setting (setting, prop, &setting_info))) { + if ((property_info = nm_meta_property_info_find_by_setting (setting, prop, &setting_info))) { if (property_info->is_name) { /* Traditionally, the "name" property was not handled here. * For the moment, skip it from get_property_val(). */ @@ -700,7 +570,7 @@ nmc_setting_set_property (NMSetting *setting, const char *prop, const char *valu g_return_val_if_fail (NM_IS_SETTING (setting), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - if ((property_info = _meta_find_property_info_by_setting (setting, prop, &setting_info))) { + if ((property_info = nm_meta_property_info_find_by_setting (setting, prop, &setting_info))) { if (!value) { /* No value argument sets default value */ @@ -774,7 +644,7 @@ nmc_setting_reset_property (NMSetting *setting, const char *prop, GError **error g_return_val_if_fail (NM_IS_SETTING (setting), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - if ((property_info = _meta_find_property_info_by_setting (setting, prop, &setting_info))) { + if ((property_info = nm_meta_property_info_find_by_setting (setting, prop, &setting_info))) { if (property_info->is_name) { /* Traditionally, the "name" property was not handled here. * For the moment, skip it from get_property_val(). */ @@ -810,7 +680,7 @@ nmc_setting_remove_property_option (NMSetting *setting, g_return_val_if_fail (NM_IS_SETTING (setting), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - if ((property_info = _meta_find_property_info_by_setting (setting, prop, &setting_info))) { + if ((property_info = nm_meta_property_info_find_by_setting (setting, prop, &setting_info))) { if (property_info->is_name) { /* Traditionally, the "name" property was not handled here. * For the moment, skip it from get_property_val(). */ @@ -872,7 +742,7 @@ nmc_setting_get_property_allowed_values (NMSetting *setting, const char *prop, c *out_to_free = NULL; - if ((property_info = _meta_find_property_info_by_setting (setting, prop, &setting_info))) { + if ((property_info = nm_meta_property_info_find_by_setting (setting, prop, &setting_info))) { if (property_info->is_name) { /* Traditionally, the "name" property was not handled here. * For the moment, skip it from get_property_val(). */ @@ -909,7 +779,7 @@ nmc_setting_get_property_desc (NMSetting *setting, const char *prop) g_return_val_if_fail (NM_IS_SETTING (setting), FALSE); - if ((property_info = _meta_find_property_info_by_setting (setting, prop, &setting_info))) { + if ((property_info = nm_meta_property_info_find_by_setting (setting, prop, &setting_info))) { const char *desc = NULL; if (property_info->describe_doc) { diff --git a/clients/cli/settings.h b/clients/cli/settings.h index 7b99f8598d..6235c08fdf 100644 --- a/clients/cli/settings.h +++ b/clients/cli/settings.h @@ -28,7 +28,6 @@ /*****************************************************************************/ -NMSetting *nmc_setting_new_for_name (const char *name); void nmc_setting_custom_init (NMSetting *setting); void nmc_setting_ip4_connect_handlers (NMSettingIPConfig *setting); void nmc_setting_ip6_connect_handlers (NMSettingIPConfig *setting); diff --git a/clients/common/nm-meta-setting-access.c b/clients/common/nm-meta-setting-access.c new file mode 100644 index 0000000000..2abf5d8b5e --- /dev/null +++ b/clients/common/nm-meta-setting-access.c @@ -0,0 +1,137 @@ +/* NetworkManager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2010 - 2017 Red Hat, Inc. + */ + +#include "nm-default.h" + +#include "nm-meta-setting-access.h" + +/*****************************************************************************/ + +const NMMetaSettingInfoEditor * +nm_meta_setting_info_editor_find_by_name (const char *setting_name) +{ + const NMMetaSettingInfo *meta_setting_info; + const NMMetaSettingInfoEditor *setting_info; + + g_return_val_if_fail (setting_name, NULL); + + meta_setting_info = nm_meta_setting_infos_by_name (setting_name); + + if (!meta_setting_info) + return NULL; + + g_return_val_if_fail (nm_streq0 (meta_setting_info->setting_name, setting_name), NULL); + + if (meta_setting_info->meta_type >= G_N_ELEMENTS (nm_meta_setting_infos_editor)) + return NULL; + + setting_info = &nm_meta_setting_infos_editor[meta_setting_info->meta_type]; + + g_return_val_if_fail (setting_info->general == meta_setting_info, NULL); + + return setting_info; +} + +const NMMetaSettingInfoEditor * +nm_meta_setting_info_editor_find_by_gtype (GType gtype) +{ + const NMMetaSettingInfo *meta_setting_info; + const NMMetaSettingInfoEditor *setting_info; + + meta_setting_info = nm_meta_setting_infos_by_gtype (gtype); + + if (!meta_setting_info) + return NULL; + + g_return_val_if_fail (meta_setting_info->get_setting_gtype, NULL); + g_return_val_if_fail (meta_setting_info->get_setting_gtype () == gtype, NULL); + + if (meta_setting_info->meta_type >= G_N_ELEMENTS (nm_meta_setting_infos_editor)) + return NULL; + + setting_info = &nm_meta_setting_infos_editor[meta_setting_info->meta_type]; + + g_return_val_if_fail (setting_info->general == meta_setting_info, NULL); + + return setting_info; +} + +static const NMMetaSettingInfoEditor * +nm_meta_setting_info_editor_find_by_setting (NMSetting *setting) +{ + const NMMetaSettingInfoEditor *setting_info; + + g_return_val_if_fail (NM_IS_SETTING (setting), NULL); + + setting_info = nm_meta_setting_info_editor_find_by_gtype (G_OBJECT_TYPE (setting)); + + if (!setting_info) + return NULL; + + g_return_val_if_fail (setting_info == nm_meta_setting_info_editor_find_by_name (nm_setting_get_name (setting)), NULL); + + return setting_info; +} + +const NMMetaPropertyInfo * +nm_meta_setting_info_editor_get_property_info (const NMMetaSettingInfoEditor *setting_info, const char *property_name) +{ + guint i; + + g_return_val_if_fail (setting_info, NULL); + g_return_val_if_fail (property_name, NULL); + + for (i = 0; i < setting_info->properties_num; i++) { + if (nm_streq (setting_info->properties[i].property_name, property_name)) + return &setting_info->properties[i]; + } + + return NULL; +} + +const NMMetaPropertyInfo * +nm_meta_property_info_find_by_name (const char *setting_name, const char *property_name, const NMMetaSettingInfoEditor **out_setting_info) +{ + const NMMetaSettingInfoEditor *setting_info; + + setting_info = nm_meta_setting_info_editor_find_by_name (setting_name); + + NM_SET_OUT (out_setting_info, setting_info); + if (!setting_info) + return NULL; + return nm_meta_setting_info_editor_get_property_info (setting_info, property_name); +} + +const NMMetaPropertyInfo * +nm_meta_property_info_find_by_setting (NMSetting *setting, const char *property_name, const NMMetaSettingInfoEditor **out_setting_info) +{ + const NMMetaSettingInfoEditor *setting_info; + const NMMetaPropertyInfo *property_info; + + setting_info = nm_meta_setting_info_editor_find_by_setting (setting); + + NM_SET_OUT (out_setting_info, setting_info); + if (!setting_info) + return NULL; + property_info = nm_meta_setting_info_editor_get_property_info (setting_info, property_name); + + nm_assert (property_info == nm_meta_property_info_find_by_name (nm_setting_get_name (setting), property_name, NULL)); + + return property_info; +} diff --git a/clients/common/nm-meta-setting-access.h b/clients/common/nm-meta-setting-access.h new file mode 100644 index 0000000000..24f271a84a --- /dev/null +++ b/clients/common/nm-meta-setting-access.h @@ -0,0 +1,42 @@ +/* NetworkManager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2010 - 2017 Red Hat, Inc. + */ + +#ifndef _NM_META_SETTING_ACCESS_H__ +#define _NM_META_SETTING_ACCESS_H__ + +#include "nm-meta-setting.h" +#include "nm-meta-setting-desc.h" + +/*****************************************************************************/ + +const NMMetaSettingInfoEditor *nm_meta_setting_info_editor_find_by_name (const char *setting_name); +const NMMetaSettingInfoEditor *nm_meta_setting_info_editor_find_by_gtype (GType gtype); + +const NMMetaPropertyInfo *nm_meta_setting_info_editor_get_property_info (const NMMetaSettingInfoEditor *setting_info, + const char *property_name); +const NMMetaPropertyInfo *nm_meta_property_info_find_by_name (const char *setting_name, + const char *property_name, + const NMMetaSettingInfoEditor **out_setting_info); +const NMMetaPropertyInfo *nm_meta_property_info_find_by_setting (NMSetting *setting, + const char *property_name, + const NMMetaSettingInfoEditor **out_setting_info); + +/*****************************************************************************/ + +#endif /* _NM_META_SETTING_ACCESS_H__ */