Projects/Nepomuk/QuickStart: Difference between revisions

From KDE TechBase
(internationalization)
(Minor grammar edit)
Line 9: Line 9:
==Nepomuk Quickstart==
==Nepomuk Quickstart==


Reading or setting simple metadata in your own application can be very easy. But keep in mind that the process described here does not make much sense in terms of performance when changing a lot of metadata.
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.
We will now take a look at a simple way to access a resource's metadata.


===Initializing the Resource Manager===
===Initializing the Resource Manager===


The ResourceManager is the central KMetaData configuration point. After KDE 4.2 we must explicitly initialize it, so it connects 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>
<code cppqt>
Line 20: Line 20:
</code>
</code>


This method returns a bool. If it's true, the connection to the Nepomuk service is stablished and we can work with it. If it's false, we couldn't contact the service and we can't continue working with any Nepomuk related code. This is so because the user can disable the Nepomuk service for his session.
This method returns a <i>bool</i>. If it returns <i>true</i>, the connection to the Nepomuk service has been established and we can work with it. If it returns <i>false</i>, 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.


===Retrieve Metadata===
===Retrieving Metadata===


Let's get the metadata for a file. Imagine 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>
<code cppqt>
Line 31: Line 31:
</code>
</code>


That gives us all properties assigned to the file.
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:
We can now use Nepomuk to get human-readable labels for the properties, and display the properties in a generic way:


<code cppqt>
<code cppqt>
Line 47: Line 47:
</code>
</code>


===Set Metadata===
===Setting Metadata===


Again <i>uri</i> is the URL of the file we want to set some metadata for. This time we want to set a tag and a comment and will do this in two slightly different ways:
Similarly, assume <i>uri</i> 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:
Let's start with the tag and use the easy Nepomukish way:
Line 59: Line 59:
</code>
</code>


Simple! Actually if the tag already exists it will be reused.
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:
Now let's set a comment for the file without the use of the convenience methods in Nepomuk:

Revision as of 22:58, 21 May 2009


Development/Tutorials/Metadata/Nepomuk/Quickstart


Nepmuk Quickstart
Tutorial Series   Nepomuk
Previous   None
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 a bool. If it returns true, the connection to the Nepomuk service has been established and we can work with it. If it returns false, 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, 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, Variant>::const_iterator it = properties.constBegin();

    it != properties.constEnd(); ++it ) {
  QUrl propertyUri = it.key();
  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.