Quantum GIS API Documentation  1.8
src/gui/qgsmanageconnectionsdialog.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgsmanageconnectionsdialog.cpp
00003     ---------------------
00004     begin                : Dec 2009
00005     copyright            : (C) 2009 by Alexander Bruy
00006     email                : alexander dot bruy at gmail dot com
00007 
00008  ***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 
00017 
00018 #include <QCloseEvent>
00019 #include <QFileDialog>
00020 #include <QMessageBox>
00021 #include <QPushButton>
00022 #include <QSettings>
00023 #include <QTextStream>
00024 
00025 #include "qgsmanageconnectionsdialog.h"
00026 
00027 QgsManageConnectionsDialog::QgsManageConnectionsDialog( QWidget *parent, Mode mode, Type type, QString fileName )
00028     : QDialog( parent )
00029     , mFileName( fileName )
00030     , mDialogMode( mode )
00031     , mConnectionType( type )
00032 {
00033   setupUi( this );
00034 
00035   // additional buttons
00036   QPushButton *pb;
00037   pb = new QPushButton( tr( "Select all" ) );
00038   buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
00039   connect( pb, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
00040 
00041   pb = new QPushButton( tr( "Clear selection" ) );
00042   buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
00043   connect( pb, SIGNAL( clicked() ), this, SLOT( clearSelection() ) );
00044 
00045   if ( mDialogMode == Import )
00046   {
00047     label->setText( tr( "Select connections to import" ) );
00048     buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Import" ) );
00049   }
00050   else
00051   {
00052     //label->setText( tr( "Select connections to export" ) );
00053     buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Export" ) );
00054   }
00055 
00056   if ( !populateConnections() )
00057   {
00058     QApplication::postEvent( this, new QCloseEvent() );
00059   }
00060 
00061   // use Ok button for starting import and export operations
00062   disconnect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
00063   connect( buttonBox, SIGNAL( accepted() ), this, SLOT( doExportImport() ) );
00064 }
00065 
00066 void QgsManageConnectionsDialog::doExportImport()
00067 {
00068   QList<QListWidgetItem *> selection = listConnections->selectedItems();
00069   if ( selection.isEmpty() )
00070   {
00071     QMessageBox::warning( this, tr( "Export/import error" ),
00072                           tr( "You should select at least one connection from list." ) );
00073     return;
00074   }
00075 
00076   QStringList items;
00077   for ( int i = 0; i < selection.size(); ++i )
00078   {
00079     items.append( selection.at( i )->text() );
00080   }
00081 
00082   if ( mDialogMode == Export )
00083   {
00084     QString fileName = QFileDialog::getSaveFileName( this, tr( "Save connections" ), ".",
00085                        tr( "XML files (*.xml *.XML)" ) );
00086     if ( fileName.isEmpty() )
00087     {
00088       return;
00089     }
00090 
00091     // ensure the user never ommited the extension from the file name
00092     if ( !fileName.toLower().endsWith( ".xml" ) )
00093     {
00094       fileName += ".xml";
00095     }
00096 
00097     mFileName = fileName;
00098 
00099     QDomDocument doc;
00100     switch ( mConnectionType )
00101     {
00102       case WMS:
00103         doc = saveWMSConnections( items );
00104         break;
00105       case WFS:
00106         doc = saveWFSConnections( items );
00107         break;
00108       case PostGIS:
00109         doc = savePgConnections( items );
00110         break;
00111       case MSSQL:
00112         doc = saveMssqlConnections( items );
00113         break;
00114     }
00115 
00116     QFile file( mFileName );
00117     if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
00118     {
00119       QMessageBox::warning( this, tr( "Saving connections" ),
00120                             tr( "Cannot write file %1:\n%2." )
00121                             .arg( mFileName )
00122                             .arg( file.errorString() ) );
00123       return;
00124     }
00125 
00126     QTextStream out( &file );
00127     doc.save( out, 4 );
00128   }
00129   else // import connections
00130   {
00131     QFile file( mFileName );
00132     if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
00133     {
00134       QMessageBox::warning( this, tr( "Loading connections" ),
00135                             tr( "Cannot read file %1:\n%2." )
00136                             .arg( mFileName )
00137                             .arg( file.errorString() ) );
00138       return;
00139     }
00140 
00141     QDomDocument doc;
00142     QString errorStr;
00143     int errorLine;
00144     int errorColumn;
00145 
00146     if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
00147     {
00148       QMessageBox::warning( this, tr( "Loading connections" ),
00149                             tr( "Parse error at line %1, column %2:\n%3" )
00150                             .arg( errorLine )
00151                             .arg( errorColumn )
00152                             .arg( errorStr ) );
00153       return;
00154     }
00155 
00156     switch ( mConnectionType )
00157     {
00158       case WMS:
00159         loadWMSConnections( doc, items );
00160         break;
00161       case WFS:
00162         loadWFSConnections( doc, items );
00163         break;
00164       case PostGIS:
00165         loadPgConnections( doc, items );
00166         break;
00167       case MSSQL:
00168         loadMssqlConnections( doc, items );
00169         break;
00170     }
00171     // clear connections list and close window
00172     listConnections->clear();
00173     accept();
00174   }
00175 
00176   mFileName = "";
00177 }
00178 
00179 bool QgsManageConnectionsDialog::populateConnections()
00180 {
00181   // Export mode. Populate connections list from settings
00182   if ( mDialogMode == Export )
00183   {
00184     QSettings settings;
00185     switch ( mConnectionType )
00186     {
00187       case WMS:
00188         settings.beginGroup( "/Qgis/connections-wms" );
00189         break;
00190       case WFS:
00191         settings.beginGroup( "/Qgis/connections-wfs" );
00192         break;
00193       case PostGIS:
00194         settings.beginGroup( "/PostgreSQL/connections" );
00195         break;
00196       case MSSQL:
00197         settings.beginGroup( "/MSSQL/connections" );
00198         break;
00199     }
00200     QStringList keys = settings.childGroups();
00201     QStringList::Iterator it = keys.begin();
00202     while ( it != keys.end() )
00203     {
00204       QListWidgetItem *item = new QListWidgetItem();
00205       item->setText( *it );
00206       listConnections->addItem( item );
00207       ++it;
00208     }
00209     settings.endGroup();
00210   }
00211   // Import mode. Populate connections list from file
00212   else
00213   {
00214     QFile file( mFileName );
00215     if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
00216     {
00217       QMessageBox::warning( this, tr( "Loading connections" ),
00218                             tr( "Cannot read file %1:\n%2." )
00219                             .arg( mFileName )
00220                             .arg( file.errorString() ) );
00221       return false;
00222     }
00223 
00224     QDomDocument doc;
00225     QString errorStr;
00226     int errorLine;
00227     int errorColumn;
00228 
00229     if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
00230     {
00231       QMessageBox::warning( this, tr( "Loading connections" ),
00232                             tr( "Parse error at line %1, column %2:\n%3" )
00233                             .arg( errorLine )
00234                             .arg( errorColumn )
00235                             .arg( errorStr ) );
00236       return false;
00237     }
00238 
00239     QDomElement root = doc.documentElement();
00240     switch ( mConnectionType )
00241     {
00242       case WMS:
00243         if ( root.tagName() != "qgsWMSConnections" )
00244         {
00245           QMessageBox::information( this, tr( "Loading connections" ),
00246                                     tr( "The file is not an WMS connections exchange file." ) );
00247           return false;
00248         }
00249         break;
00250 
00251       case WFS:
00252         if ( root.tagName() != "qgsWFSConnections" )
00253         {
00254           QMessageBox::information( this, tr( "Loading connections" ),
00255                                     tr( "The file is not an WFS connections exchange file." ) );
00256           return false;
00257         }
00258         break;
00259 
00260       case PostGIS:
00261         if ( root.tagName() != "qgsPgConnections" )
00262         {
00263           QMessageBox::information( this, tr( "Loading connections" ),
00264                                     tr( "The file is not an PostGIS connections exchange file." ) );
00265           return false;
00266         }
00267         break;
00268 
00269       case MSSQL:
00270         if ( root.tagName() != "qgsMssqlConnections" )
00271         {
00272           QMessageBox::information( this, tr( "Loading connections" ),
00273                                     tr( "The file is not an MSSQL connections exchange file." ) );
00274           return false;
00275         }
00276         break;
00277     }
00278 
00279     QDomElement child = root.firstChildElement();
00280     while ( !child.isNull() )
00281     {
00282       QListWidgetItem *item = new QListWidgetItem();
00283       item->setText( child.attribute( "name" ) );
00284       listConnections->addItem( item );
00285       child = child.nextSiblingElement();
00286     }
00287   }
00288   return true;
00289 }
00290 
00291 QDomDocument QgsManageConnectionsDialog::saveWMSConnections( const QStringList &connections )
00292 {
00293   QDomDocument doc( "connections" );
00294   QDomElement root = doc.createElement( "qgsWMSConnections" );
00295   root.setAttribute( "version", "1.0" );
00296   doc.appendChild( root );
00297 
00298   QSettings settings;
00299   QString path;
00300   for ( int i = 0; i < connections.count(); ++i )
00301   {
00302     path = "/Qgis/connections-wms/";
00303     QDomElement el = doc.createElement( "wms" );
00304     el.setAttribute( "name", connections[ i ] );
00305     el.setAttribute( "url", settings.value( path + connections[ i ] + "/url", "" ).toString() );
00306     el.setAttribute( "ignoreGetMapURI", settings.value( path + connections[i] + "/ignoreGetMapURI", false ).toBool() ? "true" : "false" );
00307     el.setAttribute( "ignoreGetFeatureInfoURI", settings.value( path + connections[i] + "/ignoreGetFeatureInfoURI", false ).toBool() ? "true" : "false" );
00308 
00309     path = "/Qgis/WMS/";
00310     el.setAttribute( "username", settings.value( path + connections[ i ] + "/username", "" ).toString() );
00311     el.setAttribute( "password", settings.value( path + connections[ i ] + "/password", "" ).toString() );
00312     root.appendChild( el );
00313   }
00314 
00315   return doc;
00316 }
00317 
00318 QDomDocument QgsManageConnectionsDialog::saveWFSConnections( const QStringList &connections )
00319 {
00320   QDomDocument doc( "connections" );
00321   QDomElement root = doc.createElement( "qgsWFSConnections" );
00322   root.setAttribute( "version", "1.0" );
00323   doc.appendChild( root );
00324 
00325   QSettings settings;
00326   QString path;
00327   for ( int i = 0; i < connections.count(); ++i )
00328   {
00329     path = "/Qgis/connections-wfs/";
00330     QDomElement el = doc.createElement( "wfs" );
00331     el.setAttribute( "name", connections[ i ] );
00332     el.setAttribute( "url", settings.value( path + connections[ i ] + "/url", "" ).toString() );
00333 
00334     path = "/Qgis/WFS/";
00335     el.setAttribute( "username", settings.value( path + connections[ i ] + "/username", "" ).toString() );
00336     el.setAttribute( "password", settings.value( path + connections[ i ] + "/password", "" ).toString() );
00337     root.appendChild( el );
00338   }
00339 
00340   return doc;
00341 }
00342 
00343 QDomDocument QgsManageConnectionsDialog::savePgConnections( const QStringList &connections )
00344 {
00345   QDomDocument doc( "connections" );
00346   QDomElement root = doc.createElement( "qgsPgConnections" );
00347   root.setAttribute( "version", "1.0" );
00348   doc.appendChild( root );
00349 
00350   QSettings settings;
00351   QString path;
00352   for ( int i = 0; i < connections.count(); ++i )
00353   {
00354     path = "/PostgreSQL/connections/" + connections[ i ];
00355     QDomElement el = doc.createElement( "postgis" );
00356     el.setAttribute( "name", connections[ i ] );
00357     el.setAttribute( "host", settings.value( path + "/host", "" ).toString() );
00358     el.setAttribute( "port", settings.value( path + "/port", "" ).toString() );
00359     el.setAttribute( "database", settings.value( path + "/database", "" ).toString() );
00360     el.setAttribute( "service", settings.value( path + "/service", "" ).toString() );
00361     el.setAttribute( "sslmode", settings.value( path + "/sslmode", "1" ).toString() );
00362     el.setAttribute( "estimatedMetadata", settings.value( path + "/estimatedMetadata", "0" ).toString() );
00363 
00364     el.setAttribute( "saveUsername", settings.value( path + "/saveUsername", "false" ).toString() );
00365 
00366     if ( settings.value( path + "/saveUsername", "false" ).toString() == "true" )
00367     {
00368       el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
00369     }
00370 
00371     el.setAttribute( "savePassword", settings.value( path + "/savePassword", "false" ).toString() );
00372 
00373     if ( settings.value( path + "/savePassword", "false" ).toString() == "true" )
00374     {
00375       el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
00376     }
00377 
00378     root.appendChild( el );
00379   }
00380 
00381   return doc;
00382 }
00383 
00384 QDomDocument QgsManageConnectionsDialog::saveMssqlConnections( const QStringList &connections )
00385 {
00386   QDomDocument doc( "connections" );
00387   QDomElement root = doc.createElement( "qgsMssqlConnections" );
00388   root.setAttribute( "version", "1.0" );
00389   doc.appendChild( root );
00390 
00391   QSettings settings;
00392   QString path;
00393   for ( int i = 0; i < connections.count(); ++i )
00394   {
00395     path = "/MSSQL/connections/" + connections[ i ];
00396     QDomElement el = doc.createElement( "mssql" );
00397     el.setAttribute( "name", connections[ i ] );
00398     el.setAttribute( "host", settings.value( path + "/host", "" ).toString() );
00399     el.setAttribute( "port", settings.value( path + "/port", "" ).toString() );
00400     el.setAttribute( "database", settings.value( path + "/database", "" ).toString() );
00401     el.setAttribute( "service", settings.value( path + "/service", "" ).toString() );
00402     el.setAttribute( "sslmode", settings.value( path + "/sslmode", "1" ).toString() );
00403     el.setAttribute( "estimatedMetadata", settings.value( path + "/estimatedMetadata", "0" ).toString() );
00404 
00405     el.setAttribute( "saveUsername", settings.value( path + "/saveUsername", "false" ).toString() );
00406 
00407     if ( settings.value( path + "/saveUsername", "false" ).toString() == "true" )
00408     {
00409       el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
00410     }
00411 
00412     el.setAttribute( "savePassword", settings.value( path + "/savePassword", "false" ).toString() );
00413 
00414     if ( settings.value( path + "/savePassword", "false" ).toString() == "true" )
00415     {
00416       el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
00417     }
00418 
00419     root.appendChild( el );
00420   }
00421 
00422   return doc;
00423 }
00424 
00425 void QgsManageConnectionsDialog::loadWMSConnections( const QDomDocument &doc, const QStringList &items )
00426 {
00427   QDomElement root = doc.documentElement();
00428   if ( root.tagName() != "qgsWMSConnections" )
00429   {
00430     QMessageBox::information( this, tr( "Loading connections" ),
00431                               tr( "The file is not an WMS connections exchange file." ) );
00432     return;
00433   }
00434 
00435   QString connectionName;
00436   QSettings settings;
00437   settings.beginGroup( "/Qgis/connections-wms" );
00438   QStringList keys = settings.childGroups();
00439   settings.endGroup();
00440   QDomElement child = root.firstChildElement();
00441   bool prompt = true;
00442   bool overwrite = true;
00443 
00444   while ( !child.isNull() )
00445   {
00446     connectionName = child.attribute( "name" );
00447     if ( !items.contains( connectionName ) )
00448     {
00449       child = child.nextSiblingElement();
00450       continue;
00451     }
00452 
00453     // check for duplicates
00454     if ( keys.contains( connectionName ) && prompt )
00455     {
00456       int res = QMessageBox::warning( this,
00457                                       tr( "Loading connections" ),
00458                                       tr( "Connection with name '%1' already exists. Overwrite?" )
00459                                       .arg( connectionName ),
00460                                       QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
00461 
00462       switch ( res )
00463       {
00464         case QMessageBox::Cancel:
00465           return;
00466         case QMessageBox::No:
00467           child = child.nextSiblingElement();
00468           continue;
00469         case QMessageBox::Yes:
00470           overwrite = true;
00471           break;
00472         case QMessageBox::YesToAll:
00473           prompt = false;
00474           overwrite = true;
00475           break;
00476         case QMessageBox::NoToAll:
00477           prompt = false;
00478           overwrite = false;
00479           break;
00480       }
00481     }
00482 
00483     if ( keys.contains( connectionName ) && !overwrite )
00484     {
00485       child = child.nextSiblingElement();
00486       continue;
00487     }
00488 
00489     // no dups detected or overwrite is allowed
00490     settings.beginGroup( "/Qgis/connections-wms" );
00491     settings.setValue( QString( "/" + connectionName + "/url" ) , child.attribute( "url" ) );
00492     settings.setValue( QString( "/" + connectionName + "/ignoreGetMapURI" ), child.attribute( "ignoreGetMapURI" ) == "true" );
00493     settings.setValue( QString( "/" + connectionName + "/ignoreGetFeatureInfoURI" ), child.attribute( "ignoreGetFeatureInfoURI" ) == "true" );
00494     settings.endGroup();
00495 
00496     if ( !child.attribute( "username" ).isEmpty() )
00497     {
00498       settings.beginGroup( "/Qgis/WMS/" + connectionName );
00499       settings.setValue( "/username", child.attribute( "username" ) );
00500       settings.setValue( "/password", child.attribute( "password" ) );
00501       settings.endGroup();
00502     }
00503     child = child.nextSiblingElement();
00504   }
00505 }
00506 
00507 void QgsManageConnectionsDialog::loadWFSConnections( const QDomDocument &doc, const QStringList &items )
00508 {
00509   QDomElement root = doc.documentElement();
00510   if ( root.tagName() != "qgsWFSConnections" )
00511   {
00512     QMessageBox::information( this, tr( "Loading connections" ),
00513                               tr( "The file is not an WFS connections exchange file." ) );
00514     return;
00515   }
00516 
00517   QString connectionName;
00518   QSettings settings;
00519   settings.beginGroup( "/Qgis/connections-wfs" );
00520   QStringList keys = settings.childGroups();
00521   settings.endGroup();
00522   QDomElement child = root.firstChildElement();
00523   bool prompt = true;
00524   bool overwrite = true;
00525 
00526   while ( !child.isNull() )
00527   {
00528     connectionName = child.attribute( "name" );
00529     if ( !items.contains( connectionName ) )
00530     {
00531       child = child.nextSiblingElement();
00532       continue;
00533     }
00534 
00535     // check for duplicates
00536     if ( keys.contains( connectionName ) && prompt )
00537     {
00538       int res = QMessageBox::warning( this,
00539                                       tr( "Loading connections" ),
00540                                       tr( "Connection with name '%1' already exists. Overwrite?" )
00541                                       .arg( connectionName ),
00542                                       QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
00543 
00544       switch ( res )
00545       {
00546         case QMessageBox::Cancel:
00547           return;
00548         case QMessageBox::No:
00549           child = child.nextSiblingElement();
00550           continue;
00551         case QMessageBox::Yes:
00552           overwrite = true;
00553           break;
00554         case QMessageBox::YesToAll:
00555           prompt = false;
00556           overwrite = true;
00557           break;
00558         case QMessageBox::NoToAll:
00559           prompt = false;
00560           overwrite = false;
00561           break;
00562       }
00563     }
00564 
00565     if ( keys.contains( connectionName ) && !overwrite )
00566     {
00567       child = child.nextSiblingElement();
00568       continue;
00569     }
00570 
00571     // no dups detected or overwrite is allowed
00572     settings.beginGroup( "/Qgis/connections-wfs" );
00573     settings.setValue( QString( "/" + connectionName + "/url" ) , child.attribute( "url" ) );
00574     settings.endGroup();
00575 
00576     if ( !child.attribute( "username" ).isEmpty() )
00577     {
00578       settings.beginGroup( "/Qgis/WFS/" + connectionName );
00579       settings.setValue( "/username", child.attribute( "username" ) );
00580       settings.setValue( "/password", child.attribute( "password" ) );
00581       settings.endGroup();
00582     }
00583     child = child.nextSiblingElement();
00584   }
00585 }
00586 
00587 
00588 void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, const QStringList &items )
00589 {
00590   QDomElement root = doc.documentElement();
00591   if ( root.tagName() != "qgsPgConnections" )
00592   {
00593     QMessageBox::information( this,
00594                               tr( "Loading connections" ),
00595                               tr( "The file is not an PostGIS connections exchange file." ) );
00596     return;
00597   }
00598 
00599   QString connectionName;
00600   QSettings settings;
00601   settings.beginGroup( "/PostgreSQL/connections" );
00602   QStringList keys = settings.childGroups();
00603   settings.endGroup();
00604   QDomElement child = root.firstChildElement();
00605   bool prompt = true;
00606   bool overwrite = true;
00607 
00608   while ( !child.isNull() )
00609   {
00610     connectionName = child.attribute( "name" );
00611     if ( !items.contains( connectionName ) )
00612     {
00613       child = child.nextSiblingElement();
00614       continue;
00615     }
00616 
00617     // check for duplicates
00618     if ( keys.contains( connectionName ) && prompt )
00619     {
00620       int res = QMessageBox::warning( this,
00621                                       tr( "Loading connections" ),
00622                                       tr( "Connection with name '%1' already exists. Overwrite?" )
00623                                       .arg( connectionName ),
00624                                       QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
00625       switch ( res )
00626       {
00627         case QMessageBox::Cancel:
00628           return;
00629         case QMessageBox::No:
00630           child = child.nextSiblingElement();
00631           continue;
00632         case QMessageBox::Yes:
00633           overwrite = true;
00634           break;
00635         case QMessageBox::YesToAll:
00636           prompt = false;
00637           overwrite = true;
00638           break;
00639         case QMessageBox::NoToAll:
00640           prompt = false;
00641           overwrite = false;
00642           break;
00643       }
00644     }
00645 
00646     if ( keys.contains( connectionName ) && !overwrite )
00647     {
00648       child = child.nextSiblingElement();
00649       continue;
00650     }
00651 
00652     //no dups detected or overwrite is allowed
00653     settings.beginGroup( "/PostgreSQL/connections/" + connectionName );
00654 
00655     settings.setValue( "/host", child.attribute( "host" ) );
00656     settings.setValue( "/port", child.attribute( "port" ) );
00657     settings.setValue( "/database", child.attribute( "database" ) );
00658     if ( child.hasAttribute( "service" ) )
00659     {
00660       settings.setValue( "/service", child.attribute( "service" ) );
00661     }
00662     else
00663     {
00664       settings.setValue( "/service", "" );
00665     }
00666     settings.setValue( "/sslmode", child.attribute( "sslmode" ) );
00667     settings.setValue( "/estimatedMetadata", child.attribute( "estimatedMetadata" ) );
00668     settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
00669     settings.setValue( "/username", child.attribute( "username" ) );
00670     settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
00671     settings.setValue( "/password", child.attribute( "password" ) );
00672     settings.endGroup();
00673 
00674     child = child.nextSiblingElement();
00675   }
00676 }
00677 
00678 void QgsManageConnectionsDialog::loadMssqlConnections( const QDomDocument &doc, const QStringList &items )
00679 {
00680   QDomElement root = doc.documentElement();
00681   if ( root.tagName() != "qgsMssqlConnections" )
00682   {
00683     QMessageBox::information( this,
00684                               tr( "Loading connections" ),
00685                               tr( "The file is not an PostGIS connections exchange file." ) );
00686     return;
00687   }
00688 
00689   QString connectionName;
00690   QSettings settings;
00691   settings.beginGroup( "/MSSQL/connections" );
00692   QStringList keys = settings.childGroups();
00693   settings.endGroup();
00694   QDomElement child = root.firstChildElement();
00695   bool prompt = true;
00696   bool overwrite = true;
00697 
00698   while ( !child.isNull() )
00699   {
00700     connectionName = child.attribute( "name" );
00701     if ( !items.contains( connectionName ) )
00702     {
00703       child = child.nextSiblingElement();
00704       continue;
00705     }
00706 
00707     // check for duplicates
00708     if ( keys.contains( connectionName ) && prompt )
00709     {
00710       int res = QMessageBox::warning( this,
00711                                       tr( "Loading connections" ),
00712                                       tr( "Connection with name '%1' already exists. Overwrite?" )
00713                                       .arg( connectionName ),
00714                                       QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
00715       switch ( res )
00716       {
00717         case QMessageBox::Cancel:
00718           return;
00719         case QMessageBox::No:
00720           child = child.nextSiblingElement();
00721           continue;
00722         case QMessageBox::Yes:
00723           overwrite = true;
00724           break;
00725         case QMessageBox::YesToAll:
00726           prompt = false;
00727           overwrite = true;
00728           break;
00729         case QMessageBox::NoToAll:
00730           prompt = false;
00731           overwrite = false;
00732           break;
00733       }
00734     }
00735 
00736     if ( keys.contains( connectionName ) && !overwrite )
00737     {
00738       child = child.nextSiblingElement();
00739       continue;
00740     }
00741 
00742     //no dups detected or overwrite is allowed
00743     settings.beginGroup( "/MSSQL/connections/" + connectionName );
00744 
00745     settings.setValue( "/host", child.attribute( "host" ) );
00746     settings.setValue( "/port", child.attribute( "port" ) );
00747     settings.setValue( "/database", child.attribute( "database" ) );
00748     if ( child.hasAttribute( "service" ) )
00749     {
00750       settings.setValue( "/service", child.attribute( "service" ) );
00751     }
00752     else
00753     {
00754       settings.setValue( "/service", "" );
00755     }
00756     settings.setValue( "/sslmode", child.attribute( "sslmode" ) );
00757     settings.setValue( "/estimatedMetadata", child.attribute( "estimatedMetadata" ) );
00758     settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
00759     settings.setValue( "/username", child.attribute( "username" ) );
00760     settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
00761     settings.setValue( "/password", child.attribute( "password" ) );
00762     settings.endGroup();
00763 
00764     child = child.nextSiblingElement();
00765   }
00766 }
00767 
00768 void QgsManageConnectionsDialog::selectAll()
00769 {
00770   listConnections->selectAll();
00771 }
00772 
00773 void QgsManageConnectionsDialog::clearSelection()
00774 {
00775   listConnections->clearSelection();
00776 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines