Logo Search packages:      
Sourcecode: kdegraphics-kde4 version File versions  Download package

bool Document::openDocument ( const QString &  docFile,
const KUrl &  url,
const KMimeType::Ptr &  mime 
)

Opens the document.

Definition at line 1072 of file document.cpp.

References Okular::AudioPlayer::d, Okular::DocumentObserver::DocumentChanged, error(), Okular::AudioPlayer::instance(), Okular::DocumentViewport::isValid(), notice(), Okular::DocumentViewport::pageNumber, Okular::Generator::ReadRawData, setViewport(), warning(), and widget().

{
    KMimeType::Ptr mime = _mime;
    QByteArray filedata;
    qint64 document_size = -1;
    bool isstdin = url.fileName( KUrl::ObeyTrailingSlash ) == QLatin1String( "-" );
    if ( !isstdin )
    {
        if ( mime.count() <= 0 )
            return false;

        // docFile is always local so we can use QFile on it
        QFile fileReadTest( docFile );
        if ( !fileReadTest.open( QIODevice::ReadOnly ) )
        {
            d->m_docFileName.clear();
            return false;
        }
        // determine the related "xml document-info" filename
        d->m_url = url;
        d->m_docFileName = docFile;
        if ( url.isLocalFile() )
        {
        QString fn = docFile.contains('/') ? docFile.section('/', -1, -1) : docFile;
        document_size = fileReadTest.size();
        fn = QString::number( document_size ) + '.' + fn + ".xml";
        fileReadTest.close();
        QString newokular = "okular/docdata/" + fn;
        QString newokularfile = KStandardDirs::locateLocal( "data", newokular );
        if ( !QFile::exists( newokularfile ) )
        {
            QString oldkpdf = "kpdf/" + fn;
            QString oldkpdffile = KStandardDirs::locateLocal( "data", oldkpdf );
            if ( QFile::exists( oldkpdffile ) )
            {
                // ### copy or move?
                if ( !QFile::copy( oldkpdffile, newokularfile ) )
                    return false;
            }
        }
        d->m_xmlFileName = newokularfile;
        }
    }
    else
    {
        QFile qstdin;
        qstdin.open( stdin, QIODevice::ReadOnly );
        filedata = qstdin.readAll();
        mime = KMimeType::findByContent( filedata );
        if ( !mime || mime->name() == QLatin1String( "application/octet-stream" ) )
            return false;
        document_size = filedata.size();
    }

    // 0. load Generator
    // request only valid non-disabled plugins suitable for the mimetype
    QString constraint("([X-KDE-Priority] > 0) and (exist Library)") ;
    KService::List offers = KMimeTypeTrader::self()->query(mime->name(),"okular/Generator",constraint);
    if (offers.isEmpty())
    {
        emit error( i18n( "Can not find a plugin which is able to handle the passed document." ), -1 );
        kWarning(OkularDebug).nospace() << "No plugin for mimetype '" << mime->name() << "'.";
        return false;
    }
    int hRank=0;
    // best ranked offer search
    int offercount = offers.count();
    if ( offercount > 1 )
    {
        // sort the offers: the offers with an higher priority come before
        qStableSort( offers.begin(), offers.end(), kserviceMoreThan );

        if ( Settings::chooseGenerators() )
        {
            QStringList list;
            for ( int i = 0; i < offercount; ++i )
            {
                list << offers.at(i)->name();
            }

            ChooseEngineDialog choose( list, mime, widget() );

            if ( choose.exec() == QDialog::Rejected )
                return false;

            hRank = choose.selectedGenerator();
        }
    }

    QString propName = offers.at(hRank)->name();
    QHash< QString, GeneratorInfo >::const_iterator genIt = d->m_loadedGenerators.constFind( propName );
    QString catalogName;
    if ( genIt != d->m_loadedGenerators.constEnd() )
    {
        d->m_generator = genIt.value().generator;
        catalogName = genIt.value().catalogName;
    }
    else
    {
        d->m_generator = d->loadGeneratorLibrary( offers.at(hRank) );
        if ( !d->m_generator )
            return false;
        genIt = d->m_loadedGenerators.constFind( propName );
        Q_ASSERT( genIt != d->m_loadedGenerators.constEnd() );
        catalogName = genIt.value().catalogName;
    }
    Q_ASSERT_X( d->m_generator, "Document::load()", "null generator?!" );

    if ( !catalogName.isEmpty() )
        KGlobal::locale()->insertCatalog( catalogName );

    d->m_generator->d_func()->m_document = d;

    // connect error reporting signals
    connect( d->m_generator, SIGNAL( error( const QString&, int ) ), this, SIGNAL( error( const QString&, int ) ) );
    connect( d->m_generator, SIGNAL( warning( const QString&, int ) ), this, SIGNAL( warning( const QString&, int ) ) );
    connect( d->m_generator, SIGNAL( notice( const QString&, int ) ), this, SIGNAL( notice( const QString&, int ) ) );

    // 1. load Document (and set busy cursor while loading)
    QApplication::setOverrideCursor( Qt::WaitCursor );
    bool openOk = false;
    if ( !isstdin )
    {
        openOk = d->m_generator->loadDocument( docFile, d->m_pagesVector );
    }
    else if ( !filedata.isEmpty() )
    {
        if ( d->m_generator->hasFeature( Generator::ReadRawData ) )
        {
            openOk = d->m_generator->loadDocumentFromData( filedata, d->m_pagesVector );
        }
        else
        {
            d->m_tempFile = new KTemporaryFile();
            if ( !d->m_tempFile->open() )
            {
                delete d->m_tempFile;
                d->m_tempFile = 0;
            }
            else
            {
                d->m_tempFile->write( filedata );
                QString tmpFileName = d->m_tempFile->fileName();
                d->m_tempFile->close();
                openOk = d->m_generator->loadDocument( tmpFileName, d->m_pagesVector );
            }
        }
    }

    QApplication::restoreOverrideCursor();
    if ( !openOk || d->m_pagesVector.size() <= 0 )
    {
        if ( !catalogName.isEmpty() )
            KGlobal::locale()->removeCatalog( catalogName );

        d->m_generator = 0;
        return openOk;
    }

    d->m_generatorName = propName;

    // 2. load Additional Data (our bookmarks and metadata) about the document
    d->loadDocumentInfo();
    d->m_bookmarkManager->setUrl( d->m_url );

    // 3. setup observers inernal lists and data
    foreachObserver( notifySetup( d->m_pagesVector, DocumentObserver::DocumentChanged ) );

    // 4. set initial page (restoring the page saved in xml if loaded)
    DocumentViewport loadedViewport = (*d->m_viewportIterator);
    if ( loadedViewport.isValid() )
    {
        (*d->m_viewportIterator) = DocumentViewport();
        if ( loadedViewport.pageNumber >= (int)d->m_pagesVector.size() )
            loadedViewport.pageNumber = d->m_pagesVector.size() - 1;
    }
    else
        loadedViewport.pageNumber = 0;
    setViewport( loadedViewport );

    // start bookmark saver timer
    if ( !d->m_saveBookmarksTimer )
    {
        d->m_saveBookmarksTimer = new QTimer( this );
        connect( d->m_saveBookmarksTimer, SIGNAL( timeout() ), this, SLOT( saveDocumentInfo() ) );
    }
    d->m_saveBookmarksTimer->start( 5 * 60 * 1000 );

    // start memory check timer
    if ( !d->m_memCheckTimer )
    {
        d->m_memCheckTimer = new QTimer( this );
        connect( d->m_memCheckTimer, SIGNAL( timeout() ), this, SLOT( slotTimedMemoryCheck() ) );
    }
    d->m_memCheckTimer->start( 2000 );

    if (d->m_nextDocumentViewport.isValid())
    {
        setViewport(d->m_nextDocumentViewport);
        d->m_nextDocumentViewport = DocumentViewport();
    }

    AudioPlayer::instance()->d->m_currentDocument = isstdin ? KUrl() : d->m_url;
    d->m_docSize = document_size;

    return true;
}


Generated by  Doxygen 1.6.0   Back to index