diff --git a/apps/reader/Makefile b/apps/reader/Makefile index 0d5267e5f..71adec9e0 100644 --- a/apps/reader/Makefile +++ b/apps/reader/Makefile @@ -5,6 +5,8 @@ app_sreader_src = $(addprefix apps/reader/,\ app.cpp \ list_book_controller.cpp \ utility.cpp \ + read_book_controller \ + word_wrap_view.cpp \ ) apps_src += $(app_sreader_src) diff --git a/apps/reader/app.cpp b/apps/reader/app.cpp index 68ca41115..18ae33da0 100644 --- a/apps/reader/app.cpp +++ b/apps/reader/app.cpp @@ -30,8 +30,9 @@ App::Descriptor * App::Snapshot::descriptor() { App::App(Snapshot * snapshot) : - ::App(snapshot, &m_listBookController), - m_listBookController(nullptr) + ::App(snapshot, &m_stackViewController), + m_listBookController(&m_stackViewController), + m_stackViewController(nullptr, &m_listBookController) { } diff --git a/apps/reader/app.h b/apps/reader/app.h index d2f4fbc1d..1204862c4 100644 --- a/apps/reader/app.h +++ b/apps/reader/app.h @@ -22,6 +22,7 @@ public: private: App(Snapshot * snapshot); ListBookController m_listBookController; + StackViewController m_stackViewController; }; } diff --git a/apps/reader/base.de.i18n b/apps/reader/base.de.i18n index ecc03a749..68a64986d 100644 --- a/apps/reader/base.de.i18n +++ b/apps/reader/base.de.i18n @@ -1,2 +1,3 @@ ReaderApp = "Reader" -ReaderAppCapital = "READER" \ No newline at end of file +ReaderAppCapital = "READER" +NoFileToDisplay = "Keine Dateien zum Anzeigen!" \ No newline at end of file diff --git a/apps/reader/base.en.i18n b/apps/reader/base.en.i18n index ecc03a749..0101ebe2a 100644 --- a/apps/reader/base.en.i18n +++ b/apps/reader/base.en.i18n @@ -1,2 +1,3 @@ ReaderApp = "Reader" -ReaderAppCapital = "READER" \ No newline at end of file +ReaderAppCapital = "READER" +NoFileToDisplay = "No file to display!" \ No newline at end of file diff --git a/apps/reader/base.es.i18n b/apps/reader/base.es.i18n index ecc03a749..51be1eaf4 100644 --- a/apps/reader/base.es.i18n +++ b/apps/reader/base.es.i18n @@ -1,2 +1,3 @@ ReaderApp = "Reader" -ReaderAppCapital = "READER" \ No newline at end of file +ReaderAppCapital = "READER" +NoFileToDisplay ="No hay archivos para mostrar!" \ No newline at end of file diff --git a/apps/reader/base.fr.i18n b/apps/reader/base.fr.i18n index ecc03a749..220c36fa1 100644 --- a/apps/reader/base.fr.i18n +++ b/apps/reader/base.fr.i18n @@ -1,2 +1,3 @@ ReaderApp = "Reader" -ReaderAppCapital = "READER" \ No newline at end of file +ReaderAppCapital = "READER" +NoFileToDisplay = "Aucun fichier à afficher!" \ No newline at end of file diff --git a/apps/reader/base.hu.i18n b/apps/reader/base.hu.i18n index ecc03a749..264d6e878 100644 --- a/apps/reader/base.hu.i18n +++ b/apps/reader/base.hu.i18n @@ -1,2 +1,3 @@ ReaderApp = "Reader" -ReaderAppCapital = "READER" \ No newline at end of file +ReaderAppCapital = "READER" +NoFileToDisplay ="Nincs megjeleníthető fájl" \ No newline at end of file diff --git a/apps/reader/base.it.i18n b/apps/reader/base.it.i18n index ecc03a749..4464ff077 100644 --- a/apps/reader/base.it.i18n +++ b/apps/reader/base.it.i18n @@ -1,2 +1,3 @@ ReaderApp = "Reader" -ReaderAppCapital = "READER" \ No newline at end of file +ReaderAppCapital = "READER" +NoFileToDisplay ="essun file da visualizzare" \ No newline at end of file diff --git a/apps/reader/base.nl.i18n b/apps/reader/base.nl.i18n index ecc03a749..55693de3e 100644 --- a/apps/reader/base.nl.i18n +++ b/apps/reader/base.nl.i18n @@ -1,2 +1,3 @@ ReaderApp = "Reader" -ReaderAppCapital = "READER" \ No newline at end of file +ReaderAppCapital = "READER" +NoFileToDisplay ="Geen bestanden om weer te geven" \ No newline at end of file diff --git a/apps/reader/base.pt.i18n b/apps/reader/base.pt.i18n index ecc03a749..886d4930c 100644 --- a/apps/reader/base.pt.i18n +++ b/apps/reader/base.pt.i18n @@ -1,2 +1,3 @@ ReaderApp = "Reader" -ReaderAppCapital = "READER" \ No newline at end of file +ReaderAppCapital = "READER" +NoFileToDisplay ="Nenhum arquivo para exibir" \ No newline at end of file diff --git a/apps/reader/list_book_controller.cpp b/apps/reader/list_book_controller.cpp index bbc327f16..a963ad0ba 100644 --- a/apps/reader/list_book_controller.cpp +++ b/apps/reader/list_book_controller.cpp @@ -1,5 +1,7 @@ #include "list_book_controller.h" #include "utility.h" +#include "apps/i18n.h" + namespace reader { @@ -10,7 +12,8 @@ View* ListBookController::view() ListBookController::ListBookController(Responder * parentResponder): ViewController(parentResponder), - m_tableView(this, this) + m_tableView(this, this, this), + m_readBookController(this) { m_nbFiles = filesWithExtension(".txt", m_files, NB_FILES); } @@ -43,4 +46,30 @@ void ListBookController::willDisplayCellForIndex(HighlightCell * cell, int index myTextCell->setMessageFont(KDFont::LargeFont); } +void ListBookController::didBecomeFirstResponder() +{ + if (selectedRow() < 0) { + selectCellAtLocation(0, 0); + } + Container::activeApp()->setFirstResponder(&m_tableView); + if(m_nbFiles == 0) + { + Container::activeApp()->displayWarning(I18n::Message::NoFileToDisplay); + } +} + +bool ListBookController::handleEvent(Ion::Events::Event event) +{ + if (event == Ion::Events::OK || event == Ion::Events::EXE || event == Ion::Events::Right) + { + + m_readBookController.setBook(m_files[selectedRow()]); + static_cast(parentResponder())->push(&m_readBookController); + Container::activeApp()->setFirstResponder(&m_readBookController); + return true; + } + + return false; +} + } \ No newline at end of file diff --git a/apps/reader/list_book_controller.h b/apps/reader/list_book_controller.h index 4c5050c98..9fdd11262 100644 --- a/apps/reader/list_book_controller.h +++ b/apps/reader/list_book_controller.h @@ -4,10 +4,12 @@ #include #include +#include "read_book_controller.h" + namespace reader { -class ListBookController : public ViewController, public SimpleListViewDataSource, public ScrollViewDataSource +class ListBookController : public ViewController, public SimpleListViewDataSource, public SelectableTableViewDataSource { public: ListBookController(Responder * parentResponder); @@ -18,8 +20,10 @@ public: HighlightCell * reusableCell(int index) override; int reusableCellCount() const override; void willDisplayCellForIndex(HighlightCell * cell, int index) override; + void didBecomeFirstResponder() override; + bool handleEvent(Ion::Events::Event event) override; private: - TableView m_tableView; + SelectableTableView m_tableView; static const int NB_FILES = 20; External::Archive::File m_files[NB_FILES]; @@ -27,6 +31,8 @@ private: static const int NB_CELLS = 6; MessageTableCell m_cells[NB_CELLS]; + + ReadBookController m_readBookController; }; } diff --git a/apps/reader/normalize.py b/apps/reader/normalize.py new file mode 100644 index 000000000..fff638b33 --- /dev/null +++ b/apps/reader/normalize.py @@ -0,0 +1,18 @@ +import sys +import unicodedata +import argparse +import io +import shutil + +filename = sys.argv[1] + +print("Normalization of "+filename) + +output = open(filename+".tmp", "wb") + +with io.open(filename, "r", encoding='utf-8') as file: + for line in file: + unicodeLine = unicodedata.normalize("NFKD", line) + output.write(unicodeLine.encode("UTF-8")) +output.close() +shutil.move(filename+".tmp",filename) diff --git a/apps/reader/read_book_controller.cpp b/apps/reader/read_book_controller.cpp new file mode 100644 index 000000000..75288029f --- /dev/null +++ b/apps/reader/read_book_controller.cpp @@ -0,0 +1,21 @@ +#include "read_book_controller.h" + +namespace reader +{ + +ReadBookController::ReadBookController(Responder * parentResponder) : + ViewController(parentResponder) +{ +} + +View * ReadBookController::view() +{ + return &m_readerView; +} + +void ReadBookController::setBook(const External::Archive::File& file) +{ + m_readerView.setText(reinterpret_cast(file.data)); +} + +} \ No newline at end of file diff --git a/apps/reader/read_book_controller.h b/apps/reader/read_book_controller.h new file mode 100644 index 000000000..3c734f1a4 --- /dev/null +++ b/apps/reader/read_book_controller.h @@ -0,0 +1,23 @@ +#ifndef _READ_BOOK_CONTROLLER_H_ +#define _READ_BOOK_CONTROLLER_H_ + +#include +#include "apps/external/archive.h" +#include "word_wrap_view.h" + +namespace reader { + +class ReadBookController : public ViewController { +public: + ReadBookController(Responder * parentResponder); + View * view() override; + + void setBook(const External::Archive::File& file); + +private: + WordWrapTextView m_readerView; +}; + +} + +#endif \ No newline at end of file diff --git a/apps/reader/utility.cpp b/apps/reader/utility.cpp index 7a9779909..2f82b61f3 100644 --- a/apps/reader/utility.cpp +++ b/apps/reader/utility.cpp @@ -31,6 +31,19 @@ bool stringEndsWith(const char* str, const char* pattern) return false; } +void stringNCopy(char* dest, int max, const char* src, int len) +{ + while(len>0 && max >1 && *src) + { + *dest = *src; + dest++; + src++; + len--; + max--; + } + *dest=0; +} + #ifdef DEVICE @@ -54,6 +67,34 @@ int filesWithExtension(const char* extension, External::Archive::File* files, in } #else +static void fillFileData(External::Archive::File& file) +{ + file.data = nullptr; + file.dataLength = 0; + + struct stat info; + if (stat(file.name, &info) != 0) + { + return; + } + + unsigned char* content = new unsigned char[info.st_size]; + if (content == NULL) + { + return ; + } + FILE *fp = fopen(file.name, "rb"); + if (fp == NULL) + { + return ; + } + + fread(content, info.st_size, 1, fp); + fclose(fp); + file.data = content; + file.dataLength = info.st_size; +} + int filesWithExtension(const char* extension, External::Archive::File* files, int filesSize) { dirent *file; @@ -63,9 +104,10 @@ int filesWithExtension(const char* extension, External::Archive::File* files, in { while ((file = readdir(d)) != NULL) { - if(stringEndsWith(dir->d_name, extension)) + if(stringEndsWith(file->d_name, extension)) { files[nb].name = strdup(file->d_name);//will probably leak + fillFileData(files[nb]); nb++; if(nb == filesSize) break; @@ -75,7 +117,6 @@ int filesWithExtension(const char* extension, External::Archive::File* files, in } return nb; } - #endif } \ No newline at end of file diff --git a/apps/reader/utility.h b/apps/reader/utility.h index 9cd455008..6893ab95c 100644 --- a/apps/reader/utility.h +++ b/apps/reader/utility.h @@ -8,6 +8,7 @@ namespace reader bool stringEndsWith(const char* str, const char* end); int filesWithExtension(const char* extension, External::Archive::File* files, int filesSize) ; +void stringNCopy(char* dest, int max, const char* src, int len); } #endif \ No newline at end of file diff --git a/apps/reader/word_wrap_view.cpp b/apps/reader/word_wrap_view.cpp new file mode 100644 index 000000000..f0f673a1a --- /dev/null +++ b/apps/reader/word_wrap_view.cpp @@ -0,0 +1,68 @@ +#include "word_wrap_view.h" + +#include "utility.h" + +#include + +namespace reader +{ +void WordWrapTextView::drawRect(KDContext * ctx, KDRect rect) const +{ + ctx->fillRect(KDRect(0, 0, bounds().width(), bounds().height()), m_backgroundColor); + + + const char * current = text(); + const char * startOfWord = current; + const char * endOfWord = UTF8Helper::EndOfWord(startOfWord); + KDPoint textPosition(0, 0); + + const int wordMaxLength = 128; + char word[wordMaxLength]; + + const int spaceWidth = m_font->stringSize(" ").width(); + + while(*startOfWord != 0) + { + + KDSize textSize = m_font->stringSizeUntil(startOfWord, endOfWord); + KDPoint nextTextPosition = KDPoint(textPosition.x()+textSize.width(), textPosition.y()); + + if(nextTextPosition.x() > m_frame.width()) + { + textPosition = KDPoint(0, textPosition.y() + textSize.height()); + nextTextPosition = KDPoint(textSize.width(), textPosition.y()); + } + + stringNCopy(word, wordMaxLength, startOfWord, endOfWord-startOfWord); + ctx->drawString(word, textPosition, m_font, m_textColor, m_backgroundColor); + + while(*endOfWord == ' ') + { + nextTextPosition = KDPoint(nextTextPosition.x() + spaceWidth, nextTextPosition.y()); + ++endOfWord; + } + if(nextTextPosition.x() > m_frame.width()) + { + nextTextPosition = KDPoint(0, nextTextPosition.y() + textSize.height()); + } + + while(*endOfWord == '\n') + { + nextTextPosition = KDPoint(0, nextTextPosition.y() + textSize.height()); + ++endOfWord; + } + + if(nextTextPosition.y() + textSize.height() > m_frame.height()) + { + break; + } + + textPosition = nextTextPosition; + startOfWord = endOfWord; + endOfWord = UTF8Helper::EndOfWord(startOfWord); + + std::cout< + +namespace reader +{ + +class WordWrapTextView : public PointerTextView { +public: + + void drawRect(KDContext * ctx, KDRect rect) const override; + +protected: + +}; + +} + +#endif \ No newline at end of file