Various fixes; compress RecentBooks.db

This commit is contained in:
Nicolas Mailloux 2022-07-01 18:56:34 -04:00
parent 3be9608a10
commit 35fe0ae0e3
7 changed files with 144 additions and 77 deletions

View file

@ -241,6 +241,10 @@ void generalDialog::on_cancelBtn_clicked()
global::kobox::resetKoboxDialog = false; global::kobox::resetKoboxDialog = false;
generalDialog::close(); generalDialog::close();
} }
else if(keypadDialog == true) {
global::keyboard::keypadDialog = false;
generalDialog::close();
}
else if(global::encfs::disableStorageEncryptionDialog == true) { else if(global::encfs::disableStorageEncryptionDialog == true) {
emit cancelDisableStorageEncryption(); emit cancelDisableStorageEncryption();
global::encfs::disableStorageEncryptionDialog = false; global::encfs::disableStorageEncryptionDialog = false;

View file

@ -101,88 +101,128 @@ void homePageWidget::setupDisplay(bool databaseGenerated) {
QString function = __func__; log(function + ": Failed to open local library database file for reading at '" + database.fileName() + "'", className); QString function = __func__; log(function + ": Failed to open local library database file for reading at '" + database.fileName() + "'", className);
} }
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 databaseJsonObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(data))).object(); QJsonObject databaseJsonObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(data))).object();
QJsonArray databaseJsonArrayList = databaseJsonObject["database"].toArray(); QJsonArray databaseJsonArrayList = databaseJsonObject["database"].toArray();
int databaseBooksNumber = databaseJsonArrayList.size(); int databaseBooksNumber = databaseJsonArrayList.size();
QJsonObject recentBooksJsonObject = QJsonDocument::fromJson(readFile(global::localLibrary::recentBooksDatabasePath).toUtf8()).object(); QJsonObject recentBooksJsonObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(recentBooksData))).object();
log("Setting up home page", className); log("Setting up home page", className);
int in = 0; if(!recentBooksJsonObject["Book1"].toObject().value("BookPath").toString().isEmpty()) {
bool newRow = false; int in = 0;
for(int i = 1; i <= (global::homePageWidget::recentBooksNumber + 1); i++) { bool newRow = false;
if(in < global::homePageWidget::recentBooksRowNumber) { for(int i = 1; i <= (global::homePageWidget::recentBooksNumber + 1); i++) {
QString objectName = "Book" + QString::number(i); if(in < global::homePageWidget::recentBooksRowNumber) {
QJsonObject jsonObject = recentBooksJsonObject[objectName].toObject(); QString objectName = "Book" + QString::number(i);
QString bookPath = jsonObject.value("BookPath").toString(); QJsonObject jsonObject = recentBooksJsonObject[objectName].toObject();
bookBtnArray[i] = new QClickableLabel(this); QString bookPath = jsonObject.value("BookPath").toString();
bookTitleArray[i] = new QToolTipLabel(this); bookBtnArray[i] = new QClickableLabel(this);
bookTitleArray[i] = new QToolTipLabel(this);
// Iterate until we find a book matching the recently opened book's "BookPath" key/value pair // Iterate until we find a book matching the recently opened book's "BookPath" key/value pair
for(int in = 1; in <= databaseBooksNumber; in++) { for(int in = 1; in <= databaseBooksNumber; in++) {
QJsonObject bookJsonObject = databaseJsonArrayList.at(in - 1).toObject(); QJsonObject bookJsonObject = databaseJsonArrayList.at(in - 1).toObject();
if(bookJsonObject["BookPath"].toString() == bookPath) { if(bookJsonObject["BookPath"].toString() == bookPath) {
QByteArray data = qCompress(QJsonDocument(bookJsonObject).toJson()).toBase64(); QByteArray data = qCompress(QJsonDocument(bookJsonObject).toJson()).toBase64();
QString dataString = QString(data); QString dataString = QString(data);
bookBtnArray[i]->setObjectName(dataString); bookBtnArray[i]->setObjectName(dataString);
}
} }
QJsonObject uncompressedJsonObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(bookBtnArray[i]->objectName().toUtf8()))).object();
verticalLayoutArray[i] = new QVBoxLayout();
// Book icon button
QObject::connect(bookBtnArray[i], &QClickableLabel::bookPath, this, &homePageWidget::openBook);
bookBtnArray[i]->setAlignment(Qt::AlignCenter);
bookBtnArray[i]->setFont(QFont("u001"));
bookBtnArray[i]->setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px");
// Book title label
bookTitleArray[i]->setWordWrap(true);
bookTitleArray[i]->setAlignment(Qt::AlignCenter);
bookTitleArray[i]->setFont(QFont("u001"));
bookTitleArray[i]->setStyleSheet("font-size: 7pt");
QString bookTitle = uncompressedJsonObject["Title"].toString();
bookTitleArray[i]->setObjectName(bookTitle);
int localBookTitleTruncateThreshold;
if(!bookTitle.contains(" ")) {
localBookTitleTruncateThreshold = bookTitleTruncateThreshold - 10;
}
else {
localBookTitleTruncateThreshold = bookTitleTruncateThreshold;
}
if(bookTitle.length() > localBookTitleTruncateThreshold) {
bookTitleArray[i]->setProperty("showToolTip", "true");
bookTitle.truncate(localBookTitleTruncateThreshold);
bookTitle.append("...");
}
bookTitleArray[i]->setText(bookTitle);
QString bookIcon = uncompressedJsonObject["CoverPath"].toString();
if(QFile::exists(bookIcon)) {
bookBtnArray[i]->setPixmap(QPixmap(bookIcon).scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
}
else {
bookBtnArray[i]->setPixmap(QPixmap(":/resources/cover_unavailable").scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
}
verticalLayoutArray[i]->addWidget(bookBtnArray[i]);
verticalLayoutArray[i]->addWidget(bookTitleArray[i]);
} }
verticalLayoutArray[i] = new QVBoxLayout(); if(newRow == true) {
newRow = false;
// Book icon button horizontalLayoutArray[in] = new QHBoxLayout();
QObject::connect(bookBtnArray[i], &QClickableLabel::bookPath, this, &homePageWidget::openBook); for(int n = ((i - 1) - (global::homePageWidget::recentBooksNumberPerRow - 1)); n < (((i - 1) - (global::homePageWidget::recentBooksNumberPerRow - 1)) + global::homePageWidget::recentBooksNumberPerRow); n++) {
bookBtnArray[i]->setAlignment(Qt::AlignCenter); horizontalLayoutArray[in]->addLayout(verticalLayoutArray[n]);
bookBtnArray[i]->setFont(QFont("u001")); }
bookBtnArray[i]->setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px"); ui->booksVerticalLayout->addLayout(horizontalLayoutArray[in]);
// Book title label
bookTitleArray[i]->setWordWrap(true);
bookTitleArray[i]->setAlignment(Qt::AlignCenter);
bookTitleArray[i]->setFont(QFont("u001"));
bookTitleArray[i]->setStyleSheet("font-size: 7pt");
QJsonObject uncompressedJsonObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(bookBtnArray[i]->objectName().toUtf8()))).object();
QString bookTitle = uncompressedJsonObject["Title"].toString();
bookTitleArray[i]->setObjectName(bookTitle);
int localBookTitleTruncateThreshold;
if(!bookTitle.contains(" ")) {
localBookTitleTruncateThreshold = bookTitleTruncateThreshold - 10;
} }
else { if(!(i % global::homePageWidget::recentBooksNumberPerRow)) {
localBookTitleTruncateThreshold = bookTitleTruncateThreshold; newRow = true;
in++;
} }
if(bookTitle.length() > localBookTitleTruncateThreshold) {
bookTitle.truncate(localBookTitleTruncateThreshold);
bookTitle.append("...");
}
bookTitleArray[i]->setText(bookTitle);
QString bookIcon = uncompressedJsonObject["CoverPath"].toString();
if(QFile::exists(bookIcon)) {
bookBtnArray[i]->setPixmap(QPixmap(bookIcon).scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
}
else {
bookBtnArray[i]->setPixmap(QPixmap(":/resources/cover_unavailable").scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
}
verticalLayoutArray[i]->addWidget(bookBtnArray[i]);
verticalLayoutArray[i]->addWidget(bookTitleArray[i]);
} }
if(newRow == true) { // Hiding books that don't exist/are not in the database
newRow = false; // If you think this is not an efficient way to do it, try to teach the code above not create the button if the book does not exist and arrange layouts so that it looks linear
horizontalLayoutArray[in] = new QHBoxLayout(); for(int i = 1; i <= global::homePageWidget::recentBooksNumber; i++) {
for(int n = ((i - 1) - (global::homePageWidget::recentBooksNumberPerRow - 1)); n < (((i - 1) - (global::homePageWidget::recentBooksNumberPerRow - 1)) + global::homePageWidget::recentBooksNumberPerRow); n++) { QString bookPath = recentBooksJsonObject["Book" + QString::number(i)].toObject().value("BookPath").toString();
horizontalLayoutArray[in]->addLayout(verticalLayoutArray[n]); if(bookPath.isEmpty()) {
bookBtnArray[i]->hide();
bookTitleArray[i]->hide();
bookBtnArray[i]->deleteLater();
bookTitleArray[i]->deleteLater();
} }
ui->booksVerticalLayout->addLayout(horizontalLayoutArray[in]);
}
if(!(i % global::homePageWidget::recentBooksNumberPerRow)) {
newRow = true;
in++;
} }
} }
else {
log("No books found in recent books database", className);
QFrame * line = new QFrame(this);
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Plain);
line->setLineWidth(1);
ui->booksVerticalLayout->addWidget(line);
QLabel * noRecentBooksLabel = new QLabel(this);
noRecentBooksLabel->setFont(QFont("Chivo"));
noRecentBooksLabel->setAlignment(Qt::AlignCenter);
noRecentBooksLabel->setStyleSheet("padding: 30px");
noRecentBooksLabel->setText("Recently read books will appear here");
ui->booksVerticalLayout->addWidget(noRecentBooksLabel);
}
QTimer::singleShot(500, this, SLOT(refreshScreenNative())); QTimer::singleShot(500, this, SLOT(refreshScreenNative()));
} }

View file

@ -259,17 +259,19 @@ void localLibraryWidget::setupBooksList(int pageNumber) {
// Set boundaries for 'Previous'/'Next' page turn buttons // Set boundaries for 'Previous'/'Next' page turn buttons
currentPageNumber = pageNumber; currentPageNumber = pageNumber;
ui->previousPageBtn->setEnabled(true);
ui->nextPageBtn->setEnabled(true);
if(currentPageNumber - 1 < 1) { if(currentPageNumber - 1 < 1) {
ui->previousPageBtn->setEnabled(false); ui->previousPageBtn->setEnabled(false);
ui->nextPageBtn->setEnabled(true); ui->nextPageBtn->setEnabled(true);
} }
else if(currentPageNumber + 1 > pagesNumber) { if(currentPageNumber + 1 > pagesNumber) {
ui->previousPageBtn->setEnabled(true); ui->previousPageBtn->setEnabled(true);
ui->nextPageBtn->setEnabled(false); ui->nextPageBtn->setEnabled(false);
} }
else { if(currentPageNumber - 1 < 1 and currentPageNumber + 1 > pagesNumber) {
ui->previousPageBtn->setEnabled(true); ui->previousPageBtn->setEnabled(false);
ui->nextPageBtn->setEnabled(true); ui->nextPageBtn->setEnabled(false);
} }
} }
@ -322,6 +324,7 @@ void localLibraryWidget::openGoToPageDialog() {
void localLibraryWidget::goToPage(int page) { void localLibraryWidget::goToPage(int page) {
if(page > pagesNumber or page <= 0) { if(page > pagesNumber or page <= 0) {
log("Page number specified (" + QString::number(page) + ") is out of range", className); log("Page number specified (" + QString::number(page) + ") is out of range", className);
showToast("Request is beyond page range");
} }
else { else {
log("Going to page " + QString::number(page), className); log("Going to page " + QString::number(page), className);

View file

@ -51,9 +51,9 @@
</widget> </widget>
</item> </item>
<item row="0" column="4"> <item row="0" column="4">
<widget class="QClickableLabel" name="pageNumberLabel"> <widget class="QClickableLabel" name="pageNumberLabel" native="true">
<property name="text"> <property name="text" stdset="0">
<string>Page</string> <string/>
</property> </property>
</widget> </widget>
</item> </item>
@ -205,6 +205,13 @@ Add some via USB or explore the Online Library.</string>
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>QClickableLabel</class>
<extends>QWidget</extends>
<header>qclickablelabel.h</header>
</customwidget>
</customwidgets>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>

View file

@ -13,7 +13,7 @@ QClickableLabel::~QClickableLabel() {}
void QClickableLabel::mousePressEvent(QMouseEvent * event) { void QClickableLabel::mousePressEvent(QMouseEvent * event) {
emit clicked(); emit clicked();
if(objectName() == "pageNumberLabel") { if(objectName() == "pageNumberLabel") {
QClickableLabel::setStyleSheet("color: white; background-color: black; border-radius: 10px; padding-left: 10px; padding-right: 10px"); QClickableLabel::setStyleSheet("border-radius: 10px; padding-left: 10px; padding-right: 10px");
} }
else { else {
QClickableLabel::setStyleSheet("color: white; background-color: black; border-radius: 10px; padding: 10px"); QClickableLabel::setStyleSheet("color: white; background-color: black; border-radius: 10px; padding: 10px");
@ -25,7 +25,7 @@ void QClickableLabel::mouseReleaseEvent(QMouseEvent * event) {
emit bookID(objectName().toInt()); emit bookID(objectName().toInt());
emit bookPath(QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(objectName().toUtf8()))).object()["BookPath"].toString()); emit bookPath(QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(objectName().toUtf8()))).object()["BookPath"].toString());
if(objectName() == "pageNumberLabel") { if(objectName() == "pageNumberLabel") {
QClickableLabel::setStyleSheet("color: black; background-color: white; border-radius: 10px; padding-left: 10px; padding-right: 10px"); QClickableLabel::setStyleSheet("border-radius: 10px; padding-left: 10px; padding-right: 10px");
} }
else { else {
QClickableLabel::setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px"); QClickableLabel::setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px");

View file

@ -1,4 +1,5 @@
#include <QToolTip> #include <QToolTip>
#include <QVariant>
#include "qtooltiplabel.h" #include "qtooltiplabel.h"
@ -10,7 +11,9 @@ QToolTipLabel::QToolTipLabel(QWidget* parent, Qt::WindowFlags f)
QToolTipLabel::~QToolTipLabel() {} QToolTipLabel::~QToolTipLabel() {}
void QToolTipLabel::mousePressEvent(QMouseEvent * event) { void QToolTipLabel::mousePressEvent(QMouseEvent * event) {
QToolTip::showText(mapToGlobal(QPoint(0, 0)), "<font face='u001' size=-2>" + objectName() + "</font>"); if(QToolTipLabel::property("showToolTip").toString() == "true") {
QToolTip::showText(mapToGlobal(QPoint(0, 0)), "<font face='u001' size=-2>" + objectName() + "</font>");
}
} }
void QToolTipLabel::mouseReleaseEvent(QMouseEvent * event) { void QToolTipLabel::mouseReleaseEvent(QMouseEvent * event) {

View file

@ -625,7 +625,17 @@ reader::reader(QWidget *parent) :
// Maintain a 'Recent books' list // Maintain a 'Recent books' list
QJsonObject recentBooksObject; QJsonObject recentBooksObject;
if(QFile::exists(global::localLibrary::recentBooksDatabasePath)) { if(QFile::exists(global::localLibrary::recentBooksDatabasePath)) {
QJsonObject mainJsonObject = QJsonDocument::fromJson(readFile(global::localLibrary::recentBooksDatabasePath).toUtf8()).object(); 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();
for(int i = 1; i <= global::homePageWidget::recentBooksNumber; i++) { for(int i = 1; i <= global::homePageWidget::recentBooksNumber; i++) {
QString objectName = "Book" + QString::number(i); QString objectName = "Book" + QString::number(i);
QJsonObject jsonObject = mainJsonObject[objectName].toObject(); QJsonObject jsonObject = mainJsonObject[objectName].toObject();
@ -656,7 +666,7 @@ reader::reader(QWidget *parent) :
recentBooksObject = mainJsonObject; recentBooksObject = mainJsonObject;
} }
QFile::remove(global::localLibrary::recentBooksDatabasePath); QFile::remove(global::localLibrary::recentBooksDatabasePath);
writeFile(global::localLibrary::recentBooksDatabasePath, QString(QJsonDocument(recentBooksObject).toJson())); writeFile(global::localLibrary::recentBooksDatabasePath, qCompress(QJsonDocument(recentBooksObject).toJson()).toBase64());
// USB mass storage prompt // USB mass storage prompt
if(global::reader::startUsbmsPrompt == true) { if(global::reader::startUsbmsPrompt == true) {