Index: src/app/qgsnewhttpconnection.cpp =================================================================== --- src/app/qgsnewhttpconnection.cpp (revision 10589) +++ src/app/qgsnewhttpconnection.cpp (working copy) @@ -35,8 +35,12 @@ QSettings settings; QString key = mBaseKey + connName; + QString credentialsKey = "/Qgis/WMS/" + connName; txtName->setText( connName ); txtUrl->setText( settings.value( key + "/url" ).toString() ); + txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() ); + txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() ); + } connect( buttonBox, SIGNAL( helpRequested() ), this, SLOT( helpRequested() ) ); } @@ -49,13 +53,17 @@ { QSettings settings; QString key = mBaseKey + txtName->text(); + QString credentialsKey = "/Qgis/WMS/" + txtName->text(); //delete original entry first if ( !mOriginalConnName.isNull() && mOriginalConnName != key ) { settings.remove( mBaseKey + mOriginalConnName ); + settings.remove ( "/Qgis/WMS/" + mOriginalConnName ); } settings.setValue( key + "/url", txtUrl->text().trimmed() ); + settings.setValue( credentialsKey + "/username", txtUserName->text() ); + settings.setValue( credentialsKey + "/password", txtPassword->text() ); QDialog::accept(); } Index: src/app/qgsserversourceselect.cpp =================================================================== --- src/app/qgsserversourceselect.cpp (revision 10589) +++ src/app/qgsserversourceselect.cpp (working copy) @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -363,6 +364,7 @@ QSettings settings; QString key = "/Qgis/connections-wms/" + cmbConnections->currentText(); + QString credentialsKey = "/Qgis/WMS/" + cmbConnections->currentText(); QStringList connStringParts; QString part; @@ -372,6 +374,21 @@ m_connName = cmbConnections->currentText(); m_connectionInfo = connStringParts.join( " " ); + // Check for credentials and prepend to the connection info + QString username = settings.value( credentialsKey + "/username" ).toString(); + QString password = settings.value( credentialsKey + "/password" ).toString(); + if ( !username.isEmpty() ) + { + // check for a password, if none prompt to get it + if ( password.isEmpty() ) + { + password = QInputDialog::getText( this, tr( "WMS Password for " ) + m_connName, "Password", QLineEdit::Password ); + + } + m_connectionInfo = "username=" + username + ",password=" + password + ",url=" + m_connectionInfo; + } + + QgsDebugMsg( QString( "Connection info: '%1'." ).arg( m_connectionInfo ) ); @@ -774,6 +791,7 @@ } #endif } + // Get username/password from settings for protected WMS QUrl url( QString( "http://geopole.org/wms/search?search=%1&type=rss" ).arg( searchTerm ) ); QgsHttpTransaction http( url.toEncoded(), Index: src/core/qgshttptransaction.h =================================================================== --- src/core/qgshttptransaction.h (revision 10589) +++ src/core/qgshttptransaction.h (working copy) @@ -42,13 +42,16 @@ public: /** * Constructor. + * \note userName and password added in 1.1 */ QgsHttpTransaction( QString uri, QString proxyHost = QString(), int proxyPort = 80, QString proxyUser = QString(), QString proxyPass = QString(), - QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy ); + QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy, + QString userName = QString(), + QString password = QString() ); //! Destructor virtual ~QgsHttpTransaction(); @@ -86,7 +89,14 @@ @param return true if proxy settings was applied, false else*/ static bool applyProxySettings( QHttp& http, const QString& url ); + /** + * Set the credentials (username and password) + * \note added in 1.1 + */ + void setCredentials( const QString& username, const QString &password ); + + public slots: void dataStarted( int id ); @@ -188,6 +198,15 @@ */ QString mError; + /** + * User name + */ + QString mUserName; + + /** + * Password + */ + QString mPassword; }; #endif Index: src/core/qgshttptransaction.cpp =================================================================== --- src/core/qgshttptransaction.cpp (revision 10589) +++ src/core/qgshttptransaction.cpp (working copy) @@ -34,12 +34,18 @@ static int NETWORK_TIMEOUT_MSEC = ( 120 * 1000 ); // 120 seconds static int HTTP_PORT_DEFAULT = 80; +//XXX Set the connection name when creating the provider instance +//XXX in qgswmsprovider. When creating a QgsHttpTransaction, pass +//XXX the user/pass combination to the constructor. Then set the +//XXX username and password using QHttp::setUser. QgsHttpTransaction::QgsHttpTransaction( QString uri, QString proxyHost, int proxyPort, QString proxyUser, QString proxyPass, - QNetworkProxy::ProxyType proxyType ) + QNetworkProxy::ProxyType proxyType, + QString userName, + QString password ) : httpresponsecontenttype( 0 ), httpurl( uri ), httphost( proxyHost ), @@ -53,6 +59,11 @@ } +void QgsHttpTransaction::setCredentials( const QString& username, const QString& password ) +{ + mUserName = username; + mPassword = password; +} void QgsHttpTransaction::getAsynchronously() { @@ -67,6 +78,7 @@ QgsDebugMsg( "Entered." ); QgsDebugMsg( "Using '" + httpurl + "'." ); + QgsDebugMsg( "Creds: " + mUserName + "/" + mPassword ); int httpport; @@ -88,6 +100,12 @@ header.setValue( "User-agent", QString( "Quantum GIS - " ) + VERSION ); // Set the host in the QHttp object http->setHost( qurl.host(), qurl.port( HTTP_PORT_DEFAULT ) ); + // Set the username and password if supplied for this connection + // If we have username and password set in header + if ( !mUserName.isEmpty() && !mPassword.isEmpty() ) + { + http->setUser( mUserName, mPassword ); + } if ( !QgsHttpTransaction::applyProxySettings( *http, httpurl ) ) { Index: src/providers/wms/qgswmsprovider.h =================================================================== --- src/providers/wms/qgswmsprovider.h (revision 10589) +++ src/providers/wms/qgswmsprovider.h (working copy) @@ -422,6 +422,12 @@ */ void setImageCrs( QString const & crs ); + /** + * Set the name of the connection for use in authentication where required + * \note added in 1.1 + */ + void setConnectionName( QString const & connName); + // TODO: Document this better. /** \brief Renders the layer as an image * @@ -687,6 +693,17 @@ bool calculateExtent(); /** + * \brief Check for authentication information contained in the uri, + * stripping and saving the username and password if present. + * + * \param uri uri to check + * + * \note added in 1.1 + */ + + void setAuthentication( QString uri ); + + /** * \brief Prepare the URI so that we can later simply append param=value * \param uri uri to prepare * \retval prepared uri @@ -696,6 +713,9 @@ //! Data source URI of the WMS for this layer QString httpuri; + //! Name of the stored connection + QString connectionName; + //! URL part of URI (httpuri) QString baseUrl; @@ -823,6 +843,12 @@ QMap mLayerParents; QMap mLayerParentNames; + //! Username for basic http authentication + QString mUserName; + + //! Password for basic http authentication + QString mPassword; + }; #endif Index: src/providers/wms/qgswmsprovider.cpp =================================================================== --- src/providers/wms/qgswmsprovider.cpp (revision 10589) +++ src/providers/wms/qgswmsprovider.cpp (working copy) @@ -62,9 +62,17 @@ extentDirty( TRUE ), mGetFeatureInfoUrlBase( 0 ), mLayerCount( -1 ) + { - QgsDebugMsg( "QgsWmsProvider: constructing with uri '" + uri + "'." ); + // URL may contain username/password information for a WMS + // requiring authentication. In this case the URL is prefixed + // with username=user,password=pass,url=http://xxx.xxx.xx/yyy... + mUserName= ""; + mPassword = ""; + setAuthentication( httpuri ); + QgsDebugMsg( "QgsWmsProvider: constructing with uri '" + httpuri + "'." ); + // assume this is a valid layer until we determine otherwise valid = true; @@ -100,6 +108,39 @@ QgsDebugMsg( "QgsWmsProvider: exiting constructor." ); } +void QgsWmsProvider::setAuthentication( QString uri ) +{ + // Strip off and store the user name and password (if they exist) + if ( ! uri.startsWith(" http:" ) ) + { + // uri potentially contains username and password + QStringList parts = uri.split( "," ); + QStringListIterator iter( parts ); + while ( iter.hasNext() ) + { + QString item = iter.next(); + QgsDebugMsg( "QgsWmsProvider: Testing for creds: " + item ); + if ( item.startsWith( "username=" ) ) + { + mUserName = item.mid( 9 ); + QgsDebugMsg( "QgsWmsProvider: Set username to " + mUserName ); + } + else if ( item.startsWith( "password=" ) ) + { + mPassword = item.mid( 9 ); + QgsDebugMsg( "QgsWmsProvider: Set password to " + mPassword ); + } + else if ( item.startsWith( "url=" ) ) + { + // strip the authentication information from the front of the uri + httpuri = item.mid( 4 ); + QgsDebugMsg( "QgsWmsProvider: Set httpuri to " + httpuri ); + } + } + + } + +} QString QgsWmsProvider::prepareUri( QString uri ) { if ( !( uri.contains( "?" ) ) ) @@ -221,6 +262,10 @@ QgsDebugMsg( "Exiting." ); } +void QgsWmsProvider::setConnectionName( QString const &connName ) +{ + connectionName = connName; +} void QgsWmsProvider::setLayerOrder( QStringList const &layers ) { @@ -640,7 +685,9 @@ { QgsDebugMsg( "WMS request Url: " + url ); QgsHttpTransaction http( url ); - + QgsDebugMsg( "Setting creds: " + mUserName + "/" + mPassword ); + http.setCredentials( mUserName, mPassword ); + // Do a passthrough for the status bar text connect( &http, SIGNAL( statusChanged( QString ) ), Index: src/ui/qgsnewhttpconnectionbase.ui =================================================================== --- src/ui/qgsnewhttpconnectionbase.ui (revision 10589) +++ src/ui/qgsnewhttpconnectionbase.ui (working copy) @@ -5,8 +5,8 @@ 0 0 - 431 - 159 + 606 + 264 @@ -24,8 +24,38 @@ Connection details + + + + 14 + 112 + 553 + 48 + + + + If the WMS requires basic authentication, enter a user name and optional password + + + Qt::PlainText + + + true + + + + + + 15 + 34 + 552 + 68 + + + + Name @@ -38,7 +68,24 @@ + + + + URL + + + 5 + + + txtUrl + + + + + + + @@ -54,28 +101,79 @@ - - + + + + HTTP address of the Web Map Server + + + + + + + + + + + 20 + 150 + 68 + 56 + + + + + - URL + User name - - 5 + + + + + + Password - - txtUrl + + + + + + + + 98 + 151 + 122 + 56 + + + + + + + + 120 + 16777215 + - - - - HTTP address of the Web Map Server + + + + + 120 + 120 + + + + QLineEdit::Password +