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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*
* empathy-connection-managers.c - Source for EmpathyConnectionManagers
* Copyright (C) 2009 Collabora Ltd.
* @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <telepathy-glib/connection-manager.h>
#include <telepathy-glib/util.h>
#include "empathy-connection-managers.h"
#include "empathy-utils.h"
#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include <libempathy/empathy-debug.h>
static GObject *managers = NULL;
G_DEFINE_TYPE(EmpathyConnectionManagers, empathy_connection_managers,
G_TYPE_OBJECT)
/* signal enum */
enum
{
UPDATED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = {0};
/* properties */
enum {
PROP_READY = 1
};
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectionManagers)
/* private structure */
typedef struct _EmpathyConnectionManagersPriv
EmpathyConnectionManagersPriv;
struct _EmpathyConnectionManagersPriv
{
gboolean dispose_has_run;
gboolean ready;
GList *cms;
TpDBusDaemon *dbus;
};
static void
empathy_connection_managers_init (EmpathyConnectionManagers *obj)
{
EmpathyConnectionManagersPriv *priv =
G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
EMPATHY_TYPE_CONNECTION_MANAGERS, EmpathyConnectionManagersPriv);
obj->priv = priv;
priv->dbus = tp_dbus_daemon_dup (NULL);
g_assert (priv->dbus != NULL);
empathy_connection_managers_update (obj);
/* allocate any data required by the object here */
}
static void empathy_connection_managers_dispose (GObject *object);
static GObject *
empathy_connection_managers_constructor (GType type,
guint n_construct_params,
GObjectConstructParam *construct_params)
{
if (managers != NULL)
return g_object_ref (managers);
managers =
G_OBJECT_CLASS (empathy_connection_managers_parent_class)->constructor
(type, n_construct_params, construct_params);
g_object_add_weak_pointer (managers, (gpointer) &managers);
return managers;
}
static void
empathy_connection_managers_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
switch (prop_id)
{
case PROP_READY:
g_value_set_boolean (value, priv->ready);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
empathy_connection_managers_class_init (
EmpathyConnectionManagersClass *empathy_connection_managers_class)
{
GObjectClass *object_class =
G_OBJECT_CLASS (empathy_connection_managers_class);
g_type_class_add_private (empathy_connection_managers_class, sizeof
(EmpathyConnectionManagersPriv));
object_class->constructor = empathy_connection_managers_constructor;
object_class->dispose = empathy_connection_managers_dispose;
object_class->get_property = empathy_connection_managers_get_property;
g_object_class_install_property (object_class, PROP_READY,
g_param_spec_boolean ("ready",
"Ready",
"Whether the connection manager information is ready to be used",
FALSE,
G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
signals[UPDATED] = g_signal_new ("updated",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST,
0, NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
empathy_connection_managers_free_cm_list (EmpathyConnectionManagers *self)
{
EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
GList *l;
for (l = priv->cms ; l != NULL ; l = g_list_next (l))
{
g_object_unref (l->data);
}
g_list_free (priv->cms);
priv->cms = NULL;
}
static void
empathy_connection_managers_dispose (GObject *object)
{
EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
if (priv->dispose_has_run)
return;
priv->dispose_has_run = TRUE;
if (priv->dbus != NULL)
g_object_unref (priv->dbus);
priv->dbus = NULL;
empathy_connection_managers_free_cm_list (self);
/* release any references held by the object here */
if (G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose)
G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose (object);
}
EmpathyConnectionManagers *
empathy_connection_managers_dup_singleton (void)
{
return EMPATHY_CONNECTION_MANAGERS (
g_object_new (EMPATHY_TYPE_CONNECTION_MANAGERS, NULL));
}
gboolean
empathy_connection_managers_is_ready (EmpathyConnectionManagers *self)
{
EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
return priv->ready;
}
static void
empathy_connection_managers_listed_cb (TpConnectionManager * const *cms,
gsize n_cms,
const GError *error,
gpointer user_data,
GObject *weak_object)
{
EmpathyConnectionManagers *self =
EMPATHY_CONNECTION_MANAGERS (weak_object);
EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
TpConnectionManager * const *iter;
empathy_connection_managers_free_cm_list (self);
if (error != NULL)
{
DEBUG ("Failed to get connection managers: %s", error->message);
goto out;
}
for (iter = cms ; iter != NULL && *iter != NULL; iter++)
{
/* only list cms that didn't hit errors */
if (tp_connection_manager_is_ready (*iter))
priv->cms = g_list_prepend (priv->cms, g_object_ref (*iter));
}
out:
g_object_ref (weak_object);
if (!priv->ready)
{
priv->ready = TRUE;
g_object_notify (weak_object, "ready");
}
g_signal_emit (weak_object, signals[UPDATED], 0);
g_object_unref (weak_object);
}
void
empathy_connection_managers_update (EmpathyConnectionManagers *self)
{
EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
tp_list_connection_managers (priv->dbus,
empathy_connection_managers_listed_cb,
NULL, NULL, G_OBJECT (self));
}
GList *
empathy_connection_managers_get_cms (EmpathyConnectionManagers *self)
{
EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
return priv->cms;
}
TpConnectionManager *
empathy_connection_managers_get_cm (EmpathyConnectionManagers *self,
const gchar *cm)
{
EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
GList *l;
for (l = priv->cms ; l != NULL; l = g_list_next (l))
{
TpConnectionManager *c = TP_CONNECTION_MANAGER (l->data);
if (!tp_strdiff (c->name, cm))
return c;
}
return NULL;
}
guint
empathy_connection_managers_get_cms_num (EmpathyConnectionManagers *self)
{
EmpathyConnectionManagersPriv *priv;
g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (self), 0);
priv = GET_PRIV (self);
return g_list_length (priv->cms);
}
typedef struct
{
EmpathyConnectionManagers *self;
EmpathyConnectionManagersWhenReadyCb callback;
gpointer user_data;
guint ready_id;
} CallWhenReadyContext;
static void
call_when_ready_ctx_free (CallWhenReadyContext *ctx)
{
g_signal_handler_disconnect (ctx->self, ctx->ready_id);
g_object_unref (ctx->self);
g_slice_free (CallWhenReadyContext, ctx);
}
static void
cwr_ready (EmpathyConnectionManagers *self,
GParamSpec *spec,
CallWhenReadyContext *ctx)
{
g_assert (ctx->callback != NULL);
ctx->callback (self, NULL, ctx->user_data);
call_when_ready_ctx_free (ctx);
}
static CallWhenReadyContext *
call_when_ready_ctx_new (EmpathyConnectionManagers *self,
EmpathyConnectionManagersWhenReadyCb callback,
gpointer user_data)
{
CallWhenReadyContext *ctx = g_slice_new (CallWhenReadyContext);
ctx->self = g_object_ref (self);
ctx->callback = callback;
ctx->user_data = user_data;
ctx->ready_id = g_signal_connect (self, "notify::ready",
G_CALLBACK (cwr_ready), ctx);
return ctx;
}
void
empathy_connection_managers_call_when_ready (
EmpathyConnectionManagers *self,
EmpathyConnectionManagersWhenReadyCb callback,
gpointer user_data)
{
EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
if (priv->ready)
{
callback (self, NULL, user_data);
}
else
{
call_when_ready_ctx_new (self, callback, user_data);
}
}
|