1
/****************************************************************************
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** GNU Lesser General Public License Usage
10
** This file may be used under the terms of the GNU Lesser General Public
11
** License version 2.1 as published by the Free Software Foundation and
12
** appearing in the file LICENSE.LGPL included in the packaging of this
13
** file. Please review the following information to ensure the GNU Lesser
14
** General Public License version 2.1 requirements will be met:
15
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
** In addition, as a special exception, Nokia gives you certain additional
18
** rights. These rights are described in the Nokia Qt LGPL Exception
19
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
** GNU General Public License Usage
22
** Alternatively, this file may be used under the terms of the GNU General
23
** Public License version 3.0 as published by the Free Software Foundation
24
** and appearing in the file LICENSE.GPL included in the packaging of this
25
** file. Please review the following information to ensure the GNU General
26
** Public License version 3.0 requirements will be met:
27
** http://www.gnu.org/copyleft/gpl.html.
30
** Alternatively, this file may be used in accordance with the terms and
31
** conditions contained in a signed written agreement between you and Nokia.
40
****************************************************************************/
42
#include "fileinfothread_p.h"
43
#include <qdiriterator.h>
48
FileInfoThread::FileInfoThread(QObject *parent)
52
sortFlags(QDir::Name),
59
showOnlyReadable(false)
61
watcher = new QFileSystemWatcher(this);
62
connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(dirChanged(QString)));
63
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(updateFile(QString)));
67
FileInfoThread::~FileInfoThread()
69
QMutexLocker locker(&mutex);
76
void FileInfoThread::clear()
78
QMutexLocker locker(&mutex);
79
watcher->removePaths(watcher->files());
80
watcher->removePaths(watcher->directories());
83
void FileInfoThread::removePath(const QString &path)
85
QMutexLocker locker(&mutex);
86
watcher->removePath(path);
90
void FileInfoThread::setPath(const QString &path)
92
Q_ASSERT(!path.isEmpty());
94
QMutexLocker locker(&mutex);
95
watcher->addPath(path);
101
void FileInfoThread::setRootPath(const QString &path)
103
Q_ASSERT(!path.isEmpty());
105
QMutexLocker locker(&mutex);
109
void FileInfoThread::dirChanged(const QString &directoryPath)
111
Q_UNUSED(directoryPath);
112
QMutexLocker locker(&mutex);
117
void FileInfoThread::setSortFlags(QDir::SortFlags flags)
119
QMutexLocker locker(&mutex);
125
void FileInfoThread::setNameFilters(const QStringList & filters)
127
QMutexLocker locker(&mutex);
128
nameFilters = filters;
133
void FileInfoThread::setShowDirs(bool showFolders)
135
QMutexLocker locker(&mutex);
136
showDirs = showFolders;
141
void FileInfoThread::setShowDirsFirst(bool showDirsFirst)
143
QMutexLocker locker(&mutex);
144
showDirsFirst = showDirsFirst;
149
void FileInfoThread::setShowDotDot(bool on)
151
QMutexLocker locker(&mutex);
157
void FileInfoThread::setShowOnlyReadable(bool on)
159
QMutexLocker locker(&mutex);
160
showOnlyReadable = on;
165
void FileInfoThread::updateFile(const QString &path)
168
QMutexLocker locker(&mutex);
173
void FileInfoThread::run()
176
bool updateFiles = false;
177
QMutexLocker locker(&mutex);
181
if (currentPath.isEmpty() || !needUpdate)
182
condition.wait(&mutex);
188
if (!currentPath.isEmpty()) {
192
getFileInfos(currentPath);
198
void FileInfoThread::getFileInfos(const QString &path)
200
QDir::Filters filter;
201
filter = QDir::Files | QDir::NoDot | QDir::CaseSensitive;
203
filter = filter | QDir::AllDirs | QDir::Drives;
204
if ((path == rootPath) || !showDotDot)
205
filter = filter | QDir::NoDotDot;
206
if (showOnlyReadable)
207
filter = filter | QDir::Readable;
209
sortFlags = sortFlags | QDir::DirsFirst;
211
QDir currentDir(path, QString(), sortFlags);
212
QFileInfoList fileInfoList;
213
QList<FileProperty> filePropertyList;
215
fileInfoList = currentDir.entryInfoList(nameFilters, filter, sortFlags);
217
if (!fileInfoList.isEmpty()) {
218
foreach (QFileInfo info, fileInfoList) {
219
//qDebug() << "Adding file : " << info.fileName() << "to list ";
220
filePropertyList << FileProperty(info);
224
int toIndex = currentFileList.size()-1;
225
findChangeRange(filePropertyList, fromIndex, toIndex);
226
folderUpdate = false;
227
currentFileList = filePropertyList;
228
//qDebug() << "emit directoryUpdated : " << fromIndex << " " << toIndex;
229
emit directoryUpdated(path, filePropertyList, fromIndex, toIndex);
231
currentFileList = filePropertyList;
233
emit sortFinished(filePropertyList);
236
emit directoryChanged(path, filePropertyList);
239
// The directory is empty
242
int toIndex = currentFileList.size()-1;
243
folderUpdate = false;
244
currentFileList.clear();
245
emit directoryUpdated(path, filePropertyList, fromIndex, toIndex);
247
currentFileList.clear();
248
emit directoryChanged(path, filePropertyList);
254
void FileInfoThread::findChangeRange(const QList<FileProperty> &list, int &fromIndex, int &toIndex)
256
if (currentFileList.size() == 0) {
258
toIndex = list.size();
263
int listSize = list.size() < currentFileList.size() ? list.size() : currentFileList.size();
264
bool changeFound = false;
266
for (i=0; i < listSize; i++) {
267
if (list.at(i) != currentFileList.at(i)) {
278
// For now I let the rest of the list be updated..
279
toIndex = list.size() > currentFileList.size() ? list.size() - 1 : currentFileList.size() - 1;