7 #include "FileStorageWatcher.h"
11 #include <QDirIterator>
16 #include "MarbleGlobal.h"
17 #include "MarbleDebug.h"
18 #include "MarbleDirs.h"
24 static const int maxFilesDelete = 20;
25 static const int softLimitPercent = 5;
29 FileStorageWatcherThread::FileStorageWatcherThread(
const QString &dataDirectory,
QObject *parent )
31 m_dataDirectory( dataDirectory ),
38 connect(
this, SIGNAL(variableChanged()),
39 this, SLOT(ensureCacheSize()),
43 FileStorageWatcherThread::~FileStorageWatcherThread()
47 quint64 FileStorageWatcherThread::cacheLimit()
52 void FileStorageWatcherThread::setCacheLimit( quint64 bytes )
56 m_cacheSoftLimit = bytes * ( 100 - softLimitPercent ) / 100;
57 m_limitMutex.unlock();
58 emit variableChanged();
61 void FileStorageWatcherThread::addToCurrentSize( qint64 bytes )
64 qint64 changedSize = bytes + m_currentCacheSize;
65 if( changedSize >= 0 )
66 m_currentCacheSize = changedSize;
68 m_currentCacheSize = 0;
69 emit variableChanged();
72 void FileStorageWatcherThread::resetCurrentSize()
74 m_currentCacheSize = 0;
75 emit variableChanged();
78 void FileStorageWatcherThread::prepareQuit()
83 void FileStorageWatcherThread::getCurrentCacheSize()
85 mDebug() <<
"FileStorageWatcher: Creating cache size";
93 while( it.hasNext() && !m_willQuit ) {
101 if (
path.
size() > basePathDepth + 3 ) {
103 int tileLevel =
path[basePathDepth + 2].
toInt(&ok);
106 if (!ok) tileLevel =
path[basePathDepth + 3].
toInt(&ok);
107 if ((ok && tileLevel >= maxBaseTileLevel ) &&
113 dataSize += file.
size();
118 m_currentCacheSize = dataSize;
121 void 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();
150 it = m_filesCache.
erase(it);
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 );
179 bool FileStorageWatcherThread::keepDeleting()
const
181 return ( ( m_currentCacheSize > m_cacheSoftLimit ) &&
182 ( m_filesDeleted < maxFilesDelete ) &&
189 FileStorageWatcher::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();
206 FileStorageWatcher::~FileStorageWatcher()
208 mDebug() <<
"Deleting FileStorageWatcher";
214 m_thread->prepareQuit();
216 if( !wait( 5000 ) ) {
217 mDebug() <<
"Failed to stop FileStorageWatcher-Thread, terminating!";
226 void FileStorageWatcher::setCacheLimit( quint64 bytes )
232 m_thread->setCacheLimit( bytes );
237 quint64 FileStorageWatcher::cacheLimit()
240 return m_thread->cacheLimit();
245 void FileStorageWatcher::addToCurrentSize( qint64 bytes )
247 emit sizeChanged( bytes );
250 void FileStorageWatcher::resetCurrentSize()
255 void FileStorageWatcher::run()
257 m_thread =
new FileStorageWatcherThread( m_dataDirectory );
259 m_limitMutex->lock();
260 m_thread->setCacheLimit( m_limit );
262 m_limitMutex->unlock();
264 m_thread->getCurrentCacheSize();
266 connect(
this, SIGNAL(sizeChanged(qint64)),
267 m_thread, SLOT(addToCurrentSize(qint64)) );
268 connect(
this, SIGNAL(cleared()),
269 m_thread, SLOT(resetCurrentSize()) );
283 #include "moc_FileStorageWatcher.cpp"