Fix qt warnings thrown by QgsFileDownloader

This commit is contained in:
Nyall Dawson 2017-10-19 07:21:17 +10:00
parent 587072cae9
commit 30da28a7b0
3 changed files with 19 additions and 9 deletions

24
src/core/qgsfiledownloader.cpp Executable file → Normal file
View File

@ -25,14 +25,14 @@
#include <QSslError>
#endif
QgsFileDownloader::QgsFileDownloader(const QUrl &url, const QString &outputFileName, const QString &authcfg , bool delayStart)
QgsFileDownloader::QgsFileDownloader( const QUrl &url, const QString &outputFileName, const QString &authcfg, bool delayStart )
: mUrl( url )
, mDownloadCanceled( false )
{
mFile.setFileName( outputFileName );
mAuthCfg = authcfg;
if ( !delayStart )
startDownload();
startDownload();
}
@ -124,7 +124,12 @@ void QgsFileDownloader::error( const QString &errorMessage )
void QgsFileDownloader::onReadyRead()
{
Q_ASSERT( mReply );
if ( ! mFile.isOpen() && ! mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
if ( mFile.fileName().isEmpty() )
{
error( tr( "No output filename specified" ) );
onFinished();
}
else if ( ! mFile.isOpen() && ! mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
error( tr( "Cannot open output file: %1" ).arg( mFile.fileName() ) );
onFinished();
@ -141,14 +146,19 @@ void QgsFileDownloader::onFinished()
// when canceled
if ( ! mErrors.isEmpty() || mDownloadCanceled )
{
mFile.close();
mFile.remove();
if ( mFile.isOpen() )
mFile.close();
if ( mFile.exists() )
mFile.remove();
}
else
{
// download finished normally
mFile.flush();
mFile.close();
if ( mFile.isOpen() )
{
mFile.flush();
mFile.close();
}
// get redirection url
QVariant redirectionTarget = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );

2
tests/src/gui/testqgsfiledownloader.cpp Executable file → Normal file
View File

@ -185,7 +185,7 @@ void TestQgsFileDownloader::testInvalidFile()
QVERIFY( !mCompleted );
QVERIFY( mError );
QVERIFY( !mCanceled );
QCOMPARE( mErrorMessage, QString( "Cannot open output file: " ) );
QCOMPARE( mErrorMessage, QString( "No output filename specified" ) );
}
void TestQgsFileDownloader::testInvalidUrl()

View File

@ -107,7 +107,7 @@ class TestQgsFileDownloader(unittest.TestCase):
self.assertFalse(self.completed_was_called)
self.assertFalse(self.canceled_was_called)
self.assertTrue(self.error_was_called)
self.assertEqual(self.error_args[1], [u"Cannot open output file: "])
self.assertEqual(self.error_args[1], [u"No output filename specified"])
def test_BlankUrl(self):
destination = tempfile.mktemp()