aboutsummaryrefslogtreecommitdiff
path: root/libcontextsubscriber/src/contextkitplugin.h
blob: 077a027c7ab3b850cc1f8ff17aa10f26eaa737e3 (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
/*
 * 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
 *
 */

#ifndef CONTEXTKITPLUGIN_H
#define CONTEXTKITPLUGIN_H

#include "dbusnamelistener.h"
#include "subscriberinterface.h"
#include "provider.h"
#include "iproviderplugin.h"
#include "asyncdbusinterface.h"
#include "timedvalue.h"
#include <QString>
#include <QDBusConnection>
#include <QDBusPendingCallWatcher>
#include <QDBusObjectPath>
#include <QSet>
#include <QVariant>
#include <QMap>
#include <QHash>

extern "C" {
    ContextSubscriber::IProviderPlugin* contextKitPluginFactory(QString constructionString);
}

namespace ContextSubscriber {
class PendingSubscribeWatcher : public QDBusPendingCallWatcher
{
    Q_OBJECT;

public:
    PendingSubscribeWatcher(const QDBusPendingCall &call,
                            const QString &key,
                            QObject * parent = 0);
private Q_SLOTS:
    void onFinished();

Q_SIGNALS:
    void subscribeFailed(QString, QString);
    void valueChanged(QString, TimedValue);
    void subscribeFinished(QString);

private:
    QString key;
};

class ContextKitPlugin : public IProviderPlugin
{
    Q_OBJECT

public:
    ContextKitPlugin(const QDBusConnection bus, const QString& busName);
    void subscribe(QSet<QString> keys);
    void unsubscribe(QSet<QString> keys);
    void setDefaultNewProtocol(bool s);

Q_SIGNALS:
#ifdef DOXYGEN_ONLY
    void ready(); ///< Emitted when the GetSubscriber call returns successfully.
    void failed(QString error); ///< Emitted when the GetSubscriber call fails or provider not on D-Bus at all.
    void subscribeFinished(QString key); ///< Emitted when Subscribe call succeeds on D-Bus.
    void subscribeFailed(QString failedKey, QString error); ///< Emitted when Subscribe call fails on D-Bus.
    void valueChanged(QString key, QVariant value); ///< Emitted when ValueChanged signal comes on D-Bus
#endif

private Q_SLOTS:
    void onNewValueChanged(QList<QVariant> value,
                           quint64 timestamp,
                           QDBusMessage message);
    void onDBusValuesChanged(QMap<QString, QVariant> values);
    void onDBusGetSubscriberFinished(QDBusObjectPath objectPath);
    void onDBusGetSubscriberFailed(QDBusError err);
    void onDBusSubscribeFinished(QList<QString> keys);
    void onDBusSubscribeFailed(QList<QString> keys, QString error);
    void onProviderAppeared();
    void onProviderDisappeared();
    void newSubscribe(const QString& key);

private:
    static QString keyToPath(QString key);

    void reset();
    void useNewProtocol();

    QMap<QString, QVariant>& mergeNullsWithMap(QMap<QString, QVariant> &map, QStringList nulls) const;

    DBusNameListener *providerListener; ///< Listens to provider's (dis)appearance over DBus
    SubscriberInterface *subscriberInterface; ///< The D-Bus interface for the Subscriber object
    QDBusAbstractInterface *managerInterface; ///< The D-Bus interface for the Manager object

    QDBusConnection *connection; ///< The connection to DBus
    QString busName; ///< The D-Bus service name of the ContextKit provider connected to

    bool newProtocol; ///< The current provider on D-Bus speaks the new protocol only.
    bool defaultNewProtocol; ///< Let's only try the new protocol to talk with the provider.

    QHash<QString, QString> objectPathToKey;

    static const QString managerIName; ///< org.freedesktop.ContextKit.Manager
    static const QString subscriberIName; ///< org.freedesktop.ContextKit.Subscriber
    static const QString managerPath; ///< /org/freedesktop/ContextKit/Manager
    static const QString propertyIName; ///< org.maemo.contextkit.Property
    static const QString corePrefix; ///< /org/maemo/contextkit/

};

QVariant demarshallValue(const QVariant &v);

}

#endif