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