QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsmanageconnectionsdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmanageconnectionsdialog.cpp
3  ---------------------
4  begin : Dec 2009
5  copyright : (C) 2009 by Alexander Bruy
6  email : alexander dot bruy at gmail dot com
7 
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 
18 #include <QCloseEvent>
19 #include <QFileDialog>
20 #include <QMessageBox>
21 #include <QPushButton>
22 #include <QSettings>
23 #include <QTextStream>
24 
26 
27 QgsManageConnectionsDialog::QgsManageConnectionsDialog( QWidget *parent, Mode mode, Type type, QString fileName )
28  : QDialog( parent )
29  , mFileName( fileName )
30  , mDialogMode( mode )
31  , mConnectionType( type )
32 {
33  setupUi( this );
34 
35  // additional buttons
36  QPushButton *pb;
37  pb = new QPushButton( tr( "Select all" ) );
38  buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
39  connect( pb, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
40 
41  pb = new QPushButton( tr( "Clear selection" ) );
42  buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
43  connect( pb, SIGNAL( clicked() ), this, SLOT( clearSelection() ) );
44 
45  if ( mDialogMode == Import )
46  {
47  label->setText( tr( "Select connections to import" ) );
48  buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Import" ) );
49  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
50  }
51  else
52  {
53  //label->setText( tr( "Select connections to export" ) );
54  buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Export" ) );
55  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
56  }
57 
58  if ( !populateConnections() )
59  {
60  QApplication::postEvent( this, new QCloseEvent() );
61  }
62 
63  // use Ok button for starting import and export operations
64  disconnect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
65  connect( buttonBox, SIGNAL( accepted() ), this, SLOT( doExportImport() ) );
66 
67  connect( listConnections, SIGNAL( itemSelectionChanged() ), this, SLOT( selectionChanged() ) );
68 }
69 
71 {
72  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !listConnections->selectedItems().isEmpty() );
73 }
74 
76 {
77  QList<QListWidgetItem *> selection = listConnections->selectedItems();
78  if ( selection.isEmpty() )
79  {
80  QMessageBox::warning( this, tr( "Export/import error" ),
81  tr( "You should select at least one connection from list." ) );
82  return;
83  }
84 
85  QStringList items;
86  for ( int i = 0; i < selection.size(); ++i )
87  {
88  items.append( selection.at( i )->text() );
89  }
90 
91  if ( mDialogMode == Export )
92  {
93  QString fileName = QFileDialog::getSaveFileName( this, tr( "Save connections" ), ".",
94  tr( "XML files (*.xml *.XML)" ) );
95  if ( fileName.isEmpty() )
96  {
97  return;
98  }
99 
100  // ensure the user never ommited the extension from the file name
101  if ( !fileName.toLower().endsWith( ".xml" ) )
102  {
103  fileName += ".xml";
104  }
105 
106  mFileName = fileName;
107 
108  QDomDocument doc;
109  switch ( mConnectionType )
110  {
111  case WMS:
112  doc = saveOWSConnections( items, "WMS" );
113  break;
114  case WFS:
115  doc = saveWFSConnections( items );
116  break;
117  case PostGIS:
118  doc = savePgConnections( items );
119  break;
120  case MSSQL:
121  doc = saveMssqlConnections( items );
122  break;
123  case WCS:
124  doc = saveOWSConnections( items, "WCS" );
125  break;
126  case Oracle:
127  doc = saveOracleConnections( items );
128  break;
129  }
130 
131  QFile file( mFileName );
132  if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
133  {
134  QMessageBox::warning( this, tr( "Saving connections" ),
135  tr( "Cannot write file %1:\n%2." )
136  .arg( mFileName )
137  .arg( file.errorString() ) );
138  return;
139  }
140 
141  QTextStream out( &file );
142  doc.save( out, 4 );
143  }
144  else // import connections
145  {
146  QFile file( mFileName );
147  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
148  {
149  QMessageBox::warning( this, tr( "Loading connections" ),
150  tr( "Cannot read file %1:\n%2." )
151  .arg( mFileName )
152  .arg( file.errorString() ) );
153  return;
154  }
155 
156  QDomDocument doc;
157  QString errorStr;
158  int errorLine;
159  int errorColumn;
160 
161  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
162  {
163  QMessageBox::warning( this, tr( "Loading connections" ),
164  tr( "Parse error at line %1, column %2:\n%3" )
165  .arg( errorLine )
166  .arg( errorColumn )
167  .arg( errorStr ) );
168  return;
169  }
170 
171  switch ( mConnectionType )
172  {
173  case WMS:
174  loadOWSConnections( doc, items, "WMS" );
175  break;
176  case WFS:
177  loadWFSConnections( doc, items );
178  break;
179  case PostGIS:
180  loadPgConnections( doc, items );
181  break;
182  case MSSQL:
183  loadMssqlConnections( doc, items );
184  break;
185  case WCS:
186  loadOWSConnections( doc, items, "WCS" );
187  break;
188  case Oracle:
189  loadOracleConnections( doc, items );
190  break;
191  }
192  // clear connections list and close window
193  listConnections->clear();
194  accept();
195  }
196 
197  mFileName = "";
198 }
199 
201 {
202  // Export mode. Populate connections list from settings
203  if ( mDialogMode == Export )
204  {
205  QSettings settings;
206  switch ( mConnectionType )
207  {
208  case WMS:
209  settings.beginGroup( "/Qgis/connections-wms" );
210  break;
211  case WFS:
212  settings.beginGroup( "/Qgis/connections-wfs" );
213  break;
214  case WCS:
215  settings.beginGroup( "/Qgis/connections-wcs" );
216  break;
217  case PostGIS:
218  settings.beginGroup( "/PostgreSQL/connections" );
219  break;
220  case MSSQL:
221  settings.beginGroup( "/MSSQL/connections" );
222  break;
223  case Oracle:
224  settings.beginGroup( "/Oracle/connections" );
225  break;
226  }
227  QStringList keys = settings.childGroups();
228  QStringList::Iterator it = keys.begin();
229  while ( it != keys.end() )
230  {
231  QListWidgetItem *item = new QListWidgetItem();
232  item->setText( *it );
233  listConnections->addItem( item );
234  ++it;
235  }
236  settings.endGroup();
237  }
238  // Import mode. Populate connections list from file
239  else
240  {
241  QFile file( mFileName );
242  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
243  {
244  QMessageBox::warning( this, tr( "Loading connections" ),
245  tr( "Cannot read file %1:\n%2." )
246  .arg( mFileName )
247  .arg( file.errorString() ) );
248  return false;
249  }
250 
251  QDomDocument doc;
252  QString errorStr;
253  int errorLine;
254  int errorColumn;
255 
256  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
257  {
258  QMessageBox::warning( this, tr( "Loading connections" ),
259  tr( "Parse error at line %1, column %2:\n%3" )
260  .arg( errorLine )
261  .arg( errorColumn )
262  .arg( errorStr ) );
263  return false;
264  }
265 
266  QDomElement root = doc.documentElement();
267  switch ( mConnectionType )
268  {
269  case WMS:
270  if ( root.tagName() != "qgsWMSConnections" )
271  {
272  QMessageBox::information( this, tr( "Loading connections" ),
273  tr( "The file is not an WMS connections exchange file." ) );
274  return false;
275  }
276  break;
277 
278  case WFS:
279  if ( root.tagName() != "qgsWFSConnections" )
280  {
281  QMessageBox::information( this, tr( "Loading connections" ),
282  tr( "The file is not an WFS connections exchange file." ) );
283  return false;
284  }
285  break;
286 
287  case WCS:
288  if ( root.tagName() != "qgsWCSConnections" )
289  {
290  QMessageBox::information( this, tr( "Loading connections" ),
291  tr( "The file is not an WCS connections exchange file." ) );
292  return false;
293  }
294  break;
295 
296  case PostGIS:
297  if ( root.tagName() != "qgsPgConnections" )
298  {
299  QMessageBox::information( this, tr( "Loading connections" ),
300  tr( "The file is not an PostGIS connections exchange file." ) );
301  return false;
302  }
303  break;
304 
305  case MSSQL:
306  if ( root.tagName() != "qgsMssqlConnections" )
307  {
308  QMessageBox::information( this, tr( "Loading connections" ),
309  tr( "The file is not an MSSQL connections exchange file." ) );
310  return false;
311  }
312  break;
313  case Oracle:
314  if ( root.tagName() != "qgsOracleConnections" )
315  {
316  QMessageBox::information( this, tr( "Loading connections" ),
317  tr( "The file is not an Oracle connections exchange file." ) );
318  return false;
319  }
320  break;
321  }
322 
323  QDomElement child = root.firstChildElement();
324  while ( !child.isNull() )
325  {
326  QListWidgetItem *item = new QListWidgetItem();
327  item->setText( child.attribute( "name" ) );
328  listConnections->addItem( item );
329  child = child.nextSiblingElement();
330  }
331  }
332  return true;
333 }
334 
335 QDomDocument QgsManageConnectionsDialog::saveOWSConnections( const QStringList &connections, const QString & service )
336 {
337  QDomDocument doc( "connections" );
338  QDomElement root = doc.createElement( "qgs" + service.toUpper() + "Connections" );
339  root.setAttribute( "version", "1.0" );
340  doc.appendChild( root );
341 
342  QSettings settings;
343  QString path;
344  for ( int i = 0; i < connections.count(); ++i )
345  {
346  path = "/Qgis/connections-" + service.toLower() + "/";
347  QDomElement el = doc.createElement( service.toLower() );
348  el.setAttribute( "name", connections[ i ] );
349  el.setAttribute( "url", settings.value( path + connections[ i ] + "/url", "" ).toString() );
350 
351  if ( service == "WMS" )
352  {
353  el.setAttribute( "ignoreGetMapURI", settings.value( path + connections[i] + "/ignoreGetMapURI", false ).toBool() ? "true" : "false" );
354  el.setAttribute( "ignoreGetFeatureInfoURI", settings.value( path + connections[i] + "/ignoreGetFeatureInfoURI", false ).toBool() ? "true" : "false" );
355  el.setAttribute( "ignoreAxisOrientation", settings.value( path + connections[i] + "/ignoreAxisOrientation", false ).toBool() ? "true" : "false" );
356  el.setAttribute( "invertAxisOrientation", settings.value( path + connections[i] + "/invertAxisOrientation", false ).toBool() ? "true" : "false" );
357  el.setAttribute( "referer", settings.value( path + connections[ i ] + "/referer", "" ).toString() );
358  el.setAttribute( "smoothPixmapTransform", settings.value( path + connections[i] + "/smoothPixmapTransform", false ).toBool() ? "true" : "false" );
359  el.setAttribute( "dpiMode", settings.value( path + connections[i] + "/dpiMode", "7" ).toInt() );
360  }
361 
362  path = "/Qgis/" + service.toUpper() + "/";
363  el.setAttribute( "username", settings.value( path + connections[ i ] + "/username", "" ).toString() );
364  el.setAttribute( "password", settings.value( path + connections[ i ] + "/password", "" ).toString() );
365  root.appendChild( el );
366  }
367 
368  return doc;
369 }
370 
371 QDomDocument QgsManageConnectionsDialog::saveWFSConnections( const QStringList &connections )
372 {
373  QDomDocument doc( "connections" );
374  QDomElement root = doc.createElement( "qgsWFSConnections" );
375  root.setAttribute( "version", "1.0" );
376  doc.appendChild( root );
377 
378  QSettings settings;
379  QString path;
380  for ( int i = 0; i < connections.count(); ++i )
381  {
382  path = "/Qgis/connections-wfs/";
383  QDomElement el = doc.createElement( "wfs" );
384  el.setAttribute( "name", connections[ i ] );
385  el.setAttribute( "url", settings.value( path + connections[ i ] + "/url", "" ).toString() );
386 
387  el.setAttribute( "referer", settings.value( path + connections[ i ] + "/referer", "" ).toString() );
388 
389  path = "/Qgis/WFS/";
390  el.setAttribute( "username", settings.value( path + connections[ i ] + "/username", "" ).toString() );
391  el.setAttribute( "password", settings.value( path + connections[ i ] + "/password", "" ).toString() );
392  root.appendChild( el );
393  }
394 
395  return doc;
396 }
397 
398 QDomDocument QgsManageConnectionsDialog::savePgConnections( const QStringList &connections )
399 {
400  QDomDocument doc( "connections" );
401  QDomElement root = doc.createElement( "qgsPgConnections" );
402  root.setAttribute( "version", "1.0" );
403  doc.appendChild( root );
404 
405  QSettings settings;
406  QString path;
407  for ( int i = 0; i < connections.count(); ++i )
408  {
409  path = "/PostgreSQL/connections/" + connections[ i ];
410  QDomElement el = doc.createElement( "postgis" );
411  el.setAttribute( "name", connections[ i ] );
412  el.setAttribute( "host", settings.value( path + "/host", "" ).toString() );
413  el.setAttribute( "port", settings.value( path + "/port", "" ).toString() );
414  el.setAttribute( "database", settings.value( path + "/database", "" ).toString() );
415  el.setAttribute( "service", settings.value( path + "/service", "" ).toString() );
416  el.setAttribute( "sslmode", settings.value( path + "/sslmode", "1" ).toString() );
417  el.setAttribute( "estimatedMetadata", settings.value( path + "/estimatedMetadata", "0" ).toString() );
418 
419  el.setAttribute( "saveUsername", settings.value( path + "/saveUsername", "false" ).toString() );
420 
421  if ( settings.value( path + "/saveUsername", "false" ).toString() == "true" )
422  {
423  el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
424  }
425 
426  el.setAttribute( "savePassword", settings.value( path + "/savePassword", "false" ).toString() );
427 
428  if ( settings.value( path + "/savePassword", "false" ).toString() == "true" )
429  {
430  el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
431  }
432 
433  root.appendChild( el );
434  }
435 
436  return doc;
437 }
438 
439 QDomDocument QgsManageConnectionsDialog::saveMssqlConnections( const QStringList &connections )
440 {
441  QDomDocument doc( "connections" );
442  QDomElement root = doc.createElement( "qgsMssqlConnections" );
443  root.setAttribute( "version", "1.0" );
444  doc.appendChild( root );
445 
446  QSettings settings;
447  QString path;
448  for ( int i = 0; i < connections.count(); ++i )
449  {
450  path = "/MSSQL/connections/" + connections[ i ];
451  QDomElement el = doc.createElement( "mssql" );
452  el.setAttribute( "name", connections[ i ] );
453  el.setAttribute( "host", settings.value( path + "/host", "" ).toString() );
454  el.setAttribute( "port", settings.value( path + "/port", "" ).toString() );
455  el.setAttribute( "database", settings.value( path + "/database", "" ).toString() );
456  el.setAttribute( "service", settings.value( path + "/service", "" ).toString() );
457  el.setAttribute( "sslmode", settings.value( path + "/sslmode", "1" ).toString() );
458  el.setAttribute( "estimatedMetadata", settings.value( path + "/estimatedMetadata", "0" ).toString() );
459 
460  el.setAttribute( "saveUsername", settings.value( path + "/saveUsername", "false" ).toString() );
461 
462  if ( settings.value( path + "/saveUsername", "false" ).toString() == "true" )
463  {
464  el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
465  }
466 
467  el.setAttribute( "savePassword", settings.value( path + "/savePassword", "false" ).toString() );
468 
469  if ( settings.value( path + "/savePassword", "false" ).toString() == "true" )
470  {
471  el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
472  }
473 
474  root.appendChild( el );
475  }
476 
477  return doc;
478 }
479 
480 QDomDocument QgsManageConnectionsDialog::saveOracleConnections( const QStringList &connections )
481 {
482  QDomDocument doc( "connections" );
483  QDomElement root = doc.createElement( "qgsOracleConnections" );
484  root.setAttribute( "version", "1.0" );
485  doc.appendChild( root );
486 
487  QSettings settings;
488  QString path;
489  for ( int i = 0; i < connections.count(); ++i )
490  {
491  path = "/Oracle/connections/" + connections[ i ];
492  QDomElement el = doc.createElement( "oracle" );
493  el.setAttribute( "name", connections[ i ] );
494  el.setAttribute( "host", settings.value( path + "/host", "" ).toString() );
495  el.setAttribute( "port", settings.value( path + "/port", "" ).toString() );
496  el.setAttribute( "database", settings.value( path + "/database", "" ).toString() );
497  el.setAttribute( "estimatedMetadata", settings.value( path + "/estimatedMetadata", "0" ).toString() );
498  el.setAttribute( "userTablesOnly", settings.value( path + "/userTablesOnly", "0" ).toString() );
499  el.setAttribute( "geometryColumnsOnly", settings.value( path + "/geometryColumnsOnly", "0" ).toString() );
500  el.setAttribute( "allowGeometrylessTables", settings.value( path + "/allowGeometrylessTables", "0" ).toString() );
501 
502  el.setAttribute( "saveUsername", settings.value( path + "/saveUsername", "false" ).toString() );
503 
504  if ( settings.value( path + "/saveUsername", "false" ).toString() == "true" )
505  {
506  el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
507  }
508 
509  el.setAttribute( "savePassword", settings.value( path + "/savePassword", "false" ).toString() );
510 
511  if ( settings.value( path + "/savePassword", "false" ).toString() == "true" )
512  {
513  el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
514  }
515 
516  root.appendChild( el );
517  }
518 
519  return doc;
520 }
521 
522 void QgsManageConnectionsDialog::loadOWSConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
523 {
524  QDomElement root = doc.documentElement();
525  if ( root.tagName() != "qgs" + service.toUpper() + "Connections" )
526  {
527  QMessageBox::information( this, tr( "Loading connections" ),
528  tr( "The file is not an %1 connections exchange file." ).arg( service ) );
529  return;
530  }
531 
532  QString connectionName;
533  QSettings settings;
534  settings.beginGroup( "/Qgis/connections-" + service.toLower() );
535  QStringList keys = settings.childGroups();
536  settings.endGroup();
537  QDomElement child = root.firstChildElement();
538  bool prompt = true;
539  bool overwrite = true;
540 
541  while ( !child.isNull() )
542  {
543  connectionName = child.attribute( "name" );
544  if ( !items.contains( connectionName ) )
545  {
546  child = child.nextSiblingElement();
547  continue;
548  }
549 
550  // check for duplicates
551  if ( keys.contains( connectionName ) && prompt )
552  {
553  int res = QMessageBox::warning( this,
554  tr( "Loading connections" ),
555  tr( "Connection with name '%1' already exists. Overwrite?" )
556  .arg( connectionName ),
557  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
558 
559  switch ( res )
560  {
561  case QMessageBox::Cancel:
562  return;
563  case QMessageBox::No:
564  child = child.nextSiblingElement();
565  continue;
566  case QMessageBox::Yes:
567  overwrite = true;
568  break;
569  case QMessageBox::YesToAll:
570  prompt = false;
571  overwrite = true;
572  break;
573  case QMessageBox::NoToAll:
574  prompt = false;
575  overwrite = false;
576  break;
577  }
578  }
579 
580  if ( keys.contains( connectionName ) && !overwrite )
581  {
582  child = child.nextSiblingElement();
583  continue;
584  }
585 
586  // no dups detected or overwrite is allowed
587  settings.beginGroup( "/Qgis/connections-" + service.toLower() );
588  settings.setValue( QString( "/" + connectionName + "/url" ) , child.attribute( "url" ) );
589  settings.setValue( QString( "/" + connectionName + "/ignoreGetMapURI" ), child.attribute( "ignoreGetMapURI" ) == "true" );
590  settings.setValue( QString( "/" + connectionName + "/ignoreGetFeatureInfoURI" ), child.attribute( "ignoreGetFeatureInfoURI" ) == "true" );
591  settings.setValue( QString( "/" + connectionName + "/ignoreAxisOrientation" ), child.attribute( "ignoreAxisOrientation" ) == "true" );
592  settings.setValue( QString( "/" + connectionName + "/invertAxisOrientation" ), child.attribute( "invertAxisOrientation" ) == "true" );
593  settings.setValue( QString( "/" + connectionName + "/referer" ), child.attribute( "referer" ) );
594  settings.setValue( QString( "/" + connectionName + "/smoothPixmapTransform" ), child.attribute( "smoothPixmapTransform" ) == "true" );
595  settings.setValue( QString( "/" + connectionName + "/dpiMode" ), child.attribute( "dpiMode", "7" ).toInt() );
596  settings.endGroup();
597 
598  if ( !child.attribute( "username" ).isEmpty() )
599  {
600  settings.beginGroup( "/Qgis/" + service.toUpper() + "/" + connectionName );
601  settings.setValue( "/username", child.attribute( "username" ) );
602  settings.setValue( "/password", child.attribute( "password" ) );
603  settings.endGroup();
604  }
605  child = child.nextSiblingElement();
606  }
607 }
608 
609 void QgsManageConnectionsDialog::loadWFSConnections( const QDomDocument &doc, const QStringList &items )
610 {
611  QDomElement root = doc.documentElement();
612  if ( root.tagName() != "qgsWFSConnections" )
613  {
614  QMessageBox::information( this, tr( "Loading connections" ),
615  tr( "The file is not an WFS connections exchange file." ) );
616  return;
617  }
618 
619  QString connectionName;
620  QSettings settings;
621  settings.beginGroup( "/Qgis/connections-wfs" );
622  QStringList keys = settings.childGroups();
623  settings.endGroup();
624  QDomElement child = root.firstChildElement();
625  bool prompt = true;
626  bool overwrite = true;
627 
628  while ( !child.isNull() )
629  {
630  connectionName = child.attribute( "name" );
631  if ( !items.contains( connectionName ) )
632  {
633  child = child.nextSiblingElement();
634  continue;
635  }
636 
637  // check for duplicates
638  if ( keys.contains( connectionName ) && prompt )
639  {
640  int res = QMessageBox::warning( this,
641  tr( "Loading connections" ),
642  tr( "Connection with name '%1' already exists. Overwrite?" )
643  .arg( connectionName ),
644  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
645 
646  switch ( res )
647  {
648  case QMessageBox::Cancel:
649  return;
650  case QMessageBox::No:
651  child = child.nextSiblingElement();
652  continue;
653  case QMessageBox::Yes:
654  overwrite = true;
655  break;
656  case QMessageBox::YesToAll:
657  prompt = false;
658  overwrite = true;
659  break;
660  case QMessageBox::NoToAll:
661  prompt = false;
662  overwrite = false;
663  break;
664  }
665  }
666 
667  if ( keys.contains( connectionName ) && !overwrite )
668  {
669  child = child.nextSiblingElement();
670  continue;
671  }
672 
673  // no dups detected or overwrite is allowed
674  settings.beginGroup( "/Qgis/connections-wfs" );
675  settings.setValue( QString( "/" + connectionName + "/url" ) , child.attribute( "url" ) );
676  settings.endGroup();
677 
678  if ( !child.attribute( "username" ).isEmpty() )
679  {
680  settings.beginGroup( "/Qgis/WFS/" + connectionName );
681  settings.setValue( "/username", child.attribute( "username" ) );
682  settings.setValue( "/password", child.attribute( "password" ) );
683  settings.endGroup();
684  }
685  child = child.nextSiblingElement();
686  }
687 }
688 
689 
690 void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, const QStringList &items )
691 {
692  QDomElement root = doc.documentElement();
693  if ( root.tagName() != "qgsPgConnections" )
694  {
695  QMessageBox::information( this,
696  tr( "Loading connections" ),
697  tr( "The file is not an PostGIS connections exchange file." ) );
698  return;
699  }
700 
701  QString connectionName;
702  QSettings settings;
703  settings.beginGroup( "/PostgreSQL/connections" );
704  QStringList keys = settings.childGroups();
705  settings.endGroup();
706  QDomElement child = root.firstChildElement();
707  bool prompt = true;
708  bool overwrite = true;
709 
710  while ( !child.isNull() )
711  {
712  connectionName = child.attribute( "name" );
713  if ( !items.contains( connectionName ) )
714  {
715  child = child.nextSiblingElement();
716  continue;
717  }
718 
719  // check for duplicates
720  if ( keys.contains( connectionName ) && prompt )
721  {
722  int res = QMessageBox::warning( this,
723  tr( "Loading connections" ),
724  tr( "Connection with name '%1' already exists. Overwrite?" )
725  .arg( connectionName ),
726  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
727  switch ( res )
728  {
729  case QMessageBox::Cancel:
730  return;
731  case QMessageBox::No:
732  child = child.nextSiblingElement();
733  continue;
734  case QMessageBox::Yes:
735  overwrite = true;
736  break;
737  case QMessageBox::YesToAll:
738  prompt = false;
739  overwrite = true;
740  break;
741  case QMessageBox::NoToAll:
742  prompt = false;
743  overwrite = false;
744  break;
745  }
746  }
747 
748  if ( keys.contains( connectionName ) && !overwrite )
749  {
750  child = child.nextSiblingElement();
751  continue;
752  }
753 
754  //no dups detected or overwrite is allowed
755  settings.beginGroup( "/PostgreSQL/connections/" + connectionName );
756 
757  settings.setValue( "/host", child.attribute( "host" ) );
758  settings.setValue( "/port", child.attribute( "port" ) );
759  settings.setValue( "/database", child.attribute( "database" ) );
760  if ( child.hasAttribute( "service" ) )
761  {
762  settings.setValue( "/service", child.attribute( "service" ) );
763  }
764  else
765  {
766  settings.setValue( "/service", "" );
767  }
768  settings.setValue( "/sslmode", child.attribute( "sslmode" ) );
769  settings.setValue( "/estimatedMetadata", child.attribute( "estimatedMetadata" ) );
770  settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
771  settings.setValue( "/username", child.attribute( "username" ) );
772  settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
773  settings.setValue( "/password", child.attribute( "password" ) );
774  settings.endGroup();
775 
776  child = child.nextSiblingElement();
777  }
778 }
779 
780 void QgsManageConnectionsDialog::loadMssqlConnections( const QDomDocument &doc, const QStringList &items )
781 {
782  QDomElement root = doc.documentElement();
783  if ( root.tagName() != "qgsMssqlConnections" )
784  {
785  QMessageBox::information( this,
786  tr( "Loading connections" ),
787  tr( "The file is not an MSSQL connections exchange file." ) );
788  return;
789  }
790 
791  QString connectionName;
792  QSettings settings;
793  settings.beginGroup( "/MSSQL/connections" );
794  QStringList keys = settings.childGroups();
795  settings.endGroup();
796  QDomElement child = root.firstChildElement();
797  bool prompt = true;
798  bool overwrite = true;
799 
800  while ( !child.isNull() )
801  {
802  connectionName = child.attribute( "name" );
803  if ( !items.contains( connectionName ) )
804  {
805  child = child.nextSiblingElement();
806  continue;
807  }
808 
809  // check for duplicates
810  if ( keys.contains( connectionName ) && prompt )
811  {
812  int res = QMessageBox::warning( this,
813  tr( "Loading connections" ),
814  tr( "Connection with name '%1' already exists. Overwrite?" )
815  .arg( connectionName ),
816  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
817  switch ( res )
818  {
819  case QMessageBox::Cancel:
820  return;
821  case QMessageBox::No:
822  child = child.nextSiblingElement();
823  continue;
824  case QMessageBox::Yes:
825  overwrite = true;
826  break;
827  case QMessageBox::YesToAll:
828  prompt = false;
829  overwrite = true;
830  break;
831  case QMessageBox::NoToAll:
832  prompt = false;
833  overwrite = false;
834  break;
835  }
836  }
837 
838  if ( keys.contains( connectionName ) && !overwrite )
839  {
840  child = child.nextSiblingElement();
841  continue;
842  }
843 
844  //no dups detected or overwrite is allowed
845  settings.beginGroup( "/MSSQL/connections/" + connectionName );
846 
847  settings.setValue( "/host", child.attribute( "host" ) );
848  settings.setValue( "/port", child.attribute( "port" ) );
849  settings.setValue( "/database", child.attribute( "database" ) );
850  if ( child.hasAttribute( "service" ) )
851  {
852  settings.setValue( "/service", child.attribute( "service" ) );
853  }
854  else
855  {
856  settings.setValue( "/service", "" );
857  }
858  settings.setValue( "/sslmode", child.attribute( "sslmode" ) );
859  settings.setValue( "/estimatedMetadata", child.attribute( "estimatedMetadata" ) );
860  settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
861  settings.setValue( "/username", child.attribute( "username" ) );
862  settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
863  settings.setValue( "/password", child.attribute( "password" ) );
864  settings.endGroup();
865 
866  child = child.nextSiblingElement();
867  }
868 }
869 
870 void QgsManageConnectionsDialog::loadOracleConnections( const QDomDocument &doc, const QStringList &items )
871 {
872  QDomElement root = doc.documentElement();
873  if ( root.tagName() != "qgsOracleConnections" )
874  {
875  QMessageBox::information( this,
876  tr( "Loading connections" ),
877  tr( "The file is not an Oracle connections exchange file." ) );
878  return;
879  }
880 
881  QString connectionName;
882  QSettings settings;
883  settings.beginGroup( "/Oracle/connections" );
884  QStringList keys = settings.childGroups();
885  settings.endGroup();
886  QDomElement child = root.firstChildElement();
887  bool prompt = true;
888  bool overwrite = true;
889 
890  while ( !child.isNull() )
891  {
892  connectionName = child.attribute( "name" );
893  if ( !items.contains( connectionName ) )
894  {
895  child = child.nextSiblingElement();
896  continue;
897  }
898 
899  // check for duplicates
900  if ( keys.contains( connectionName ) && prompt )
901  {
902  int res = QMessageBox::warning( this,
903  tr( "Loading connections" ),
904  tr( "Connection with name '%1' already exists. Overwrite?" )
905  .arg( connectionName ),
906  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
907  switch ( res )
908  {
909  case QMessageBox::Cancel:
910  return;
911  case QMessageBox::No:
912  child = child.nextSiblingElement();
913  continue;
914  case QMessageBox::Yes:
915  overwrite = true;
916  break;
917  case QMessageBox::YesToAll:
918  prompt = false;
919  overwrite = true;
920  break;
921  case QMessageBox::NoToAll:
922  prompt = false;
923  overwrite = false;
924  break;
925  }
926  }
927 
928  if ( keys.contains( connectionName ) && !overwrite )
929  {
930  child = child.nextSiblingElement();
931  continue;
932  }
933 
934  //no dups detected or overwrite is allowed
935  settings.beginGroup( "/Oracle/connections/" + connectionName );
936 
937  settings.setValue( "/host", child.attribute( "host" ) );
938  settings.setValue( "/port", child.attribute( "port" ) );
939  settings.setValue( "/database", child.attribute( "database" ) );
940  settings.setValue( "/estimatedMetadata", child.attribute( "estimatedMetadata" ) );
941  settings.setValue( "/userTablesOnly", child.attribute( "userTablesOnly" ) );
942  settings.setValue( "/geometryColumnsOnly", child.attribute( "geometryColumnsOnly" ) );
943  settings.setValue( "/allowGeometrylessTables", child.attribute( "allowGeometrylessTables" ) );
944  settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
945  settings.setValue( "/username", child.attribute( "username" ) );
946  settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
947  settings.setValue( "/password", child.attribute( "password" ) );
948  settings.endGroup();
949 
950  child = child.nextSiblingElement();
951  }
952 }
953 
955 {
956  listConnections->selectAll();
957  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !listConnections->selectedItems().isEmpty() );
958 }
959 
961 {
962  listConnections->clearSelection();
963  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
964 }