summaryrefslogtreecommitdiff
path: root/src/nm-dbus-utils.c
blob: 07fa909d207087a3f24057424a807180c37aeb99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2018 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nm-dbus-utils.h"

#include "nm-dbus-object.h"

/*****************************************************************************/

const GDBusSignalInfo nm_signal_info_property_changed_legacy = NM_DEFINE_GDBUS_SIGNAL_INFO_INIT (
	"PropertiesChanged",
	.args = NM_DEFINE_GDBUS_ARG_INFOS (
		NM_DEFINE_GDBUS_ARG_INFO ("properties", "a{sv}"),
	),
);

GDBusPropertyInfo *
nm_dbus_utils_interface_info_lookup_property (const GDBusInterfaceInfo *interface_info,
                                              const char *property_name,
                                              guint *property_idx)
{
	guint i;

	nm_assert (interface_info);
	nm_assert (property_name);

	/* there is also g_dbus_interface_info_lookup_property(), however that makes use
	 * of a global cache. */
	if (interface_info->properties) {
		for (i = 0; interface_info->properties[i]; i++) {
			GDBusPropertyInfo *info = interface_info->properties[i];

			if (nm_streq (info->name, property_name)) {
				NM_SET_OUT (property_idx, i);
				return info;
			}
		}
	}

	return NULL;
}

GDBusMethodInfo *
nm_dbus_utils_interface_info_lookup_method (const GDBusInterfaceInfo *interface_info,
                                            const char *method_name)
{
	guint i;

	nm_assert (interface_info);
	nm_assert (method_name);

	/* there is also g_dbus_interface_info_lookup_property(), however that makes use
	 * of a global cache. */
	if (interface_info->methods) {
		for (i = 0; interface_info->methods[i]; i++) {
			GDBusMethodInfo *info = interface_info->methods[i];

			if (nm_streq (info->name, method_name))
				return info;
		}
	}

	return NULL;
}

GVariant *
nm_dbus_utils_get_property (GObject *obj,
                            const char *signature,
                            const char *property_name)
{
	GParamSpec *pspec;
	nm_auto_unset_gvalue GValue value = G_VALUE_INIT;

	nm_assert (G_IS_OBJECT (obj));
	nm_assert (g_variant_type_string_is_valid (signature));
	nm_assert (property_name && property_name[0]);

	pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), property_name);
	if (!pspec)
		g_return_val_if_reached (NULL);

	g_value_init (&value, pspec->value_type);
	g_object_get_property (obj, property_name, &value);
	/* returns never-floating variant */
	return g_dbus_gvalue_to_gvariant (&value, G_VARIANT_TYPE (signature));
}

/*****************************************************************************/

void
nm_dbus_utils_g_value_set_object_path (GValue *value, gpointer object)
{
	const char *path;

	g_return_if_fail (!object || NM_IS_DBUS_OBJECT (object));

	if (   object
	    && (path = nm_dbus_object_get_path (object)))
		g_value_set_string (value, path);
	else
		g_value_set_string (value, NULL);
}

void
nm_dbus_utils_g_value_set_object_path_still_exported (GValue *value, gpointer object)
{
	const char *path;

	g_return_if_fail (!object || NM_IS_DBUS_OBJECT (object));

	if (   object
	    && (path = nm_dbus_object_get_path_still_exported (object)))
		g_value_set_string (value, path);
	else
		g_value_set_string (value, "/");
}

void
nm_dbus_utils_g_value_set_object_path_from_hash (GValue *value,
                                                 GHashTable *hash /* has keys of NMDBusObject type. */,
                                                 gboolean expect_all_exported)
{
	NMDBusObject *obj;
	char **strv;
	guint i, n;
	GHashTableIter iter;

	nm_assert (value);
	nm_assert (hash);

	n = g_hash_table_size (hash);
	strv = g_new (char *, n + 1);
	i = 0;
	g_hash_table_iter_init (&iter, hash);
	while (g_hash_table_iter_next (&iter, (gpointer *) &obj, NULL)) {
		const char *path;

		path = nm_dbus_object_get_path_still_exported (obj);
		if (!path) {
			nm_assert (!expect_all_exported);
			continue;
		}
		strv[i++] = g_strdup (path);
	}
	nm_assert (i <= n);
	strv[i] = NULL;

	/* sort the names, to give a well-defined, stable order. */
	nm_utils_strv_sort (strv, i);

	g_value_take_boxed (value, strv);
}

const char **
nm_dbus_utils_get_paths_for_clist (const CList *lst_head,
                                   gssize lst_len,
                                   guint member_offset,
                                   gboolean expect_all_exported)
{
	const CList *iter;
	const char **strv;
	const char *path;
	gsize i, n;

	nm_assert (lst_head);

	if (lst_len < 0)
		n = c_list_length (lst_head);
	else {
		n = lst_len;
		nm_assert (n == c_list_length (lst_head));
	}

	i = 0;
	strv = g_new (const char *, n + 1);
	c_list_for_each (iter, lst_head) {
		NMDBusObject *obj = (NMDBusObject *) (((const char *) iter) - member_offset);

		path = nm_dbus_object_get_path (obj);
		if (!path) {
			nm_assert (expect_all_exported);
			continue;
		}

		nm_assert (i < n);
		strv[i++] = path;
	}
	nm_assert (i <= n);
	strv[i] = NULL;

	return strv;
}

/*****************************************************************************/

void
nm_dbus_track_obj_path_init (NMDBusTrackObjPath *track,
                             GObject *target,
                             const GParamSpec *pspec)
{
	nm_assert (track);
	nm_assert (G_IS_OBJECT (target));
	nm_assert (G_IS_PARAM_SPEC (pspec));

	track->_obj = NULL;
	track->_notify_target = target;
	track->_notify_pspec = pspec;
	track->_notify_signal_id = 0;
	track->_visible = FALSE;
}

void
nm_dbus_track_obj_path_deinit (NMDBusTrackObjPath *track)
{
	/* we allow deinit() to be called multiple times (e.g. from
	 * dispose(), which must be re-entrant). */
	nm_assert (track);
	nm_assert (!track->_notify_target || G_IS_OBJECT (track->_notify_target));

	nm_clear_g_signal_handler (track->obj, &track->_notify_signal_id);
	track->_notify_target = NULL;
	track->_notify_pspec = NULL;
	track->_visible = FALSE;
	nm_clear_g_object (&track->_obj);
}

void
nm_dbus_track_obj_path_notify (const NMDBusTrackObjPath *track)
{
	nm_assert (track);
	nm_assert (G_IS_OBJECT (track->_notify_target));
	nm_assert (G_IS_PARAM_SPEC (track->_notify_pspec));

	g_object_notify_by_pspec (track->_notify_target,
	                          (GParamSpec *) track->_notify_pspec);
}

const char *
nm_dbus_track_obj_path_get (const NMDBusTrackObjPath *track)
{
	nm_assert (track);
	nm_assert (G_IS_OBJECT (track->_notify_target));

	return track->obj && track->visible
	       ? nm_dbus_object_get_path_still_exported (track->obj)
	       : NULL;
}

static void
_track_obj_exported_changed (NMDBusObject *obj,
                             NMDBusTrackObjPath *track)
{
	nm_dbus_track_obj_path_notify (track);
}

void
nm_dbus_track_obj_path_set (NMDBusTrackObjPath *track,
                            gpointer obj,
                            gboolean visible)
{
	gs_unref_object NMDBusObject *old_obj = NULL;
	const char *old_path;

	nm_assert (track);
	nm_assert (G_IS_OBJECT (track->_notify_target));

	g_return_if_fail (!obj || NM_IS_DBUS_OBJECT (obj));

	if (   track->obj == obj
	    && track->visible == !!visible)
		return;

	old_path = nm_dbus_track_obj_path_get (track);

	track->_visible = visible;

	if (track->obj != obj) {
		nm_clear_g_signal_handler (track->obj, &track->_notify_signal_id);

		old_obj = track->obj;
		track->_obj = nm_g_object_ref (obj);

		if (obj) {
			track->_notify_signal_id = g_signal_connect (obj,
			                                             NM_DBUS_OBJECT_EXPORTED_CHANGED,
			                                             G_CALLBACK (_track_obj_exported_changed),
			                                             track);
		}
	}

	if (!nm_streq0 (old_path, nm_dbus_track_obj_path_get (track)))
		nm_dbus_track_obj_path_notify (track);
}