Changeset View
Changeset View
Standalone View
Standalone View
src/kitemviews/private/kdirectorycontentscounter.cpp
Show All 21 Lines | |||||
22 | #include "kitemviews/kfileitemmodel.h" | 22 | #include "kitemviews/kfileitemmodel.h" | ||
23 | 23 | | |||
24 | #include <KDirWatch> | 24 | #include <KDirWatch> | ||
25 | 25 | | |||
26 | #include <QThread> | 26 | #include <QThread> | ||
27 | 27 | | |||
28 | KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObject* parent) : | 28 | KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObject* parent) : | ||
29 | QObject(parent), | 29 | QObject(parent), | ||
30 | m_model(model), | 30 | m_model(model), | ||
meven: It would be great to be able to share this cache with baloo-widgets, so that those data can be… | |||||
meven: The Status bar info could be updated to get this data. | |||||
31 | m_queue(), | 31 | m_queue(), | ||
32 | m_worker(nullptr), | 32 | m_worker(nullptr), | ||
33 | m_workerIsBusy(false), | 33 | m_workerIsBusy(false), | ||
34 | m_dirWatcher(nullptr), | 34 | m_dirWatcher(nullptr), | ||
35 | m_watchedDirs() | 35 | m_watchedDirs() | ||
36 | { | 36 | { | ||
37 | connect(m_model, &KFileItemModel::itemsRemoved, | 37 | connect(m_model, &KFileItemModel::itemsRemoved, | ||
38 | this, &KDirectoryContentsCounter::slotItemsRemoved); | 38 | this, &KDirectoryContentsCounter::slotItemsRemoved); | ||
Show All 13 Lines | 51 | connect(m_worker, &KDirectoryContentsCounterWorker::result, | |||
52 | this, &KDirectoryContentsCounter::slotResult); | 52 | this, &KDirectoryContentsCounter::slotResult); | ||
53 | 53 | | |||
54 | m_dirWatcher = new KDirWatch(this); | 54 | m_dirWatcher = new KDirWatch(this); | ||
55 | connect(m_dirWatcher, &KDirWatch::dirty, this, &KDirectoryContentsCounter::slotDirWatchDirty); | 55 | connect(m_dirWatcher, &KDirWatch::dirty, this, &KDirectoryContentsCounter::slotDirWatchDirty); | ||
56 | } | 56 | } | ||
57 | 57 | | |||
58 | KDirectoryContentsCounter::~KDirectoryContentsCounter() | 58 | KDirectoryContentsCounter::~KDirectoryContentsCounter() | ||
59 | { | 59 | { | ||
60 | --m_workersCount; | 60 | if (m_workerThread->isRunning()) { | ||
elvisangelaccio: Why this change? What's wrong with `m_workersCount` ? | |||||
I intend to have a single worker KDirectoryContentsCounter, so why bother reimplementing a refCount ? meven: I intend to have a single worker KDirectoryContentsCounter, so why bother reimplementing a… | |||||
I see. This is valuable information, please put it in the commit message :) elvisangelaccio: I see. This is valuable information, please put it in the commit message :) | |||||
61 | | ||||
62 | if (m_workersCount > 0) { | | |||
63 | // The worker thread will continue running. It could even be running | 61 | // The worker thread will continue running. It could even be running | ||
64 | // a method of m_worker at the moment, so we delete it using | 62 | // a method of m_worker at the moment, so we delete it using | ||
65 | // deleteLater() to prevent a crash. | 63 | // deleteLater() to prevent a crash. | ||
66 | m_worker->deleteLater(); | 64 | m_worker->deleteLater(); | ||
67 | } else { | 65 | } else { | ||
68 | // There are no remaining workers -> stop the worker thread. | 66 | // There are no remaining workers -> stop the worker thread. | ||
69 | m_workerThread->quit(); | 67 | m_workerThread->quit(); | ||
70 | m_workerThread->wait(); | 68 | m_workerThread->wait(); | ||
71 | delete m_workerThread; | 69 | delete m_workerThread; | ||
72 | m_workerThread = nullptr; | 70 | m_workerThread = nullptr; | ||
73 | 71 | | |||
74 | // The worker thread has finished running now, so it's safe to delete | 72 | // The worker thread has finished running now, so it's safe to delete | ||
75 | // m_worker. deleteLater() would not work at all because the event loop | 73 | // m_worker. deleteLater() would not work at all because the event loop | ||
76 | // which would deliver the event to m_worker is not running any more. | 74 | // which would deliver the event to m_worker is not running any more. | ||
77 | delete m_worker; | 75 | delete m_worker; | ||
78 | } | 76 | } | ||
79 | } | 77 | } | ||
80 | 78 | | |||
81 | void KDirectoryContentsCounter::addDirectory(const QString& path) | 79 | void KDirectoryContentsCounter::addDirectory(const QString& path) | ||
82 | { | 80 | { | ||
83 | startWorker(path); | 81 | startWorker(path); | ||
84 | } | 82 | } | ||
85 | 83 | | |||
86 | int KDirectoryContentsCounter::countDirectoryContentsSynchronously(const QString& path) | 84 | KDirectoryContentsCounterWorker::CountResult KDirectoryContentsCounter::countDirectoryContentsSynchronously(const QString& path) | ||
87 | { | 85 | { | ||
88 | if (!m_dirWatcher->contains(path)) { | 86 | if (!m_dirWatcher->contains(path)) { | ||
89 | m_dirWatcher->addDir(path); | 87 | m_dirWatcher->addDir(path); | ||
90 | m_watchedDirs.insert(path); | 88 | m_watchedDirs.insert(path); | ||
91 | } | 89 | } | ||
92 | 90 | | |||
93 | KDirectoryContentsCounterWorker::Options options; | 91 | KDirectoryContentsCounterWorker::Options options; | ||
94 | 92 | | |||
95 | if (m_model->showHiddenFiles()) { | 93 | if (m_model->showHiddenFiles()) { | ||
96 | options |= KDirectoryContentsCounterWorker::CountHiddenFiles; | 94 | options |= KDirectoryContentsCounterWorker::CountHiddenFiles; | ||
97 | } | 95 | } | ||
98 | 96 | | |||
99 | if (m_model->showDirectoriesOnly()) { | 97 | if (m_model->showDirectoriesOnly()) { | ||
100 | options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly; | 98 | options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly; | ||
101 | } | 99 | } | ||
102 | 100 | | |||
103 | return KDirectoryContentsCounterWorker::subItemsCount(path, options); | 101 | return KDirectoryContentsCounterWorker::subItemsCount(path, options); | ||
104 | } | 102 | } | ||
105 | 103 | | |||
106 | void KDirectoryContentsCounter::slotResult(const QString& path, int count) | 104 | void KDirectoryContentsCounter::slotResult(const QString& path, int count, long size) | ||
107 | { | 105 | { | ||
108 | m_workerIsBusy = false; | 106 | m_workerIsBusy = false; | ||
109 | 107 | | |||
110 | if (!m_dirWatcher->contains(path)) { | 108 | if (!m_dirWatcher->contains(path)) { | ||
111 | m_dirWatcher->addDir(path); | 109 | m_dirWatcher->addDir(path); | ||
112 | m_watchedDirs.insert(path); | 110 | m_watchedDirs.insert(path); | ||
113 | } | 111 | } | ||
114 | 112 | | |||
115 | if (!m_queue.isEmpty()) { | 113 | if (!m_queue.isEmpty()) { | ||
116 | startWorker(m_queue.dequeue()); | 114 | startWorker(m_queue.dequeue()); | ||
117 | } | 115 | } | ||
118 | 116 | | |||
119 | emit result(path, count); | 117 | emit result(path, count, size); | ||
120 | } | 118 | } | ||
121 | 119 | | |||
122 | void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path) | 120 | void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path) | ||
123 | { | 121 | { | ||
124 | const int index = m_model->index(QUrl::fromLocalFile(path)); | 122 | const int index = m_model->index(QUrl::fromLocalFile(path)); | ||
125 | if (index >= 0) { | 123 | if (index >= 0) { | ||
126 | if (!m_model->fileItem(index).isDir()) { | 124 | if (!m_model->fileItem(index).isDir()) { | ||
127 | // If INotify is used, KDirWatch issues the dirty() signal | 125 | // If INotify is used, KDirWatch issues the dirty() signal | ||
▲ Show 20 Lines • Show All 56 Lines • Show Last 20 Lines |
It would be great to be able to share this cache with baloo-widgets, so that those data can be used in the metadata widget