From e435fdfedf56ca83b44f2be76a069d3540a83355 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 17 Jun 2021 23:41:19 +0200 Subject: libnm: extend to_dbus_fcn() property type for efficiently converting string property --- src/libnm-core-impl/nm-setting-connection.c | 12 +++--- src/libnm-core-impl/nm-setting-private.h | 65 +++++++++++++++++++++++++++++ src/libnm-core-impl/nm-setting.c | 25 +++++++++++ src/libnm-core-impl/tests/test-setting.c | 5 +++ src/libnm-core-intern/nm-core-internal.h | 1 + 5 files changed, 102 insertions(+), 6 deletions(-) diff --git a/src/libnm-core-impl/nm-setting-connection.c b/src/libnm-core-impl/nm-setting-connection.c index 81f280be9e..9f78cff022 100644 --- a/src/libnm-core-impl/nm-setting-connection.c +++ b/src/libnm-core-impl/nm-setting-connection.c @@ -1956,12 +1956,12 @@ nm_setting_connection_class_init(NMSettingConnectionClass *klass) * description: Token to generate stable IDs. * ---end--- */ - obj_properties[PROP_STABLE_ID] = g_param_spec_string( - NM_SETTING_CONNECTION_STABLE_ID, - "", - "", - NULL, - G_PARAM_READWRITE | NM_SETTING_PARAM_FUZZY_IGNORE | G_PARAM_STATIC_STRINGS); + _nm_setting_property_define_string(properties_override, + obj_properties, + NM_SETTING_CONNECTION_STABLE_ID, + PROP_STABLE_ID, + NM_SETTING_PARAM_FUZZY_IGNORE, + nm_setting_connection_get_stable_id); /** * NMSettingConnection:interface-name: diff --git a/src/libnm-core-impl/nm-setting-private.h b/src/libnm-core-impl/nm-setting-private.h index 93c2f03623..434cab2ba2 100644 --- a/src/libnm-core-impl/nm-setting-private.h +++ b/src/libnm-core-impl/nm-setting-private.h @@ -254,6 +254,7 @@ extern const NMSettInfoPropertType nm_sett_info_propert_type_plain_i; extern const NMSettInfoPropertType nm_sett_info_propert_type_plain_u; extern const NMSettInfoPropertType nm_sett_info_propert_type_boolean; +extern const NMSettInfoPropertType nm_sett_info_propert_type_string; NMSettingVerifyResult _nm_setting_verify(NMSetting *setting, NMConnection *connection, GError **error); @@ -282,6 +283,14 @@ _nm_setting_property_to_dbus_fcn_get_boolean(const NMSettInfoSetting * NMConnectionSerializationFlags flags, const NMConnectionSerializationOptions *options); +GVariant * +_nm_setting_property_to_dbus_fcn_get_string(const NMSettInfoSetting * sett_info, + guint property_idx, + NMConnection * connection, + NMSetting * setting, + NMConnectionSerializationFlags flags, + const NMConnectionSerializationOptions *options); + GVariant *_nm_setting_to_dbus(NMSetting * setting, NMConnection * connection, NMConnectionSerializationFlags flags, @@ -449,6 +458,62 @@ _nm_properties_override(GArray *properties_override, const NMSettInfoProperty *p /*****************************************************************************/ +#define _nm_setting_property_define_string_full(properties_override, \ + obj_properties, \ + prop_name, \ + prop_id, \ + param_flags, \ + property_type, \ + get_fcn, \ + ...) \ + G_STMT_START \ + { \ + GParamSpec * _param_spec; \ + const NMSettInfoPropertType *const _property_type = (property_type); \ + \ + G_STATIC_ASSERT(!NM_FLAGS_ANY((param_flags), \ + ~(NM_SETTING_PARAM_SECRET | NM_SETTING_PARAM_FUZZY_IGNORE \ + | NM_SETTING_PARAM_INFERRABLE \ + | NM_SETTING_PARAM_REAPPLY_IMMEDIATELY))); \ + nm_assert(_property_type); \ + nm_assert(_property_type->to_dbus_fcn == _nm_setting_property_to_dbus_fcn_get_string); \ + \ + _param_spec = \ + g_param_spec_string("" prop_name "", \ + "", \ + "", \ + NULL, \ + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | (param_flags)); \ + \ + (obj_properties)[(prop_id)] = _param_spec; \ + \ + _nm_properties_override_gobj((properties_override), \ + _param_spec, \ + _property_type, \ + .to_dbus_data.get_string = \ + (const char *(*) (NMSetting *) ) (get_fcn), \ + __VA_ARGS__); \ + } \ + G_STMT_END + +#define _nm_setting_property_define_string(properties_override, \ + obj_properties, \ + prop_name, \ + prop_id, \ + param_flags, \ + get_fcn, \ + ...) \ + _nm_setting_property_define_string_full((properties_override), \ + (obj_properties), \ + prop_name, \ + (prop_id), \ + (param_flags), \ + &nm_sett_info_propert_type_string, \ + (get_fcn), \ + __VA_ARGS__) + +/*****************************************************************************/ + gboolean _nm_setting_use_legacy_property(NMSetting * setting, GVariant * connection_dict, const char *legacy_property, diff --git a/src/libnm-core-impl/nm-setting.c b/src/libnm-core-impl/nm-setting.c index 261c32d4a9..ce40c61d38 100644 --- a/src/libnm-core-impl/nm-setting.c +++ b/src/libnm-core-impl/nm-setting.c @@ -548,6 +548,27 @@ _nm_setting_property_to_dbus_fcn_get_boolean(const NMSettInfoSetting * return g_variant_ref(nm_g_variant_singleton_b(val)); } +GVariant * +_nm_setting_property_to_dbus_fcn_get_string(const NMSettInfoSetting * sett_info, + guint property_idx, + NMConnection * connection, + NMSetting * setting, + NMConnectionSerializationFlags flags, + const NMConnectionSerializationOptions *options) +{ + const NMSettInfoProperty *property_info = &sett_info->property_infos[property_idx]; + const char * val; + + nm_assert(!NM_G_PARAM_SPEC_GET_DEFAULT_STRING(property_info->param_spec)); + + val = property_info->to_dbus_data.get_string(setting); + if (!val) + return NULL; + if (!val[0]) + return g_variant_ref(nm_g_variant_singleton_s_empty()); + return g_variant_new_string(val); +} + GVariant * _nm_setting_property_to_dbus_fcn_gprop(const NMSettInfoSetting * sett_info, guint property_idx, @@ -2377,6 +2398,10 @@ const NMSettInfoPropertType nm_sett_info_propert_type_boolean = NM_SETT_INFO_PRO G_VARIANT_TYPE_BOOLEAN, .to_dbus_fcn = _nm_setting_property_to_dbus_fcn_get_boolean); +const NMSettInfoPropertType nm_sett_info_propert_type_string = + NM_SETT_INFO_PROPERT_TYPE_DBUS_INIT(G_VARIANT_TYPE_STRING, + .to_dbus_fcn = _nm_setting_property_to_dbus_fcn_get_string); + /*****************************************************************************/ static GenData * diff --git a/src/libnm-core-impl/tests/test-setting.c b/src/libnm-core-impl/tests/test-setting.c index 0572de1412..77edf4afa4 100644 --- a/src/libnm-core-impl/tests/test-setting.c +++ b/src/libnm-core-impl/tests/test-setting.c @@ -4407,6 +4407,11 @@ check_done:; g_assert(sip->param_spec); g_assert(sip->param_spec->value_type == G_TYPE_BOOLEAN); g_assert(sip->to_dbus_data.get_boolean); + } else if (sip->property_type->to_dbus_fcn + == _nm_setting_property_to_dbus_fcn_get_string) { + g_assert(sip->param_spec); + g_assert(sip->param_spec->value_type == G_TYPE_STRING); + g_assert(sip->to_dbus_data.get_string); } g_assert(!sip->property_type->from_dbus_fcn diff --git a/src/libnm-core-intern/nm-core-internal.h b/src/libnm-core-intern/nm-core-internal.h index 1fde5d455d..c4e420f0d8 100644 --- a/src/libnm-core-intern/nm-core-internal.h +++ b/src/libnm-core-intern/nm-core-internal.h @@ -712,6 +712,7 @@ struct _NMSettInfoProperty { gpointer none; NMSettInfoPropGPropToDBusFcn gprop_to_dbus_fcn; gboolean (*get_boolean)(NMSetting *); + const char *(*get_string)(NMSetting *); }; } to_dbus_data; }; -- cgit v1.2.3