From 296bc328412f0cfec2318d04abd4717a4c70461c Mon Sep 17 00:00:00 2001 From: Shane Kearns Date: Wed, 19 Oct 2011 14:00:28 +0100 Subject: Fix FTP example to handle failure to open network session The example code only dealt with successful opening of the session. If the session open failed, the application is stuck because the connect button is disabled. Moved the session open to be part of connection. Handled session open failure by puttin the UI back in the default state where connection button is enabled. Task-Number: QTBUG-9909 Reviewed-By: Miikka Heikkinen (cherry picked from commit 104c22a68c422152ff3cf03eb3615e7826fefbd0) Change-Id: Ifa40fcd8b83c43cda364b3ec5e58f80b539aa244 Reviewed-by: Peter Hartmann --- examples/network/qftp/ftpwindow.cpp | 56 +++++++++++++++++++++---------------- examples/network/qftp/ftpwindow.h | 3 ++ 2 files changed, 35 insertions(+), 24 deletions(-) (limited to 'examples') diff --git a/examples/network/qftp/ftpwindow.cpp b/examples/network/qftp/ftpwindow.cpp index b0d2e5d9d8..ba568d6ec0 100644 --- a/examples/network/qftp/ftpwindow.cpp +++ b/examples/network/qftp/ftpwindow.cpp @@ -99,29 +99,6 @@ FtpWindow::FtpWindow(QWidget *parent) mainLayout->addWidget(buttonBox); setLayout(mainLayout); - QNetworkConfigurationManager manager; - if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { - // Get saved network configuration - QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); - settings.beginGroup(QLatin1String("QtNetwork")); - const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); - settings.endGroup(); - - // If the saved network configuration is not currently discovered use the system default - QNetworkConfiguration config = manager.configurationFromIdentifier(id); - if ((config.state() & QNetworkConfiguration::Discovered) != - QNetworkConfiguration::Discovered) { - config = manager.defaultConfiguration(); - } - - networkSession = new QNetworkSession(config, this); - connect(networkSession, SIGNAL(opened()), this, SLOT(enableConnectButton())); - - connectButton->setEnabled(false); - statusLabel->setText(tr("Opening network session.")); - networkSession->open(); - } - setWindowTitle(tr("FTP")); } @@ -154,6 +131,37 @@ void FtpWindow::connectOrDisconnect() setCursor(Qt::WaitCursor); #endif + if (!networkSession || !networkSession->isOpen()) { + if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { + if (!networkSession) { + // Get saved network configuration + QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); + settings.beginGroup(QLatin1String("QtNetwork")); + const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); + settings.endGroup(); + + // If the saved network configuration is not currently discovered use the system default + QNetworkConfiguration config = manager.configurationFromIdentifier(id); + if ((config.state() & QNetworkConfiguration::Discovered) != + QNetworkConfiguration::Discovered) { + config = manager.defaultConfiguration(); + } + + networkSession = new QNetworkSession(config, this); + connect(networkSession, SIGNAL(opened()), this, SLOT(connectToFtp())); + connect(networkSession, SIGNAL(error(QNetworkSession::SessionError)), this, SLOT(enableConnectButton())); + } + connectButton->setEnabled(false); + statusLabel->setText(tr("Opening network session.")); + networkSession->open(); + return; + } + } + connectToFtp(); +} + +void FtpWindow::connectToFtp() +{ //![1] ftp = new QFtp(this); connect(ftp, SIGNAL(commandFinished(int,bool)), @@ -392,7 +400,7 @@ void FtpWindow::enableConnectButton() settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id); settings.endGroup(); - connectButton->setEnabled(networkSession->isOpen()); + connectButton->setEnabled(true); statusLabel->setText(tr("Please enter the name of an FTP server.")); } diff --git a/examples/network/qftp/ftpwindow.h b/examples/network/qftp/ftpwindow.h index a9df99d2b3..f060bfc0ce 100644 --- a/examples/network/qftp/ftpwindow.h +++ b/examples/network/qftp/ftpwindow.h @@ -43,6 +43,7 @@ #include #include +#include QT_BEGIN_NAMESPACE class QDialogButtonBox; @@ -71,6 +72,7 @@ private slots: void connectOrDisconnect(); void downloadFile(); void cancelDownload(); + void connectToFtp(); void ftpCommandFinished(int commandId, bool error); void addToList(const QUrlInfo &urlInfo); @@ -101,6 +103,7 @@ private: QFile *file; QNetworkSession *networkSession; + QNetworkConfigurationManager manager; //![1] }; -- cgit v1.2.3