1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA.
19 * Copyright 2007 - 2014 Red Hat, Inc.
20 * Copyright 2007 - 2008 Novell, Inc.
23 #include "nm-default.h"
27 #include <netinet/in.h>
29 #include "nm-property-compare.h"
32 _nm_property_compare_collection (GVariant *value1, GVariant *value2)
34 GVariant *child1, *child2;
38 len1 = g_variant_n_children (value1);
39 len2 = g_variant_n_children (value2);
42 return len1 < len2 ? -1 : len1 > len2;
44 for (i = 0; i < len1; i++) {
45 child1 = g_variant_get_child_value (value1, i);
46 child2 = g_variant_get_child_value (value2, i);
48 ret = nm_property_compare (child1, child2);
49 g_variant_unref (child1);
50 g_variant_unref (child2);
60 _nm_property_compare_strdict (GVariant *value1, GVariant *value2)
64 const char *key, *val1, *val2;
67 len1 = g_variant_n_children (value1);
68 len2 = g_variant_n_children (value2);
71 return len1 < len2 ? -1 : len1 > len2;
73 g_variant_iter_init (&iter, value1);
74 while (g_variant_iter_next (&iter, "{&s&s}", &key, &val1)) {
75 if (!g_variant_lookup (value2, key, "&s", &val2))
78 ret = strcmp (val1, val2);
87 nm_property_compare (GVariant *value1, GVariant *value2)
89 const GVariantType *type1;
90 const GVariantType *type2;
100 type1 = g_variant_get_type (value1);
101 type2 = g_variant_get_type (value2);
103 if (!g_variant_type_equal (type1, type2))
104 return type1 < type2 ? -1 : type1 > type2;
106 if (g_variant_type_is_basic (type1))
107 ret = g_variant_compare (value1, value2);
108 else if (g_variant_is_of_type (value1, G_VARIANT_TYPE ("a{ss}")))
109 ret = _nm_property_compare_strdict (value1, value2);
110 else if (g_variant_type_is_array (type1))
111 ret = _nm_property_compare_collection (value1, value2);
112 else if (g_variant_type_is_tuple (type1))
113 ret = _nm_property_compare_collection (value1, value2);
115 g_warning ("Don't know how to compare variant type '%s'", (const char *) type1);
116 ret = value1 == value2;