Dirty commit that's not even working

This commit is contained in:
Nicolas Mailloux 2021-06-17 08:34:00 -04:00
parent f6099b9394
commit cb65131841
4 changed files with 159 additions and 78 deletions

View file

@ -21,6 +21,7 @@ namespace global {
inline int bookNumber; inline int bookNumber;
inline bool skipOpenDialog; inline bool skipOpenDialog;
inline bool startBatteryWatchdog; inline bool startBatteryWatchdog;
inline bool bookIsEpub;
} }
namespace kobox { namespace kobox {
inline bool showKoboxSplash; inline bool showKoboxSplash;

View file

@ -64,6 +64,13 @@ int main(int argc, char *argv[])
global::reader::startBatteryWatchdog = true; global::reader::startBatteryWatchdog = true;
global::reader::skipOpenDialog = true; global::reader::skipOpenDialog = true;
if(checkconfig("/inkbox/bookIsEpub") == true) {
global::reader::bookIsEpub = true;
}
else {
global::reader::bookIsEpub = false;
}
string_writeconfig("/inkbox/skip_opendialog", "true"); string_writeconfig("/inkbox/skip_opendialog", "true");
QApplication a(argc, argv); QApplication a(argc, argv);
reader w; reader w;

View file

@ -25,6 +25,9 @@ reader::reader(QWidget *parent) :
// Variables // Variables
global::battery::showLowBatteryDialog = true; global::battery::showLowBatteryDialog = true;
global::battery::showCriticalBatteryAlert = true; global::battery::showCriticalBatteryAlert = true;
if(global::reader::bookIsEpub == true) {
is_epub = true;
}
ui->setupUi(this); ui->setupUi(this);
ui->previousBtn->setProperty("type", "borderless"); ui->previousBtn->setProperty("type", "borderless");
@ -402,7 +405,12 @@ reader::reader(QWidget *parent) :
ui->text->setStyleSheet(font_size); ui->text->setStyleSheet(font_size);
// Wheeee! // Wheeee!
ui->text->setText(ittext); if(is_epub != true) {
ui->text->setText(ittext);
}
else {
ui->text->setText(epubPageContent);
}
// Clean up // Clean up
string_writeconfig("/inkbox/remount", "true"); string_writeconfig("/inkbox/remount", "true");
@ -515,30 +523,35 @@ int reader::setup_book(QString book, int i, bool run_parser) {
mount_proc->waitForFinished(); mount_proc->waitForFinished();
} }
if(booktostr_ran != true) { if(filematch_ran != true) {
if(epub_file_match(book) == true) { if(epub_file_match(book) == true) {
// Parsing ePUBs with epub2txt, thanks to GitHub:kevinboone QFile::copy(book, "/inkbox/book/book.epub");
QFile::copy(book, "/mutool_rootfs/run/book.epub");
// Parsing ePUBs with `mutool'
QString epubProg ("sh"); QString epubProg ("sh");
QStringList epubArgs; QStringList epubArgs;
epubArgs << "/mnt/onboard/.adds/inkbox/epub.sh" << book; convertMuPdfVars();
epubArgs << "/mnt/onboard/.adds/inkbox/epub.sh" << mupdf::fontSize_qstr << mupdf::width_qstr << mupdf::height_qstr << mupdf::epubPageNumber_qstr;
QProcess *epubProc = new QProcess(); QProcess *epubProc = new QProcess();
epubProc->start(epubProg, epubArgs); epubProc->start(epubProg, epubArgs);
epubProc->waitForFinished(); epubProc->waitForFinished();
filematch_ran = true;
is_epub = true; is_epub = true;
booktostr_ran = true; }
} else {
else {
// This is likely not an ePUB. // This is likely not an ePUB.
// Copying book specified in the function call // Copying book specified in the function call
QFile::copy(book, "/inkbox/book/book.txt"); QFile::copy(book, "/inkbox/book/book.txt");
filematch_ran = true;
is_epub = false; is_epub = false;
booktostr_ran = true;
} }
} }
// Checking if the user has defined an option for the number of words per page; if not, then setting the default. // Checking whether the user has defined an option for the number of words per page; if not, then setting the default.
// NOTE: This is only for plain text files.
QDir::setCurrent("/mnt/onboard/.adds/inkbox"); QDir::setCurrent("/mnt/onboard/.adds/inkbox");
string_checkconfig(".config/07-words_number/config"); string_checkconfig(".config/07-words_number/config");
if(checkconfig_str_val == "") { if(checkconfig_str_val == "") {
@ -547,44 +560,59 @@ int reader::setup_book(QString book, int i, bool run_parser) {
} }
// Parsing file // Parsing file
if(parser_ran != true) { if(filematch_ran != true) {
if(is_epub == true) { if(is_epub == true) {
QString parse_prog ("python3"); QString epubProg ("sh");
QStringList parse_args; QStringList epubArgs;
parse_args << "split.py" << checkconfig_str_val; convertMuPdfVars();
QProcess *parse_proc = new QProcess(); epubArgs << "/mnt/onboard/.adds/inkbox/epub.sh" << mupdf::fontSize_qstr << mupdf::width_qstr << mupdf::height_qstr << mupdf::epubPageNumber_qstr;
parse_proc->start(parse_prog, parse_args); QProcess *epubProc = new QProcess();
parse_proc->waitForFinished(); epubProc->start(epubProg, epubArgs);
parser_ran = true; epubProc->waitForFinished();
} }
else { else {
QString parse_prog ("python3"); if(parser_ran != true) {
QStringList parse_args; QString parse_prog ("python3");
parse_args << "split-txt.py" << checkconfig_str_val; QStringList parse_args;
QProcess *parse_proc = new QProcess(); parse_args << "split-txt.py" << checkconfig_str_val;
parse_proc->start(parse_prog, parse_args); QProcess *parse_proc = new QProcess();
parse_proc->waitForFinished(); parse_proc->start(parse_prog, parse_args);
parser_ran = true; parse_proc->waitForFinished();
parser_ran = true;
}
else {
;
}
} }
} }
// Changing current working directory // Changing current working directory
QDir::setCurrent("/inkbox/book"); QDir::setCurrent("/inkbox/book");
// Reading file // Reading files
if(run_parser == true) { if(is_epub != true) {
QDirIterator it("/inkbox/book/split"); if(run_parser == true) {
while (it.hasNext()) { QDirIterator it("/inkbox/book/split");
QFile f(it.next()); while (it.hasNext()) {
f.open(QIODevice::ReadOnly); QFile f(it.next());
content << f.readAll(); f.open(QIODevice::ReadOnly);
f.close(); content << f.readAll();
f.close();
}
return content.size();
QDir::setCurrent("/mnt/onboard/.adds/inkbox");
}
else {
ittext = content[i];
QDir::setCurrent("/mnt/onboard/.adds/inkbox");
} }
return content.size();
QDir::setCurrent("/mnt/onboard/.adds/inkbox");
} }
else { else {
ittext = content[i]; QFile epubPage("/inkbox/book/page");
epubPage.open(QIODevice::ReadOnly);
QTextStream in(&epubPage);
epubPageContent = in.readAll();
epubPage.close();
QDir::setCurrent("/mnt/onboard/.adds/inkbox"); QDir::setCurrent("/mnt/onboard/.adds/inkbox");
} }
return 0; return 0;
@ -665,65 +693,82 @@ void reader::save_word(string word, bool remove) {
void reader::on_nextBtn_clicked() void reader::on_nextBtn_clicked()
{ {
if(split_total - 1 == 1 or split_total - 1 == 0) { if(is_epub != true) {
QMessageBox::critical(this, tr("Invalid argument"), tr("You've reached the end of the document.")); if(split_total - 1 == 1 or split_total - 1 == 0) {
} QMessageBox::critical(this, tr("Invalid argument"), tr("You've reached the end of the document."));
else {
parser_ran = true;
split_total = split_total - 1;
setup_book(book_file, split_total, false);
ui->text->setText("");
ui->text->setText(ittext);
pagesTurned = pagesTurned + 1;
if(neverRefresh == true) {
// Do nothing; "Never refresh" was set
;
} }
else { else {
if(pagesTurned >= pageRefreshSetting) { parser_ran = true;
// Refreshing the screen split_total = split_total - 1;
this->repaint();
// Reset count setup_book(book_file, split_total, false);
pagesTurned = 0; ui->text->setText("");
} ui->text->setText(ittext);
pagesTurned = pagesTurned + 1;
writeconfig_pagenumber();
} }
}
else {
mupdf::epubPageNumber = mupdf::epubPageNumber + 1;
setup_book(book_file, mupdf::epubPageNumber, false);
pagesTurned = pagesTurned + 1;
writeconfig_pagenumber(); writeconfig_pagenumber();
} }
refreshScreen();
} }
void reader::on_previousBtn_clicked() void reader::on_previousBtn_clicked()
{ {
if(is_epub != true) {
// Making sure we won't encounter a "List index out of range" error ;) // Making sure we won't encounter a "List index out of range" error ;)
if(split_total >= split_files_number - 1) { if(split_total >= split_files_number - 1) {
QMessageBox::critical(this, tr("Invalid argument"), tr("No previous page.")); QMessageBox::critical(this, tr("Invalid argument"), tr("No previous page."));
}
else {
parser_ran = true;
split_total = split_total + 1;
setup_book(book_file, split_total, false);
ui->text->setText("");
ui->text->setText(ittext);
// We always increment pagesTurned regardless if we press the Previous or Next button
pagesTurned = pagesTurned + 1;
if(neverRefresh == true) {
// Do nothing; "Never refresh" was set
;
} }
else { else {
if(pagesTurned >= pageRefreshSetting) { parser_ran = true;
// Refreshing the screen split_total = split_total + 1;
this->repaint(); setup_book(book_file, split_total, false);
// Reset count ui->text->setText("");
pagesTurned = 0; if(is_epub != true) {
ui->text->setText(ittext);
} }
else {
ui->text->setText(epubPageContent);
}
// We always increment pagesTurned regardless whether we press the Previous or Next button
pagesTurned = pagesTurned + 1;
refreshScreen();
writeconfig_pagenumber();
} }
}
else {
mupdf::epubPageNumber = mupdf::epubPageNumber - 1;
setup_book(book_file, mupdf::epubPageNumber, false);
// We always increment pagesTurned regardless whether we press the Previous or Next button
pagesTurned = pagesTurned + 1;
writeconfig_pagenumber(); writeconfig_pagenumber();
} }
} }
void reader::refreshScreen() {
if(neverRefresh == true) {
// Do nothing; "Never refresh" was set
;
}
else {
if(pagesTurned >= pageRefreshSetting) {
// Refreshing the screen
this->repaint();
// Reset count
pagesTurned = 0;
}
}
}
void reader::on_optionsBtn_clicked() void reader::on_optionsBtn_clicked()
{ {
menubar_show(); menubar_show();
@ -1069,8 +1114,14 @@ void reader::on_sizeSlider_valueChanged(int value)
void reader::writeconfig_pagenumber() { void reader::writeconfig_pagenumber() {
// Saving the page number in tmpfs // Saving the page number in tmpfs
string split_total_str = to_string(split_total); if(is_epub != true) {
string_writeconfig("/tmp/inkboxPageNumber", split_total_str); std::string split_total_str = std::to_string(split_total);
string_writeconfig("/tmp/inkboxPageNumber", split_total_str);
}
else {
std::string epubPageNumber_str = std::to_string(mupdf::epubPageNumber);
string_writeconfig("/tmp/inkboxPageNumber", epubPageNumber_str);
}
} }
void reader::quit_restart() { void reader::quit_restart() {
@ -1099,3 +1150,9 @@ void reader::openCriticalBatteryAlertWindow() {
alertWindow->setGeometry(QRect(QPoint(0,0), screen()->geometry ().size())); alertWindow->setGeometry(QRect(QPoint(0,0), screen()->geometry ().size()));
alertWindow->show(); alertWindow->show();
} }
void reader::convertMuPdfVars() {
mupdf::fontSize_qstr = QString::number(mupdf::fontSize);
mupdf::width_qstr = QString::number(mupdf::width);
mupdf::height_qstr = QString::number(mupdf::height);
mupdf::epubPageNumber_qstr = QString::number(mupdf::epubPageNumber);
}

View file

@ -9,6 +9,18 @@
using namespace std; using namespace std;
// ePUB scaling
namespace mupdf {
inline int fontSize;
inline int width;
inline int height;
inline int epubPageNumber;
inline QString fontSize_qstr;
inline QString width_qstr;
inline QString height_qstr;
inline QString epubPageNumber_qstr;
}
namespace Ui { namespace Ui {
class reader; class reader;
} }
@ -37,6 +49,7 @@ public:
bool is_epub = false; bool is_epub = false;
bool parser_ran = false; bool parser_ran = false;
bool booktostr_ran = false; bool booktostr_ran = false;
bool filematch_ran = false;
bool neverRefresh = false; bool neverRefresh = false;
bool wakeFromSleep = false; bool wakeFromSleep = false;
bool remount = true; bool remount = true;
@ -58,6 +71,7 @@ public:
QPixmap scaledFullPixmap; QPixmap scaledFullPixmap;
QPixmap scaledEmptyPixmap; QPixmap scaledEmptyPixmap;
QList<QString> content; QList<QString> content;
QString epubPageContent;
int setup_book(QString book, int i, bool run_parser); int setup_book(QString book, int i, bool run_parser);
void checkwords(); void checkwords();
bool epub_file_match(QString file); bool epub_file_match(QString file);
@ -69,6 +83,8 @@ public:
void wordwidget_hide(); void wordwidget_hide();
void openLowBatteryDialog(); void openLowBatteryDialog();
void openCriticalBatteryAlertWindow(); void openCriticalBatteryAlertWindow();
void convertMuPdfVars();
void refreshScreen();
private slots: private slots:
void on_nextBtn_clicked(); void on_nextBtn_clicked();