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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
/*
* This file is part of TelepathyQt4
*
* Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
* Copyright (C) 2008 Nokia Corporation
*
* 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
*/
#define IN_TELEPATHY_QT4_INTERNALS
#include "connection.h"
#include "connection.moc.hpp"
#include <TelepathyQt4/_gen/cli-connection-body.hpp>
#include <TelepathyQt4/_gen/cli-connection.moc.hpp>
#include <TelepathyQt4/Client/PendingChannel>
#include <TelepathyQt4/Client/PendingVoidMethodCall>
#include <TelepathyQt4/debug-internal.h>
#include <QMap>
#include <QQueue>
#include <QString>
#include <QtGlobal>
namespace Telepathy
{
namespace Client
{
struct Connection::Private
{
// Public object
Connection& parent;
// Instance of generated interface class
ConnectionInterface* baseInterface;
// Optional interface proxies
ConnectionInterfaceAliasingInterface* aliasing;
ConnectionInterfacePresenceInterface* presence;
DBus::PropertiesInterface* properties;
// Introspection
bool initialIntrospection;
Readiness readiness;
QStringList interfaces;
QQueue<void (Private::*)()> introspectQueue;
// Introspected properties
uint status;
uint statusReason;
uint aliasFlags;
StatusSpecMap presenceStatuses;
SimpleStatusSpecMap simplePresenceStatuses;
Private(Connection &parent)
: parent(parent)
{
aliasing = 0;
presence = 0;
properties = 0;
baseInterface = 0;
initialIntrospection = false;
readiness = ReadinessJustCreated;
status = ConnectionStatusDisconnected;
statusReason = ConnectionStatusReasonNoneSpecified;
aliasFlags = 0;
}
void startIntrospection()
{
Q_ASSERT(baseInterface == 0);
baseInterface = new ConnectionInterface(parent.dbusConnection(),
parent.busName(), parent.objectPath(), &parent);
debug() << "Connecting to StatusChanged()";
parent.connect(baseInterface,
SIGNAL(StatusChanged(uint, uint)),
SLOT(onStatusChanged(uint, uint)));
debug() << "Calling GetStatus()";
QDBusPendingCallWatcher* watcher =
new QDBusPendingCallWatcher(baseInterface->GetStatus(), &parent);
parent.connect(watcher,
SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(gotStatus(QDBusPendingCallWatcher*)));
}
void introspectAliasing()
{
// The Aliasing interface is not usable before the connection is established
if (initialIntrospection) {
continueIntrospection();
return;
}
if (!aliasing) {
aliasing = parent.aliasingInterface();
Q_ASSERT(aliasing != 0);
}
debug() << "Calling GetAliasFlags()";
QDBusPendingCallWatcher* watcher =
new QDBusPendingCallWatcher(aliasing->GetAliasFlags(), &parent);
parent.connect(watcher,
SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(gotAliasFlags(QDBusPendingCallWatcher*)));
}
void introspectMain()
{
// Introspecting the main interface is currently just calling
// GetInterfaces(), but it might include other stuff in the future if we
// gain GetAll-able properties on the connection
debug() << "Calling GetInterfaces()";
QDBusPendingCallWatcher* watcher =
new QDBusPendingCallWatcher(baseInterface->GetInterfaces(), &parent);
parent.connect(watcher,
SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(gotInterfaces(QDBusPendingCallWatcher*)));
}
void introspectPresence()
{
// The Presence interface is not usable before the connection is established
if (initialIntrospection) {
continueIntrospection();
return;
}
if (!presence) {
presence = parent.presenceInterface();
Q_ASSERT(presence != 0);
}
debug() << "Calling GetStatuses() (legacy)";
QDBusPendingCallWatcher* watcher =
new QDBusPendingCallWatcher(presence->GetStatuses(), &parent);
parent.connect(watcher,
SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(gotStatuses(QDBusPendingCallWatcher*)));
}
void introspectSimplePresence()
{
if (!properties) {
properties = parent.propertiesInterface();
Q_ASSERT(properties != 0);
}
debug() << "Getting available SimplePresence statuses";
QDBusPendingCall call =
properties->Get(TELEPATHY_INTERFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE,
"Statuses");
QDBusPendingCallWatcher* watcher =
new QDBusPendingCallWatcher(call, &parent);
parent.connect(watcher,
SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(gotSimpleStatuses(QDBusPendingCallWatcher*)));
}
void continueIntrospection()
{
if (introspectQueue.isEmpty()) {
if (initialIntrospection) {
initialIntrospection = false;
if (readiness < ReadinessNotYetConnected)
changeReadiness(ReadinessNotYetConnected);
} else {
if (readiness != ReadinessDead)
changeReadiness(ReadinessFull);
}
} else {
(this->*introspectQueue.dequeue())();
}
}
void changeReadiness(Readiness newReadiness)
{
Q_ASSERT(newReadiness != readiness);
switch (readiness) {
case ReadinessJustCreated:
break;
case ReadinessNotYetConnected:
Q_ASSERT(newReadiness == ReadinessConnecting
|| newReadiness == ReadinessDead);
break;
case ReadinessConnecting:
Q_ASSERT(newReadiness == ReadinessFull
|| newReadiness == ReadinessDead);
break;
case ReadinessFull:
Q_ASSERT(newReadiness == ReadinessDead);
break;
case ReadinessDead:
default:
Q_ASSERT(false);
}
debug() << "Readiness changed from" << readiness << "to" << newReadiness;
readiness = newReadiness;
emit parent.readinessChanged(newReadiness);
}
};
Connection::Connection(const QString& serviceName,
const QString& objectPath,
QObject* parent)
: StatefulDBusProxy(QDBusConnection::sessionBus(), serviceName,
objectPath, parent),
mPriv(new Private(*this))
{
mPriv->startIntrospection();
}
Connection::Connection(const QDBusConnection& connection,
const QString& serviceName,
const QString& objectPath,
QObject* parent)
: StatefulDBusProxy(connection, serviceName, objectPath, parent),
mPriv(new Private(*this))
{
mPriv->startIntrospection();
}
Connection::~Connection()
{
delete mPriv;
}
Connection::Readiness Connection::readiness() const
{
return mPriv->readiness;
}
uint Connection::status() const
{
if (mPriv->readiness == ReadinessJustCreated)
warning() << "Connection::status() used with readiness ReadinessJustCreated";
return mPriv->status;
}
uint Connection::statusReason() const
{
if (mPriv->readiness == ReadinessJustCreated)
warning() << "Connection::statusReason() used with readiness ReadinessJustCreated";
return mPriv->statusReason;
}
QStringList Connection::interfaces() const
{
// Different check than the others, because the optional interface getters
// may be used internally with the knowledge about getting the interfaces
// list, so we don't want this to cause warnings.
if (mPriv->readiness != ReadinessNotYetConnected && mPriv->readiness != ReadinessFull && mPriv->interfaces.empty())
warning() << "Connection::interfaces() used possibly before the list of interfaces has been received";
else if (mPriv->readiness == ReadinessDead)
warning() << "Connection::interfaces() used with readiness ReadinessDead";
return mPriv->interfaces;
}
uint Connection::aliasFlags() const
{
if (mPriv->readiness != ReadinessFull)
warning() << "Connection::aliasFlags() used with readiness" << mPriv->readiness << "!= ReadinessFull";
else if (!interfaces().contains(TELEPATHY_INTERFACE_CONNECTION_INTERFACE_ALIASING))
warning() << "Connection::aliasFlags() used without the remote object supporting the Aliasing interface";
return mPriv->aliasFlags;
}
StatusSpecMap Connection::presenceStatuses() const
{
if (mPriv->readiness != ReadinessFull)
warning() << "Connection::presenceStatuses() used with readiness" << mPriv->readiness << "!= ReadinessFull";
else if (!interfaces().contains(TELEPATHY_INTERFACE_CONNECTION_INTERFACE_PRESENCE))
warning() << "Connection::presenceStatuses() used without the remote object supporting the Presence interface";
return mPriv->presenceStatuses;
}
SimpleStatusSpecMap Connection::simplePresenceStatuses() const
{
if (mPriv->readiness != ReadinessNotYetConnected && mPriv->readiness != ReadinessFull)
warning() << "Connection::simplePresenceStatuses() used with readiness" << mPriv->readiness << "not in (ReadinessNotYetConnected, ReadinessFull)";
else if (!interfaces().contains(TELEPATHY_INTERFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE))
warning() << "Connection::simplePresenceStatuses() used without the remote object supporting the SimplePresence interface";
return mPriv->simplePresenceStatuses;
}
void Connection::onStatusChanged(uint status, uint reason)
{
if (status == ConnectionStatusConnected &&
mPriv->status != ConnectionStatusConnecting) {
// CMs aren't meant to go straight from Disconnected to
// Connected; recover by faking Connecting
warning() << "Non-compliant CM - went straight to CONNECTED!";
onStatusChanged(ConnectionStatusConnecting, reason);
}
debug() << "Status changed from" << mPriv->status << "to" << status << "for reason" << reason;
mPriv->status = status;
mPriv->statusReason = reason;
switch (status) {
case ConnectionStatusConnected:
debug() << "Performing introspection for the Connected status";
mPriv->introspectQueue.enqueue(&Private::introspectMain);
mPriv->continueIntrospection();
break;
case ConnectionStatusConnecting:
if (mPriv->readiness < ReadinessConnecting)
mPriv->changeReadiness(ReadinessConnecting);
else
warning() << "Got unexpected status change to Connecting";
break;
case ConnectionStatusDisconnected:
if (mPriv->readiness != ReadinessDead)
mPriv->changeReadiness(ReadinessDead);
else
warning() << "Got unexpected status change to Disconnected";
break;
default:
warning() << "Unknown connection status" << status;
break;
}
}
void Connection::gotStatus(QDBusPendingCallWatcher* watcher)
{
QDBusPendingReply<uint> reply = *watcher;
if (reply.isError()) {
warning().nospace() << "GetStatus() failed with " << reply.error().name() << ": " << reply.error().message();
mPriv->changeReadiness(ReadinessDead);
return;
}
uint status = reply.value();
debug() << "Got connection status" << status;
mPriv->status = status;
// Don't do any introspection yet if the connection is in the Connecting
// state; the StatusChanged handler will take care of doing that, if the
// connection ever gets to the Connected state.
if (status == ConnectionStatusConnecting) {
debug() << "Not introspecting yet because the connection is currently Connecting";
mPriv->changeReadiness(ReadinessConnecting);
return;
}
if (status == ConnectionStatusDisconnected) {
debug() << "Performing introspection for the Disconnected status";
mPriv->initialIntrospection = true;
} else {
if (status != ConnectionStatusConnected) {
warning() << "Not performing introspection for unknown status" << status;
return;
} else {
debug() << "Performing introspection for the Connected status";
}
}
mPriv->introspectQueue.enqueue(&Private::introspectMain);
mPriv->continueIntrospection();
}
void Connection::gotInterfaces(QDBusPendingCallWatcher* watcher)
{
QDBusPendingReply<QStringList> reply = *watcher;
if (!reply.isError()) {
mPriv->interfaces = reply.value();
debug() << "Got reply to GetInterfaces():" << mPriv->interfaces;
} else {
warning().nospace() << "GetInterfaces() failed with " << reply.error().name() << ": " << reply.error().message() << " - assuming no new interfaces";
}
for (QStringList::const_iterator i = mPriv->interfaces.constBegin();
i != mPriv->interfaces.constEnd();
++i) {
void (Private::*introspectFunc)() = 0;
if (*i == TELEPATHY_INTERFACE_CONNECTION_INTERFACE_ALIASING)
introspectFunc = &Private::introspectAliasing;
else if (*i == TELEPATHY_INTERFACE_CONNECTION_INTERFACE_PRESENCE)
introspectFunc = &Private::introspectPresence;
else if (*i == TELEPATHY_INTERFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE)
introspectFunc = &Private::introspectSimplePresence;
if (introspectFunc)
mPriv->introspectQueue.enqueue(introspectFunc);
}
mPriv->continueIntrospection();
}
void Connection::gotAliasFlags(QDBusPendingCallWatcher* watcher)
{
QDBusPendingReply<uint> reply = *watcher;
if (!reply.isError()) {
mPriv->aliasFlags = static_cast<ConnectionAliasFlag>(reply.value());
debug().nospace() << "Got alias flags 0x" << hex << mPriv->aliasFlags;
} else {
warning().nospace() << "GetAliasFlags() failed with " << reply.error().name() << ": " << reply.error().message();
}
mPriv->continueIntrospection();
}
void Connection::gotStatuses(QDBusPendingCallWatcher* watcher)
{
QDBusPendingReply<StatusSpecMap> reply = *watcher;
if (!reply.isError()) {
mPriv->presenceStatuses = reply.value();
debug() << "Got" << mPriv->presenceStatuses.size() << "legacy presence statuses";
} else {
warning().nospace() << "GetStatuses() failed with " << reply.error().name() << ": " << reply.error().message();
}
mPriv->continueIntrospection();
}
void Connection::gotSimpleStatuses(QDBusPendingCallWatcher* watcher)
{
QDBusPendingReply<QDBusVariant> reply = *watcher;
if (!reply.isError()) {
mPriv->simplePresenceStatuses = qdbus_cast<SimpleStatusSpecMap>(reply.value().variant());
debug() << "Got" << mPriv->simplePresenceStatuses.size() << "simple presence statuses";
} else {
warning().nospace() << "Getting simple presence statuses failed with " << reply.error().name() << ": " << reply.error().message();
}
mPriv->continueIntrospection();
}
ConnectionInterface* Connection::baseInterface() const
{
Q_ASSERT(mPriv->baseInterface != 0);
return mPriv->baseInterface;
}
PendingChannel* Connection::requestChannel(const QString& channelType, uint handleType, uint handle)
{
debug() << "Requesting a Channel with type" << channelType << "and handle" << handle << "of type" << handleType;
PendingChannel* channel =
new PendingChannel(this, channelType, handleType, handle);
QDBusPendingCallWatcher* watcher =
new QDBusPendingCallWatcher(mPriv->baseInterface->RequestChannel(channelType, handleType, handle, true), channel);
channel->connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(onCallFinished(QDBusPendingCallWatcher*)));
return channel;
}
#if 0
// FIXME: this is a 1:1 mapping of the method from TpPrototype, but
// most likely what we really want as a high-level API is something
// more analogous to tp_connection_call_when_ready() in telepathy-glib
PendingOperation* Connection::requestConnect()
{
return new PendingVoidMethodCall(this, baseInterface()->Connect());
}
#endif
PendingOperation* Connection::requestDisconnect()
{
return new PendingVoidMethodCall(this, baseInterface()->Disconnect());
}
}
}
|