hydrogen 1.2.6
DownloadWidget.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2025 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
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 2 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 https://www.gnu.org/licenses
20 *
21 */
22
23#include "DownloadWidget.h"
24
25#include <cmath>
26#include <cstdlib>
27#include <QNetworkReply>
28#include <QtNetwork>
29
30
31Download::Download( QWidget* pParent, const QString& download_url, const QString& local_file )
32 : QDialog( pParent )
34 , __eta( 0 )
35 , __bytes_current( 0 )
36 , __bytes_total( 0 )
37 , __remote_url( download_url )
38 , __local_file( local_file )
39 , __reply(nullptr)
40 , __error( "" )
41{
42 if ( !__local_file.isEmpty() ) {
43 INFOLOG( QString( "Downloading '%1' in '%2'" ).arg( __remote_url.toString() ).arg( __local_file ) );
44 } else {
45 INFOLOG( QString( "Downloading '%1'" ).arg( __remote_url.toString() ) );
46 }
47
48 __http_client = new QNetworkAccessManager(this);
49
50 QString sEnvHttpProxy = QString( getenv( "http_proxy" ) );
51 int nEnvHttpPort = 0;
52 QString sEnvHttpUser = QString( getenv( "http_user" ) );
53 QString sEnvHttpPassword = QString( getenv( "http_password" ) );
54
55 nEnvHttpPort = sEnvHttpProxy.right( sEnvHttpProxy.length() - sEnvHttpProxy.indexOf(':') - 1 ).toInt();
56 sEnvHttpProxy = sEnvHttpProxy.left( sEnvHttpProxy.indexOf(':') );
57
58 __time.start();
59
60 if ( ( !sEnvHttpProxy.isNull() ) && ( nEnvHttpPort != 0 ) ) {
61 QNetworkProxy proxy;
62 proxy.setType( QNetworkProxy::DefaultProxy );
63 proxy.setHostName( sEnvHttpProxy );
64 proxy.setPort( nEnvHttpPort );
65 proxy.setUser( sEnvHttpUser );
66 proxy.setPassword( sEnvHttpPassword );
67 __http_client->setProxy(proxy);
68 }
69
70 QNetworkRequest getReq;
71 getReq.setUrl( __remote_url );
72 getReq.setRawHeader( "User-Agent" , "Hydrogen" );
73
74 __reply = __http_client->get( getReq );
75
76 connect(__reply, SIGNAL(finished()),this, SLOT(finished()));
77 connect(__reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
78}
79
80
81
85
88{
89 if ( __reply->error() ) {
90 __error = QString( tr( "Importing item failed: %1" ) ).arg( __reply->errorString() );
92 reject();
93 return;
94 }
95
96
97 int StatusAttribute = __reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
98 if(StatusAttribute >= 200 && StatusAttribute < 300){
99 //do nothing, handling will be done later..
100 } else if(StatusAttribute >= 300 && StatusAttribute < 400){
101 QVariant RedirectAttribute = __reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
102
103 if ( RedirectAttribute != 0 ) {
104
105 __redirect_url = __remote_url.resolved(RedirectAttribute.toUrl());
106 __reply->deleteLater();
107 INFOLOG( QString( "Download redirected to '%1'" ).arg( __redirect_url.toString() ) );
108
109 reject();
110 return;
111 }
112 }
113
114 INFOLOG( "Download completed. " );
115
116 if ( __local_file.isEmpty() ) {
117 // store the text received only when not using the file.
118 __feed_xml_string = QString( __reply->readAll() );
119 } else {
120 QFile file( __local_file );
121
122 if ( !file.open( QIODevice::WriteOnly ) ) {
123 ERRORLOG( QString( "Unable to save %1" ).arg( __local_file ) );
124 } else {
125 file.write(__reply->readAll());
126 file.flush();
127 file.close();
128 }
129 }
130 accept();
131}
132
133
134
135void Download::downloadProgress( qint64 done, qint64 total )
136{
137 __bytes_current = done;
138 __bytes_total = total;
139
140 __download_percent = ( float )done / ( float )total * 100.0;
141}
142
143
144// :::::::::::::::::::..
145
146
147
148DownloadWidget::DownloadWidget( QWidget* parent, const QString& title, const QString& __remote_url, const QString& local_file )
149 : Download( parent, __remote_url, local_file )
150{
151 setWindowTitle( title );
152 setModal( true );
153
154 setFixedSize( 500, 100 );
155
156 QFont boldFont;
157 boldFont.setBold( true );
158
159 __url_label = new QLabel( nullptr );
160 __url_label->setFont( boldFont );
161 __url_label->setAlignment( Qt::AlignCenter );
162 __url_label->setText( QFileInfo( __remote_url ).fileName() );
163
164 __progress_bar = new QProgressBar( nullptr );
165
166 __progress_bar->setMinimum( 0 );
167 __progress_bar->setMaximum( 100 );
168
169 __eta_label = new QLabel( nullptr );
170 __eta_label->setAlignment( Qt::AlignHCenter );
171
172
173 QVBoxLayout* pVBox = new QVBoxLayout();
174 pVBox->addWidget( __url_label );
175 pVBox->addWidget( __progress_bar );
176 pVBox->addWidget( __eta_label );
177
178
179 setLayout( pVBox );
180
181 __update_timer = new QTimer( this );
182 connect( __update_timer, SIGNAL( timeout() ), this, SLOT( updateStats() ) );
183
184 __close_timer = new QTimer( this );
185 connect( __close_timer, SIGNAL( timeout() ), this, SLOT( close() ) );
186
187 __update_timer->start( 100 );
188}
189
190
191
197
198
199
201{
202 if ( __download_percent > 0 ) {
203 __eta = ( int )( round( ( __time.elapsed() / __download_percent * ( 100 - __download_percent ) ) / 1000 ) );
204 }
205
206 __progress_bar->setValue( get_percent_done() );
207
208 QString hours = QString( "%1" ).arg( __eta / 60 / 60 );
209 QString minutes = QString( "%1" ).arg( ( __eta / 60 ) % 60 );
210 QString seconds = QString( "%1" ).arg( __eta % 60 );
211
212 hours = hours.rightJustified( 2, '0' );
213 minutes = minutes.rightJustified( 2, '0' );
214 seconds = seconds.rightJustified( 2, '0' );
215
216 QString sETA = hours + ":" + minutes + ":" + seconds;
217
218 __eta_label->setText( tr( "(%1/%2 KiB) - ETA %3" ).arg( __bytes_current / 1024 ).arg( __bytes_total / 1024 ).arg( sETA ) );
219
220 if ( __download_percent == 100 ) {
221 __update_timer->stop();
222
223 __close_timer->start( 1000 ); // close the window after 1 second
224 }
225}
226
#define INFOLOG(x)
Definition Object.h:240
#define ERRORLOG(x)
Definition Object.h:242
QTimer * __update_timer
QProgressBar * __progress_bar
QLabel * __url_label
QTimer * __close_timer
QLabel * __eta_label
DownloadWidget(QWidget *parent, const QString &title, const QString &download_url, const QString &local_file="")
qint64 __bytes_current
Download(QWidget *parent, const QString &download_url, const QString &local_file)
qint64 __bytes_total
float __download_percent
QNetworkAccessManager * __http_client
QUrl __remote_url
QString __error
QString __local_file
QNetworkReply * __reply
int get_percent_done()
QString __feed_xml_string
QElapsedTimer __time
void downloadProgress(qint64 done, qint64 total)
QUrl __redirect_url
void finished()
TODO: I have to save the file to disk on a temporary dir and then move it if everything is ok.