mirror of
https://github.com/YACReader/yacreader
synced 2025-05-28 03:10:27 -04:00
Implement stop/cancel on LibrariesUpdateCoordinator
This commit is contained in:
parent
bf35d0a621
commit
0623a6b101
@ -83,11 +83,15 @@ void LibrariesUpdateCoordinator::startUpdate()
|
||||
return;
|
||||
}
|
||||
|
||||
canceled = false;
|
||||
|
||||
updateFuture = std::async(std::launch::async, [this] {
|
||||
emit updateStarted();
|
||||
for (auto library : libraries.getLibraries()) {
|
||||
if (!canceled) {
|
||||
updateLibrary(library.getPath());
|
||||
}
|
||||
}
|
||||
emit updateEnded();
|
||||
});
|
||||
}
|
||||
@ -100,14 +104,34 @@ void LibrariesUpdateCoordinator::updateLibrary(const QString &path)
|
||||
}
|
||||
|
||||
QEventLoop eventLoop;
|
||||
LibraryCreator *libraryCreator = new LibraryCreator(settings);
|
||||
auto libraryCreator = new LibraryCreator(settings);
|
||||
std::shared_ptr<LibraryCreator> sharedPtr(libraryCreator);
|
||||
currentLibraryCreator = sharedPtr;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void LibrariesUpdateCoordinator::stop()
|
||||
{
|
||||
canceled = true;
|
||||
|
||||
if (auto libraryCreator = currentLibraryCreator.lock()) {
|
||||
libraryCreator->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void LibrariesUpdateCoordinator::cancel()
|
||||
{
|
||||
canceled = true;
|
||||
|
||||
if (auto libraryCreator = currentLibraryCreator.lock()) {
|
||||
libraryCreator->cancel();
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <QtCore>
|
||||
|
||||
class YACReaderLibraries;
|
||||
class LibraryCreator;
|
||||
|
||||
class LibrariesUpdateCoordinator : public QObject
|
||||
{
|
||||
@ -13,6 +14,7 @@ public:
|
||||
LibrariesUpdateCoordinator(QSettings *settings, YACReaderLibraries &libraries, QObject *parent = 0);
|
||||
|
||||
void updateLibraries();
|
||||
void cancel();
|
||||
|
||||
signals:
|
||||
void updateStarted();
|
||||
@ -29,6 +31,8 @@ private:
|
||||
QTimer *timer;
|
||||
QElapsedTimer elapsedTimer;
|
||||
std::future<void> updateFuture;
|
||||
bool canceled;
|
||||
std::weak_ptr<LibraryCreator> currentLibraryCreator;
|
||||
};
|
||||
|
||||
#endif // LIBRARIES_UPDATE_COORDINATOR_H
|
||||
|
Loading…
Reference in New Issue
Block a user