2
* Copyright (C) 2012 Robin Burchell <robin+nemo@viroteck.net>
4
* You may use this file under the terms of the BSD license as follows:
6
* "Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions are
9
* * Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in
13
* the documentation and/or other materials provided with the
15
* * Neither the name of Nemo Mobile nor the names of its contributors
16
* may be used to endorse or promote products derived from this
17
* software without specific prior written permission.
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
32
#include "iorequest.h"
33
#include "qtrashutilinfo.h"
34
#include "diriteminfo.h"
35
#include "trashiteminfo.h"
37
#include <QDirIterator>
45
IORequest::IORequest() : QObject(), m_type(DirList)
49
IORequest::~IORequest()
54
IORequest::RequestType IORequest::type() const
59
//----------------------------------------------------------------------------------
60
IORequestLoader::IORequestLoader(const QString &pathName,
64
, mLoaderType(NormalLoader)
67
, mIsRecursive(isRecursive)
71
IORequestLoader::IORequestLoader(const QString& trashRootDir,
72
const QString &pathName,
76
, mLoaderType(TrashLoader)
79
, mIsRecursive(isRecursive)
80
, mTtrashRootDir(trashRootDir)
85
IORequestLoader::~IORequestLoader()
90
DirItemInfoList IORequestLoader::getContents()
95
case NormalLoader: list = getNormalContent(); break;
96
case TrashLoader: list = getTrashContent(); break;
97
case NetworkLoader: list = getNetworkContent(); break;
102
DirItemInfoList IORequestLoader::getNormalContent()
104
#if DEBUG_EXT_FS_WATCHER
105
qDebug() << "[exfsWatcher]" << QDateTime::currentDateTime().toString("hh:mm:ss.zzz")
108
DirItemInfoList directoryContents;
109
directoryContents = add(mPathName, mFilter, mIsRecursive, directoryContents);
110
return directoryContents;
113
DirItemInfoList IORequestLoader::add(const QString &pathName,
114
QDir::Filters filter,
116
DirItemInfoList directoryContents)
118
QDir tmpDir = QDir(pathName, QString(), QDir::NoSort, filter);
119
QDirIterator it(tmpDir);
120
while (it.hasNext()) {
122
if(it.fileInfo().isDir() && isRecursive) {
123
directoryContents = add(it.fileInfo().filePath(),
124
filter, isRecursive, directoryContents);
126
directoryContents.append(DirItemInfo(it.fileInfo()));
129
return directoryContents;
132
DirItemInfoList IORequestLoader::getTrashContent()
134
DirItemInfoList directoryContents;
135
QTrashUtilInfo trashInfo;
136
QDir tmpDir = QDir(mPathName, QString(), QDir::NoSort, mFilter);
137
bool isTopLevel = QFileInfo(mPathName).absolutePath() == mTtrashRootDir;
138
QDirIterator it(tmpDir);
142
trashInfo.setInfo(mTtrashRootDir, it.fileInfo().absoluteFilePath());
143
if (!isTopLevel || (isTopLevel && trashInfo.existsInfoFile() && trashInfo.existsFile()) )
145
//TODO read the trashinfo file and set it into a display field
146
// the display field can be a string the usally points to absoluteFilePath()
147
// it would be used only in the DirModel::data()
148
TrashItemInfo item(QTrashUtilInfo::filesTrashDir(mTtrashRootDir),
149
it.fileInfo().absoluteFilePath());
150
directoryContents.append(item);
153
return directoryContents;
157
DirItemInfoList IORequestLoader::getNetworkContent()
159
DirItemInfoList emptyContent;
165
//-----------------------------------------------------------------------------------------------
166
DirListWorker::DirListWorker(const QString &pathName,QDir::Filters filter, const bool isRecursive)
167
: IORequestLoader(pathName, filter, isRecursive)
173
DirListWorker::DirListWorker(const QString& trashRootDir, const QString &pathName,QDir::Filters filter, const bool isRecursive)
174
: IORequestLoader(trashRootDir, pathName, filter, isRecursive)
179
DirListWorker::~DirListWorker()
184
void DirListWorker::run()
187
qDebug() << Q_FUNC_INFO << "Running on: " << QThread::currentThreadId();
190
DirItemInfoList directoryContents = getContents();
193
emit itemsAdded(directoryContents);
194
emit workerFinished();
200
//-------------------------------------------------------------------------------------
201
TrashListWorker::TrashListWorker(const QString& trashRoot, const QString &path,QDir::Filters filter)
202
: DirListWorker(trashRoot, path, filter, false)
204
mLoaderType = TrashLoader;
207
TrashListWorker::~TrashListWorker()
212
//---------------------------------------------------------------------------------------------------------
213
ExternalFileSystemChangesWorker::ExternalFileSystemChangesWorker(const DirItemInfoList &content,
214
const QString &pathName,
215
QDir::Filters filter,
216
const bool isRecursive)
217
: IORequestLoader(pathName, filter, isRecursive)
220
m_type = DirListExternalFSChanges;
221
int counter = content.count();
224
m_curContent.insert( content.at(counter).absoluteFilePath(), content.at(counter) );
229
ExternalFileSystemChangesWorker::~ExternalFileSystemChangesWorker()
234
int ExternalFileSystemChangesWorker::compareItems(const DirItemInfoList& contentNew)
237
int removedCounter=0;
238
#if DEBUG_EXT_FS_WATCHER
239
qDebug() << "[exfsWatcher]" << QDateTime::currentDateTime().toString("hh:mm:ss.zzz")
241
<< "m_curContent.count():" << m_curContent.count()
242
<< "contentNew.count():" << contentNew.count();
244
int counter = contentNew.count();
247
int tmpCounter = counter;
250
const DirItemInfo& originalItem = contentNew.at(tmpCounter);
251
const DirItemInfo existItem = m_curContent.value(originalItem.absoluteFilePath());
252
if ( existItem.exists() )
254
//it may have changed
255
if ( originalItem.size() != existItem.size()
256
|| originalItem.lastModified() != existItem.lastModified()
257
|| originalItem.permissions() != existItem.permissions()
260
emit changed(originalItem);
263
m_curContent.remove(originalItem.absoluteFilePath());
265
else // originalItem was added
267
emit added(originalItem);
272
QHash<QString, DirItemInfo>::iterator i = m_curContent.begin();
273
for ( ; i != m_curContent.end(); ++removedCounter, ++i )
275
emit removed(i.value());
278
#if DEBUG_EXT_FS_WATCHER
279
qDebug() << "[exfsWatcher]" << QDateTime::currentDateTime().toString("hh:mm:ss.zzz")
281
<< "addedCounter:" << addedCounter
282
<< "removedCounter:" << removedCounter;
288
void ExternalFileSystemChangesWorker::run()
290
DirItemInfoList directoryContents = getContents();
291
int remainingitemsCounter = compareItems(directoryContents);
292
emit finished(remainingitemsCounter);
296
//---------------------------------------------------------------------
297
ExternalFileSystemTrashChangesWorker::ExternalFileSystemTrashChangesWorker(const QStringList &pathNames,
298
const DirItemInfoList &list,
299
QDir::Filters filter)
300
: ExternalFileSystemChangesWorker(list, pathNames.at(0), filter, false)
301
, m_pathList(pathNames)
303
mLoaderType = TrashLoader;
306
ExternalFileSystemTrashChangesWorker::~ExternalFileSystemTrashChangesWorker()
311
void ExternalFileSystemTrashChangesWorker::run()
313
DirItemInfoList directoryContents;
314
for(int counter = 0; counter < m_pathList.count(); counter++)
316
mPathName = QTrashUtilInfo::filesTrashDir(m_pathList.at(counter));
317
directoryContents += getContents();
319
int remainingitemsCounter = compareItems(directoryContents);
320
emit finished(remainingitemsCounter);