summaryrefslogtreecommitdiff
path: root/TelepathyQt/abstract-interface.cpp
blob: bd42d0c37450dbe15c82863ee118732a385c26da (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
/**
 * This file is part of TelepathyQt
 *
 * @copyright Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
 * @copyright Copyright (C) 2009 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 <TelepathyQt/AbstractInterface>

#include "TelepathyQt/_gen/abstract-interface.moc.hpp"

#include "TelepathyQt/debug-internal.h"

#include <TelepathyQt/Constants>
#include <TelepathyQt/DBusProxy>
#include <TelepathyQt/PendingVariant>
#include <TelepathyQt/PendingVariantMap>
#include <TelepathyQt/PendingVoid>
#include <TelepathyQt/Types>

#include <QDBusPendingCall>
#include <QDBusVariant>

namespace Tp
{

struct TP_QT_NO_EXPORT AbstractInterface::Private
{
    Private();
    QString mError;
    QString mMessage;
    bool monitorProperties;
};

AbstractInterface::Private::Private()
    : monitorProperties(false)
{
}

/**
 * \class AbstractInterface
 * \ingroup clientsideproxies
 * \headerfile TelepathyQt/abstract-interface.h <TelepathyQt/AbstractInterface>
 *
 * \brief The AbstractInterface class is the base class for all client side
 * D-Bus interfaces, allowing access to remote methods/properties/signals.
 */

AbstractInterface::AbstractInterface(const QString &busName,
        const QString &path, const QLatin1String &interface,
        const QDBusConnection &dbusConnection, QObject *parent)
    : QDBusAbstractInterface(busName, path, interface.latin1(), dbusConnection, parent),
      mPriv(new Private)
{
}

AbstractInterface::AbstractInterface(DBusProxy *parent, const QLatin1String &interface)
    : QDBusAbstractInterface(parent->busName(), parent->objectPath(),
            interface.latin1(), parent->dbusConnection(), parent),
      mPriv(new Private)
{
    connect(parent, SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)),
            this, SLOT(invalidate(Tp::DBusProxy*,QString,QString)));
}

AbstractInterface::~AbstractInterface()
{
    delete mPriv;
}

bool AbstractInterface::isValid() const
{
    return QDBusAbstractInterface::isValid() && mPriv->mError.isEmpty();
}

QString AbstractInterface::invalidationReason() const
{
    return mPriv->mError;
}

QString AbstractInterface::invalidationMessage() const
{
    return mPriv->mMessage;
}

void AbstractInterface::invalidate(DBusProxy *proxy,
        const QString &error, const QString &message)
{
    Q_ASSERT(!error.isEmpty());

    if (mPriv->mError.isEmpty()) {
        mPriv->mError = error;
        mPriv->mMessage = message;
    }
}

PendingVariant *AbstractInterface::internalRequestProperty(const QString &name) const
{
    QDBusMessage msg = QDBusMessage::createMethodCall(service(), path(),
            TP_QT_IFACE_PROPERTIES, QLatin1String("Get"));
    msg << interface() << name;
    QDBusPendingCall pendingCall = connection().asyncCall(msg);
    DBusProxy *proxy = qobject_cast<DBusProxy*>(parent());
    return new PendingVariant(pendingCall, DBusProxyPtr(proxy));
}

PendingOperation *AbstractInterface::internalSetProperty(const QString &name,
        const QVariant &newValue)
{
    QDBusMessage msg = QDBusMessage::createMethodCall(service(), path(),
            TP_QT_IFACE_PROPERTIES, QLatin1String("Set"));
    msg << interface() << name << QVariant::fromValue(QDBusVariant(newValue));
    QDBusPendingCall pendingCall = connection().asyncCall(msg);
    DBusProxy *proxy = qobject_cast<DBusProxy*>(parent());
    return new PendingVoid(pendingCall, DBusProxyPtr(proxy));
}

PendingVariantMap *AbstractInterface::internalRequestAllProperties() const
{
    QDBusMessage msg = QDBusMessage::createMethodCall(service(), path(),
            TP_QT_IFACE_PROPERTIES, QLatin1String("GetAll"));
    msg << interface();
    QDBusPendingCall pendingCall = connection().asyncCall(msg);
    DBusProxy *proxy = qobject_cast<DBusProxy*>(parent());
    return new PendingVariantMap(pendingCall, DBusProxyPtr(proxy));
}

/**
 * Sets whether this abstract interface will be monitoring properties or not. If it's set to monitor,
 * the signal propertiesChanged will be emitted whenever a property on this interface will
 * change.
 *
 * By default, AbstractInterface does not monitor properties: you need to call this method
 * for this to happen.
 *
 * \param monitorProperties Whether this interface should monitor property changes or not.
 * \sa isMonitoringProperties
 *     propertiesChanged()
 */
void AbstractInterface::setMonitorProperties(bool monitorProperties)
{
    if (monitorProperties == mPriv->monitorProperties) {
        return;
    }

    QStringList argumentMatch;
    argumentMatch << interface();

    bool success;

    if (monitorProperties) {
        success = connection().connect(service(), path(), TP_QT_IFACE_PROPERTIES,
                QLatin1String("PropertiesChanged"), argumentMatch,
                QString(), this,
                SLOT(onPropertiesChanged(QString,QVariantMap,QStringList)));
    } else {
        success = connection().disconnect(service(), path(), TP_QT_IFACE_PROPERTIES,
                QLatin1String("PropertiesChanged"), argumentMatch,
                QString(), this,
                SLOT(onPropertiesChanged(QString,QVariantMap,QStringList)));
    }

    if (!success) {
        warning() << "Connection or disconnection to " << TP_QT_IFACE_PROPERTIES <<
                ".PropertiesChanged failed.";
    }
}

/**
 * Return whether this abstract interface is monitoring properties or not. If it's monitoring,
 * the signal propertiesChanged will be emitted whenever a property on this interface will
 * change.
 *
 * By default, AbstractInterface does not monitor properties: you need to call setMonitorProperties
 * for this to happen.
 *
 * \return \c true if the interface is monitoring for property changes, \c false otherwise.
 * \sa setMonitorProperties
 *     propertiesChanged()
 */
bool AbstractInterface::isMonitoringProperties() const
{
    return mPriv->monitorProperties;
}

void AbstractInterface::onPropertiesChanged(const QString &interface,
            const QVariantMap &changedProperties,
            const QStringList &invalidatedProperties)
{
    emit propertiesChanged(changedProperties, invalidatedProperties);
}

/**
 * \fn void AbstractInterface::propertiesChanged(const QVariantMap &changedProperties,
 *             const QStringList &invalidatedProperties)
 *
 * Emitted when one or more properties on this interface change or become invalidated.
 * This signal will be emitted only if the interface is monitoring properties.
 *
 * \param changedProperties A map of the changed properties with their new value, if any.
 * \param invalidatedProperties A list of the invalidated properties, if any.
 * \sa isMonitoringProperties()
 */

} // Tp