BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
downloader.h
1 /*
2  Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  * Neither the name of the BuildmLearn nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 
32 #ifndef DOWNLOADER_H
33 #define DOWNLOADER_H
34 
35 #include <QObject>
36 
37 #include <QNetworkReply>
38 #include <QSslError>
39 
40 #include "definitions/definitions.h"
41 
42 
44 class QTimer;
45 
46 /// \brief Simple file downloader with progress reporting.
47 class Downloader : public QObject {
48  Q_OBJECT
49 
50  public:
51  /// \brief Constructor.
52  /// \param parent Parent to this instance.
53  explicit Downloader(QObject *parent = 0);
54  virtual ~Downloader();
55 
56  public slots:
57  /// \brief Performs asynchronous download of given file. Redirections are handled.
58  /// \param url URL of file to be downloaded.
59  /// \param protected_contents Are contents of URL protected?
60  /// \param username Username if contents are protected.
61  /// \param password Password if contents are protected.
62  void downloadFile(const QString &url,
63  bool protected_contents = false,
64  const QString &username = QString(),
65  const QString &password = QString());
66 
67  /// \brief Uploads given bundle_data to store server via HTTP POST.
68  /// \param url URL to store server
69  /// \param bundle_data
70  void uploadBundleFile(QString url, const QString &bundle_data,
71  const QString &key, const QString &author_name,
72  const QString &author_email, const QString &application_name,
73  const QString &application_icon);
74 
75  signals:
76  /// \brief Emitted when new progress is known.
77  /// \param bytes_received Number of bytes received.
78  /// \param bytes_total Number of bytes total.
79  void progress(qint64 bytes_received, qint64 bytes_total);
80 
81  /// \brief Emitted if file download or upload completes (un)successfully.
82  /// \param status Status of file download/upload.
83  /// \param contents QByteArray containing downloaded file (if any) or post reply.
84  void completed(QNetworkReply::NetworkError status, QByteArray contents = QByteArray());
85 
86  private slots:
87  // Called when current reply is processed.
88  void finished(QNetworkReply *reply);
89 
90  // Called when progress of downloaded file changes.
91  void progressInternal(qint64 bytes_received, qint64 bytes_total);
92 
93  // Called when current operation times out.
94  void timeout();
95 
96  private:
97  // Issues new network requests.
98  void runGetRequest(const QNetworkRequest &request);
99  void runPostRequest(const QNetworkRequest &request, const QByteArray &data);
100 
101  private:
102  QNetworkReply *m_activeReply;
103  SilentNetworkAccessManager *m_downloadManager;
104  QTimer *m_timer;
105 };
106 
107 #endif // DOWNLOADER_H
void completed(QNetworkReply::NetworkError status, QByteArray contents=QByteArray())
Emitted if file download or upload completes (un)successfully.
Simple file downloader with progress reporting.
Definition: downloader.h:47
Network access manager with supressed authentication dialogs.
void progress(qint64 bytes_received, qint64 bytes_total)
Emitted when new progress is known.
void downloadFile(const QString &url, bool protected_contents=false, const QString &username=QString(), const QString &password=QString())
Performs asynchronous download of given file. Redirections are handled.
Definition: downloader.cpp:57
Downloader(QObject *parent=0)
Constructor.
Definition: downloader.cpp:40
void uploadBundleFile(QString url, const QString &bundle_data, const QString &key, const QString &author_name, const QString &author_email, const QString &application_name, const QString &application_icon)
Uploads given bundle_data to store server via HTTP POST.
Definition: downloader.cpp:73