[QGIS Commit] r10608 - in trunk/qgis/src: app core providers/wms ui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Tue Apr 21 03:46:54 EDT 2009


Author: timlinux
Date: 2009-04-21 03:46:54 -0400 (Tue, 21 Apr 2009)
New Revision: 10608

Modified:
   trunk/qgis/src/app/qgsnewhttpconnection.cpp
   trunk/qgis/src/app/qgsserversourceselect.cpp
   trunk/qgis/src/core/qgshttptransaction.cpp
   trunk/qgis/src/core/qgshttptransaction.h
   trunk/qgis/src/providers/wms/qgswmsprovider.cpp
   trunk/qgis/src/providers/wms/qgswmsprovider.h
   trunk/qgis/src/ui/qgsnewhttpconnectionbase.ui
Log:
Applied last minute patch for http auth support in wms (see ticket #1603).


Modified: trunk/qgis/src/app/qgsnewhttpconnection.cpp
===================================================================
--- trunk/qgis/src/app/qgsnewhttpconnection.cpp	2009-04-20 21:25:53 UTC (rev 10607)
+++ trunk/qgis/src/app/qgsnewhttpconnection.cpp	2009-04-21 07:46:54 UTC (rev 10608)
@@ -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();
 }

Modified: trunk/qgis/src/app/qgsserversourceselect.cpp
===================================================================
--- trunk/qgis/src/app/qgsserversourceselect.cpp	2009-04-20 21:25:53 UTC (rev 10607)
+++ trunk/qgis/src/app/qgsserversourceselect.cpp	2009-04-21 07:46:54 UTC (rev 10608)
@@ -39,6 +39,7 @@
 #include <QDomDocument>
 #include <QHeaderView>
 #include <QImageReader>
+#include <QInputDialog>
 #include <QMap>
 #include <QMessageBox>
 #include <QPicture>
@@ -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(),

Modified: trunk/qgis/src/core/qgshttptransaction.cpp
===================================================================
--- trunk/qgis/src/core/qgshttptransaction.cpp	2009-04-20 21:25:53 UTC (rev 10607)
+++ trunk/qgis/src/core/qgshttptransaction.cpp	2009-04-21 07:46:54 UTC (rev 10608)
@@ -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 ) )
   {

Modified: trunk/qgis/src/core/qgshttptransaction.h
===================================================================
--- trunk/qgis/src/core/qgshttptransaction.h	2009-04-20 21:25:53 UTC (rev 10607)
+++ trunk/qgis/src/core/qgshttptransaction.h	2009-04-21 07:46:54 UTC (rev 10608)
@@ -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

Modified: trunk/qgis/src/providers/wms/qgswmsprovider.cpp
===================================================================
--- trunk/qgis/src/providers/wms/qgswmsprovider.cpp	2009-04-20 21:25:53 UTC (rev 10607)
+++ trunk/qgis/src/providers/wms/qgswmsprovider.cpp	2009-04-21 07:46:54 UTC (rev 10608)
@@ -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 ) ),

Modified: trunk/qgis/src/providers/wms/qgswmsprovider.h
===================================================================
--- trunk/qgis/src/providers/wms/qgswmsprovider.h	2009-04-20 21:25:53 UTC (rev 10607)
+++ trunk/qgis/src/providers/wms/qgswmsprovider.h	2009-04-21 07:46:54 UTC (rev 10608)
@@ -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<int, int> mLayerParents;
     QMap<int, QStringList> mLayerParentNames;
 
+    //! Username for basic http authentication
+    QString mUserName;
+
+    //! Password for basic http authentication
+    QString mPassword;
+
 };
 
 #endif

Modified: trunk/qgis/src/ui/qgsnewhttpconnectionbase.ui
===================================================================
--- trunk/qgis/src/ui/qgsnewhttpconnectionbase.ui	2009-04-20 21:25:53 UTC (rev 10607)
+++ trunk/qgis/src/ui/qgsnewhttpconnectionbase.ui	2009-04-21 07:46:54 UTC (rev 10608)
@@ -5,8 +5,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>431</width>
-    <height>159</height>
+    <width>512</width>
+    <height>254</height>
    </rect>
   </property>
   <property name="windowTitle" >
@@ -38,7 +38,7 @@
         </property>
        </widget>
       </item>
-      <item row="0" column="1" >
+      <item row="0" column="1" colspan="4" >
        <widget class="QLineEdit" name="txtName" >
         <property name="minimumSize" >
          <size>
@@ -67,13 +67,63 @@
         </property>
        </widget>
       </item>
-      <item row="1" column="1" >
+      <item row="1" column="1" colspan="4" >
        <widget class="QLineEdit" name="txtUrl" >
         <property name="toolTip" >
          <string>HTTP address of the Web Map Server</string>
         </property>
        </widget>
       </item>
+      <item row="2" column="0" colspan="5" >
+       <widget class="QLabel" name="label" >
+        <property name="text" >
+         <string>If the WMS requires basic authentication, enter a user name and optional password</string>
+        </property>
+        <property name="textFormat" >
+         <enum>Qt::PlainText</enum>
+        </property>
+        <property name="wordWrap" >
+         <bool>true</bool>
+        </property>
+       </widget>
+      </item>
+      <item row="3" column="0" colspan="2" >
+       <widget class="QLabel" name="label_2" >
+        <property name="text" >
+         <string>User name</string>
+        </property>
+       </widget>
+      </item>
+      <item row="3" column="2" >
+       <widget class="QLineEdit" name="txtUserName" >
+        <property name="maximumSize" >
+         <size>
+          <width>120</width>
+          <height>16777215</height>
+         </size>
+        </property>
+       </widget>
+      </item>
+      <item row="3" column="3" >
+       <widget class="QLabel" name="label_3" >
+        <property name="text" >
+         <string>Password</string>
+        </property>
+       </widget>
+      </item>
+      <item row="3" column="4" >
+       <widget class="QLineEdit" name="txtPassword" >
+        <property name="maximumSize" >
+         <size>
+          <width>120</width>
+          <height>120</height>
+         </size>
+        </property>
+        <property name="echoMode" >
+         <enum>QLineEdit::Password</enum>
+        </property>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>



More information about the QGIS-commit mailing list