QGIS API Documentation  2.8.2-Wien
 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 
200 bool QgsManageConnectionsDialog::populateConnections()
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( "dboptions", settings.value( path + "/dboptions", "" ).toString() );
498  el.setAttribute( "estimatedMetadata", settings.value( path + "/estimatedMetadata", "0" ).toString() );
499  el.setAttribute( "userTablesOnly", settings.value( path + "/userTablesOnly", "0" ).toString() );
500  el.setAttribute( "geometryColumnsOnly", settings.value( path + "/geometryColumnsOnly", "0" ).toString() );
501  el.setAttribute( "allowGeometrylessTables", settings.value( path + "/allowGeometrylessTables", "0" ).toString() );
502 
503  el.setAttribute( "saveUsername", settings.value( path + "/saveUsername", "false" ).toString() );
504 
505  if ( settings.value( path + "/saveUsername", "false" ).toString() == "true" )
506  {
507  el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
508  }
509 
510  el.setAttribute( "savePassword", settings.value( path + "/savePassword", "false" ).toString() );
511 
512  if ( settings.value( path + "/savePassword", "false" ).toString() == "true" )
513  {
514  el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
515  }
516 
517  root.appendChild( el );
518  }
519 
520  return doc;
521 }
522 
523 void QgsManageConnectionsDialog::loadOWSConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
524 {
525  QDomElement root = doc.documentElement();
526  if ( root.tagName() != "qgs" + service.toUpper() + "Connections" )
527  {
528  QMessageBox::information( this, tr( "Loading connections" ),
529  tr( "The file is not an %1 connections exchange file." ).arg( service ) );
530  return;
531  }
532 
533  QString connectionName;
534  QSettings settings;
535  settings.beginGroup( "/Qgis/connections-" + service.toLower() );
536  QStringList keys = settings.childGroups();
537  settings.endGroup();
538  QDomElement child = root.firstChildElement();
539  bool prompt = true;
540  bool overwrite = true;
541 
542  while ( !child.isNull() )
543  {
544  connectionName = child.attribute( "name" );
545  if ( !items.contains( connectionName ) )
546  {
547  child = child.nextSiblingElement();
548  continue;
549  }
550 
551  // check for duplicates
552  if ( keys.contains( connectionName ) && prompt )
553  {
554  int res = QMessageBox::warning( this,
555  tr( "Loading connections" ),
556  tr( "Connection with name '%1' already exists. Overwrite?" )
557  .arg( connectionName ),
558  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
559 
560  switch ( res )
561  {
562  case QMessageBox::Cancel:
563  return;
564  case QMessageBox::No:
565  child = child.nextSiblingElement();
566  continue;
567  case QMessageBox::Yes:
568  overwrite = true;
569  break;
570  case QMessageBox::YesToAll:
571  prompt = false;
572  overwrite = true;
573  break;
574  case QMessageBox::NoToAll:
575  prompt = false;
576  overwrite = false;
577  break;
578  }
579  }
580 
581  if ( keys.contains( connectionName ) && !overwrite )
582  {
583  child = child.nextSiblingElement();
584  continue;
585  }
586 
587  // no dups detected or overwrite is allowed
588  settings.beginGroup( "/Qgis/connections-" + service.toLower() );
589  settings.setValue( QString( "/" + connectionName + "/url" ), child.attribute( "url" ) );
590  settings.setValue( QString( "/" + connectionName + "/ignoreGetMapURI" ), child.attribute( "ignoreGetMapURI" ) == "true" );
591  settings.setValue( QString( "/" + connectionName + "/ignoreGetFeatureInfoURI" ), child.attribute( "ignoreGetFeatureInfoURI" ) == "true" );
592  settings.setValue( QString( "/" + connectionName + "/ignoreAxisOrientation" ), child.attribute( "ignoreAxisOrientation" ) == "true" );
593  settings.setValue( QString( "/" + connectionName + "/invertAxisOrientation" ), child.attribute( "invertAxisOrientation" ) == "true" );
594  settings.setValue( QString( "/" + connectionName + "/referer" ), child.attribute( "referer" ) );
595  settings.setValue( QString( "/" + connectionName + "/smoothPixmapTransform" ), child.attribute( "smoothPixmapTransform" ) == "true" );
596  settings.setValue( QString( "/" + connectionName + "/dpiMode" ), child.attribute( "dpiMode", "7" ).toInt() );
597  settings.endGroup();
598 
599  if ( !child.attribute( "username" ).isEmpty() )
600  {
601  settings.beginGroup( "/Qgis/" + service.toUpper() + "/" + connectionName );
602  settings.setValue( "/username", child.attribute( "username" ) );
603  settings.setValue( "/password", child.attribute( "password" ) );
604  settings.endGroup();
605  }
606  child = child.nextSiblingElement();
607  }
608 }
609 
610 void QgsManageConnectionsDialog::loadWFSConnections( const QDomDocument &doc, const QStringList &items )
611 {
612  QDomElement root = doc.documentElement();
613  if ( root.tagName() != "qgsWFSConnections" )
614  {
615  QMessageBox::information( this, tr( "Loading connections" ),
616  tr( "The file is not an WFS connections exchange file." ) );
617  return;
618  }
619 
620  QString connectionName;
621  QSettings settings;
622  settings.beginGroup( "/Qgis/connections-wfs" );
623  QStringList keys = settings.childGroups();
624  settings.endGroup();
625  QDomElement child = root.firstChildElement();
626  bool prompt = true;
627  bool overwrite = true;
628 
629  while ( !child.isNull() )
630  {
631  connectionName = child.attribute( "name" );
632  if ( !items.contains( connectionName ) )
633  {
634  child = child.nextSiblingElement();
635  continue;
636  }
637 
638  // check for duplicates
639  if ( keys.contains( connectionName ) && prompt )
640  {
641  int res = QMessageBox::warning( this,
642  tr( "Loading connections" ),
643  tr( "Connection with name '%1' already exists. Overwrite?" )
644  .arg( connectionName ),
645  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
646 
647  switch ( res )
648  {
649  case QMessageBox::Cancel:
650  return;
651  case QMessageBox::No:
652  child = child.nextSiblingElement();
653  continue;
654  case QMessageBox::Yes:
655  overwrite = true;
656  break;
657  case QMessageBox::YesToAll:
658  prompt = false;
659  overwrite = true;
660  break;
661  case QMessageBox::NoToAll:
662  prompt = false;
663  overwrite = false;
664  break;
665  }
666  }
667 
668  if ( keys.contains( connectionName ) && !overwrite )
669  {
670  child = child.nextSiblingElement();
671  continue;
672  }
673 
674  // no dups detected or overwrite is allowed
675  settings.beginGroup( "/Qgis/connections-wfs" );
676  settings.setValue( QString( "/" + connectionName + "/url" ), child.attribute( "url" ) );
677  settings.endGroup();
678 
679  if ( !child.attribute( "username" ).isEmpty() )
680  {
681  settings.beginGroup( "/Qgis/WFS/" + connectionName );
682  settings.setValue( "/username", child.attribute( "username" ) );
683  settings.setValue( "/password", child.attribute( "password" ) );
684  settings.endGroup();
685  }
686  child = child.nextSiblingElement();
687  }
688 }
689 
690 
691 void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, const QStringList &items )
692 {
693  QDomElement root = doc.documentElement();
694  if ( root.tagName() != "qgsPgConnections" )
695  {
696  QMessageBox::information( this,
697  tr( "Loading connections" ),
698  tr( "The file is not an PostGIS connections exchange file." ) );
699  return;
700  }
701 
702  QString connectionName;
703  QSettings settings;
704  settings.beginGroup( "/PostgreSQL/connections" );
705  QStringList keys = settings.childGroups();
706  settings.endGroup();
707  QDomElement child = root.firstChildElement();
708  bool prompt = true;
709  bool overwrite = true;
710 
711  while ( !child.isNull() )
712  {
713  connectionName = child.attribute( "name" );
714  if ( !items.contains( connectionName ) )
715  {
716  child = child.nextSiblingElement();
717  continue;
718  }
719 
720  // check for duplicates
721  if ( keys.contains( connectionName ) && prompt )
722  {
723  int res = QMessageBox::warning( this,
724  tr( "Loading connections" ),
725  tr( "Connection with name '%1' already exists. Overwrite?" )
726  .arg( connectionName ),
727  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
728  switch ( res )
729  {
730  case QMessageBox::Cancel:
731  return;
732  case QMessageBox::No:
733  child = child.nextSiblingElement();
734  continue;
735  case QMessageBox::Yes:
736  overwrite = true;
737  break;
738  case QMessageBox::YesToAll:
739  prompt = false;
740  overwrite = true;
741  break;
742  case QMessageBox::NoToAll:
743  prompt = false;
744  overwrite = false;
745  break;
746  }
747  }
748 
749  if ( keys.contains( connectionName ) && !overwrite )
750  {
751  child = child.nextSiblingElement();
752  continue;
753  }
754 
755  //no dups detected or overwrite is allowed
756  settings.beginGroup( "/PostgreSQL/connections/" + connectionName );
757 
758  settings.setValue( "/host", child.attribute( "host" ) );
759  settings.setValue( "/port", child.attribute( "port" ) );
760  settings.setValue( "/database", child.attribute( "database" ) );
761  if ( child.hasAttribute( "service" ) )
762  {
763  settings.setValue( "/service", child.attribute( "service" ) );
764  }
765  else
766  {
767  settings.setValue( "/service", "" );
768  }
769  settings.setValue( "/sslmode", child.attribute( "sslmode" ) );
770  settings.setValue( "/estimatedMetadata", child.attribute( "estimatedMetadata" ) );
771  settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
772  settings.setValue( "/username", child.attribute( "username" ) );
773  settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
774  settings.setValue( "/password", child.attribute( "password" ) );
775  settings.endGroup();
776 
777  child = child.nextSiblingElement();
778  }
779 }
780 
781 void QgsManageConnectionsDialog::loadMssqlConnections( const QDomDocument &doc, const QStringList &items )
782 {
783  QDomElement root = doc.documentElement();
784  if ( root.tagName() != "qgsMssqlConnections" )
785  {
786  QMessageBox::information( this,
787  tr( "Loading connections" ),
788  tr( "The file is not an MSSQL connections exchange file." ) );
789  return;
790  }
791 
792  QString connectionName;
793  QSettings settings;
794  settings.beginGroup( "/MSSQL/connections" );
795  QStringList keys = settings.childGroups();
796  settings.endGroup();
797  QDomElement child = root.firstChildElement();
798  bool prompt = true;
799  bool overwrite = true;
800 
801  while ( !child.isNull() )
802  {
803  connectionName = child.attribute( "name" );
804  if ( !items.contains( connectionName ) )
805  {
806  child = child.nextSiblingElement();
807  continue;
808  }
809 
810  // check for duplicates
811  if ( keys.contains( connectionName ) && prompt )
812  {
813  int res = QMessageBox::warning( this,
814  tr( "Loading connections" ),
815  tr( "Connection with name '%1' already exists. Overwrite?" )
816  .arg( connectionName ),
817  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
818  switch ( res )
819  {
820  case QMessageBox::Cancel:
821  return;
822  case QMessageBox::No:
823  child = child.nextSiblingElement();
824  continue;
825  case QMessageBox::Yes:
826  overwrite = true;
827  break;
828  case QMessageBox::YesToAll:
829  prompt = false;
830  overwrite = true;
831  break;
832  case QMessageBox::NoToAll:
833  prompt = false;
834  overwrite = false;
835  break;
836  }
837  }
838 
839  if ( keys.contains( connectionName ) && !overwrite )
840  {
841  child = child.nextSiblingElement();
842  continue;
843  }
844 
845  //no dups detected or overwrite is allowed
846  settings.beginGroup( "/MSSQL/connections/" + connectionName );
847 
848  settings.setValue( "/host", child.attribute( "host" ) );
849  settings.setValue( "/port", child.attribute( "port" ) );
850  settings.setValue( "/database", child.attribute( "database" ) );
851  if ( child.hasAttribute( "service" ) )
852  {
853  settings.setValue( "/service", child.attribute( "service" ) );
854  }
855  else
856  {
857  settings.setValue( "/service", "" );
858  }
859  settings.setValue( "/sslmode", child.attribute( "sslmode" ) );
860  settings.setValue( "/estimatedMetadata", child.attribute( "estimatedMetadata" ) );
861  settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
862  settings.setValue( "/username", child.attribute( "username" ) );
863  settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
864  settings.setValue( "/password", child.attribute( "password" ) );
865  settings.endGroup();
866 
867  child = child.nextSiblingElement();
868  }
869 }
870 
871 void QgsManageConnectionsDialog::loadOracleConnections( const QDomDocument &doc, const QStringList &items )
872 {
873  QDomElement root = doc.documentElement();
874  if ( root.tagName() != "qgsOracleConnections" )
875  {
876  QMessageBox::information( this,
877  tr( "Loading connections" ),
878  tr( "The file is not an Oracle connections exchange file." ) );
879  return;
880  }
881 
882  QString connectionName;
883  QSettings settings;
884  settings.beginGroup( "/Oracle/connections" );
885  QStringList keys = settings.childGroups();
886  settings.endGroup();
887  QDomElement child = root.firstChildElement();
888  bool prompt = true;
889  bool overwrite = true;
890 
891  while ( !child.isNull() )
892  {
893  connectionName = child.attribute( "name" );
894  if ( !items.contains( connectionName ) )
895  {
896  child = child.nextSiblingElement();
897  continue;
898  }
899 
900  // check for duplicates
901  if ( keys.contains( connectionName ) && prompt )
902  {
903  int res = QMessageBox::warning( this,
904  tr( "Loading connections" ),
905  tr( "Connection with name '%1' already exists. Overwrite?" )
906  .arg( connectionName ),
907  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
908  switch ( res )
909  {
910  case QMessageBox::Cancel:
911  return;
912  case QMessageBox::No:
913  child = child.nextSiblingElement();
914  continue;
915  case QMessageBox::Yes:
916  overwrite = true;
917  break;
918  case QMessageBox::YesToAll:
919  prompt = false;
920  overwrite = true;
921  break;
922  case QMessageBox::NoToAll:
923  prompt = false;
924  overwrite = false;
925  break;
926  }
927  }
928 
929  if ( keys.contains( connectionName ) && !overwrite )
930  {
931  child = child.nextSiblingElement();
932  continue;
933  }
934 
935  //no dups detected or overwrite is allowed
936  settings.beginGroup( "/Oracle/connections/" + connectionName );
937 
938  settings.setValue( "/host", child.attribute( "host" ) );
939  settings.setValue( "/port", child.attribute( "port" ) );
940  settings.setValue( "/database", child.attribute( "database" ) );
941  settings.setValue( "/dboptions", child.attribute( "dboptions" ) );
942  settings.setValue( "/estimatedMetadata", child.attribute( "estimatedMetadata" ) );
943  settings.setValue( "/userTablesOnly", child.attribute( "userTablesOnly" ) );
944  settings.setValue( "/geometryColumnsOnly", child.attribute( "geometryColumnsOnly" ) );
945  settings.setValue( "/allowGeometrylessTables", child.attribute( "allowGeometrylessTables" ) );
946  settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
947  settings.setValue( "/username", child.attribute( "username" ) );
948  settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
949  settings.setValue( "/password", child.attribute( "password" ) );
950  settings.endGroup();
951 
952  child = child.nextSiblingElement();
953  }
954 }
955 
957 {
958  listConnections->selectAll();
959  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !listConnections->selectedItems().isEmpty() );
960 }
961 
963 {
964  listConnections->clearSelection();
965  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
966 }