| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtDBus module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #ifndef QDBUSREPLY_H | 
|---|
| 43 | #define QDBUSREPLY_H | 
|---|
| 44 |  | 
|---|
| 45 | #include <QtCore/qglobal.h> | 
|---|
| 46 | #include <QtCore/qvariant.h> | 
|---|
| 47 |  | 
|---|
| 48 | #include <QtDBus/qdbusmacros.h> | 
|---|
| 49 | #include <QtDBus/qdbusmessage.h> | 
|---|
| 50 | #include <QtDBus/qdbuserror.h> | 
|---|
| 51 | #include <QtDBus/qdbusextratypes.h> | 
|---|
| 52 | #include <QtDBus/qdbuspendingreply.h> | 
|---|
| 53 |  | 
|---|
| 54 | QT_BEGIN_HEADER | 
|---|
| 55 |  | 
|---|
| 56 | QT_BEGIN_NAMESPACE | 
|---|
| 57 |  | 
|---|
| 58 | QT_MODULE(DBus) | 
|---|
| 59 |  | 
|---|
| 60 | QDBUS_EXPORT void qDBusReplyFill(const QDBusMessage &reply, QDBusError &error, QVariant &data); | 
|---|
| 61 |  | 
|---|
| 62 | template<typename T> | 
|---|
| 63 | class QDBusReply | 
|---|
| 64 | { | 
|---|
| 65 | typedef T Type; | 
|---|
| 66 | public: | 
|---|
| 67 | inline QDBusReply(const QDBusMessage &reply) | 
|---|
| 68 | { | 
|---|
| 69 | *this = reply; | 
|---|
| 70 | } | 
|---|
| 71 | inline QDBusReply& operator=(const QDBusMessage &reply) | 
|---|
| 72 | { | 
|---|
| 73 | QVariant data(qMetaTypeId(&m_data), reinterpret_cast<void*>(0)); | 
|---|
| 74 | qDBusReplyFill(reply, m_error, data); | 
|---|
| 75 | m_data = qvariant_cast<Type>(data); | 
|---|
| 76 | return *this; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | inline QDBusReply(const QDBusPendingCall &pcall) | 
|---|
| 80 | { | 
|---|
| 81 | *this = pcall; | 
|---|
| 82 | } | 
|---|
| 83 | inline QDBusReply &operator=(const QDBusPendingCall &pcall) | 
|---|
| 84 | { | 
|---|
| 85 | QDBusPendingCall other(pcall); | 
|---|
| 86 | other.waitForFinished(); | 
|---|
| 87 | return *this = other.reply(); | 
|---|
| 88 | } | 
|---|
| 89 | inline QDBusReply(const QDBusPendingReply<T> &reply) | 
|---|
| 90 | { | 
|---|
| 91 | *this = static_cast<QDBusPendingCall>(reply); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | inline QDBusReply(const QDBusError &dbusError = QDBusError()) | 
|---|
| 95 | : m_error(dbusError), m_data(Type()) | 
|---|
| 96 | { | 
|---|
| 97 | } | 
|---|
| 98 | inline QDBusReply& operator=(const QDBusError& dbusError) | 
|---|
| 99 | { | 
|---|
| 100 | m_error = dbusError; | 
|---|
| 101 | m_data = Type(); | 
|---|
| 102 | return *this; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | inline QDBusReply& operator=(const QDBusReply& other) | 
|---|
| 106 | { | 
|---|
| 107 | m_error = other.m_error; | 
|---|
| 108 | m_data = other.m_data; | 
|---|
| 109 | return *this; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | inline bool isValid() const { return !m_error.isValid(); } | 
|---|
| 113 |  | 
|---|
| 114 | inline const QDBusError& error() { return m_error; } | 
|---|
| 115 |  | 
|---|
| 116 | inline Type value() const | 
|---|
| 117 | { | 
|---|
| 118 | return m_data; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | inline operator Type () const | 
|---|
| 122 | { | 
|---|
| 123 | return m_data; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | private: | 
|---|
| 127 | QDBusError m_error; | 
|---|
| 128 | Type m_data; | 
|---|
| 129 | }; | 
|---|
| 130 |  | 
|---|
| 131 | # ifndef Q_QDOC | 
|---|
| 132 | // specialize for QVariant: | 
|---|
| 133 | template<> inline QDBusReply<QVariant>& | 
|---|
| 134 | QDBusReply<QVariant>::operator=(const QDBusMessage &reply) | 
|---|
| 135 | { | 
|---|
| 136 | void *null = 0; | 
|---|
| 137 | QVariant data(qMetaTypeId<QDBusVariant>(), null); | 
|---|
| 138 | qDBusReplyFill(reply, m_error, data); | 
|---|
| 139 | m_data = qvariant_cast<QDBusVariant>(data).variant(); | 
|---|
| 140 | return *this; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | // specialize for void: | 
|---|
| 144 | template<> | 
|---|
| 145 | class QDBusReply<void> | 
|---|
| 146 | { | 
|---|
| 147 | public: | 
|---|
| 148 | inline QDBusReply(const QDBusMessage &reply) | 
|---|
| 149 | : m_error(reply) | 
|---|
| 150 | { | 
|---|
| 151 | } | 
|---|
| 152 | inline QDBusReply& operator=(const QDBusMessage &reply) | 
|---|
| 153 | { | 
|---|
| 154 | m_error = reply; | 
|---|
| 155 | return *this; | 
|---|
| 156 | } | 
|---|
| 157 | inline QDBusReply(const QDBusError &dbusError = QDBusError()) | 
|---|
| 158 | : m_error(dbusError) | 
|---|
| 159 | { | 
|---|
| 160 | } | 
|---|
| 161 | inline QDBusReply(const QDBusPendingCall &pcall) | 
|---|
| 162 | { | 
|---|
| 163 | *this = pcall; | 
|---|
| 164 | } | 
|---|
| 165 | inline QDBusReply &operator=(const QDBusPendingCall &pcall) | 
|---|
| 166 | { | 
|---|
| 167 | QDBusPendingCall other(pcall); | 
|---|
| 168 | other.waitForFinished(); | 
|---|
| 169 | return *this = other.reply(); | 
|---|
| 170 | } | 
|---|
| 171 | inline QDBusReply& operator=(const QDBusError& dbusError) | 
|---|
| 172 | { | 
|---|
| 173 | m_error = dbusError; | 
|---|
| 174 | return *this; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | inline QDBusReply& operator=(const QDBusReply& other) | 
|---|
| 178 | { | 
|---|
| 179 | m_error = other.m_error; | 
|---|
| 180 | return *this; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | inline bool isValid() const { return !m_error.isValid(); } | 
|---|
| 184 |  | 
|---|
| 185 | inline const QDBusError& error() { return m_error; } | 
|---|
| 186 |  | 
|---|
| 187 | private: | 
|---|
| 188 | QDBusError m_error; | 
|---|
| 189 | }; | 
|---|
| 190 | # endif | 
|---|
| 191 |  | 
|---|
| 192 | QT_END_NAMESPACE | 
|---|
| 193 |  | 
|---|
| 194 | QT_END_HEADER | 
|---|
| 195 |  | 
|---|
| 196 | #endif | 
|---|