Marble

FileStoragePolicy.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
4//
5
6
7// Own
8#include "FileStoragePolicy.h"
9
10// Qt
11#include <QDir>
12#include <QDirIterator>
13#include <QFile>
14#include <QFileInfo>
15
16// Marble
17#include "MarbleDebug.h"
18#include "MarbleGlobal.h"
19#include "MarbleDirs.h"
20
21using namespace Marble;
22
23FileStoragePolicy::FileStoragePolicy( const QString &dataDirectory, QObject *parent )
24 : StoragePolicy( parent ),
25 m_dataDirectory( dataDirectory )
26{
27 if ( m_dataDirectory.isEmpty() )
28 m_dataDirectory = MarbleDirs::localPath() + QLatin1String("/cache/");
29
30 if ( !QDir( m_dataDirectory ).exists() )
31 QDir::root().mkpath( m_dataDirectory );
32}
33
34FileStoragePolicy::~FileStoragePolicy()
35{
36}
37
38bool FileStoragePolicy::fileExists( const QString &fileName ) const
39{
40 const QString fullName = m_dataDirectory + QLatin1Char('/') + fileName;
41 return QFile::exists( fullName );
42}
43
44bool FileStoragePolicy::updateFile( const QString &fileName, const QByteArray &data )
45{
46 QFileInfo const dirInfo( fileName );
47 QString const fullName = dirInfo.isAbsolute() ? fileName : m_dataDirectory + QLatin1Char('/') + fileName;
48
49 // Create directory if it doesn't exist yet...
50 QFileInfo info( fullName );
51
52 const QDir localFileDir = info.dir();
53 const QString localFileDirPath = localFileDir.absolutePath();
54
55 if ( !QDir( localFileDirPath ).exists() )
57
58 // ... and save the file content
59 QFile file( fullName );
60 if ( !file.open( QIODevice::WriteOnly ) ) {
61 m_errorMsg = fullName + QLatin1String(": ") + file.errorString();
62 qCritical() << "file.open" << m_errorMsg;
63 return false;
64 }
65
66 quint64 oldSize = file.size();
67
68 if ( !file.write( data ) ) {
69 m_errorMsg = fullName + QLatin1String(": ") + file.errorString();
70 qCritical() << "file.write" << m_errorMsg;
71 emit sizeChanged( file.size() - oldSize );
72 return false;
73 }
74
75 emit sizeChanged( file.size() - oldSize );
76 file.close();
77
78 return true;
79}
80
81void FileStoragePolicy::clearCache()
82{
83 mDebug();
84 if ( m_dataDirectory.isEmpty()
85 || !(m_dataDirectory.endsWith(QLatin1String( "data" )) // on Windows
86 || m_dataDirectory.endsWith(QLatin1String( "marble" )) // on all other OSes
87 ))
88 {
89 mDebug() << "Data Directory:" << m_dataDirectory;
90 mDebug() << "Error: Refusing to erase files under unknown conditions for safety reasons!";
91 return;
92 }
93
94 const QString cachedMapsDirectory = m_dataDirectory + QLatin1String("/maps");
95
97 mDebug() << cachedMapsDirectory;
98 while (it.hasNext()) {
99 it.next();
100 QString planetDirectory = it.filePath();
102 while (itPlanet.hasNext()) {
103 itPlanet.next();
104 QString themeDirectory = itPlanet.filePath();
106 while (itTheme.hasNext()) {
107 itTheme.next();
108 QString tileDirectory = itTheme.filePath();
109
110 if ( itTheme.fileName().toInt() <= maxBaseTileLevel ) {
111 continue;
112 }
113
115 while (itTile.hasNext()) {
116 itTile.next();
117 QString filePath = itTile.filePath();
118 QString lowerCase = filePath.toLower();
119
120 // We try to be very careful and just delete images
121 // FIXME, when vectortiling I suppose also vector tiles will have
122 // to be deleted
123 if ( lowerCase.endsWith( QLatin1String( ".jpg" ) )
124 || lowerCase.endsWith( QLatin1String( ".png" ) )
125 || lowerCase.endsWith( QLatin1String( ".gif" ) )
126 || lowerCase.endsWith( QLatin1String( ".svg" ) )
127 || lowerCase.endsWith( QLatin1String( ".o5m" ) )
128 )
129 {
130 // We cannot emit clear, because we don't make a full clear
131 QFile file( filePath );
132 emit sizeChanged( -file.size() );
133 file.remove();
134 }
135 }
136 }
137 }
138 }
139}
140
141QString FileStoragePolicy::lastErrorMessage() const
142{
143 return m_errorMsg;
144}
145
146#include "moc_FileStoragePolicy.cpp"
QString fullName(const PartType &type)
Binds a QML item to a specific geodetic location in screen coordinates.
NoDotAndDotDot
bool mkpath(const QString &dirPath) const const
QDir root()
bool exists() const const
T qobject_cast(QObject *object)
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype size() const const
QString toLower() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:18:17 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.