X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/4bfc28cb4bceb55996eec853da383efb44aaeeba..119f7a3f:/src/kitemviews/private/kdirectorycontentscounter.cpp diff --git a/src/kitemviews/private/kdirectorycontentscounter.cpp b/src/kitemviews/private/kdirectorycontentscounter.cpp index fd8479feb..af7312ad2 100644 --- a/src/kitemviews/private/kdirectorycontentscounter.cpp +++ b/src/kitemviews/private/kdirectorycontentscounter.cpp @@ -30,36 +30,53 @@ KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObj QObject(parent), m_model(model), m_queue(), - m_workerThread(0), m_worker(0), m_workerIsBusy(false), m_dirWatcher(0), m_watchedDirs() { - connect(m_model, SIGNAL(itemsRemoved(KItemRangeList)), - this, SLOT(slotItemsRemoved())); + connect(m_model, &KFileItemModel::itemsRemoved, + this, &KDirectoryContentsCounter::slotItemsRemoved); + + if (!m_workerThread) { + m_workerThread = new QThread(); + m_workerThread->start(); + } - m_workerThread = new QThread(this); m_worker = new KDirectoryContentsCounterWorker(); m_worker->moveToThread(m_workerThread); + ++m_workersCount; - connect(this, SIGNAL(requestDirectoryContentsCount(QString,KDirectoryContentsCounterWorker::Options)), - m_worker, SLOT(countDirectoryContents(QString,KDirectoryContentsCounterWorker::Options))); - connect(m_worker, SIGNAL(result(QString,int)), - this, SLOT(slotResult(QString,int))); - - m_workerThread->start(); + connect(this, &KDirectoryContentsCounter::requestDirectoryContentsCount, + m_worker, &KDirectoryContentsCounterWorker::countDirectoryContents); + connect(m_worker, &KDirectoryContentsCounterWorker::result, + this, &KDirectoryContentsCounter::slotResult); m_dirWatcher = new KDirWatch(this); - connect(m_dirWatcher, SIGNAL(dirty(QString)), this, SLOT(slotDirWatchDirty(QString))); + connect(m_dirWatcher, &KDirWatch::dirty, this, &KDirectoryContentsCounter::slotDirWatchDirty); } KDirectoryContentsCounter::~KDirectoryContentsCounter() { - m_workerThread->quit(); - m_workerThread->wait(); + --m_workersCount; - delete m_worker; + if (m_workersCount > 0) { + // The worker thread will continue running. It could even be running + // a method of m_worker at the moment, so we delete it using + // deleteLater() to prevent a crash. + m_worker->deleteLater(); + } else { + // There are no remaining workers -> stop the worker thread. + m_workerThread->quit(); + m_workerThread->wait(); + delete m_workerThread; + m_workerThread = 0; + + // The worker thread has finished running now, so it's safe to delete + // m_worker. deleteLater() would not work at all because the event loop + // which would deliver the event to m_worker is not running any more. + delete m_worker; + } } void KDirectoryContentsCounter::addDirectory(const QString& path) @@ -105,7 +122,7 @@ void KDirectoryContentsCounter::slotResult(const QString& path, int count) void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path) { - const int index = m_model->index(KUrl(path)); + const int index = m_model->index(QUrl::fromLocalFile(path)); if (index >= 0) { if (!m_model->fileItem(index).isDir()) { // If INotify is used, KDirWatch issues the dirty() signal @@ -134,7 +151,7 @@ void KDirectoryContentsCounter::slotItemsRemoved() QMutableSetIterator it(m_watchedDirs); while (it.hasNext()) { const QString& path = it.next(); - if (m_model->index(KUrl(path)) < 0) { + if (m_model->index(QUrl::fromLocalFile(path)) < 0) { m_dirWatcher->removeDir(path); it.remove(); } @@ -162,3 +179,6 @@ void KDirectoryContentsCounter::startWorker(const QString& path) m_workerIsBusy = true; } } + +QThread* KDirectoryContentsCounter::m_workerThread = 0; +int KDirectoryContentsCounter::m_workersCount = 0;