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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
/**
* This file is part of TelepathyQt
*
* @copyright Copyright (C) 2008-2009 Collabora Ltd. <http://www.collabora.co.uk/>
* @copyright Copyright (C) 2008-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/PendingOperation>
#define IN_TP_QT_HEADER
#include "simple-pending-operations.h"
#undef IN_TP_QT_HEADER
#include "TelepathyQt/_gen/pending-operation.moc.hpp"
#include "TelepathyQt/_gen/simple-pending-operations.moc.hpp"
#include "TelepathyQt/debug-internal.h"
#include <QDBusPendingCall>
#include <QDBusPendingCallWatcher>
#include <QTimer>
namespace Tp
{
struct TP_QT_NO_EXPORT PendingOperation::Private
{
Private(const SharedPtr<RefCounted> &object)
: object(object),
finished(false)
{
}
SharedPtr<RefCounted> object;
QString errorName;
QString errorMessage;
bool finished;
};
/**
* \class PendingOperation
* \headerfile TelepathyQt/pending-operation.h <TelepathyQt/PendingOperation>
*
* \brief The PendingOperation class is a base class for pending asynchronous
* operations.
*
* This class represents an incomplete asynchronous operation, such as a
* D-Bus method call. When the operation has finished, it emits
* finished(). The slot or slots connected to the finished() signal may obtain
* additional information from the pending operation.
*
* In simple cases, like a D-Bus method with no 'out' arguments or for which
* all 'out' arguments are to be ignored (so the possible results are
* success with no extra information, or failure with an error code), the
* trivial subclass PendingVoid can be used.
*
* For pending operations that produce a result, another subclass of
* PendingOperation can be used, with additional methods that provide that
* result to the library user.
*
* After finished() is emitted, the PendingOperation is automatically
* deleted using deleteLater(), so library users must not explicitly
* delete this object.
*
* The design is loosely based on KDE's KJob.
*
* See \ref async_model
*/
/**
* Construct a new PendingOperation object.
*
* \param object The object on which this pending operation takes place.
*/
PendingOperation::PendingOperation(const SharedPtr<RefCounted> &object)
: QObject(),
mPriv(new Private(object))
{
}
/**
* Class destructor.
*/
PendingOperation::~PendingOperation()
{
if (!mPriv->finished) {
warning() << this <<
"still pending when it was deleted - finished will "
"never be emitted";
}
delete mPriv;
}
/**
* Return the object on which this pending operation takes place.
*
* \return A pointer to a RefCounted object.
*/
SharedPtr<RefCounted> PendingOperation::object() const
{
return mPriv->object;
}
void PendingOperation::emitFinished()
{
Q_ASSERT(mPriv->finished);
emit finished(this);
deleteLater();
}
/**
* Record that this pending operation has finished successfully, and
* emit the finished() signal next time the event loop runs.
*/
void PendingOperation::setFinished()
{
if (mPriv->finished) {
if (!mPriv->errorName.isEmpty()) {
warning() << this << "trying to finish with success, but already"
" failed with" << mPriv->errorName << ":" << mPriv->errorMessage;
} else {
warning() << this << "trying to finish with success, but already"
" succeeded";
}
return;
}
mPriv->finished = true;
Q_ASSERT(isValid());
QTimer::singleShot(0, this, SLOT(emitFinished()));
}
/**
* Record that this pending operation has finished with an error, and
* emit the finished() signal next time the event loop runs.
*
* \param name The D-Bus error name, which must be non-empty.
* \param message The debugging message.
*/
void PendingOperation::setFinishedWithError(const QString &name,
const QString &message)
{
if (mPriv->finished) {
if (mPriv->errorName.isEmpty()) {
warning() << this << "trying to fail with" << name <<
"but already failed with" << errorName() << ":" <<
errorMessage();
} else {
warning() << this << "trying to fail with" << name <<
"but already succeeded";
}
return;
}
if (name.isEmpty()) {
warning() << this << "should be given a non-empty error name";
mPriv->errorName = QLatin1String("org.freedesktop.Telepathy.Qt.ErrorHandlingError");
} else {
mPriv->errorName = name;
}
mPriv->errorMessage = message;
mPriv->finished = true;
Q_ASSERT(isError());
QTimer::singleShot(0, this, SLOT(emitFinished()));
}
/**
* Record that this pending operation has finished with an error, and
* emit the finished() signal next time the event loop runs.
*
* \param error The error.
* \sa finished()
*/
void PendingOperation::setFinishedWithError(const QDBusError &error)
{
setFinishedWithError(error.name(), error.message());
}
/**
* Return whether or not the request completed successfully. If the
* request has not yet finished processing (isFinished() returns
* \c false), this cannot yet be known, and \c false
* will be returned.
*
* Equivalent to <code>(isFinished() && !isError())</code>.
*
* \return \c true if the request has finished processing and
* has completed successfully, \c false otherwise.
*/
bool PendingOperation::isValid() const
{
return (mPriv->finished && mPriv->errorName.isEmpty());
}
/**
* Return whether or not the request has finished processing.
*
* The signal finished() is emitted when this changes from \c false
* to true.
*
* Equivalent to <code>(isValid() || isError())</code>.
*
* \return \c true if the request has finished, \c false otherwise.
* \sa finished()
*/
bool PendingOperation::isFinished() const
{
return mPriv->finished;
}
/**
* Return whether or not the request resulted in an error.
*
* If the request has not yet finished processing (isFinished() returns
* \c false), this cannot yet be known, and \c false
* will be returned.
*
* Equivalent to <code>(isFinished() && !isValid())</code>.
*
* \return \c true if the request has finished processing and
* has resulted in an error, \c false otherwise.
*/
bool PendingOperation::isError() const
{
return (mPriv->finished && !mPriv->errorName.isEmpty());
}
/**
* If isError() returns \c true, returns the D-Bus error with which
* the operation failed. If the operation succeeded or has not yet
* finished, returns an empty string.
*
* \return A D-Bus error name, or an empty string.
*/
QString PendingOperation::errorName() const
{
return mPriv->errorName;
}
/**
* If isError() would return \c true, returns a debugging message associated
* with the error, which may be an empty string. Otherwise, return an
* empty string.
*
* \return A debugging message, or an empty string.
*/
QString PendingOperation::errorMessage() const
{
return mPriv->errorMessage;
}
/**
* \fn void PendingOperation::finished(Tp::PendingOperation* operation)
*
* Emitted when the pending operation finishes, i.e. when isFinished()
* changes from \c false to \c true.
*
* \param operation This operation object, from which further information
* may be obtained.
*/
/**
* \class PendingSuccess
* \ingroup utils
* \headerfile TelepathyQt/simple-pending-operations.h <TelepathyQt/PendingSuccess>
*
* \brief The PendingSuccess class represents PendingOperation that is always
* successful.
*/
/**
* \class PendingFailure
* \ingroup utils
* \headerfile TelepathyQt/simple-pending-operations.h <TelepathyQt/PendingFailure>
*
* \brief The PendingFailure class represents a PendingOperation that always
* fails with the error passed to the constructor.
*/
/**
* \class PendingVoid
* \ingroup utils
* \headerfile TelepathyQt/simple-pending-operations.h <TelepathyQt/PendingVoid>
*
* \brief The PendingVoid class is a generic subclass of PendingOperation
* representing a pending D-Bus method call that does not return anything
* (or returns a result that is not interesting).
*/
/**
* Construct a new PendingVoid object.
*
* \param object The object on which this pending operation takes place.
* \param call A pending call as returned by the auto-generated low level
* Telepathy API; if the method returns anything, the return
* value(s) will be ignored.
*/
PendingVoid::PendingVoid(QDBusPendingCall call, const SharedPtr<RefCounted> &object)
: PendingOperation(object)
{
connect(new QDBusPendingCallWatcher(call),
SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(watcherFinished(QDBusPendingCallWatcher*)));
}
void PendingVoid::watcherFinished(QDBusPendingCallWatcher *watcher)
{
if (watcher->isError()) {
setFinishedWithError(watcher->error());
} else {
setFinished();
}
watcher->deleteLater();
}
struct TP_QT_NO_EXPORT PendingComposite::Private
{
Private(bool failOnFirstError, uint nOperations)
: failOnFirstError(failOnFirstError),
error(false),
nOperations(nOperations),
nOperationsFinished(0)
{
}
bool failOnFirstError;
bool error;
QString errorName;
QString errorMessage;
uint nOperations;
uint nOperationsFinished;
};
/**
* \class PendingComposite
* \ingroup utils
* \headerfile TelepathyQt/simple-pending-operations.h <TelepathyQt/PendingComposite>
*
* \brief The PendingComposite class is a PendingOperation that can be used
* to track multiple pending operations at once.
*/
PendingComposite::PendingComposite(const QList<PendingOperation*> &operations,
const SharedPtr<RefCounted> &object)
: PendingOperation(object),
mPriv(new Private(true, operations.size()))
{
foreach (PendingOperation *operation, operations) {
connect(operation,
SIGNAL(finished(Tp::PendingOperation*)),
SLOT(onOperationFinished(Tp::PendingOperation*)));
}
}
PendingComposite::PendingComposite(const QList<PendingOperation*> &operations,
bool failOnFirstError, const SharedPtr<RefCounted> &object)
: PendingOperation(object),
mPriv(new Private(failOnFirstError, operations.size()))
{
foreach (PendingOperation *operation, operations) {
connect(operation,
SIGNAL(finished(Tp::PendingOperation*)),
SLOT(onOperationFinished(Tp::PendingOperation*)));
}
}
PendingComposite::~PendingComposite()
{
delete mPriv;
}
void PendingComposite::onOperationFinished(Tp::PendingOperation *op)
{
if (op->isError()) {
if (mPriv->failOnFirstError) {
setFinishedWithError(op->errorName(), op->errorMessage());
return;
} else if (!mPriv->error) {
/* only save the first error that will be used on setFinishedWithError when all
* pending operations finish */
mPriv->error = true;
mPriv->errorName = op->errorName();
mPriv->errorMessage = op->errorMessage();
}
}
if (++mPriv->nOperationsFinished == mPriv->nOperations) {
if (!mPriv->error) {
setFinished();
} else {
setFinishedWithError(mPriv->errorName, mPriv->errorMessage);
}
}
}
} // Tp
|