~loic.molinari/+junk/qtdeclarative-shadereffectsource-changes

« back to all changes in this revision

Viewing changes to src/imports/folderlistmodel/fileinfothread.cpp

  • Committer: Loïc Molinari
  • Date: 2012-04-21 17:59:51 UTC
  • Revision ID: loic.molinari@canonical.com-20120421175951-bqx68caaf5zrp76l
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/
 
5
**
 
6
** This file is part of the examples of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
20
**
 
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.
 
28
**
 
29
** Other Usage
 
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.
 
32
**
 
33
**
 
34
**
 
35
**
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "fileinfothread_p.h"
 
43
#include <qdiriterator.h>
 
44
 
 
45
#include <QDebug>
 
46
 
 
47
 
 
48
FileInfoThread::FileInfoThread(QObject *parent)
 
49
    : QThread(parent),
 
50
      abort(false),
 
51
      watcher(0),
 
52
      sortFlags(QDir::Name),
 
53
      needUpdate(true),
 
54
      folderUpdate(false),
 
55
      sortUpdate(false),
 
56
      showDirs(true),
 
57
      showDirsFirst(false),
 
58
      showDotDot(false),
 
59
      showOnlyReadable(false)
 
60
{
 
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)));
 
64
    start(LowPriority);
 
65
}
 
66
 
 
67
FileInfoThread::~FileInfoThread()
 
68
{
 
69
    QMutexLocker locker(&mutex);
 
70
    abort = true;
 
71
    condition.wakeOne();
 
72
    locker.unlock();
 
73
    wait();
 
74
}
 
75
 
 
76
void FileInfoThread::clear()
 
77
{
 
78
    QMutexLocker locker(&mutex);
 
79
    watcher->removePaths(watcher->files());
 
80
    watcher->removePaths(watcher->directories());
 
81
}
 
82
 
 
83
void FileInfoThread::removePath(const QString &path)
 
84
{
 
85
    QMutexLocker locker(&mutex);
 
86
    watcher->removePath(path);
 
87
    currentPath.clear();
 
88
}
 
89
 
 
90
void FileInfoThread::setPath(const QString &path)
 
91
{
 
92
    Q_ASSERT(!path.isEmpty());
 
93
 
 
94
    QMutexLocker locker(&mutex);
 
95
    watcher->addPath(path);
 
96
    currentPath = path;
 
97
    needUpdate = true;
 
98
    condition.wakeAll();
 
99
}
 
100
 
 
101
void FileInfoThread::setRootPath(const QString &path)
 
102
{
 
103
    Q_ASSERT(!path.isEmpty());
 
104
 
 
105
    QMutexLocker locker(&mutex);
 
106
    rootPath = path;
 
107
}
 
108
 
 
109
void FileInfoThread::dirChanged(const QString &directoryPath)
 
110
{
 
111
    Q_UNUSED(directoryPath);
 
112
    QMutexLocker locker(&mutex);
 
113
    folderUpdate = true;
 
114
    condition.wakeAll();
 
115
}
 
116
 
 
117
void FileInfoThread::setSortFlags(QDir::SortFlags flags)
 
118
{
 
119
    QMutexLocker locker(&mutex);
 
120
    sortFlags = flags;
 
121
    sortUpdate = true;
 
122
    condition.wakeAll();
 
123
}
 
124
 
 
125
void FileInfoThread::setNameFilters(const QStringList & filters)
 
126
{
 
127
    QMutexLocker locker(&mutex);
 
128
    nameFilters = filters;
 
129
    folderUpdate = true;
 
130
    condition.wakeAll();
 
131
}
 
132
 
 
133
void FileInfoThread::setShowDirs(bool showFolders)
 
134
{
 
135
    QMutexLocker locker(&mutex);
 
136
    showDirs = showFolders;
 
137
    folderUpdate = true;
 
138
    condition.wakeAll();
 
139
}
 
140
 
 
141
void FileInfoThread::setShowDirsFirst(bool showDirsFirst)
 
142
{
 
143
    QMutexLocker locker(&mutex);
 
144
    showDirsFirst = showDirsFirst;
 
145
    folderUpdate = true;
 
146
    condition.wakeAll();
 
147
}
 
148
 
 
149
void FileInfoThread::setShowDotDot(bool on)
 
150
{
 
151
    QMutexLocker locker(&mutex);
 
152
    showDotDot = on;
 
153
    folderUpdate = true;
 
154
    condition.wakeAll();
 
155
}
 
156
 
 
157
void FileInfoThread::setShowOnlyReadable(bool on)
 
158
{
 
159
    QMutexLocker locker(&mutex);
 
160
    showOnlyReadable = on;
 
161
    folderUpdate = true;
 
162
    condition.wakeAll();
 
163
}
 
164
 
 
165
void FileInfoThread::updateFile(const QString &path)
 
166
{
 
167
    Q_UNUSED(path);
 
168
    QMutexLocker locker(&mutex);
 
169
    folderUpdate = true;
 
170
    condition.wakeAll();
 
171
}
 
172
 
 
173
void FileInfoThread::run()
 
174
{
 
175
    forever {
 
176
        bool updateFiles = false;
 
177
        QMutexLocker locker(&mutex);
 
178
        if (abort) {
 
179
            return;
 
180
        }
 
181
        if (currentPath.isEmpty() || !needUpdate)
 
182
            condition.wait(&mutex);
 
183
 
 
184
        if (abort) {
 
185
            return;
 
186
        }
 
187
 
 
188
        if (!currentPath.isEmpty()) {
 
189
            updateFiles = true;
 
190
        }
 
191
        if (updateFiles)
 
192
            getFileInfos(currentPath);
 
193
        locker.unlock();
 
194
    }
 
195
}
 
196
 
 
197
 
 
198
void FileInfoThread::getFileInfos(const QString &path)
 
199
{
 
200
    QDir::Filters filter;
 
201
    filter = QDir::Files | QDir::NoDot | QDir::CaseSensitive;
 
202
    if (showDirs)
 
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;
 
208
    if (showDirsFirst)
 
209
        sortFlags = sortFlags | QDir::DirsFirst;
 
210
 
 
211
    QDir currentDir(path, QString(), sortFlags);
 
212
    QFileInfoList fileInfoList;
 
213
    QList<FileProperty> filePropertyList;
 
214
 
 
215
    fileInfoList = currentDir.entryInfoList(nameFilters, filter, sortFlags);
 
216
 
 
217
    if (!fileInfoList.isEmpty()) {
 
218
        foreach (QFileInfo info, fileInfoList) {
 
219
            //qDebug() << "Adding file : " << info.fileName() << "to list ";
 
220
            filePropertyList << FileProperty(info);
 
221
        }
 
222
        if (folderUpdate) {
 
223
            int fromIndex = 0;
 
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);
 
230
        } else {
 
231
            currentFileList = filePropertyList;
 
232
            if (sortUpdate) {
 
233
                emit sortFinished(filePropertyList);
 
234
                sortUpdate = false;
 
235
            } else
 
236
                emit directoryChanged(path, filePropertyList);
 
237
        }
 
238
    } else {
 
239
        // The directory is empty
 
240
        if (folderUpdate) {
 
241
            int fromIndex = 0;
 
242
            int toIndex = currentFileList.size()-1;
 
243
            folderUpdate = false;
 
244
            currentFileList.clear();
 
245
            emit directoryUpdated(path, filePropertyList, fromIndex, toIndex);
 
246
        } else {
 
247
            currentFileList.clear();
 
248
            emit directoryChanged(path, filePropertyList);
 
249
        }
 
250
    }
 
251
    needUpdate = false;
 
252
}
 
253
 
 
254
void FileInfoThread::findChangeRange(const QList<FileProperty> &list, int &fromIndex, int &toIndex)
 
255
{
 
256
    if (currentFileList.size() == 0) {
 
257
        fromIndex = 0;
 
258
        toIndex = list.size();
 
259
        return;
 
260
    }
 
261
 
 
262
    int i;
 
263
    int listSize = list.size() < currentFileList.size() ? list.size() : currentFileList.size();
 
264
    bool changeFound = false;
 
265
 
 
266
    for (i=0; i < listSize; i++) {
 
267
        if (list.at(i) != currentFileList.at(i)) {
 
268
            changeFound = true;
 
269
            break;
 
270
        }
 
271
    }
 
272
 
 
273
    if (changeFound)
 
274
        fromIndex = i;
 
275
    else
 
276
        fromIndex = i-1;
 
277
 
 
278
    // For now I let the rest of the list be updated..
 
279
    toIndex = list.size() > currentFileList.size() ? list.size() - 1 : currentFileList.size() - 1;
 
280
}