summaryrefslogtreecommitdiff
path: root/libnm/nm-object.c
blob: d8c4fb41d2c15891130191441afb2ddfd7ca411d (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
298
299
300
301
302
303
304
305
306
307
308
309
// SPDX-License-Identifier: LGPL-2.1+
/*
 * Copyright (C) 2007 - 2008 Novell, Inc.
 * Copyright (C) 2007 - 2012 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nm-object.h"

#include <stdlib.h>
#include <stdio.h>

#include "nm-utils.h"
#include "nm-dbus-interface.h"
#include "nm-object-private.h"
#include "nm-dbus-helpers.h"
#include "nm-client.h"
#include "nm-core-internal.h"
#include "c-list/src/c-list.h"

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

NM_GOBJECT_PROPERTIES_DEFINE_BASE (
	PROP_PATH,
);

typedef struct _NMObjectPrivate {
	NMClient *client;
	NMLDBusObject *dbobj;
} NMObjectPrivate;

G_DEFINE_ABSTRACT_TYPE (NMObject, nm_object, G_TYPE_OBJECT);

#define NM_OBJECT_GET_PRIVATE(self) _NM_GET_PRIVATE_PTR(self, NMObject, NM_IS_OBJECT)

static NMObjectClass *_nm_object_class = NULL;

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

static gpointer
_nm_object_get_private (NMObjectClass *klass, NMObject *self, guint16 extra_offset)
{
	char *ptr;

	nm_assert (klass->priv_ptr_offset > 0);

	ptr = (char *) self;
	ptr += klass->priv_ptr_offset;
	if (klass->priv_ptr_indirect)
		ptr = *((gpointer *) ptr);
	return ptr + extra_offset;
}

NMLDBusObject *
_nm_object_get_dbobj (gpointer self)
{
	return NM_OBJECT_GET_PRIVATE (self)->dbobj;
}

const char *
_nm_object_get_path (gpointer self)
{
	return NM_OBJECT_GET_PRIVATE (self)->dbobj->dbus_path->str;
}

NMClient *
_nm_object_get_client (gpointer self)
{
	return NM_OBJECT_GET_PRIVATE (self)->client;
}

/**
 * nm_object_get_path:
 * @object: a #NMObject
 *
 * Gets the DBus path of the #NMObject.
 *
 * Returns: the object's path. This is the internal string used by the
 * object, and must not be modified.
 *
 * Note that the D-Bus path of an NMObject never changes, even
 * if the instance gets removed from the cache. To find out
 * whether the object is still alive/cached, check nm_object_get_client().
 **/
const char *
nm_object_get_path (NMObject *object)
{
	g_return_val_if_fail (NM_IS_OBJECT (object), NULL);

	return _nm_object_get_path (object);
}

/**
 * nm_object_get_client:
 * @object: a #NMObject
 *
 * Returns the #NMClient instance in which object is cached.
 * Also, if the object got removed from the client cached,
 * this returns %NULL. So it can be used to check whether the
 * object is still alive.
 *
 * Returns: (transfer none): the #NMClient cache in which the
 * object can be found, or %NULL if the object is no longer
 * cached.
 *
 * Since: 1.24
 **/
NMClient *
nm_object_get_client (NMObject *object)
{
	g_return_val_if_fail (NM_IS_OBJECT (object), NULL);

	return _nm_object_get_client (object);
}

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

static void
clear_properties (NMObject *self,
                  NMClient *client)
{
	NMObjectClass *klass = NM_OBJECT_GET_CLASS (self);
	const _NMObjectClassFieldInfo *p;

	nm_assert (NM_IS_OBJECT (self));
	nm_assert (!client || NM_IS_CLIENT (client));

	for (p = klass->property_o_info; p; p = p->parent) {
		nml_dbus_property_o_clear_many (_nm_object_get_private (p->klass, self, p->offset),
		                                p->num,
		                                client);
	}

	for (p = klass->property_ao_info; p; p = p->parent) {
		nml_dbus_property_ao_clear_many (_nm_object_get_private (p->klass, self, p->offset),
		                                 p->num,
		                                 client);
	}
}

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

static gboolean
is_ready (NMObject *self)
{
	NMObjectClass *klass = NM_OBJECT_GET_CLASS (self);
	NMClient *client = _nm_object_get_client (self);
	const _NMObjectClassFieldInfo *p;
	guint16 i;

	nm_assert (NM_IS_CLIENT (client));

	for (p = klass->property_o_info; p; p = p->parent) {
		NMLDBusPropertyO *fields = _nm_object_get_private (p->klass, self, p->offset);

		for (i = 0; i < p->num; i++) {
			if (!nml_dbus_property_o_is_ready (&fields[i]))
				return FALSE;
		}
	}

	for (p = klass->property_ao_info; p; p = p->parent) {
		NMLDBusPropertyAO *fields = _nm_object_get_private (p->klass, self, p->offset);

		for (i = 0; i < p->num; i++) {
			if (!nml_dbus_property_ao_is_ready (&fields[i]))
				return FALSE;
		}
	}

	return TRUE;
}

static void
obj_changed_notify (NMObject *self)
{
	NMObjectClass *klass = NM_OBJECT_GET_CLASS (self);
	NMClient *client = _nm_object_get_client (self);
	const _NMObjectClassFieldInfo *p;

	nm_assert (NM_IS_CLIENT (client));

	for (p = klass->property_o_info; p; p = p->parent) {
		nml_dbus_property_o_notify_changed_many (_nm_object_get_private (p->klass, self, p->offset),
		                                         p->num,
		                                         client);
	}

	for (p = klass->property_ao_info; p; p = p->parent) {
		nml_dbus_property_ao_notify_changed_many (_nm_object_get_private (p->klass, self, p->offset),
		                                          p->num,
		                                          client);
	}
}

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

static void
register_client (NMObject *self,
                 NMClient *client,
                 NMLDBusObject *dbobj)
{
	NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);

	nm_assert (!priv->client);
	nm_assert (NML_IS_DBUS_OBJECT (dbobj));
	nm_assert (dbobj->nmobj == G_OBJECT (self));

	priv->client = client;
	priv->dbobj = nml_dbus_object_ref (dbobj);
}

static void
unregister_client (NMObject *self,
                   NMClient *client,
                   NMLDBusObject *dbobj)
{
	NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);

	nm_assert (NM_IS_CLIENT (client));
	nm_assert (priv->client == client);
	priv->client = NULL;

	clear_properties (self, client);
}

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

static void
get_property (GObject *object, guint prop_id,
              GValue *value, GParamSpec *pspec)
{
	NMObject *self = NM_OBJECT (object);

	switch (prop_id) {
	case PROP_PATH:
		g_value_set_string (value, nm_object_get_path (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

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

static void
nm_object_init (NMObject *object)
{
	NMObject *self = NM_OBJECT (object);
	NMObjectPrivate *priv;

	priv = G_TYPE_INSTANCE_GET_PRIVATE (self, NM_TYPE_OBJECT, NMObjectPrivate);

	self->_priv = priv;

	c_list_init (&self->obj_base.queue_notify_lst);
}

static void
dispose (GObject *object)
{
	NMObject *self = NM_OBJECT (object);
	NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);

	self->obj_base.is_disposing = TRUE;

	nm_assert (c_list_is_empty (&self->obj_base.queue_notify_lst));
	nm_assert (!priv->client);
	nm_assert (!priv->dbobj || !priv->dbobj->nmobj);

	clear_properties (self, NULL);

	G_OBJECT_CLASS (nm_object_parent_class)->dispose (object);

	nm_clear_pointer (&priv->dbobj, nml_dbus_object_unref);
}

static void
nm_object_class_init (NMObjectClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	_nm_object_class = klass;

	g_type_class_add_private (klass, sizeof (NMObjectPrivate));

	object_class->get_property = get_property;
	object_class->dispose      = dispose;

	klass->register_client    = register_client;
	klass->unregister_client  = unregister_client;
	klass->is_ready           = is_ready;
	klass->obj_changed_notify = obj_changed_notify;

	/**
	 * NMObject:path:
	 *
	 * The D-Bus object path.
	 **/
	obj_properties[PROP_PATH] =
	    g_param_spec_string (NM_OBJECT_PATH, "", "",
	                         NULL,
	                         G_PARAM_READABLE |
	                         G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties (object_class, _PROPERTY_ENUMS_LAST, obj_properties);
}