Projects/Nepomuk/QuickStart: Difference between revisions

From KDE TechBase
m (Added Nepomuk tag and Soprano header files; Changed Variant to Nepomuk::Variant in examples.)
(update to new syntaxhighlighter)
Line 17: Line 17:
The ResourceManager is the central KMetaData configuration point. For KDE 4.2 and newer, we must explicitly initialize it in order to connect to the Nepomuk service.
The ResourceManager is the central KMetaData configuration point. For KDE 4.2 and newer, we must explicitly initialize it in order to connect to the Nepomuk service.


<code cppqt>
<syntaxhighlight lang="cpp-qt">
Nepomuk::ResourceManager::instance()->init();
Nepomuk::ResourceManager::instance()->init();
</code>
</syntaxhighlight>


This method returns an <i>int</i>. If it succeeds (returns 0), the connection to the Nepomuk service has been established and we can work with it. If it fails (returns negative integer), the Nepomuk service was unreachable and we can't continue working with any Nepomuk related code--this may occur if the user has disabled the Nepomuk service for his current session.
This method returns an <i>int</i>. If it succeeds (returns 0), the connection to the Nepomuk service has been established and we can work with it. If it fails (returns negative integer), the Nepomuk service was unreachable and we can't continue working with any Nepomuk related code--this may occur if the user has disabled the Nepomuk service for his current session.
Line 27: Line 27:
Let's get the metadata for a file, assuming the URL or the file is stored in <i>uri</i>.
Let's get the metadata for a file, assuming the URL or the file is stored in <i>uri</i>.


<code cppqt>
<syntaxhighlight lang="cpp-qt">
Nepomuk::Resource res( uri );
Nepomuk::Resource res( uri );
QHash<QUrl, Nepomuk::Variant> properties = res.properties();
QHash<QUrl, Nepomuk::Variant> properties = res.properties();
</code>
</syntaxhighlight>


This allows us to obtain all the properties assigned to the file.
This allows us to obtain all the properties assigned to the file.
Line 36: Line 36:
We can now use Nepomuk to get human-readable labels for the properties, and display the properties in a generic way:
We can now use Nepomuk to get human-readable labels for the properties, and display the properties in a generic way:


<code cppqt>
<syntaxhighlight lang="cpp-qt">
for( QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
for( QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
     it != properties.constEnd(); ++it ) {
     it != properties.constEnd(); ++it ) {
Line 46: Line 46:
   someList->appendItem( propertyType.label() + ": " + value.toString() );
   someList->appendItem( propertyType.label() + ": " + value.toString() );
}
}
</code>
</syntaxhighlight>


===Setting Metadata===
===Setting Metadata===
Line 54: Line 54:
Let's start with the tag and use the easy Nepomukish way:
Let's start with the tag and use the easy Nepomukish way:


<code cppqt>
<syntaxhighlight lang="cpp-qt">
Nepomuk::Tag tag( "This is my nice tag name" );
Nepomuk::Tag tag( "This is my nice tag name" );
Nepomuk::Resource res( uri );
Nepomuk::Resource res( uri );
res.addTag( tag );
res.addTag( tag );
</code>
</syntaxhighlight>


Simple! Actually, if the tag already exists, it will be reused.
Simple! Actually, if the tag already exists, it will be reused.
Line 64: Line 64:
Now let's set a comment for the file without the use of the convenience methods in Nepomuk:
Now let's set a comment for the file without the use of the convenience methods in Nepomuk:


<code cppqt>
<syntaxhighlight lang="cpp-qt">
Nepomuk::Resource res( uri );
Nepomuk::Resource res( uri );
QString comment = getFancyFileComment();
QString comment = getFancyFileComment();
res.setProperty( Soprano::Vocabulary::NAO::description(), comment );
res.setProperty( Soprano::Vocabulary::NAO::description(), comment );
</code>
</syntaxhighlight>


That's all. The comment is saved and will now be searchable via Nepomuk.
That's all. The comment is saved and will now be searchable via Nepomuk.
Line 74: Line 74:
===Includes and Linking===
===Includes and Linking===
For the example you need the following includes:
For the example you need the following includes:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
#include <Nepomuk/ResourceManager>
#include <Nepomuk/ResourceManager>
#include <Nepomuk/Variant>
#include <Nepomuk/Variant>
Line 80: Line 80:
#include <Nepomuk/Tag>
#include <Nepomuk/Tag>
#include <Soprano/Vocabulary/NAO>
#include <Soprano/Vocabulary/NAO>
</code>
</syntaxhighlight>
In your CMakeLists.txt, you need the find macro for Nepomuk and link to it of course ;)
In your CMakeLists.txt, you need the find macro for Nepomuk and link to it of course ;)
<code cppqt>
<syntaxhighlight lang="cmake">
find_package(Nepomuk REQUIRED)
find_package(Nepomuk REQUIRED)
target_link_libraries(myfile ${NEPOMUK_LIBRARIES} )
target_link_libraries(myfile ${NEPOMUK_LIBRARIES} )
</code>
</syntaxhighlight>

Revision as of 19:57, 29 June 2011


Development/Tutorials/Metadata/Nepomuk/Quickstart


Nepmuk Quickstart
Tutorial Series   Nepomuk
Previous   Getting started with KDE development
What's Next   Handle Resource Metadata with Nepomuk
Further Reading   n/a

Nepomuk Quickstart

Reading or setting simple metadata in your application with Nepomuk, can be very easy. However, keep in mind that the process described here may have performance drawbacks when changing a lot of metadata. We will now take a look at a simple way to access a resource's metadata.

Initializing the Resource Manager

The ResourceManager is the central KMetaData configuration point. For KDE 4.2 and newer, we must explicitly initialize it in order to connect to the Nepomuk service.

Nepomuk::ResourceManager::instance()->init();

This method returns an int. If it succeeds (returns 0), the connection to the Nepomuk service has been established and we can work with it. If it fails (returns negative integer), the Nepomuk service was unreachable and we can't continue working with any Nepomuk related code--this may occur if the user has disabled the Nepomuk service for his current session.

Retrieving Metadata

Let's get the metadata for a file, assuming the URL or the file is stored in uri.

Nepomuk::Resource res( uri );
QHash<QUrl, Nepomuk::Variant> properties = res.properties();

This allows us to obtain all the properties assigned to the file.

We can now use Nepomuk to get human-readable labels for the properties, and display the properties in a generic way:

for( QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
     it != properties.constEnd(); ++it ) {
   QUrl propertyUri = it.key();
   Nepomuk::Variant value = it.value();

   Nepomuk::Types::Class propertyType( propertyUri );

   someList->appendItem( propertyType.label() + ": " + value.toString() );
}

Setting Metadata

Similarly, assume uri is the URL of a file that we want to set some metadata to. This time we want to set a tag and a comment and will do this using two slightly different methods:

Let's start with the tag and use the easy Nepomukish way:

Nepomuk::Tag tag( "This is my nice tag name" );
Nepomuk::Resource res( uri );
res.addTag( tag );

Simple! Actually, if the tag already exists, it will be reused.

Now let's set a comment for the file without the use of the convenience methods in Nepomuk:

Nepomuk::Resource res( uri );
QString comment = getFancyFileComment();
res.setProperty( Soprano::Vocabulary::NAO::description(), comment );

That's all. The comment is saved and will now be searchable via Nepomuk.

Includes and Linking

For the example you need the following includes:

#include <Nepomuk/ResourceManager>
#include <Nepomuk/Variant>
#include <Nepomuk/Types/Class>
#include <Nepomuk/Tag>
#include <Soprano/Vocabulary/NAO>

In your CMakeLists.txt, you need the find macro for Nepomuk and link to it of course ;)

find_package(Nepomuk REQUIRED)
target_link_libraries(myfile ${NEPOMUK_LIBRARIES} )