* applet loading

* make the library names appear in descending order of specificity (plasma_kind_instance)

svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=668019
This commit is contained in:
Aaron J. Seigo 2007-05-24 20:01:12 +00:00
parent 14a1013a16
commit 1f797b162f
5 changed files with 135 additions and 28 deletions

View File

@ -67,5 +67,8 @@ install( FILES
includes/Svg
DESTINATION ${INCLUDE_INSTALL_DIR}/KDE/Plasma )
install( FILES plasma_dataengine.desktop DESTINATION ${SERVICETYPES_INSTALL_DIR} )
install( FILES
plasma_applet.desktop
plasma_dataengine.desktop
DESTINATION ${SERVICETYPES_INSTALL_DIR} )

View File

@ -27,6 +27,7 @@
#include <KPluginInfo>
#include <KStandardDirs>
#include <KService>
#include <KServiceTypeTrader>
#include "interface.h"
@ -40,8 +41,12 @@ class Applet::Private
: appletId( uniqueID ),
globalConfig( 0 ),
appletConfig( 0 ),
appletDescription( new KPluginInfo( appletDescription ) )
{ }
appletDescription(new KPluginInfo(appletDescription))
{
if (appletId > s_maxAppletId) {
s_maxAppletId = appletId;
}
}
~Private()
{
@ -51,23 +56,42 @@ class Applet::Private
delete appletDescription;
}
int appletId;
static uint nextId()
{
++s_maxAppletId;
return s_maxAppletId;
}
uint appletId;
KSharedConfig::Ptr globalConfig;
KSharedConfig::Ptr appletConfig;
KPluginInfo* appletDescription;
QList<QObject*> watchedForFocus;
QStringList loadedEngines;
static uint s_maxAppletId;
};
Applet::Applet( QGraphicsItem *parent,
const QString& serviceID,
int appletId )
: QWidget( 0 ),
QGraphicsItemGroup( parent ),
d( new Private( KService::serviceByStorageId( serviceID ), appletId ) )
uint Applet::Private::s_maxAppletId = 0;
Applet::Applet(QGraphicsItem *parent,
const QString& serviceID,
int appletId)
: QObject(0),
QGraphicsItemGroup(parent),
d(new Private(KService::serviceByStorageId(serviceID), appletId))
{
}
Applet::Applet(QObject* parent, const QStringList& args)
: QObject(parent),
QGraphicsItemGroup(0),
d(new Private(KService::serviceByStorageId(args[0]), args[1].toInt()))
{
// the brain damage seen in the initialization list is due to the
// rediculous inflexibility of KService::createInstance
// too bad i couldn't convince others that this was a real issue.
}
Applet::~Applet()
{
needsFocus( false );
@ -144,8 +168,7 @@ void Applet::watchForFocus(QObject *widget, bool watch)
void Applet::needsFocus( bool focus )
{
if ( focus == QWidget::hasFocus() ||
focus == QGraphicsItem::hasFocus() ) {
if (focus == QGraphicsItem::hasFocus()) {
return;
}
@ -164,7 +187,47 @@ bool Applet::eventFilter( QObject *o, QEvent * e )
}
}
return QWidget::eventFilter( o, e );
return QObject::eventFilter(o, e);
}
KPluginInfo::List Applet::knownApplets()
{
QHash<QString, KPluginInfo> applets;
KService::List offers = KServiceTypeTrader::self()->query("Plasma/Applet");
return KPluginInfo::fromServices(offers);
}
Applet* Applet::loadApplet(const QString& appletName, uint appletId)
{
if (appletName.isEmpty()) {
return 0;
}
QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(appletName);
KService::List offers = KServiceTypeTrader::self()->query("Plasma/Applet", constraint);
if (offers.isEmpty()) {
//TODO: what would be -really- cool is offer to try and download the applet
// from the network at this point
kDebug() << "Applet::loadApplet: offers is empty for \"" << appletName << "\"" << endl;
return 0;
}
if (appletId == 0) {
appletId = Private::nextId();
}
QStringList sillyness;
QString id;
id.setNum(appletId);
sillyness << offers.first()->storageId() << id;
Applet* applet = KService::createInstance<Plasma::Applet>(offers.first(), 0, sillyness);
if (!applet) {
kDebug() << "Couldn't load applet \"" << appletName << "\"!" << endl;
}
return applet;
}
} // Plasma namespace

View File

@ -22,7 +22,9 @@
#include <QtGui/QGraphicsItemGroup>
#include <QtGui/QWidget>
#include <ksharedconfig.h>
#include <KPluginInfo>
#include <KSharedConfig>
#include <KGenericFactory>
#include <plasma.h>
#include <dataengine.h>
@ -34,12 +36,13 @@ namespace Plasma
*
*
*/
class PLASMA_EXPORT Applet : public QWidget, public QGraphicsItemGroup
class PLASMA_EXPORT Applet : public QObject, public QGraphicsItemGroup
{
Q_OBJECT
public:
typedef QList<Applet*> List;
typedef QHash<QString, Applet*> Dict;
/**
* @arg parent the QGraphicsItem this applet is parented to
@ -48,9 +51,21 @@ class PLASMA_EXPORT Applet : public QWidget, public QGraphicsItemGroup
* @arg appletId a unique id used to differentiate between multiple
* instances of the same Applet type
*/
Applet( QGraphicsItem* parent,
const QString& serviceId,
int appletId );
Applet(QGraphicsItem* parent,
const QString& serviceId,
int appletId);
/**
* This constructor is to be used with the plugin loading systems
* found in KPluginInfo and KService. The argument list is expected
* to have two elements: the KService service ID for the desktop entry
* and an applet ID which must be a base 10 number.
*
* @arg parent a QObject parent; you probably want to pass in 0
* @arg args a list of strings containing two entries: the service id
* and the applet id
*/
Applet(QObject* parent, const QStringList& args);
~Applet();
/**
@ -87,8 +102,25 @@ class PLASMA_EXPORT Applet : public QWidget, public QGraphicsItemGroup
*/
virtual void constraintsUpdated();
public Q_SLOTS:
virtual void updated(const QString& source, const Plasma::DataEngine::Data&) = 0;
/**
* Returns a list of all known applets in a hash keyed by a unique
* identifier for each applet
*
* @return list of applets
**/
static KPluginInfo::List knownApplets();
/**
* Attempts to load an applet, returning a pointer to the applet if
* successful. The caller takes responsibility for the applet, including
* deleting it when no longer needed.
*
* @param name the plugin name, as returned by KPluginInfo::pluginName()
* @param applet unique ID to assign the applet, or zero to have one
* assigned automatically.
* @return a pointer to the loaded applet, or 0 on load failure
**/
static Applet* loadApplet(const QString &name, uint appletId = 0);
Q_SIGNALS:
void requestFocus( bool focus );
@ -123,12 +155,12 @@ class PLASMA_EXPORT Applet : public QWidget, public QGraphicsItemGroup
Private* const d;
};
#define K_EXPORT_PLASMA_APPLET(libname, classname) \
K_EXPORT_COMPONENT_FACTORY( \
plasmaapplet_##libname, \
KGenericFactory<classname>("libplasmaapplet_" #libname))
} // Plasma namespace
#define K_EXPORT_PLASMA_APPLET(libname, classname) \
K_EXPORT_COMPONENT_FACTORY( \
plasma_applet_##libname, \
KGenericFactory<classname>("plasma_applet_" #libname))
#endif // multiple inclusion guard

View File

@ -24,6 +24,8 @@
#include <QtCore/QObject>
#include <QtCore/QStringList>
#include <KGenericFactory>
#include <plasma_export.h>
namespace Plasma
@ -198,8 +200,8 @@ class PLASMA_EXPORT DataEngine : public QObject
#define K_EXPORT_PLASMA_DATAENGINE(libname, classname) \
K_EXPORT_COMPONENT_FACTORY( \
plasma_##libname##_engine, \
KGenericFactory<classname>("plasma_" #libname "_engine"))
plasma_engine_##libname, \
KGenericFactory<classname>("plasma_engine_" #libname))
#endif // multiple inclusion guard

7
plasma_applet.desktop Normal file
View File

@ -0,0 +1,7 @@
[Desktop Entry]
Encoding=UTF-8
Type=ServiceType
X-KDE-ServiceType=Plasma/Applet
Comment=Plasma applet