]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kdirectorycontentscounter.cpp
Merge remote-tracking branch 'origin/KDE/4.12' into KDE/4.13
[dolphin.git] / 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> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "kdirectorycontentscounter.h"
22
23 #include "kdirectorycontentscounterworker.h"
24 #include <kitemviews/kfileitemmodel.h>
25
26 #include <KDirWatch>
27 #include <QThread>
28
29 KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObject* parent) :
30 QObject(parent),
31 m_model(model),
32 m_queue(),
33 m_worker(0),
34 m_workerIsBusy(false),
35 m_dirWatcher(0),
36 m_watchedDirs()
37 {
38 connect(m_model, SIGNAL(itemsRemoved(KItemRangeList)),
39 this, SLOT(slotItemsRemoved()));
40
41 if (!m_workerThread) {
42 m_workerThread = new QThread();
43 m_workerThread->start();
44 }
45
46 m_worker = new KDirectoryContentsCounterWorker();
47 m_worker->moveToThread(m_workerThread);
48 ++m_workersCount;
49
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)));
54
55 m_dirWatcher = new KDirWatch(this);
56 connect(m_dirWatcher, SIGNAL(dirty(QString)), this, SLOT(slotDirWatchDirty(QString)));
57 }
58
59 KDirectoryContentsCounter::~KDirectoryContentsCounter()
60 {
61 --m_workersCount;
62
63 if (m_workersCount == 0) {
64 m_workerThread->quit();
65 m_workerThread->wait();
66 delete m_workerThread;
67 m_workerThread = 0;
68 }
69
70 delete m_worker;
71 }
72
73 void KDirectoryContentsCounter::addDirectory(const QString& path)
74 {
75 startWorker(path);
76 }
77
78 int KDirectoryContentsCounter::countDirectoryContentsSynchronously(const QString& path)
79 {
80 if (!m_dirWatcher->contains(path)) {
81 m_dirWatcher->addDir(path);
82 m_watchedDirs.insert(path);
83 }
84
85 KDirectoryContentsCounterWorker::Options options;
86
87 if (m_model->showHiddenFiles()) {
88 options |= KDirectoryContentsCounterWorker::CountHiddenFiles;
89 }
90
91 if (m_model->showDirectoriesOnly()) {
92 options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
93 }
94
95 return KDirectoryContentsCounterWorker::subItemsCount(path, options);
96 }
97
98 void KDirectoryContentsCounter::slotResult(const QString& path, int count)
99 {
100 m_workerIsBusy = false;
101
102 if (!m_dirWatcher->contains(path)) {
103 m_dirWatcher->addDir(path);
104 m_watchedDirs.insert(path);
105 }
106
107 if (!m_queue.isEmpty()) {
108 startWorker(m_queue.dequeue());
109 }
110
111 emit result(path, count);
112 }
113
114 void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path)
115 {
116 const int index = m_model->index(KUrl(path));
117 if (index >= 0) {
118 if (!m_model->fileItem(index).isDir()) {
119 // If INotify is used, KDirWatch issues the dirty() signal
120 // also for changed files inside the directory, even if we
121 // don't enable this behavior explicitly (see bug 309740).
122 return;
123 }
124
125 startWorker(path);
126 }
127 }
128
129 void KDirectoryContentsCounter::slotItemsRemoved()
130 {
131 const bool allItemsRemoved = (m_model->count() == 0);
132
133 if (!m_watchedDirs.isEmpty()) {
134 // Don't let KDirWatch watch for removed items
135 if (allItemsRemoved) {
136 foreach (const QString& path, m_watchedDirs) {
137 m_dirWatcher->removeDir(path);
138 }
139 m_watchedDirs.clear();
140 m_queue.clear();
141 } else {
142 QMutableSetIterator<QString> it(m_watchedDirs);
143 while (it.hasNext()) {
144 const QString& path = it.next();
145 if (m_model->index(KUrl(path)) < 0) {
146 m_dirWatcher->removeDir(path);
147 it.remove();
148 }
149 }
150 }
151 }
152 }
153
154 void KDirectoryContentsCounter::startWorker(const QString& path)
155 {
156 if (m_workerIsBusy) {
157 m_queue.enqueue(path);
158 } else {
159 KDirectoryContentsCounterWorker::Options options;
160
161 if (m_model->showHiddenFiles()) {
162 options |= KDirectoryContentsCounterWorker::CountHiddenFiles;
163 }
164
165 if (m_model->showDirectoriesOnly()) {
166 options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
167 }
168
169 emit requestDirectoryContentsCount(path, options);
170 m_workerIsBusy = true;
171 }
172 }
173
174 QThread* KDirectoryContentsCounter::m_workerThread = 0;
175 int KDirectoryContentsCounter::m_workersCount = 0;