summaryrefslogtreecommitdiff
path: root/TelepathyQt/dbus-proxy.cpp
blob: 3f333046aa319da06f43b6a5bb5e4b090ea59835 (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
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
/**
 * This file is part of TelepathyQt
 *
 * @copyright Copyright (C) 2008-2010 Collabora Ltd. <http://www.collabora.co.uk/>
 * @copyright Copyright (C) 2008-2010 Nokia Corporation
 * @license LGPL 2.1
 *
 * 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 "config.h"

#include <TelepathyQt/DBusProxy>

#include "TelepathyQt/_gen/dbus-proxy.moc.hpp"

#include "TelepathyQt/debug-internal.h"

#include <TelepathyQt/Constants>

#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusError>
#include <QDBusServiceWatcher>
#include <QTimer>

namespace Tp
{

// ==== DBusProxy ======================================================

// Features in TpProxy but not here:
// * tracking which interfaces we have (in tpqt, subclasses do that)
// * being Introspectable, a Peer and a Properties implementation
// * disconnecting from signals when invalidated (probably has to be in the
//   generated code)
// * making methods always raise an error when called after invalidated
//   (has to be in the generated code)

struct TP_QT_NO_EXPORT DBusProxy::Private
{
    Private(const QDBusConnection &dbusConnection, const QString &busName,
            const QString &objectPath);

    QDBusConnection dbusConnection;
    QString busName;
    QString objectPath;
    QString invalidationReason;
    QString invalidationMessage;
};

DBusProxy::Private::Private(const QDBusConnection &dbusConnection,
            const QString &busName, const QString &objectPath)
    : dbusConnection(dbusConnection),
      busName(busName),
      objectPath(objectPath)
{
    debug() << "Creating new DBusProxy";
}

/**
 * \class DBusProxy
 * \ingroup clientproxies
 * \headerfile TelepathyQt/dbus-proxy.h <TelepathyQt/DBusProxy>
 *
 * \brief The DBusProxy class is a base class representing a remote object available over D-Bus.
 *
 * All Telepathy-Qt client convenience classes that wrap Telepathy interfaces
 * inherit from this class in order to provide basic D-Bus interface
 * information.
 */

/**
 * Construct a new DBusProxy object.
 *
 * \param dbusConnection QDBusConnection to use.
 * \param busName D-Bus bus name of the service that provides the remote object.
 * \param objectPath The object path.
 * \param featureCore The object core feature.
 */
DBusProxy::DBusProxy(const QDBusConnection &dbusConnection,
        const QString &busName, const QString &objectPath, const Feature &featureCore)
    : Object(),
      ReadyObject(this, featureCore),
      mPriv(new Private(dbusConnection, busName, objectPath))
{
    if (!dbusConnection.isConnected()) {
        invalidate(TP_QT_ERROR_DISCONNECTED,
                QLatin1String("DBus connection disconnected"));
    }
}

/**
 * Class destructor.
 */
DBusProxy::~DBusProxy()
{
    delete mPriv;
}

/**
 * Return the D-Bus connection through which the remote object is
 * accessed.
 *
 * \return A QDBusConnection object.
 */
QDBusConnection DBusProxy::dbusConnection() const
{
    return mPriv->dbusConnection;
}

/**
 * Return the D-Bus object path of the remote object within the service.
 *
 * \return The D-Bus object path.
 */
QString DBusProxy::objectPath() const
{
    return mPriv->objectPath;
}

/**
 * Return the D-Bus bus name (either a unique name or a well-known
 * name) of the service that provides the remote object.
 *
 * \return The D-Bus bus name.
 */
QString DBusProxy::busName() const
{
    return mPriv->busName;
}

/**
 * Sets the D-Bus bus name. This is used by subclasses after converting
 * well-known names to unique names.
 *
 * \param busName The D-Bus bus name to set.
 */
void DBusProxy::setBusName(const QString &busName)
{
    mPriv->busName = busName;
}

/**
 * Return whether this proxy is still valid (has not emitted invalidated()).
 *
 * \return \c true if still valid, \c false otherwise.
 */
bool DBusProxy::isValid() const
{
    return mPriv->invalidationReason.isEmpty();
}

/**
 * Return the error name indicating the reason this proxy became invalid.
 *
 * \return A D-Bus error name, or QString() if this object is still valid.
 */
QString DBusProxy::invalidationReason() const
{
    return mPriv->invalidationReason;
}

/**
 * Return a debugging message indicating the reason this proxy became invalid.
 *
 * \return A debugging message, or QString() if this object is still valid.
 */
QString DBusProxy::invalidationMessage() const
{
    return mPriv->invalidationMessage;
}

/**
 * Called by subclasses when the DBusProxy should become invalid.
 *
 * This method takes care of setting the invalidationReason,
 * invalidationMessage, and emitting the invalidated signal.
 *
 * \param reason A D-Bus error name (a string in a subset of ASCII,
 *               prefixed with a reversed domain name)
 * \param message A debugging message associated with the error
 */
void DBusProxy::invalidate(const QString &reason, const QString &message)
{
    if (!isValid()) {
        debug().nospace() << "Already invalidated by "
            << mPriv->invalidationReason
            << ", not replacing with " << reason
            << " \"" << message << "\"";
        return;
    }

    Q_ASSERT(!reason.isEmpty());

    debug().nospace() << "proxy invalidated: " << reason
        << ": " << message;

    mPriv->invalidationReason = reason;
    mPriv->invalidationMessage = message;

    Q_ASSERT(!isValid());

    // Defer emitting the invalidated signal until we next
    // return to the mainloop.
    QTimer::singleShot(0, this, SLOT(emitInvalidated()));
}

void DBusProxy::invalidate(const QDBusError &error)
{
    invalidate(error.name(), error.message());
}

void DBusProxy::emitInvalidated()
{
    Q_ASSERT(!isValid());

    emit invalidated(this, mPriv->invalidationReason, mPriv->invalidationMessage);
}

/**
 * \fn void DBusProxy::invalidated(Tp::DBusProxy *proxy,
 *          const QString &errorName, const QString &errorMessage)
 *
 * Emitted when this object is no longer usable.
 *
 * After this signal is emitted, any D-Bus method calls on the object
 * will fail, but it may be possible to retrieve information that has
 * already been retrieved and cached.
 *
 * \param proxy This proxy.
 * \param errorName The name of a D-Bus error describing the reason for the invalidation.
 * \param errorMessage A debugging message associated with the error.
 */

// ==== StatefulDBusProxy ==============================================

struct TP_QT_NO_EXPORT StatefulDBusProxy::Private
{
    Private(const QString &originalName)
        : originalName(originalName) {}

    QString originalName;
};

/**
 * \class StatefulDBusProxy
 * \ingroup clientproxies
 * \headerfile TelepathyQt/dbus-proxy.h <TelepathyQt/StatefulDBusProxy>
 *
 * \brief The StatefulDBusProxy class is a base class representing a remote object whose API is
 * stateful.
 *
 * These objects do not remain useful if the service providing them exits or
 * crashes, so they emit invalidated() if this happens.
 *
 * Examples include the Connection and Channel classes.
 */

/**
 * Construct a new StatefulDBusProxy object.
 *
 * \param dbusConnection QDBusConnection to use.
 * \param busName D-Bus bus name of the service that provides the remote object.
 * \param objectPath The object path.
 * \param featureCore The object core feature.
 */
StatefulDBusProxy::StatefulDBusProxy(const QDBusConnection &dbusConnection,
        const QString &busName, const QString &objectPath, const Feature &featureCore)
    : DBusProxy(dbusConnection, busName, objectPath, featureCore),
      mPriv(new Private(busName))
{
    QDBusServiceWatcher *serviceWatcher = new QDBusServiceWatcher(busName,
            dbusConnection, QDBusServiceWatcher::WatchForUnregistration, this);
    connect(serviceWatcher,
            SIGNAL(serviceOwnerChanged(QString,QString,QString)),
            SLOT(onServiceOwnerChanged(QString,QString,QString)));

    QString error, message;
    QString uniqueName = uniqueNameFrom(dbusConnection, busName, error, message);

    if (uniqueName.isEmpty()) {
        invalidate(error, message);
        return;
    }

    setBusName(uniqueName);
}

/**
 * Class destructor.
 */
StatefulDBusProxy::~StatefulDBusProxy()
{
    delete mPriv;
}

QString StatefulDBusProxy::uniqueNameFrom(const QDBusConnection &bus, const QString &name)
{
    QString error, message;
    QString uniqueName = uniqueNameFrom(bus, name, error, message);
    if (uniqueName.isEmpty()) {
        warning() << "StatefulDBusProxy::uniqueNameFrom(): Failed to get unique name of" << name;
        warning() << "  error:" << error << "message:" << message;
    }

    return uniqueName;
}

QString StatefulDBusProxy::uniqueNameFrom(const QDBusConnection &bus, const QString &name,
        QString &error, QString &message)
{
    if (name.startsWith(QLatin1String(":"))) {
        return name;
    }

    // For a stateful interface, it makes no sense to follow name-owner
    // changes, so we want to bind to the unique name.
    QDBusReply<QString> reply = bus.interface()->serviceOwner(name);
    if (reply.isValid()) {
        return reply.value();
    } else {
        error = reply.error().name();
        message = reply.error().message();
        return QString();
    }
}

void StatefulDBusProxy::onServiceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner)
{
    // We only want to invalidate this object if it is not already invalidated,
    // and its (not any other object's) name owner changed signal is emitted.
    if (isValid() && name == mPriv->originalName && newOwner.isEmpty()) {
        invalidate(TP_QT_DBUS_ERROR_NAME_HAS_NO_OWNER,
                QLatin1String("Name owner lost (service crashed?)"));
    }
}

// ==== StatelessDBusProxy =============================================

/**
 * \class StatelessDBusProxy
 * \ingroup clientproxies
 * \headerfile TelepathyQt/dbus-proxy.h <TelepathyQt/DBusProxy>
 *
 * \brief The StatelessDBusProxy class is a base class representing a remote object whose API is
 * basically stateless.
 *
 * These objects can remain valid even if the service providing them exits
 * and is restarted.
 *
 * Examples include the AccountManager, Account and ConnectionManager.
 */

/**
 * Construct a new StatelessDBusProxy object.
 *
 * \param dbusConnection QDBusConnection to use.
 * \param busName D-Bus bus name of the service that provides the remote object.
 * \param objectPath The object path.
 * \param featureCore The object core feature.
 */
StatelessDBusProxy::StatelessDBusProxy(const QDBusConnection &dbusConnection,
        const QString &busName, const QString &objectPath, const Feature &featureCore)
    : DBusProxy(dbusConnection, busName, objectPath, featureCore),
      mPriv(nullptr)
{
    if (busName.startsWith(QLatin1String(":"))) {
        warning() <<
            "Using StatelessDBusProxy for a unique name does not make sense";
    }
}

/**
 * Class destructor.
 */
StatelessDBusProxy::~StatelessDBusProxy()
{
}

} // Tp