mirror of
https://github.com/YACReader/yacreader
synced 2025-05-28 03:10:27 -04:00
Add class to coordinate automatic library updates
This commit is contained in:
parent
86e0e7be0a
commit
afa9763499
@ -81,6 +81,7 @@ HEADERS += comic_flow.h \
|
||||
db/search_query.h \
|
||||
folder_content_view.h \
|
||||
initial_comic_info_extractor.h \
|
||||
libraries_update_coordinator.h \
|
||||
library_comic_opener.h \
|
||||
library_creator.h \
|
||||
library_window.h \
|
||||
@ -167,6 +168,7 @@ SOURCES += comic_flow.cpp \
|
||||
db/search_query.cpp \
|
||||
folder_content_view.cpp \
|
||||
initial_comic_info_extractor.cpp \
|
||||
libraries_update_coordinator.cpp \
|
||||
library_comic_opener.cpp \
|
||||
library_creator.cpp \
|
||||
library_window.cpp \
|
||||
|
113
YACReaderLibrary/libraries_update_coordinator.cpp
Normal file
113
YACReaderLibrary/libraries_update_coordinator.cpp
Normal file
@ -0,0 +1,113 @@
|
||||
|
||||
#include "libraries_update_coordinator.h"
|
||||
|
||||
#include "library_creator.h"
|
||||
#include "yacreader_libraries.h"
|
||||
#include "yacreader_global.h"
|
||||
|
||||
LibrariesUpdateCoordinator::LibrariesUpdateCoordinator(QSettings *settings, YACReaderLibraries &libraries, QObject *parent)
|
||||
: QObject(parent), libraries(libraries), timer(new QTimer(this))
|
||||
{
|
||||
libraries.load();
|
||||
|
||||
this->settings = settings;
|
||||
|
||||
timer->setInterval(1000 * 60);
|
||||
timer->start();
|
||||
connect(timer, &QTimer::timeout, this, &LibrariesUpdateCoordinator::checkUpdatePolicy);
|
||||
|
||||
elapsedTimer.start();
|
||||
|
||||
if (settings->value(UPDATE_LIBRARIES_AT_STARTUP, false).toBool()) {
|
||||
updateLibraries();
|
||||
}
|
||||
}
|
||||
|
||||
void LibrariesUpdateCoordinator::checkUpdatePolicy()
|
||||
{
|
||||
if (settings->value(UPDATE_LIBRARIES_PERIODICALLY, false).toBool()) {
|
||||
auto variant = settings->value(UPDATE_LIBRARIES_PERIODICALLY_INTERVAL, static_cast<typename std::underlying_type<YACReader::LibrariesUpdateInterval>::type>(YACReader::LibrariesUpdateInterval::Hours2));
|
||||
|
||||
bool itIsDue = false;
|
||||
auto interval = static_cast<YACReader::LibrariesUpdateInterval>(variant.toInt());
|
||||
switch (interval) {
|
||||
case YACReader::LibrariesUpdateInterval::Minutes30:
|
||||
itIsDue = elapsedTimer.elapsed() >= 1000 * 60 * 30;
|
||||
break;
|
||||
case YACReader::LibrariesUpdateInterval::Hourly:
|
||||
itIsDue = elapsedTimer.elapsed() >= 1000 * 60 * 60;
|
||||
break;
|
||||
case YACReader::LibrariesUpdateInterval::Hours2:
|
||||
itIsDue = elapsedTimer.elapsed() >= 1000 * 60 * 60 * 2;
|
||||
break;
|
||||
case YACReader::LibrariesUpdateInterval::Hours4:
|
||||
itIsDue = elapsedTimer.elapsed() >= 1000 * 60 * 60 * 4;
|
||||
break;
|
||||
case YACReader::LibrariesUpdateInterval::Hours8:
|
||||
itIsDue = elapsedTimer.elapsed() >= 1000 * 60 * 60 * 8;
|
||||
break;
|
||||
case YACReader::LibrariesUpdateInterval::Hours12:
|
||||
itIsDue = elapsedTimer.elapsed() >= 1000 * 60 * 60 * 12;
|
||||
break;
|
||||
case YACReader::LibrariesUpdateInterval::Daily:
|
||||
itIsDue = elapsedTimer.elapsed() >= 1000 * 60 * 60 * 24;
|
||||
break;
|
||||
}
|
||||
|
||||
if (itIsDue) {
|
||||
elapsedTimer.restart();
|
||||
updateLibraries();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (settings->value(UPDATE_LIBRARIES_AT_CERTAIN_TIME, false).toBool()) {
|
||||
QTime time = settings->value(UPDATE_LIBRARIES_AT_CERTAIN_TIME_TIME, "00:00").toTime();
|
||||
QTime currentTime = QTime::currentTime();
|
||||
|
||||
if (currentTime.hour() == time.hour() && currentTime.minute() == time.minute()) {
|
||||
updateLibraries();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LibrariesUpdateCoordinator::updateLibraries()
|
||||
{
|
||||
startUpdate();
|
||||
}
|
||||
|
||||
void LibrariesUpdateCoordinator::startUpdate()
|
||||
{
|
||||
if (updateFuture.valid() && updateFuture.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
|
||||
return;
|
||||
}
|
||||
|
||||
updateFuture = std::async(std::launch::async, [this] {
|
||||
emit updateStarted();
|
||||
for (auto library : libraries.getLibraries()) {
|
||||
updateLibrary(library.getPath());
|
||||
}
|
||||
emit updateEnded();
|
||||
});
|
||||
}
|
||||
|
||||
void LibrariesUpdateCoordinator::updateLibrary(const QString &path)
|
||||
{
|
||||
QDir pathDir(path);
|
||||
if (!pathDir.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QEventLoop eventLoop;
|
||||
LibraryCreator *libraryCreator = new LibraryCreator(settings);
|
||||
QString cleanPath = QDir::cleanPath(pathDir.absolutePath());
|
||||
|
||||
libraryCreator->updateLibrary(cleanPath, QDir::cleanPath(pathDir.absolutePath() + "/.yacreaderlibrary"));
|
||||
|
||||
connect(libraryCreator, &LibraryCreator::finished, &eventLoop, &QEventLoop::quit);
|
||||
connect(libraryCreator, &LibraryCreator::finished, libraryCreator, &QObject::deleteLater);
|
||||
|
||||
libraryCreator->start();
|
||||
eventLoop.exec();
|
||||
}
|
34
YACReaderLibrary/libraries_update_coordinator.h
Normal file
34
YACReaderLibrary/libraries_update_coordinator.h
Normal file
@ -0,0 +1,34 @@
|
||||
|
||||
#ifndef LIBRARIES_UPDATE_COORDINATOR_H
|
||||
#define LIBRARIES_UPDATE_COORDINATOR_H
|
||||
|
||||
#include <QtCore>
|
||||
|
||||
class YACReaderLibraries;
|
||||
|
||||
class LibrariesUpdateCoordinator : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
LibrariesUpdateCoordinator(QSettings *settings, YACReaderLibraries &libraries, QObject *parent = 0);
|
||||
|
||||
void updateLibraries();
|
||||
|
||||
signals:
|
||||
void updateStarted();
|
||||
void updateEnded();
|
||||
|
||||
private slots:
|
||||
void checkUpdatePolicy();
|
||||
void startUpdate();
|
||||
void updateLibrary(const QString &path);
|
||||
|
||||
private:
|
||||
QSettings *settings;
|
||||
YACReaderLibraries &libraries;
|
||||
QTimer *timer;
|
||||
QElapsedTimer elapsedTimer;
|
||||
std::future<void> updateFuture;
|
||||
};
|
||||
|
||||
#endif // LIBRARIES_UPDATE_COORDINATOR_H
|
Loading…
Reference in New Issue
Block a user