17 #include <QDirIterator>
26 using namespace Marble;
39 m_dataDirectory( dataDirectory ),
47 this, SLOT(ensureCacheSize()),
48 Qt::QueuedConnection );
66 m_limitMutex.unlock();
73 qint64 changedSize = bytes + m_currentCacheSize;
74 if( changedSize >= 0 )
75 m_currentCacheSize = changedSize;
77 m_currentCacheSize = 0;
83 m_currentCacheSize = 0;
89 mDebug() <<
"Theme changed to " << mapTheme;
91 m_mapThemeId = mapTheme;
92 m_themeMutex.unlock();
103 mDebug() <<
"FileStorageWatcher: Creating cache size";
104 quint64 dataSize = 0;
105 QDirIterator it( m_dataDirectory, QDir::Files, QDirIterator::Subdirectories );
107 while( it.hasNext() && !m_willQuit )
110 QFileInfo file = it.fileInfo();
111 dataSize += file.size();
113 m_currentCacheSize = dataSize;
116 void FileStorageWatcherThread::ensureCacheSize()
123 if( ( ( m_currentCacheSize > m_cacheLimit )
124 || ( m_deleting && ( m_currentCacheSize > m_cacheSoftLimit ) ) )
125 && ( m_cacheLimit != 0 )
126 && ( m_cacheSoftLimit != 0 )
127 && !( m_mapThemeId.isEmpty() )
136 if ( m_dataDirectory.isEmpty() ||
137 !m_dataDirectory.endsWith(QLatin1String(
"data" )) )
140 <<
"Error: Refusing to erase files under"
141 <<
"unknown conditions for safety reasons!";
147 QStringList currentList = m_mapThemeId.split(
'/' );
148 QString shownPlanet( currentList.first() );
149 currentList.removeFirst();
153 QString cachedMapsDirectory = m_dataDirectory +
"/maps";
156 QDirIterator it( cachedMapsDirectory,
157 QDir::NoDotAndDotDot | QDir::Dirs );
159 while ( it.hasNext() &&
164 QString planetDirectory = it.filePath();
165 QFileInfo fileInfo = it.fileInfo();
169 if( fileInfo.fileName() == shownPlanet ) {
170 lastPlanet = planetDirectory;
174 ensureSizePerPlanet( planetDirectory );
177 if( keepDeleting() ) {
178 ensureSizePerPlanet( lastPlanet, currentList.first() );
184 QTimer::singleShot( 100,
this, SLOT(ensureCacheSize()) );
192 if( m_currentCacheSize > m_cacheSoftLimit ) {
193 mDebug() <<
"FileStorageWatcher: Could not set cache size.";
202 void FileStorageWatcherThread::ensureSizePerPlanet(
const QString &planetDirectory,
203 const QString ¤tTheme )
205 mDebug() <<
"Deleting from folder: " << planetDirectory;
210 QDirIterator itPlanet( planetDirectory,
211 QDir::NoDotAndDotDot | QDir::Dirs );
212 while( itPlanet.hasNext() &&
215 QString themeDirectory = itPlanet.filePath();
216 QFileInfo fileInfo = itPlanet.fileInfo();
220 if( !currentTheme.isEmpty() && fileInfo.fileName() == currentTheme ) {
221 mDebug() <<
"FileStorageWatcher: Skipping " << themeDirectory
223 lastTheme = themeDirectory;
227 ensureSizePerTheme( themeDirectory );
230 if( keepDeleting() ) {
231 mDebug() <<
"Removing files of: "
233 ensureSizePerTheme( lastTheme );
239 return s1.toInt() > s2.toInt();
242 void FileStorageWatcherThread::ensureSizePerTheme(
const QString &themeDirectory )
244 mDebug() <<
"Deleting from folder: " << themeDirectory;
247 QStringList folders =
248 QDir( themeDirectory ).entryList( QDir::Dirs
249 | QDir::NoDotAndDotDot );
252 QStringListIterator itTheme( folders );
253 while ( itTheme.hasNext() &&
255 QString subDirectory = itTheme.next();
262 QString tileDirectory = themeDirectory +
'/' + subDirectory;
264 QDirIterator itTile( tileDirectory,
265 QDir::Files | QDir::NoSymLinks,
266 QDirIterator::Subdirectories );
267 while ( itTile.hasNext() &&
270 QString filePath = itTile.filePath();
271 QString lowerCase = filePath.toLower();
273 QFileInfo info( filePath );
279 if ( ( lowerCase.endsWith( QLatin1String(
".jpg" ) )
280 || lowerCase.endsWith( QLatin1String(
".png" ) )
281 || lowerCase.endsWith( QLatin1String(
".gif" ) )
282 || lowerCase.endsWith( QLatin1String(
".svg" ) ) )
283 && ( info.lastModified().secsTo( QDateTime::currentDateTime() )
286 mDebug() <<
"FileStorageWatcher: Delete "
289 m_currentCacheSize -= info.size();
290 QFile::remove( filePath );
296 bool FileStorageWatcherThread::keepDeleting()
const
298 return ( ( m_currentCacheSize > m_cacheSoftLimit ) &&
308 m_dataDirectory( dataDirectory )
310 if ( m_dataDirectory.isEmpty() )
313 if ( ! QDir( m_dataDirectory ).exists() )
314 QDir::root().mkpath( m_dataDirectory );
317 m_themeLimitMutex =
new QMutex();
325 mDebug() <<
"Deleting FileStorageWatcher";
333 if( !wait( 5000 ) ) {
334 mDebug() <<
"Failed to stop FileStorageWatcher-Thread, terminating!";
340 delete m_themeLimitMutex;
345 QMutexLocker locker( m_themeLimitMutex );
374 QMutexLocker locker( m_themeLimitMutex );
386 m_themeLimitMutex->lock();
391 m_themeLimitMutex->unlock();
397 connect(
this, SIGNAL(
cleared()),
412 #include "FileStorageWatcher.moc"
void variableChanged()
Is emitted when a variable has changed.
static const int softLimitPercent
static QString localPath()
FileStorageWatcher(const QString &dataDirectory=QString(), QObject *parent=0)
Creates a new FileStorageWatcher, which is a thread watching the space Marble takes on the hard drive...
void addToCurrentSize(qint64 bytes)
Add bytes to the current cache size.
void sizeChanged(qint64 bytes)
const int maxBaseTileLevel
quint64 cacheLimit()
Returns the limit of the cache in bytes.
void run()
The function being called at starting Thread.
void resetCurrentSize()
Setting current cache size to 0.
static bool greaterThanByNumber(const QString &s1, const QString &s2)
void getCurrentCacheSize()
Getting the current size of the data stored on the disc.
void addToCurrentSize(qint64 bytes)
Add bytes to the current cache size.
void updateTheme(const QString &mapTheme)
Updates the name of the theme.
static const int maxFilesDelete
static const int deleteOnlyFilesOlderThan
void updateTheme(const QString &mapTheme)
Updates the name of the theme.
void setCacheLimit(quint64 bytes)
Sets the limit of the cache in bytes.
~FileStorageWatcherThread()
void resetCurrentSize()
Setting current cache size to 0.
FileStorageWatcherThread(const QString &dataDirectory, QObject *parent=0)
QDebug mDebug()
a function to replace qDebug() in Marble library code
void setCacheLimit(quint64 bytes)
Sets the limit of the cache in bytes.
void prepareQuit()
Stop doing things that take a long time to quit.