settings: add nm_settings_plugin_cmp_by_priority() function

Initially I thought I would use this somewhere else. Didn't do so far,
but this seems a useful function to have on its own because also
NMSettings is concerned about the relative priority of plugins.
This commit is contained in:
Thomas Haller 2019-07-17 11:42:16 +02:00
parent e1867d917b
commit 1735a0a8ab
3 changed files with 35 additions and 22 deletions

View file

@ -42,6 +42,32 @@ G_DEFINE_TYPE (NMSettingsPlugin, nm_settings_plugin, G_TYPE_OBJECT)
/*****************************************************************************/
int
nm_settings_plugin_cmp_by_priority (const NMSettingsPlugin *a,
const NMSettingsPlugin *b,
const GSList *plugin_list)
{
nm_assert (NM_IS_SETTINGS_PLUGIN (a));
nm_assert (NM_IS_SETTINGS_PLUGIN (b));
if (a != b) {
int idx_a = g_slist_index ((GSList *) plugin_list, a);
int idx_b = g_slist_index ((GSList *) plugin_list, b);
/* the plugins must be found in the list. */
nm_assert (idx_a >= 0);
nm_assert (idx_b >= 0);
/* plugins that appear first in @plugin_list have higher priority.
* That means: smaller index -> higher priority. Reverse sort. */
NM_CMP_DIRECT (idx_b, idx_a);
}
return 0;
}
/*****************************************************************************/
GSList *
nm_settings_plugin_get_unmanaged_specs (NMSettingsPlugin *self)
{

View file

@ -196,6 +196,12 @@ void _nm_settings_plugin_emit_signal_unrecognized_specs_changed (NMSettingsPlugi
/*****************************************************************************/
int nm_settings_plugin_cmp_by_priority (const NMSettingsPlugin *a,
const NMSettingsPlugin *b,
const GSList *plugin_list);
/*****************************************************************************/
/* forward declare this function from NMSettings. It's used by the ifcfg-rh plugin,
* but that shouldn't include all "nm-settings.h" header. */
NMSettings *nm_settings_get (void);

View file

@ -34,8 +34,6 @@ nm_settings_storage_cmp (NMSettingsStorage *a,
const GSList *plugin_list)
{
NMSettingsStorageClass *klass;
NMSettingsPlugin *plugin_a;
NMSettingsPlugin *plugin_b;
/* Sort by priority.
*
@ -51,28 +49,11 @@ nm_settings_storage_cmp (NMSettingsStorage *a,
NM_CMP_DIRECT (nm_settings_storage_is_keyfile_run (a),
nm_settings_storage_is_keyfile_run (b));
plugin_a = nm_settings_storage_get_plugin (a);
plugin_b = nm_settings_storage_get_plugin (b);
if (plugin_a != plugin_b) {
int idx_a = g_slist_index ((GSList *) plugin_list, plugin_a);
int idx_b = g_slist_index ((GSList *) plugin_list, plugin_b);
/* the plugins must be found in the list. */
nm_assert (idx_a >= 0);
nm_assert (idx_b >= 0);
nm_assert (idx_a != idx_b);
/* plugins that appear first in @plugin_list have higher priority.
* That means: smaller index -> higher priority. Reverse sort. */
NM_CMP_DIRECT (idx_b, idx_a);
/* undecided. We really don't expect unknown plugins here. */
return 0;
}
NM_CMP_RETURN (nm_settings_plugin_cmp_by_priority (nm_settings_storage_get_plugin (a),
nm_settings_storage_get_plugin (b),
plugin_list));
klass = NM_SETTINGS_STORAGE_GET_CLASS (a);
if (klass != NM_SETTINGS_STORAGE_GET_CLASS (b)) {
/* one plugin must return storages of the same type. Otherwise, it's
* unclear how cmp_fcn() should compare them. */