2
* Copyright (C) 2012 Ivan Cukic <ivan.cukic(at)kde.org>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 2,
6
* or (at your option) any later version, as published by the Free
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details
14
* You should have received a copy of the GNU General Public
15
* License along with this program; if not, write to the
16
* Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
#include <QDBusPendingCallWatcher>
25
#include <KLocalizedString>
27
// Creates an async call to retrieve a value from the dbus service
28
// and initializes the call watcher
29
#define KAMD_RETRIEVE_REMOTE_VALUE(Variable, MethodToCall, Target) \
30
kDebug() << "Locking mutex for" << #Variable; \
31
Variable##Mutex.lock(); \
32
const QDBusPendingCall & Variable##Call = Manager::activities()->MethodToCall; \
33
Variable##CallWatcher = new QDBusPendingCallWatcher(Variable##Call, Target); \
35
QObject::connect(Variable##CallWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)), \
36
Target, SLOT(Variable##CallFinished(QDBusPendingCallWatcher*)))
38
// Defines a variable and handlers for a variable on a dbus service
39
// without a handler for when a call is finished
40
#define KAMD_REMOTE_VALUE_CUSTOM_HANDLER(Type, Name) \
42
QDBusPendingCallWatcher * Name##CallWatcher; \
45
// Defines a variable and handlers for a variable on a dbus service
46
#define KAMD_REMOTE_VALUE(Type, Name) \
47
KAMD_REMOTE_VALUE_CUSTOM_HANDLER(Type, Name); \
48
void Name##CallFinished(QDBusPendingCallWatcher * call)
50
// macro defines a shorthand for validating and returning a d-bus result
51
// @param TYPE type of the result
52
// @param METHOD invocation of the d-bus method
53
// @param DEFAULT value to be used if the reply was not valid
54
#define KAMD_RETRIEVE_REMOTE_VALUE_SYNC(TYPE, OBJECT, METHOD, DEFAULT) \
55
if (!Manager::isServicePresent()) return DEFAULT; \
57
QDBusReply < TYPE > dbusReply = Manager::OBJECT()->METHOD; \
58
if (dbusReply.isValid()) { \
59
return dbusReply.value(); \
61
kDebug() << "d-bus reply was invalid" \
62
<< dbusReply.value() \
63
<< dbusReply.error(); \
67
// Defines a handler for pre-fetching of the activity info
68
#define KAMD_RETRIEVE_REMOTE_VALUE_HANDLER(ReturnType, Namespace, Variable, DefaultValue) \
69
void Namespace::Variable##CallFinished(QDBusPendingCallWatcher * call) \
71
QDBusPendingReply <ReturnType> reply = * call; \
73
Variable = reply.isError() \
75
: reply.argumentAt<0>(); \
77
Variable##CallWatcher = 0; \
78
Variable##Mutex.unlock(); \
79
call->deleteLater(); \
80
kDebug() << "Unlocked mutex"; \
83
// Implements a value getter
84
#define KAMD_REMOTE_VALUE_GETTER(ReturnType, Namespace, Variable, Default) \
85
ReturnType Namespace::Variable() const \
87
if (!Manager::isServicePresent()) return Default; \
88
waitForCallFinished(d->Variable##CallWatcher, &d->Variable##Mutex); \
89
kDebug() << "Returning" << #Variable << d->Variable; \
93
static inline void waitForCallFinished(QDBusPendingCallWatcher * watcher, QMutex * mutex)
96
watcher->waitForFinished();
98
kDebug() << "Trying to lock mutex";