~ubuntu-filemanager-dev/ubuntu-filemanager-app/trunk

« back to all changes in this revision

Viewing changes to src/plugin/folderlistmodel/iorequest.cpp

  • Committer: Bileto Bot
  • Date: 2017-04-04 17:06:41 UTC
  • mfrom: (588.1.19 fix-desktop-file)
  • Revision ID: ci-train-bot@canonical.com-20170404170641-1p15lmx8wodlx2ut
* Rename binary file to ubuntu-filemanager-app
* Join plugin packages into the main package 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2012 Robin Burchell <robin+nemo@viroteck.net>
3
 
 *
4
 
 * You may use this file under the terms of the BSD license as follows:
5
 
 *
6
 
 * "Redistribution and use in source and binary forms, with or without
7
 
 * modification, are permitted provided that the following conditions are
8
 
 * met:
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
14
 
 *     distribution.
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.
18
 
 *
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."
30
 
 */
31
 
 
32
 
#include "iorequest.h"
33
 
#include "qtrashutilinfo.h"
34
 
#include "diriteminfo.h"
35
 
#include "trashiteminfo.h"
36
 
 
37
 
#include <QDirIterator>
38
 
#include <QDebug>
39
 
 
40
 
#ifdef DEBUG_MESSAGES
41
 
#include <QDateTime>
42
 
#include <QThread>
43
 
#endif
44
 
 
45
 
IORequest::IORequest() : QObject(), m_type(DirList)
46
 
{
47
 
}
48
 
 
49
 
IORequest::~IORequest()
50
 
{
51
 
 
52
 
}
53
 
 
54
 
IORequest::RequestType IORequest::type() const
55
 
{
56
 
    return m_type;
57
 
}
58
 
 
59
 
//----------------------------------------------------------------------------------
60
 
IORequestLoader::IORequestLoader(const QString &pathName,
61
 
                                QDir::Filters filter,
62
 
                                 bool isRecursive)
63
 
      : IORequest()
64
 
      , mLoaderType(NormalLoader)
65
 
      , mPathName(pathName)
66
 
      , mFilter(filter)
67
 
      , mIsRecursive(isRecursive)
68
 
{
69
 
}
70
 
 
71
 
IORequestLoader::IORequestLoader(const QString& trashRootDir,
72
 
                                 const QString &pathName,
73
 
                                QDir::Filters filter,
74
 
                                 bool isRecursive)
75
 
      : IORequest()
76
 
      , mLoaderType(TrashLoader)
77
 
      , mPathName(pathName)
78
 
      , mFilter(filter)
79
 
      , mIsRecursive(isRecursive)
80
 
      , mTtrashRootDir(trashRootDir)
81
 
{
82
 
 
83
 
}
84
 
 
85
 
IORequestLoader::~IORequestLoader()
86
 
{
87
 
 
88
 
}
89
 
 
90
 
DirItemInfoList  IORequestLoader::getContents()
91
 
{
92
 
   DirItemInfoList list;
93
 
   switch(mLoaderType)
94
 
   {
95
 
      case  NormalLoader:  list = getNormalContent();  break;
96
 
      case  TrashLoader:   list = getTrashContent();   break;
97
 
      case  NetworkLoader: list = getNetworkContent(); break;
98
 
   }   
99
 
   return list;
100
 
}
101
 
 
102
 
DirItemInfoList  IORequestLoader::getNormalContent()
103
 
{
104
 
#if DEBUG_EXT_FS_WATCHER
105
 
    qDebug() << "[exfsWatcher]" << QDateTime::currentDateTime().toString("hh:mm:ss.zzz")
106
 
             << Q_FUNC_INFO;
107
 
#endif
108
 
    DirItemInfoList directoryContents;
109
 
    directoryContents = add(mPathName, mFilter, mIsRecursive, directoryContents);
110
 
    return directoryContents;
111
 
}
112
 
 
113
 
DirItemInfoList IORequestLoader::add(const QString &pathName,
114
 
                                     QDir::Filters filter,
115
 
                                      bool isRecursive,
116
 
                                      DirItemInfoList directoryContents)
117
 
{
118
 
    QDir tmpDir = QDir(pathName, QString(), QDir::NoSort, filter);
119
 
    QDirIterator it(tmpDir);
120
 
    while (it.hasNext()) {
121
 
        it.next();
122
 
        if(it.fileInfo().isDir() && isRecursive) {
123
 
            directoryContents = add(it.fileInfo().filePath(),
124
 
                                    filter, isRecursive, directoryContents);
125
 
        } else {
126
 
            directoryContents.append(DirItemInfo(it.fileInfo()));
127
 
        }
128
 
    }
129
 
    return directoryContents;
130
 
}
131
 
 
132
 
DirItemInfoList  IORequestLoader::getTrashContent()
133
 
{
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);
139
 
   while (it.hasNext())
140
 
   {
141
 
       it.next();
142
 
       trashInfo.setInfo(mTtrashRootDir, it.fileInfo().absoluteFilePath());
143
 
       if (!isTopLevel || (isTopLevel && trashInfo.existsInfoFile() && trashInfo.existsFile()) )
144
 
       {
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);
151
 
       }
152
 
   }
153
 
   return directoryContents;
154
 
}
155
 
 
156
 
 
157
 
DirItemInfoList IORequestLoader::getNetworkContent()
158
 
{
159
 
  DirItemInfoList emptyContent;
160
 
  return emptyContent;
161
 
}
162
 
 
163
 
 
164
 
 
165
 
//-----------------------------------------------------------------------------------------------
166
 
DirListWorker::DirListWorker(const QString &pathName,QDir::Filters filter, const bool isRecursive)
167
 
    : IORequestLoader(pathName, filter, isRecursive)
168
 
{
169
 
 
170
 
}
171
 
 
172
 
 
173
 
DirListWorker::DirListWorker(const QString& trashRootDir, const QString &pathName,QDir::Filters filter, const bool isRecursive)
174
 
    : IORequestLoader(trashRootDir, pathName, filter, isRecursive)
175
 
{
176
 
 
177
 
}
178
 
 
179
 
DirListWorker::~DirListWorker()
180
 
{
181
 
 
182
 
}
183
 
 
184
 
void DirListWorker::run()
185
 
{
186
 
#if DEBUG_MESSAGES
187
 
    qDebug() << Q_FUNC_INFO << "Running on: " << QThread::currentThreadId();
188
 
#endif
189
 
 
190
 
    DirItemInfoList directoryContents = getContents();
191
 
 
192
 
    // last batch
193
 
    emit itemsAdded(directoryContents);
194
 
    emit workerFinished();
195
 
}
196
 
 
197
 
 
198
 
 
199
 
 
200
 
//-------------------------------------------------------------------------------------
201
 
TrashListWorker::TrashListWorker(const QString& trashRoot, const QString &path,QDir::Filters filter)
202
 
  : DirListWorker(trashRoot, path, filter, false)
203
 
{
204
 
    mLoaderType = TrashLoader;
205
 
}
206
 
 
207
 
TrashListWorker::~TrashListWorker()
208
 
{
209
 
 
210
 
}
211
 
 
212
 
//---------------------------------------------------------------------------------------------------------
213
 
ExternalFileSystemChangesWorker::ExternalFileSystemChangesWorker(const DirItemInfoList &content,
214
 
                                                   const QString &pathName,
215
 
                                                  QDir::Filters filter,
216
 
                                                   const bool isRecursive)
217
 
    : IORequestLoader(pathName, filter, isRecursive)
218
 
 
219
 
{
220
 
    m_type        = DirListExternalFSChanges;
221
 
    int counter = content.count();
222
 
    while (counter--)
223
 
    {
224
 
        m_curContent.insert( content.at(counter).absoluteFilePath(), content.at(counter) );
225
 
    }
226
 
}
227
 
 
228
 
 
229
 
ExternalFileSystemChangesWorker::~ExternalFileSystemChangesWorker()
230
 
{
231
 
 
232
 
}
233
 
 
234
 
int ExternalFileSystemChangesWorker::compareItems(const DirItemInfoList& contentNew)
235
 
{
236
 
    int   addedCounter=0;
237
 
    int   removedCounter=0;
238
 
#if DEBUG_EXT_FS_WATCHER
239
 
        qDebug() << "[exfsWatcher]" << QDateTime::currentDateTime().toString("hh:mm:ss.zzz")
240
 
                 << Q_FUNC_INFO
241
 
                 << "m_curContent.count():"      << m_curContent.count()
242
 
                 << "contentNew.count():" << contentNew.count();
243
 
#endif
244
 
    int counter = contentNew.count();
245
 
    if (counter > 0)
246
 
    {
247
 
        int tmpCounter = counter;
248
 
        while (tmpCounter--)
249
 
        {
250
 
            const DirItemInfo& originalItem = contentNew.at(tmpCounter);
251
 
            const DirItemInfo  existItem    = m_curContent.value(originalItem.absoluteFilePath());
252
 
            if ( existItem.exists() )
253
 
            {
254
 
                //it may have changed
255
 
                if (   originalItem.size()         != existItem.size()
256
 
                       || originalItem.lastModified() != existItem.lastModified()
257
 
                       || originalItem.permissions()  != existItem.permissions()
258
 
                       )
259
 
                {
260
 
                    emit changed(originalItem);
261
 
                }
262
 
                //remove this item
263
 
                m_curContent.remove(originalItem.absoluteFilePath());
264
 
            }
265
 
            else // originalItem was added
266
 
            {
267
 
                emit added(originalItem);
268
 
                ++addedCounter;
269
 
            }
270
 
        }
271
 
 
272
 
        QHash<QString, DirItemInfo>::iterator  i = m_curContent.begin();
273
 
        for ( ;  i != m_curContent.end();  ++removedCounter, ++i )
274
 
        {
275
 
            emit removed(i.value());
276
 
        }
277
 
    }
278
 
#if DEBUG_EXT_FS_WATCHER
279
 
        qDebug() << "[exfsWatcher]" << QDateTime::currentDateTime().toString("hh:mm:ss.zzz")
280
 
                 << Q_FUNC_INFO
281
 
                 << "addedCounter:"   << addedCounter
282
 
                 << "removedCounter:" << removedCounter;
283
 
#endif
284
 
 
285
 
   return counter;
286
 
}
287
 
 
288
 
void ExternalFileSystemChangesWorker::run()
289
 
{
290
 
    DirItemInfoList directoryContents = getContents();    
291
 
    int remainingitemsCounter = compareItems(directoryContents);
292
 
    emit finished(remainingitemsCounter);
293
 
}
294
 
 
295
 
 
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)
302
 
{
303
 
    mLoaderType = TrashLoader;
304
 
}
305
 
 
306
 
ExternalFileSystemTrashChangesWorker::~ExternalFileSystemTrashChangesWorker()
307
 
{
308
 
 
309
 
}
310
 
 
311
 
void ExternalFileSystemTrashChangesWorker::run()
312
 
{
313
 
    DirItemInfoList directoryContents;
314
 
    for(int counter = 0; counter < m_pathList.count(); counter++)
315
 
    {
316
 
        mPathName = QTrashUtilInfo::filesTrashDir(m_pathList.at(counter));
317
 
        directoryContents += getContents();
318
 
    }
319
 
    int remainingitemsCounter = compareItems(directoryContents);
320
 
    emit finished(remainingitemsCounter);
321
 
}