File indexing completed on 2024-05-05 04:49:22

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Rick W. Chen <stuffcorpse@archlinux.us>                           *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "NetworkProgressBar.h"
0018 
0019 NetworkProgressBar::NetworkProgressBar( QWidget *parent, QNetworkReply *reply )
0020     : ProgressBar( parent )
0021 {
0022     connect( reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::errorOccurred),
0023              this, &NetworkProgressBar::infoMessage );
0024     connect( reply, &QNetworkReply::finished, this, &NetworkProgressBar::delayedDone );
0025     connect( reply, &QNetworkReply::destroyed, this, &NetworkProgressBar::delayedDone );
0026 
0027     switch( reply->operation() )
0028     {
0029     case QNetworkAccessManager::HeadOperation:
0030     case QNetworkAccessManager::GetOperation:
0031         connect( reply, &QNetworkReply::downloadProgress, this, &NetworkProgressBar::progressChanged );
0032         break;
0033 
0034     case QNetworkAccessManager::PutOperation:
0035     case QNetworkAccessManager::PostOperation:
0036         connect( reply, &QNetworkReply::uploadProgress, this, &NetworkProgressBar::progressChanged );
0037         break;
0038 
0039     default:
0040         break;
0041     }
0042 }
0043 
0044 NetworkProgressBar::~NetworkProgressBar()
0045 {
0046 }
0047 
0048 void NetworkProgressBar::progressChanged( qint64 bytesChanged, qint64 bytesTotal )
0049 {
0050     qreal percent = qreal(bytesChanged) / bytesTotal;
0051     setValue( int(percent) * 100 );
0052 }
0053 
0054 void NetworkProgressBar::infoMessage( QNetworkReply::NetworkError code )
0055 {
0056     if( code != QNetworkReply::NoError )
0057     {
0058         QNetworkReply *reply = qobject_cast<QNetworkReply*>( sender() );
0059         setDescription( reply->errorString() );
0060     }
0061 }
0062