File indexing completed on 2023-10-01 04:11:45
0001 /* 0002 SPDX-FileCopyrightText: 2008 Aaron Seigo <aseigo@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef PLASMA_SERVICEJOB_H 0008 #define PLASMA_SERVICEJOB_H 0009 0010 #include <QVariant> 0011 0012 #include <KJob> 0013 0014 #include <plasma/plasma_export.h> 0015 0016 namespace Plasma 0017 { 0018 class ServiceJobPrivate; 0019 0020 /** 0021 * @class ServiceJob plasma/servicejob.h <Plasma/ServiceJob> 0022 * 0023 * @short This class provides jobs for use with Plasma::Service 0024 * 0025 * Unlike KJob, you can do the work in start(), since Plasma::Service already 0026 * delays the call to start() until the event loop is reached. 0027 * 0028 * If the job is quick enough that it is not worth reporting the progress, 0029 * you just need to implement start() to do the task, then call emitResult() 0030 * at the end of it. If the task does not complete successfully, you should 0031 * set a non-zero error code with setError(int) and an error message with 0032 * setErrorText(QString). 0033 * 0034 * If the job is longer (involving network access, for instance), you should 0035 * report the progress at regular intervals. See the KJob documentation for 0036 * information on how to do this. 0037 */ 0038 class PLASMA_EXPORT ServiceJob : public KJob 0039 { 0040 Q_OBJECT 0041 Q_PROPERTY(QString destination READ destination) 0042 Q_PROPERTY(QString operationName READ operationName) 0043 Q_PROPERTY(QVariant result READ result) 0044 0045 public: 0046 /** 0047 * Default constructor 0048 * 0049 * @param destination the subject that the job is acting on 0050 * @param operation the action that the job is performing on the @p destination 0051 * @param parameters the parameters of the @p action 0052 * @param parent the parent object for this service 0053 */ 0054 ServiceJob(const QString &destination, const QString &operation, const QVariantMap ¶meters, QObject *parent = nullptr); 0055 0056 /** 0057 * Destructor 0058 */ 0059 ~ServiceJob() override; 0060 0061 /** 0062 * @return the subject that the job is acting on 0063 */ 0064 QString destination() const; 0065 0066 /** 0067 * @return the operation the job is performing on the destination 0068 */ 0069 QString operationName() const; 0070 0071 /** 0072 * @return the parameters for the operation 0073 */ 0074 QVariantMap parameters() const; 0075 0076 /** 0077 * Returns the result of the operation 0078 * 0079 * The result will be invalid if the job has not completed yet, or 0080 * if the job does not have a meaningful result. 0081 * 0082 * Note that this should not be used to find out whether the operation 0083 * was successful. Instead, you should check the value of error(). 0084 * 0085 * @return the result of the operation 0086 */ 0087 QVariant result() const; 0088 0089 /** 0090 * Default implementation of start, which simply sets the results to false. 0091 * This makes it easy to create a "failure" job. 0092 */ 0093 Q_INVOKABLE void start() override; 0094 0095 protected: 0096 /** 0097 * Sets the result for an operation. 0098 */ 0099 void setResult(const QVariant &result); 0100 0101 private: 0102 Q_PRIVATE_SLOT(d, void autoStart()) 0103 Q_PRIVATE_SLOT(d, void preventAutoStart()) 0104 0105 ServiceJobPrivate *const d; 0106 }; 0107 0108 } // namespace Plasma 0109 0110 #endif // multiple inclusion guard