1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2002-2013 Werner Schweer
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2
9 // as published by the Free Software Foundation and appearing in
10 // the file LICENCE.GPL
11 //=============================================================================
12
13 #include "downloadUtils.h"
14 #include <QTimer>
15
16 namespace Ms {
17
DownloadUtils(QWidget * parent)18 DownloadUtils::DownloadUtils(QWidget *parent)
19 : QObject(parent)
20 {
21 }
22
saveFile()23 bool DownloadUtils::saveFile()
24 {
25 QFile localFile(_localFile);
26 if (!localFile.open(QIODevice::WriteOnly))
27 {
28 qDebug() << "can't access";
29 return false;
30 }
31 qDebug() << "here writing to file " << _localFile;
32 localFile.write(sdata);
33 localFile.close();
34 return true;
35 }
36
downloadFinished(QNetworkReply * data)37 void DownloadUtils::downloadFinished(QNetworkReply *data)
38 {
39 sdata = data->readAll();
40 emit done();
41 }
42
returnData()43 QByteArray DownloadUtils::returnData()
44 {
45 return sdata;
46 }
47
download(bool showProgress,const int timeOutMSecs)48 void DownloadUtils::download(bool showProgress, const int timeOutMSecs)
49 {
50 QUrl url = QUrl::fromEncoded(_target.toLocal8Bit());
51 QNetworkRequest request(url);
52 QEventLoop loop;
53 QNetworkReply* reply = manager.get(request);
54
55 QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
56 QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
57 QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
58
59 QTimer timer;
60 timer.setSingleShot(true);
61 timer.start(timeOutMSecs);
62 QObject::connect(&timer, &QTimer::timeout, this, [reply] () {
63 reply->abort();
64 });
65
66 QObject::connect(reply, &QNetworkReply::finished, &timer, &QTimer::stop);
67
68 if (showProgress) {
69 progressDialog = new QProgressDialog(static_cast<QWidget*>(parent()));
70 progressDialog->setWindowFlags(Qt::WindowFlags(Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowTitleHint));
71 progressDialog->setWindowModality(Qt::ApplicationModal);
72 progressDialog->setCancelButtonText(tr("Cancel"));
73 progressDialog->setLabelText(tr("Downloading…"));
74 progressDialog->setAutoClose(true);
75 progressDialog->setAutoReset(true);
76 QObject::connect(progressDialog, SIGNAL(canceled()), &loop, SLOT(quit()));
77 progressDialog->show();
78 }
79
80 loop.exec();
81
82 QObject::disconnect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
83 QObject::disconnect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
84 QObject::disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
85 }
86
downloadProgress(qint64 received,qint64 total)87 void DownloadUtils::downloadProgress(qint64 received, qint64 total)
88 {
89 double curVal = (double(received)/total)*100;
90 if (progressDialog && progressDialog->isVisible())
91 progressDialog->setValue(curVal);
92 }
93
94 }
95