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

KDE's Doxygen guidelines are available online.