support enabling and disabling operations in services

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=845852
This commit is contained in:
Aaron J. Seigo 2008-08-12 13:58:58 +00:00
parent 0e22094ff0
commit 3461b97edc
3 changed files with 78 additions and 9 deletions

View File

@ -25,6 +25,7 @@
#include <QGraphicsWidget> #include <QGraphicsWidget>
#include <QMultiHash> #include <QMultiHash>
#include <QWidget> #include <QWidget>
#include <QSet>
#include <KTemporaryFile> #include <KTemporaryFile>
@ -77,14 +78,6 @@ public:
delete tempFile; delete tempFile;
} }
Service *q;
QString destination;
QString name;
ConfigXml *config;
KTemporaryFile *tempFile;
QMultiHash<QWidget *, QString> associatedWidgets;
QMultiHash<QGraphicsWidget *, QString> associatedGraphicsWidgets;
void jobFinished(KJob* job) void jobFinished(KJob* job)
{ {
emit q->finished(static_cast<ServiceJob*>(job)); emit q->finished(static_cast<ServiceJob*>(job));
@ -99,6 +92,15 @@ public:
{ {
associatedGraphicsWidgets.remove(static_cast<QGraphicsWidget*>(obj)); associatedGraphicsWidgets.remove(static_cast<QGraphicsWidget*>(obj));
} }
Service *q;
QString destination;
QString name;
ConfigXml *config;
KTemporaryFile *tempFile;
QMultiHash<QWidget *, QString> associatedWidgets;
QMultiHash<QGraphicsWidget *, QString> associatedGraphicsWidgets;
QSet<QString> disabledOperations;
}; };
} // namespace Plasma } // namespace Plasma

View File

@ -137,9 +137,14 @@ ServiceJob* Service::startOperationCall(const KConfigGroup &description)
return new NullServiceJob(parent()); 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(); d->config->writeConfig();
QMap<QString, QVariant> params; QMap<QString, QVariant> params;
QString op = description.name();
foreach (const QString &key, description.keyList()) { foreach (const QString &key, description.keyList()) {
KConfigSkeletonItem *item = d->config->findItem(op, key); KConfigSkeletonItem *item = d->config->findItem(op, key);
if (item) { if (item) {
@ -157,12 +162,20 @@ void Service::associateWidget(QWidget *widget, const QString &operation)
{ {
d->associatedWidgets.insert(widget, operation); d->associatedWidgets.insert(widget, operation);
connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(associatedWidgetDestroyed(QObject*))); 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) void Service::associateWidget(QGraphicsWidget *widget, const QString &operation)
{ {
d->associatedGraphicsWidgets.insert(widget, operation); d->associatedGraphicsWidgets.insert(widget, operation);
connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(associatedGraphicsWidgetDestroyed(QObject*))); connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(associatedGraphicsWidgetDestroyed(QObject*)));
if (d->disabledOperations.contains(operation)) {
widget->setEnabled(false);
}
} }
QString Service::name() const QString Service::name() const
@ -184,6 +197,44 @@ void Service::setName(const QString &name)
registerOperationsScheme(); 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<QWidget *, QString> it(d->associatedWidgets);
while (it.hasNext()) {
it.next();
if (it.value() == operation) {
it.key()->setEnabled(enable);
}
}
}
{
QHashIterator<QGraphicsWidget *, QString> 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) void Service::setOperationsScheme(QIODevice *xml)
{ {
delete d->config; delete d->config;

View File

@ -204,6 +204,22 @@ protected:
*/ */
void setName(const QString &name); 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: private:
Q_PRIVATE_SLOT(d, void jobFinished(KJob *)) Q_PRIVATE_SLOT(d, void jobFinished(KJob *))
Q_PRIVATE_SLOT(d, void associatedWidgetDestroyed(QObject *)) Q_PRIVATE_SLOT(d, void associatedWidgetDestroyed(QObject *))