mirror of
https://invent.kde.org/graphics/okular
synced 2024-10-12 10:53:09 +00:00
Txt backend
BUGS: 261930 REVIEW: 109009 FIXED-IN: 4.11.0
This commit is contained in:
parent
7bdf8d1f05
commit
74420f1baa
|
@ -72,3 +72,5 @@ if(EPUB_FOUND)
|
|||
add_subdirectory(epub)
|
||||
endif(EPUB_FOUND)
|
||||
|
||||
add_subdirectory(txt)
|
||||
|
||||
|
|
25
generators/txt/CMakeLists.txt
Normal file
25
generators/txt/CMakeLists.txt
Normal 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} )
|
14
generators/txt/active-documentviewer_txt.desktop
Normal file
14
generators/txt/active-documentviewer_txt.desktop
Normal 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
|
41
generators/txt/converter.cpp
Normal file
41
generators/txt/converter.cpp
Normal 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;
|
||||
}
|
31
generators/txt/converter.h
Normal file
31
generators/txt/converter.h
Normal 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
|
68
generators/txt/document.cpp
Normal file
68
generators/txt/document.cpp
Normal 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
26
generators/txt/document.h
Normal 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 );
|
||||
};
|
||||
}
|
36
generators/txt/generator_txt.cpp
Normal file
36
generators/txt/generator_txt.cpp
Normal 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 )
|
||||
{
|
||||
}
|
24
generators/txt/generator_txt.h
Normal file
24
generators/txt/generator_txt.h
Normal 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
|
10
generators/txt/libokularGenerator_txt.desktop
Normal file
10
generators/txt/libokularGenerator_txt.desktop
Normal 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
|
12
generators/txt/okularApplication_txt.desktop
Normal file
12
generators/txt/okularApplication_txt.desktop
Normal 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
|
7
generators/txt/okularTxt.desktop
Normal file
7
generators/txt/okularTxt.desktop
Normal 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;
|
Loading…
Reference in a new issue