Implement auto-sync if user searchs in Online Library

This commit is contained in:
Nicolas Mailloux 2022-01-11 14:17:14 -05:00
parent 2ba3b42599
commit a24b84acf4
4 changed files with 108 additions and 35 deletions

View file

@ -13,6 +13,7 @@
#include <QDirIterator>
#include <QStringListModel>
#include <QListView>
#include <QDateTime>
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();
}

View file

@ -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;

View file

@ -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();
}

View file

@ -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()) {