]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kdirectorycontentscounter.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "kdirectorycontentscounter.h"
23 #include "kdirectorycontentscounterworker.h"
24 #include <kitemviews/kfileitemmodel.h>
29 KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel
* model
, QObject
* parent
) :
34 m_workerIsBusy(false),
38 connect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
39 this, SLOT(slotItemsRemoved()));
41 if (!m_workerThread
) {
42 m_workerThread
= new QThread();
43 m_workerThread
->start();
46 m_worker
= new KDirectoryContentsCounterWorker();
47 m_worker
->moveToThread(m_workerThread
);
50 connect(this, SIGNAL(requestDirectoryContentsCount(QString
,KDirectoryContentsCounterWorker::Options
)),
51 m_worker
, SLOT(countDirectoryContents(QString
,KDirectoryContentsCounterWorker::Options
)));
52 connect(m_worker
, SIGNAL(result(QString
,int)),
53 this, SLOT(slotResult(QString
,int)));
55 m_dirWatcher
= new KDirWatch(this);
56 connect(m_dirWatcher
, SIGNAL(dirty(QString
)), this, SLOT(slotDirWatchDirty(QString
)));
59 KDirectoryContentsCounter::~KDirectoryContentsCounter()
63 if (m_workersCount
> 0) {
64 // The worker thread will continue running. It could even be running
65 // a method of m_worker at the moment, so we delete it using
66 // deleteLater() to prevent a crash.
67 m_worker
->deleteLater();
69 // There are no remaining workers -> stop the worker thread.
70 m_workerThread
->quit();
71 m_workerThread
->wait();
72 delete m_workerThread
;
75 // The worker thread has finished running now, so it's safe to delete
76 // m_worker. deleteLater() would not work at all because the event loop
77 // which would deliver the event to m_worker is not running any more.
82 void KDirectoryContentsCounter::addDirectory(const QString
& path
)
87 int KDirectoryContentsCounter::countDirectoryContentsSynchronously(const QString
& path
)
89 if (!m_dirWatcher
->contains(path
)) {
90 m_dirWatcher
->addDir(path
);
91 m_watchedDirs
.insert(path
);
94 KDirectoryContentsCounterWorker::Options options
;
96 if (m_model
->showHiddenFiles()) {
97 options
|= KDirectoryContentsCounterWorker::CountHiddenFiles
;
100 if (m_model
->showDirectoriesOnly()) {
101 options
|= KDirectoryContentsCounterWorker::CountDirectoriesOnly
;
104 return KDirectoryContentsCounterWorker::subItemsCount(path
, options
);
107 void KDirectoryContentsCounter::slotResult(const QString
& path
, int count
)
109 m_workerIsBusy
= false;
111 if (!m_dirWatcher
->contains(path
)) {
112 m_dirWatcher
->addDir(path
);
113 m_watchedDirs
.insert(path
);
116 if (!m_queue
.isEmpty()) {
117 startWorker(m_queue
.dequeue());
120 emit
result(path
, count
);
123 void KDirectoryContentsCounter::slotDirWatchDirty(const QString
& path
)
125 const int index
= m_model
->index(KUrl(path
));
127 if (!m_model
->fileItem(index
).isDir()) {
128 // If INotify is used, KDirWatch issues the dirty() signal
129 // also for changed files inside the directory, even if we
130 // don't enable this behavior explicitly (see bug 309740).
138 void KDirectoryContentsCounter::slotItemsRemoved()
140 const bool allItemsRemoved
= (m_model
->count() == 0);
142 if (!m_watchedDirs
.isEmpty()) {
143 // Don't let KDirWatch watch for removed items
144 if (allItemsRemoved
) {
145 foreach (const QString
& path
, m_watchedDirs
) {
146 m_dirWatcher
->removeDir(path
);
148 m_watchedDirs
.clear();
151 QMutableSetIterator
<QString
> it(m_watchedDirs
);
152 while (it
.hasNext()) {
153 const QString
& path
= it
.next();
154 if (m_model
->index(KUrl(path
)) < 0) {
155 m_dirWatcher
->removeDir(path
);
163 void KDirectoryContentsCounter::startWorker(const QString
& path
)
165 if (m_workerIsBusy
) {
166 m_queue
.enqueue(path
);
168 KDirectoryContentsCounterWorker::Options options
;
170 if (m_model
->showHiddenFiles()) {
171 options
|= KDirectoryContentsCounterWorker::CountHiddenFiles
;
174 if (m_model
->showDirectoriesOnly()) {
175 options
|= KDirectoryContentsCounterWorker::CountDirectoriesOnly
;
178 emit
requestDirectoryContentsCount(path
, options
);
179 m_workerIsBusy
= true;
183 QThread
* KDirectoryContentsCounter::m_workerThread
= 0;
184 int KDirectoryContentsCounter::m_workersCount
= 0;