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.cpp
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 #include "network-web/downloader.h"
33 
34 #include "network-web/silentnetworkaccessmanager.h"
35 #include "miscellaneous/iofactory.h"
36 
37 #include <QTimer>
38 
39 
40 Downloader::Downloader(QObject *parent)
41  : QObject(parent),
42  m_activeReply(NULL),
43  m_downloadManager(new SilentNetworkAccessManager(this)),
44  m_timer(new QTimer(this)) {
45 
46  m_timer->setInterval(2000);
47  m_timer->setSingleShot(true);
48 
49  //connect(m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
50  connect(m_downloadManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
51 }
52 
53 Downloader::~Downloader() {
54  m_downloadManager->deleteLater();
55 }
56 
57 void Downloader::downloadFile(const QString &url, bool protected_contents,
58  const QString &username, const QString &password) {
59  QNetworkRequest request;
60  QObject originatingObject;
61 
62  // Set credential information as originating object.
63  originatingObject.setProperty("protected", protected_contents);
64  originatingObject.setProperty("username", username);
65  originatingObject.setProperty("password", password);
66  request.setOriginatingObject(&originatingObject);
67 
68  // Set url for this reques.
69  request.setUrl(url);
70  runGetRequest(request);
71 }
72 
73 void Downloader::uploadBundleFile(QString url, const QString &bundle_data,
74  const QString &key, const QString &author_name,
75  const QString &author_email, const QString &application_name,
76  const QString &application_icon) {
77  QNetworkRequest request;
78 
79  request.setUrl(url);
80  request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
81 
82  QString data;
83 
84  data += "key=%1&";
85  data += "author_name=%2&";
86  data += "author_email=%3&";
87  data += "application_name=%4&";
88  data += "file_content=%5&";
89  data += "application_icon=%6";
90 
91  // Replace placeholders with actual URL-encoded values.
92  data = data.arg(QUrl::toPercentEncoding(key),
93  QUrl::toPercentEncoding(author_name),
94  QUrl::toPercentEncoding(author_email),
95  QUrl::toPercentEncoding(application_name),
96  QUrl::toPercentEncoding(bundle_data),
97  QUrl::toPercentEncoding(IOFactory::fileToBase64(application_icon)));
98 /*
99  m_timer->start();
100  m_activeReply = m_downloadManager->post(request, data.toLocal8Bit());
101 
102  connect(m_activeReply, SIGNAL(uploadProgress(qint64,qint64)),
103  this, SLOT(progressInternal(qint64,qint64)));
104 */
105  runPostRequest(request, data.toLocal8Bit());
106 }
107 
108 void Downloader::finished(QNetworkReply *reply) {
109  m_timer->stop();
110 
111  // In this phase, some part of downloading process is completed.
112  QUrl redirection_url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
113 
114  if (redirection_url.isValid()) {
115  // Communication indicates that HTTP redirection is needed.
116  // Setup redirection URL and download again.
117  QNetworkRequest request = reply->request();
118  request.setUrl(redirection_url);
119 
120  m_activeReply->deleteLater();
121  m_activeReply = NULL;
122 
123  runGetRequest(request);
124  }
125  else {
126  // No redirection is indicated. Final file is obtained
127  // in our "reply" object.
128 
129  // Read the data into output buffer.
130  QByteArray output = reply->readAll();
131  QNetworkReply::NetworkError reply_error = reply->error();
132 
133  m_activeReply->deleteLater();
134  m_activeReply = NULL;
135 
136  emit completed(reply_error, output);
137  }
138 }
139 
140 void Downloader::progressInternal(qint64 bytes_received, qint64 bytes_total) {
141  if (m_timer->interval() > 0) {
142  m_timer->start();
143  }
144 
145  emit progress(bytes_received, bytes_total);
146 }
147 
148 void Downloader::timeout() {
149  if (m_activeReply != NULL) {
150  m_activeReply->abort();
151  }
152 }
153 
154 void Downloader::runGetRequest(const QNetworkRequest &request) {
155  m_timer->start();
156  m_activeReply = m_downloadManager->get(request);
157 
158  connect(m_activeReply, SIGNAL(downloadProgress(qint64,qint64)),
159  this, SLOT(progressInternal(qint64,qint64)));
160 }
161 
162 void Downloader::runPostRequest(const QNetworkRequest &request, const QByteArray &data) {
163  m_timer->start();
164  m_activeReply = m_downloadManager->post(request, data);
165 
166  connect(m_activeReply, SIGNAL(uploadProgress(qint64,qint64)),
167  this, SLOT(progressInternal(qint64,qint64)));
168 }
169 
170 
void completed(QNetworkReply::NetworkError status, QByteArray contents=QByteArray())
Emitted if file download or upload completes (un)successfully.
static QByteArray fileToBase64(const QString &file_name)
Takes input file and reads it into base64 byte array.
Definition: iofactory.cpp:111
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