From 89b70374c30296d3ea179e1398bef9fceef40ab9 Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Tue, 31 Mar 2009 23:51:52 +0000 Subject: [PATCH] abstract the bit of data engine management code out into a small class that the private classes can then subclass, and add dataEngine(const QString &) to Wallpaper svn path=/trunk/KDE/kdelibs/; revision=947636 --- applet.cpp | 16 +-------- private/applet_p.h | 5 +-- private/dataengineconsumer_p.h | 65 ++++++++++++++++++++++++++++++++++ wallpaper.cpp | 10 +++++- wallpaper.h | 21 +++++++++++ 5 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 private/dataengineconsumer_p.h diff --git a/applet.cpp b/applet.cpp index ab06a69b9..2eb1371aa 100644 --- a/applet.cpp +++ b/applet.cpp @@ -574,17 +574,7 @@ ConfigLoader *Applet::configScheme() const DataEngine *Applet::dataEngine(const QString &name) const { - int index = d->loadedEngines.indexOf(name); - if (index != -1) { - return DataEngineManager::self()->engine(name); - } - - DataEngine *engine = DataEngineManager::self()->loadEngine(name); - if (engine->isValid()) { - d->loadedEngines.append(name); - } - - return engine; + return d->dataEngine(name); } const Package *Applet::package() const @@ -2035,10 +2025,6 @@ AppletPrivate::~AppletPrivate() activationAction->forgetGlobalShortcut(); } - foreach (const QString &engine, loadedEngines) { - DataEngineManager::self()->unloadEngine(engine); - } - if (extender) { delete extender; extender = 0; diff --git a/private/applet_p.h b/private/applet_p.h index 7c71d7c10..71453b486 100644 --- a/private/applet_p.h +++ b/private/applet_p.h @@ -26,6 +26,8 @@ #include +#include "plasma/private/dataengineconsumer_p.h" + class KKeySequenceWidget; namespace Plasma @@ -51,7 +53,7 @@ protected Q_SLOTS: void overlayAnimationComplete(); }; -class AppletPrivate +class AppletPrivate : public DataEngineConsumer { public: AppletPrivate(KService::Ptr service, int uniqueID, Applet *applet); @@ -107,7 +109,6 @@ public: AppletOverlayWidget *messageOverlay; Plasma::BusyWidget *busyWidget; QSet registeredAsDragHandle; - QStringList loadedEngines; Plasma::FrameSvg *background; AppletScript *script; QVariantList args; diff --git a/private/dataengineconsumer_p.h b/private/dataengineconsumer_p.h new file mode 100644 index 000000000..856cd700a --- /dev/null +++ b/private/dataengineconsumer_p.h @@ -0,0 +1,65 @@ +/* + * Copyright 2005 by Aaron Seigo + * Copyright 2007 by Riccardo Iaconelli + * Copyright 2008 by Ménard Alexis + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef PLASMA_DATAENGINECONSUMER_H +#define PLASMA_DATAENGINECONSUMER_H + +#include + +#include "plasma/dataenginemanager.h" + +namespace Plasma +{ + +class DataEngineConsumer +{ +public: + ~DataEngineConsumer() + { + foreach (const QString &engine, m_loadedEngines) { + DataEngineManager::self()->unloadEngine(engine); + } + } + + DataEngine *dataEngine(const QString &name) + { + int index = m_loadedEngines.indexOf(name); + if (index != -1) { + return DataEngineManager::self()->engine(name); + } + + DataEngine *engine = DataEngineManager::self()->loadEngine(name); + if (engine->isValid()) { + m_loadedEngines.append(name); + } + + return engine; + } + +private: + QStringList m_loadedEngines; +}; + +} // namespace Plasma + +#endif + + diff --git a/wallpaper.cpp b/wallpaper.cpp index ae75db848..339a86015 100644 --- a/wallpaper.cpp +++ b/wallpaper.cpp @@ -25,10 +25,12 @@ #include +#include "plasma/private/dataengineconsumer_p.h" + namespace Plasma { -class WallpaperPrivate +class WallpaperPrivate : public DataEngineConsumer { public: WallpaperPrivate(KService::Ptr service, Wallpaper *wallpaper) : @@ -109,6 +111,7 @@ Wallpaper *Wallpaper::load(const QString &wallpaperName, const QVariantList &arg if (!wallpaper) { kDebug() << "Couldn't load wallpaper \"" << wallpaperName << "\"! reason given: " << error; } + return wallpaper; } @@ -237,6 +240,11 @@ void Wallpaper::wheelEvent(QGraphicsSceneWheelEvent *event) Q_UNUSED(event) } +DataEngine *Wallpaper::dataEngine(const QString &name) const +{ + return d->dataEngine(name); +} + } // Plasma namespace #include "wallpaper.moc" diff --git a/wallpaper.h b/wallpaper.h index 00204f97d..2dbc5459b 100644 --- a/wallpaper.h +++ b/wallpaper.h @@ -28,6 +28,8 @@ namespace Plasma { + +class DataEngine; class WallpaperPrivate; /** @@ -204,6 +206,25 @@ class PLASMA_EXPORT Wallpaper : public QObject */ virtual void wheelEvent(QGraphicsSceneWheelEvent *event); + /** + * Loads the given DataEngine + * + * Tries to load the data engine given by @p name. Each engine is + * only loaded once, and that instance is re-used on all subsequent + * requests. + * + * If the data engine was not found, an invalid data engine is returned + * (see DataEngine::isValid()). + * + * Note that you should not delete the returned engine. + * + * @param name Name of the data engine to load + * @return pointer to the data engine if it was loaded, + * or an invalid data engine if the requested engine + * could not be loaded + */ + Q_INVOKABLE DataEngine *dataEngine(const QString &name) const; + Q_SIGNALS: /** * This signal indicates that wallpaper needs to be repainted.