]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kdirectorycontentscounter.cpp
Merge remote-tracking branch 'origin/KDE/4.11'
[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_workerThread(0),
34 m_worker(0),
35 m_workerIsBusy(false),
36 m_dirWatcher(0),
37 m_watchedDirs()
38 {
39 connect(m_model, SIGNAL(itemsRemoved(KItemRangeList)),
40 this, SLOT(slotItemsRemoved()));
41
42 m_workerThread = new QThread(this);
43 m_worker = new KDirectoryContentsCounterWorker();
44 m_worker->moveToThread(m_workerThread);
45
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)));
50
51 m_workerThread->start();
52
53 m_dirWatcher = new KDirWatch(this);
54 connect(m_dirWatcher, SIGNAL(dirty(QString)), this, SLOT(slotDirWatchDirty(QString)));
55 }
56
57 KDirectoryContentsCounter::~KDirectoryContentsCounter()
58 {
59 m_workerThread->quit();
60 m_workerThread->wait();
61
62 delete m_worker;
63 }
64
65 void KDirectoryContentsCounter::addDirectory(const QString& path)
66 {
67 startWorker(path);
68 }
69
70 int KDirectoryContentsCounter::countDirectoryContentsSynchronously(const QString& path)
71 {
72 if (!m_dirWatcher->contains(path)) {
73 m_dirWatcher->addDir(path);
74 m_watchedDirs.insert(path);
75 }
76
77 KDirectoryContentsCounterWorker::Options options;
78
79 if (m_model->showHiddenFiles()) {
80 options |= KDirectoryContentsCounterWorker::CountHiddenFiles;
81 }
82
83 if (m_model->showDirectoriesOnly()) {
84 options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
85 }
86
87 return KDirectoryContentsCounterWorker::subItemsCount(path, options);
88 }
89
90 void KDirectoryContentsCounter::slotResult(const QString& path, int count)
91 {
92 m_workerIsBusy = false;
93
94 if (!m_dirWatcher->contains(path)) {
95 m_dirWatcher->addDir(path);
96 m_watchedDirs.insert(path);
97 }
98
99 if (!m_queue.isEmpty()) {
100 startWorker(m_queue.dequeue());
101 }
102
103 emit result(path, count);
104 }
105
106 void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path)
107 {
108 const int index = m_model->index(KUrl(path));
109 if (index >= 0) {
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).
114 return;
115 }
116
117 startWorker(path);
118 }
119 }
120
121 void KDirectoryContentsCounter::slotItemsRemoved()
122 {
123 const bool allItemsRemoved = (m_model->count() == 0);
124
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);
130 }
131 m_watchedDirs.clear();
132 m_queue.clear();
133 } else {
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);
139 it.remove();
140 }
141 }
142 }
143 }
144 }
145
146 void KDirectoryContentsCounter::startWorker(const QString& path)
147 {
148 if (m_workerIsBusy) {
149 m_queue.enqueue(path);
150 } else {
151 KDirectoryContentsCounterWorker::Options options;
152
153 if (m_model->showHiddenFiles()) {
154 options |= KDirectoryContentsCounterWorker::CountHiddenFiles;
155 }
156
157 if (m_model->showDirectoriesOnly()) {
158 options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
159 }
160
161 emit requestDirectoryContentsCount(path, options);
162 m_workerIsBusy = true;
163 }
164 }