Home · All Classes · Grouped Classes · Annotated · Functions

main.cpp Example File
content/filtering/main.cpp

    /****************************************************************************
    **
    ** Copyright (C) 2000-2007 TROLLTECH ASA. All rights reserved.
    **
    ** This file is part of the Opensource Edition of the Qtopia Toolkit.
    **
    ** $TROLLTECH_DUAL_LICENSE$
    **
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    **
    ****************************************************************************/

    #include <QtopiaApplication>
    #include <QContent>
    #include <QContentSet>
    #include <QContentSetModel>
    #include <QContentFilter>
    #include <QFile>
    #include <QByteArray>
    #include <QIODevice>
    #include <QDebug>

    // A function that will loop through the items in the given content set.
    void querySet(const QContentSet &);

    // This is a command line example to demonstrate database access and filtering.
    // The application shows you how to (i) look at a filtered subset of Qtopia's documents database,
    // (ii) add a new document to that database.
    int main(int argc,char **argv)
    {
        QtopiaApplication app(argc,argv);

        // Request all documents of mimetype text.
        QContentSet contentSet(QContentFilter::MimeType, "text/*");
        qDebug() << "Number of text documents: " << contentSet.count();
        querySet(contentSet);

        // Now request all JPEGS.
        contentSet.setCriteria(QContentFilter(QContentFilter::MimeType,"image/jpeg"));
        qDebug() << "Number of JPEGS: " << contentSet.count();
        querySet(contentSet);

        // We have a local file, "demo.jpg". We'd like to store the contents of that file in the
        // database.
        QFile file("demo.jpg");
        if ( !file.open(QIODevice::ReadOnly) ) {
            qWarning() << "Could not open demo.jpg!!";
            return 1;
        }
        QByteArray bytes = file.readAll();
        file.close();

        QContent newContent;
        newContent.setName("MyDemo");
        newContent.setType("image/jpeg");
        qDebug() << "New content object " << newContent.name() << " created, whose type is " << newContent.type();
        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();
        if ( newContent.commit() ) {
            qDebug() << "Writing the new content was successful";
        } else {
            qDebug() << "Writing the new content failed";
        }
        // Important!! It is the user's reponsibility to delete the return value of QContent::open().
        delete ioDevice;

        // Now we'll have another look at the content set, to see whether our new content has been added.
        querySet(contentSet);

        return 0;
    }

    // Traverses the list of documents, and writes out some information on each of them, to console.
    void querySet(const QContentSet &contentSet)
    {
        // There are a number of ways of doing this. In general, you should wrap the content
        // set in a model, and iterate through that.
        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!!";
            }
        }
    }


Copyright © 2007 Trolltech Trademarks
Qtopia 4.3.0