Providing file meta-data support with KFile plugins | ||
|---|---|---|
| <<< Previous | Next >>> | |
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.
If you want to display a standard properties dialog, you can
use KPropertiesDialog, which will
produce a multi-tab widget as shown below.
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 );
|
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.
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.
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
}
|
There are a range of other things you can test or obtain -
refer to the API documentation for
KFileMetaInfo,
KFileMetaInfoGroup, and
KFileMetaInfoItem.
| <<< Previous | Home | Next >>> |
| Write support | Copyright and License |