File indexing completed on 2023-09-24 04:14:57
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_SERVICE_H 0008 #define PLASMA_SERVICE_H 0009 0010 #include <QHash> 0011 #include <QObject> 0012 #include <QVariant> 0013 0014 #include <KConfigGroup> 0015 0016 #include <plasma/plasma.h> 0017 #include <plasma/plasma_export.h> 0018 0019 class QIODevice; 0020 class QWidget; 0021 class QUrl; 0022 class QQuickItem; 0023 0024 namespace Plasma 0025 { 0026 class ServiceJob; 0027 class ServicePrivate; 0028 0029 /** 0030 * @class Service plasma/service.h <Plasma/Service> 0031 * 0032 * @short This class provides a generic API for write access to settings or services. 0033 * 0034 * Plasma::Service allows interaction with a "destination", the definition of which 0035 * depends on the Service itself. For a network settings Service this might be a 0036 * profile name ("Home", "Office", "Road Warrior") while a web based Service this 0037 * might be a username ("aseigo", "stranger65"). 0038 * 0039 * A Service provides one or more operations, each of which provides some sort 0040 * of interaction with the destination. Operations are described using config 0041 * XML which is used to create a KConfig object with one group per operation. 0042 * The group names are used as the operation names, and the defined items in 0043 * the group are the parameters available to be set when using that operation. 0044 * 0045 * A service is started with a KConfigGroup (representing a ready to be serviced 0046 * operation) and automatically deletes itself after completion and signaling 0047 * success or failure. See KJob for more information on this part of the process. 0048 * 0049 * Services may either be loaded "stand alone" from plugins, or from a DataEngine 0050 * by passing in a source name to be used as the destination. 0051 * 0052 * Sample use might look like: 0053 * 0054 * @code 0055 * Plasma::DataEngine *twitter = dataEngine("twitter"); 0056 * Plasma::Service *service = twitter.serviceForSource("aseigo"); 0057 * QVariantMap op = service->operationDescription("update"); 0058 * op.insert("tweet", "Hacking on plasma!"); 0059 * Plasma::ServiceJob *job = service->startOperationCall(op); 0060 * connect(job, SIGNAL(finished(KJob*)), this, SLOT(jobCompeted())); 0061 * @endcode 0062 * 0063 * Please remember, the service needs to be deleted when it will no longer be 0064 * used. This can be done manually or by these (perhaps easier) alternatives: 0065 * 0066 * If it is needed throughout the lifetime of the object: 0067 * @code 0068 * service->setParent(this); 0069 * @endcode 0070 * 0071 * If the service will not be used after just one operation call, use: 0072 * @code 0073 * connect(job, SIGNAL(finished(KJob*)), service, SLOT(deleteLater())); 0074 * @endcode 0075 * 0076 */ 0077 class PLASMA_EXPORT Service : public QObject 0078 { 0079 Q_OBJECT 0080 Q_DECLARE_PRIVATE(Service) 0081 Q_PROPERTY(QString destination READ destination WRITE setDestination) 0082 Q_PROPERTY(QStringList operationNames READ operationNames) 0083 Q_PROPERTY(QString name READ name) 0084 0085 public: 0086 /** 0087 * Destructor 0088 */ 0089 ~Service() override; 0090 0091 /** 0092 * Sets the destination for this Service to operate on 0093 * 0094 * @param destination specific to each Service, this sets which 0095 * target or address for ServiceJobs to operate on 0096 */ 0097 Q_INVOKABLE void setDestination(const QString &destination); 0098 0099 /** 0100 * @return the target destination, if any, that this service is associated with 0101 */ 0102 Q_INVOKABLE QString destination() const; 0103 0104 /** 0105 * @return the possible operations for this profile 0106 */ 0107 Q_INVOKABLE QStringList operationNames() const; 0108 0109 /** 0110 * Retrieves the parameters for a given operation 0111 * 0112 * @param operationName the operation to retrieve parameters for 0113 * @return QVariantMap containing the parameters 0114 */ 0115 Q_INVOKABLE QVariantMap operationDescription(const QString &operationName); 0116 0117 /** 0118 * Called to create a ServiceJob which is associated with a given 0119 * operation and parameter set. 0120 * 0121 * @return a started ServiceJob; the consumer may connect to relevant 0122 * signals before returning to the event loop 0123 */ 0124 Q_INVOKABLE ServiceJob *startOperationCall(const QVariantMap &description, QObject *parent = nullptr); 0125 0126 /** 0127 * Query to find if an operation is enabled or not. 0128 * 0129 * @param operation the name of the operation to check 0130 * @return true if the operation is enabled, false otherwise 0131 */ 0132 Q_INVOKABLE bool isOperationEnabled(const QString &operation) const; 0133 0134 /** 0135 * The name of this service 0136 */ 0137 Q_INVOKABLE QString name() const; 0138 0139 Q_SIGNALS: 0140 /** 0141 * Emitted when this service is ready for use 0142 */ 0143 void serviceReady(Plasma::Service *service); 0144 0145 /** 0146 * Emitted when an operation got enabled or disabled 0147 */ 0148 void operationEnabledChanged(const QString &operation, bool enabled); 0149 0150 protected: 0151 /** 0152 * Default constructor 0153 * 0154 * @param parent the parent object for this service 0155 */ 0156 explicit Service(QObject *parent = nullptr); 0157 0158 /** 0159 * Constructor for plugin loading 0160 */ 0161 Service(QObject *parent, const QVariantList &args); 0162 0163 /** 0164 * Called when a job should be created by the Service. 0165 * 0166 * @param operation which operation to work on 0167 * @param parameters the parameters set by the user for the operation 0168 * @return a ServiceJob that can be started and monitored by the consumer 0169 */ 0170 virtual ServiceJob *createJob(const QString &operation, QVariantMap ¶meters) = 0; 0171 0172 /** 0173 * By default this is based on the file in plasma/services/name.operations, but can be 0174 * reimplemented to use a different mechanism. 0175 * 0176 * It should result in a call to setOperationsScheme(QIODevice *); 0177 */ 0178 virtual void registerOperationsScheme(); 0179 0180 /** 0181 * Sets the XML used to define the operation schema for 0182 * this Service. 0183 */ 0184 void setOperationsScheme(QIODevice *xml); 0185 0186 /** 0187 * Sets the name of the Service; useful for Services not loaded from plugins, 0188 * which use the plugin name for this. 0189 * 0190 * @param name the name to use for this service 0191 */ 0192 void setName(const QString &name); 0193 0194 /** 0195 * Enables a given service by name 0196 * 0197 * @param operation the name of the operation to enable or disable 0198 * @param enable true if the operation should be enabled, false if disabled 0199 */ 0200 void setOperationEnabled(const QString &operation, bool enable); 0201 0202 private: 0203 ServicePrivate *const d; 0204 0205 friend class DataEnginePrivate; 0206 friend class PluginLoader; 0207 }; 0208 0209 } // namespace Plasma 0210 0211 /** 0212 * Register a service when it is contained in a loadable module 0213 * @deprecated Since 5.88, use K_PLUGIN_CLASS_WITH_JSON instead 0214 */ 0215 #define K_EXPORT_PLASMA_SERVICE(libname, classname) K_PLUGIN_FACTORY(factory, registerPlugin<classname>();) 0216 0217 /// @deprecated Since 5.88, use K_PLUGIN_CLASS_WITH_JSON instead 0218 #define K_EXPORT_PLASMA_SERVICE_WITH_JSON(libname, classname, jsonFile) K_PLUGIN_FACTORY_WITH_JSON(factory, jsonFile, registerPlugin<classname>();) 0219 0220 #endif // multiple inclusion guard