]>
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
) :
35 m_workerIsBusy(false),
39 connect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
40 this, SLOT(slotItemsRemoved()));
42 m_workerThread
= new QThread(this);
43 m_worker
= new KDirectoryContentsCounterWorker();
44 m_worker
->moveToThread(m_workerThread
);
46 connect(this, SIGNAL(requestDirectoryContentsCount(QString
,KDirectoryContentsCounterWorker::Options
)),
47 m_worker
, SLOT(countDirectoryContents(QString
,KDirectoryContentsCounterWorker::Options
)));
48 connect(m_worker
, SIGNAL(result(QString
,int)),
49 this, SLOT(slotResult(QString
,int)));
51 m_workerThread
->start();
53 m_dirWatcher
= new KDirWatch(this);
54 connect(m_dirWatcher
, SIGNAL(dirty(QString
)), this, SLOT(slotDirWatchDirty(QString
)));
57 KDirectoryContentsCounter::~KDirectoryContentsCounter()
59 m_workerThread
->quit();
60 m_workerThread
->wait();
65 void KDirectoryContentsCounter::addDirectory(const QString
& path
)
70 int KDirectoryContentsCounter::countDirectoryContentsSynchronously(const QString
& path
)
72 if (!m_dirWatcher
->contains(path
)) {
73 m_dirWatcher
->addDir(path
);
74 m_watchedDirs
.insert(path
);
77 KDirectoryContentsCounterWorker::Options options
;
79 if (m_model
->showHiddenFiles()) {
80 options
|= KDirectoryContentsCounterWorker::CountHiddenFiles
;
83 if (m_model
->showDirectoriesOnly()) {
84 options
|= KDirectoryContentsCounterWorker::CountDirectoriesOnly
;
87 return KDirectoryContentsCounterWorker::subItemsCount(path
, options
);
90 void KDirectoryContentsCounter::slotResult(const QString
& path
, int count
)
92 m_workerIsBusy
= false;
94 if (!m_dirWatcher
->contains(path
)) {
95 m_dirWatcher
->addDir(path
);
96 m_watchedDirs
.insert(path
);
99 if (!m_queue
.isEmpty()) {
100 startWorker(m_queue
.dequeue());
103 emit
result(path
, count
);
106 void KDirectoryContentsCounter::slotDirWatchDirty(const QString
& path
)
108 const int index
= m_model
->index(KUrl(path
));
110 if (!m_model
->fileItem(index
).isDir()) {
111 // If INotify is used, KDirWatch issues the dirty() signal
112 // also for changed files inside the directory, even if we
113 // don't enable this behavior explicitly (see bug 309740).
121 void KDirectoryContentsCounter::slotItemsRemoved()
123 const bool allItemsRemoved
= (m_model
->count() == 0);
125 if (!m_watchedDirs
.isEmpty()) {
126 // Don't let KDirWatch watch for removed items
127 if (allItemsRemoved
) {
128 foreach (const QString
& path
, m_watchedDirs
) {
129 m_dirWatcher
->removeDir(path
);
131 m_watchedDirs
.clear();
134 QMutableSetIterator
<QString
> it(m_watchedDirs
);
135 while (it
.hasNext()) {
136 const QString
& path
= it
.next();
137 if (m_model
->index(KUrl(path
)) < 0) {
138 m_dirWatcher
->removeDir(path
);
146 void KDirectoryContentsCounter::startWorker(const QString
& path
)
148 if (m_workerIsBusy
) {
149 m_queue
.enqueue(path
);
151 KDirectoryContentsCounterWorker::Options options
;
153 if (m_model
->showHiddenFiles()) {
154 options
|= KDirectoryContentsCounterWorker::CountHiddenFiles
;
157 if (m_model
->showDirectoriesOnly()) {
158 options
|= KDirectoryContentsCounterWorker::CountDirectoriesOnly
;
161 emit
requestDirectoryContentsCount(path
, options
);
162 m_workerIsBusy
= true;