• Skip to content
  • Skip to link menu
KDE API Documentation - DownloadManager.h Source File (GCompris-qt)
  • KDE Home
  • Contact Us
 

GCompris-qt

  • src
  • core
DownloadManager.h
1 /* GCompris - DownloadManager.h
2  *
3  * Copyright (C) 2014 Holger Kaelberer <holger.k@elberer.de>
4  *
5  * Authors:
6  * Holger Kaelberer <holger.k@elberer.de>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef DOWNLOADMANAGER_H
23 #define DOWNLOADMANAGER_H
24 
25 #include <QCryptographicHash>
26 #include <QFile>
27 #include <QNetworkAccessManager>
28 #include <QNetworkReply>
29 #include <QMutex>
30 #include <QNetworkConfiguration>
31 #include <QString>
32 #include <QUrl>
33 #include <QVariant>
34 #include <QQmlEngine>
35 #include <QJSEngine>
36 
81 class DownloadManager : public QObject
82 {
83  Q_OBJECT
84 
85 private:
86  DownloadManager(); // prohibit external creation, we are a singleton!
87  static DownloadManager* _instance; // singleton instance
88 
90  typedef struct DownloadJob
91  {
92  QUrl url;
93  QFile file;
94  QNetworkReply *reply;
95  QList<QUrl> queue;
96  QMap<QString,QString> contents;
97  QList<QUrl> knownContentsUrls;
98 
99  DownloadJob(QUrl u = QUrl()) : url(u), file(), reply(0),
100  queue(QList<QUrl>()) {}
101  } DownloadJob;
102 
103  QList<DownloadJob*> activeJobs;
104  QMutex jobsMutex;
105 
106  static const QString contentsFilename;
107  static const QCryptographicHash::Algorithm hashMethod = QCryptographicHash::Md5;
108 
109  QList<QString> registeredResources;
110  QMutex rcMutex;
111 
112  QNetworkAccessManager accessManager;
113  QUrl serverUrl;
114 
125  QString getSystemDownloadPath() const;
126 
146  QStringList getSystemResourcePaths() const;
147  QString getResourceRootForFilename(const QString& filename) const;
148  QString getFilenameForUrl(const QUrl& url) const;
149  QUrl getUrlForFilename(const QString& filename) const;
150 
158  QString getAbsoluteResourcePath(const QString& path) const;
159 
166  QString getRelativeResourcePath(const QString& path) const;
167  QString tempFilenameForFilename(const QString &filename) const;
168  QString filenameForTempFilename(const QString &tempFilename) const;
169 
170  bool checkDownloadRestriction() const;
171  DownloadJob* getJobByReply(QNetworkReply *r);
172 
174  bool download(DownloadJob* job);
175 
177  bool parseContents(DownloadJob *job);
178 
181  bool checksumMatches(DownloadJob *job, const QString& filename) const;
182 
183  bool registerResource(const QString& filename);
184 
189  void unregisterResource_locked(const QString& filename);
190  bool isRegistered(const QString& filename) const;
191 
192  QStringList getLocalResources();
193 
194 private slots:
195 
201  void downloadFinished();
202  void downloadReadyRead();
203  void handleError(QNetworkReply::NetworkError code);
204 
205 public:
206  // public interface:
207 
211  enum DownloadFinishedCode {
212  Success = 0,
213  Error = 1,
214  NoChange = 2
215  };
216 
217  virtual ~DownloadManager();
218 
222  static void init();
223  static QObject *systeminfoProvider(QQmlEngine *engine,
224  QJSEngine *scriptEngine);
225  static DownloadManager* getInstance();
226 
234  Q_INVOKABLE QString getVoicesResourceForLocale(const QString& locale) const;
235 
241  Q_INVOKABLE bool haveLocalResource(const QString& path) const;
242 
246  Q_INVOKABLE bool downloadIsRunning() const;
247 
259  Q_INVOKABLE bool isDataRegistered(const QString& data) const;
260 
261 
267  Q_INVOKABLE bool areVoicesRegistered() const;
268 
269 public slots:
270 
287  Q_INVOKABLE bool updateResource(const QString& path);
288 
301  Q_INVOKABLE bool downloadResource(const QString& path);
302 
308  Q_INVOKABLE void shutdown();
309 
313  Q_INVOKABLE void abortDownloads();
314 
315 #if 0
316  Q_INVOKABLE bool checkForUpdates(); // might be helpful later with other use-cases!
317  Q_INVOKABLE void registerLocalResources();
318 #endif
319 
320 signals:
321 
327  void error(int code, const QString& msg);
328 
333  void downloadStarted(const QString& resource);
334 
342  void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
343 
352  void downloadFinished(int code);
353 
360  void resourceRegistered(const QString& resource);
361 
368  void voicesRegistered();
369 };
370 
371 #endif /* DOWNLOADMANAGER_H */
DownloadManager::shutdown
Q_INVOKABLE void shutdown()
Shutdown DownloadManager instance.
Definition: DownloadManager.cpp:56
DownloadManager::abortDownloads
Q_INVOKABLE void abortDownloads()
Abort all currently running downloads.
Definition: DownloadManager.cpp:94
DownloadManager::Success
Download finished successfully.
Definition: DownloadManager.h:212
DownloadManager::downloadStarted
void downloadStarted(const QString &resource)
Emitted when a download has started.
DownloadManager::resourceRegistered
void resourceRegistered(const QString &resource)
Emitted when a resource has been registered.
DownloadManager::NoChange
Local files are up-to-date, no download was needed.
Definition: DownloadManager.h:214
DownloadManager
A singleton class responsible for downloading, updating and maintaining remote resources.
Definition: DownloadManager.h:81
DownloadManager::updateResource
Q_INVOKABLE bool updateResource(const QString &path)
Updates a resource path from the upstream server unless prohibited by the settings and registers it i...
Definition: DownloadManager.cpp:150
DownloadManager::downloadProgress
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted during a running download.
DownloadManager::downloadResource
Q_INVOKABLE bool downloadResource(const QString &path)
Download a resource specified by the relative resource path and register it if possible.
Definition: DownloadManager.cpp:167
DownloadManager::init
static void init()
Registers DownloadManager singleton in the QML engine.
Definition: DownloadManager.cpp:82
DownloadManager::downloadIsRunning
Q_INVOKABLE bool downloadIsRunning() const
Whether any download is currently running.
Definition: DownloadManager.cpp:89
DownloadManager::error
void error(int code, const QString &msg)
Emitted when a download error occurs.
DownloadManager::isDataRegistered
Q_INVOKABLE bool isDataRegistered(const QString &data) const
Whether the passed relative data is registered.
Definition: DownloadManager.cpp:476
DownloadManager::getVoicesResourceForLocale
Q_INVOKABLE QString getVoicesResourceForLocale(const QString &locale) const
Generates a relative voices resources file-path for a given locale.
Definition: DownloadManager.cpp:120
DownloadManager::voicesRegistered
void voicesRegistered()
Emitted when voices resources for current locale have been registered.
DownloadManager::DownloadFinishedCode
DownloadFinishedCode
Possible return codes of a finished download.
Definition: DownloadManager.h:211
DownloadManager::areVoicesRegistered
Q_INVOKABLE bool areVoicesRegistered() const
Whether voices for the currently active locale are registered.
Definition: DownloadManager.cpp:483
DownloadManager::haveLocalResource
Q_INVOKABLE bool haveLocalResource(const QString &path) const
Checks whether the given relative resource path exists locally.
Definition: DownloadManager.cpp:145
DownloadManager::Error
Download error.
Definition: DownloadManager.h:213
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Tue Jun 2 2015 21:47:47 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

GCompris-qt

Skip menu "GCompris-qt"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Modules

Class Picker

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal