Displaying and using meta-data in your application

Up until now, we've been working on how to write a plug-in that can extract the data for a file, and pass it over a standard interface for use by an application. This section introduces the other part of the problem - how to receive and display the data extracted by a plug-in.

There are two slightly different approaches that can be taken - you can either handle the data yourself, or just use a built-in KDE dialog. Which you use will, of course, depends on what you are trying to achieve with the meta-data. We'll look at both, so you can compare and contrast.

Using a built-in KDE dialog to display meta-data

If you want to display a standard properties dialog, you can use KPropertiesDialog, which will produce a multi-tab widget as shown below.

Figure 8. Standard KDE Properties Dialog, showing JPEG meta-data

The code required to do that is almost trivial, courtesy the functionality embedded in the KPropertiesDialog class. Basically you just pass a KURL, KFileItem or KFileItemList, whatever is most convenient for your application. If all you have is the file name, something like the following fragment will probably help:

Example 16. example showing how to display meta-data for /home/bradh/ENVREPOR.PDF

	  KURL url;
	  url.setPath( "/home/bradh/ENVREPOR.PDF" );
	  new KPropertiesDialog( url, this );
	

You can pass any QString to the setPath method - it doesn't have to be a constant. Also, if you don't need the KURL object for anything else, you can just use a simple constructor form, as

	  new KPropertiesDialog( KURL("/home/bradh/ENVREPOR.PDF"), this );
	

Handling meta-data yourself

If you want to do something different to what KPropertiesDialog provides, you can handle the meta-data yourself. This alternative is described in this section.

The first thing you need to do is to create a KfileMetaInfo object. You can construct this object using either a QString path, or with a KURL object. I suggest using a KURL, to ensure future network transparency:

	  KFileMetaInfo info = KFileMetaInfo( KURL("/home/bradh/ENVREPOR.PDF") );
	

You can then call a range of methods from the KFileMetaInfo object.

Extracting a specific data element

To extract a specific bit of meta-data from the file, we can query by key. For example, to extract the number of pages in a document (assuming that the meta-data plugin provides this information), we can use the item method, which returns a KFileMetaInfoItem object. You can then extract the value, which returns a QVariant. An example of this is shown below:

	    KFileMetaInfo info = KFileMetaInfo( KURL("/home/bradh/ENVREPOR.PDF") );
	    KFileMetaInfoItem pages = info.item("Pages");
	    QVariant numPages = pages.value();
	    cout << "The document has " << numPages.toInt() << " pages" << endl;
	  

The use of toInt is required to turn the QVariant into an integer value.

Testing if a data element is provided

If a data element can optionally be provided, then you may want to test if the data element is included. This can be done using the contains method, as shown below:

	    KFileMetaInfo info = KFileMetaInfo( KURL("/home/bradh/ENVREPOR.PDF") );
	    if ( info.contains( "Pages" ) ) {
	        KFileMetaInfoItem pages = info.item("Pages");
	        QVariant numPages = pages.value();
	        cout << "The document has " << numPages.toInt() << " pages" << endl;
	    } else {
	        // assume some default
	    }
	  

More information

There are a range of other things you can test or obtain - refer to the API documentation for KFileMetaInfo, KFileMetaInfoGroup, and KFileMetaInfoItem.