~ubuntu-branches/ubuntu/saucy/kactivities/saucy-proposed

« back to all changes in this revision

Viewing changes to src/lib/core/utils_p.h

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman, Jonathan Riddell, Scott Kitterman
  • Date: 2012-11-20 13:47:27 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20121120134727-vqzk04slqjoay3de
Tags: 4:4.9.80-0ubuntu1
[ Jonathan Riddell ]
* New upstream beta release

[ Scott Kitterman ]
* Add new libkactivities-models1 library package (debian/control, .install,
  and .symbols)
* Add nepomuk-core-dev to build-depends
* Wildcard libkactivities6.install so that soversion changes don't cause
  build failures
* Update libkactivities6.symbols
* Update libkactivities-dev.install for new files for libkactivies-models
* Add new files to libkactivities-bin.install, including Activities kcm

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2012 Ivan Cukic <ivan.cukic(at)kde.org>
 
3
 *
 
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
 
7
 *   Software Foundation
 
8
 *
 
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
 
13
 *
 
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.
 
18
 */
 
19
 
 
20
#include <QDBusPendingCallWatcher>
 
21
 
 
22
#include <QMutex>
 
23
 
 
24
#include <KDebug>
 
25
#include <KLocalizedString>
 
26
 
 
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);        \
 
34
                                                                                        \
 
35
    QObject::connect(Variable##CallWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)), \
 
36
            Target, SLOT(Variable##CallFinished(QDBusPendingCallWatcher*)))
 
37
 
 
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) \
 
41
    mutable Type Name;                               \
 
42
    QDBusPendingCallWatcher * Name##CallWatcher;     \
 
43
    QMutex Name##Mutex
 
44
 
 
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)
 
49
 
 
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;              \
 
56
                                                                   \
 
57
    QDBusReply < TYPE > dbusReply = Manager::OBJECT()->METHOD;     \
 
58
    if (dbusReply.isValid()) {                                     \
 
59
        return dbusReply.value();                                  \
 
60
    } else {                                                       \
 
61
        kDebug() << "d-bus reply was invalid"                      \
 
62
                 << dbusReply.value()                              \
 
63
                 << dbusReply.error();                             \
 
64
        return DEFAULT;                                            \
 
65
    }
 
66
 
 
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)       \
 
70
    {                                                                            \
 
71
        QDBusPendingReply <ReturnType> reply = * call;                           \
 
72
                                                                                 \
 
73
        Variable = reply.isError()                                               \
 
74
            ? DefaultValue                                                       \
 
75
            : reply.argumentAt<0>();                                             \
 
76
                                                                                 \
 
77
        Variable##CallWatcher = 0;                                               \
 
78
        Variable##Mutex.unlock();                                                \
 
79
        call->deleteLater();                                                     \
 
80
        kDebug() << "Unlocked mutex";                                            \
 
81
    }
 
82
 
 
83
// Implements a value getter
 
84
#define KAMD_REMOTE_VALUE_GETTER(ReturnType, Namespace, Variable, Default) \
 
85
    ReturnType Namespace::Variable() const                            \
 
86
    {                                                                 \
 
87
        if (!Manager::isServicePresent()) return Default;             \
 
88
        waitForCallFinished(d->Variable##CallWatcher, &d->Variable##Mutex); \
 
89
        kDebug() << "Returning" << #Variable << d->Variable;          \
 
90
        return d->Variable;                                           \
 
91
    }
 
92
 
 
93
static inline void waitForCallFinished(QDBusPendingCallWatcher * watcher, QMutex * mutex)
 
94
{
 
95
    if (watcher) {
 
96
        watcher->waitForFinished();
 
97
 
 
98
        kDebug() << "Trying to lock mutex";
 
99
        mutex->lock();
 
100
        mutex->unlock();
 
101
    }
 
102
}