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;
generalDialog::close();
}
else if(keypadDialog == true) {
global::keyboard::keypadDialog = false;
generalDialog::close();
}
else if(global::encfs::disableStorageEncryptionDialog == true) {
emit cancelDisableStorageEncryption();
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);
}
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();
QJsonArray databaseJsonArrayList = databaseJsonObject["database"].toArray();
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);
int in = 0;
bool newRow = false;
for(int i = 1; i <= (global::homePageWidget::recentBooksNumber + 1); i++) {
if(in < global::homePageWidget::recentBooksRowNumber) {
QString objectName = "Book" + QString::number(i);
QJsonObject jsonObject = recentBooksJsonObject[objectName].toObject();
QString bookPath = jsonObject.value("BookPath").toString();
bookBtnArray[i] = new QClickableLabel(this);
bookTitleArray[i] = new QToolTipLabel(this);
if(!recentBooksJsonObject["Book1"].toObject().value("BookPath").toString().isEmpty()) {
int in = 0;
bool newRow = false;
for(int i = 1; i <= (global::homePageWidget::recentBooksNumber + 1); i++) {
if(in < global::homePageWidget::recentBooksRowNumber) {
QString objectName = "Book" + QString::number(i);
QJsonObject jsonObject = recentBooksJsonObject[objectName].toObject();
QString bookPath = jsonObject.value("BookPath").toString();
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
for(int in = 1; in <= databaseBooksNumber; in++) {
QJsonObject bookJsonObject = databaseJsonArrayList.at(in - 1).toObject();
if(bookJsonObject["BookPath"].toString() == bookPath) {
QByteArray data = qCompress(QJsonDocument(bookJsonObject).toJson()).toBase64();
QString dataString = QString(data);
bookBtnArray[i]->setObjectName(dataString);
// Iterate until we find a book matching the recently opened book's "BookPath" key/value pair
for(int in = 1; in <= databaseBooksNumber; in++) {
QJsonObject bookJsonObject = databaseJsonArrayList.at(in - 1).toObject();
if(bookJsonObject["BookPath"].toString() == bookPath) {
QByteArray data = qCompress(QJsonDocument(bookJsonObject).toJson()).toBase64();
QString dataString = QString(data);
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();
// 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");
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;
if(newRow == true) {
newRow = false;
horizontalLayoutArray[in] = new QHBoxLayout();
for(int n = ((i - 1) - (global::homePageWidget::recentBooksNumberPerRow - 1)); n < (((i - 1) - (global::homePageWidget::recentBooksNumberPerRow - 1)) + global::homePageWidget::recentBooksNumberPerRow); n++) {
horizontalLayoutArray[in]->addLayout(verticalLayoutArray[n]);
}
ui->booksVerticalLayout->addLayout(horizontalLayoutArray[in]);
}
else {
localBookTitleTruncateThreshold = bookTitleTruncateThreshold;
if(!(i % global::homePageWidget::recentBooksNumberPerRow)) {
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) {
newRow = false;
horizontalLayoutArray[in] = new QHBoxLayout();
for(int n = ((i - 1) - (global::homePageWidget::recentBooksNumberPerRow - 1)); n < (((i - 1) - (global::homePageWidget::recentBooksNumberPerRow - 1)) + global::homePageWidget::recentBooksNumberPerRow); n++) {
horizontalLayoutArray[in]->addLayout(verticalLayoutArray[n]);
// Hiding books that don't exist/are not in the database
// 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
for(int i = 1; i <= global::homePageWidget::recentBooksNumber; i++) {
QString bookPath = recentBooksJsonObject["Book" + QString::number(i)].toObject().value("BookPath").toString();
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()));
}

View file

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

View file

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

View file

@ -13,7 +13,7 @@ QClickableLabel::~QClickableLabel() {}
void QClickableLabel::mousePressEvent(QMouseEvent * event) {
emit clicked();
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 {
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 bookPath(QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(objectName().toUtf8()))).object()["BookPath"].toString());
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 {
QClickableLabel::setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px");

View file

@ -1,4 +1,5 @@
#include <QToolTip>
#include <QVariant>
#include "qtooltiplabel.h"
@ -10,7 +11,9 @@ QToolTipLabel::QToolTipLabel(QWidget* parent, Qt::WindowFlags f)
QToolTipLabel::~QToolTipLabel() {}
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) {

View file

@ -625,7 +625,17 @@ reader::reader(QWidget *parent) :
// Maintain a 'Recent books' list
QJsonObject recentBooksObject;
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++) {
QString objectName = "Book" + QString::number(i);
QJsonObject jsonObject = mainJsonObject[objectName].toObject();
@ -656,7 +666,7 @@ reader::reader(QWidget *parent) :
recentBooksObject = mainJsonObject;
}
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
if(global::reader::startUsbmsPrompt == true) {