From 74420f1baac621f6822ed9c5212e6004711d695a Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 19 Feb 2013 00:22:16 +0100 Subject: [PATCH] Txt backend BUGS: 261930 REVIEW: 109009 FIXED-IN: 4.11.0 --- generators/CMakeLists.txt | 2 + generators/txt/CMakeLists.txt | 25 +++++++ .../txt/active-documentviewer_txt.desktop | 14 ++++ generators/txt/converter.cpp | 41 +++++++++++ generators/txt/converter.h | 31 +++++++++ generators/txt/document.cpp | 68 +++++++++++++++++++ generators/txt/document.h | 26 +++++++ generators/txt/generator_txt.cpp | 36 ++++++++++ generators/txt/generator_txt.h | 24 +++++++ generators/txt/libokularGenerator_txt.desktop | 10 +++ generators/txt/okularApplication_txt.desktop | 12 ++++ generators/txt/okularTxt.desktop | 7 ++ 12 files changed, 296 insertions(+) create mode 100644 generators/txt/CMakeLists.txt create mode 100644 generators/txt/active-documentviewer_txt.desktop create mode 100644 generators/txt/converter.cpp create mode 100644 generators/txt/converter.h create mode 100644 generators/txt/document.cpp create mode 100644 generators/txt/document.h create mode 100644 generators/txt/generator_txt.cpp create mode 100644 generators/txt/generator_txt.h create mode 100644 generators/txt/libokularGenerator_txt.desktop create mode 100644 generators/txt/okularApplication_txt.desktop create mode 100644 generators/txt/okularTxt.desktop diff --git a/generators/CMakeLists.txt b/generators/CMakeLists.txt index 5a101bf4c..531cc6e38 100644 --- a/generators/CMakeLists.txt +++ b/generators/CMakeLists.txt @@ -72,3 +72,5 @@ if(EPUB_FOUND) add_subdirectory(epub) endif(EPUB_FOUND) +add_subdirectory(txt) + diff --git a/generators/txt/CMakeLists.txt b/generators/txt/CMakeLists.txt new file mode 100644 index 000000000..be2b0002a --- /dev/null +++ b/generators/txt/CMakeLists.txt @@ -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} ) diff --git a/generators/txt/active-documentviewer_txt.desktop b/generators/txt/active-documentviewer_txt.desktop new file mode 100644 index 000000000..25b72a91f --- /dev/null +++ b/generators/txt/active-documentviewer_txt.desktop @@ -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 \ No newline at end of file diff --git a/generators/txt/converter.cpp b/generators/txt/converter.cpp new file mode 100644 index 000000000..1928abb91 --- /dev/null +++ b/generators/txt/converter.cpp @@ -0,0 +1,41 @@ +/*************************************************************************** + * Copyright (C) 2013 by Azat Khuzhin * + * * + * 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 + +#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; +} \ No newline at end of file diff --git a/generators/txt/converter.h b/generators/txt/converter.h new file mode 100644 index 000000000..98619f44e --- /dev/null +++ b/generators/txt/converter.h @@ -0,0 +1,31 @@ +/*************************************************************************** + * Copyright (C) 2013 by Azat Khuzhin * + * * + * 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 +#include + +namespace Txt +{ + class Converter : public Okular::TextDocumentConverter + { + public: + Converter(); + ~Converter(); + + virtual QTextDocument *convert( const QString &fileName ); + + private: + QTextDocument *m_textDocument; + }; +} + +#endif diff --git a/generators/txt/document.cpp b/generators/txt/document.cpp new file mode 100644 index 000000000..900d562dd --- /dev/null +++ b/generators/txt/document.cpp @@ -0,0 +1,68 @@ +/*************************************************************************** + * Copyright (C) 2013 by Azat Khuzhin * + * * + * 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 +#include +#include + +#include +#include + +#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 ); +} diff --git a/generators/txt/document.h b/generators/txt/document.h new file mode 100644 index 000000000..104f0ddf3 --- /dev/null +++ b/generators/txt/document.h @@ -0,0 +1,26 @@ +/*************************************************************************** + * Copyright (C) 2013 by Azat Khuzhin * + * * + * 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 + +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 ); + }; +} \ No newline at end of file diff --git a/generators/txt/generator_txt.cpp b/generators/txt/generator_txt.cpp new file mode 100644 index 000000000..93ca4aa3a --- /dev/null +++ b/generators/txt/generator_txt.cpp @@ -0,0 +1,36 @@ +/*************************************************************************** + * Copyright (C) 2013 by Azat Khuzhin * + * * + * 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 + +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 ) +{ +} \ No newline at end of file diff --git a/generators/txt/generator_txt.h b/generators/txt/generator_txt.h new file mode 100644 index 000000000..5c15ec42b --- /dev/null +++ b/generators/txt/generator_txt.h @@ -0,0 +1,24 @@ +/*************************************************************************** + * Copyright (C) 2013 by Azat Khuzhin * + * * + * 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 + +class TxtGenerator : public Okular::TextDocumentGenerator +{ + public: + TxtGenerator( QObject *parent, const QVariantList &args ); + ~TxtGenerator() {} +}; + +#endif \ No newline at end of file diff --git a/generators/txt/libokularGenerator_txt.desktop b/generators/txt/libokularGenerator_txt.desktop new file mode 100644 index 000000000..33c2f2920 --- /dev/null +++ b/generators/txt/libokularGenerator_txt.desktop @@ -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 \ No newline at end of file diff --git a/generators/txt/okularApplication_txt.desktop b/generators/txt/okularApplication_txt.desktop new file mode 100644 index 000000000..126f132f3 --- /dev/null +++ b/generators/txt/okularApplication_txt.desktop @@ -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 diff --git a/generators/txt/okularTxt.desktop b/generators/txt/okularTxt.desktop new file mode 100644 index 000000000..417be1522 --- /dev/null +++ b/generators/txt/okularTxt.desktop @@ -0,0 +1,7 @@ +[Desktop Entry] +Icon=okular +Name=Okular +X-KDE-ServiceTypes=KParts/ReadOnlyPart +X-KDE-Library=okularpart +Type=Service +MimeType=text/plain; \ No newline at end of file