libnm: add nm_setting_option_clear_by_name()

More general purpose API for generic options of settings.

The predicate function is also nicely usable via bindings.

One question is about the form of the predicate. In this case,
it is convenient to pass nm_ethtool_optname_is_coalesce(). On the
other hand, it's not very flexible as it does not accept a user
data argument. Use NMUtilsPredicateStr here, which is not flexible
but convenient for where it's used.
This commit is contained in:
Thomas Haller 2020-05-14 09:16:33 +02:00
parent 1a56a2105c
commit 49db9d8d78
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
5 changed files with 44 additions and 29 deletions

View file

@ -310,8 +310,6 @@ gboolean _nm_setting_get_property (NMSetting *setting, const char *name, GValue
/*****************************************************************************/
typedef gboolean (*NMSettingOptionFilterFcn)(const char *name);
GHashTable *_nm_setting_option_hash (NMSetting *setting,
gboolean create_if_necessary);
@ -325,9 +323,6 @@ guint _nm_setting_option_get_all (NMSetting *setting,
gboolean _nm_setting_option_clear (NMSetting *setting,
const char *optname);
gboolean _nm_setting_option_clear_all (NMSetting *setting,
NMSettingOptionFilterFcn filter);
/*****************************************************************************/
guint nm_setting_ethtool_init_features (NMSettingEthtool *setting,

View file

@ -226,9 +226,8 @@ nm_setting_ethtool_clear_features (NMSettingEthtool *setting)
{
g_return_if_fail (NM_IS_SETTING_ETHTOOL (setting));
if (_nm_setting_option_clear_all (NM_SETTING (setting),
&nm_ethtool_optname_is_feature))
_notify_attributes (setting);
nm_setting_option_clear_by_name (NM_SETTING (setting),
nm_ethtool_optname_is_feature);
}
guint
@ -369,9 +368,8 @@ nm_setting_ethtool_clear_coalesce_all (NMSettingEthtool *setting)
{
g_return_if_fail (NM_IS_SETTING_ETHTOOL (setting));
if (_nm_setting_option_clear_all (NM_SETTING (setting),
&nm_ethtool_optname_is_coalesce))
_notify_attributes (setting);
nm_setting_option_clear_by_name (NM_SETTING (setting),
nm_ethtool_optname_is_coalesce);
}
/**
@ -468,9 +466,8 @@ nm_setting_ethtool_clear_ring_all (NMSettingEthtool *setting)
{
g_return_if_fail (NM_IS_SETTING_ETHTOOL (setting));
if (_nm_setting_option_clear_all (NM_SETTING (setting),
&nm_ethtool_optname_is_ring))
_notify_attributes (setting);
nm_setting_option_clear_by_name (NM_SETTING (setting),
nm_ethtool_optname_is_ring);
}
/*****************************************************************************/

View file

@ -2525,30 +2525,47 @@ _nm_setting_option_clear (NMSetting *setting,
return g_hash_table_remove (ht, optname);
}
gboolean
_nm_setting_option_clear_all (NMSetting *setting,
NMSettingOptionFilterFcn filter)
/**
* nm_setting_option_clear_by_name:
* @setting: the #NMSetting
* @predicate: (allow-none) (scope call): the predicate for which names
* should be clear.
* If the predicate returns %TRUE for an option name, the option
* gets removed. If %NULL, all options will be removed.
*
* Since: 1.26
*/
void
nm_setting_option_clear_by_name (NMSetting *setting,
NMUtilsPredicateStr predicate)
{
GHashTable *ht;
const char *name;
GHashTable *hash;
GHashTableIter iter;
const char *name;
gboolean changed = FALSE;
nm_assert (NM_IS_SETTING (setting));
g_return_if_fail (NM_IS_SETTING (setting));
ht = _nm_setting_option_hash (setting, FALSE);
if (!ht)
return FALSE;
hash = _nm_setting_option_hash (NM_SETTING (setting), FALSE);
if (!hash)
return;
g_hash_table_iter_init (&iter, ht);
while (g_hash_table_iter_next (&iter, (gpointer *) &name, NULL)) {
if (!filter || filter (name)) {
g_hash_table_iter_remove (&iter);
changed = TRUE;
if (!predicate) {
changed = (g_hash_table_size (hash) > 0);
if (changed)
g_hash_table_remove_all (hash);
} else {
g_hash_table_iter_init (&iter, hash);
while (g_hash_table_iter_next (&iter, (gpointer *) &name, NULL)) {
if (predicate (name)) {
g_hash_table_iter_remove (&iter);
changed = TRUE;
}
}
}
return changed;
if (changed)
_nm_setting_option_notify (setting, TRUE);
}
/*****************************************************************************/

View file

@ -366,6 +366,11 @@ NM_AVAILABLE_IN_1_26
const char *const*nm_setting_option_get_all_names (NMSetting *setting,
guint *out_len);
NM_AVAILABLE_IN_1_26
void nm_setting_option_clear_by_name (NMSetting *setting,
NMUtilsPredicateStr predicate);
/*****************************************************************************/
const GVariantType *nm_setting_get_dbus_property_type (NMSetting *setting,

View file

@ -1732,6 +1732,7 @@ global:
nm_setting_match_remove_driver_by_value;
nm_setting_match_remove_kernel_command_line;
nm_setting_match_remove_kernel_command_line_by_value;
nm_setting_option_clear_by_name;
nm_setting_option_get;
nm_setting_option_get_all_names;
nm_setting_option_get_boolean;