All pastes #401849 Raw Edit

Mine

public text v1 · immutable
·published 2007-03-19 15:28 UTC
rendered paste body
/* This file is part of the KDE project
*  Copyright (C) 2001, 2002 Rolf Magnus <ramagnus@kde.org>
 * Copyright (C) 2007 Matthias Lechner
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#define STRIGI_IMPORT_API
#include <strigi/streamthroughanalyzer.h>
#include <strigi/analyzerplugin.h>
#include <strigi/fieldtypes.h>
#include <strigi/analysisresult.h>
#include <strigi/cnstr.h>

#define UNSTABLE_POPPLER_QT4
#include <poppler-qt4.h>

#include <klocalizedstring.h>

using namespace Strigi;
using namespace std;

class PDFThroughAnalyzerFactory;
class PDFThroughAnalyzer : public StreamThroughAnalyzer {
    private:
        const PDFThroughAnalyzerFactory* factory;
        AnalysisResult* idx;

        void setIndexable( AnalysisResult *i ) {
            idx = i;
        }
        jstreams::InputStream* connectInputStream( jstreams::InputStream *in );
        bool isReadyWithStream() { return true; }

        Poppler::Document* m_pdfDocument;
    public:
        PDFThroughAnalyzer( const PDFThroughAnalyzerFactory* f ) : factory( f ) {}
};

class PDFThroughAnalyzerFactory : public StreamThroughAnalyzerFactory {
private:
    const char* getName() const {
        return "PDFThroughAnalyzer";
    }
    StreamThroughAnalyzer* newInstance() const {
        return new PDFThroughAnalyzer(this);
    }
    void registerFields( FieldRegister& );
    static const cnstr titleFieldName;
    static const cnstr subjectFieldName;
    static const cnstr authorFieldName;
    static const cnstr keywordsFieldName;
    static const cnstr creatorFieldName;
    static const cnstr producerFieldName;
    static const cnstr creationDateFieldName;
    static const cnstr modificationDateFieldName;
    static const cnstr pagesFieldName;
    static const cnstr protectedFieldName;
    static const cnstr linearizedFieldName;
    static const cnstr versionFieldName;
public:
    const RegisteredField* titleField;
    const RegisteredField* subjectField;
    const RegisteredField* authorField;
    const RegisteredField* keywordsField;
    const RegisteredField* creatorField;
    const RegisteredField* producerField;
    const RegisteredField* creationDateField;
    const RegisteredField* modificationDateField;
    const RegisteredField* pagesField;
    const RegisteredField* protectedField;
    const RegisteredField* linearizedField;
    const RegisteredField* versionField;
};

const cnstr PDFThroughAnalyzerFactory::titleFieldName( "title" );
const cnstr PDFThroughAnalyzerFactory::subjectFieldName( "subject" );
const cnstr PDFThroughAnalyzerFactory::authorFieldName( "author" );
const cnstr PDFThroughAnalyzerFactory::keywordsFieldName( "keywords" );
const cnstr PDFThroughAnalyzerFactory::creatorFieldName( "creator" );
const cnstr PDFThroughAnalyzerFactory::producerFieldName( "producer" );
const cnstr PDFThroughAnalyzerFactory::creationDateFieldName( "creationdate" );
const cnstr PDFThroughAnalyzerFactory::modificationDateFieldName( "modificationdate" );
const cnstr PDFThroughAnalyzerFactory::pagesFieldName( "pages" );
const cnstr PDFThroughAnalyzerFactory::protectedFieldName( "protected" );
const cnstr PDFThroughAnalyzerFactory::linearizedFieldName( "linearized" );
const cnstr PDFThroughAnalyzerFactory::versionFieldName( "version" );

void PDFThroughAnalyzerFactory::registerFields( FieldRegister& reg ) {
    titleField = reg.registerField( titleFieldName, FieldRegister::stringType, 1, 0 );
    subjectField = reg.registerField( subjectFieldName, FieldRegister::stringType, 1, 0 );
    authorField = reg.registerField( authorFieldName, FieldRegister::stringType, 1, 0 );
    keywordsField = reg.registerField( keywordsFieldName, FieldRegister::stringType, 1, 0 );
    creatorField = reg.registerField( creatorFieldName, FieldRegister::stringType, 1, 0 );
    producerField = reg.registerField( producerFieldName, FieldRegister::stringType, 1, 0 );
    creationDateField = reg.registerField( creationDateFieldName, FieldRegister::integerType, 1, 0 );
    modificationDateField = reg.registerField( modificationDateFieldName, FieldRegister::integerType, 1, 0 );
    pagesField = reg.registerField( pagesFieldName, FieldRegister::integerType, 1, 0 );
    protectedField = reg.registerField( protectedFieldName, FieldRegister::stringType, 1, 0 );
    linearizedField = reg.registerField( linearizedFieldName, FieldRegister::stringType, 1, 0 );
    versionField = reg.registerField( versionFieldName, FieldRegister::stringType, 1, 0 );
}

jstreams::InputStream* PDFThroughAnalyzer::connectInputStream( jstreams::InputStream* in ) {
    m_pdfDocument = Poppler::Document::load( QString( idx->path().c_str() ) );
    if( !m_pdfDocument || m_pdfDocument->isLocked() ) {
        delete m_pdfDocument;
        return in;
    }

    idx->setField( factory->titleField, (const char*) m_pdfDocument->info( "Title" ).toUtf8() );
    idx->setField( factory->subjectField, (const char*) m_pdfDocument->info( "Subject" ).toUtf8() );
    idx->setField( factory->authorField, (const char*) m_pdfDocument->info( "Author" ).toUtf8() );
    idx->setField( factory->keywordsField, (const char*) m_pdfDocument->info( "Keywords" ).toUtf8() );
    idx->setField( factory->creatorField, (const char*) m_pdfDocument->info( "Creator" ).toUtf8() );
    idx->setField( factory->producerField, (const char*) m_pdfDocument->info( "Producer" ).toUtf8() );

    idx->setField( factory->creationDateField,
                   (const char*) m_pdfDocument->date( "CreationDate" ).toString().toUtf8() );
    idx->setField( factory->modificationDateField,
                   (const char*) m_pdfDocument->date( "ModDate" ).toString().toUtf8() );

    idx->setField( factory->pagesField, m_pdfDocument->numPages() );

    QString enc;
    if ( m_pdfDocument->isEncrypted() ) {
        enc = i18n( "Yes (Can Print:%1 Can Copy:%2 Can Change:%3 Can Add notes:%4)",
        m_pdfDocument->okToPrint() ? i18n("Yes") : i18n("No"),
        m_pdfDocument->okToCopy() ? i18n("Yes") : i18n("No"),
        m_pdfDocument->okToChange() ? i18n("Yes") : i18n("No"),
        m_pdfDocument->okToAddNotes() ? i18n("Yes") : i18n("No"));
    } else
        enc = i18n( "No" );

    idx->setField( factory->protectedField, (const char*) enc.toUtf8() );

    QString linearized;
    if( m_pdfDocument->isLinearized() )
        linearized = i18n( "Yes" );
    else
        linearized = i18n( "No" );

    idx->setField( factory->linearizedField, (const char*) linearized.toUtf8() );

    QString versionString = QString("%1").arg( m_pdfDocument->pdfVersion(), 0, 'f', 1 );
    idx->setField( factory->versionField, (const char*) versionString.toUtf8() );
    return in;
}

class Factory : public AnalyzerFactoryFactory {
public:
    list<StreamThroughAnalyzerFactory*>
    getStreamThroughAnalyzerFactories() const {
        list<StreamThroughAnalyzerFactory*> af;
        af.push_back(new PDFThroughAnalyzerFactory());
        return af;
    }
};

STRIGI_ANALYZER_FACTORY(Factory)