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 #ifndef _TelepathyLoggerQt_pending_operation_h_HEADER_GUARD_
0022 #define _TelepathyLoggerQt_pending_operation_h_HEADER_GUARD_
0023 
0024 #include <QtCore/QObject>
0025 
0026 #include <TelepathyLoggerQt_export.h>
0027 
0028 namespace Tpl
0029 {
0030 
0031 /**
0032  * \headerfile pending-operation.h <TelepathyLoggerQt/PendingOperation>
0033  * \brief Abstract base class for pending asynchronous operations.
0034  *
0035  * This class represents an incomplete asynchronous operation, such as a
0036  * D-Bus method call. When the operation has finished, it emits
0037  * #finished(). The slot or slots connected to the #finished() signal may obtain
0038  * additional information from the PendingOperation.
0039  *
0040  * For pending operations that produce a result, another subclass of
0041  * PendingOperation can be used, with additional methods that provide that
0042  * result to the library user.
0043  *
0044  * After #finished() is emitted, the PendingOperation is automatically
0045  * deleted using deleteLater(), so library users must not explicitly
0046  * delete this object.
0047  *
0048  * The design is loosely based on KDE's KJob.
0049  */
0050 class TELEPATHY_LOGGER_QT_EXPORT PendingOperation : public QObject
0051 {
0052     Q_OBJECT
0053     Q_DISABLE_COPY(PendingOperation)
0054 
0055 public:
0056 
0057     /**
0058      * \brief Class destructor.
0059      */
0060     virtual ~PendingOperation();
0061 
0062     /**
0063      * \brief Returns whether or not the request has finished processing.
0064      *
0065      * finished() is emitted when this changes from <code>false</code> to
0066      * <code>true</code>.
0067      *
0068      * Equivalent to <code>(isValid() || isError())</code>.
0069      *
0070      * \sa finished()
0071      *
0072      * \return <code>true</code> if the request has finished
0073      */
0074     bool isFinished() const;
0075 
0076     /**
0077      * \brief Returns whether or not the request completed successfully.
0078      *
0079      * If the request has not yet finished processing (isFinished() returns
0080      * <code>false</code>), this cannot yet be known, and <code>false</code>
0081      * will be returned.
0082      *
0083      * Equivalent to <code>(isFinished() && !#isError())</code>.
0084      *
0085      * \return <code>true</code> iff the request has finished processing AND
0086      *         has completed successfully.
0087      */
0088     bool isValid() const;
0089 
0090     /**
0091      * \brief Returns whether or not the request resulted in an error.
0092      *
0093      * If the request has not yet finished processing (isFinished() returns
0094      * <code>false</code>), this cannot yet be known, and <code>false</code>
0095      * will be returned.
0096      *
0097      * Equivalent to <code>(isFinished() && !#isValid())</code>.
0098      *
0099      * \return <code>true</code> iff the request has finished processing AND
0100      *         has resulted in an error.
0101      */
0102     bool isError() const;
0103 
0104 
0105     /**
0106      * \brief If isError() would return true, returns the D-Bus error with which
0107      * the operation failed.
0108      *
0109      * If the operation succeeded or has not yet
0110      * finished, returns an empty string.
0111      *
0112      * \return a D-Bus error name or an empty string
0113      */
0114     QString errorName() const;
0115 
0116     /**
0117      * \brief If isError() would return true, returns a debugging message associated
0118      * with the error.
0119      *
0120      * The message may be an empty string. Otherwise, return an
0121      * empty string.
0122      *
0123      * \return a debugging message or an empty string
0124      */
0125     QString errorMessage() const;
0126 
0127 Q_SIGNALS:
0128     /**
0129      * \brief Emitted when the pending operation finishes.
0130      *
0131      * Emitted when #isFinished() changes from <code>false</code> to <code>true</code>.
0132      *
0133      * \param operation This operation object, from which further information
0134      *                  may be obtained
0135      */
0136     void finished(Tpl::PendingOperation *operation);
0137 
0138 protected:
0139     /**
0140      * \brief Protected constructor.
0141      *
0142      * Only subclasses of this class may be constructed.
0143      *
0144      * \param object The object on which this pending operation takes place
0145      */
0146     PendingOperation();
0147 
0148 protected Q_SLOTS:
0149     /**
0150      * \brief Record that this pending operation has finished successfully.
0151      *
0152      * Will emit the #finished() signal next time the event loop runs.
0153      */
0154     void setFinished();
0155 
0156     /**
0157      * \brief Record that this pending operation has finished with an error.
0158      *
0159      * Will emit the #finished() signal next time the event loop runs.
0160      *
0161      * \param name A D-Bus error name, which must be non-empty
0162      * \param message A debugging message
0163      */
0164     void setFinishedWithError(const QString &name, const QString &message);
0165 
0166 private Q_SLOTS:
0167     virtual void start();
0168     void emitFinished();
0169 
0170 private:
0171     struct Private;
0172     friend struct Private;
0173     Private *mPriv;
0174 };
0175 
0176 } // Tpl
0177 
0178 #endif