QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 #include <QCloseEvent>
18 #include <QFileDialog>
19 #include <QMessageBox>
20 #include <QPushButton>
21 #include <QTextStream>
22 
23 #include "qgssettings.h"
25 
26 
27 QgsManageConnectionsDialog::QgsManageConnectionsDialog( QWidget *parent, Mode mode, Type type, const 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 = nullptr;
37  pb = new QPushButton( tr( "Select all" ) );
38  buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
39  connect( pb, &QAbstractButton::clicked, this, &QgsManageConnectionsDialog::selectAll );
40 
41  pb = new QPushButton( tr( "Clear selection" ) );
42  buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
43  connect( pb, &QAbstractButton::clicked, this, &QgsManageConnectionsDialog::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, &QDialogButtonBox::accepted, this, &QDialog::accept );
65  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsManageConnectionsDialog::doExportImport );
66 
67  connect( listConnections, &QListWidget::itemSelectionChanged, this, &QgsManageConnectionsDialog::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 omitted the extension from the file name
102  if ( !fileName.endsWith( QLatin1String( ".xml" ), Qt::CaseInsensitive ) )
103  {
104  fileName += QLatin1String( ".xml" );
105  }
106 
107  mFileName = fileName;
108 
109  QDomDocument doc;
110  switch ( mConnectionType )
111  {
112  case WMS:
113  doc = saveOWSConnections( items, QStringLiteral( "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, QStringLiteral( "WCS" ) );
126  break;
127  case Oracle:
128  doc = saveOracleConnections( items );
129  break;
130  case DB2:
131  doc = saveDb2Connections( items );
132  break;
133  case GeoNode:
134  doc = saveGeonodeConnections( items );
135  break;
136  case XyzTiles:
137  doc = saveXyzTilesConnections( items );
138  break;
139  case ArcgisMapServer:
140  doc = saveArcgisConnections( items, QStringLiteral( "ARCGISMAPSERVER" ) );
141  break;
142  case ArcgisFeatureServer:
143  doc = saveArcgisConnections( items, QStringLiteral( "ARCGISFEATURESERVER" ) );
144  break;
145  case VectorTile:
146  doc = saveVectorTileConnections( items );
147  break;
148  }
149 
150  QFile file( mFileName );
151  if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
152  {
153  QMessageBox::warning( this, tr( "Saving Connections" ),
154  tr( "Cannot write file %1:\n%2." )
155  .arg( mFileName,
156  file.errorString() ) );
157  return;
158  }
159 
160  QTextStream out( &file );
161  doc.save( out, 4 );
162  }
163  else // import connections
164  {
165  QFile file( mFileName );
166  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
167  {
168  QMessageBox::warning( this, tr( "Loading Connections" ),
169  tr( "Cannot read file %1:\n%2." )
170  .arg( mFileName,
171  file.errorString() ) );
172  return;
173  }
174 
175  QDomDocument doc;
176  QString errorStr;
177  int errorLine;
178  int errorColumn;
179 
180  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
181  {
182  QMessageBox::warning( this, tr( "Loading Connections" ),
183  tr( "Parse error at line %1, column %2:\n%3" )
184  .arg( errorLine )
185  .arg( errorColumn )
186  .arg( errorStr ) );
187  return;
188  }
189 
190  switch ( mConnectionType )
191  {
192  case WMS:
193  loadOWSConnections( doc, items, QStringLiteral( "WMS" ) );
194  break;
195  case WFS:
196  loadWfsConnections( doc, items );
197  break;
198  case PostGIS:
199  loadPgConnections( doc, items );
200  break;
201  case MSSQL:
202  loadMssqlConnections( doc, items );
203  break;
204  case WCS:
205  loadOWSConnections( doc, items, QStringLiteral( "WCS" ) );
206  break;
207  case Oracle:
208  loadOracleConnections( doc, items );
209  break;
210  case DB2:
211  loadDb2Connections( doc, items );
212  break;
213  case GeoNode:
214  loadGeonodeConnections( doc, items );
215  break;
216  case XyzTiles:
217  loadXyzTilesConnections( doc, items );
218  break;
219  case ArcgisMapServer:
220  loadArcgisConnections( doc, items, QStringLiteral( "ARCGISMAPSERVER" ) );
221  break;
222  case ArcgisFeatureServer:
223  loadArcgisConnections( doc, items, QStringLiteral( "ARCGISFEATURESERVER" ) );
224  break;
225  case VectorTile:
226  loadVectorTileConnections( doc, items );
227  break;
228  }
229  // clear connections list and close window
230  listConnections->clear();
231  accept();
232  }
233 
234  mFileName.clear();
235 }
236 
237 bool QgsManageConnectionsDialog::populateConnections()
238 {
239  // Export mode. Populate connections list from settings
240  if ( mDialogMode == Export )
241  {
242  QgsSettings settings;
243  switch ( mConnectionType )
244  {
245  case WMS:
246  settings.beginGroup( QStringLiteral( "/qgis/connections-wms" ) );
247  break;
248  case WFS:
249  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
250  break;
251  case WCS:
252  settings.beginGroup( QStringLiteral( "/qgis/connections-wcs" ) );
253  break;
254  case PostGIS:
255  settings.beginGroup( QStringLiteral( "/PostgreSQL/connections" ) );
256  break;
257  case MSSQL:
258  settings.beginGroup( QStringLiteral( "/MSSQL/connections" ) );
259  break;
260  case Oracle:
261  settings.beginGroup( QStringLiteral( "/Oracle/connections" ) );
262  break;
263  case DB2:
264  settings.beginGroup( QStringLiteral( "/DB2/connections" ) );
265  break;
266  case GeoNode:
267  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
268  break;
269  case XyzTiles:
270  settings.beginGroup( QStringLiteral( "/qgis/connections-xyz" ) );
271  break;
272  case ArcgisMapServer:
273  settings.beginGroup( QStringLiteral( "/qgis/connections-arcgismapserver" ) );
274  break;
275  case ArcgisFeatureServer:
276  settings.beginGroup( QStringLiteral( "/qgis/connections-arcgisfeatureserver" ) );
277  break;
278  case VectorTile:
279  settings.beginGroup( QStringLiteral( "/qgis/connections-vector-tile" ) );
280  break;
281  }
282  QStringList keys = settings.childGroups();
283  QStringList::Iterator it = keys.begin();
284  while ( it != keys.end() )
285  {
286  QListWidgetItem *item = new QListWidgetItem();
287  item->setText( *it );
288  listConnections->addItem( item );
289  ++it;
290  }
291  settings.endGroup();
292  }
293  // Import mode. Populate connections list from file
294  else
295  {
296  QFile file( mFileName );
297  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
298  {
299  QMessageBox::warning( this, tr( "Loading Connections" ),
300  tr( "Cannot read file %1:\n%2." )
301  .arg( mFileName,
302  file.errorString() ) );
303  return false;
304  }
305 
306  QDomDocument doc;
307  QString errorStr;
308  int errorLine;
309  int errorColumn;
310 
311  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
312  {
313  QMessageBox::warning( this, tr( "Loading Connections" ),
314  tr( "Parse error at line %1, column %2:\n%3" )
315  .arg( errorLine )
316  .arg( errorColumn )
317  .arg( errorStr ) );
318  return false;
319  }
320 
321  QDomElement root = doc.documentElement();
322  switch ( mConnectionType )
323  {
324  case WMS:
325  if ( root.tagName() != QLatin1String( "qgsWMSConnections" ) )
326  {
327  QMessageBox::information( this, tr( "Loading Connections" ),
328  tr( "The file is not a WMS connections exchange file." ) );
329  return false;
330  }
331  break;
332 
333  case WFS:
334  if ( root.tagName() != QLatin1String( "qgsWFSConnections" ) )
335  {
336  QMessageBox::information( this, tr( "Loading Connections" ),
337  tr( "The file is not a WFS connections exchange file." ) );
338  return false;
339  }
340  break;
341 
342  case WCS:
343  if ( root.tagName() != QLatin1String( "qgsWCSConnections" ) )
344  {
345  QMessageBox::information( this, tr( "Loading Connections" ),
346  tr( "The file is not a WCS connections exchange file." ) );
347  return false;
348  }
349  break;
350 
351  case PostGIS:
352  if ( root.tagName() != QLatin1String( "qgsPgConnections" ) )
353  {
354  QMessageBox::information( this, tr( "Loading Connections" ),
355  tr( "The file is not a PostGIS connections exchange file." ) );
356  return false;
357  }
358  break;
359 
360  case MSSQL:
361  if ( root.tagName() != QLatin1String( "qgsMssqlConnections" ) )
362  {
363  QMessageBox::information( this, tr( "Loading Connections" ),
364  tr( "The file is not a MSSQL connections exchange file." ) );
365  return false;
366  }
367  break;
368  case Oracle:
369  if ( root.tagName() != QLatin1String( "qgsOracleConnections" ) )
370  {
371  QMessageBox::information( this, tr( "Loading Connections" ),
372  tr( "The file is not an Oracle connections exchange file." ) );
373  return false;
374  }
375  break;
376  case DB2:
377  if ( root.tagName() != QLatin1String( "qgsDb2Connections" ) )
378  {
379  QMessageBox::information( this, tr( "Loading Connections" ),
380  tr( "The file is not a DB2 connections exchange file." ) );
381  return false;
382  }
383  break;
384  case GeoNode:
385  if ( root.tagName() != QLatin1String( "qgsGeoNodeConnections" ) )
386  {
387  QMessageBox::information( this, tr( "Loading Connections" ),
388  tr( "The file is not a GeoNode connections exchange file." ) );
389  return false;
390  }
391  break;
392  case XyzTiles:
393  if ( root.tagName() != QLatin1String( "qgsXYZTilesConnections" ) )
394  {
395  QMessageBox::information( this, tr( "Loading Connections" ),
396  tr( "The file is not a XYZ Tiles connections exchange file." ) );
397  return false;
398  }
399  break;
400  case ArcgisMapServer:
401  if ( root.tagName() != QLatin1String( "qgsARCGISMAPSERVERConnections" ) )
402  {
403  QMessageBox::information( this, tr( "Loading Connections" ),
404  tr( "The file is not a ArcGIS Map Service connections exchange file." ) );
405  return false;
406  }
407  break;
408  case ArcgisFeatureServer:
409  if ( root.tagName() != QLatin1String( "qgsARCGISFEATURESERVERConnections" ) )
410  {
411  QMessageBox::information( this, tr( "Loading Connections" ),
412  tr( "The file is not a ArcGIS Feature Service connections exchange file." ) );
413  return false;
414  }
415  break;
416  case VectorTile:
417  if ( root.tagName() != QLatin1String( "qgsVectorTileConnections" ) )
418  {
419  QMessageBox::information( this, tr( "Loading Connections" ),
420  tr( "The file is not a Vector Tile connections exchange file." ) );
421  return false;
422  }
423  break;
424  }
425 
426  QDomElement child = root.firstChildElement();
427  while ( !child.isNull() )
428  {
429  QListWidgetItem *item = new QListWidgetItem();
430  item->setText( child.attribute( QStringLiteral( "name" ) ) );
431  listConnections->addItem( item );
432  child = child.nextSiblingElement();
433  }
434  }
435  return true;
436 }
437 
438 QDomDocument QgsManageConnectionsDialog::saveOWSConnections( const QStringList &connections, const QString &service )
439 {
440  QDomDocument doc( QStringLiteral( "connections" ) );
441  QDomElement root = doc.createElement( "qgs" + service.toUpper() + "Connections" );
442  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
443  doc.appendChild( root );
444 
445  QgsSettings settings;
446  QString path;
447  for ( int i = 0; i < connections.count(); ++i )
448  {
449  path = "/qgis/connections-" + service.toLower() + '/';
450  QDomElement el = doc.createElement( service.toLower() );
451  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
452  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url" ).toString() );
453 
454  if ( service == QLatin1String( "WMS" ) )
455  {
456  el.setAttribute( QStringLiteral( "ignoreGetMapURI" ), settings.value( path + connections[i] + "/ignoreGetMapURI", false ).toBool() ? "true" : "false" );
457  el.setAttribute( QStringLiteral( "ignoreGetFeatureInfoURI" ), settings.value( path + connections[i] + "/ignoreGetFeatureInfoURI", false ).toBool() ? "true" : "false" );
458  el.setAttribute( QStringLiteral( "ignoreAxisOrientation" ), settings.value( path + connections[i] + "/ignoreAxisOrientation", false ).toBool() ? "true" : "false" );
459  el.setAttribute( QStringLiteral( "invertAxisOrientation" ), settings.value( path + connections[i] + "/invertAxisOrientation", false ).toBool() ? "true" : "false" );
460  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + connections[ i ] + "/referer" ).toString() );
461  el.setAttribute( QStringLiteral( "smoothPixmapTransform" ), settings.value( path + connections[i] + "/smoothPixmapTransform", false ).toBool() ? "true" : "false" );
462  el.setAttribute( QStringLiteral( "dpiMode" ), settings.value( path + connections[i] + "/dpiMode", "7" ).toInt() );
463  }
464 
465  path = "/qgis/" + service.toUpper() + '/';
466  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username" ).toString() );
467  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password" ).toString() );
468  root.appendChild( el );
469  }
470 
471  return doc;
472 }
473 
474 QDomDocument QgsManageConnectionsDialog::saveWfsConnections( const QStringList &connections )
475 {
476  QDomDocument doc( QStringLiteral( "connections" ) );
477  QDomElement root = doc.createElement( QStringLiteral( "qgsWFSConnections" ) );
478  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.1" ) );
479  doc.appendChild( root );
480 
481  QgsSettings settings;
482  QString path;
483  for ( int i = 0; i < connections.count(); ++i )
484  {
485  path = QStringLiteral( "/qgis/connections-wfs/" );
486  QDomElement el = doc.createElement( QStringLiteral( "wfs" ) );
487  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
488  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url" ).toString() );
489 
490  el.setAttribute( QStringLiteral( "version" ), settings.value( path + connections[ i ] + "/version" ).toString() );
491  el.setAttribute( QStringLiteral( "maxnumfeatures" ), settings.value( path + connections[ i ] + "/maxnumfeatures" ).toString() );
492  el.setAttribute( QStringLiteral( "pagesize" ), settings.value( path + connections[ i ] + "/pagesize" ).toString() );
493  el.setAttribute( QStringLiteral( "pagingenabled" ), settings.value( path + connections[ i ] + "/pagingenabled", false ).toString() );
494  el.setAttribute( QStringLiteral( "ignoreAxisOrientation" ), settings.value( path + connections[ i ] + "/ignoreAxisOrientation", false ).toString() );
495  el.setAttribute( QStringLiteral( "invertAxisOrientation" ), settings.value( path + connections[ i ] + "/invertAxisOrientation", false ).toString() );
496 
497  path = QStringLiteral( "/qgis/WFS/" );
498  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username" ).toString() );
499  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password" ).toString() );
500  root.appendChild( el );
501  }
502 
503  return doc;
504 }
505 
506 QDomDocument QgsManageConnectionsDialog::savePgConnections( const QStringList &connections )
507 {
508  QDomDocument doc( QStringLiteral( "connections" ) );
509  QDomElement root = doc.createElement( QStringLiteral( "qgsPgConnections" ) );
510  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
511  doc.appendChild( root );
512 
513  QgsSettings settings;
514  QString path;
515  for ( int i = 0; i < connections.count(); ++i )
516  {
517  path = "/PostgreSQL/connections/" + connections[ i ];
518  QDomElement el = doc.createElement( QStringLiteral( "postgis" ) );
519  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
520  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host" ).toString() );
521  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port" ).toString() );
522  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database" ).toString() );
523  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service" ).toString() );
524  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
525  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
526 
527  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
528 
529  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
530  {
531  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
532  }
533 
534  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
535 
536  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
537  {
538  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
539  }
540 
541  root.appendChild( el );
542  }
543 
544  return doc;
545 }
546 
547 QDomDocument QgsManageConnectionsDialog::saveMssqlConnections( const QStringList &connections )
548 {
549  QDomDocument doc( QStringLiteral( "connections" ) );
550  QDomElement root = doc.createElement( QStringLiteral( "qgsMssqlConnections" ) );
551  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
552  doc.appendChild( root );
553 
554  QgsSettings settings;
555  QString path;
556  for ( int i = 0; i < connections.count(); ++i )
557  {
558  path = "/MSSQL/connections/" + connections[ i ];
559  QDomElement el = doc.createElement( QStringLiteral( "mssql" ) );
560  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
561  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host" ).toString() );
562  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port" ).toString() );
563  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database" ).toString() );
564  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service" ).toString() );
565  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
566  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
567 
568  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
569 
570  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
571  {
572  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
573  }
574 
575  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
576 
577  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
578  {
579  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
580  }
581 
582  root.appendChild( el );
583  }
584 
585  return doc;
586 }
587 
588 QDomDocument QgsManageConnectionsDialog::saveOracleConnections( const QStringList &connections )
589 {
590  QDomDocument doc( QStringLiteral( "connections" ) );
591  QDomElement root = doc.createElement( QStringLiteral( "qgsOracleConnections" ) );
592  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
593  doc.appendChild( root );
594 
595  QgsSettings settings;
596  QString path;
597  for ( int i = 0; i < connections.count(); ++i )
598  {
599  path = "/Oracle/connections/" + connections[ i ];
600  QDomElement el = doc.createElement( QStringLiteral( "oracle" ) );
601  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
602  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host" ).toString() );
603  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port" ).toString() );
604  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database" ).toString() );
605  el.setAttribute( QStringLiteral( "dboptions" ), settings.value( path + "/dboptions" ).toString() );
606  el.setAttribute( QStringLiteral( "dbworkspace" ), settings.value( path + "/dbworkspace" ).toString() );
607  el.setAttribute( QStringLiteral( "schema" ), settings.value( path + "/schema" ).toString() );
608  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
609  el.setAttribute( QStringLiteral( "userTablesOnly" ), settings.value( path + "/userTablesOnly", "0" ).toString() );
610  el.setAttribute( QStringLiteral( "geometryColumnsOnly" ), settings.value( path + "/geometryColumnsOnly", "0" ).toString() );
611  el.setAttribute( QStringLiteral( "allowGeometrylessTables" ), settings.value( path + "/allowGeometrylessTables", "0" ).toString() );
612 
613  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
614 
615  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
616  {
617  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
618  }
619 
620  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
621 
622  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
623  {
624  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
625  }
626 
627  root.appendChild( el );
628  }
629 
630  return doc;
631 }
632 
633 QDomDocument QgsManageConnectionsDialog::saveDb2Connections( const QStringList &connections )
634 {
635  QDomDocument doc( QStringLiteral( "connections" ) );
636  QDomElement root = doc.createElement( QStringLiteral( "qgsDb2Connections" ) );
637  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
638  doc.appendChild( root );
639 
640  QgsSettings settings;
641  QString path;
642  for ( int i = 0; i < connections.count(); ++i )
643  {
644  path = "/DB2/connections/" + connections[ i ];
645  QDomElement el = doc.createElement( QStringLiteral( "db2" ) );
646  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
647  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host" ).toString() );
648  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port" ).toString() );
649  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database" ).toString() );
650  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service" ).toString() );
651  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
652  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
653 
654  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
655 
656  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
657  {
658  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
659  }
660 
661  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
662 
663  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
664  {
665  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
666  }
667 
668  root.appendChild( el );
669  }
670 
671  return doc;
672 }
673 
674 QDomDocument QgsManageConnectionsDialog::saveGeonodeConnections( const QStringList &connections )
675 {
676  QDomDocument doc( QStringLiteral( "connections" ) );
677  QDomElement root = doc.createElement( QStringLiteral( "qgsGeoNodeConnections" ) );
678  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
679  doc.appendChild( root );
680 
681  QgsSettings settings;
682  QString path;
683  for ( int i = 0; i < connections.count(); ++i )
684  {
685  path = QStringLiteral( "/qgis/connections-geonode/" );
686  QDomElement el = doc.createElement( QStringLiteral( "geonode" ) );
687  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
688  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url" ).toString() );
689 
690  path = QStringLiteral( "/qgis/GeoNode/" );
691  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username" ).toString() );
692  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password" ).toString() );
693  root.appendChild( el );
694  }
695 
696  return doc;
697 }
698 
699 QDomDocument QgsManageConnectionsDialog::saveXyzTilesConnections( const QStringList &connections )
700 {
701  QDomDocument doc( QStringLiteral( "connections" ) );
702  QDomElement root = doc.createElement( QStringLiteral( "qgsXYZTilesConnections" ) );
703  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
704  doc.appendChild( root );
705 
706  QgsSettings settings;
707  QString path;
708  for ( int i = 0; i < connections.count(); ++i )
709  {
710  path = "qgis/connections-xyz/" + connections[ i ];
711  QDomElement el = doc.createElement( QStringLiteral( "xyztiles" ) );
712 
713  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
714  el.setAttribute( QStringLiteral( "url" ), settings.value( path + "/url" ).toString() );
715  el.setAttribute( QStringLiteral( "zmin" ), settings.value( path + "/zmin", -1 ).toInt() );
716  el.setAttribute( QStringLiteral( "zmax" ), settings.value( path + "/zmax", -1 ).toInt() );
717  el.setAttribute( QStringLiteral( "authcfg" ), settings.value( path + "/authcfg" ).toString() );
718  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
719  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
720  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + "/referer" ).toString() );
721  el.setAttribute( QStringLiteral( "tilePixelRatio" ), settings.value( path + "/tilePixelRatio", 0 ).toDouble() );
722 
723  root.appendChild( el );
724  }
725 
726  return doc;
727 }
728 
729 QDomDocument QgsManageConnectionsDialog::saveArcgisConnections( const QStringList &connections, const QString &service )
730 {
731  QDomDocument doc( QStringLiteral( "connections" ) );
732  QDomElement root = doc.createElement( "qgs" + service.toUpper() + "Connections" );
733  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
734  doc.appendChild( root );
735 
736  QgsSettings settings;
737  QString path;
738  for ( int i = 0; i < connections.count(); ++i )
739  {
740  path = "/qgis/connections-" + service.toLower() + '/';
741  QDomElement el = doc.createElement( service.toLower() );
742  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
743  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url" ).toString() );
744  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + connections[ i ] + "/referer" ).toString() );
745 
746  path = "/qgis/" + service.toUpper() + '/';
747  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username" ).toString() );
748  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password" ).toString() );
749  el.setAttribute( QStringLiteral( "authcfg" ), settings.value( path + connections[ i ] + "/authcfg" ).toString() );
750  root.appendChild( el );
751  }
752 
753  return doc;
754 }
755 
756 QDomDocument QgsManageConnectionsDialog::saveVectorTileConnections( const QStringList &connections )
757 {
758  QDomDocument doc( QStringLiteral( "connections" ) );
759  QDomElement root = doc.createElement( QStringLiteral( "qgsVectorTileConnections" ) );
760  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
761  doc.appendChild( root );
762 
763  QgsSettings settings;
764  QString path;
765  for ( int i = 0; i < connections.count(); ++i )
766  {
767  path = "qgis/connections-vector-tile/" + connections[ i ];
768  QDomElement el = doc.createElement( QStringLiteral( "vectortile" ) );
769 
770  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
771  el.setAttribute( QStringLiteral( "url" ), settings.value( path + "/url" ).toString() );
772  el.setAttribute( QStringLiteral( "zmin" ), settings.value( path + "/zmin", -1 ).toInt() );
773  el.setAttribute( QStringLiteral( "zmax" ), settings.value( path + "/zmax", -1 ).toInt() );
774 
775  root.appendChild( el );
776  }
777 
778  return doc;
779 }
780 
781 void QgsManageConnectionsDialog::loadOWSConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
782 {
783  QDomElement root = doc.documentElement();
784  if ( root.tagName() != "qgs" + service.toUpper() + "Connections" )
785  {
786  QMessageBox::information( this, tr( "Loading Connections" ),
787  tr( "The file is not a %1 connections exchange file." ).arg( service ) );
788  return;
789  }
790 
791  QString connectionName;
792  QgsSettings settings;
793  settings.beginGroup( "/qgis/connections-" + service.toLower() );
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( QStringLiteral( "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 
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( "/qgis/connections-" + service.toLower() );
847  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
848  settings.setValue( QString( '/' + connectionName + "/ignoreGetMapURI" ), child.attribute( QStringLiteral( "ignoreGetMapURI" ) ) == QLatin1String( "true" ) );
849  settings.setValue( QString( '/' + connectionName + "/ignoreGetFeatureInfoURI" ), child.attribute( QStringLiteral( "ignoreGetFeatureInfoURI" ) ) == QLatin1String( "true" ) );
850  settings.setValue( QString( '/' + connectionName + "/ignoreAxisOrientation" ), child.attribute( QStringLiteral( "ignoreAxisOrientation" ) ) == QLatin1String( "true" ) );
851  settings.setValue( QString( '/' + connectionName + "/invertAxisOrientation" ), child.attribute( QStringLiteral( "invertAxisOrientation" ) ) == QLatin1String( "true" ) );
852  settings.setValue( QString( '/' + connectionName + "/referer" ), child.attribute( QStringLiteral( "referer" ) ) );
853  settings.setValue( QString( '/' + connectionName + "/smoothPixmapTransform" ), child.attribute( QStringLiteral( "smoothPixmapTransform" ) ) == QLatin1String( "true" ) );
854  settings.setValue( QString( '/' + connectionName + "/dpiMode" ), child.attribute( QStringLiteral( "dpiMode" ), QStringLiteral( "7" ) ).toInt() );
855  settings.endGroup();
856 
857  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
858  {
859  settings.beginGroup( "/qgis/" + service.toUpper() + '/' + connectionName );
860  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
861  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
862  settings.endGroup();
863  }
864  child = child.nextSiblingElement();
865  }
866 }
867 
868 void QgsManageConnectionsDialog::loadWfsConnections( const QDomDocument &doc, const QStringList &items )
869 {
870  QDomElement root = doc.documentElement();
871  if ( root.tagName() != QLatin1String( "qgsWFSConnections" ) )
872  {
873  QMessageBox::information( this, tr( "Loading Connections" ),
874  tr( "The file is not a WFS connections exchange file." ) );
875  return;
876  }
877 
878  QString connectionName;
879  QgsSettings settings;
880  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
881  QStringList keys = settings.childGroups();
882  settings.endGroup();
883  QDomElement child = root.firstChildElement();
884  bool prompt = true;
885  bool overwrite = true;
886 
887  while ( !child.isNull() )
888  {
889  connectionName = child.attribute( QStringLiteral( "name" ) );
890  if ( !items.contains( connectionName ) )
891  {
892  child = child.nextSiblingElement();
893  continue;
894  }
895 
896  // check for duplicates
897  if ( keys.contains( connectionName ) && prompt )
898  {
899  int res = QMessageBox::warning( this,
900  tr( "Loading Connections" ),
901  tr( "Connection with name '%1' already exists. Overwrite?" )
902  .arg( connectionName ),
903  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
904 
905  switch ( res )
906  {
907  case QMessageBox::Cancel:
908  return;
909  case QMessageBox::No:
910  child = child.nextSiblingElement();
911  continue;
912  case QMessageBox::Yes:
913  overwrite = true;
914  break;
915  case QMessageBox::YesToAll:
916  prompt = false;
917  overwrite = true;
918  break;
919  case QMessageBox::NoToAll:
920  prompt = false;
921  overwrite = false;
922  break;
923  }
924  }
925 
926  if ( keys.contains( connectionName ) && !overwrite )
927  {
928  child = child.nextSiblingElement();
929  continue;
930  }
931 
932  // no dups detected or overwrite is allowed
933  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
934  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
935 
936  settings.setValue( QString( '/' + connectionName + "/version" ), child.attribute( QStringLiteral( "version" ) ) );
937  settings.setValue( QString( '/' + connectionName + "/maxnumfeatures" ), child.attribute( QStringLiteral( "maxnumfeatures" ) ) );
938  settings.setValue( QString( '/' + connectionName + "/pagesize" ), child.attribute( QStringLiteral( "pagesize" ) ) );
939  settings.setValue( QString( '/' + connectionName + "/pagingenabled" ), child.attribute( QStringLiteral( "pagingenabled" ) ) );
940  settings.setValue( QString( '/' + connectionName + "/ignoreAxisOrientation" ), child.attribute( QStringLiteral( "ignoreAxisOrientation" ) ) );
941  settings.setValue( QString( '/' + connectionName + "/invertAxisOrientation" ), child.attribute( QStringLiteral( "invertAxisOrientation" ) ) );
942  settings.endGroup();
943 
944  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
945  {
946  settings.beginGroup( "/qgis/WFS/" + connectionName );
947  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
948  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
949  settings.endGroup();
950  }
951  child = child.nextSiblingElement();
952  }
953 }
954 
955 void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, const QStringList &items )
956 {
957  QDomElement root = doc.documentElement();
958  if ( root.tagName() != QLatin1String( "qgsPgConnections" ) )
959  {
960  QMessageBox::information( this,
961  tr( "Loading Connections" ),
962  tr( "The file is not a PostGIS connections exchange file." ) );
963  return;
964  }
965 
966  QString connectionName;
967  QgsSettings settings;
968  settings.beginGroup( QStringLiteral( "/PostgreSQL/connections" ) );
969  QStringList keys = settings.childGroups();
970  settings.endGroup();
971  QDomElement child = root.firstChildElement();
972  bool prompt = true;
973  bool overwrite = true;
974 
975  while ( !child.isNull() )
976  {
977  connectionName = child.attribute( QStringLiteral( "name" ) );
978  if ( !items.contains( connectionName ) )
979  {
980  child = child.nextSiblingElement();
981  continue;
982  }
983 
984  // check for duplicates
985  if ( keys.contains( connectionName ) && prompt )
986  {
987  int res = QMessageBox::warning( this,
988  tr( "Loading Connections" ),
989  tr( "Connection with name '%1' already exists. Overwrite?" )
990  .arg( connectionName ),
991  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
992  switch ( res )
993  {
994  case QMessageBox::Cancel:
995  return;
996  case QMessageBox::No:
997  child = child.nextSiblingElement();
998  continue;
999  case QMessageBox::Yes:
1000  overwrite = true;
1001  break;
1002  case QMessageBox::YesToAll:
1003  prompt = false;
1004  overwrite = true;
1005  break;
1006  case QMessageBox::NoToAll:
1007  prompt = false;
1008  overwrite = false;
1009  break;
1010  }
1011  }
1012 
1013  if ( keys.contains( connectionName ) && !overwrite )
1014  {
1015  child = child.nextSiblingElement();
1016  continue;
1017  }
1018 
1019  //no dups detected or overwrite is allowed
1020  settings.beginGroup( "/PostgreSQL/connections/" + connectionName );
1021 
1022  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1023  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1024  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1025  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1026  {
1027  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1028  }
1029  else
1030  {
1031  settings.setValue( QStringLiteral( "/service" ), "" );
1032  }
1033  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1034  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1035  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1036  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1037  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1038  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1039  settings.endGroup();
1040 
1041  child = child.nextSiblingElement();
1042  }
1043 }
1044 
1045 void QgsManageConnectionsDialog::loadMssqlConnections( const QDomDocument &doc, const QStringList &items )
1046 {
1047  QDomElement root = doc.documentElement();
1048  if ( root.tagName() != QLatin1String( "qgsMssqlConnections" ) )
1049  {
1050  QMessageBox::information( this,
1051  tr( "Loading Connections" ),
1052  tr( "The file is not a MSSQL connections exchange file." ) );
1053  return;
1054  }
1055 
1056  QString connectionName;
1057  QgsSettings settings;
1058  settings.beginGroup( QStringLiteral( "/MSSQL/connections" ) );
1059  QStringList keys = settings.childGroups();
1060  settings.endGroup();
1061  QDomElement child = root.firstChildElement();
1062  bool prompt = true;
1063  bool overwrite = true;
1064 
1065  while ( !child.isNull() )
1066  {
1067  connectionName = child.attribute( QStringLiteral( "name" ) );
1068  if ( !items.contains( connectionName ) )
1069  {
1070  child = child.nextSiblingElement();
1071  continue;
1072  }
1073 
1074  // check for duplicates
1075  if ( keys.contains( connectionName ) && prompt )
1076  {
1077  int res = QMessageBox::warning( this,
1078  tr( "Loading Connections" ),
1079  tr( "Connection with name '%1' already exists. Overwrite?" )
1080  .arg( connectionName ),
1081  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1082  switch ( res )
1083  {
1084  case QMessageBox::Cancel:
1085  return;
1086  case QMessageBox::No:
1087  child = child.nextSiblingElement();
1088  continue;
1089  case QMessageBox::Yes:
1090  overwrite = true;
1091  break;
1092  case QMessageBox::YesToAll:
1093  prompt = false;
1094  overwrite = true;
1095  break;
1096  case QMessageBox::NoToAll:
1097  prompt = false;
1098  overwrite = false;
1099  break;
1100  }
1101  }
1102 
1103  if ( keys.contains( connectionName ) && !overwrite )
1104  {
1105  child = child.nextSiblingElement();
1106  continue;
1107  }
1108 
1109  //no dups detected or overwrite is allowed
1110  settings.beginGroup( "/MSSQL/connections/" + connectionName );
1111 
1112  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1113  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1114  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1115  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1116  {
1117  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1118  }
1119  else
1120  {
1121  settings.setValue( QStringLiteral( "/service" ), "" );
1122  }
1123  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1124  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1125  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1126  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1127  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1128  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1129  settings.endGroup();
1130 
1131  child = child.nextSiblingElement();
1132  }
1133 }
1134 
1135 void QgsManageConnectionsDialog::loadOracleConnections( const QDomDocument &doc, const QStringList &items )
1136 {
1137  QDomElement root = doc.documentElement();
1138  if ( root.tagName() != QLatin1String( "qgsOracleConnections" ) )
1139  {
1140  QMessageBox::information( this,
1141  tr( "Loading Connections" ),
1142  tr( "The file is not an Oracle connections exchange file." ) );
1143  return;
1144  }
1145 
1146  QString connectionName;
1147  QgsSettings settings;
1148  settings.beginGroup( QStringLiteral( "/Oracle/connections" ) );
1149  QStringList keys = settings.childGroups();
1150  settings.endGroup();
1151  QDomElement child = root.firstChildElement();
1152  bool prompt = true;
1153  bool overwrite = true;
1154 
1155  while ( !child.isNull() )
1156  {
1157  connectionName = child.attribute( QStringLiteral( "name" ) );
1158  if ( !items.contains( connectionName ) )
1159  {
1160  child = child.nextSiblingElement();
1161  continue;
1162  }
1163 
1164  // check for duplicates
1165  if ( keys.contains( connectionName ) && prompt )
1166  {
1167  int res = QMessageBox::warning( this,
1168  tr( "Loading Connections" ),
1169  tr( "Connection with name '%1' already exists. Overwrite?" )
1170  .arg( connectionName ),
1171  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1172  switch ( res )
1173  {
1174  case QMessageBox::Cancel:
1175  return;
1176  case QMessageBox::No:
1177  child = child.nextSiblingElement();
1178  continue;
1179  case QMessageBox::Yes:
1180  overwrite = true;
1181  break;
1182  case QMessageBox::YesToAll:
1183  prompt = false;
1184  overwrite = true;
1185  break;
1186  case QMessageBox::NoToAll:
1187  prompt = false;
1188  overwrite = false;
1189  break;
1190  }
1191  }
1192 
1193  if ( keys.contains( connectionName ) && !overwrite )
1194  {
1195  child = child.nextSiblingElement();
1196  continue;
1197  }
1198 
1199  //no dups detected or overwrite is allowed
1200  settings.beginGroup( "/Oracle/connections/" + connectionName );
1201 
1202  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1203  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1204  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1205  settings.setValue( QStringLiteral( "/dboptions" ), child.attribute( QStringLiteral( "dboptions" ) ) );
1206  settings.setValue( QStringLiteral( "/dbworkspace" ), child.attribute( QStringLiteral( "dbworkspace" ) ) );
1207  settings.setValue( QStringLiteral( "/schema" ), child.attribute( QStringLiteral( "schema" ) ) );
1208  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1209  settings.setValue( QStringLiteral( "/userTablesOnly" ), child.attribute( QStringLiteral( "userTablesOnly" ) ) );
1210  settings.setValue( QStringLiteral( "/geometryColumnsOnly" ), child.attribute( QStringLiteral( "geometryColumnsOnly" ) ) );
1211  settings.setValue( QStringLiteral( "/allowGeometrylessTables" ), child.attribute( QStringLiteral( "allowGeometrylessTables" ) ) );
1212  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1213  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1214  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1215  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1216  settings.endGroup();
1217 
1218  child = child.nextSiblingElement();
1219  }
1220 }
1221 
1222 void QgsManageConnectionsDialog::loadDb2Connections( const QDomDocument &doc, const QStringList &items )
1223 {
1224  QDomElement root = doc.documentElement();
1225  if ( root.tagName() != QLatin1String( "qgsDb2Connections" ) )
1226  {
1227  QMessageBox::information( this,
1228  tr( "Loading Connections" ),
1229  tr( "The file is not a DB2 connections exchange file." ) );
1230  return;
1231  }
1232 
1233  QString connectionName;
1234  QgsSettings settings;
1235  settings.beginGroup( QStringLiteral( "/DB2/connections" ) );
1236  QStringList keys = settings.childGroups();
1237  settings.endGroup();
1238  QDomElement child = root.firstChildElement();
1239  bool prompt = true;
1240  bool overwrite = true;
1241 
1242  while ( !child.isNull() )
1243  {
1244  connectionName = child.attribute( QStringLiteral( "name" ) );
1245  if ( !items.contains( connectionName ) )
1246  {
1247  child = child.nextSiblingElement();
1248  continue;
1249  }
1250 
1251  // check for duplicates
1252  if ( keys.contains( connectionName ) && prompt )
1253  {
1254  int res = QMessageBox::warning( this,
1255  tr( "Loading Connections" ),
1256  tr( "Connection with name '%1' already exists. Overwrite?" )
1257  .arg( connectionName ),
1258  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1259  switch ( res )
1260  {
1261  case QMessageBox::Cancel:
1262  return;
1263  case QMessageBox::No:
1264  child = child.nextSiblingElement();
1265  continue;
1266  case QMessageBox::Yes:
1267  overwrite = true;
1268  break;
1269  case QMessageBox::YesToAll:
1270  prompt = false;
1271  overwrite = true;
1272  break;
1273  case QMessageBox::NoToAll:
1274  prompt = false;
1275  overwrite = false;
1276  break;
1277  }
1278  }
1279 
1280  if ( keys.contains( connectionName ) && !overwrite )
1281  {
1282  child = child.nextSiblingElement();
1283  continue;
1284  }
1285 
1286  //no dups detected or overwrite is allowed
1287  settings.beginGroup( "/DB2/connections/" + connectionName );
1288 
1289  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1290  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1291  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1292  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1293  {
1294  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1295  }
1296  else
1297  {
1298  settings.setValue( QStringLiteral( "/service" ), "" );
1299  }
1300  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1301  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1302  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1303  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1304  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1305  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1306  settings.endGroup();
1307 
1308  child = child.nextSiblingElement();
1309  }
1310 }
1311 
1312 void QgsManageConnectionsDialog::loadGeonodeConnections( const QDomDocument &doc, const QStringList &items )
1313 {
1314  QDomElement root = doc.documentElement();
1315  if ( root.tagName() != QLatin1String( "qgsGeoNodeConnections" ) )
1316  {
1317  QMessageBox::information( this, tr( "Loading Connections" ),
1318  tr( "The file is not a GeoNode connections exchange file." ) );
1319  return;
1320  }
1321 
1322  QString connectionName;
1323  QgsSettings settings;
1324  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
1325  QStringList keys = settings.childGroups();
1326  settings.endGroup();
1327  QDomElement child = root.firstChildElement();
1328  bool prompt = true;
1329  bool overwrite = true;
1330 
1331  while ( !child.isNull() )
1332  {
1333  connectionName = child.attribute( QStringLiteral( "name" ) );
1334  if ( !items.contains( connectionName ) )
1335  {
1336  child = child.nextSiblingElement();
1337  continue;
1338  }
1339 
1340  // check for duplicates
1341  if ( keys.contains( connectionName ) && prompt )
1342  {
1343  int res = QMessageBox::warning( this,
1344  tr( "Loading Connections" ),
1345  tr( "Connection with name '%1' already exists. Overwrite?" )
1346  .arg( connectionName ),
1347  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1348 
1349  switch ( res )
1350  {
1351  case QMessageBox::Cancel:
1352  return;
1353  case QMessageBox::No:
1354  child = child.nextSiblingElement();
1355  continue;
1356  case QMessageBox::Yes:
1357  overwrite = true;
1358  break;
1359  case QMessageBox::YesToAll:
1360  prompt = false;
1361  overwrite = true;
1362  break;
1363  case QMessageBox::NoToAll:
1364  prompt = false;
1365  overwrite = false;
1366  break;
1367  }
1368  }
1369 
1370  if ( keys.contains( connectionName ) && !overwrite )
1371  {
1372  child = child.nextSiblingElement();
1373  continue;
1374  }
1375 
1376  // no dups detected or overwrite is allowed
1377  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
1378  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
1379  settings.endGroup();
1380 
1381  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
1382  {
1383  settings.beginGroup( "/qgis/GeoNode/" + connectionName );
1384  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1385  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1386  settings.endGroup();
1387  }
1388  child = child.nextSiblingElement();
1389  }
1390 }
1391 
1392 void QgsManageConnectionsDialog::loadXyzTilesConnections( const QDomDocument &doc, const QStringList &items )
1393 {
1394  QDomElement root = doc.documentElement();
1395  if ( root.tagName() != QLatin1String( "qgsXYZTilesConnections" ) )
1396  {
1397  QMessageBox::information( this, tr( "Loading Connections" ),
1398  tr( "The file is not a XYZ Tiles connections exchange file." ) );
1399  return;
1400  }
1401 
1402  QString connectionName;
1403  QgsSettings settings;
1404  settings.beginGroup( QStringLiteral( "/qgis/connections-xyz" ) );
1405  QStringList keys = settings.childGroups();
1406  settings.endGroup();
1407  QDomElement child = root.firstChildElement();
1408  bool prompt = true;
1409  bool overwrite = true;
1410 
1411  while ( !child.isNull() )
1412  {
1413  connectionName = child.attribute( QStringLiteral( "name" ) );
1414  if ( !items.contains( connectionName ) )
1415  {
1416  child = child.nextSiblingElement();
1417  continue;
1418  }
1419 
1420  // check for duplicates
1421  if ( keys.contains( connectionName ) && prompt )
1422  {
1423  int res = QMessageBox::warning( this,
1424  tr( "Loading Connections" ),
1425  tr( "Connection with name '%1' already exists. Overwrite?" )
1426  .arg( connectionName ),
1427  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1428 
1429  switch ( res )
1430  {
1431  case QMessageBox::Cancel:
1432  return;
1433  case QMessageBox::No:
1434  child = child.nextSiblingElement();
1435  continue;
1436  case QMessageBox::Yes:
1437  overwrite = true;
1438  break;
1439  case QMessageBox::YesToAll:
1440  prompt = false;
1441  overwrite = true;
1442  break;
1443  case QMessageBox::NoToAll:
1444  prompt = false;
1445  overwrite = false;
1446  break;
1447  }
1448  }
1449 
1450  if ( keys.contains( connectionName ) && !overwrite )
1451  {
1452  child = child.nextSiblingElement();
1453  continue;
1454  }
1455 
1456  settings.beginGroup( "qgis/connections-xyz/" + connectionName );
1457  settings.setValue( QStringLiteral( "url" ), child.attribute( QStringLiteral( "url" ) ) );
1458  settings.setValue( QStringLiteral( "zmin" ), child.attribute( QStringLiteral( "zmin" ) ) );
1459  settings.setValue( QStringLiteral( "zmax" ), child.attribute( QStringLiteral( "zmax" ) ) );
1460  settings.setValue( QStringLiteral( "authcfg" ), child.attribute( QStringLiteral( "authcfg" ) ) );
1461  settings.setValue( QStringLiteral( "username" ), child.attribute( QStringLiteral( "username" ) ) );
1462  settings.setValue( QStringLiteral( "password" ), child.attribute( QStringLiteral( "password" ) ) );
1463  settings.setValue( QStringLiteral( "referer" ), child.attribute( QStringLiteral( "referer" ) ) );
1464  settings.setValue( QStringLiteral( "tilePixelRatio" ), child.attribute( QStringLiteral( "tilePixelRatio" ) ) );
1465  settings.endGroup();
1466 
1467  child = child.nextSiblingElement();
1468  }
1469 }
1470 
1471 void QgsManageConnectionsDialog::loadArcgisConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
1472 {
1473  QDomElement root = doc.documentElement();
1474  if ( root.tagName() != "qgs" + service.toUpper() + "Connections" )
1475  {
1476  QMessageBox::information( this, tr( "Loading Connections" ),
1477  tr( "The file is not a %1 connections exchange file." ).arg( service ) );
1478  return;
1479  }
1480 
1481  QString connectionName;
1482  QgsSettings settings;
1483  settings.beginGroup( "/qgis/connections-" + service.toLower() );
1484  QStringList keys = settings.childGroups();
1485  settings.endGroup();
1486  QDomElement child = root.firstChildElement();
1487  bool prompt = true;
1488  bool overwrite = true;
1489 
1490  while ( !child.isNull() )
1491  {
1492  connectionName = child.attribute( QStringLiteral( "name" ) );
1493  if ( !items.contains( connectionName ) )
1494  {
1495  child = child.nextSiblingElement();
1496  continue;
1497  }
1498 
1499  // check for duplicates
1500  if ( keys.contains( connectionName ) && prompt )
1501  {
1502  int res = QMessageBox::warning( this,
1503  tr( "Loading Connections" ),
1504  tr( "Connection with name '%1' already exists. Overwrite?" )
1505  .arg( connectionName ),
1506  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1507 
1508  switch ( res )
1509  {
1510  case QMessageBox::Cancel:
1511  return;
1512  case QMessageBox::No:
1513  child = child.nextSiblingElement();
1514  continue;
1515  case QMessageBox::Yes:
1516  overwrite = true;
1517  break;
1518  case QMessageBox::YesToAll:
1519  prompt = false;
1520  overwrite = true;
1521  break;
1522  case QMessageBox::NoToAll:
1523  prompt = false;
1524  overwrite = false;
1525  break;
1526  }
1527  }
1528 
1529  if ( keys.contains( connectionName ) && !overwrite )
1530  {
1531  child = child.nextSiblingElement();
1532  continue;
1533  }
1534 
1535  // no dups detected or overwrite is allowed
1536  settings.beginGroup( "/qgis/connections-" + service.toLower() );
1537  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
1538  settings.setValue( QString( '/' + connectionName + "/referer" ), child.attribute( QStringLiteral( "referer" ) ) );
1539  settings.endGroup();
1540 
1541  settings.beginGroup( "/qgis/" + service.toUpper() + '/' + connectionName );
1542  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1543  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1544  settings.setValue( QStringLiteral( "/authcfg" ), child.attribute( QStringLiteral( "authcfg" ) ) );
1545  settings.endGroup();
1546 
1547  child = child.nextSiblingElement();
1548  }
1549 }
1550 
1551 void QgsManageConnectionsDialog::loadVectorTileConnections( const QDomDocument &doc, const QStringList &items )
1552 {
1553  QDomElement root = doc.documentElement();
1554  if ( root.tagName() != QLatin1String( "qgsVectorTileConnections" ) )
1555  {
1556  QMessageBox::information( this, tr( "Loading Connections" ),
1557  tr( "The file is not a Vector Tile connections exchange file." ) );
1558  return;
1559  }
1560 
1561  QString connectionName;
1562  QgsSettings settings;
1563  settings.beginGroup( QStringLiteral( "/qgis/connections-vector-tile" ) );
1564  QStringList keys = settings.childGroups();
1565  settings.endGroup();
1566  QDomElement child = root.firstChildElement();
1567  bool prompt = true;
1568  bool overwrite = true;
1569 
1570  while ( !child.isNull() )
1571  {
1572  connectionName = child.attribute( QStringLiteral( "name" ) );
1573  if ( !items.contains( connectionName ) )
1574  {
1575  child = child.nextSiblingElement();
1576  continue;
1577  }
1578 
1579  // check for duplicates
1580  if ( keys.contains( connectionName ) && prompt )
1581  {
1582  int res = QMessageBox::warning( this,
1583  tr( "Loading Connections" ),
1584  tr( "Connection with name '%1' already exists. Overwrite?" )
1585  .arg( connectionName ),
1586  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1587 
1588  switch ( res )
1589  {
1590  case QMessageBox::Cancel:
1591  return;
1592  case QMessageBox::No:
1593  child = child.nextSiblingElement();
1594  continue;
1595  case QMessageBox::Yes:
1596  overwrite = true;
1597  break;
1598  case QMessageBox::YesToAll:
1599  prompt = false;
1600  overwrite = true;
1601  break;
1602  case QMessageBox::NoToAll:
1603  prompt = false;
1604  overwrite = false;
1605  break;
1606  }
1607  }
1608 
1609  if ( keys.contains( connectionName ) && !overwrite )
1610  {
1611  child = child.nextSiblingElement();
1612  continue;
1613  }
1614 
1615  settings.beginGroup( "qgis/connections-vector-tile/" + connectionName );
1616  settings.setValue( QStringLiteral( "url" ), child.attribute( QStringLiteral( "url" ) ) );
1617  settings.setValue( QStringLiteral( "zmin" ), child.attribute( QStringLiteral( "zmin" ) ) );
1618  settings.setValue( QStringLiteral( "zmax" ), child.attribute( QStringLiteral( "zmax" ) ) );
1619  settings.endGroup();
1620 
1621  child = child.nextSiblingElement();
1622  }
1623 }
1624 
1626 {
1627  listConnections->selectAll();
1628  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !listConnections->selectedItems().isEmpty() );
1629 }
1630 
1632 {
1633  listConnections->clearSelection();
1634  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
1635 }
QgsManageConnectionsDialog::XyzTiles
@ XyzTiles
Definition: qgsmanageconnectionsdialog.h:52
QgsManageConnectionsDialog::Mode
Mode
Definition: qgsmanageconnectionsdialog.h:36
QgsSettings::endGroup
void endGroup()
Resets the group to what it was before the corresponding beginGroup() call.
Definition: qgssettings.cpp:97
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
qgsmanageconnectionsdialog.h
QgsManageConnectionsDialog::MSSQL
@ MSSQL
Definition: qgsmanageconnectionsdialog.h:47
QgsSettings
Definition: qgssettings.h:61
QgsManageConnectionsDialog::QgsManageConnectionsDialog
QgsManageConnectionsDialog(QWidget *parent=nullptr, Mode mode=Export, Type type=WMS, const QString &fileName=QString())
Constructor for QgsManageConnectionsDialog.
Definition: qgsmanageconnectionsdialog.cpp:27
QgsManageConnectionsDialog::Oracle
@ Oracle
Definition: qgsmanageconnectionsdialog.h:50
QgsManageConnectionsDialog::clearSelection
void clearSelection()
Definition: qgsmanageconnectionsdialog.cpp:1631
QgsManageConnectionsDialog::Import
@ Import
Definition: qgsmanageconnectionsdialog.h:39
QgsManageConnectionsDialog::PostGIS
@ PostGIS
Definition: qgsmanageconnectionsdialog.h:45
QgsManageConnectionsDialog::VectorTile
@ VectorTile
Definition: qgsmanageconnectionsdialog.h:55
QgsManageConnectionsDialog::selectionChanged
void selectionChanged()
Definition: qgsmanageconnectionsdialog.cpp:70
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:289
QgsManageConnectionsDialog::Export
@ Export
Definition: qgsmanageconnectionsdialog.h:38
QgsManageConnectionsDialog::WCS
@ WCS
Definition: qgsmanageconnectionsdialog.h:49
QgsManageConnectionsDialog::ArcgisFeatureServer
@ ArcgisFeatureServer
Definition: qgsmanageconnectionsdialog.h:54
QgsSettings::beginGroup
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:87
QgsManageConnectionsDialog::Type
Type
Definition: qgsmanageconnectionsdialog.h:42
QgsManageConnectionsDialog::WMS
@ WMS
Definition: qgsmanageconnectionsdialog.h:44
QgsManageConnectionsDialog::selectAll
void selectAll()
Definition: qgsmanageconnectionsdialog.cpp:1625
QgsManageConnectionsDialog::ArcgisMapServer
@ ArcgisMapServer
Definition: qgsmanageconnectionsdialog.h:53
qgssettings.h
QgsManageConnectionsDialog::doExportImport
void doExportImport()
Definition: qgsmanageconnectionsdialog.cpp:75
QgsManageConnectionsDialog::DB2
@ DB2
Definition: qgsmanageconnectionsdialog.h:48
QgsManageConnectionsDialog::WFS
@ WFS
Definition: qgsmanageconnectionsdialog.h:46
QgsManageConnectionsDialog::GeoNode
@ GeoNode
Definition: qgsmanageconnectionsdialog.h:51
QgsSettings::childGroups
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
Definition: qgssettings.cpp:144