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 <QDebug>
10#include <QString>
11#include <QHash>
12#include <QUrl>
13#include <QImage>
14#include <QByteArray>
15#include <QFileInfo>
16#include <QCryptographicHash>
17
18// Marble
19#include "HttpDownloadManager.h"
20#include "FileStoragePolicy.h"
21#include "MarbleDirs.h"
22#include "MarbleGlobal.h"
23#include "MarbleDebug.h"
24
25namespace Marble
26{
27
28class RemoteIconLoaderPrivate
29{
30 public:
31 RemoteIconLoaderPrivate()
32 : m_storagePolicy(MarbleDirs::localPath() + QLatin1String("/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() + QLatin1String("/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,
109 SLOT(storeIcon(QByteArray,QString)) );
110}
111
112RemoteIconLoader::~RemoteIconLoader()
113{
114 delete d;
115}
116
117
118QImage RemoteIconLoader::load( const QUrl& url )
119{
120 /*
121 * If image has been downloaded previously then
122 * return it from m_iconCache. All the downloaded
123 * images in current running marble session are stored
124 * in m_iconCache.
125 */
126 if ( d->isCached( url ) ) {
127 return d->cachedIcon( url );
128 }
129
130 /*
131 * All the downloaded images are saved on disk
132 * at location cache/icons/ ( relative location ).
133 * If the rquested icon is present at this location then
134 * return it.
135 */
136 else if ( d->loadFromDiskCache( url ) ) {
137 return d->cachedIcon( url );
138 }
139
140 // Otherwise initiate download
141 else {
142 d->initiateDownload( url );
143 }
144 return QImage();
145}
146
147
148void RemoteIconLoader::storeIcon( const QByteArray &data, const QString &fileName)
149{
150 QImage icon = QImage::fromData( data );
151 d->m_iconCache.insert( QUrl(fileName), icon );
152 emit iconReady();
153}
154
155}
156
157#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
iterator insert(const Key &key, const T &value)
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 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.