Marble

RemoteIconLoader.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2014 Abhinav Gangwar <[email protected]>
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 
25 namespace Marble
26 {
27 
28 class 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 
67 bool RemoteIconLoaderPrivate::isCached( const QUrl& url ) const
68 {
69  return m_iconCache.contains( url );
70 }
71 
72 QImage RemoteIconLoaderPrivate::cachedIcon( const QUrl& url ) const
73 {
74  return m_iconCache.value( url );
75 }
76 
77 bool 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 
88 void RemoteIconLoaderPrivate::initiateDownload( const QUrl& url )
89 {
91  m_downloadManager.setDownloadEnabled(true);
92  QString fileName = cacheFileName( url );
93  m_downloadManager.addJob(url, fileName, url.toString(), usage );
94 }
95 
96 QString 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 
104 RemoteIconLoader::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 
112 RemoteIconLoader::~RemoteIconLoader()
113 {
114  delete d;
115 }
116 
117 
118 QImage 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 
148 void 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"
@ DownloadBrowse
Browsing mode, normal operation of Marble, like a web browser.
Definition: MarbleGlobal.h:155
QByteArray toEncoded(QUrl::FormattingOptions options) const const
DownloadUsage
This enum is used to describe the type of download.
Definition: MarbleGlobal.h:153
QString suffix() const const
bool exists() const const
QString toString(QUrl::FormattingOptions options) const const
bool isNull() const const
Binds a QML item to a specific geodetic location in screen coordinates.
QByteArray hash(const QByteArray &data, QCryptographicHash::Algorithm method)
QByteArray toHex() const const
QString path(QUrl::ComponentFormattingOptions options) const const
QString path(const QString &relativePath)
QString fromLatin1(const char *str, int size)
QImage fromData(const uchar *data, int size, const char *format)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:12:28 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.