How to: Nepomuk QML plugin

Hello all!

Today I’ll be talking about something useful (apparently) for all those wanting to use
data stored in Nepomuk via a QML plasmoid.

So, let’s start!

First of all you need a C++ class able to extract info from nepomuk and store it in a class (don’t ask me how to do that. It’s black magic for me at the moment).
In our case (KDE-Telepathy) we have contacts stored in nepomuk and the info we need are the usual im infos one would want to know about a contact: mail, nickname, online presence, presence message and all that kind of stuff.

So, once you’ve got this sorted out it’s time to create your “QML export plugin” or whatever you want to call it.

Your plugin class will have to inherit “QDeclarativeExtensionPlugin”.
Here is a header file so you can see what’s to be done (taken from my plugin in the ktelepathy library)

declarativeplugins.h

#include

/**
* @class DeclarativePlugins
* @brief Class to enable access via QML
* This class permits QML plasmoids to access data stored by the library
* @author Francesco Nwokeka
*/

class DeclarativePlugins : public QDeclarativeExtensionPlugin
{
Q_OBJECT

public:
void registerTypes(const char *uri);
};

Q_EXPORT_PLUGIN2(declarativeplugins, DeclarativePlugins)

The QDeclarativeExtensionPlugin::registerTypes is a virtual funtion so it’s to be re-implemented.
“What is that function supposed to do?” you’re probably thinking.

Well as documentation says:

“Registers the QML types in the given uri. Subclasses should implement this to call qmlRegisterType() for all types which are provided by the extension plugin. The uri is an identifier for the plugin generated by the QML engine based on the name and path of the extension’s plugin library.”

so, let’s see how we’re supposed to re-implement this function.

declarativeplugins.cpp

#include "declarativeplugins.h"
#include "everyone-person-set-model.h"

#include

void DeclarativePlugins::registerTypes(const char* uri)
{
Q_ASSERT(uri == QLatin1String("org.kde.telepathy.declarative"));

qmlRegisterType(uri, 0, 1, "ContactListModel");
}

#include "declarativeplugins.moc"

As you can see, the “uri” (identifier for the plugin) is the string to be used in your qml file to actually “import” the QML plugin.

Once you’ve defined how you want to import your plugin, you’ll have to define the C++ type in the QML system so that you can access it via import that in this case will be something like this:


import org.kde.telepathy.declarativeplugins 0.1 as TelepathyDeclarative

and the C++ class will be defined in QML like this:


qmlRegisterType(uri, 0, 1, "ContactListModel");

To access the class from your QML file you’ll have to write a line like the following:


TelepathyDeclarative.ContactListModel{}

So once you’ve defined these few things you’re ready to go! Hope this helped!

Advertisements
Tagged ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: