aboutsummaryrefslogtreecommitdiff
path: root/libcontextsubscriber/src/contextkitplugin.cpp
blob: da43ddb021203ebbcd8f55efba0560a453614611 (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
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
/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 *
 * Contact: Marius Vollmer <marius.vollmer@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * 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 "contextkitplugin.h"
#include "logging.h"
#include "subscriberinterface.h"
#include "sconnect.h"
#include "propertyhandle.h"
#include <QStringList>
#include <QDBusPendingCall>
#include <QTimer>
#include <QDBusPendingReply>

/// Creates a new instance, the service to connect to has to be passed
/// in \c constructionString in the format <tt>[session|dbus]:servicename</tt>.
ContextSubscriber::IProviderPlugin* contextKitPluginFactory(QString constructionString)
{
    QStringList constr = constructionString.split(":");
    if (constr.size() != 2) {
        contextCritical() << "Bad syntax for contextkit-dbus plugin:" << constructionString;
        return 0;
    }
    if (constr[0] == "session") {
        ContextSubscriber::ContextKitPlugin* ret =
            new ContextSubscriber::ContextKitPlugin(QDBusConnection::sessionBus(), constr[1]);
        if (constructionString == ContextSubscriber::PropertyHandle::commanderInfo.constructionString)
            ret->setDefaultNewProtocol(false);
        return ret;
    } else if (constr[0] == "system")
        return new ContextSubscriber::ContextKitPlugin(QDBusConnection::systemBus(), constr[1]);

    contextCritical() << "Unknown bus type: " << constructionString;
    return 0;
}

namespace ContextSubscriber {

/*!
  \class ContextKitPlugin
  \brief Implementation of the ContextKit D-Bus protocol.
*/

static const char managerIName[] = "org.freedesktop.ContextKit.Manager";
static const char subscriberIName[] = "org.freedesktop.ContextKit.Subscriber";
static const char managerPath[] = "/org/freedesktop/ContextKit/Manager";
static const char propertyIName[] = "org.maemo.contextkit.Property";
static const char corePrefix[] = "/org/maemo/contextkit/";

/// Converts a key name to a protocol level object path.  There is a
/// distinction, because core properties have the form
/// <tt>/org/maemo/contextkit/Screen/TopEdge</tt> on D-Bus level, but
/// on higher levels they are <tt>Screen.TopEdge</tt>.  Non-core
/// properties should simply have a name like
/// /com/nokia/modem/Specific/Feature, so they can be used as object
/// paths without further conversions.
QString ContextKitPlugin::keyToPath(QString key)
{
    if (key.startsWith("/"))
        return key;

    return corePrefix + key.replace('.', '/').replace(QRegExp("[^A-Za-z0-9_/]"), "_");
}

/// Creates subscriber and manager interface, tries to get a
/// subscriber instance from the manager and starts listening for
/// provider appearing and disappearing on D-Bus.
ContextKitPlugin::ContextKitPlugin(const QDBusConnection bus, const QString& busName)
    : providerListener(new DBusNameListener(bus, busName, this)),
      subscriberInterface(0),
      managerInterface(0),
      connection(new QDBusConnection(bus)),
      busName(busName),
      newProtocol(true),
      defaultNewProtocol(true),
      providerAppearedQueued(false),
      providerAppearedSkip(false)
{
    reset();
    // Notice if the provider on the dbus comes and goes
    sconnect(providerListener, SIGNAL(nameAppeared()),
             this, SLOT(onProviderAppeared()));
    sconnect(providerListener, SIGNAL(nameDisappeared()),
             this, SLOT(onProviderDisappeared()));

    // Listen to the provider appearing and disappearing, but don't
    // perform the initial NameHasOwner check.  We try to connect to
    // the provider at startup, whether it's present or not.
    providerListener->startListening(false);
    QMetaObject::invokeMethod(this, "onProviderAppeared", Qt::QueuedConnection);
    providerAppearedQueued = true;
}

void ContextKitPlugin::setDefaultNewProtocol(bool s) {
    defaultNewProtocol = s;
}

void ContextKitPlugin::reset()
{
    delete(subscriberInterface);
    subscriberInterface = 0;
    delete(managerInterface);
    managerInterface = 0;
    newProtocol = defaultNewProtocol;
    // Disconnect the ValueChanged signal for all keys (object paths)
    connection->disconnect(busName, "", propertyIName, "ValueChanged",
                           this, SLOT(onNewValueChanged(QList<QVariant>,quint64,QDBusMessage)));
}

/// Gets a new subscriber interface from manager when the provider
/// appears.
void ContextKitPlugin::onProviderAppeared()
{
    // It is possible that this function is called and we have a Subscribe call
    // in progress.  This happens when things happen in the following order:
    // 1. the subscriber is started
    // 2. the subscriber optimistically sends the Subscribe call
    // 3. the provider is started (quick enough to handle the Subscribe call)
    // 4. providerListener notices that the provider was started
    // In this case, the plugin is in "ready" state already.

    providerAppearedQueued = false;

    if (providerAppearedSkip) {
        // Now we had queued this function to be called, but before it was
        // called, waitUntilReadyAndBlock called it.  Skip the scheduled call
        // (but not the later calls).
        providerAppearedSkip = false;
        return;
    }

    contextDebug() << "Provider appeared:" << busName;

    reset();
    if (defaultNewProtocol) {
        useNewProtocol();
        return;
    }
    managerInterface = new AsyncDBusInterface(busName, managerPath, managerIName, *connection, this);
    if (!managerInterface->callWithCallback("GetSubscriber",
                                            QList<QVariant>(),
                                            this,
                                            SLOT(onDBusGetSubscriberFinished(QDBusObjectPath)),
                                            SLOT(onDBusGetSubscriberFailed(QDBusError)))) {
        onDBusGetSubscriberFailed(managerInterface->lastError());
    }
}

/// Delete our subscriber interface when the provider goes away.
void ContextKitPlugin::onProviderDisappeared()
{
    contextDebug() << "Provider disappeared:" << busName;
    reset();
    Q_EMIT failed("Provider went away " + busName);
}

/// Starts using the fresh subscriber interface when it is returned by
/// the manager in response to the GetSubscriber call.
void ContextKitPlugin::onDBusGetSubscriberFinished(QDBusObjectPath objectPath)
{
    delete subscriberInterface;
    subscriberInterface = new SubscriberInterface(*connection, busName, objectPath.path(), this);
    sconnect(subscriberInterface,
             SIGNAL(valuesChanged(QMap<QString, QVariant>)),
             this,
             SLOT(onDBusValuesChanged(QMap<QString, QVariant>)));
    sconnect(subscriberInterface,
             SIGNAL(subscribeFinished(QList<QString>)),
             this,
             SLOT(onDBusSubscribeFinished(QList<QString>)));
    sconnect(subscriberInterface,
             SIGNAL(subscribeFailed(QList<QString>, QString)),
             this,
             SLOT(onDBusSubscribeFailed(QList<QString>, QString)));

    Q_EMIT ready();
}

void ContextKitPlugin::onDBusGetSubscriberFailed(QDBusError err)
{
    if (err.type() != QDBusError::UnknownObject) {
        contextWarning() <<
            "D-Bus failed for: " + busName + ", error: " +
            err.message();
        Q_EMIT failed("D-Bus error while trying old protocol " + busName);
        return;
    }
    contextWarning() <<
        "Trying new protocol, old failed for: " + busName + ", error: " +
        err.message();
    reset();
    useNewProtocol();
}

void ContextKitPlugin::useNewProtocol()
{
    newProtocol = true;

    if (providerListener->isServicePresent() == DBusNameListener::NotPresent)
        return;

    // Ready to try out new protocol. Ready should not be queued,
    // because otherwise ready and failed might get reordered.
    Q_EMIT ready();
}

/// Signals the Provider that the Subscribe call (old protocol) is finished.
void ContextKitPlugin::onDBusSubscribeFinished(QList<QString> keys)
{
    Q_FOREACH (const QString& key, keys)
        Q_EMIT subscribeFinished(key);
}

/// Signals the Provider that the Subscribe call (old protocol) has failed.
void ContextKitPlugin::onDBusSubscribeFailed(QList<QString> keys, QString error)
{
    Q_FOREACH (const QString& key, keys)
        Q_EMIT subscribeFailed(key, error);
}

/// Forwards the subscribe request to the wire.
void ContextKitPlugin::subscribe(QSet<QString> keys)
{
    if (newProtocol)
        Q_FOREACH (const QString& key, keys) {
            // Queue calling the Subscribe asynchronously. Don't
            // create the async call here: Qt will deadlock if we
            // create an async call while handling the results of the
            // previous async call. (We emit "ready" when handling
            // GetSubscriber. "Ready" is not queued, and the above
            // layer can call subscribe when handling it.)
            pendingKeys.insert(key);
            QMetaObject::invokeMethod(this, "newSubscribe", Qt::QueuedConnection, Q_ARG(QString, key));
        }
    else {
        subscriberInterface->subscribe(keys);
    }
}

void ContextKitPlugin::newSubscribe(const QString& key)
{
    if (pendingKeys.contains(key) == false) {
        // this key was already handled, probably because
        // waitForSubscriptionAndBlock forced the subscription to happen.
        return;
    }
    pendingKeys.remove(key);

    QString objectPath = keyToPath(key);
    // Store the "object path -> key" mapping so that we can transform
    // back when a valueChanged signal comes over D-Bus. (Note the
    // special character transformation.)
    objectPathToKey[objectPath] = key;

    QDBusPendingCall pc = connection->asyncCall(QDBusMessage::createMethodCall(busName,
                                                                               objectPath,
                                                                               propertyIName,
                                                                               "Subscribe"));

    // connect to dbus value changes too, but only for this key
    connection->connect(busName, objectPath, propertyIName, "ValueChanged",
                        this,
                        SLOT(onNewValueChanged(QList<QVariant>,quint64,QDBusMessage)));

    PendingSubscribeWatcher *psw = new PendingSubscribeWatcher(pc, key, this);
    pendingWatchers.insert(key, psw);
    sconnect(psw,
             SIGNAL(subscribeFinished(QString)),
             this,
             SIGNAL(subscribeFinished(QString)));
    sconnect(psw,
             SIGNAL(subscribeFailed(QString,QString)),
             this,
             SIGNAL(subscribeFailed(QString,QString)));
    sconnect(psw,
             SIGNAL(valueChanged(QString,TimedValue)),
             this,
             SIGNAL(valueChanged(QString,TimedValue)));
    sconnect(psw,
             SIGNAL(subscribeFinished(QString)),
             this,
             SLOT(removePendingWatcher(const QString&)));
    sconnect(psw,
             SIGNAL(subscribeFailed(QString,QString)),
             this,
             SLOT(removePendingWatcher(const QString&)));
}


/// Forwards the unsubscribe request to the wire.
void ContextKitPlugin::unsubscribe(QSet<QString> keys)
{
    if (newProtocol)
        Q_FOREACH (QString key, keys) {
            QString objectPath = keyToPath(key);
            objectPathToKey.remove(objectPath);
            connection->asyncCall(QDBusMessage::createMethodCall(busName,
                                                                 objectPath,
                                                                 propertyIName,
                                                                 "Unsubscribe"));
            // disconnect the ValueChanged signal for this key
            connection->disconnect(busName, objectPath,
                                   propertyIName, "ValueChanged",
                                   this,
                                   SLOT(onNewValueChanged(QList<QVariant>,quint64,QDBusMessage)));

        }
    else
        subscriberInterface->unsubscribe(keys);
}

/// Forwards value changes from the wire to the upper layer (Provider).
void ContextKitPlugin::onDBusValuesChanged(QMap<QString, QVariant> values)
{
    Q_FOREACH (const QString& key, values.keys())
        Q_EMIT valueChanged(key, values[key]);
}

// QDBus doesn't unmarshall non-basic types inside QVariants (since it cannot
// know the intention), but leaves them as QDBusArguments.  Here we walk the
// structure and unpack lists and maps.
QVariant demarshallValue(const QVariant &v)
{
    if (v.userType() != qMetaTypeId<QDBusArgument>())
        return v;
    const QDBusArgument &dba = v.value<QDBusArgument>();
    switch (dba.currentType()) {
    case QDBusArgument::ArrayType: {
        QVariantList vl;
        dba.beginArray();
        while (!dba.atEnd()) {
            QVariant v;
            dba >> v;
            vl << demarshallValue(v);
        }
        dba.endArray();
        return QVariant(vl);
        break;
    }
    case QDBusArgument::MapType: {
        dba.beginMap();
        QVariantMap vm;
        while (!dba.atEnd()) {
            QString k;
            QVariant v;
            dba.beginMapEntry();
            dba >> k >> v;
            dba.endMapEntry();
            v = demarshallValue(v);
            vm.insert(k, v);
        }
        dba.endMap();
        return QVariant(vm);
        break;
    }
    default:
        // Shouldn't reach this.
        contextWarning() << "got something unexpected: QDBusArgument of type"
                         << dba.currentType();
        return QVariant();
        break;
    }
}

static TimedValue createTimedValue(const QList<QVariant> value, quint64 time)
{
    if (value.size() == 0)
        return TimedValue(QVariant(), time);
    else
        return TimedValue(demarshallValue(value.at(0)), time);
}

void ContextKitPlugin::onNewValueChanged(QList<QVariant> value,
                                         quint64 timestamp,
                                         QDBusMessage message)
{
    if (objectPathToKey.contains(message.path())) {
        Q_EMIT valueChanged(objectPathToKey[message.path()],
                          createTimedValue(value, timestamp));
    }
    else {
        contextWarning() << "Unrecognized key" << message.path();
    }
}

void ContextKitPlugin::waitUntilReadyAndBlock()
{
    onProviderAppeared();
    if (providerAppearedQueued) {
        // Calling onProviderAppeared was already scheduled but we already
        // called it now.  Arrange so that the scheduled call is skipped when
        // the event is processed.
        providerAppearedSkip = true;
    }
}

void ContextKitPlugin::waitForSubscriptionAndBlock(const QString& key)
{
    // Force the subscriptions (that were scheduled) to happen now
    while (newProtocol && pendingKeys.size() > 0) {
        QString key = *(pendingKeys.constBegin());
        newSubscribe(key);
    }
    if (pendingWatchers.contains(key)) {
        pendingWatchers.value(key)->waitForFinished();
    }
}

void ContextKitPlugin::removePendingWatcher(const QString& key)
{
    pendingWatchers.remove(key);
}

PendingSubscribeWatcher::PendingSubscribeWatcher(const QDBusPendingCall &call,
                                                 const QString &key,
                                                 QObject * parent) :
    QDBusPendingCallWatcher(call, parent), key(key)
{
    sconnect(this, SIGNAL(finished(QDBusPendingCallWatcher *)),
             this, SLOT(onFinished()));
    sconnect(this, SIGNAL(finished(QDBusPendingCallWatcher *)),
             this, SLOT(deleteLater()));
}

void PendingSubscribeWatcher::onFinished()
{
    QDBusPendingReply<QList<QVariant>, quint64> reply = *this;
    if (reply.isError()) {
        Q_EMIT subscribeFailed(key, reply.error().message());
        return;
    }

    Q_EMIT valueChanged(key, createTimedValue(reply.argumentAt<0>(),
                                            reply.argumentAt<1>()));
    Q_EMIT subscribeFinished(key);
}

}