1
/**************************************************************************
3
* Copyright 2014 Canonical Ltd.
4
* Copyright 2014 Carlos J Mazieri <carlos.mazieri@gmail.com>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published by
8
* the Free Software Foundation; version 3.
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 Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
* File: disklocation.cpp
22
#include "disklocation.h"
23
#include "disklocationitemdiriterator.h"
24
#include "iorequest.h"
25
#include "ioworkerthread.h"
26
#include "externalfswatcher.h"
27
#include "locationurl.h"
28
#include "disklocationitemfile.h"
29
#include "disklocationitemdir.h"
32
#if defined(Q_OS_UNIX)
33
#include <sys/statvfs.h>
38
#if defined(DEBUG_EXT_FS_WATCHER)
39
# define DEBUG_WATCHER() qDebug() << "[extFsWatcher]" << QDateTime::currentDateTime().toString("hh:mm:ss.zzz") \
40
<< Q_FUNC_INFO << this
42
# define DEBUG_WATCHER() /**/
45
DiskLocation::DiskLocation(int type, QObject *parent)
46
: Location(type, parent)
52
DiskLocation::~ DiskLocation()
54
stopExternalFsWatcher();
59
* \brief DiskLocation::stopExternalFsWatcher() stops the External File System Watcher
61
void DiskLocation::stopExternalFsWatcher()
73
* \brief DiskLocation::startExternalFsWatcher() starts the External File System Watcher
75
void DiskLocation::startExternalFsWatcher()
77
if (m_extWatcher == 0)
80
m_extWatcher = new ExternalFSWatcher(this);
81
m_extWatcher->setIntervalToNotifyChanges(EX_FS_WATCHER_TIMER_INTERVAL);
83
connect(m_extWatcher, SIGNAL(pathModified(QString)),
84
this, SIGNAL(extWatcherPathChanged(QString)));
86
if (m_extWatcher && m_info)
88
//setCurrentPath() checks for empty paths
89
m_extWatcher->setCurrentPath(m_info->absoluteFilePath());
94
void DiskLocation::onItemsFetched()
98
m_extWatcher->setCurrentPath(m_info->absoluteFilePath());
104
void DiskLocation::startWorking()
106
if (m_usingExternalWatcher)
108
startExternalFsWatcher();
113
void DiskLocation::stopWorking()
115
stopExternalFsWatcher();
119
void DiskLocation::fetchExternalChanges(const QString &path,
120
const DirItemInfoList &list,
121
QDir::Filters dirFilter)
123
ExternalFileSystemChangesWorker *extFsWorker =
124
new ExternalFileSystemChangesWorker(list,
127
addExternalFsWorkerRequest(extFsWorker);
132
void DiskLocation::addExternalFsWorkerRequest(ExternalFileSystemChangesWorker *extFsWorker)
134
connect(extFsWorker, SIGNAL(added(DirItemInfo)),
135
this, SIGNAL(extWatcherItemAdded(DirItemInfo)));
137
connect(extFsWorker, SIGNAL(removed(DirItemInfo)),
138
this, SIGNAL(extWatcherItemRemoved(DirItemInfo)));
140
connect(extFsWorker, SIGNAL(changed(DirItemInfo)),
141
this, SIGNAL(extWatcherItemChanged(DirItemInfo)));
143
connect(extFsWorker, SIGNAL(finished(int)),
144
this, SIGNAL(extWatcherChangesFetched(int)));
146
workerThread()->addRequest(extFsWorker);
150
ExternalFSWatcher * DiskLocation::getExternalFSWatcher() const
156
void DiskLocation::setUsingExternalWatcher(bool use)
158
if ((m_usingExternalWatcher = use))
160
startExternalFsWatcher();
164
stopExternalFsWatcher();
169
DirItemInfo * DiskLocation::newItemInfo(const QString &urlPath)
171
return new DirItemInfo(urlPath);
175
DirListWorker * DiskLocation::newListWorker(const QString &urlPath, QDir::Filters filter, const bool isRecursive)
177
return new DirListWorker(urlPath,filter,isRecursive);
181
QString DiskLocation::urlBelongsToLocation(const QString &urlPath, int indexOfColonAndSlash)
184
if (urlPath.startsWith(LocationUrl::DiskRootURL.midRef(0,5)))
186
ret = QDir::rootPath() + DirItemInfo::removeExtraSlashes(urlPath, indexOfColonAndSlash+1);
192
LocationItemDirIterator *
193
DiskLocation::newDirIterator(const QString &path,
194
QDir::Filters filters,
195
QDirIterator::IteratorFlags flags,
196
LocationItemDirIterator::LoadMode loadmode)
199
return new DiskLocationItemDirIterator(path, filters, flags);
204
DiskLocation::newFile(const QString &path)
206
return new DiskLocationItemFile(path, this);
211
DiskLocation::newDir(const QString &dir)
213
return new DiskLocationItemDir(dir);
217
bool DiskLocation::isThereDiskSpace(const QString &pathname, qint64 requiredSize)
220
#if defined(Q_OS_UNIX)
221
QFileInfo info(pathname);
222
while (!info.exists() && info.absoluteFilePath() != QDir::rootPath())
224
info.setFile(info.absolutePath());
227
if ( ::statvfs( QFile::encodeName(info.absoluteFilePath()).constData(), &vfs) == 0 )
229
qint64 free = vfs.f_bsize * vfs.f_bfree;
230
ret = free > requiredSize;