7#include "FileStorageWatcher.h"
11#include <QDirIterator>
16#include "MarbleGlobal.h"
17#include "MarbleDebug.h"
18#include "MarbleDirs.h"
24static const int maxFilesDelete = 20;
25static const int softLimitPercent = 5;
29FileStorageWatcherThread::FileStorageWatcherThread(
const QString &dataDirectory,
QObject *parent )
31 m_dataDirectory( dataDirectory ),
38 connect(
this, SIGNAL(variableChanged()),
39 this, SLOT(ensureCacheSize()),
43FileStorageWatcherThread::~FileStorageWatcherThread()
47quint64 FileStorageWatcherThread::cacheLimit()
52void FileStorageWatcherThread::setCacheLimit( quint64 bytes )
56 m_cacheSoftLimit =
bytes * ( 100 - softLimitPercent ) / 100;
58 emit variableChanged();
61void FileStorageWatcherThread::addToCurrentSize( qint64 bytes )
68 m_currentCacheSize = 0;
69 emit variableChanged();
72void FileStorageWatcherThread::resetCurrentSize()
74 m_currentCacheSize = 0;
75 emit variableChanged();
78void FileStorageWatcherThread::prepareQuit()
83void FileStorageWatcherThread::getCurrentCacheSize()
85 mDebug() <<
"FileStorageWatcher: Creating cache size";
93 while(
it.hasNext() && !m_willQuit ) {
107 if ((ok && tileLevel >= maxBaseTileLevel ) &&
113 dataSize += file.
size();
118 m_currentCacheSize = dataSize;
121void FileStorageWatcherThread::ensureCacheSize()
128 if( ( ( m_currentCacheSize > m_cacheLimit )
129 || ( m_deleting && ( m_currentCacheSize > m_cacheSoftLimit ) ) )
130 && ( m_cacheLimit != 0 )
131 && ( m_cacheSoftLimit != 0 )
134 mDebug() <<
"Deleting extra cached tiles";
143 while (
it != m_filesCache.
end() &&
149 m_currentCacheSize -= info.size();
153 mDebug() <<
"Failed to remove:" << filePath;
158 if( m_filesDeleted >= maxFilesDelete ) {
170 if( m_currentCacheSize > m_cacheSoftLimit ) {
171 mDebug() <<
"FileStorageWatcher: Requested Cache Limit could not be reached!";
172 mDebug() <<
"Increasing Cache Limit to prevent further futile attempts.";
174 setCacheLimit( m_currentCacheSize / ( 100 - softLimitPercent ) * 100 );
179bool FileStorageWatcherThread::keepDeleting()
const
181 return ( ( m_currentCacheSize > m_cacheSoftLimit ) &&
182 ( m_filesDeleted < maxFilesDelete ) &&
189FileStorageWatcher::FileStorageWatcher(
const QString &dataDirectory,
QObject * parent )
191 m_dataDirectory( dataDirectory )
193 if ( m_dataDirectory.isEmpty() )
194 m_dataDirectory = MarbleDirs::localPath() +
QLatin1String(
"/cache/");
196 if ( !
QDir( m_dataDirectory ).exists() )
200 m_limitMutex =
new QMutex();
206FileStorageWatcher::~FileStorageWatcher()
214 m_thread->prepareQuit();
216 if( !
wait( 5000 ) ) {
217 mDebug() <<
"Failed to stop FileStorageWatcher-Thread, terminating!";
226void FileStorageWatcher::setCacheLimit( quint64 bytes )
232 m_thread->setCacheLimit(
bytes );
237quint64 FileStorageWatcher::cacheLimit()
240 return m_thread->cacheLimit();
245void FileStorageWatcher::addToCurrentSize( qint64 bytes )
250void FileStorageWatcher::resetCurrentSize()
255void FileStorageWatcher::run()
257 m_thread =
new FileStorageWatcherThread( m_dataDirectory );
259 m_limitMutex->
lock();
260 m_thread->setCacheLimit( m_limit );
264 m_thread->getCurrentCacheSize();
267 m_thread,
SLOT(addToCurrentSize(qint64)) );
269 m_thread,
SLOT(resetCurrentSize()) );
283#include "moc_FileStorageWatcher.cpp"
QString path(const QString &relativePath)
Binds a QML item to a specific geodetic location in screen coordinates.
bool mkpath(const QString &dirPath) const const
QString absoluteFilePath() const const
QDateTime lastModified() const const
QString path() const const
qint64 size() const const
QString suffix() const const
qsizetype size() const const
iterator erase(const_iterator first, const_iterator last)
iterator insert(const Key &key, const T &value)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
qsizetype size() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
QString toLower() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool wait(QDeadlineTimer deadline)