| Home · All Classes · Grouped Classes · Annotated · Functions |
Files:
This example demonstrates how to gain access to documents that already reside in Qtopia's document database, and how to add a completely new document to the database. The example code can be compiled and run in conjunction with the image file demo.jpg, which resides within the examples/content/filtering directory. The example is best demonstrated with some existing text and JPEG documents already in the database.
It is often desirable to create a subset of existing documents based, for example, on mime type. The following code fragments create subsets, first for a text documents, and then for JPEGs.
QContentSet contentSet(QContentFilter::MimeType, "text/*");
...
contentSet.setCriteria(QContentFilter(QContentFilter::MimeType,"image/jpeg"));
...
Filters can also be created for other criteria. For more information, see the documentation for the class QContentFilter.
Once a subset of documents has been created, we then iterate through the list and extract information about each QContent document object. There are a number of ways that this can be achieved, but in general it is advisable to wrap the QContentSet list of documents in a QContentSetModel object.
QContentSetModel model(&contentSet);
int rowCount = model.rowCount();
for ( int idx = 0; idx < rowCount; idx++ ) {
QContent content = model.content(idx);
if ( content.isValid() ) {
// You can query the content object for information. See the QContent class
// documentation for other public querying methods.
qDebug() << " Visible name: " << content.name() << ", Type: " << content.type() << ", Size: " << content.size() << " bytes";
} else {
qDebug() << " Invalid document!!";
}
}
You may also wish to add an entirely new document to the database. For example, you may be writing an application that allows the user to compose melodies or to draw pictures. The new data needs to be persisted, and in both these cases the documents database can be used.
The given example uses an existing local file and reads its contents into memory.
QFile file("demo.jpg");
if ( !file.open(QIODevice::ReadOnly) ) {
qWarning() << "Could not open demo.jpg!!";
return 1;
}
QByteArray bytes = file.readAll();
file.close();
Now a new QContent object can be created, ready to store the contents of the JPEG file. We also need to set some attributes on it, such as the new document's name and its MIME type.
QContent newContent;
newContent.setName("MyDemo");
newContent.setType("image/jpeg");
The new QContent object can now be opened so that the data obtained from the local file can be copied into it.
QIODevice *ioDevice = newContent.open(QIODevice::WriteOnly);
if ( !ioDevice ) {
qWarning("Could not open the new content object to write to!!");
return 1;
}
qint64 rc = ioDevice->write(bytes);
qDebug() << "Number of bytes written to new content, MyDemo: " << rc;
ioDevice->close();
Now the new QContent object is ready to be persisted to the database. This can be achieved via the QContent::commit() method.
if ( newContent.commit() ) {
qDebug() << "Writing the new content was successful";
} else {
qDebug() << "Writing the new content failed";
}
It is also the responsibility of the calling code to clean up the dynamic memory that was created within QContent::open(). Failing to do so will result in a memory leak.
delete ioDevice;
The example should be compiled and run within its own directory, so that the code can access the JPEG file.
cd examples\content\filtering
make
./filtering
The application should write the following output to the console:
If the user now starts or restarts Qtopia and opens either the Pictures application or the Documents application, the image MyDemo should now be available.
| Copyright © 2007 Trolltech | Trademarks | Qtopia 4.3.0 |