From a24b84acf40252430e4d0a9dbcafab214856ddce Mon Sep 17 00:00:00 2001 From: Nicolas Mailloux Date: Tue, 11 Jan 2022 14:17:14 -0500 Subject: [PATCH] Implement auto-sync if user searchs in Online Library --- generaldialog.cpp | 135 ++++++++++++++++++++++++++++++---------- generaldialog.h | 5 ++ mainwindow.cpp | 2 + searchresultswidget.cpp | 1 - 4 files changed, 108 insertions(+), 35 deletions(-) diff --git a/generaldialog.cpp b/generaldialog.cpp index e273396..e5c9de1 100644 --- a/generaldialog.cpp +++ b/generaldialog.cpp @@ -13,6 +13,7 @@ #include #include #include +#include generalDialog::generalDialog(QWidget *parent) : QDialog(parent), @@ -39,6 +40,11 @@ generalDialog::generalDialog(QWidget *parent) : ui->bodyLabel->setStyleSheet("font-size: 9pt"); ui->searchComboBox->setStyleSheet("font-size: 9pt"); + // Disabling "Online library" search if device doesn't have Wi-Fi + if(global::device::isWifiAble == false && readFile("/opt/inkbox_device") != "emu\n") { + ui->searchComboBox->removeItem(2); + } + if(QFile::exists("/inkbox/searchComboBoxFunction") == true) { string_checkconfig_ro("/inkbox/searchComboBoxFunction"); if(checkconfig_str_val == "Dictionary") { @@ -356,43 +362,69 @@ void generalDialog::on_okBtn_clicked() } else if(ui->searchComboBox->currentText() == "Online library") { string_writeconfig("/inkbox/searchComboBoxFunction", "Online library"); - string_writeconfig("/inkbox/gutenberg_search_request", global::keyboard::keyboardText.toStdString()); - string_writeconfig("/opt/ibxd", "gutenberg_search\n"); - while(true) { - if(QFile::exists("/inkbox/gutenberg-search/search_done")) { - if(checkconfig("/inkbox/gutenberg-search/search_done") == true) { - QStringList searchResults = readFile("/inkbox/gutenberg-search/search_results_titles").split("\n"); - global::library::libraryResults = true; - for(int i = ui->mainStackedWidget->count(); i >= 0; i--) { - QWidget * widget = ui->mainStackedWidget->widget(i); - ui->mainStackedWidget->removeWidget(widget); - widget->deleteLater(); - } - ui->topStackedWidget->setVisible(false); - ui->stackedWidget->setVisible(false); - searchResultsWidgetWindow = new searchResultsWidget(this); - searchResultsWidgetWindow->setAttribute(Qt::WA_DeleteOnClose); - global::forbidOpenSearchDialog = true; - connect(searchResultsWidgetWindow, SIGNAL(destroyed(QObject*)), SLOT(restartSearchDialog())); - connect(searchResultsWidgetWindow, SIGNAL(showToast(QString)), SLOT(showToastNative(QString))); - connect(searchResultsWidgetWindow, SIGNAL(closeIndefiniteToast()), SLOT(closeIndefiniteToastNative())); - connect(searchResultsWidgetWindow, SIGNAL(hideDialog()), SLOT(hide())); - searchResultsWidgetWindow->setListViewContents(searchResults); - ui->mainStackedWidget->insertWidget(1, searchResultsWidgetWindow); - QFile::remove("/inkbox/gutenberg-search/search_done"); - break; - } - else { - global::toast::delay = 3000; - emit showToast("No results found"); - keyboardWidget->clearLineEdit(); - global::keyboard::keyboardText = ""; - QFile::remove("/inkbox/gutenberg-search/search_done"); - break; - } + if(!readFile("/external_root/opt/storage/gutenberg/last_sync").isEmpty()) { + unsigned long currentEpoch = QDateTime::currentSecsSinceEpoch(); + unsigned long syncEpoch = readFile("/external_root/opt/storage/gutenberg/last_sync").toULong(); + unsigned long allowSyncEpoch = syncEpoch + 86400; + if(currentEpoch > allowSyncEpoch) { + syncCatalog(); + } + else { + noGutenbergSyncToDo = true; } } + else { + syncCatalog(); + } + + QTimer * syncTimer = new QTimer(this); + syncTimer->setInterval(100); + connect(syncTimer, &QTimer::timeout, [&]() { + if(noGutenbergSyncToDo == true or (gutenbergSyncDone == true && gutenbergSyncStatus == true)) { + if(syncTimerDone == false) { + syncTimerDone = true; + string_writeconfig("/inkbox/gutenberg_search_request", global::keyboard::keyboardText.toStdString()); + string_writeconfig("/opt/ibxd", "gutenberg_search\n"); + while(true) { + if(QFile::exists("/inkbox/gutenberg-search/search_done")) { + if(checkconfig("/inkbox/gutenberg-search/search_done") == true) { + QStringList searchResults = readFile("/inkbox/gutenberg-search/search_results_titles").split("\n"); + global::library::libraryResults = true; + + for(int i = ui->mainStackedWidget->count(); i >= 0; i--) { + QWidget * widget = ui->mainStackedWidget->widget(i); + ui->mainStackedWidget->removeWidget(widget); + widget->deleteLater(); + } + ui->topStackedWidget->setVisible(false); + ui->stackedWidget->setVisible(false); + searchResultsWidgetWindow = new searchResultsWidget(this); + searchResultsWidgetWindow->setAttribute(Qt::WA_DeleteOnClose); + global::forbidOpenSearchDialog = true; + connect(searchResultsWidgetWindow, SIGNAL(destroyed(QObject*)), SLOT(restartSearchDialog())); + connect(searchResultsWidgetWindow, SIGNAL(showToast(QString)), SLOT(showToastNative(QString))); + connect(searchResultsWidgetWindow, SIGNAL(closeIndefiniteToast()), SLOT(closeIndefiniteToastNative())); + connect(searchResultsWidgetWindow, SIGNAL(hideDialog()), SLOT(hide())); + searchResultsWidgetWindow->setListViewContents(searchResults); + ui->mainStackedWidget->insertWidget(1, searchResultsWidgetWindow); + QFile::remove("/inkbox/gutenberg-search/search_done"); + break; + } + else { + global::toast::delay = 3000; + emit showToast("No results found"); + keyboardWidget->clearLineEdit(); + global::keyboard::keyboardText = ""; + QFile::remove("/inkbox/gutenberg-search/search_done"); + break; + } + } + } + } + } + } ); + syncTimer->start(); } else { ; @@ -610,3 +642,38 @@ void generalDialog::quit_restart() { process.startDetached("inkbox", QStringList()); qApp->quit(); } + +void generalDialog::syncCatalog() { + global::toast::modalToast = true; + global::toast::indefiniteToast = true; + showToast("Sync in progress"); + + string_writeconfig("/opt/ibxd", "gutenberg_sync\n"); + QTimer * syncCheckTimer = new QTimer(this); + syncCheckTimer->setInterval(100); + connect(syncCheckTimer, &QTimer::timeout, [&]() { + if(QFile::exists("/inkbox/gutenbergSyncDone") == true) { + if(checkconfig("/inkbox/gutenbergSyncDone") == true) { + qDebug() << "Gutenberg sync successfully completed"; + gutenbergSyncDone = true; + gutenbergSyncStatus = true; + emit closeIndefiniteToast(); + } + else { + qDebug() << "Gutenberg sync encountered an error"; + gutenbergSyncDone = true; + gutenbergSyncStatus = false; + emit closeIndefiniteToast(); + showToast("Error"); + global::keyboard::searchDialog = false; + global::forbidOpenSearchDialog = true; + global::keyboard::keyboardDialog = false; + global::keyboard::keyboardText = ""; + global::library::librarySearchDialog = false; + QTimer::singleShot(5000, this, SLOT(close())); + } + QFile::remove("/inkbox/gutenbergSyncDone"); + } + } ); + syncCheckTimer->start(); +} diff --git a/generaldialog.h b/generaldialog.h index 4274592..1447a83 100644 --- a/generaldialog.h +++ b/generaldialog.h @@ -37,6 +37,10 @@ public: bool dictionaryResults = false; bool vncServerSet = false; bool vncPasswordSet = false; + bool gutenbergSyncDone = false; + bool gutenbergSyncStatus = false; + bool noGutenbergSyncToDo = false; + bool syncTimerDone = false; QString vncServerAddress; QString vncServerPassword; QString vncServerPort; @@ -58,6 +62,7 @@ private slots: void showToastNative(QString messageToDisplay); void closeIndefiniteToastNative(); void quit_restart(); + void syncCatalog(); private: Ui::generalDialog *ui; diff --git a/mainwindow.cpp b/mainwindow.cpp index 6775ae2..5486221 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -182,6 +182,8 @@ MainWindow::MainWindow(QWidget *parent) // Deleting/Hiding "Library" button if device is not WiFi-able if(global::device::isWifiAble == false && readFile("/opt/inkbox_device") != "emu\n") { + ui->libraryButton->hide(); + ui->line_10->hide(); ui->libraryButton->deleteLater(); ui->line_10->deleteLater(); } diff --git a/searchresultswidget.cpp b/searchresultswidget.cpp index 56e26d0..8499537 100644 --- a/searchresultswidget.cpp +++ b/searchresultswidget.cpp @@ -40,7 +40,6 @@ void searchResultsWidget::setListViewContents(QStringList searchResults) { void searchResultsWidget::on_openBtn_clicked() { if(libraryResults == true) { - qDebug() << "Got there"; index = ui->listView->currentIndex(); itemText = index.data(Qt::DisplayRole).toString(); if(!itemText.isEmpty()) {