File indexing completed on 2024-04-21 15:43:00

0001 /*
0002  * This file is part of TelepathyLoggerQt
0003  *
0004  * Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
0005  * Copyright (C) 2011 Nokia Corporation
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Lesser General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2.1 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library; if not, write to the Free Software
0019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0020  */
0021 
0022 #include "pending-operation.h"
0023 
0024 #include <QtCore/QDebug>
0025 #include <QtCore/QTimer>
0026 
0027 using namespace Tpl;
0028 
0029 struct TELEPATHY_LOGGER_QT_NO_EXPORT PendingOperation::Private
0030 {
0031     Private()
0032         : finished(false)
0033     {
0034     }
0035 
0036     QString errorName;
0037     QString errorMessage;
0038     bool finished;
0039 };
0040 
0041 
0042 PendingOperation::PendingOperation()
0043     : QObject(),
0044       mPriv(new Private())
0045 {
0046     QTimer::singleShot(0, this, SLOT(start()));
0047 }
0048 
0049 PendingOperation::~PendingOperation()
0050 {
0051     if (!mPriv->finished) {
0052         qWarning() << this <<
0053             "still pending when it was deleted - finished will "
0054             "never be emitted";
0055     }
0056 
0057     delete mPriv;
0058 }
0059 
0060 void PendingOperation::emitFinished()
0061 {
0062     Q_ASSERT(mPriv->finished);
0063     Q_EMIT finished(this);
0064     deleteLater();
0065 }
0066 
0067 void PendingOperation::setFinished()
0068 {
0069     if (mPriv->finished) {
0070         if (!mPriv->errorName.isEmpty()) {
0071             qWarning() << this << "trying to finish with success, but already"
0072                 " failed with" << mPriv->errorName << ":" << mPriv->errorMessage;
0073         } else {
0074             qWarning() << this << "trying to finish with success, but already"
0075                 " succeeded";
0076         }
0077         return;
0078     }
0079 
0080     mPriv->finished = true;
0081     Q_ASSERT(isValid());
0082     QTimer::singleShot(0, this, SLOT(emitFinished()));
0083 }
0084 
0085 void PendingOperation::setFinishedWithError(const QString &name,
0086         const QString &message)
0087 {
0088     if (mPriv->finished) {
0089         if (mPriv->errorName.isEmpty()) {
0090             qWarning() << this << "trying to fail with" << name <<
0091                 "but already failed with" << errorName() << ":" <<
0092                 errorMessage();
0093         } else {
0094             qWarning() << this << "trying to fail with" << name <<
0095                 "but already succeeded";
0096         }
0097         return;
0098     }
0099 
0100     if (name.isEmpty()) {
0101         qWarning() << this << "should be given a non-empty error name";
0102         mPriv->errorName = QLatin1String("org.freedesktop.Telepathy.Qt4.ErrorHandlingError");
0103     } else {
0104         mPriv->errorName = name;
0105     }
0106 
0107     mPriv->errorMessage = message;
0108     mPriv->finished = true;
0109     Q_ASSERT(isError());
0110     QTimer::singleShot(0, this, SLOT(emitFinished()));
0111 }
0112 
0113 
0114 bool PendingOperation::isValid() const
0115 {
0116     return (mPriv->finished && mPriv->errorName.isEmpty());
0117 }
0118 
0119 bool PendingOperation::isFinished() const
0120 {
0121     return mPriv->finished;
0122 }
0123 
0124 bool PendingOperation::isError() const
0125 {
0126     return (mPriv->finished && !mPriv->errorName.isEmpty());
0127 }
0128 
0129 QString PendingOperation::errorName() const
0130 {
0131     return mPriv->errorName;
0132 }
0133 
0134 QString PendingOperation::errorMessage() const
0135 {
0136     return mPriv->errorMessage;
0137 }
0138 
0139 void PendingOperation::start()
0140 {
0141 }