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
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2007 - 2008 Novell, Inc.
* Copyright (C) 2008 - 2012 Red Hat, Inc.
*/
#include <string.h>
#include <stdio.h>
#include <dbus/dbus-glib.h>
#include "nm-logging.h"
#include "nm-properties-changed-signal.h"
#include "nm-dbus-glib-types.h"
typedef struct {
GHashTable *exported_props;
guint signal_id;
} NMPropertiesChangedClassInfo;
typedef struct {
GHashTable *hash;
guint signal_id;
guint idle_id;
} NMPropertiesChangedInfo;
static GQuark
nm_properties_changed_signal_quark (void)
{
static GQuark q;
if (G_UNLIKELY (q == 0))
q = g_quark_from_static_string ("nm-properties-changed-signal");
return q;
}
static void
destroy_value (gpointer data)
{
GValue *val = (GValue *) data;
g_value_unset (val);
g_slice_free (GValue, val);
}
static void
properties_changed_info_destroy (gpointer data)
{
NMPropertiesChangedInfo *info = data;
if (info->idle_id)
g_source_remove (info->idle_id);
g_hash_table_destroy (info->hash);
g_slice_free (NMPropertiesChangedInfo, info);
}
static void
add_to_string (gpointer key, gpointer value, gpointer user_data)
{
const char *name = (const char *) key;
GString *buf = user_data;
GValue str_val = G_VALUE_INIT;
g_value_init (&str_val, G_TYPE_STRING);
if (!g_value_transform ((GValue *) value, &str_val)) {
if (G_VALUE_HOLDS_OBJECT (value)) {
GObject *obj = g_value_get_object (value);
if (obj) {
g_string_append_printf (buf, "{%s: %p (%s)}, ", name, obj,
G_OBJECT_TYPE_NAME (obj));
} else
g_string_append_printf (buf, "{%s: %p}, ", name, obj);
} else
g_string_append_printf (buf, "{%s: <transform error>}, ", name);
} else
g_string_append_printf (buf, "{%s: %s}, ", name, g_value_get_string (&str_val));
g_value_unset (&str_val);
}
static gboolean
properties_changed (gpointer data)
{
GObject *object = G_OBJECT (data);
NMPropertiesChangedInfo *info = g_object_get_qdata (object, nm_properties_changed_signal_quark ());
g_assert (info);
if (nm_logging_enabled (LOGL_DEBUG, LOGD_DBUS_PROPS)) {
GString *buf = g_string_new (NULL);
g_hash_table_foreach (info->hash, add_to_string, buf);
nm_log_dbg (LOGD_DBUS_PROPS, "%s -> %s", G_OBJECT_TYPE_NAME (object), buf->str);
g_string_free (buf, TRUE);
}
g_signal_emit (object, info->signal_id, 0, info->hash);
g_hash_table_remove_all (info->hash);
return FALSE;
}
static void
idle_id_reset (gpointer data)
{
GObject *object = G_OBJECT (data);
NMPropertiesChangedInfo *info = g_object_get_qdata (object, nm_properties_changed_signal_quark ());
/* info is unset when the object is being destroyed */
if (info)
info->idle_id = 0;
}
static void
notify (GObject *object, GParamSpec *pspec)
{
NMPropertiesChangedClassInfo *classinfo;
NMPropertiesChangedInfo *info;
const char *dbus_property_name = NULL;
GValue *value;
GType type;
for (type = G_OBJECT_TYPE (object); type; type = g_type_parent (type)) {
classinfo = g_type_get_qdata (type, nm_properties_changed_signal_quark ());
if (!classinfo)
continue;
dbus_property_name = g_hash_table_lookup (classinfo->exported_props, pspec->name);
if (dbus_property_name)
break;
}
if (!dbus_property_name) {
nm_log_dbg (LOGD_DBUS_PROPS, "ignoring notification for prop %s on type %s",
pspec->name, G_OBJECT_TYPE_NAME (object));
return;
}
info = g_object_get_qdata (object, nm_properties_changed_signal_quark ());
if (!info) {
info = g_slice_new0 (NMPropertiesChangedInfo);
info->hash = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, destroy_value);
info->signal_id = classinfo->signal_id;
g_object_set_qdata_full (object, nm_properties_changed_signal_quark (),
info, properties_changed_info_destroy);
}
value = g_slice_new0 (GValue);
g_value_init (value, pspec->value_type);
g_object_get_property (object, pspec->name, value);
g_hash_table_insert (info->hash, (char *) dbus_property_name, value);
if (!info->idle_id)
info->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, properties_changed, object, idle_id_reset);
}
static NMPropertiesChangedClassInfo *
nm_properties_changed_signal_setup_type (GType type)
{
NMPropertiesChangedClassInfo *classinfo;
NMPropertiesChangedClassInfo *parent_classinfo = NULL;
GObjectClass *object_class;
GType parent;
classinfo = g_slice_new (NMPropertiesChangedClassInfo);
g_type_set_qdata (type, nm_properties_changed_signal_quark (), classinfo);
object_class = g_type_class_ref (type);
object_class->notify = notify;
g_type_class_unref (object_class);
classinfo->exported_props = g_hash_table_new (g_str_hash, g_str_equal);
/* See if we've already added the signal to a parent class. (We can't just use
* g_signal_lookup() here because it prints a warning if the signal doesn't exist!)
*/
parent = g_type_parent (type);
while (parent) {
parent_classinfo = g_type_get_qdata (parent, nm_properties_changed_signal_quark ());
if (parent_classinfo)
break;
parent = g_type_parent (parent);
}
if (parent_classinfo)
classinfo->signal_id = parent_classinfo->signal_id;
else {
classinfo->signal_id = g_signal_new ("properties-changed",
type,
G_SIGNAL_RUN_FIRST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__BOXED,
G_TYPE_NONE, 1, DBUS_TYPE_G_MAP_OF_VARIANT);
}
return classinfo;
}
void
nm_properties_changed_signal_add_property (GType type,
const char *dbus_property_name,
const char *gobject_property_name)
{
NMPropertiesChangedClassInfo *classinfo;
char *hyphen_name, *p;
classinfo = g_type_get_qdata (type, nm_properties_changed_signal_quark ());
if (!classinfo)
classinfo = nm_properties_changed_signal_setup_type (type);
g_hash_table_insert (classinfo->exported_props,
(char *) gobject_property_name,
(char *) dbus_property_name);
hyphen_name = g_strdup (gobject_property_name);
for (p = hyphen_name; *p; p++) {
if (*p == '_')
*p = '-';
}
g_hash_table_insert (classinfo->exported_props,
hyphen_name,
(char *) dbus_property_name);
}
|