From 3461b97edc172d0b2f0bd6d378821b4af9056e71 Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Tue, 12 Aug 2008 13:58:58 +0000 Subject: [PATCH] support enabling and disabling operations in services svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=845852 --- private/service_p.h | 18 ++++++++------- service.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++- service.h | 16 ++++++++++++++ 3 files changed, 78 insertions(+), 9 deletions(-) diff --git a/private/service_p.h b/private/service_p.h index 8e06f55e1..ae0d42549 100644 --- a/private/service_p.h +++ b/private/service_p.h @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -77,14 +78,6 @@ public: delete tempFile; } - Service *q; - QString destination; - QString name; - ConfigXml *config; - KTemporaryFile *tempFile; - QMultiHash associatedWidgets; - QMultiHash associatedGraphicsWidgets; - void jobFinished(KJob* job) { emit q->finished(static_cast(job)); @@ -99,6 +92,15 @@ public: { associatedGraphicsWidgets.remove(static_cast(obj)); } + + Service *q; + QString destination; + QString name; + ConfigXml *config; + KTemporaryFile *tempFile; + QMultiHash associatedWidgets; + QMultiHash associatedGraphicsWidgets; + QSet disabledOperations; }; } // namespace Plasma diff --git a/service.cpp b/service.cpp index 3c2af1c54..be22b1d86 100644 --- a/service.cpp +++ b/service.cpp @@ -137,9 +137,14 @@ ServiceJob* Service::startOperationCall(const KConfigGroup &description) return new NullServiceJob(parent()); } + QString op = description.name(); + if (d->disabledOperations.contains(op)) { + kDebug() << "Operation" << op << "is disabled"; + return new NullServiceJob(parent()); + } + d->config->writeConfig(); QMap params; - QString op = description.name(); foreach (const QString &key, description.keyList()) { KConfigSkeletonItem *item = d->config->findItem(op, key); if (item) { @@ -157,12 +162,20 @@ void Service::associateWidget(QWidget *widget, const QString &operation) { d->associatedWidgets.insert(widget, operation); connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(associatedWidgetDestroyed(QObject*))); + + if (d->disabledOperations.contains(operation)) { + widget->setEnabled(false); + } } void Service::associateWidget(QGraphicsWidget *widget, const QString &operation) { d->associatedGraphicsWidgets.insert(widget, operation); connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(associatedGraphicsWidgetDestroyed(QObject*))); + + if (d->disabledOperations.contains(operation)) { + widget->setEnabled(false); + } } QString Service::name() const @@ -184,6 +197,44 @@ void Service::setName(const QString &name) registerOperationsScheme(); } +void Service::setOperationEnabled(const QString &operation, bool enable) +{ + if (!d->config->hasGroup(operation)) { + return; + } + + if (enable) { + d->disabledOperations.remove(operation); + } else if (!d->disabledOperations.contains(operation)) { + d->disabledOperations.insert(operation); + } + + { + QHashIterator it(d->associatedWidgets); + while (it.hasNext()) { + it.next(); + if (it.value() == operation) { + it.key()->setEnabled(enable); + } + } + } + + { + QHashIterator it(d->associatedGraphicsWidgets); + while (it.hasNext()) { + it.next(); + if (it.value() == operation) { + it.key()->setEnabled(enable); + } + } + } +} + +bool Service::operationIsEnabled(const QString &operation) const +{ + d->config->hasGroup(operation) && !d->disabledOperations.contains(operation); +} + void Service::setOperationsScheme(QIODevice *xml) { delete d->config; diff --git a/service.h b/service.h index c81e7efae..0d221bb73 100644 --- a/service.h +++ b/service.h @@ -204,6 +204,22 @@ protected: */ void setName(const QString &name); + /** + * Enables a given service by name + * + * @param operation the name of the operation to enable or disable + * @param enable true if the operation should be enabld, false if disabled + */ + void setOperationEnabled(const QString &operation, bool enable); + + /** + * Query to find if an operation is enabled or not. + * + * @param operation the name of the operation to check + * @return true if the operation is enabled, false otherwise + */ + bool operationIsEnabled(const QString &operation) const; + private: Q_PRIVATE_SLOT(d, void jobFinished(KJob *)) Q_PRIVATE_SLOT(d, void associatedWidgetDestroyed(QObject *))