File indexing completed on 2024-04-28 03:52:06

0001 /*
0002  * BluezQt - Asynchronous BlueZ wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #ifndef BLUEZQT_PENDINGCALL_H
0010 #define BLUEZQT_PENDINGCALL_H
0011 
0012 #include <functional>
0013 
0014 #include <QObject>
0015 
0016 #include "bluezqt_export.h"
0017 
0018 #include <memory>
0019 
0020 class QDBusError;
0021 class QDBusPendingCall;
0022 class QDBusPendingCallWatcher;
0023 
0024 namespace BluezQt
0025 {
0026 /**
0027  * @class BluezQt::PendingCall pendingcall.h <BluezQt/PendingCall>
0028  *
0029  * Pending method call.
0030  *
0031  * This class represents a pending method call. It is a convenient wrapper
0032  * around QDBusPendingReply and QDBusPendingCallWatcher.
0033  */
0034 class BLUEZQT_EXPORT PendingCall : public QObject
0035 {
0036     Q_OBJECT
0037 
0038     Q_PROPERTY(QVariant value READ value)
0039     Q_PROPERTY(QVariantList values READ values)
0040     Q_PROPERTY(int error READ error)
0041     Q_PROPERTY(QString errorText READ errorText)
0042     Q_PROPERTY(bool isFinished READ isFinished)
0043     Q_PROPERTY(QVariant userData READ userData WRITE setUserData)
0044 
0045 public:
0046     /**
0047      * Known error types.
0048      */
0049     enum Error {
0050         /** Indicates there is no error. */
0051         NoError = 0,
0052         /** Indicates that the device is not ready. */
0053         NotReady = 1,
0054         /** Indicates that the action have failed. */
0055         Failed = 2,
0056         /** Indicates that the action was rejected. */
0057         Rejected = 3,
0058         /** Indicates that the action was canceled. */
0059         Canceled = 4,
0060         /** Indicates that invalid arguments were passed. */
0061         InvalidArguments = 5,
0062         /** Indicates that an agent or pairing record already exists. */
0063         AlreadyExists = 6,
0064         /** Indicates that an agent, service or pairing operation does not exists. */
0065         DoesNotExist = 7,
0066         /** Indicates that the action is already in progress. */
0067         InProgress = 8,
0068         /** Indicates that the action is not in progress. */
0069         NotInProgress = 9,
0070         /** Indicates that the device is already connected. */
0071         AlreadyConnected = 10,
0072         /** Indicates that the connection to the device have failed. */
0073         ConnectFailed = 11,
0074         /** Indicates that the device is not connected. */
0075         NotConnected = 12,
0076         /** Indicates that the action is not supported. */
0077         NotSupported = 13,
0078         /** Indicates that the caller is not authorized to do the action. */
0079         NotAuthorized = 14,
0080         /** Indicates that the authentication was canceled. */
0081         AuthenticationCanceled = 15,
0082         /** Indicates that the authentication have failed. */
0083         AuthenticationFailed = 16,
0084         /** Indicates that the authentication was rejected. */
0085         AuthenticationRejected = 17,
0086         /** Indicates that the authentication timed out. */
0087         AuthenticationTimeout = 18,
0088         /** Indicates that the connection attempt have failed. */
0089         ConnectionAttemptFailed = 19,
0090         /** Indicates that the data provided generates a data packet which is too long. */
0091         InvalidLength = 20,
0092         /** Indicates that the action is not permitted (e.g. maximum reached or socket locked). */
0093         NotPermitted = 21,
0094         /** Indicates an error with D-Bus. */
0095         DBusError = 98,
0096         /** Indicates an internal error. */
0097         InternalError = 99,
0098         /** Indicates an unknown error. */
0099         UnknownError = 100,
0100     };
0101     Q_ENUM(Error)
0102 
0103     /**
0104      * Destroys a PendingCall object.
0105      */
0106     ~PendingCall() override;
0107 
0108     /**
0109      * Returns a first return value of the call.
0110      *
0111      * @return first return value
0112      */
0113     QVariant value() const;
0114 
0115     /**
0116      * Returns all values of the call.
0117      *
0118      * @return all return values
0119      */
0120     QVariantList values() const;
0121 
0122     /**
0123      * Returns an error code.
0124      *
0125      * @return error code
0126      * @see Error
0127      */
0128     int error() const;
0129 
0130     /**
0131      * Returns an error text.
0132      *
0133      * @return error text
0134      */
0135     QString errorText() const;
0136 
0137     /**
0138      * Returns whether the call is finished.
0139      *
0140      * @return true if call is finished
0141      */
0142     bool isFinished() const;
0143 
0144     /**
0145      * Waits for the call to finish.
0146      *
0147      * @warning This method blocks until the call finishes!
0148      */
0149     void waitForFinished();
0150 
0151     /**
0152      * Returns the user data of the call.
0153      *
0154      * @return user data of call
0155      */
0156     QVariant userData() const;
0157 
0158     /**
0159      * Sets the user data of the call.
0160      *
0161      * @param userData user data
0162      */
0163     void setUserData(const QVariant &userData);
0164 
0165 Q_SIGNALS:
0166     /**
0167      * Indicates that the call have finished.
0168      */
0169     void finished(PendingCall *call);
0170 
0171 private:
0172     enum ReturnType {
0173         ReturnVoid,
0174         ReturnUint32,
0175         ReturnString,
0176         ReturnStringList,
0177         ReturnObjectPath,
0178         ReturnFileTransferList,
0179         ReturnTransferWithProperties,
0180         ReturnByteArray
0181     };
0182 
0183     BLUEZQT_NO_EXPORT explicit PendingCall(Error error, const QString &errorText, QObject *parent = nullptr);
0184     BLUEZQT_NO_EXPORT explicit PendingCall(const QDBusPendingCall &call, ReturnType type, QObject *parent = nullptr);
0185 
0186     // exported because called from template BluezQt::TPendingCall constructor
0187     using ErrorProcessor = std::function<void(const QDBusError &error)>;
0188     using ExternalProcessor = std::function<void(QDBusPendingCallWatcher *watcher, ErrorProcessor errorProcessor, QVariantList *values)>;
0189     explicit PendingCall(const QDBusPendingCall &call, ExternalProcessor externalProcessor, QObject *parent = nullptr);
0190 
0191     std::unique_ptr<class PendingCallPrivate> const d;
0192 
0193     friend class PendingCallPrivate;
0194     friend class Manager;
0195     friend class Adapter;
0196     friend class GattServiceRemote;
0197     friend class GattCharacteristicRemote;
0198     friend class GattDescriptorRemote;
0199     friend class Device;
0200     friend class GattManager;
0201     friend class LEAdvertisingManager;
0202     friend class Media;
0203     friend class MediaPlayer;
0204     friend class ObexManager;
0205     friend class ObexTransfer;
0206     friend class ObexSession;
0207     friend class ObexObjectPush;
0208     friend class ObexFileTransfer;
0209     template<class... T>
0210     friend class TPendingCall;
0211 };
0212 
0213 } // namespace BluezQt
0214 
0215 #endif // BLUEZQT_PENDINGCALL_H