From e3c29708824bd56ca1ec7ac86b5e3fddc42c3492 Mon Sep 17 00:00:00 2001 From: trialuser02 Date: Sun, 21 Mar 2021 16:37:31 +0000 Subject: library: using QThread git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@9771 90c681e8-e032-0410-971d-27865f9a5e38 --- src/plugins/General/library/library.cpp | 32 +++++++++++++++----------------- src/plugins/General/library/library.h | 14 +++++--------- src/plugins/General/library/library.pro | 2 +- 3 files changed, 21 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/plugins/General/library/library.cpp b/src/plugins/General/library/library.cpp index 9c9582e9f..a833e4b32 100644 --- a/src/plugins/General/library/library.cpp +++ b/src/plugins/General/library/library.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -43,7 +42,7 @@ #define CONNECTION_NAME "qmmp_library" Library::Library(QPointer *libraryWidget, QObject *parent) : - QObject(parent), + QThread(parent), m_libraryWidget(libraryWidget) { { @@ -73,7 +72,7 @@ Library::Library(QPointer *libraryWidget, QObject *parent) : UiHelper::instance()->addAction(refreshAction, UiHelper::TOOLS_MENU); connect(refreshAction, SIGNAL(triggered()), SLOT(startDirectoryScanning())); - connect(&m_watcher, &QFutureWatcher::finished, [=] { + connect(this, &QThread::finished, [=] { if(!m_libraryWidget->isNull()) { m_libraryWidget->data()->setBusyMode(false); @@ -98,10 +97,10 @@ Library::Library(QPointer *libraryWidget, QObject *parent) : Library::~Library() { - if(m_future.isRunning()) + if(isRunning()) { m_stopped = true; - m_future.waitForFinished(); + wait(); } if(QSqlDatabase::contains(CONNECTION_NAME)) @@ -111,11 +110,6 @@ Library::~Library() } } -bool Library::isRunning() const -{ - return m_future.isRunning(); -} - QAction *Library::showAction() const { return m_showAction; @@ -129,22 +123,26 @@ void Library::showLibraryWindow() if(m_libraryWidget->data()->isWindow()) m_libraryWidget->data()->show(); - if(m_future.isRunning()) + if(isRunning()) m_libraryWidget->data()->setBusyMode(true); } void Library::startDirectoryScanning() { - if(m_future.isRunning()) + if(isRunning()) return; m_filters = MetaDataManager::instance()->nameFilters(); - m_future = QtConcurrent::run(this, &Library::scanDirectories, m_dirs); - m_watcher.setFuture(m_future); + start(QThread::IdlePriority); if(!m_libraryWidget->isNull()) m_libraryWidget->data()->setBusyMode(true); } +void Library::run() +{ + scanDirectories(m_dirs); +} + bool Library::createTables() { QSqlDatabase db = QSqlDatabase::database(CONNECTION_NAME); @@ -248,6 +246,7 @@ bool Library::scanDirectories(const QStringList &paths) { m_stopped = false; + while(!m_stopped) { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME); db.setDatabaseName(Qmmp::configDir() + "/" + "library.sqlite"); @@ -263,8 +262,7 @@ bool Library::scanDirectories(const QStringList &paths) if(m_stopped) { db.close(); - QSqlDatabase::removeDatabase(CONNECTION_NAME); - return false; + break; } } @@ -274,7 +272,7 @@ bool Library::scanDirectories(const QStringList &paths) QSqlDatabase::removeDatabase(CONNECTION_NAME); qDebug("Library: directory scan finished"); - return true; + return !m_stopped; } void Library::addDirectory(const QString &s) diff --git a/src/plugins/General/library/library.h b/src/plugins/General/library/library.h index 053571a5c..766488908 100644 --- a/src/plugins/General/library/library.h +++ b/src/plugins/General/library/library.h @@ -24,9 +24,9 @@ #include #include #include -#include -#include #include +#include +#include #include #include @@ -36,15 +36,13 @@ class SoundCore; class PlayListTrack; class LibraryWidget; -class Library : public QObject +class Library : public QThread { Q_OBJECT public: explicit Library(QPointer *libraryWidget, QObject *parent = nullptr); ~Library(); - bool isRunning() const; - QAction *showAction() const; private slots: @@ -54,6 +52,7 @@ private slots: void startDirectoryScanning(); private: + void run() override; bool createTables(); void addTrack(TrackInfo *track, const QString &filePath); QByteArray serializeAudioInfo(const QMap &properties); @@ -63,13 +62,10 @@ private: bool checkFile(const QFileInfo &info); void removeIgnoredTracks(QList *tracks, const QStringList &ignoredPaths); - QFuture m_future; QStringList m_filters, m_dirs; - bool m_stopped = false; + std::atomic_bool m_stopped = ATOMIC_VAR_INIT(false); QPointer *m_libraryWidget; - QFutureWatcher m_watcher; QAction *m_showAction; - }; #endif // LIBRARY_H diff --git a/src/plugins/General/library/library.pro b/src/plugins/General/library/library.pro index 8655a9cc3..6befd48a2 100644 --- a/src/plugins/General/library/library.pro +++ b/src/plugins/General/library/library.pro @@ -2,7 +2,7 @@ include(../../plugins.pri) TARGET = $$PLUGINS_PREFIX/General/library -QT += sql concurrent +QT += sql HEADERS += libraryfactory.h \ library.h \ -- cgit v1.2.3-13-gbd6f