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,10 +83,14 @@ void LibrariesUpdateCoordinator::startUpdate()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
canceled = false;
|
||||||
|
|
||||||
updateFuture = std::async(std::launch::async, [this] {
|
updateFuture = std::async(std::launch::async, [this] {
|
||||||
emit updateStarted();
|
emit updateStarted();
|
||||||
for (auto library : libraries.getLibraries()) {
|
for (auto library : libraries.getLibraries()) {
|
||||||
updateLibrary(library.getPath());
|
if (!canceled) {
|
||||||
|
updateLibrary(library.getPath());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
emit updateEnded();
|
emit updateEnded();
|
||||||
});
|
});
|
||||||
@ -100,14 +104,34 @@ void LibrariesUpdateCoordinator::updateLibrary(const QString &path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
QEventLoop eventLoop;
|
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());
|
QString cleanPath = QDir::cleanPath(pathDir.absolutePath());
|
||||||
|
|
||||||
libraryCreator->updateLibrary(cleanPath, QDir::cleanPath(pathDir.absolutePath() + "/.yacreaderlibrary"));
|
libraryCreator->updateLibrary(cleanPath, QDir::cleanPath(pathDir.absolutePath() + "/.yacreaderlibrary"));
|
||||||
|
|
||||||
connect(libraryCreator, &LibraryCreator::finished, &eventLoop, &QEventLoop::quit);
|
connect(libraryCreator, &LibraryCreator::finished, &eventLoop, &QEventLoop::quit);
|
||||||
connect(libraryCreator, &LibraryCreator::finished, libraryCreator, &QObject::deleteLater);
|
|
||||||
|
|
||||||
libraryCreator->start();
|
libraryCreator->start();
|
||||||
eventLoop.exec();
|
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>
|
#include <QtCore>
|
||||||
|
|
||||||
class YACReaderLibraries;
|
class YACReaderLibraries;
|
||||||
|
class LibraryCreator;
|
||||||
|
|
||||||
class LibrariesUpdateCoordinator : public QObject
|
class LibrariesUpdateCoordinator : public QObject
|
||||||
{
|
{
|
||||||
@ -13,6 +14,7 @@ public:
|
|||||||
LibrariesUpdateCoordinator(QSettings *settings, YACReaderLibraries &libraries, QObject *parent = 0);
|
LibrariesUpdateCoordinator(QSettings *settings, YACReaderLibraries &libraries, QObject *parent = 0);
|
||||||
|
|
||||||
void updateLibraries();
|
void updateLibraries();
|
||||||
|
void cancel();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void updateStarted();
|
void updateStarted();
|
||||||
@ -29,6 +31,8 @@ private:
|
|||||||
QTimer *timer;
|
QTimer *timer;
|
||||||
QElapsedTimer elapsedTimer;
|
QElapsedTimer elapsedTimer;
|
||||||
std::future<void> updateFuture;
|
std::future<void> updateFuture;
|
||||||
|
bool canceled;
|
||||||
|
std::weak_ptr<LibraryCreator> currentLibraryCreator;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LIBRARIES_UPDATE_COORDINATOR_H
|
#endif // LIBRARIES_UPDATE_COORDINATOR_H
|
||||||
|
Loading…
Reference in New Issue
Block a user