quill/reader.cpp

2342 lines
83 KiB
C++
Raw Permalink Normal View History

2021-03-25 05:00:19 -07:00
#include "reader.h"
#include "ui_reader.h"
#include "functions.h"
2021-03-25 05:00:19 -07:00
#include <QProcess>
#include <QFileDialog>
#include <QDir>
#include <QTimer>
#include <QTime>
#include <QMessageBox>
#include <QFont>
#include <QIcon>
#include <QSize>
#include <QDesktopWidget>
#include <QScreen>
#include <QFontDatabase>
#include <QDirIterator>
#include <QTextCursor>
2021-08-29 12:05:07 -07:00
#include <QGraphicsScene>
2022-06-28 23:15:13 -07:00
#include <QJsonDocument>
2021-03-25 05:00:19 -07:00
using namespace std;
reader::reader(QWidget *parent) :
QWidget(parent),
ui(new Ui::reader)
{
2021-08-29 12:05:07 -07:00
// Elements
graphicsScene = new QGraphicsScene(this);
// Variables
global::battery::showLowBatteryDialog = true;
global::battery::showCriticalBatteryAlert = true;
2021-06-17 05:34:00 -07:00
if(global::reader::bookIsEpub == true) {
is_epub = true;
}
2021-09-04 10:46:39 -07:00
mupdf::convertRelativeValues = false;
wordwidgetLock = false;
goToSavedPageDone = false;
initialPdfRotationDone = false;
2021-03-25 05:00:19 -07:00
ui->setupUi(this);
ui->brightnessStatus->setFont(QFont("u001"));
ui->fontLabel->setFont(QFont("u001"));
ui->sizeLabel->setFont(QFont("u001"));
2022-04-03 20:26:15 -07:00
ui->sizeValueLabel->setFont(QFont("Inter"));
ui->alignmentLabel->setFont(QFont("u001"));
ui->fontChooser->setFont(QFont("u001"));
ui->definitionStatusLabel->setFont(QFont("u001"));
ui->pageProgressBar->setFont(QFont("u001"));
2021-03-25 05:00:19 -07:00
ui->previousBtn->setProperty("type", "borderless");
ui->nextBtn->setProperty("type", "borderless");
ui->optionsBtn->setProperty("type", "borderless");
ui->brightnessDecBtn->setProperty("type", "borderless");
ui->brightnessIncBtn->setProperty("type", "borderless");
ui->homeBtn->setProperty("type", "borderless");
ui->aboutBtn->setProperty("type", "borderless");
ui->alignLeftBtn->setProperty("type", "borderless");
ui->alignRightBtn->setProperty("type", "borderless");
ui->alignCenterBtn->setProperty("type", "borderless");
ui->alignLeftBtn->setProperty("type", "borderless");
ui->alignJustifyBtn->setProperty("type", "borderless");
ui->infoCloseBtn->setProperty("type", "borderless");
ui->saveWordBtn->setProperty("type", "borderless");
ui->previousDefinitionBtn->setProperty("type", "borderless");
ui->nextDefinitionBtn->setProperty("type", "borderless");
ui->nightModeBtn->setProperty("type", "borderless");
2021-07-06 17:06:40 -07:00
ui->searchBtn->setProperty("type", "borderless");
ui->gotoBtn->setProperty("type", "borderless");
2021-09-01 19:07:30 -07:00
ui->increaseScaleBtn->setProperty("type", "borderless");
ui->decreaseScaleBtn->setProperty("type", "borderless");
ui->quitBtn->setProperty("type", "borderless");
2021-03-25 05:00:19 -07:00
// Icons
ui->alignLeftBtn->setText("");
ui->alignLeftBtn->setIcon(QIcon(":/resources/align-left.png"));
ui->alignRightBtn->setText("");
ui->alignRightBtn->setIcon(QIcon(":/resources/align-right.png"));
ui->alignCenterBtn->setText("");
ui->alignCenterBtn->setIcon(QIcon(":/resources/align-center.png"));
ui->alignJustifyBtn->setText("");
ui->alignJustifyBtn->setIcon(QIcon(":/resources/align-justify.png"));
ui->infoCloseBtn->setText("");
ui->infoCloseBtn->setIcon(QIcon(":/resources/close.png"));
ui->saveWordBtn->setText("");
ui->saveWordBtn->setIcon(QIcon(":/resources/star.png"));
ui->previousDefinitionBtn->setText("");
ui->previousDefinitionBtn->setIcon(QIcon(":/resources/chevron-left.png"));
ui->nextDefinitionBtn->setText("");
ui->nextDefinitionBtn->setIcon(QIcon(":/resources/chevron-right.png"));
ui->brightnessDecBtn->setText("");
ui->brightnessDecBtn->setIcon(QIcon(":/resources/minus.png"));
ui->brightnessIncBtn->setText("");
ui->brightnessIncBtn->setIcon(QIcon(":/resources/plus.png"));
2021-06-21 04:19:14 -07:00
ui->homeBtn->setText("");
ui->homeBtn->setIcon(QIcon(":/resources/home.png"));
ui->aboutBtn->setText("");
ui->aboutBtn->setIcon(QIcon(":/resources/info.png"));
2021-07-06 17:06:40 -07:00
ui->searchBtn->setText("");
ui->searchBtn->setIcon(QIcon(":/resources/search.png"));
2021-09-01 19:07:30 -07:00
ui->increaseScaleBtn->setText("");
ui->increaseScaleBtn->setIcon(QIcon(":/resources/zoom-in.png"));
ui->decreaseScaleBtn->setText("");
ui->decreaseScaleBtn->setIcon(QIcon(":/resources/zoom-out.png"));
ui->quitBtn->setText("");
ui->quitBtn->setIcon(QIcon(":/resources/power.png"));
ui->previousBtn->setText("");
ui->previousBtn->setIcon(QIcon(":/resources/arrow-left.png"));
ui->optionsBtn->setText("");
ui->optionsBtn->setIcon(QIcon(":/resources/settings.png"));
ui->nextBtn->setText("");
ui->nextBtn->setIcon(QIcon(":/resources/arrow-right.png"));
2021-03-25 05:00:19 -07:00
// On the Mini with QT_FONT_DPI set to 187 (default for this device), quitBtn makes the UI go beyond the limits of the screen when the menu bar is shown
if(global::deviceID == "n705\n") {
ui->quitBtn->hide();
ui->quitBtn->deleteLater();
ui->line_19->hide();
ui->line_19->deleteLater();
}
// Style misc.
ui->bookInfoLabel->setStyleSheet("font-style: italic");
2021-03-25 05:00:19 -07:00
// Making text selectable
ui->text->setTextInteractionFlags(Qt::TextSelectableByMouse);
// Font misc.
int id = QFontDatabase::addApplicationFont(":/resources/fonts/CrimsonPro-Italic.ttf");
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
QFont crimson(family);
2021-08-05 21:48:49 -07:00
// Stylesheet + misc.
2022-04-03 20:26:15 -07:00
QFile stylesheetFile("/mnt/onboard/.adds/inkbox/eink.qss");
2021-08-05 21:48:49 -07:00
stylesheetFile.open(QFile::ReadOnly);
this->setStyleSheet(stylesheetFile.readAll());
stylesheetFile.close();
// We have to get the file's path
if(global::reader::skipOpenDialog == true) {
if(checkconfig("/tmp/suspendBook") == true) {
wakeFromSleep = true;
// Prevent from opening the Reader framework next time unless the condition is reset
string_writeconfig("/tmp/suspendBook", "false");
book_file = "/inkbox/book/book.txt";
}
else if(global::reader::bookFile.isEmpty() == false) {
book_file = global::reader::bookFile;
global::reader::bookFile = "";
}
2021-08-05 21:48:49 -07:00
}
else {
if(checkconfig("/opt/inkbox_genuine") == true) {
2021-10-10 14:01:26 -07:00
if(checkconfig("/external_root/run/encfs_mounted") == true) {
QDir::setCurrent("/mnt/onboard/onboard/encfs-decrypted");
}
else {
QDir::setCurrent("/mnt/onboard/onboard");
}
2021-08-05 21:48:49 -07:00
QFileDialog *dialog = new QFileDialog(this);
// https://forum.qt.io/topic/29471/solve-how-to-show-qfiledialog-at-center-position-screen/4
QDesktopWidget desk;
QRect screenres = desk.screenGeometry(0); dialog->setGeometry(QRect(screenres.width()/4,screenres.height() /4,screenres.width()/2,screenres.height()/2));
stylesheetFile.open(QFile::ReadOnly);
dialog->setStyleSheet(stylesheetFile.readAll());
stylesheetFile.close();
book_file = dialog->getOpenFileName(dialog, tr("Open File"), QDir::currentPath());
2022-03-23 19:09:37 -07:00
if(!book_file.isEmpty()) {
2021-08-05 21:48:49 -07:00
setDefaultWorkDir();
}
else {
// User clicked "Cancel" button
2022-03-23 19:09:37 -07:00
// Restarting InkBox
2021-08-05 21:48:49 -07:00
setDefaultWorkDir();
2022-03-23 19:09:37 -07:00
QProcess process;
process.startDetached("inkbox", QStringList());
qApp->quit();
2021-08-05 21:48:49 -07:00
}
}
else {
2021-10-10 14:01:26 -07:00
if(checkconfig("/external_root/run/encfs_mounted") == true) {
QDir::setCurrent("/mnt/onboard/onboard/encfs-decrypted");
}
else {
QDir::setCurrent("/mnt/onboard/onboard");
}
2021-08-05 21:48:49 -07:00
QDir::setCurrent("/mnt/onboard");
QFileDialog *dialog = new QFileDialog(this);
// https://forum.qt.io/topic/29471/solve-how-to-show-qfiledialog-at-center-position-screen/4
QDesktopWidget desk;
QRect screenres = desk.screenGeometry(0); dialog->setGeometry(QRect(screenres.width()/4,screenres.height() /4,screenres.width()/2,screenres.height()/2));
stylesheetFile.open(QFile::ReadOnly);
dialog->setStyleSheet(stylesheetFile.readAll());
stylesheetFile.close();
book_file = dialog->getOpenFileName(dialog, tr("Open File"), QDir::currentPath());
2022-03-23 19:09:37 -07:00
if(!book_file.isEmpty()) {
2021-08-05 21:48:49 -07:00
setDefaultWorkDir();
}
else {
// User clicked "Cancel" button
2022-03-23 19:09:37 -07:00
// Restarting InkBox
2021-08-05 21:48:49 -07:00
setDefaultWorkDir();
2022-03-23 19:09:37 -07:00
QProcess process;
process.startDetached("inkbox", QStringList());
qApp->quit();
2021-08-05 21:48:49 -07:00
}
}
}
log("Opening file '" + book_file + "'", className);
2021-08-05 21:48:49 -07:00
// Writing book path to file
std::string book_file_str = book_file.toStdString();
string_writeconfig("/tmp/inkboxBookPath", book_file_str);
// Calling InkBox daemon (ibxd) via FIFO interface to run bookconfig_mount
2022-03-23 19:09:37 -07:00
if(!book_file.isEmpty()) {
if(checkconfig(".config/16-global_reading_settings/config") == false) {
global::reader::globalReadingSettings = false;
string_writeconfig("/opt/ibxd", "bookconfig_mount\n");
// Callback handler to wait until bookconfig_mount has finished execution
while(true) {
if(QFile::exists("/inkbox/bookConfigSetUp")) {
QFile::remove("/inkbox/bookConfigSetUp");
setupLocalSettingsEnvironment();
break;
}
}
}
2022-03-23 19:09:37 -07:00
else {
global::reader::globalReadingSettings = true;
}
2021-08-19 09:55:12 -07:00
}
2021-08-05 21:48:49 -07:00
2021-03-25 05:00:19 -07:00
// Custom settings
// Brightness
if(global::reader::globalReadingSettings == false) {
2022-05-22 12:38:02 -07:00
if(global::deviceID != "n705\n" and global::deviceID != "n905\n" and global::deviceID != "kt\n") {
int brightness_value = brightness_checkconfig(".config/03-brightness/config");
log("Local Reading Settings: Setting brightness to " + QString::number(brightness_value), className);
cinematicBrightness(brightness_value, 2);
}
}
2021-03-25 05:00:19 -07:00
// Font
string_checkconfig(".config/04-book/font");
if(checkconfig_str_val == "") {
2021-06-21 04:19:14 -07:00
ui->fontChooser->setCurrentText("Source Serif Pro");
ui->text->setFont(QFont("Source Serif Pro"));
2021-03-25 05:00:19 -07:00
}
else {
if(checkconfig_str_val == "Crimson Pro") {
2021-07-11 19:11:13 -07:00
setCrimsonProFont();
}
else if(checkconfig_str_val == "Bitter") {
2021-07-11 19:11:13 -07:00
setBitterFont();
}
else if(checkconfig_str_val == "Ibarra Real Nova") {
setIbarraFont();
}
else if(checkconfig_str_val == "u001") {
ui->text->setFont(QFont("u001"));
ui->fontChooser->setCurrentText("Univers (u001)");
}
else {
QFont config_font(checkconfig_str_val);
ui->text->setFont(config_font);
ui->fontChooser->setCurrentText(checkconfig_str_val);
}
2021-03-25 05:00:19 -07:00
}
// Night mode
if(global::deviceID == "n705\n" or global::deviceID == "n905\n" or global::deviceID == "n613\n" or global::deviceID == "n236\n" or global::deviceID == "n437\n" or global::deviceID == "n306\n") {
if(checkconfig(".config/10-dark_mode/config") == true) {
2022-04-16 20:20:24 -07:00
log("Setting night mode to ON", className);
string_writeconfig("/tmp/invertScreen", "y");
ui->nightModeBtn->setText("");
ui->nightModeBtn->setIcon(QIcon(":/resources/nightmode-full.png"));
isNightModeActive = true;
}
else {
2022-04-16 20:20:24 -07:00
log("Setting night mode to OFF", className);
string_writeconfig("/tmp/invertScreen", "n");
ui->nightModeBtn->setText("");
ui->nightModeBtn->setIcon(QIcon(":/resources/nightmode-empty.png"));
isNightModeActive = false;
}
}
else {
2022-04-16 20:20:24 -07:00
log("Night mode disabled by software", className);
ui->line_7->hide();
ui->line_7->deleteLater();
ui->nightModeBtn->hide();
ui->nightModeBtn->deleteLater();
}
2021-03-25 05:00:19 -07:00
if(global::deviceID == "n873\n") {
2021-07-11 21:11:04 -07:00
ui->nextBtn->setStyleSheet("padding: 13.5px");
ui->previousBtn->setStyleSheet("padding: 13.5px");
ui->optionsBtn->setStyleSheet("padding: 13.5px");
}
else if(global::deviceID == "n437\n") {
ui->nextBtn->setStyleSheet("padding: 12.5px");
ui->previousBtn->setStyleSheet("padding: 12.5px");
ui->optionsBtn->setStyleSheet("padding: 12.5px");
}
2022-04-03 20:26:15 -07:00
ui->sizeValueLabel->setStyleSheet("font-size: 9pt; font-weight: bold");
ui->homeBtn->setStyleSheet("font-size: 9pt; padding: 5px");
ui->aboutBtn->setStyleSheet("font-size: 9pt; padding: 5px");
ui->fontChooser->setStyleSheet("font-size: 9pt");
ui->gotoBtn->setStyleSheet("font-size: 9pt; padding: 9px; font-weight: bold; background: lightGrey");
ui->pageNumberLabel->setFont(QFont("Source Serif Pro"));
2021-03-25 05:00:19 -07:00
2021-08-29 12:05:07 -07:00
// Hiding the menubar + definition widget + brightness widget + buttons bar widget
ui->menuWidget->setVisible(false);
ui->brightnessWidget->setVisible(false);
ui->menuBarWidget->setVisible(false);
2021-08-29 12:05:07 -07:00
ui->buttonsBarWidget->setVisible(false);
2021-09-01 19:07:30 -07:00
ui->pdfScaleWidget->setVisible(false);
ui->wordWidget->setVisible(false);
if(checkconfig(".config/11-menubar/sticky") == true) {
ui->menuWidget->setVisible(true);
ui->statusBarWidget->setVisible(true);
}
else {
ui->menuWidget->setVisible(false);
ui->statusBarWidget->setVisible(false);
}
ui->pageWidget->hide();
2021-08-29 12:05:07 -07:00
if(pdf_file_match(book_file) == true) {
ui->line_4->setLineWidth(2);
}
2021-03-25 05:00:19 -07:00
// Topbar widget / book info
ui->topbarStackedWidget->setVisible(true);
showTopbarWidget = true;
ui->bookInfoLabel->setFont(crimson);
2021-03-25 05:00:19 -07:00
// Getting brightness level
2021-06-26 07:09:51 -07:00
int brightness_value;
2022-05-22 12:38:02 -07:00
if(global::isN705 == true or global::isN905C == true or global::isKT == true or global::isN873 == true) {
2021-06-26 07:09:51 -07:00
brightness_value = get_brightness();
}
else if(global::isN613 == true) {
setDefaultWorkDir();
brightness_value = brightness_checkconfig(".config/03-brightness/config");
}
else {
brightness_value = get_brightness();
2021-06-26 07:09:51 -07:00
}
2021-03-25 05:00:19 -07:00
ui->brightnessStatus->setValue(brightness_value);
// Defining pixmaps
// Getting the screen's size
float sW = QGuiApplication::screens()[0]->size().width();
float sH = QGuiApplication::screens()[0]->size().height();
// Defining what the icons' size will be
if(checkconfig("/opt/inkbox_genuine") == true) {
2022-05-22 12:38:02 -07:00
if(global::deviceID == "n705\n" or global::deviceID == "n905\n" or global::deviceID == "n613\n" or global::deviceID == "n236\n" or global::deviceID == "n437\n" or global::deviceID == "n306\n" or global::deviceID == "kt\n" or global::deviceID == "emu\n") {
2021-03-25 05:00:19 -07:00
float stdIconWidth = sW / 16;
float stdIconHeight = sW / 16;
QPixmap chargingPixmap(":/resources/battery_charging.png");
scaledChargingPixmap = chargingPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
QPixmap fullPixmap(":/resources/battery_full.png");
scaledFullPixmap = fullPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
QPixmap halfPixmap(":/resources/battery_half.png");
scaledHalfPixmap = halfPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
QPixmap emptyPixmap(":/resources/battery_empty.png");
scaledEmptyPixmap = emptyPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
}
else {
float stdIconWidth = sW / 19;
float stdIconHeight = sH / 19;
QPixmap chargingPixmap(":/resources/battery_charging.png");
scaledChargingPixmap = chargingPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
QPixmap fullPixmap(":/resources/battery_full.png");
scaledFullPixmap = fullPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
QPixmap halfPixmap(":/resources/battery_half.png");
scaledHalfPixmap = halfPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
QPixmap emptyPixmap(":/resources/battery_empty.png");
scaledEmptyPixmap = emptyPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
}
}
else {
float stdIconWidth = sW / 19;
float stdIconHeight = sH / 19;
QPixmap chargingPixmap(":/resources/battery_charging.png");
scaledChargingPixmap = chargingPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
QPixmap fullPixmap(":/resources/battery_full.png");
scaledFullPixmap = fullPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
QPixmap halfPixmap(":/resources/battery_half.png");
scaledHalfPixmap = halfPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
QPixmap emptyPixmap(":/resources/battery_empty.png");
scaledEmptyPixmap = emptyPixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
}
2021-03-30 05:24:13 -07:00
// Checking if there is a page refresh setting set
string_checkconfig(".config/04-book/refresh");
if(checkconfig_str_val == "") {
// Writing the default, refresh every 3 pages
string_writeconfig(".config/04-book/refresh", "3");
string_checkconfig(".config/04-book/refresh");
}
else {
// A config option was set, continuing after the Else statement...
;
}
pageRefreshSetting = checkconfig_str_val.toInt();
// Checking if that config option was set to "Never refresh"...
if(pageRefreshSetting == -1) {
2022-04-16 20:20:24 -07:00
log("Setting page refresh to 'disabled'", className);
2021-03-30 05:24:13 -07:00
neverRefresh = true;
}
else {
// Safety measure
2022-04-16 20:20:24 -07:00
log("Setting page refresh to each " + checkconfig_str_val + " pages", className);
2021-03-30 05:24:13 -07:00
neverRefresh = false;
}
2021-03-25 05:00:19 -07:00
// Clock setting to show seconds + battery level
if(checkconfig(".config/02-clock/config") == true) {
QTimer *t = new QTimer(this);
t->setInterval(500);
connect(t, &QTimer::timeout, [&]() {
QString time = QTime::currentTime().toString("hh:mm:ss");
get_battery_level();
ui->batteryLabel->setText(batt_level);
ui->timeLabel->setText(time);
} );
t->start();
}
else {
QTimer *t = new QTimer(this);
t->setInterval(500);
connect(t, &QTimer::timeout, [&]() {
QString time = QTime::currentTime().toString("hh:mm");
get_battery_level();
ui->batteryLabel->setText(batt_level);
ui->timeLabel->setText(time);
} );
t->start();
}
// Checking if we're waking from sleep and have lockscreen enabled; if so, do nothing there because the book should already have been parsed
if(wakeFromSleep != true) {
2021-06-28 21:55:44 -07:00
// Remount tmpfs
string_writeconfig("/inkbox/remount", "true");
// Counting number of parsed files
split_total = setup_book(book_file, 0, true);
split_files_number = split_total;
split_total = split_total - 1;
2021-08-19 09:55:12 -07:00
writeconfig_pagenumber(false);
}
else {
2021-06-29 09:26:30 -07:00
// Retrieve split_total from tmpfs
string_checkconfig("/tmp/inkboxPageNumber");
split_total = checkconfig_str_val.toInt();
setup_book(book_file, 0, true);
}
2021-03-25 05:00:19 -07:00
// Get text; no need to do it multiple times for ePUB books
2022-01-09 08:06:22 -08:00
if(is_epub == false && is_pdf == false && is_image == false) {
setDefaultWorkDir();
if(global::reader::globalReadingSettings == false) {
string_checkconfig_ro(".config/A-page_number/config");
if(checkconfig_str_val != "") {
split_total = checkconfig_str_val.toInt();
}
}
setup_book(book_file, split_total, true);
}
2021-03-25 05:00:19 -07:00
// Display text
// Checking saved font size if any
string_checkconfig(".config/04-book/size");
if(checkconfig_str_val == "0") {
checkconfig_str_val = "6";
ui->sizeSlider->setValue(0);
ui->sizeValueLabel->setText("1");
}
if(checkconfig_str_val == "1") {
checkconfig_str_val = "10";
ui->sizeSlider->setValue(1);
ui->sizeValueLabel->setText("2");
}
if(checkconfig_str_val == "2") {
checkconfig_str_val = "14";
ui->sizeSlider->setValue(2);
ui->sizeValueLabel->setText("3");
}
if(checkconfig_str_val == "3") {
checkconfig_str_val = "18";
ui->sizeSlider->setValue(3);
ui->sizeValueLabel->setText("4");
}
if(checkconfig_str_val == "4") {
checkconfig_str_val = "22";
ui->sizeSlider->setValue(4);
ui->sizeValueLabel->setText("5");
}
if(checkconfig_str_val == "") {
checkconfig_str_val = "10";
ui->sizeSlider->setValue(1);
ui->sizeValueLabel->setText("2");
}
QString font_size = "font-size: ";
font_size = font_size.append(checkconfig_str_val);
font_size = font_size.append("pt");
2022-04-16 20:20:24 -07:00
log("Setting font size to " + checkconfig_str_val + " points", className);
2021-03-25 05:00:19 -07:00
ui->text->setStyleSheet(font_size);
// If needed, show scroll bar when rendering engine isn't doing its job properly
if(checkconfig(".config/14-reader_scrollbar/config") == true) {
ui->text->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->text->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
}
else {
ui->text->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->text->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
2021-03-25 05:00:19 -07:00
// Wheeee!
2021-08-19 09:55:12 -07:00
if(is_epub == true) {
2021-08-29 12:05:07 -07:00
ui->graphicsView->hide();
ui->graphicsView->deleteLater();
2021-08-19 09:55:12 -07:00
ui->text->setText(epubPageContent);
2021-06-17 05:34:00 -07:00
}
2021-08-29 12:05:07 -07:00
else if(is_pdf == true) {
ui->text->hide();
ui->text->deleteLater();
setupPng();
}
2022-01-09 08:06:22 -08:00
else if(is_image == true) {
ui->text->hide();
ui->text->deleteLater();
setupPng();
}
2021-06-17 05:34:00 -07:00
else {
ui->graphicsView->hide();
ui->graphicsView->deleteLater();
2021-08-19 09:55:12 -07:00
ui->text->setText(ittext);
2021-06-17 05:34:00 -07:00
}
2021-03-25 05:00:19 -07:00
// Alignment
string_checkconfig(".config/04-book/alignment");
if (checkconfig_str_val == "") {
;
}
else {
if(checkconfig_str_val == "Left") {
textAlignment = 0;
alignText(0);
}
if(checkconfig_str_val == "Center") {
textAlignment = 1;
alignText(1);
}
if(checkconfig_str_val == "Right") {
textAlignment = 2;
alignText(2);
}
if(checkconfig_str_val == "Justify") {
textAlignment = 3;
alignText(3);
}
2022-04-16 20:20:24 -07:00
log("Setting text alignment to '" + checkconfig_str_val + "'", className);
}
// Topbar info widget
if(is_epub == true) {
QString bookCreator = findEpubMetadata(book_file, "creator");
QString bookTitle = findEpubMetadata(book_file, "title");
bookCreator = bookCreator.trimmed();
bookTitle = bookTitle.trimmed();
QString infoLabelContent = bookCreator;
infoLabelContent.append("");
infoLabelContent.append(bookTitle);
2021-06-21 04:19:14 -07:00
int infoLabelLength = infoLabelContent.length();
int infoLabelDefinedLength;
if(global::deviceID == "n705\n") {
infoLabelDefinedLength = 35;
}
2022-05-22 12:38:02 -07:00
else if(global::deviceID == "n905\n" or global::deviceID == "n613\n" or global::deviceID == "n236\n" or global::deviceID == "n437\n" or global::deviceID == "n306\n" or global::deviceID == "kt\n") {
infoLabelDefinedLength = 50;
}
else {
infoLabelDefinedLength = 50;
}
if(infoLabelLength <= infoLabelDefinedLength) {
2021-06-21 04:19:14 -07:00
ui->bookInfoLabel->setWordWrap(false);
}
else {
ui->bookInfoLabel->setWordWrap(true);
}
ui->bookInfoLabel->setText(infoLabelContent);
}
else {
2021-06-29 09:26:30 -07:00
QString bookReadRelativePath;
if(wakeFromSleep == true) {
string_checkconfig_ro("/tmp/inkboxBookPath");
bookReadRelativePath = checkconfig_str_val.split("/").last();
}
else {
bookReadRelativePath = book_file.split("/").last();
}
int infoLabelDefinedLength;
if(global::deviceID == "n705\n") {
infoLabelDefinedLength = 35;
}
2022-05-22 12:38:02 -07:00
else if(global::deviceID == "n905\n" or global::deviceID == "n613\n" or global::deviceID == "n236\n" or global::deviceID == "n437\n" or global::deviceID == "n306\n" or global::deviceID == "kt\n") {
infoLabelDefinedLength = 50;
}
else {
infoLabelDefinedLength = 50;
}
if(bookReadRelativePath <= infoLabelDefinedLength) {
ui->bookInfoLabel->setWordWrap(false);
}
else {
ui->bookInfoLabel->setWordWrap(true);
}
ui->bookInfoLabel->setText(bookReadRelativePath);
}
// Clean up
string_writeconfig("/inkbox/remount", "true");
// Way to tell shell scripts that we're in the Reader framework
string_writeconfig("/tmp/inkboxReading", "true");
// Maintain a 'Recent books' list
2022-06-28 23:15:13 -07:00
QJsonObject recentBooksObject;
if(QFile::exists(global::localLibrary::recentBooksDatabasePath)) {
2022-07-01 15:56:34 -07:00
log("Reading recent books database", className);
QFile recentBooksDatabase(global::localLibrary::recentBooksDatabasePath);
QByteArray recentBooksData;
if(recentBooksDatabase.open(QIODevice::ReadOnly)) {
recentBooksData = recentBooksDatabase.readAll();
recentBooksDatabase.close();
}
else {
QString function = __func__; log(function + ": Failed to open recent books database file for reading at '" + recentBooksDatabase.fileName() + "'", className);
}
QJsonObject mainJsonObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(recentBooksData))).object();
2022-06-28 23:15:13 -07:00
for(int i = 1; i <= global::homePageWidget::recentBooksNumber; i++) {
QString objectName = "Book" + QString::number(i);
QJsonObject jsonObject = mainJsonObject[objectName].toObject();
if(i == 1) {
if(jsonObject.value("BookPath").toString() != book_file) {
// Circular buffer
for(int i = global::homePageWidget::recentBooksNumber; i >= 2; i--) {
mainJsonObject["Book" + QString::number(i)] = mainJsonObject["Book" + QString::number(i - 1)];
}
jsonObject.insert("BookPath", QJsonValue(book_file));
mainJsonObject[objectName] = jsonObject;
}
}
}
recentBooksObject = mainJsonObject;
}
else {
2022-06-28 23:15:13 -07:00
QJsonObject mainJsonObject;
QJsonObject firstJsonObject;
firstJsonObject.insert("BookPath", QJsonValue(book_file));
mainJsonObject["Book1"] = firstJsonObject;
2021-03-25 05:00:19 -07:00
2022-06-28 23:15:13 -07:00
for(int i = 2; i <= global::homePageWidget::recentBooksNumber; i++) {
QJsonObject jsonObject;
jsonObject.insert("BookPath", QJsonValue(""));
mainJsonObject["Book" + QString::number(i)] = jsonObject;
}
recentBooksObject = mainJsonObject;
2021-03-25 05:00:19 -07:00
}
2022-06-28 23:15:13 -07:00
QFile::remove(global::localLibrary::recentBooksDatabasePath);
2022-07-01 15:56:34 -07:00
writeFile(global::localLibrary::recentBooksDatabasePath, qCompress(QJsonDocument(recentBooksObject).toJson()).toBase64());
2021-06-29 09:26:30 -07:00
// USB mass storage prompt
if(global::reader::startUsbmsPrompt == true) {
QTimer *usbmsPrompt = new QTimer(this);
usbmsPrompt->setInterval(500);
connect(usbmsPrompt, &QTimer::timeout, [&]() {
if(checkconfig("/opt/inkbox_genuine") == true) {
if(global::usbms::showUsbmsDialog != true) {
2022-02-15 19:47:10 -08:00
if(isUsbPluggedIn() != usbmsStatus) {
2021-06-29 09:26:30 -07:00
global::usbms::showUsbmsDialog = true;
}
}
else {
2022-02-15 19:47:10 -08:00
usbmsStatus = isUsbPluggedIn();
if(usbmsStatus == false) {
2021-06-29 09:26:30 -07:00
// Loop again...
;
}
else {
// An USB cable is connected!
openUsbmsDialog();
}
}
}
else {
// Do nothing, we're running along with Nickel & friends...
;
}
} );
usbmsPrompt->start();
}
// Battery watchdog
if(global::reader::startBatteryWatchdog == true) {
QTimer *t = new QTimer(this);
t->setInterval(2000);
connect(t, &QTimer::timeout, [&]() {
// Checking if battery level is low
if(global::battery::showCriticalBatteryAlert != true) {
;
}
else {
if(isBatteryCritical() == true) {
log("Warning! Battery is at a critical charge level!", className);
openCriticalBatteryAlertWindow();
}
}
if(global::battery::showLowBatteryDialog != true) {
// Do nothing, since a dialog should already have been displayed and (probably) dismissed
;
}
else {
if(isBatteryLow() == true) {
if(global::battery::batteryAlertLock == true) {
;
}
else {
2022-02-15 19:47:10 -08:00
if(isUsbPluggedIn()) {
;
}
else {
log("Warning! Battery is low!", className);
openLowBatteryDialog();
}
}
}
}
} );
if(global::deviceID != "emu\n") {
t->start();
}
}
// Pages number info label
if(is_pdf == true) {
2021-08-29 12:05:07 -07:00
getTotalPdfPagesNumber();
}
// The total pages number is already known for ePUBs at this point
setupPageWidget();
2021-08-19 09:55:12 -07:00
// Reading settings auto-save timer
if(global::reader::globalReadingSettings == false) {
QTimer * saveSettingsTimer = new QTimer(this);
saveSettingsTimer->setInterval(900000); // 15 minutes
connect(saveSettingsTimer, SIGNAL(timeout()), this, SLOT(saveReadingSettings()));
saveSettingsTimer->start();
}
2021-03-25 05:00:19 -07:00
}
reader::~reader()
{
delete ui;
}
2021-04-22 07:10:47 -07:00
int reader::setup_book(QString book, int i, bool run_parser) {
// Parse ebook
2021-08-29 12:05:07 -07:00
if(remount == true) {
2021-04-22 07:10:47 -07:00
QString mount_prog ("sh");
QStringList mount_args;
mount_args << "split.sh";
2021-08-29 12:05:07 -07:00
QProcess * mount_proc = new QProcess();
2021-04-22 07:10:47 -07:00
mount_proc->start(mount_prog, mount_args);
mount_proc->waitForFinished();
2021-08-29 12:05:07 -07:00
mount_proc->deleteLater();
2021-04-22 07:10:47 -07:00
remount = false;
}
else {
string_writeconfig("/inkbox/remount", "false");
QString mount_prog ("sh");
QStringList mount_args;
mount_args << "split.sh";
2021-08-29 12:05:07 -07:00
QProcess * mount_proc = new QProcess();
2021-04-22 07:10:47 -07:00
mount_proc->start(mount_prog, mount_args);
mount_proc->waitForFinished();
2021-08-29 12:05:07 -07:00
mount_proc->deleteLater();
2021-04-22 07:10:47 -07:00
}
2021-08-29 12:05:07 -07:00
if(filematch_ran == false) {
2021-04-22 07:10:47 -07:00
if(epub_file_match(book) == true) {
QFile::remove("/run/book.epub");
QFile::copy(book, "/run/book.epub");
2021-06-17 05:34:00 -07:00
getTotalEpubPagesNumber();
2021-04-22 07:10:47 -07:00
2021-06-17 05:34:00 -07:00
filematch_ran = true;
2021-04-22 07:10:47 -07:00
is_epub = true;
2021-08-29 12:05:07 -07:00
}
else if(pdf_file_match(book) == true) {
getPdfOrientation(book);
2021-08-29 12:05:07 -07:00
QString pdfProg("/usr/local/bin/mutool");
QStringList pdfArgs;
convertMuPdfVars(1, true);
2021-08-29 12:05:07 -07:00
pdfArgs << "convert" << "-F" << "png" << "-O" << "width=" + mupdf::pdf::width_qstr + ",height=" + mupdf::pdf::height_qstr << "-o" << "/run/page.png" << book_file << mupdf::pdf::pdfPageNumber_qstr;
QProcess * pdfProc = new QProcess();
pdfProc->start(pdfProg, pdfArgs);
pdfProc->waitForFinished();
pdfProc->deleteLater();
filematch_ran = true;
is_pdf = true;
}
2022-01-09 08:06:22 -08:00
else if(image_file_match(book) == true) {
ui->previousBtn->hide();
ui->nextBtn->hide();
ui->line->hide();
ui->line_3->hide();
ui->previousBtn->deleteLater();
ui->nextBtn->deleteLater();
ui->line->deleteLater();
ui->line_3->deleteLater();
string_writeconfig("/tmp/inkboxImagePath", book.toStdString());
string_writeconfig("/opt/ibxd", "inkbox_convert_image\n");
while(true) {
if(QFile::exists("/inkbox/convertImageDone")) {
if(checkconfig("/inkbox/convertImageDone") == true) {
filematch_ran = true;
is_image = true;
break;
}
else {
showToast("Failed to display image");
break;
}
QFile::remove("/inkbox/convertImageDone");
}
}
filematch_ran = true;
is_image = true;
}
2021-08-29 12:05:07 -07:00
else {
2022-01-09 08:06:22 -08:00
// This is likely not an ePUB, a PDF or a picture.
2021-04-22 07:10:47 -07:00
// Copying book specified in the function call
QFile::copy(book, "/inkbox/book/book.txt");
2021-06-17 05:34:00 -07:00
filematch_ran = true;
2021-04-22 07:10:47 -07:00
is_epub = false;
}
}
// Checking whether or not the user has defined an option for the number of words per page; if not, then setting the default.
2021-06-17 05:34:00 -07:00
// NOTE: This is only for plain text files.
setDefaultWorkDir();
2021-04-22 07:10:47 -07:00
string_checkconfig(".config/07-words_number/config");
if(checkconfig_str_val == "") {
string_writeconfig(".config/07-words_number/config", "100");
string_checkconfig(".config/07-words_number/config");
}
// Parsing file
2021-06-18 17:46:34 -07:00
if(is_epub == true) {
if(run_parser == true) {
2021-08-29 12:05:07 -07:00
if(filematch_ran == true) {
QString epubProg ("sh");
QStringList epubArgs;
convertMuPdfVars(0, false);
epubArgs << "/mnt/onboard/.adds/inkbox/epub.sh" << "0" << "0" << "0" << mupdf::epub::epubPageNumber_qstr;
2021-08-29 12:05:07 -07:00
QProcess * epubProc = new QProcess();
epubProc->start(epubProg, epubArgs);
epubProc->waitForFinished();
2021-08-29 12:05:07 -07:00
epubProc->deleteLater();
}
}
}
else if(is_pdf == true) {
if(run_parser == true) {
if(filematch_ran == true) {
QString pdfProg("/usr/local/bin/mutool");
QStringList pdfArgs;
convertMuPdfVars(1, true);
2021-08-29 12:05:07 -07:00
pdfArgs << "convert" << "-F" << "png" << "-O" << "width=" + mupdf::pdf::width_qstr + ",height=" + mupdf::pdf::height_qstr << "-o" << "/run/page.png" << book_file << mupdf::pdf::pdfPageNumber_qstr;
QProcess * pdfProc = new QProcess();
pdfProc->start(pdfProg, pdfArgs);
pdfProc->waitForFinished();
pdfProc->deleteLater();
}
2021-06-18 17:46:34 -07:00
}
}
2022-01-09 08:06:22 -08:00
else if(is_image == true) {
// Do nothing, since images won't have pages
;
}
2021-06-18 17:46:34 -07:00
else {
QString parse_prog ("python3");
QStringList parse_args;
parse_args << "split-txt.py" << checkconfig_str_val;
2021-08-29 12:05:07 -07:00
QProcess * parse_proc = new QProcess();
parse_proc->start(parse_prog, parse_args);
parse_proc->waitForFinished();
2021-08-29 12:05:07 -07:00
parse_proc->deleteLater();
2021-04-22 07:10:47 -07:00
}
2021-06-17 05:34:00 -07:00
// Reading files
2021-08-29 12:05:07 -07:00
if(is_epub == false) {
2021-06-17 05:34:00 -07:00
if(run_parser == true) {
QDirIterator it("/inkbox/book/split");
while (it.hasNext()) {
QFile f(it.next());
f.open(QIODevice::ReadOnly);
content << f.readAll();
f.close();
}
ittext = content[i];
2021-06-17 05:34:00 -07:00
return content.size();
}
else {
ittext = content[i];
2021-04-22 07:10:47 -07:00
}
}
else {
2021-06-17 05:34:00 -07:00
QFile epubPage("/inkbox/book/page");
epubPage.open(QIODevice::ReadOnly);
QTextStream in(&epubPage);
epubPageContent = in.readAll();
epubPage.close();
2021-04-22 07:10:47 -07:00
}
return 0;
}
void reader::checkwords() {
QFile words_list(".config/06-words/config");
words_list.open(QIODevice::ReadWrite);
QTextStream in (&words_list);
words = in.readAll();
words_list.close();
}
bool reader::epub_file_match(QString file) {
QString fileExt = file.right(4);
if(fileExt == "epub" or fileExt == "EPUB") {
2022-04-16 20:20:24 -07:00
log("Book file format: ePUB", className);
2021-06-29 09:26:30 -07:00
string_writeconfig("/inkbox/bookIsEpub", "true");
return true;
}
else {
2021-06-29 09:26:30 -07:00
string_writeconfig("/inkbox/bookIsEpub", "false");
return false;
}
}
void reader::dictionary_lookup(string word, QString first_letter, int position) {
2022-04-15 17:27:58 -07:00
log("Dictionary lookup requested for word '" + QString::fromStdString(word) + "', position " + QString::number(position), className);
ofstream fhandler;
fhandler.open("/inkbox/dictionary/word");
fhandler << word;
fhandler.close();
QDir::setCurrent("dictionary");
QDir::setCurrent(first_letter);
QString lookup_prog ("sh");
QStringList lookup_args;
QString position_str = QString::number(position);
lookup_args << "../scripts/lookup.sh" << position_str;
2021-08-29 12:05:07 -07:00
QProcess * lookup_proc = new QProcess();
lookup_proc->start(lookup_prog, lookup_args);
lookup_proc->waitForFinished();
2021-08-29 12:05:07 -07:00
lookup_proc->deleteLater();
QFile definition_file("/inkbox/dictionary/definition");
definition_file.open(QIODevice::ReadWrite);
QTextStream in (&definition_file);
definition = in.readAll();
definition = definition.remove(QRegExp("[\n]"));
if(definition == "No definition found.") {
nextdefinition_lock = true;
}
else {
nextdefinition_lock = false;
}
definition_file.close();
setDefaultWorkDir();
}
void reader::save_word(string word, bool remove) {
if(remove == false) {
QFile words(".config/06-words/config");
words.open(QIODevice::ReadWrite);
QTextStream in (&words);
QString words_list = in.readAll();
string words_list_str = words_list.toStdString();
words.close();
2022-04-16 20:20:24 -07:00
log("Saving word '" + QString::fromStdString(word) + "' in Saved Words list", className);
ofstream fhandler;
fhandler.open(".config/06-words/config");
fhandler << words_list_str << word << "\n";
fhandler.close();
}
else {
ofstream fhandler;
fhandler.open(".config/06-words/config");
fhandler << word;
fhandler.close();
}
}
2021-03-25 05:00:19 -07:00
void reader::on_nextBtn_clicked()
{
// ui->nextBtn->setEnabled(false);
2021-08-29 12:05:07 -07:00
if(is_epub == false and is_pdf == false) {
2021-06-17 05:34:00 -07:00
if(split_total - 1 == 1 or split_total - 1 == 0) {
showToast("You've reached the end of the document");
2021-06-17 05:34:00 -07:00
}
else {
split_total = split_total - 1;
2022-04-16 20:20:24 -07:00
log("'Next' button clicked", className);
2021-06-17 05:34:00 -07:00
setup_book(book_file, split_total, false);
ui->text->setText("");
ui->text->setText(ittext);
pagesTurned = pagesTurned + 1;
2021-08-19 09:55:12 -07:00
writeconfig_pagenumber(false);
2021-08-29 12:05:07 -07:00
alignText(textAlignment);
2021-06-17 05:34:00 -07:00
}
2021-03-25 05:00:19 -07:00
}
2021-08-29 12:05:07 -07:00
else if(is_epub == true) {
if(mupdf::epub::epubPageNumber + 1 > totalPagesInt) {
showToast("You've reached the end of the document");
}
else {
2021-08-29 12:05:07 -07:00
mupdf::epub::epubPageNumber = mupdf::epub::epubPageNumber + 1;
2022-04-16 20:20:24 -07:00
log("'Next' button clicked: going to page " + QString::number(mupdf::epub::epubPageNumber), className);
2021-08-29 12:05:07 -07:00
setup_book(book_file, mupdf::epub::epubPageNumber, true);
ui->text->setText("");
ui->text->setText(epubPageContent);
2021-03-30 05:24:13 -07:00
2021-08-29 12:05:07 -07:00
pagesTurned = pagesTurned + 1;
writeconfig_pagenumber(false);
alignText(textAlignment);
}
}
else if(is_pdf == true) {
if(mupdf::pdf::pdfPageNumber + 1 > totalPagesInt) {
showToast("You've reached the end of the document");
}
else {
mupdf::pdf::pdfPageNumber = mupdf::pdf::pdfPageNumber + 1;
2022-04-16 20:20:24 -07:00
log("'Next' button clicked: going to page " + QString::number(mupdf::pdf::pdfPageNumber), className);
2022-04-01 19:20:38 -07:00
if(ui->pdfScaleSlider->value() != 1) {
2022-03-22 15:26:05 -07:00
mupdf::convertRelativeValues = true;
}
2021-08-29 12:05:07 -07:00
setup_book(book_file, mupdf::pdf::pdfPageNumber, true);
setupPng();
pagesTurned = pagesTurned + 1;
2021-08-19 09:55:12 -07:00
writeconfig_pagenumber(false);
}
2021-03-25 05:00:19 -07:00
}
setupPageWidget();
2021-06-17 05:34:00 -07:00
refreshScreen();
// ui->nextBtn->setEnabled(true);
2021-03-25 05:00:19 -07:00
}
void reader::on_previousBtn_clicked()
{
// ui->previousBtn->setEnabled(false);
2021-08-29 12:05:07 -07:00
if(is_epub == false and is_pdf == false) {
2021-03-25 05:00:19 -07:00
// Making sure we won't encounter a "List index out of range" error ;)
2021-06-17 05:34:00 -07:00
if(split_total >= split_files_number - 1) {
showToast("No previous page");
2021-03-30 05:24:13 -07:00
}
else {
2021-06-17 05:34:00 -07:00
split_total = split_total + 1;
2022-04-16 20:20:24 -07:00
log("'Previous' button clicked", className);
2021-06-17 05:34:00 -07:00
setup_book(book_file, split_total, false);
ui->text->setText("");
ui->text->setText(ittext);
2021-06-17 05:34:00 -07:00
// We always increment pagesTurned regardless whether we press the Previous or Next button or not
2021-08-29 12:05:07 -07:00
pagesTurned = pagesTurned + 1;
writeconfig_pagenumber(false);
alignText(textAlignment);
}
}
else if (is_pdf == true) {
if(mupdf::pdf::pdfPageNumber - 1 <= 0) {
showToast("No previous page");
}
else {
mupdf::pdf::pdfPageNumber = mupdf::pdf::pdfPageNumber - 1;
2022-04-16 20:20:24 -07:00
log("'Previous' button clicked: going to page " + QString::number(mupdf::pdf::pdfPageNumber), className);
2022-04-01 19:20:38 -07:00
if(ui->pdfScaleSlider->value() != 1) {
mupdf::convertRelativeValues = true;
}
2021-08-29 12:05:07 -07:00
setup_book(book_file, mupdf::pdf::pdfPageNumber, true);
setupPng();
// We always increment pagesTurned regardless whether we press the Previous or Next button or not
2021-06-17 05:34:00 -07:00
pagesTurned = pagesTurned + 1;
2021-08-19 09:55:12 -07:00
writeconfig_pagenumber(false);
2021-03-30 05:24:13 -07:00
}
2021-06-17 05:34:00 -07:00
}
else {
2021-08-29 12:05:07 -07:00
if(mupdf::epub::epubPageNumber - 1 <= 0) {
showToast("No previous page");
}
else {
2021-08-29 12:05:07 -07:00
mupdf::epub::epubPageNumber = mupdf::epub::epubPageNumber - 1;
2022-04-16 20:20:24 -07:00
log("'Previous' button clicked: going to page " + QString::number(mupdf::epub::epubPageNumber), className);
2021-08-29 12:05:07 -07:00
setup_book(book_file, mupdf::epub::epubPageNumber, true);
ui->text->setText("");
ui->text->setText(epubPageContent);
2021-06-17 05:34:00 -07:00
// We always increment pagesTurned regardless whether we press the Previous or Next button not
pagesTurned = pagesTurned + 1;
2021-08-19 09:55:12 -07:00
writeconfig_pagenumber(false);
2021-08-29 12:05:07 -07:00
alignText(textAlignment);
}
2021-03-25 05:00:19 -07:00
}
setupPageWidget();
refreshScreen();
// ui->previousBtn->setEnabled(true);
2021-03-25 05:00:19 -07:00
}
2021-06-17 05:34:00 -07:00
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;
}
}
}
2021-03-25 05:00:19 -07:00
void reader::on_optionsBtn_clicked()
{
2022-04-16 20:20:24 -07:00
log("'Options' button clicked", className);
if(menubar_shown == true) {
menubar_hide();
if(global::deviceID == "n873\n") {
ui->optionsBtn->setStyleSheet("background: white; color: black; padding: 13.5px");
}
else if(global::deviceID == "n437\n") {
ui->optionsBtn->setStyleSheet("background: white; color: black; padding: 12.5px");
}
else {
ui->optionsBtn->setStyleSheet("background: white; color: black");
}
ui->optionsBtn->setIcon(QIcon(":/resources/settings.png"));
2022-04-07 19:18:06 -07:00
// The Glo HD (N437) has a newer platform plugin that doesn't need this
if(global::deviceID != "n437\n") {
QTimer::singleShot(500, this, SLOT(repaint()));
}
menubar_shown = false;
}
else {
menubar_show();
if(global::deviceID == "n873\n") {
ui->optionsBtn->setStyleSheet("background: black; color: white; padding: 13.5px");
}
else if(global::deviceID == "n437\n") {
ui->optionsBtn->setStyleSheet("background: black; color: white; padding: 12.5px");
}
else {
ui->optionsBtn->setStyleSheet("background: black; color: white");
}
ui->optionsBtn->setIcon(QIcon(":/resources/settings-inverted.png"));
this->repaint();
menubar_shown = true;
}
2021-03-25 05:00:19 -07:00
}
void reader::on_brightnessDecBtn_clicked()
{
2021-06-26 07:09:51 -07:00
int bval;
2022-05-22 12:38:02 -07:00
if(global::isN705 == true or global::isN905C == true or global::isKT == true or global::isN873 == true) {
2021-06-26 07:09:51 -07:00
bval = get_brightness();
}
else if(global::isN613 == true) {
setDefaultWorkDir();
bval = brightness_checkconfig(".config/03-brightness/config");
}
else {
bval = get_brightness();
2021-06-26 07:09:51 -07:00
}
2021-03-25 05:00:19 -07:00
int set_bval = bval - 1;
2021-06-26 16:29:51 -07:00
if(set_bval < 0) {
set_bval = 0;
}
2021-06-25 19:09:42 -07:00
pre_set_brightness(set_bval);
2021-03-25 05:00:19 -07:00
brightness_writeconfig(set_bval);
2021-06-26 07:09:51 -07:00
ui->brightnessStatus->setValue(set_bval);
2021-03-25 05:00:19 -07:00
}
void reader::on_brightnessIncBtn_clicked()
{
2021-06-26 07:09:51 -07:00
int bval;
2022-05-22 12:38:02 -07:00
if(global::isN705 == true or global::isN905C == true or global::isKT == true or global::isN873 == true) {
2021-06-26 07:09:51 -07:00
bval = get_brightness();
}
else if(global::isN613 == true) {
setDefaultWorkDir();
bval = brightness_checkconfig(".config/03-brightness/config");
}
else {
bval = get_brightness();
2021-06-26 07:09:51 -07:00
}
2021-03-25 05:00:19 -07:00
int set_bval = bval + 1;
2021-06-26 16:29:51 -07:00
if(set_bval > 100) {
set_bval = 100;
}
2021-06-25 19:09:42 -07:00
pre_set_brightness(set_bval);
2021-03-25 05:00:19 -07:00
brightness_writeconfig(set_bval);
2021-06-26 07:09:51 -07:00
ui->brightnessStatus->setValue(set_bval);
2021-03-25 05:00:19 -07:00
}
void reader::on_aboutBtn_clicked()
{
2022-04-16 20:20:24 -07:00
log("Showing About message box", className);
2021-03-25 05:00:19 -07:00
if(checkconfig("/opt/inkbox_genuine") == true) {
QString aboutmsg = "InkBox is an open-source, Qt-based eBook reader. It aims to bring you the latest Qt features while being also fast and responsive.";
aboutmsg.prepend("<font face='u001'>");
2021-03-25 05:00:19 -07:00
string_checkconfig_ro("/external_root/opt/isa/version");
aboutmsg.append("<br><br>InkBox ");
2021-03-25 05:00:19 -07:00
aboutmsg.append(checkconfig_str_val);
aboutmsg.append("</font>");
2021-03-25 05:00:19 -07:00
QMessageBox::information(this, tr("Information"), aboutmsg);
}
else {
2021-06-05 15:54:38 -07:00
QMessageBox::information(this, tr("About"), tr("InkBox is an open-source Qt-based eBook reader. It aims to bring you the latest Qt features while being also fast and responsive."));
2021-03-25 05:00:19 -07:00
}
}
void reader::on_homeBtn_clicked()
{
2022-04-16 20:20:24 -07:00
log("Returning to Home screen", className);
// We're leaving reading mode
string_writeconfig("/tmp/inkboxReading", "false");
2021-06-28 21:55:44 -07:00
// Remount tmpfs
string_writeconfig("/inkbox/remount", "true");
// Specify cinematic brightness mode
string_writeconfig("/tmp/inkbox-cinematic_brightness_auto", "true");
// Relaunching process
quit_restart();
2021-03-25 05:00:19 -07:00
}
void reader::on_fontChooser_currentIndexChanged(const QString &arg1)
{
2022-04-16 20:20:24 -07:00
log("Setting font to '" + arg1 + "'", className);
2021-03-25 05:00:19 -07:00
if(arg1 == "Roboto") {
QFont roboto("Roboto");
ui->text->setFont(roboto);
string_writeconfig(".config/04-book/font", "Roboto");
}
if(arg1 == "Inter") {
QFont inter("Inter");
ui->text->setFont(inter);
string_writeconfig(".config/04-book/font", "Inter");
}
if(arg1 == "Source Serif Pro") {
QFont sourceserif("Source Serif Pro");
ui->text->setFont(sourceserif);
string_writeconfig(".config/04-book/font", "Source Serif Pro");
}
if(arg1 == "Libre Baskerville") {
QFont librebaskerville("Libre Baskerville");
ui->text->setFont(librebaskerville);
string_writeconfig(".config/04-book/font", "Libre Baskerville");
}
if(arg1 == "Noto Mono") {
QFont notomono("Noto Mono");
ui->text->setFont(notomono);
string_writeconfig(".config/04-book/font", "Noto Mono");
}
if(arg1 == "Libertinus Serif") {
QFont libertinus("Libertinus Serif");
ui->text->setFont(libertinus);
string_writeconfig(".config/04-book/font", "Libertinus Serif");
}
if(arg1 == "Crimson Pro") {
// As adding Crimson Pro to the default fonts bundled along with the Qt libs breaks the general u001/Inter homogeneity, it is incorporated on-demand here.
2021-07-11 19:11:13 -07:00
setCrimsonProFont();
string_writeconfig(".config/04-book/font", "Crimson Pro");
}
if(arg1 == "Bitter") {
2021-07-11 19:11:13 -07:00
setBitterFont();
string_writeconfig(".config/04-book/font", "Bitter");
}
2021-07-11 19:11:13 -07:00
if(arg1 == "Ibarra Real Nova") {
setIbarraFont();
string_writeconfig(".config/04-book/font", "Ibarra Real Nova");
}
if(arg1 == "Univers (u001)") {
ui->text->setFont(QFont("u001"));
string_writeconfig(".config/04-book/font", "u001");
}
2021-03-25 05:00:19 -07:00
}
void reader::on_alignLeftBtn_clicked()
{
2022-04-16 20:20:24 -07:00
log("Setting text alignment to 'Left'", className);
if(is_epub != true) {
ui->text->setAlignment(Qt::AlignLeft);
}
else {
alignText(0);
}
2021-03-25 05:00:19 -07:00
string_writeconfig(".config/04-book/alignment", "Left");
}
void reader::on_alignCenterBtn_clicked()
{
2022-04-16 20:20:24 -07:00
log("Setting text alignment to 'Center'", className);
if(is_epub != true) {
ui->text->setAlignment(Qt::AlignHCenter);
}
else {
alignText(1);
}
2021-03-25 05:00:19 -07:00
string_writeconfig(".config/04-book/alignment", "Center");
}
void reader::on_alignRightBtn_clicked()
{
2022-04-16 20:20:24 -07:00
log("Setting text alignment to 'Right'", className);
if(is_epub != true) {
ui->text->setAlignment(Qt::AlignRight);
}
else {
alignText(2);
}
2021-03-25 05:00:19 -07:00
string_writeconfig(".config/04-book/alignment", "Right");
}
void reader::on_alignJustifyBtn_clicked()
{
2022-04-16 20:20:24 -07:00
log("Setting text alignment to 'Justify'", className);
if(is_epub != true) {
ui->text->setAlignment(Qt::AlignJustify);
}
else {
alignText(3);
}
2021-03-25 05:00:19 -07:00
string_writeconfig(".config/04-book/alignment", "Justify");
}
void reader::alignText(int alignment) {
/*
* 0 - Left
* 1 - Center
* 2 - Right
* 3 - Justify
*/
textAlignment = alignment;
if(is_epub == true) {
if(alignment == 0) {
QString epubPageContent_alignChange = epubPageContent;
epubPageContent_alignChange.prepend("<div align='left'>");
epubPageContent_alignChange.append("</div>");
ui->text->setText(epubPageContent_alignChange);
}
if(alignment == 1) {
QString epubPageContent_alignChange = epubPageContent;
epubPageContent_alignChange.prepend("<div align='center'>");
epubPageContent_alignChange.append("</div>");
ui->text->setText(epubPageContent_alignChange);
}
if(alignment == 2) {
QString epubPageContent_alignChange = epubPageContent;
epubPageContent_alignChange.prepend("<div align='right'>");
epubPageContent_alignChange.append("</div>");
ui->text->setText(epubPageContent_alignChange);
}
if(alignment == 3) {
QString epubPageContent_alignChange = epubPageContent;
epubPageContent_alignChange.prepend("<div align='justify'>");
epubPageContent_alignChange.append("</div>");
ui->text->setText(epubPageContent_alignChange);
}
}
else {
if(alignment == 0) {
QString ittext_alignChange = ittext;
ittext_alignChange.prepend("<div align='left'>");
ittext_alignChange.append("</div>");
ui->text->setText(ittext_alignChange);
}
if(alignment == 1) {
QString ittext_alignChange = ittext;
ittext_alignChange.prepend("<div align='center'>");
ittext_alignChange.append("</div>");
ui->text->setText(ittext_alignChange);
}
if(alignment == 2) {
QString ittext_alignChange = ittext;
ittext_alignChange.prepend("<div align='right'>");
ittext_alignChange.append("</div>");
ui->text->setText(ittext_alignChange);
}
if(alignment == 3) {
QString ittext_alignChange = ittext;
ittext_alignChange.prepend("<div align='justify'>");
ittext_alignChange.append("</div>");
ui->text->setText(ittext_alignChange);
}
}
}
2021-03-25 05:00:19 -07:00
void reader::menubar_show() {
2022-04-16 20:20:24 -07:00
log("Showing menu bar", className);
2021-03-25 05:00:19 -07:00
// Checking battery level and status, then displaying the relevant icon on batteryIconLabel
2022-02-15 19:47:10 -08:00
if(isUsbPluggedIn() == true) {
2021-03-25 05:00:19 -07:00
ui->batteryIconLabel->setPixmap(scaledChargingPixmap);
}
else {
get_battery_level();
if(batt_level_int >= 75 && batt_level_int <= 100) {
ui->batteryIconLabel->setPixmap(scaledFullPixmap);
}
if(batt_level_int >= 25 && batt_level_int <= 74) {
ui->batteryIconLabel->setPixmap(scaledHalfPixmap);
}
if(batt_level_int >= 0 && batt_level_int <= 24) {
ui->batteryIconLabel->setPixmap(scaledEmptyPixmap);
}
}
ui->menuWidget->setVisible(true);
2022-01-09 08:06:22 -08:00
if(is_pdf == false && is_image == false) {
2021-08-29 12:05:07 -07:00
ui->menuBarWidget->setVisible(true);
}
2021-09-01 19:07:30 -07:00
else {
2022-01-09 08:06:22 -08:00
if(is_pdf == true) {
ui->pdfScaleWidget->setVisible(true);
}
2021-09-01 19:07:30 -07:00
ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
}
2021-08-29 12:05:07 -07:00
ui->buttonsBarWidget->setVisible(true);
ui->statusBarWidget->setVisible(true);
if(is_image == true) {
ui->pageWidget->setVisible(false);
}
else {
ui->pageWidget->setVisible(true);
}
2022-05-22 12:38:02 -07:00
if(global::deviceID == "n705\n" or global::deviceID == "n905\n" or global::deviceID == "kt\n") {
;
}
else {
ui->brightnessWidget->setVisible(true);
}
2021-03-25 05:00:19 -07:00
menubar_shown = true;
}
void reader::menubar_hide() {
2022-04-16 20:20:24 -07:00
log("Hiding menu bar", className);
2022-05-22 12:38:02 -07:00
if(global::deviceID == "n705\n" or global::deviceID == "n905\n" or global::deviceID == "kt\n") {
ui->brightnessWidget->setVisible(false);
}
else {
// Safety measure
ui->brightnessWidget->setVisible(false);
}
2022-01-09 08:06:22 -08:00
if(is_pdf == false && is_image == false) {
2021-08-29 12:05:07 -07:00
ui->menuBarWidget->setVisible(false);
}
2021-09-01 19:07:30 -07:00
else {
ui->pdfScaleWidget->setVisible(false);
if(checkconfig(".config/14-reader_scrollbar/config") == true) {
ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
}
else {
ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
}
2021-08-29 12:05:07 -07:00
ui->buttonsBarWidget->setVisible(false);
ui->pageWidget->setVisible(false);
if(checkconfig(".config/11-menubar/sticky") == true) {
ui->statusBarWidget->setVisible(true);
}
else {
ui->statusBarWidget->setVisible(false);
ui->menuWidget->setVisible(false);
}
2021-03-25 05:00:19 -07:00
menubar_shown = false;
}
void reader::wordwidget_show() {
2022-04-16 20:20:24 -07:00
log("Showing word widget", className);
2021-03-25 05:00:19 -07:00
if(menubar_shown == true) {
menubar_hide();
ui->optionsBtn->hide();
ui->line->hide();
ui->wordWidget->setVisible(true);
2021-03-25 05:00:19 -07:00
}
else {
ui->optionsBtn->hide();
ui->line->hide();
ui->wordWidget->setVisible(true);
2021-03-25 05:00:19 -07:00
}
}
void reader::wordwidget_hide() {
2022-04-16 20:20:24 -07:00
log("Hiding word widget", className);
ui->wordWidget->setVisible(false);
ui->optionsBtn->setStyleSheet("background: white; color: black");
ui->optionsBtn->show();
ui->line->show();
wordwidgetLock = false;
2021-03-25 05:00:19 -07:00
}
void reader::on_infoCloseBtn_clicked()
{
wordwidget_hide();
dictionary_position = 1;
QString dictionary_position_str = QString::number(dictionary_position);
ui->definitionStatusLabel->setText(dictionary_position_str);
QTextCursor cursor = ui->text->textCursor();
cursor.clearSelection();
ui->text->setTextCursor(cursor);
2021-03-25 05:00:19 -07:00
}
void reader::on_previousDefinitionBtn_clicked()
{
dictionary_position = dictionary_position - 1;
if(dictionary_position <= 0) {
dictionary_position = 1;
}
else {
dictionary_lookup(selected_text_str, letter, dictionary_position);
ui->definitionLabel->setText(definition);
QString dictionary_position_str = QString::number(dictionary_position);
ui->definitionStatusLabel->setText(dictionary_position_str);
}
}
void reader::on_nextDefinitionBtn_clicked()
{
dictionary_position = dictionary_position + 1;
dictionary_lookup(selected_text_str, letter, dictionary_position);
if(nextdefinition_lock == true) {
dictionary_position = dictionary_position - 1;
}
else {
ui->definitionLabel->setText(definition);
QString dictionary_position_str = QString::number(dictionary_position);
ui->definitionStatusLabel->setText(dictionary_position_str);
}
}
void reader::on_saveWordBtn_clicked()
{
if(checkconfig_match(".config/06-words/config", selected_text_str) == true) {
2022-04-16 20:20:24 -07:00
log("Removing word '" + QString::fromStdString(selected_text_str) + "' from Saved Words list", className);
2021-03-25 05:00:19 -07:00
checkwords();
word = word.append("\n");
words = words.replace(word, "");
string words_std_string = words.toStdString();
save_word(words_std_string, true);
ui->saveWordBtn->setText("");
ui->saveWordBtn->setIcon(QIcon(":/resources/star.png"));
}
else {
save_word(selected_text_str, false);
ui->saveWordBtn->setText("");
ui->saveWordBtn->setIcon(QIcon(":/resources/starred_star.png"));
}
}
void reader::on_sizeSlider_valueChanged(int value)
{
// Font size
string value_str = to_string(value);
string_writeconfig(".config/04-book/size", value_str);
2022-04-16 20:20:24 -07:00
if(global::deviceID == "n705\n") {
2021-03-25 05:00:19 -07:00
if(value == 0) {
ui->text->setStyleSheet("font-size: 6pt");
ui->sizeValueLabel->setText("1");
}
if(value == 1) {
ui->text->setStyleSheet("font-size: 10pt");
ui->sizeValueLabel->setText("2");
}
if(value == 2) {
ui->text->setStyleSheet("font-size: 14pt");
ui->sizeValueLabel->setText("3");
}
if(value == 3) {
ui->text->setStyleSheet("font-size: 18pt");
ui->sizeValueLabel->setText("4");
}
if(value == 4) {
ui->text->setStyleSheet("font-size: 22pt");
ui->sizeValueLabel->setText("5");
}
}
2022-05-22 12:38:02 -07:00
if(global::deviceID == "n905\n" or global::deviceID == "kt\n") {
2021-04-09 17:54:20 -07:00
if(value == 0) {
ui->text->setStyleSheet("font-size: 6pt");
ui->sizeValueLabel->setText("1");
}
if(value == 1) {
ui->text->setStyleSheet("font-size: 10pt");
ui->sizeValueLabel->setText("2");
}
if(value == 2) {
ui->text->setStyleSheet("font-size: 14pt");
ui->sizeValueLabel->setText("3");
}
if(value == 3) {
ui->text->setStyleSheet("font-size: 18pt");
ui->sizeValueLabel->setText("4");
}
if(value == 4) {
ui->text->setStyleSheet("font-size: 22pt");
ui->sizeValueLabel->setText("5");
}
}
2021-03-25 05:00:19 -07:00
else {
if(value == 0) {
ui->text->setStyleSheet("font-size: 6pt");
ui->sizeValueLabel->setText("1");
}
if(value == 1) {
ui->text->setStyleSheet("font-size: 10pt");
ui->sizeValueLabel->setText("2");
}
if(value == 2) {
ui->text->setStyleSheet("font-size: 14pt");
ui->sizeValueLabel->setText("3");
}
if(value == 3) {
ui->text->setStyleSheet("font-size: 18pt");
ui->sizeValueLabel->setText("4");
}
if(value == 4) {
ui->text->setStyleSheet("font-size: 22pt");
ui->sizeValueLabel->setText("5");
}
}
}
2021-08-19 09:55:12 -07:00
void reader::writeconfig_pagenumber(bool persistent) {
// Saving the page number in tmpfs and in persistent storage if requested
if(is_epub == true) {
2021-08-29 12:05:07 -07:00
std::string epubPageNumber_str = std::to_string(mupdf::epub::epubPageNumber);
2021-06-17 05:34:00 -07:00
string_writeconfig("/tmp/inkboxPageNumber", epubPageNumber_str);
2021-08-19 09:55:12 -07:00
if(persistent == true) {
2022-04-16 20:20:24 -07:00
log("Writing page number config for page '" + QString::number(mupdf::epub::epubPageNumber) + "'", className);
epubPageNumber_str.append("\n");
2021-08-19 09:55:12 -07:00
string_writeconfig(".config/A-page_number/config", epubPageNumber_str);
}
2021-06-17 05:34:00 -07:00
}
else if(is_pdf == true) {
std::string pdfPageNumber_str = std::to_string(mupdf::pdf::pdfPageNumber);
string_writeconfig("/tmp/inkboxPageNumber", pdfPageNumber_str);
if(persistent == true) {
2022-04-16 20:20:24 -07:00
log("Writing page number config for page '" + QString::number(mupdf::pdf::pdfPageNumber) + "'", className);
pdfPageNumber_str.append("\n");
string_writeconfig(".config/A-page_number/config", pdfPageNumber_str);
}
}
else {
std::string split_total_str = std::to_string(split_total);
string_writeconfig("/tmp/inkboxPageNumber", split_total_str);
if(persistent == true) {
2022-04-16 20:20:24 -07:00
log("Writing page number config for split total '" + QString::number(split_total) + "'", className);
split_total_str.append("\n");
string_writeconfig(".config/A-page_number/config", split_total_str);
}
}
}
void reader::quit_restart() {
2022-04-16 20:20:24 -07:00
log("Restarting InkBox", className);
2021-08-19 16:03:52 -07:00
// Saving current page number
saveReadingSettings();
2021-08-05 21:48:49 -07:00
// Cleaning bookconfig_mount mountpoint
string_writeconfig("/opt/ibxd", "bookconfig_unmount\n");
// Restarting InkBox
QProcess process;
process.startDetached("inkbox", QStringList());
qApp->quit();
}
void reader::openLowBatteryDialog() {
2022-04-16 20:20:24 -07:00
log("Showing low battery dialog", className);
global::mainwindow::lowBatteryDialog = true;
global::battery::batteryAlertLock = true;
generalDialogWindow = new generalDialog(this);
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
QApplication::processEvents();
}
void reader::openCriticalBatteryAlertWindow() {
2022-04-16 20:20:24 -07:00
log("Showing critical battery alert splash", className);
global::battery::showCriticalBatteryAlert = true;
global::battery::showLowBatteryDialog = false;
alertWindow = new alert();
alertWindow->setAttribute(Qt::WA_DeleteOnClose);
alertWindow->setGeometry(QRect(QPoint(0,0), screen()->geometry ().size()));
alertWindow->show();
}
void reader::convertMuPdfVars(int fileType, bool convertAll) {
2021-08-29 12:05:07 -07:00
/* fileType can be:
* 0: ePUB
* 1: PDF
2022-01-09 08:06:22 -08:00
* 2: Image
2021-08-29 12:05:07 -07:00
*/
if(fileType == 0) {
if(convertAll == 1) {
setPageStyle(0);
mupdf::epub::fontSize = 12;
mupdf::epub::fontSize_qstr = QString::number(mupdf::epub::fontSize);
mupdf::epub::width_qstr = QString::number(mupdf::epub::width);
mupdf::epub::height_qstr = QString::number(mupdf::epub::height);
}
2021-08-29 12:05:07 -07:00
if(global::reader::globalReadingSettings == false) {
if(goToSavedPageDone == false) {
mupdf::epub::epubPageNumber = readFile(".config/A-page_number/config").toInt();
2021-08-29 12:05:07 -07:00
goToSavedPageDone = true;
}
}
if(mupdf::epub::epubPageNumber <= 0) {
mupdf::epub::epubPageNumber = 1;
2021-08-19 16:03:52 -07:00
}
2021-08-29 12:05:07 -07:00
mupdf::epub::epubPageNumber_qstr = QString::number(mupdf::epub::epubPageNumber);
2021-08-19 16:03:52 -07:00
}
2021-08-29 12:05:07 -07:00
else if(fileType == 1) {
setPageStyle(1);
mupdf::pdf::width = defaultPdfPageWidth;
mupdf::pdf::height = defaultPdfPageHeight;
2021-09-04 10:46:39 -07:00
if(mupdf::convertRelativeValues == true) {
2021-09-01 19:07:30 -07:00
// For scaling
2021-09-04 10:46:39 -07:00
mupdf::pdf::width_qstr = QString::number(mupdf::pdf::relativeWidth);
mupdf::pdf::height_qstr = QString::number(mupdf::pdf::relativeHeight);
2021-09-01 19:07:30 -07:00
}
else {
// Default
mupdf::pdf::width_qstr = QString::number(mupdf::pdf::width);
mupdf::pdf::height_qstr = QString::number(mupdf::pdf::height);
}
2021-08-29 12:05:07 -07:00
if(global::reader::globalReadingSettings == false) {
if(goToSavedPageDone == false) {
string_checkconfig_ro(".config/A-page_number/config");
mupdf::pdf::pdfPageNumber = checkconfig_str_val.toInt();
goToSavedPageDone = true;
}
}
if(mupdf::pdf::pdfPageNumber <= 0) {
mupdf::pdf::pdfPageNumber = 1;
}
mupdf::pdf::pdfPageNumber_qstr = QString::number(mupdf::pdf::pdfPageNumber);
}
2022-01-09 08:06:22 -08:00
else if(fileType == 2) {
;
}
2021-09-04 10:46:39 -07:00
if(mupdf::convertRelativeValues == true) {
mupdf::convertRelativeValues = false;
}
2021-06-17 05:34:00 -07:00
}
2021-08-29 12:05:07 -07:00
void reader::setPageStyle(int fileType) {
/* fileType can be:
* 0: ePUB
* 1: PDF
2022-01-09 08:06:22 -08:00
* 2: Image
2021-08-29 12:05:07 -07:00
*/
if(fileType == 0) {
// General page size
defineDefaultPageSize(0);
string_checkconfig_ro(".config/13-epub_page_size/width");
2021-08-29 12:05:07 -07:00
if(checkconfig_str_val != "") {
;
}
else {
std::string pageWidth = std::to_string(defaultEpubPageWidth);
string_writeconfig(".config/13-epub_page_size/width", pageWidth);
string_writeconfig(".config/13-epub_page_size/set", "true");
string_checkconfig_ro(".config/13-epub_page_size/width");
}
mupdf::epub::width = checkconfig_str_val.toInt();
string_checkconfig_ro(".config/13-epub_page_size/height");
2021-08-29 12:05:07 -07:00
if(checkconfig_str_val != "") {
;
}
else {
std::string pageHeight = std::to_string(defaultEpubPageHeight);
string_writeconfig(".config/13-epub_page_size/height", pageHeight);
string_writeconfig(".config/13-epub_page_size/set", "true");
string_checkconfig_ro(".config/13-epub_page_size/height");
}
mupdf::epub::height = checkconfig_str_val.toInt();
}
else if(fileType == 1) {
defineDefaultPageSize(1);
}
}
2021-09-04 10:46:39 -07:00
void reader::delay(int mseconds) {
// https://stackoverflow.com/questions/3752742/how-do-i-create-a-pause-wait-function-using-qt
2022-04-16 20:20:24 -07:00
QTime dieTime = QTime::currentTime().addMSecs(mseconds);
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
void reader::on_text_selectionChanged() {
2021-09-04 10:46:39 -07:00
delay(100);
if(wordwidgetLock != true) {
QTextCursor cursor = ui->text->textCursor();
selected_text = cursor.selectedText();
if(selected_text != "") {
2022-04-16 20:20:24 -07:00
log("Text selection changed; selected text: '" + selected_text + "'", className);
QString dictionary_position_str = QString::number(dictionary_position);
ui->definitionStatusLabel->setText(dictionary_position_str);
selected_text = selected_text.toLower();
QStringList parts = selected_text.split(' ', QString::SkipEmptyParts);
for (int i = 0; i < parts.size(); ++i)
parts[i].replace(0, 1, parts[i][0].toUpper());
word = parts.join(" ");
letter = word.left(1);
selected_text_str = word.toStdString();
dictionary_lookup(selected_text_str, letter, dictionary_position);
ui->wordLabel->setText(word);
ui->definitionLabel->setText(definition);
if(checkconfig_match(".config/06-words/config", selected_text_str) == true) {
ui->saveWordBtn->setText("");
ui->saveWordBtn->setIcon(QIcon(":/resources/starred_star.png"));
}
else {
ui->saveWordBtn->setText("");
ui->saveWordBtn->setIcon(QIcon(":/resources/star.png"));
}
wordwidgetLock = true;
wordwidget_show();
}
else {
;
}
}
}
void reader::on_nightModeBtn_clicked()
{
if(isNightModeActive == true) {
// Disabling night/dark mode
2022-04-16 20:20:24 -07:00
log("Setting night mode to OFF", className);
string_writeconfig("/tmp/invertScreen", "n");
string_writeconfig(".config/10-dark_mode/config", "false");
ui->nightModeBtn->setIcon(QIcon(":/resources/nightmode-empty.png"));
isNightModeActive = false;
}
else {
// Enabling night/dark mode
2022-04-16 20:20:24 -07:00
log("Setting night mode to ON", className);
string_writeconfig("/tmp/invertScreen", "y");
string_writeconfig(".config/10-dark_mode/config", "true");
ui->nightModeBtn->setIcon(QIcon(":/resources/nightmode-full.png"));
isNightModeActive = true;
}
}
2021-06-29 09:26:30 -07:00
void reader::openUsbmsDialog() {
2022-04-16 20:20:24 -07:00
log("Showing USBMS dialog", className);
2021-06-29 09:26:30 -07:00
global::usbms::showUsbmsDialog = false;
global::usbms::usbmsDialog = true;
generalDialogWindow = new generalDialog(this);
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
QApplication::processEvents();
}
QString reader::setPageNumberLabelContent() {
QString content;
if(is_epub == true) {
QString pageNumber;
QString totalPages;
2021-08-29 12:05:07 -07:00
pageNumberInt = mupdf::epub::epubPageNumber;
pageNumber = QString::number(pageNumberInt);
totalPages = QString::number(totalPagesInt);
content.append(pageNumber);
content.append(" <i>of</i> ");
content.append(totalPages);
}
else if(is_pdf == true) {
QString pageNumber;
QString totalPages;
pageNumberInt = mupdf::pdf::pdfPageNumber;
pageNumber = QString::number(pageNumberInt);
totalPages = QString::number(totalPagesInt);
content.append(pageNumber);
content.append(" <i>of</i> ");
content.append(totalPages);
}
else {
QString pageNumber;
QString totalPages;
pageNumberInt = split_files_number - split_total;
totalPagesInt = split_files_number - 2;
pageNumber = QString::number(pageNumberInt);
totalPages = QString::number(totalPagesInt);
content.append(pageNumber);
content.append(" <i>of</i> ");
content.append(totalPages);
}
return content;
}
void reader::setupPageWidget() {
QString pageNumberInfoLabelContent = setPageNumberLabelContent();
ui->pageNumberLabel->setText(pageNumberInfoLabelContent);
ui->pageProgressBar->setMaximum(totalPagesInt);
ui->pageProgressBar->setMinimum(1);
ui->pageProgressBar->setValue(pageNumberInt);
}
void reader::getTotalEpubPagesNumber() {
QString epubProg ("sh");
QStringList epubArgs;
convertMuPdfVars(0, true);
2021-08-29 12:05:07 -07:00
epubArgs << "/mnt/onboard/.adds/inkbox/epub.sh" << mupdf::epub::fontSize_qstr << mupdf::epub::width_qstr << mupdf::epub::height_qstr << mupdf::epub::epubPageNumber_qstr << "get_pages_number";
QProcess *epubProc = new QProcess();
epubProc->start(epubProg, epubArgs);
epubProc->waitForFinished();
2021-08-29 12:05:07 -07:00
epubProc->deleteLater();
string_checkconfig_ro("/run/epub_total_pages_number");
totalPagesInt = checkconfig_str_val.toInt();
2022-04-16 20:20:24 -07:00
log("ePUB total pages number: " + checkconfig_str_val, className);
QFile::remove("/run/epub_total_pages_number");
}
void reader::on_gotoBtn_clicked()
{
2022-04-16 20:20:24 -07:00
log("Showing 'Go to page' dialog", className);
global::keyboard::keypadDialog = true;
generalDialogWindow = new generalDialog();
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
connect(generalDialogWindow, SIGNAL(gotoPageSelected(int)), SLOT(gotoPage(int)));
}
void reader::gotoPage(int pageNumber) {
2022-04-16 20:20:24 -07:00
log("Going to page " + QString::number(pageNumber), className);
if(is_epub == true) {
if(pageNumber > totalPagesInt or pageNumber < 1) {
showToast("Request is beyond page range");
}
else {
2021-08-29 12:05:07 -07:00
mupdf::epub::epubPageNumber = pageNumber;
setup_book(book_file, mupdf::epub::epubPageNumber, true);
ui->text->setText("");
ui->text->setText(epubPageContent);
2021-08-29 12:05:07 -07:00
pagesTurned = 0;
writeconfig_pagenumber(false);
alignText(textAlignment);
}
}
else if(is_pdf == true) {
if(pageNumber > totalPagesInt or pageNumber < 1) {
showToast("Request is beyond page range");
}
else {
mupdf::pdf::pdfPageNumber = pageNumber;
setup_book(book_file, mupdf::pdf::pdfPageNumber, true);
setupPng();
pagesTurned = 0;
2021-08-19 09:55:12 -07:00
writeconfig_pagenumber(false);
}
}
else {
if(split_files_number - pageNumber < 2 or split_files_number - pageNumber > split_files_number - 1) {
showToast("You've reached the end of the document");
}
else {
split_total = split_files_number - pageNumber;
setup_book(book_file, split_total, false);
ui->text->setText("");
ui->text->setText(ittext);
pagesTurned = 0;
2021-08-19 09:55:12 -07:00
writeconfig_pagenumber(false);
2021-08-29 12:05:07 -07:00
alignText(textAlignment);
}
}
setupPageWidget();
refreshScreen();
}
2021-07-06 17:06:40 -07:00
void reader::on_searchBtn_clicked()
{
global::forbidOpenSearchDialog = false;
setupSearchDialog();
}
void reader::setupSearchDialog() {
if(global::forbidOpenSearchDialog == false) {
2022-04-16 20:20:24 -07:00
log("Launching Search dialog", className);
2021-07-06 17:06:40 -07:00
global::keyboard::keyboardDialog = true;
global::keyboard::searchDialog = true;
global::keyboard::keyboardText = "";
generalDialogWindow = new generalDialog();
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
connect(generalDialogWindow, SIGNAL(refreshScreen()), SLOT(searchRefreshScreen()));
connect(generalDialogWindow, SIGNAL(destroyed(QObject*)), SLOT(setupSearchDialog()));
connect(generalDialogWindow, SIGNAL(openBookFile(QString, bool)), SLOT(openBookFileNative(QString, bool)));
connect(generalDialogWindow, SIGNAL(showToast(QString)), SLOT(showToast(QString)));
connect(generalDialogWindow, SIGNAL(closeIndefiniteToast()), SLOT(closeIndefiniteToast()));
2021-07-06 17:06:40 -07:00
generalDialogWindow->show();
}
else {
;
}
}
2021-07-11 19:11:13 -07:00
void reader::setBitterFont() {
QString family;
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/Bitter-Medium.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/Bitter-MediumItalic.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/Bitter-Bold.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/Bitter-BoldItalic.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
QFont bitter(family);
ui->text->setFont(bitter);
ui->fontChooser->setCurrentText("Bitter");
}
void reader::setCrimsonProFont() {
QString family;
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/CrimsonPro-Regular.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/CrimsonPro-Italic.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/CrimsonPro-Bold.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/CrimsonPro-BoldItalic.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
QFont crimson(family);
ui->text->setFont(crimson);
ui->fontChooser->setCurrentText("Crimson Pro");
}
void reader::setIbarraFont() {
QString family;
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/IbarraRealNova-Medium.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/IbarraRealNova-MediumItalic.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/IbarraRealNova-Bold.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
{
int id = QFontDatabase::addApplicationFont(":/resources/fonts/IbarraRealNova-BoldItalic.ttf");
family = QFontDatabase::applicationFontFamilies(id).at(0);
}
QFont ibarra(family);
ui->text->setFont(ibarra);
ui->fontChooser->setCurrentText("Ibarra Real Nova");
}
2021-07-06 17:06:40 -07:00
void reader::searchRefreshScreen() {
this->repaint();
}
void reader::showToast(QString messageToDisplay) {
global::toast::message = messageToDisplay;
toastWindow = new toast(this);
toastWindow->setAttribute(Qt::WA_DeleteOnClose);
toastWindow->show();
}
2021-08-19 09:55:12 -07:00
void reader::saveReadingSettings() {
writeconfig_pagenumber(true);
}
void reader::setupLocalSettingsEnvironment() {
2022-04-16 20:20:24 -07:00
log("Setting local settings environment up", className);
2021-08-19 09:55:12 -07:00
QString pageNumberDirPath = ".config/A-page_number";
QDir pageNumberDir;
pageNumberDir.mkpath(pageNumberDirPath);
}
2021-08-29 12:05:07 -07:00
void reader::setupPng() {
2022-01-09 08:06:22 -08:00
if(is_pdf == true) {
// Note: Output file is supposed to be '/run/page.png', but somehow mutool puts it in '/run/page1.png'
QPixmap pixmap("/run/page1.png");
QFile::remove("/run/page1.png");
2022-01-09 08:06:22 -08:00
// Initialized above
graphicsScene->clear();
graphicsScene->addPixmap(pixmap);
// Shrinking scene if item is smaller than previous one
QRectF rect = graphicsScene->itemsBoundingRect();
graphicsScene->setSceneRect(rect);
ui->graphicsView->items().clear();
ui->graphicsView->setScene(graphicsScene);
if(global::reader::pdfOrientation == 1) {
if(!initialPdfRotationDone) {
ui->graphicsView->rotate(270);
initialPdfRotationDone = true;
}
}
2022-01-09 08:06:22 -08:00
}
else if(is_image == true) {
QPixmap pixmap("/run/image.png");
QFile::remove("/run/image.png");
2022-01-09 08:06:22 -08:00
// Initialized above
graphicsScene->clear();
graphicsScene->addPixmap(pixmap);
// Shrinking scene if item is smaller than previous one
QRectF rect = graphicsScene->itemsBoundingRect();
graphicsScene->setSceneRect(rect);
ui->graphicsView->items().clear();
ui->graphicsView->setScene(graphicsScene);
}
2021-08-29 12:05:07 -07:00
}
bool reader::pdf_file_match(QString file) {
QString fileExt = file.right(3);
if(fileExt == "pdf" or fileExt == "PDF") {
2022-04-16 20:20:24 -07:00
log("Book file format: PDF", className);
2021-08-29 12:05:07 -07:00
string_writeconfig("/inkbox/bookIsPdf", "true");
return true;
}
else {
string_writeconfig("/inkbox/bookIsPdf", "false");
return false;
}
}
2022-01-09 08:06:22 -08:00
bool reader::image_file_match(QString file) {
2022-01-12 11:48:22 -08:00
if(file.right(3) == "png" or file.right(3) == "PNG" or file.right(3) == "jpg" or file.right(3) == "JPG" or file.right(3) == "bmp" or file.right(3) == "BMP" or file.right(3) == "tif" or file.right(3) == "TIF") {
2022-04-16 20:20:24 -07:00
log("File format: image", className);
2022-01-09 08:06:22 -08:00
string_writeconfig("/inkbox/bookIsImage", "true");
return true;
}
2022-01-12 11:48:22 -08:00
else if(file.right(4) == "jpeg" or file.right(4) == "JPEG" or file.right(4) == "tiff" or file.right(4) == "TIFF") {
2022-04-16 20:20:24 -07:00
log("File format: image", className);
2022-01-09 08:06:22 -08:00
string_writeconfig("/inkbox/bookIsImage", "true");
return true;
}
else {
string_writeconfig("/inkbox/bookIsImage", "false");
return false;
}
}
2021-08-29 12:05:07 -07:00
void reader::getTotalPdfPagesNumber() {
QString epubProg ("sh");
QStringList epubArgs;
convertMuPdfVars(0, true);
2021-08-29 12:05:07 -07:00
epubArgs << "/mnt/onboard/.adds/inkbox/pdf_get_total_pages_number.sh" << book_file;
QProcess * epubProc = new QProcess();
epubProc->start(epubProg, epubArgs);
epubProc->waitForFinished();
epubProc->deleteLater();
string_checkconfig_ro("/run/pdf_total_pages_number");
totalPagesInt = checkconfig_str_val.toInt();
2022-04-16 20:20:24 -07:00
log("Total PDF pages number: " + checkconfig_str_val, className);
QFile::remove("/run/pdf_total_pages_number");
2021-08-29 12:05:07 -07:00
}
2021-09-01 19:07:30 -07:00
void reader::on_pdfScaleSlider_valueChanged(int value)
{
2022-04-16 20:20:24 -07:00
log("Setting PDF scale to " + QString::number(value), className);
2021-09-01 19:07:30 -07:00
if(value == 1) {
mupdf::pdf::relativeWidth = 1 * mupdf::pdf::width;
mupdf::pdf::relativeHeight = 1 * mupdf::pdf::height;
}
else if(value == 2) {
mupdf::pdf::relativeWidth = 1.50 * mupdf::pdf::width;
mupdf::pdf::relativeHeight = 1.50 * mupdf::pdf::height;
}
else if(value == 3) {
mupdf::pdf::relativeWidth = 2 * mupdf::pdf::width;
mupdf::pdf::relativeHeight = 2 * mupdf::pdf::height;
}
else if(value == 4) {
mupdf::pdf::relativeWidth = 2.50 * mupdf::pdf::width;
mupdf::pdf::relativeHeight = 2.50 * mupdf::pdf::height;
}
if(value != 1) {
ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
}
else {
if(checkconfig(".config/14-reader_scrollbar/config") == true) {
ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
}
else {
ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
}
2021-09-04 10:46:39 -07:00
mupdf::convertRelativeValues = true;
2021-09-01 19:07:30 -07:00
setup_book(book_file, mupdf::pdf::pdfPageNumber, true);
setupPng();
}
void reader::on_decreaseScaleBtn_clicked()
{
int sliderCurrentValue;
int sliderWantedValue;
sliderCurrentValue = ui->pdfScaleSlider->value();
sliderWantedValue = sliderCurrentValue - 1;
if(sliderWantedValue < ui->pdfScaleSlider->QAbstractSlider::minimum()) {
showToast("Minimum scale reached");
}
else {
ui->pdfScaleSlider->setValue(sliderWantedValue);
}
}
void reader::on_increaseScaleBtn_clicked()
{
int sliderCurrentValue;
int sliderWantedValue;
sliderCurrentValue = ui->pdfScaleSlider->value();
sliderWantedValue = sliderCurrentValue + 1;
if(sliderWantedValue > ui->pdfScaleSlider->QAbstractSlider::maximum()) {
showToast("Maximum scale reached");
}
else {
ui->pdfScaleSlider->setValue(sliderWantedValue);
}
}
void reader::openBookFileNative(QString book, bool relativePath) {
if(global::runningInstanceIsReaderOnly == false) {
emit openBookFile(book, relativePath);
}
else {
showToast("Not supported");
}
}
void reader::on_quitBtn_clicked()
{
2022-04-16 20:20:24 -07:00
log("Showing Quit window", className);
writeconfig_pagenumber(true);
quitWindow = new quit();
quitWindow->setAttribute(Qt::WA_DeleteOnClose);
quitWindow->showFullScreen();
}
void reader::closeIndefiniteToast() {
// Warning: use with caution
toastWindow->close();
}
void reader::getPdfOrientation(QString file) {
log("Getting viewport orientation for PDF file '" + file + "'", className);
string_writeconfig("/inkbox/pdf_orientation_file_request", file.toStdString());
string_writeconfig("/opt/ibxd", "get_pdf_orientation\n");
while(true) {
if(QFile::exists("/inkbox/pdf_orientation_result")) {
QString result = readFile("/inkbox/pdf_orientation_result").trimmed();
if(result == "Portrait") {
QString function = __func__; log(function + ": Orientation is portrait", className);
global::reader::pdfOrientation = 0;
}
else {
QString function = __func__; log(function + ": Orientation is landscape", className);
global::reader::pdfOrientation = 1;
}
break;
}
}
}