32 #include "network-web/downloader.h"
34 #include "network-web/silentnetworkaccessmanager.h"
35 #include "miscellaneous/iofactory.h"
44 m_timer(new QTimer(this)) {
46 m_timer->setInterval(2000);
47 m_timer->setSingleShot(
true);
50 connect(m_downloadManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finished(QNetworkReply*)));
53 Downloader::~Downloader() {
54 m_downloadManager->deleteLater();
58 const QString &username,
const QString &password) {
59 QNetworkRequest request;
60 QObject originatingObject;
63 originatingObject.setProperty(
"protected", protected_contents);
64 originatingObject.setProperty(
"username", username);
65 originatingObject.setProperty(
"password", password);
66 request.setOriginatingObject(&originatingObject);
70 runGetRequest(request);
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;
80 request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
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";
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),
105 runPostRequest(request, data.toLocal8Bit());
108 void Downloader::finished(QNetworkReply *reply) {
112 QUrl redirection_url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
114 if (redirection_url.isValid()) {
117 QNetworkRequest request = reply->request();
118 request.setUrl(redirection_url);
120 m_activeReply->deleteLater();
121 m_activeReply = NULL;
123 runGetRequest(request);
130 QByteArray output = reply->readAll();
131 QNetworkReply::NetworkError reply_error = reply->error();
133 m_activeReply->deleteLater();
134 m_activeReply = NULL;
140 void Downloader::progressInternal(qint64 bytes_received, qint64 bytes_total) {
141 if (m_timer->interval() > 0) {
145 emit
progress(bytes_received, bytes_total);
148 void Downloader::timeout() {
149 if (m_activeReply != NULL) {
150 m_activeReply->abort();
154 void Downloader::runGetRequest(
const QNetworkRequest &request) {
156 m_activeReply = m_downloadManager->get(request);
158 connect(m_activeReply, SIGNAL(downloadProgress(qint64,qint64)),
159 this, SLOT(progressInternal(qint64,qint64)));
162 void Downloader::runPostRequest(
const QNetworkRequest &request,
const QByteArray &data) {
164 m_activeReply = m_downloadManager->post(request, data);
166 connect(m_activeReply, SIGNAL(uploadProgress(qint64,qint64)),
167 this, SLOT(progressInternal(qint64,qint64)));
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.
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.
Downloader(QObject *parent=0)
Constructor.
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.