Txt backend

BUGS: 261930
REVIEW: 109009
FIXED-IN: 4.11.0
This commit is contained in:
Azat Khuzhin 2013-02-19 00:22:16 +01:00 committed by Albert Astals Cid
parent 7bdf8d1f05
commit 74420f1baa
12 changed files with 296 additions and 0 deletions

View file

@ -72,3 +72,5 @@ if(EPUB_FOUND)
add_subdirectory(epub)
endif(EPUB_FOUND)
add_subdirectory(txt)

View file

@ -0,0 +1,25 @@
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/../..
${CMAKE_BINARY_DIR}/okular
)
########### next target ###############
set(okularGenerator_txt_SRCS
generator_txt.cpp
converter.cpp
document.cpp
)
kde4_add_plugin(okularGenerator_txt ${okularGenerator_txt_SRCS})
target_link_libraries(okularGenerator_txt okularcore ${KDE4_KDEUI_LIBS} ${QT_QTXML_LIBRARY})
install(TARGETS okularGenerator_txt DESTINATION ${PLUGIN_INSTALL_DIR})
########### install files ###############
install( FILES libokularGenerator_txt.desktop okularTxt.desktop DESTINATION ${SERVICES_INSTALL_DIR} )
install( PROGRAMS okularApplication_txt.desktop active-documentviewer_txt.desktop DESTINATION ${XDG_APPS_INSTALL_DIR} )

View file

@ -0,0 +1,14 @@
[Desktop Entry]
MimeType=text/plain;
Name=Reader
GenericName=Document viewer
Comment=Viewer for various types of documents
Exec=active-documentviewer %u
Terminal=false
Icon=okular
Type=Application
Categories=Qt;KDE;Graphics;Office;Viewer;
InitialPreference=2
NoDisplay=true
X-KDE-Keywords=txt

View file

@ -0,0 +1,41 @@
/***************************************************************************
* Copyright (C) 2013 by Azat Khuzhin <a3at.mail@gmail.com> *
* *
* 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; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include <QtGui/QTextFrame>
#include "converter.h"
#include "document.h"
using namespace Txt;
Converter::Converter() : m_textDocument(NULL)
{
}
Converter::~Converter()
{
}
QTextDocument* Converter::convert( const QString &fileName )
{
m_textDocument = new Document( fileName );
m_textDocument->setPageSize(QSizeF( 600, 800 ));
QTextFrameFormat frameFormat;
frameFormat.setMargin( 20 );
QTextFrame *rootFrame = m_textDocument->rootFrame();
rootFrame->setFrameFormat( frameFormat );
emit addMetaData( Okular::DocumentInfo::MimeType, "text/plain" );
return m_textDocument;
}

View file

@ -0,0 +1,31 @@
/***************************************************************************
* Copyright (C) 2013 by Azat Khuzhin <a3at.mail@gmail.com> *
* *
* 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; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#ifndef TXT_CONVERTER_H
#define TXT_CONVERTER_H
#include <core/textdocumentgenerator.h>
#include <core/document.h>
namespace Txt
{
class Converter : public Okular::TextDocumentConverter
{
public:
Converter();
~Converter();
virtual QTextDocument *convert( const QString &fileName );
private:
QTextDocument *m_textDocument;
};
}
#endif

View file

@ -0,0 +1,68 @@
/***************************************************************************
* Copyright (C) 2013 by Azat Khuzhin <a3at.mail@gmail.com> *
* *
* 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; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include <QFile>
#include <QDataStream>
#include <QTextCodec>
#include <kencodingprober.h>
#include <kdebug.h>
#include "document.h"
using namespace Txt;
Document::Document( QString fileName )
{
#ifdef TXT_DEBUG
kDebug() << "Opening file" << fileName;
#endif
QFile plainFile( fileName );
if ( !plainFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
kDebug() << "Can't open file" << plainFile.fileName();
return;
}
QByteArray buffer = plainFile.readAll();
setPlainText( toUnicode(buffer) );
}
Document::~Document()
{
}
QByteArray Document::detectEncoding( const QByteArray &array )
{
// TODO: see to "katetextloader.h"
KEncodingProber prober(KEncodingProber::Universal);
prober.feed(array);
if (!prober.confidence() > 0.5)
{
kDebug() << "Can't detect charset";
return QByteArray();
}
#ifdef TXT_DEBUG
kDebug() << "Detected" << prober.encoding() << "encoding";
#endif
return prober.encoding();
}
QString Document::toUnicode( const QByteArray &array )
{
QByteArray encoding = detectEncoding( array );
if ( encoding.isEmpty() )
{
return QString();
}
return QTextCodec::codecForName( encoding )->toUnicode( array );
}

26
generators/txt/document.h Normal file
View file

@ -0,0 +1,26 @@
/***************************************************************************
* Copyright (C) 2013 by Azat Khuzhin <a3at.mail@gmail.com> *
* *
* 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; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include <QtGui/QTextDocument>
namespace Txt
{
class Document : public QTextDocument
{
public:
Document( QString fileName );
~Document();
private:
// TODO: write a better detecter, based on some number of chunks
QByteArray detectEncoding( const QByteArray &array );
QString toUnicode( const QByteArray &array );
};
}

View file

@ -0,0 +1,36 @@
/***************************************************************************
* Copyright (C) 2013 by Azat Khuzhin <a3at.mail@gmail.com> *
* *
* 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; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "generator_txt.h"
#include "converter.h"
#include <kaboutdata.h>
static KAboutData createAboutData()
{
KAboutData aboutData(
"okular_txt",
"okular_txt",
ki18n( "Txt Backend" ),
"0.1",
ki18n( "Txt backend." ),
KAboutData::License_GPL,
ki18n( "© 2013 Azat Khuzhin" )
);
aboutData.addAuthor( ki18n( "Azat Khuzhin" ), KLocalizedString(), "a3at.mail@gmail.com" );
return aboutData;
}
OKULAR_EXPORT_PLUGIN( TxtGenerator, createAboutData() )
TxtGenerator::TxtGenerator( QObject *parent, const QVariantList &args )
: Okular::TextDocumentGenerator( new Txt::Converter, parent, args )
{
}

View file

@ -0,0 +1,24 @@
/***************************************************************************
* Copyright (C) 2013 by Azat Khuzhin <a3at.mail@gmail.com> *
* *
* 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; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#ifndef _TXT_GENERATOR_H_
#define _TXT_GENERATOR_H_
#include <core/textdocumentgenerator.h>
class TxtGenerator : public Okular::TextDocumentGenerator
{
public:
TxtGenerator( QObject *parent, const QVariantList &args );
~TxtGenerator() {}
};
#endif

View file

@ -0,0 +1,10 @@
[Desktop Entry]
Type=Service
Name=txt
Comment=Txt backend for Okular
X-KDE-ServiceTypes=okular/Generator
MimeType=text/plain;
X-KDE-Library=okularGenerator_txt
X-KDE-Priority=2
X-KDE-okularAPIVersion=1
X-KDE-okularHasInternalSettings=false

View file

@ -0,0 +1,12 @@
[Desktop Entry]
MimeType=text/plain;
Terminal=false
Name=Okular
GenericName=Document Viewer
Exec=okular %U %i -caption "%c"
Icon=okular
Type=Application
InitialPreference=3
Categories=Qt;KDE;Graphics;Viewer;
NoDisplay=true
X-KDE-Keywords=txt

View file

@ -0,0 +1,7 @@
[Desktop Entry]
Icon=okular
Name=Okular
X-KDE-ServiceTypes=KParts/ReadOnlyPart
X-KDE-Library=okularpart
Type=Service
MimeType=text/plain;