Marble

RemoteIconLoader.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2014 Abhinav Gangwar <abhgang@gmail.com>
4//
5
6#include "RemoteIconLoader.h"
7
8// Qt
9#include <QByteArray>
10#include <QCryptographicHash>
11#include <QDebug>
12#include <QFileInfo>
13#include <QHash>
14#include <QImage>
15#include <QString>
16#include <QUrl>
17
18// Marble
19#include "FileStoragePolicy.h"
20#include "HttpDownloadManager.h"
21#include "MarbleDebug.h"
22#include "MarbleDirs.h"
23#include "MarbleGlobal.h"
24
25namespace Marble
26{
27
28class RemoteIconLoaderPrivate
29{
30public:
31 RemoteIconLoaderPrivate()
32 : m_storagePolicy(MarbleDirs::localPath() + QLatin1StringView("/cache/icons/"))
33 , m_downloadManager(&m_storagePolicy)
34 {
35 }
36 QHash<QUrl, QImage> m_iconCache;
37 FileStoragePolicy m_storagePolicy;
38 HttpDownloadManager m_downloadManager;
39
40 /**
41 * Returns true if the icon for Url(=url) is available in cache
42 */
43 bool isCached(const QUrl &url) const;
44
45 /**
46 * Returns icon for the url passes in argument
47 */
48 QImage cachedIcon(const QUrl &url) const;
49
50 /**
51 * Returns true if icon is locally present on disk
52 */
53 bool loadFromDiskCache(const QUrl &url);
54
55 /**
56 * Starts downloading icon if it isn't present cache and
57 * could not be found locally on disk
58 */
59 void initiateDownload(const QUrl &url);
60
61 /**
62 * Returns a name with which downloaded icon will be saved on disk
63 */
64 static QString cacheFileName(const QUrl &url);
65};
66
67bool RemoteIconLoaderPrivate::isCached(const QUrl &url) const
68{
69 return m_iconCache.contains(url);
70}
71
72QImage RemoteIconLoaderPrivate::cachedIcon(const QUrl &url) const
73{
74 return m_iconCache.value(url);
75}
76
77bool RemoteIconLoaderPrivate::loadFromDiskCache(const QUrl &url)
78{
79 QString path = MarbleDirs::localPath() + QLatin1StringView("/cache/icons/") + cacheFileName(url);
80 QImage icon = QFile::exists(path) ? QImage(path) : QImage();
81 if (!icon.isNull()) {
82 m_iconCache.insert(url, icon);
83 return true;
84 }
85 return false;
86}
87
88void RemoteIconLoaderPrivate::initiateDownload(const QUrl &url)
89{
90 DownloadUsage usage = DownloadBrowse;
91 m_downloadManager.setDownloadEnabled(true);
92 QString fileName = cacheFileName(url);
93 m_downloadManager.addJob(url, fileName, url.toString(), usage);
94}
95
96QString RemoteIconLoaderPrivate::cacheFileName(const QUrl &url)
97{
98 const QString suffix = QFileInfo(url.path()).suffix();
100 const QString fileName = QString::fromLatin1(hash) + QLatin1Char('.') + suffix;
101 return fileName;
102}
103
104RemoteIconLoader::RemoteIconLoader(QObject *parent)
105 : QObject(parent)
106 , d(new RemoteIconLoaderPrivate())
107{
108 connect(&d->m_downloadManager, SIGNAL(downloadComplete(QByteArray, QString)), this, SLOT(storeIcon(QByteArray, QString)));
109}
110
111RemoteIconLoader::~RemoteIconLoader()
112{
113 delete d;
114}
115
116QImage RemoteIconLoader::load(const QUrl &url)
117{
118 /*
119 * If image has been downloaded previously then
120 * return it from m_iconCache. All the downloaded
121 * images in current running marble session are stored
122 * in m_iconCache.
123 */
124 if (d->isCached(url)) {
125 return d->cachedIcon(url);
126 }
127
128 /*
129 * All the downloaded images are saved on disk
130 * at location cache/icons/ ( relative location ).
131 * If the rquested icon is present at this location then
132 * return it.
133 */
134 else if (d->loadFromDiskCache(url)) {
135 return d->cachedIcon(url);
136 }
137
138 // Otherwise initiate download
139 else {
140 d->initiateDownload(url);
141 }
142 return {};
143}
144
145void RemoteIconLoader::storeIcon(const QByteArray &data, const QString &fileName)
146{
147 QImage icon = QImage::fromData(data);
148 d->m_iconCache.insert(QUrl(fileName), icon);
149 Q_EMIT iconReady();
150}
151
152}
153
154#include "moc_RemoteIconLoader.cpp"
void setDownloadEnabled(const bool enable)
Switches loading on/off, useful for offline mode.
void addJob(const QUrl &sourceUrl, const QString &destFilename, const QString &id, const DownloadUsage usage)
Adds a new job with a sourceUrl, destination file name and given id.
QString path(const QString &relativePath)
Binds a QML item to a specific geodetic location in screen coordinates.
@ DownloadBrowse
Browsing mode, normal operation of Marble, like a web browser.
QByteArray toHex(char separator) const const
QByteArray hash(QByteArrayView data, Algorithm method)
bool exists() const const
QString suffix() const const
bool contains(const Key &key) const const
T value(const Key &key) const const
QImage fromData(QByteArrayView data, const char *format)
bool isNull() const const
QString fromLatin1(QByteArrayView str)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString path(ComponentFormattingOptions options) const const
QByteArray toEncoded(FormattingOptions options) const const
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:37:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.