KDE TechBase
  • Page
  • Discussion
  • Edit
  • History
KDE TechBase is a Wiki - You can help! Please contribute! Questions?
Please ask development related questions in the KDE Community Forum.

Development/Tutorials/Metadata/Nepomuk/Quickstart

< Development | Tutorials | Metadata/Nepomuk
Revision as of 16:09, 7 November 2009 by Frederik.gladhorn (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

Languages: عربي | Asturianu | Català | Česky | Kaszëbsczi | Dansk | Deutsch | English | Esperanto | Español | فارسی | Suomi | Français | Galego | Italiano | 日本語 | 한국어 | Norwegian | Polski | Português Brasileiro | Română | Русский | Svenska | Slovenščina | српски | Українська | 简体中文 | 繁體中文

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

Contents

  • 1 Nepomuk Quickstart
    • 1.1 Initializing the Resource Manager
    • 1.2 Retrieving Metadata
    • 1.3 Setting Metadata
    • 1.4 Includes and Linking

[edit] 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.

[edit] 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.

[edit] 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() );
}

[edit] 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.

[edit] Includes and Linking

For the example you need the following includes:

#include <Nepomuk/ResourceManager>
#include <Nepomuk/Variant>
#include <Nepomuk/Types/Class>

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} )
Retrieved from "http://techbase.kde.org/Development/Tutorials/Metadata/Nepomuk/Quickstart"
Category: Tutorial

Navigation

  • Home
  • Help
  • Recent changes

Sections

  • Getting started
  • Development
  • Schedules
  • Policies
  • Contribute
  • Projects

Toolbox

  • What links here
  • Related changes
  • Special pages
  • Printable version
  • Permanent link

Personal tools

  • 38.107.191.99
  • Talk for this IP
  • Log in / create account
  • Login with OpenID
Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal