QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 HANA:
134  doc = saveHanaConnections( items );
135  break;
136  case GeoNode:
137  doc = saveGeonodeConnections( items );
138  break;
139  case XyzTiles:
140  doc = saveXyzTilesConnections( items );
141  break;
142  case ArcgisMapServer:
143  doc = saveArcgisConnections( items, QStringLiteral( "ARCGISMAPSERVER" ) );
144  break;
145  case ArcgisFeatureServer:
146  doc = saveArcgisConnections( items, QStringLiteral( "ARCGISFEATURESERVER" ) );
147  break;
148  case VectorTile:
149  doc = saveVectorTileConnections( items );
150  break;
151  }
152 
153  QFile file( mFileName );
154  if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
155  {
156  QMessageBox::warning( this, tr( "Saving Connections" ),
157  tr( "Cannot write file %1:\n%2." )
158  .arg( mFileName,
159  file.errorString() ) );
160  return;
161  }
162 
163  QTextStream out( &file );
164  doc.save( out, 4 );
165  }
166  else // import connections
167  {
168  QFile file( mFileName );
169  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
170  {
171  QMessageBox::warning( this, tr( "Loading Connections" ),
172  tr( "Cannot read file %1:\n%2." )
173  .arg( mFileName,
174  file.errorString() ) );
175  return;
176  }
177 
178  QDomDocument doc;
179  QString errorStr;
180  int errorLine;
181  int errorColumn;
182 
183  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
184  {
185  QMessageBox::warning( this, tr( "Loading Connections" ),
186  tr( "Parse error at line %1, column %2:\n%3" )
187  .arg( errorLine )
188  .arg( errorColumn )
189  .arg( errorStr ) );
190  return;
191  }
192 
193  switch ( mConnectionType )
194  {
195  case WMS:
196  loadOWSConnections( doc, items, QStringLiteral( "WMS" ) );
197  break;
198  case WFS:
199  loadWfsConnections( doc, items );
200  break;
201  case PostGIS:
202  loadPgConnections( doc, items );
203  break;
204  case MSSQL:
205  loadMssqlConnections( doc, items );
206  break;
207  case WCS:
208  loadOWSConnections( doc, items, QStringLiteral( "WCS" ) );
209  break;
210  case Oracle:
211  loadOracleConnections( doc, items );
212  break;
213  case DB2:
214  loadDb2Connections( doc, items );
215  break;
216  case HANA:
217  loadHanaConnections( doc, items );
218  break;
219  case GeoNode:
220  loadGeonodeConnections( doc, items );
221  break;
222  case XyzTiles:
223  loadXyzTilesConnections( doc, items );
224  break;
225  case ArcgisMapServer:
226  loadArcgisConnections( doc, items, QStringLiteral( "ARCGISMAPSERVER" ) );
227  break;
228  case ArcgisFeatureServer:
229  loadArcgisConnections( doc, items, QStringLiteral( "ARCGISFEATURESERVER" ) );
230  break;
231  case VectorTile:
232  loadVectorTileConnections( doc, items );
233  break;
234  }
235  // clear connections list and close window
236  listConnections->clear();
237  accept();
238  }
239 
240  mFileName.clear();
241 }
242 
243 bool QgsManageConnectionsDialog::populateConnections()
244 {
245  // Export mode. Populate connections list from settings
246  if ( mDialogMode == Export )
247  {
248  QgsSettings settings;
249  switch ( mConnectionType )
250  {
251  case WMS:
252  settings.beginGroup( QStringLiteral( "/qgis/connections-wms" ) );
253  break;
254  case WFS:
255  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
256  break;
257  case WCS:
258  settings.beginGroup( QStringLiteral( "/qgis/connections-wcs" ) );
259  break;
260  case PostGIS:
261  settings.beginGroup( QStringLiteral( "/PostgreSQL/connections" ) );
262  break;
263  case MSSQL:
264  settings.beginGroup( QStringLiteral( "/MSSQL/connections" ) );
265  break;
266  case Oracle:
267  settings.beginGroup( QStringLiteral( "/Oracle/connections" ) );
268  break;
269  case DB2:
270  settings.beginGroup( QStringLiteral( "/DB2/connections" ) );
271  break;
272  case HANA:
273  settings.beginGroup( QStringLiteral( "/HANA/connections" ) );
274  break;
275  case GeoNode:
276  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
277  break;
278  case XyzTiles:
279  settings.beginGroup( QStringLiteral( "/qgis/connections-xyz" ) );
280  break;
281  case ArcgisMapServer:
282  settings.beginGroup( QStringLiteral( "/qgis/connections-arcgismapserver" ) );
283  break;
284  case ArcgisFeatureServer:
285  settings.beginGroup( QStringLiteral( "/qgis/connections-arcgisfeatureserver" ) );
286  break;
287  case VectorTile:
288  settings.beginGroup( QStringLiteral( "/qgis/connections-vector-tile" ) );
289  break;
290  }
291  QStringList keys = settings.childGroups();
292  QStringList::Iterator it = keys.begin();
293  while ( it != keys.end() )
294  {
295  QListWidgetItem *item = new QListWidgetItem();
296  item->setText( *it );
297  listConnections->addItem( item );
298  ++it;
299  }
300  settings.endGroup();
301  }
302  // Import mode. Populate connections list from file
303  else
304  {
305  QFile file( mFileName );
306  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
307  {
308  QMessageBox::warning( this, tr( "Loading Connections" ),
309  tr( "Cannot read file %1:\n%2." )
310  .arg( mFileName,
311  file.errorString() ) );
312  return false;
313  }
314 
315  QDomDocument doc;
316  QString errorStr;
317  int errorLine;
318  int errorColumn;
319 
320  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
321  {
322  QMessageBox::warning( this, tr( "Loading Connections" ),
323  tr( "Parse error at line %1, column %2:\n%3" )
324  .arg( errorLine )
325  .arg( errorColumn )
326  .arg( errorStr ) );
327  return false;
328  }
329 
330  QDomElement root = doc.documentElement();
331  switch ( mConnectionType )
332  {
333  case WMS:
334  if ( root.tagName() != QLatin1String( "qgsWMSConnections" ) )
335  {
336  QMessageBox::information( this, tr( "Loading Connections" ),
337  tr( "The file is not a WMS connections exchange file." ) );
338  return false;
339  }
340  break;
341 
342  case WFS:
343  if ( root.tagName() != QLatin1String( "qgsWFSConnections" ) )
344  {
345  QMessageBox::information( this, tr( "Loading Connections" ),
346  tr( "The file is not a WFS connections exchange file." ) );
347  return false;
348  }
349  break;
350 
351  case WCS:
352  if ( root.tagName() != QLatin1String( "qgsWCSConnections" ) )
353  {
354  QMessageBox::information( this, tr( "Loading Connections" ),
355  tr( "The file is not a WCS connections exchange file." ) );
356  return false;
357  }
358  break;
359 
360  case PostGIS:
361  if ( root.tagName() != QLatin1String( "qgsPgConnections" ) )
362  {
363  QMessageBox::information( this, tr( "Loading Connections" ),
364  tr( "The file is not a PostGIS connections exchange file." ) );
365  return false;
366  }
367  break;
368 
369  case MSSQL:
370  if ( root.tagName() != QLatin1String( "qgsMssqlConnections" ) )
371  {
372  QMessageBox::information( this, tr( "Loading Connections" ),
373  tr( "The file is not a MSSQL connections exchange file." ) );
374  return false;
375  }
376  break;
377  case Oracle:
378  if ( root.tagName() != QLatin1String( "qgsOracleConnections" ) )
379  {
380  QMessageBox::information( this, tr( "Loading Connections" ),
381  tr( "The file is not an Oracle connections exchange file." ) );
382  return false;
383  }
384  break;
385  case DB2:
386  if ( root.tagName() != QLatin1String( "qgsDb2Connections" ) )
387  {
388  QMessageBox::information( this, tr( "Loading Connections" ),
389  tr( "The file is not a DB2 connections exchange file." ) );
390  return false;
391  }
392  break;
393  case HANA:
394  if ( root.tagName() != QLatin1String( "qgsHanaConnections" ) )
395  {
396  QMessageBox::warning( this, tr( "Loading Connections" ),
397  tr( "The file is not a HANA connections exchange file." ) );
398  return false;
399  }
400  break;
401  case GeoNode:
402  if ( root.tagName() != QLatin1String( "qgsGeoNodeConnections" ) )
403  {
404  QMessageBox::information( this, tr( "Loading Connections" ),
405  tr( "The file is not a GeoNode connections exchange file." ) );
406  return false;
407  }
408  break;
409  case XyzTiles:
410  if ( root.tagName() != QLatin1String( "qgsXYZTilesConnections" ) )
411  {
412  QMessageBox::information( this, tr( "Loading Connections" ),
413  tr( "The file is not a XYZ Tiles connections exchange file." ) );
414  return false;
415  }
416  break;
417  case ArcgisMapServer:
418  if ( root.tagName() != QLatin1String( "qgsARCGISMAPSERVERConnections" ) )
419  {
420  QMessageBox::information( this, tr( "Loading Connections" ),
421  tr( "The file is not a ArcGIS Map Service connections exchange file." ) );
422  return false;
423  }
424  break;
425  case ArcgisFeatureServer:
426  if ( root.tagName() != QLatin1String( "qgsARCGISFEATURESERVERConnections" ) )
427  {
428  QMessageBox::information( this, tr( "Loading Connections" ),
429  tr( "The file is not a ArcGIS Feature Service connections exchange file." ) );
430  return false;
431  }
432  break;
433  case VectorTile:
434  if ( root.tagName() != QLatin1String( "qgsVectorTileConnections" ) )
435  {
436  QMessageBox::information( this, tr( "Loading Connections" ),
437  tr( "The file is not a Vector Tile connections exchange file." ) );
438  return false;
439  }
440  break;
441  }
442 
443  QDomElement child = root.firstChildElement();
444  while ( !child.isNull() )
445  {
446  QListWidgetItem *item = new QListWidgetItem();
447  item->setText( child.attribute( QStringLiteral( "name" ) ) );
448  listConnections->addItem( item );
449  child = child.nextSiblingElement();
450  }
451  }
452  return true;
453 }
454 
455 QDomDocument QgsManageConnectionsDialog::saveOWSConnections( const QStringList &connections, const QString &service )
456 {
457  QDomDocument doc( QStringLiteral( "connections" ) );
458  QDomElement root = doc.createElement( "qgs" + service.toUpper() + "Connections" );
459  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
460  doc.appendChild( root );
461 
462  QgsSettings settings;
463  QString path;
464  for ( int i = 0; i < connections.count(); ++i )
465  {
466  path = "/qgis/connections-" + service.toLower() + '/';
467  QDomElement el = doc.createElement( service.toLower() );
468  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
469  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url" ).toString() );
470 
471  if ( service == QLatin1String( "WMS" ) )
472  {
473  el.setAttribute( QStringLiteral( "ignoreGetMapURI" ), settings.value( path + connections[i] + "/ignoreGetMapURI", false ).toBool() ? "true" : "false" );
474  el.setAttribute( QStringLiteral( "ignoreGetFeatureInfoURI" ), settings.value( path + connections[i] + "/ignoreGetFeatureInfoURI", false ).toBool() ? "true" : "false" );
475  el.setAttribute( QStringLiteral( "ignoreAxisOrientation" ), settings.value( path + connections[i] + "/ignoreAxisOrientation", false ).toBool() ? "true" : "false" );
476  el.setAttribute( QStringLiteral( "invertAxisOrientation" ), settings.value( path + connections[i] + "/invertAxisOrientation", false ).toBool() ? "true" : "false" );
477  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + connections[ i ] + "/referer" ).toString() );
478  el.setAttribute( QStringLiteral( "smoothPixmapTransform" ), settings.value( path + connections[i] + "/smoothPixmapTransform", false ).toBool() ? "true" : "false" );
479  el.setAttribute( QStringLiteral( "dpiMode" ), settings.value( path + connections[i] + "/dpiMode", "7" ).toInt() );
480  }
481 
482  path = "/qgis/" + service.toUpper() + '/';
483  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username" ).toString() );
484  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password" ).toString() );
485  root.appendChild( el );
486  }
487 
488  return doc;
489 }
490 
491 QDomDocument QgsManageConnectionsDialog::saveWfsConnections( const QStringList &connections )
492 {
493  QDomDocument doc( QStringLiteral( "connections" ) );
494  QDomElement root = doc.createElement( QStringLiteral( "qgsWFSConnections" ) );
495  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.1" ) );
496  doc.appendChild( root );
497 
498  QgsSettings settings;
499  QString path;
500  for ( int i = 0; i < connections.count(); ++i )
501  {
502  path = QStringLiteral( "/qgis/connections-wfs/" );
503  QDomElement el = doc.createElement( QStringLiteral( "wfs" ) );
504  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
505  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url" ).toString() );
506 
507  el.setAttribute( QStringLiteral( "version" ), settings.value( path + connections[ i ] + "/version" ).toString() );
508  el.setAttribute( QStringLiteral( "maxnumfeatures" ), settings.value( path + connections[ i ] + "/maxnumfeatures" ).toString() );
509  el.setAttribute( QStringLiteral( "pagesize" ), settings.value( path + connections[ i ] + "/pagesize" ).toString() );
510  el.setAttribute( QStringLiteral( "pagingenabled" ), settings.value( path + connections[ i ] + "/pagingenabled", false ).toString() );
511  el.setAttribute( QStringLiteral( "ignoreAxisOrientation" ), settings.value( path + connections[ i ] + "/ignoreAxisOrientation", false ).toString() );
512  el.setAttribute( QStringLiteral( "invertAxisOrientation" ), settings.value( path + connections[ i ] + "/invertAxisOrientation", false ).toString() );
513 
514  path = QStringLiteral( "/qgis/WFS/" );
515  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username" ).toString() );
516  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password" ).toString() );
517  root.appendChild( el );
518  }
519 
520  return doc;
521 }
522 
523 QDomDocument QgsManageConnectionsDialog::savePgConnections( const QStringList &connections )
524 {
525  QDomDocument doc( QStringLiteral( "connections" ) );
526  QDomElement root = doc.createElement( QStringLiteral( "qgsPgConnections" ) );
527  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
528  doc.appendChild( root );
529 
530  QgsSettings settings;
531  QString path;
532  for ( int i = 0; i < connections.count(); ++i )
533  {
534  path = "/PostgreSQL/connections/" + connections[ i ];
535  QDomElement el = doc.createElement( QStringLiteral( "postgis" ) );
536  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
537  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host" ).toString() );
538  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port" ).toString() );
539  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database" ).toString() );
540  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service" ).toString() );
541  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
542  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
543 
544  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
545 
546  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
547  {
548  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
549  }
550 
551  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
552 
553  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
554  {
555  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
556  }
557 
558  root.appendChild( el );
559  }
560 
561  return doc;
562 }
563 
564 QDomDocument QgsManageConnectionsDialog::saveMssqlConnections( const QStringList &connections )
565 {
566  QDomDocument doc( QStringLiteral( "connections" ) );
567  QDomElement root = doc.createElement( QStringLiteral( "qgsMssqlConnections" ) );
568  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
569  doc.appendChild( root );
570 
571  QgsSettings settings;
572  QString path;
573  for ( int i = 0; i < connections.count(); ++i )
574  {
575  path = "/MSSQL/connections/" + connections[ i ];
576  QDomElement el = doc.createElement( QStringLiteral( "mssql" ) );
577  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
578  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host" ).toString() );
579  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port" ).toString() );
580  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database" ).toString() );
581  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service" ).toString() );
582  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
583  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
584 
585  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
586 
587  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
588  {
589  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
590  }
591 
592  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
593 
594  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
595  {
596  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
597  }
598 
599  root.appendChild( el );
600  }
601 
602  return doc;
603 }
604 
605 QDomDocument QgsManageConnectionsDialog::saveOracleConnections( const QStringList &connections )
606 {
607  QDomDocument doc( QStringLiteral( "connections" ) );
608  QDomElement root = doc.createElement( QStringLiteral( "qgsOracleConnections" ) );
609  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
610  doc.appendChild( root );
611 
612  QgsSettings settings;
613  QString path;
614  for ( int i = 0; i < connections.count(); ++i )
615  {
616  path = "/Oracle/connections/" + connections[ i ];
617  QDomElement el = doc.createElement( QStringLiteral( "oracle" ) );
618  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
619  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host" ).toString() );
620  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port" ).toString() );
621  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database" ).toString() );
622  el.setAttribute( QStringLiteral( "dboptions" ), settings.value( path + "/dboptions" ).toString() );
623  el.setAttribute( QStringLiteral( "dbworkspace" ), settings.value( path + "/dbworkspace" ).toString() );
624  el.setAttribute( QStringLiteral( "schema" ), settings.value( path + "/schema" ).toString() );
625  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
626  el.setAttribute( QStringLiteral( "userTablesOnly" ), settings.value( path + "/userTablesOnly", "0" ).toString() );
627  el.setAttribute( QStringLiteral( "geometryColumnsOnly" ), settings.value( path + "/geometryColumnsOnly", "0" ).toString() );
628  el.setAttribute( QStringLiteral( "allowGeometrylessTables" ), settings.value( path + "/allowGeometrylessTables", "0" ).toString() );
629 
630  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
631 
632  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
633  {
634  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
635  }
636 
637  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
638 
639  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
640  {
641  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
642  }
643 
644  root.appendChild( el );
645  }
646 
647  return doc;
648 }
649 
650 QDomDocument QgsManageConnectionsDialog::saveDb2Connections( const QStringList &connections )
651 {
652  QDomDocument doc( QStringLiteral( "connections" ) );
653  QDomElement root = doc.createElement( QStringLiteral( "qgsDb2Connections" ) );
654  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
655  doc.appendChild( root );
656 
657  QgsSettings settings;
658  QString path;
659  for ( int i = 0; i < connections.count(); ++i )
660  {
661  path = "/DB2/connections/" + connections[ i ];
662  QDomElement el = doc.createElement( QStringLiteral( "db2" ) );
663  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
664  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host" ).toString() );
665  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port" ).toString() );
666  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database" ).toString() );
667  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service" ).toString() );
668  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
669  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
670 
671  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
672 
673  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
674  {
675  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
676  }
677 
678  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
679 
680  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
681  {
682  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
683  }
684 
685  root.appendChild( el );
686  }
687 
688  return doc;
689 }
690 
691 QDomDocument QgsManageConnectionsDialog::saveHanaConnections( const QStringList &connections )
692 {
693  QDomDocument doc( QStringLiteral( "connections" ) );
694  QDomElement root = doc.createElement( QStringLiteral( "qgsHanaConnections" ) );
695  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
696  doc.appendChild( root );
697 
698  QgsSettings settings;
699  QString path;
700  for ( int i = 0; i < connections.count(); ++i )
701  {
702  path = "/HANA/connections/" + connections[i];
703  QDomElement el = doc.createElement( QStringLiteral( "hana" ) );
704  el.setAttribute( QStringLiteral( "name" ), connections[i] );
705  el.setAttribute( QStringLiteral( "driver" ), settings.value( path + "/driver", QString() ).toString() );
706  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host", QString() ).toString() );
707  el.setAttribute( QStringLiteral( "identifierType" ), settings.value( path + "/identifierType", QString() ).toString() );
708  el.setAttribute( QStringLiteral( "identifier" ), settings.value( path + "/identifier", QString() ).toString() );
709  el.setAttribute( QStringLiteral( "multitenant" ), settings.value( path + "/multitenant", QString() ).toString() );
710  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database", QString() ).toString() );
711  el.setAttribute( QStringLiteral( "schema" ), settings.value( path + "/schema", QString() ).toString() );
712  el.setAttribute( QStringLiteral( "userTablesOnly" ), settings.value( path + "/userTablesOnly", QStringLiteral( "0" ) ).toString() );
713  el.setAttribute( QStringLiteral( "allowGeometrylessTables" ), settings.value( path + "/allowGeometrylessTables", QStringLiteral( "0" ) ).toString() );
714 
715  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", QStringLiteral( "false" ) ).toString() );
716  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
717  {
718  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", QString() ).toString() );
719  }
720 
721  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", QStringLiteral( "false" ) ).toString() );
722  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
723  {
724  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", QString() ).toString() );
725  }
726 
727  el.setAttribute( QStringLiteral( "sslEnabled" ), settings.value( path + "/sslEnabled", QStringLiteral( "false" ) ).toString() );
728  el.setAttribute( QStringLiteral( "sslCryptoProvider" ), settings.value( path + "/sslCryptoProvider", QStringLiteral( "openssl" ) ).toString() );
729  el.setAttribute( QStringLiteral( "sslKeyStore" ), settings.value( path + "/sslKeyStore", QString() ).toString() );
730  el.setAttribute( QStringLiteral( "sslTrustStore" ), settings.value( path + "/sslTrustStore", QString() ).toString() );
731  el.setAttribute( QStringLiteral( "sslValidateCertificate" ), settings.value( path + "/sslValidateCertificate", QStringLiteral( "false" ) ).toString() );
732  el.setAttribute( QStringLiteral( "sslHostNameInCertificate" ), settings.value( path + "/sslHostNameInCertificate", QString() ).toString() );
733 
734  root.appendChild( el );
735  }
736 
737  return doc;
738 }
739 
740 QDomDocument QgsManageConnectionsDialog::saveGeonodeConnections( const QStringList &connections )
741 {
742  QDomDocument doc( QStringLiteral( "connections" ) );
743  QDomElement root = doc.createElement( QStringLiteral( "qgsGeoNodeConnections" ) );
744  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
745  doc.appendChild( root );
746 
747  QgsSettings settings;
748  QString path;
749  for ( int i = 0; i < connections.count(); ++i )
750  {
751  path = QStringLiteral( "/qgis/connections-geonode/" );
752  QDomElement el = doc.createElement( QStringLiteral( "geonode" ) );
753  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
754  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url" ).toString() );
755 
756  path = QStringLiteral( "/qgis/GeoNode/" );
757  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username" ).toString() );
758  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password" ).toString() );
759  root.appendChild( el );
760  }
761 
762  return doc;
763 }
764 
765 QDomDocument QgsManageConnectionsDialog::saveXyzTilesConnections( const QStringList &connections )
766 {
767  QDomDocument doc( QStringLiteral( "connections" ) );
768  QDomElement root = doc.createElement( QStringLiteral( "qgsXYZTilesConnections" ) );
769  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
770  doc.appendChild( root );
771 
772  QgsSettings settings;
773  QString path;
774  for ( int i = 0; i < connections.count(); ++i )
775  {
776  path = "qgis/connections-xyz/" + connections[ i ];
777  QDomElement el = doc.createElement( QStringLiteral( "xyztiles" ) );
778 
779  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
780  el.setAttribute( QStringLiteral( "url" ), settings.value( path + "/url" ).toString() );
781  el.setAttribute( QStringLiteral( "zmin" ), settings.value( path + "/zmin", -1 ).toInt() );
782  el.setAttribute( QStringLiteral( "zmax" ), settings.value( path + "/zmax", -1 ).toInt() );
783  el.setAttribute( QStringLiteral( "authcfg" ), settings.value( path + "/authcfg" ).toString() );
784  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
785  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
786  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + "/referer" ).toString() );
787  el.setAttribute( QStringLiteral( "tilePixelRatio" ), settings.value( path + "/tilePixelRatio", 0 ).toDouble() );
788 
789  root.appendChild( el );
790  }
791 
792  return doc;
793 }
794 
795 QDomDocument QgsManageConnectionsDialog::saveArcgisConnections( const QStringList &connections, const QString &service )
796 {
797  QDomDocument doc( QStringLiteral( "connections" ) );
798  QDomElement root = doc.createElement( "qgs" + service.toUpper() + "Connections" );
799  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
800  doc.appendChild( root );
801 
802  QgsSettings settings;
803  QString path;
804  for ( int i = 0; i < connections.count(); ++i )
805  {
806  path = "/qgis/connections-" + service.toLower() + '/';
807  QDomElement el = doc.createElement( service.toLower() );
808  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
809  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url" ).toString() );
810  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + connections[ i ] + "/referer" ).toString() );
811 
812  path = "/qgis/" + service.toUpper() + '/';
813  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username" ).toString() );
814  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password" ).toString() );
815  el.setAttribute( QStringLiteral( "authcfg" ), settings.value( path + connections[ i ] + "/authcfg" ).toString() );
816  root.appendChild( el );
817  }
818 
819  return doc;
820 }
821 
822 QDomDocument QgsManageConnectionsDialog::saveVectorTileConnections( const QStringList &connections )
823 {
824  QDomDocument doc( QStringLiteral( "connections" ) );
825  QDomElement root = doc.createElement( QStringLiteral( "qgsVectorTileConnections" ) );
826  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
827  doc.appendChild( root );
828 
829  QgsSettings settings;
830  QString path;
831  for ( int i = 0; i < connections.count(); ++i )
832  {
833  path = "qgis/connections-vector-tile/" + connections[ i ];
834  QDomElement el = doc.createElement( QStringLiteral( "vectortile" ) );
835 
836  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
837  el.setAttribute( QStringLiteral( "url" ), settings.value( path + "/url" ).toString() );
838  el.setAttribute( QStringLiteral( "zmin" ), settings.value( path + "/zmin", -1 ).toInt() );
839  el.setAttribute( QStringLiteral( "zmax" ), settings.value( path + "/zmax", -1 ).toInt() );
840  el.setAttribute( QStringLiteral( "serviceType" ), settings.value( path + "/serviceType", -1 ).toInt() );
841  el.setAttribute( QStringLiteral( "authcfg" ), settings.value( path + "/authcfg" ).toString() );
842  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
843  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
844  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + "/referer" ).toString() );
845  el.setAttribute( QStringLiteral( "styleUrl" ), settings.value( path + "/styleUrl" ).toString() );
846 
847  root.appendChild( el );
848  }
849 
850  return doc;
851 }
852 
853 void QgsManageConnectionsDialog::loadOWSConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
854 {
855  QDomElement root = doc.documentElement();
856  if ( root.tagName() != "qgs" + service.toUpper() + "Connections" )
857  {
858  QMessageBox::information( this, tr( "Loading Connections" ),
859  tr( "The file is not a %1 connections exchange file." ).arg( service ) );
860  return;
861  }
862 
863  QString connectionName;
864  QgsSettings settings;
865  settings.beginGroup( "/qgis/connections-" + service.toLower() );
866  QStringList keys = settings.childGroups();
867  settings.endGroup();
868  QDomElement child = root.firstChildElement();
869  bool prompt = true;
870  bool overwrite = true;
871 
872  while ( !child.isNull() )
873  {
874  connectionName = child.attribute( QStringLiteral( "name" ) );
875  if ( !items.contains( connectionName ) )
876  {
877  child = child.nextSiblingElement();
878  continue;
879  }
880 
881  // check for duplicates
882  if ( keys.contains( connectionName ) && prompt )
883  {
884  int res = QMessageBox::warning( this,
885  tr( "Loading Connections" ),
886  tr( "Connection with name '%1' already exists. Overwrite?" )
887  .arg( connectionName ),
888  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
889 
890  switch ( res )
891  {
892  case QMessageBox::Cancel:
893  return;
894  case QMessageBox::No:
895  child = child.nextSiblingElement();
896  continue;
897  case QMessageBox::Yes:
898  overwrite = true;
899  break;
900  case QMessageBox::YesToAll:
901  prompt = false;
902  overwrite = true;
903  break;
904  case QMessageBox::NoToAll:
905  prompt = false;
906  overwrite = false;
907  break;
908  }
909  }
910 
911  if ( keys.contains( connectionName ) )
912  {
913  if ( !overwrite )
914  {
915  child = child.nextSiblingElement();
916  continue;
917  }
918  }
919  else
920  {
921  keys << connectionName;
922  }
923 
924  // no dups detected or overwrite is allowed
925  settings.beginGroup( "/qgis/connections-" + service.toLower() );
926  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
927  settings.setValue( QString( '/' + connectionName + "/ignoreGetMapURI" ), child.attribute( QStringLiteral( "ignoreGetMapURI" ) ) == QLatin1String( "true" ) );
928  settings.setValue( QString( '/' + connectionName + "/ignoreGetFeatureInfoURI" ), child.attribute( QStringLiteral( "ignoreGetFeatureInfoURI" ) ) == QLatin1String( "true" ) );
929  settings.setValue( QString( '/' + connectionName + "/ignoreAxisOrientation" ), child.attribute( QStringLiteral( "ignoreAxisOrientation" ) ) == QLatin1String( "true" ) );
930  settings.setValue( QString( '/' + connectionName + "/invertAxisOrientation" ), child.attribute( QStringLiteral( "invertAxisOrientation" ) ) == QLatin1String( "true" ) );
931  settings.setValue( QString( '/' + connectionName + "/referer" ), child.attribute( QStringLiteral( "referer" ) ) );
932  settings.setValue( QString( '/' + connectionName + "/smoothPixmapTransform" ), child.attribute( QStringLiteral( "smoothPixmapTransform" ) ) == QLatin1String( "true" ) );
933  settings.setValue( QString( '/' + connectionName + "/dpiMode" ), child.attribute( QStringLiteral( "dpiMode" ), QStringLiteral( "7" ) ).toInt() );
934  settings.endGroup();
935 
936  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
937  {
938  settings.beginGroup( "/qgis/" + service.toUpper() + '/' + connectionName );
939  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
940  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
941  settings.endGroup();
942  }
943  child = child.nextSiblingElement();
944  }
945 }
946 
947 void QgsManageConnectionsDialog::loadWfsConnections( const QDomDocument &doc, const QStringList &items )
948 {
949  QDomElement root = doc.documentElement();
950  if ( root.tagName() != QLatin1String( "qgsWFSConnections" ) )
951  {
952  QMessageBox::information( this, tr( "Loading Connections" ),
953  tr( "The file is not a WFS connections exchange file." ) );
954  return;
955  }
956 
957  QString connectionName;
958  QgsSettings settings;
959  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
960  QStringList keys = settings.childGroups();
961  settings.endGroup();
962  QDomElement child = root.firstChildElement();
963  bool prompt = true;
964  bool overwrite = true;
965 
966  while ( !child.isNull() )
967  {
968  connectionName = child.attribute( QStringLiteral( "name" ) );
969  if ( !items.contains( connectionName ) )
970  {
971  child = child.nextSiblingElement();
972  continue;
973  }
974 
975  // check for duplicates
976  if ( keys.contains( connectionName ) && prompt )
977  {
978  int res = QMessageBox::warning( this,
979  tr( "Loading Connections" ),
980  tr( "Connection with name '%1' already exists. Overwrite?" )
981  .arg( connectionName ),
982  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
983 
984  switch ( res )
985  {
986  case QMessageBox::Cancel:
987  return;
988  case QMessageBox::No:
989  child = child.nextSiblingElement();
990  continue;
991  case QMessageBox::Yes:
992  overwrite = true;
993  break;
994  case QMessageBox::YesToAll:
995  prompt = false;
996  overwrite = true;
997  break;
998  case QMessageBox::NoToAll:
999  prompt = false;
1000  overwrite = false;
1001  break;
1002  }
1003  }
1004 
1005  if ( keys.contains( connectionName ) )
1006  {
1007  if ( !overwrite )
1008  {
1009  child = child.nextSiblingElement();
1010  continue;
1011  }
1012  }
1013  else
1014  {
1015  keys << connectionName;
1016  }
1017 
1018  // no dups detected or overwrite is allowed
1019  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
1020  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
1021 
1022  settings.setValue( QString( '/' + connectionName + "/version" ), child.attribute( QStringLiteral( "version" ) ) );
1023  settings.setValue( QString( '/' + connectionName + "/maxnumfeatures" ), child.attribute( QStringLiteral( "maxnumfeatures" ) ) );
1024  settings.setValue( QString( '/' + connectionName + "/pagesize" ), child.attribute( QStringLiteral( "pagesize" ) ) );
1025  settings.setValue( QString( '/' + connectionName + "/pagingenabled" ), child.attribute( QStringLiteral( "pagingenabled" ) ) );
1026  settings.setValue( QString( '/' + connectionName + "/ignoreAxisOrientation" ), child.attribute( QStringLiteral( "ignoreAxisOrientation" ) ) );
1027  settings.setValue( QString( '/' + connectionName + "/invertAxisOrientation" ), child.attribute( QStringLiteral( "invertAxisOrientation" ) ) );
1028  settings.endGroup();
1029 
1030  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
1031  {
1032  settings.beginGroup( "/qgis/WFS/" + connectionName );
1033  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1034  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1035  settings.endGroup();
1036  }
1037  child = child.nextSiblingElement();
1038  }
1039 }
1040 
1041 void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, const QStringList &items )
1042 {
1043  QDomElement root = doc.documentElement();
1044  if ( root.tagName() != QLatin1String( "qgsPgConnections" ) )
1045  {
1046  QMessageBox::information( this,
1047  tr( "Loading Connections" ),
1048  tr( "The file is not a PostGIS connections exchange file." ) );
1049  return;
1050  }
1051 
1052  QString connectionName;
1053  QgsSettings settings;
1054  settings.beginGroup( QStringLiteral( "/PostgreSQL/connections" ) );
1055  QStringList keys = settings.childGroups();
1056  settings.endGroup();
1057  QDomElement child = root.firstChildElement();
1058  bool prompt = true;
1059  bool overwrite = true;
1060 
1061  while ( !child.isNull() )
1062  {
1063  connectionName = child.attribute( QStringLiteral( "name" ) );
1064  if ( !items.contains( connectionName ) )
1065  {
1066  child = child.nextSiblingElement();
1067  continue;
1068  }
1069 
1070  // check for duplicates
1071  if ( keys.contains( connectionName ) && prompt )
1072  {
1073  int res = QMessageBox::warning( this,
1074  tr( "Loading Connections" ),
1075  tr( "Connection with name '%1' already exists. Overwrite?" )
1076  .arg( connectionName ),
1077  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1078  switch ( res )
1079  {
1080  case QMessageBox::Cancel:
1081  return;
1082  case QMessageBox::No:
1083  child = child.nextSiblingElement();
1084  continue;
1085  case QMessageBox::Yes:
1086  overwrite = true;
1087  break;
1088  case QMessageBox::YesToAll:
1089  prompt = false;
1090  overwrite = true;
1091  break;
1092  case QMessageBox::NoToAll:
1093  prompt = false;
1094  overwrite = false;
1095  break;
1096  }
1097  }
1098 
1099  if ( keys.contains( connectionName ) )
1100  {
1101  if ( !overwrite )
1102  {
1103  child = child.nextSiblingElement();
1104  continue;
1105  }
1106  }
1107  else
1108  {
1109  keys << connectionName;
1110  }
1111 
1112  //no dups detected or overwrite is allowed
1113  settings.beginGroup( "/PostgreSQL/connections/" + connectionName );
1114 
1115  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1116  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1117  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1118  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1119  {
1120  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1121  }
1122  else
1123  {
1124  settings.setValue( QStringLiteral( "/service" ), "" );
1125  }
1126  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1127  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1128  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1129  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1130  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1131  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1132  settings.endGroup();
1133 
1134  child = child.nextSiblingElement();
1135  }
1136 }
1137 
1138 void QgsManageConnectionsDialog::loadMssqlConnections( const QDomDocument &doc, const QStringList &items )
1139 {
1140  QDomElement root = doc.documentElement();
1141  if ( root.tagName() != QLatin1String( "qgsMssqlConnections" ) )
1142  {
1143  QMessageBox::information( this,
1144  tr( "Loading Connections" ),
1145  tr( "The file is not a MSSQL connections exchange file." ) );
1146  return;
1147  }
1148 
1149  QString connectionName;
1150  QgsSettings settings;
1151  settings.beginGroup( QStringLiteral( "/MSSQL/connections" ) );
1152  QStringList keys = settings.childGroups();
1153  settings.endGroup();
1154  QDomElement child = root.firstChildElement();
1155  bool prompt = true;
1156  bool overwrite = true;
1157 
1158  while ( !child.isNull() )
1159  {
1160  connectionName = child.attribute( QStringLiteral( "name" ) );
1161  if ( !items.contains( connectionName ) )
1162  {
1163  child = child.nextSiblingElement();
1164  continue;
1165  }
1166 
1167  // check for duplicates
1168  if ( keys.contains( connectionName ) && prompt )
1169  {
1170  int res = QMessageBox::warning( this,
1171  tr( "Loading Connections" ),
1172  tr( "Connection with name '%1' already exists. Overwrite?" )
1173  .arg( connectionName ),
1174  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1175  switch ( res )
1176  {
1177  case QMessageBox::Cancel:
1178  return;
1179  case QMessageBox::No:
1180  child = child.nextSiblingElement();
1181  continue;
1182  case QMessageBox::Yes:
1183  overwrite = true;
1184  break;
1185  case QMessageBox::YesToAll:
1186  prompt = false;
1187  overwrite = true;
1188  break;
1189  case QMessageBox::NoToAll:
1190  prompt = false;
1191  overwrite = false;
1192  break;
1193  }
1194  }
1195 
1196  if ( keys.contains( connectionName ) )
1197  {
1198  if ( !overwrite )
1199  {
1200  child = child.nextSiblingElement();
1201  continue;
1202  }
1203  }
1204  else
1205  {
1206  keys << connectionName;
1207  }
1208 
1209  //no dups detected or overwrite is allowed
1210  settings.beginGroup( "/MSSQL/connections/" + connectionName );
1211 
1212  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1213  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1214  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1215  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1216  {
1217  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1218  }
1219  else
1220  {
1221  settings.setValue( QStringLiteral( "/service" ), "" );
1222  }
1223  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1224  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1225  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1226  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1227  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1228  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1229  settings.endGroup();
1230 
1231  child = child.nextSiblingElement();
1232  }
1233 }
1234 
1235 void QgsManageConnectionsDialog::loadOracleConnections( const QDomDocument &doc, const QStringList &items )
1236 {
1237  QDomElement root = doc.documentElement();
1238  if ( root.tagName() != QLatin1String( "qgsOracleConnections" ) )
1239  {
1240  QMessageBox::information( this,
1241  tr( "Loading Connections" ),
1242  tr( "The file is not an Oracle connections exchange file." ) );
1243  return;
1244  }
1245 
1246  QString connectionName;
1247  QgsSettings settings;
1248  settings.beginGroup( QStringLiteral( "/Oracle/connections" ) );
1249  QStringList keys = settings.childGroups();
1250  settings.endGroup();
1251  QDomElement child = root.firstChildElement();
1252  bool prompt = true;
1253  bool overwrite = true;
1254 
1255  while ( !child.isNull() )
1256  {
1257  connectionName = child.attribute( QStringLiteral( "name" ) );
1258  if ( !items.contains( connectionName ) )
1259  {
1260  child = child.nextSiblingElement();
1261  continue;
1262  }
1263 
1264  // check for duplicates
1265  if ( keys.contains( connectionName ) && prompt )
1266  {
1267  int res = QMessageBox::warning( this,
1268  tr( "Loading Connections" ),
1269  tr( "Connection with name '%1' already exists. Overwrite?" )
1270  .arg( connectionName ),
1271  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1272  switch ( res )
1273  {
1274  case QMessageBox::Cancel:
1275  return;
1276  case QMessageBox::No:
1277  child = child.nextSiblingElement();
1278  continue;
1279  case QMessageBox::Yes:
1280  overwrite = true;
1281  break;
1282  case QMessageBox::YesToAll:
1283  prompt = false;
1284  overwrite = true;
1285  break;
1286  case QMessageBox::NoToAll:
1287  prompt = false;
1288  overwrite = false;
1289  break;
1290  }
1291  }
1292 
1293  if ( keys.contains( connectionName ) )
1294  {
1295  if ( !overwrite )
1296  {
1297  child = child.nextSiblingElement();
1298  continue;
1299  }
1300  }
1301  else
1302  {
1303  keys << connectionName;
1304  }
1305 
1306  //no dups detected or overwrite is allowed
1307  settings.beginGroup( "/Oracle/connections/" + connectionName );
1308 
1309  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1310  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1311  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1312  settings.setValue( QStringLiteral( "/dboptions" ), child.attribute( QStringLiteral( "dboptions" ) ) );
1313  settings.setValue( QStringLiteral( "/dbworkspace" ), child.attribute( QStringLiteral( "dbworkspace" ) ) );
1314  settings.setValue( QStringLiteral( "/schema" ), child.attribute( QStringLiteral( "schema" ) ) );
1315  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1316  settings.setValue( QStringLiteral( "/userTablesOnly" ), child.attribute( QStringLiteral( "userTablesOnly" ) ) );
1317  settings.setValue( QStringLiteral( "/geometryColumnsOnly" ), child.attribute( QStringLiteral( "geometryColumnsOnly" ) ) );
1318  settings.setValue( QStringLiteral( "/allowGeometrylessTables" ), child.attribute( QStringLiteral( "allowGeometrylessTables" ) ) );
1319  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1320  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1321  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1322  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1323  settings.endGroup();
1324 
1325  child = child.nextSiblingElement();
1326  }
1327 }
1328 
1329 void QgsManageConnectionsDialog::loadDb2Connections( const QDomDocument &doc, const QStringList &items )
1330 {
1331  QDomElement root = doc.documentElement();
1332  if ( root.tagName() != QLatin1String( "qgsDb2Connections" ) )
1333  {
1334  QMessageBox::information( this,
1335  tr( "Loading Connections" ),
1336  tr( "The file is not a DB2 connections exchange file." ) );
1337  return;
1338  }
1339 
1340  QString connectionName;
1341  QgsSettings settings;
1342  settings.beginGroup( QStringLiteral( "/DB2/connections" ) );
1343  QStringList keys = settings.childGroups();
1344  settings.endGroup();
1345  QDomElement child = root.firstChildElement();
1346  bool prompt = true;
1347  bool overwrite = true;
1348 
1349  while ( !child.isNull() )
1350  {
1351  connectionName = child.attribute( QStringLiteral( "name" ) );
1352  if ( !items.contains( connectionName ) )
1353  {
1354  child = child.nextSiblingElement();
1355  continue;
1356  }
1357 
1358  // check for duplicates
1359  if ( keys.contains( connectionName ) && prompt )
1360  {
1361  int res = QMessageBox::warning( this,
1362  tr( "Loading Connections" ),
1363  tr( "Connection with name '%1' already exists. Overwrite?" )
1364  .arg( connectionName ),
1365  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1366  switch ( res )
1367  {
1368  case QMessageBox::Cancel:
1369  return;
1370  case QMessageBox::No:
1371  child = child.nextSiblingElement();
1372  continue;
1373  case QMessageBox::Yes:
1374  overwrite = true;
1375  break;
1376  case QMessageBox::YesToAll:
1377  prompt = false;
1378  overwrite = true;
1379  break;
1380  case QMessageBox::NoToAll:
1381  prompt = false;
1382  overwrite = false;
1383  break;
1384  }
1385  }
1386 
1387  if ( keys.contains( connectionName ) )
1388  {
1389  if ( !overwrite )
1390  {
1391  child = child.nextSiblingElement();
1392  continue;
1393  }
1394  }
1395  else
1396  {
1397  keys << connectionName;
1398  }
1399 
1400  //no dups detected or overwrite is allowed
1401  settings.beginGroup( "/DB2/connections/" + connectionName );
1402 
1403  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1404  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1405  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1406  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1407  {
1408  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1409  }
1410  else
1411  {
1412  settings.setValue( QStringLiteral( "/service" ), "" );
1413  }
1414  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1415  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1416  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1417  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1418  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1419  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1420  settings.endGroup();
1421 
1422  child = child.nextSiblingElement();
1423  }
1424 }
1425 
1426 void QgsManageConnectionsDialog::loadHanaConnections( const QDomDocument &doc, const QStringList &items )
1427 {
1428  QDomElement root = doc.documentElement();
1429  if ( root.tagName() != QLatin1String( "qgsHanaConnections" ) )
1430  {
1431  QMessageBox::warning( this,
1432  tr( "Loading Connections" ),
1433  tr( "The file is not a HANA connections exchange file." ) );
1434  return;
1435  }
1436 
1437  QDomAttr version = root.attributeNode( "version" );
1438  if ( version.value() != QLatin1String( "1.0" ) )
1439  {
1440  QMessageBox::warning( this,
1441  tr( "Loading Connections" ),
1442  tr( "The HANA connections exchange file version '%1' is not supported." ).arg( version.value() ) );
1443  return;
1444  }
1445 
1446  QgsSettings settings;
1447  settings.beginGroup( QStringLiteral( "/HANA/connections" ) );
1448  QStringList keys = settings.childGroups();
1449  settings.endGroup();
1450  QDomElement child = root.firstChildElement();
1451  bool prompt = true;
1452  bool overwrite = true;
1453 
1454  while ( !child.isNull() )
1455  {
1456  QString connectionName = child.attribute( QStringLiteral( "name" ) );
1457  if ( !items.contains( connectionName ) )
1458  {
1459  child = child.nextSiblingElement();
1460  continue;
1461  }
1462 
1463  // check for duplicates
1464  if ( keys.contains( connectionName ) && prompt )
1465  {
1466  int res = QMessageBox::warning( this,
1467  tr( "Loading Connections" ),
1468  tr( "Connection with name '%1' already exists. Overwrite?" )
1469  .arg( connectionName ),
1470  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1471  switch ( res )
1472  {
1473  case QMessageBox::Cancel:
1474  return;
1475  case QMessageBox::No:
1476  child = child.nextSiblingElement();
1477  continue;
1478  case QMessageBox::Yes:
1479  overwrite = true;
1480  break;
1481  case QMessageBox::YesToAll:
1482  prompt = false;
1483  overwrite = true;
1484  break;
1485  case QMessageBox::NoToAll:
1486  prompt = false;
1487  overwrite = false;
1488  break;
1489  }
1490  }
1491 
1492  if ( keys.contains( connectionName ) )
1493  {
1494  if ( !overwrite )
1495  {
1496  child = child.nextSiblingElement();
1497  continue;
1498  }
1499  }
1500  else
1501  {
1502  keys << connectionName;
1503  }
1504 
1505  //no dups detected or overwrite is allowed
1506  settings.beginGroup( "/HANA/connections/" + connectionName );
1507 
1508  for ( const QString param :
1509  {"driver", "host", "database", "identifierType", "identifier", "multitenant", "schema", "userTablesOnly",
1510  "allowGeometrylessTables", "saveUsername", "username", "savePassword", "password", "sslEnabled",
1511  "sslCryptoProvider", "sslKeyStore", "sslTrustStore", "sslValidateCertificate", "sslHostNameInCertificate"
1512  } )
1513  settings.setValue( QStringLiteral( "/" ) + param, child.attribute( param ) );
1514 
1515  settings.endGroup();
1516 
1517  child = child.nextSiblingElement();
1518  }
1519 }
1520 
1521 void QgsManageConnectionsDialog::loadGeonodeConnections( const QDomDocument &doc, const QStringList &items )
1522 {
1523  QDomElement root = doc.documentElement();
1524  if ( root.tagName() != QLatin1String( "qgsGeoNodeConnections" ) )
1525  {
1526  QMessageBox::information( this, tr( "Loading Connections" ),
1527  tr( "The file is not a GeoNode connections exchange file." ) );
1528  return;
1529  }
1530 
1531  QString connectionName;
1532  QgsSettings settings;
1533  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
1534  QStringList keys = settings.childGroups();
1535  settings.endGroup();
1536  QDomElement child = root.firstChildElement();
1537  bool prompt = true;
1538  bool overwrite = true;
1539 
1540  while ( !child.isNull() )
1541  {
1542  connectionName = child.attribute( QStringLiteral( "name" ) );
1543  if ( !items.contains( connectionName ) )
1544  {
1545  child = child.nextSiblingElement();
1546  continue;
1547  }
1548 
1549  // check for duplicates
1550  if ( keys.contains( connectionName ) && prompt )
1551  {
1552  int res = QMessageBox::warning( this,
1553  tr( "Loading Connections" ),
1554  tr( "Connection with name '%1' already exists. Overwrite?" )
1555  .arg( connectionName ),
1556  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1557 
1558  switch ( res )
1559  {
1560  case QMessageBox::Cancel:
1561  return;
1562  case QMessageBox::No:
1563  child = child.nextSiblingElement();
1564  continue;
1565  case QMessageBox::Yes:
1566  overwrite = true;
1567  break;
1568  case QMessageBox::YesToAll:
1569  prompt = false;
1570  overwrite = true;
1571  break;
1572  case QMessageBox::NoToAll:
1573  prompt = false;
1574  overwrite = false;
1575  break;
1576  }
1577  }
1578 
1579  if ( keys.contains( connectionName ) )
1580  {
1581  if ( !overwrite )
1582  {
1583  child = child.nextSiblingElement();
1584  continue;
1585  }
1586  }
1587  else
1588  {
1589  keys << connectionName;
1590  }
1591 
1592  // no dups detected or overwrite is allowed
1593  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
1594  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
1595  settings.endGroup();
1596 
1597  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
1598  {
1599  settings.beginGroup( "/qgis/GeoNode/" + connectionName );
1600  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1601  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1602  settings.endGroup();
1603  }
1604  child = child.nextSiblingElement();
1605  }
1606 }
1607 
1608 void QgsManageConnectionsDialog::loadXyzTilesConnections( const QDomDocument &doc, const QStringList &items )
1609 {
1610  QDomElement root = doc.documentElement();
1611  if ( root.tagName() != QLatin1String( "qgsXYZTilesConnections" ) )
1612  {
1613  QMessageBox::information( this, tr( "Loading Connections" ),
1614  tr( "The file is not a XYZ Tiles connections exchange file." ) );
1615  return;
1616  }
1617 
1618  QString connectionName;
1619  QgsSettings settings;
1620  settings.beginGroup( QStringLiteral( "/qgis/connections-xyz" ) );
1621  QStringList keys = settings.childGroups();
1622  settings.endGroup();
1623  QDomElement child = root.firstChildElement();
1624  bool prompt = true;
1625  bool overwrite = true;
1626 
1627  while ( !child.isNull() )
1628  {
1629  connectionName = child.attribute( QStringLiteral( "name" ) );
1630  if ( !items.contains( connectionName ) )
1631  {
1632  child = child.nextSiblingElement();
1633  continue;
1634  }
1635 
1636  // check for duplicates
1637  if ( keys.contains( connectionName ) && prompt )
1638  {
1639  int res = QMessageBox::warning( this,
1640  tr( "Loading Connections" ),
1641  tr( "Connection with name '%1' already exists. Overwrite?" )
1642  .arg( connectionName ),
1643  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1644 
1645  switch ( res )
1646  {
1647  case QMessageBox::Cancel:
1648  return;
1649  case QMessageBox::No:
1650  child = child.nextSiblingElement();
1651  continue;
1652  case QMessageBox::Yes:
1653  overwrite = true;
1654  break;
1655  case QMessageBox::YesToAll:
1656  prompt = false;
1657  overwrite = true;
1658  break;
1659  case QMessageBox::NoToAll:
1660  prompt = false;
1661  overwrite = false;
1662  break;
1663  }
1664  }
1665 
1666  if ( keys.contains( connectionName ) )
1667  {
1668  if ( !overwrite )
1669  {
1670  child = child.nextSiblingElement();
1671  continue;
1672  }
1673  }
1674  else
1675  {
1676  keys << connectionName;
1677  }
1678 
1679  settings.beginGroup( "qgis/connections-xyz/" + connectionName );
1680  settings.setValue( QStringLiteral( "url" ), child.attribute( QStringLiteral( "url" ) ) );
1681  settings.setValue( QStringLiteral( "zmin" ), child.attribute( QStringLiteral( "zmin" ) ) );
1682  settings.setValue( QStringLiteral( "zmax" ), child.attribute( QStringLiteral( "zmax" ) ) );
1683  settings.setValue( QStringLiteral( "authcfg" ), child.attribute( QStringLiteral( "authcfg" ) ) );
1684  settings.setValue( QStringLiteral( "username" ), child.attribute( QStringLiteral( "username" ) ) );
1685  settings.setValue( QStringLiteral( "password" ), child.attribute( QStringLiteral( "password" ) ) );
1686  settings.setValue( QStringLiteral( "referer" ), child.attribute( QStringLiteral( "referer" ) ) );
1687  settings.setValue( QStringLiteral( "tilePixelRatio" ), child.attribute( QStringLiteral( "tilePixelRatio" ) ) );
1688  settings.endGroup();
1689 
1690  child = child.nextSiblingElement();
1691  }
1692 }
1693 
1694 void QgsManageConnectionsDialog::loadArcgisConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
1695 {
1696  QDomElement root = doc.documentElement();
1697  if ( root.tagName() != "qgs" + service.toUpper() + "Connections" )
1698  {
1699  QMessageBox::information( this, tr( "Loading Connections" ),
1700  tr( "The file is not a %1 connections exchange file." ).arg( service ) );
1701  return;
1702  }
1703 
1704  QString connectionName;
1705  QgsSettings settings;
1706  settings.beginGroup( "/qgis/connections-" + service.toLower() );
1707  QStringList keys = settings.childGroups();
1708  settings.endGroup();
1709  QDomElement child = root.firstChildElement();
1710  bool prompt = true;
1711  bool overwrite = true;
1712 
1713  while ( !child.isNull() )
1714  {
1715  connectionName = child.attribute( QStringLiteral( "name" ) );
1716  if ( !items.contains( connectionName ) )
1717  {
1718  child = child.nextSiblingElement();
1719  continue;
1720  }
1721 
1722  // check for duplicates
1723  if ( keys.contains( connectionName ) && prompt )
1724  {
1725  int res = QMessageBox::warning( this,
1726  tr( "Loading Connections" ),
1727  tr( "Connection with name '%1' already exists. Overwrite?" )
1728  .arg( connectionName ),
1729  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1730 
1731  switch ( res )
1732  {
1733  case QMessageBox::Cancel:
1734  return;
1735  case QMessageBox::No:
1736  child = child.nextSiblingElement();
1737  continue;
1738  case QMessageBox::Yes:
1739  overwrite = true;
1740  break;
1741  case QMessageBox::YesToAll:
1742  prompt = false;
1743  overwrite = true;
1744  break;
1745  case QMessageBox::NoToAll:
1746  prompt = false;
1747  overwrite = false;
1748  break;
1749  }
1750  }
1751 
1752  if ( keys.contains( connectionName ) )
1753  {
1754  if ( !overwrite )
1755  {
1756  child = child.nextSiblingElement();
1757  continue;
1758  }
1759  }
1760  else
1761  {
1762  keys << connectionName;
1763  }
1764 
1765  // no dups detected or overwrite is allowed
1766  settings.beginGroup( "/qgis/connections-" + service.toLower() );
1767  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
1768  settings.setValue( QString( '/' + connectionName + "/referer" ), child.attribute( QStringLiteral( "referer" ) ) );
1769  settings.endGroup();
1770 
1771  settings.beginGroup( "/qgis/" + service.toUpper() + '/' + connectionName );
1772  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1773  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1774  settings.setValue( QStringLiteral( "/authcfg" ), child.attribute( QStringLiteral( "authcfg" ) ) );
1775  settings.endGroup();
1776 
1777  child = child.nextSiblingElement();
1778  }
1779 }
1780 
1781 void QgsManageConnectionsDialog::loadVectorTileConnections( const QDomDocument &doc, const QStringList &items )
1782 {
1783  QDomElement root = doc.documentElement();
1784  if ( root.tagName() != QLatin1String( "qgsVectorTileConnections" ) )
1785  {
1786  QMessageBox::information( this, tr( "Loading Connections" ),
1787  tr( "The file is not a Vector Tile connections exchange file." ) );
1788  return;
1789  }
1790 
1791  QString connectionName;
1792  QgsSettings settings;
1793  settings.beginGroup( QStringLiteral( "/qgis/connections-vector-tile" ) );
1794  QStringList keys = settings.childGroups();
1795  settings.endGroup();
1796  QDomElement child = root.firstChildElement();
1797  bool prompt = true;
1798  bool overwrite = true;
1799 
1800  while ( !child.isNull() )
1801  {
1802  connectionName = child.attribute( QStringLiteral( "name" ) );
1803  if ( !items.contains( connectionName ) )
1804  {
1805  child = child.nextSiblingElement();
1806  continue;
1807  }
1808 
1809  // check for duplicates
1810  if ( keys.contains( connectionName ) && prompt )
1811  {
1812  int res = QMessageBox::warning( this,
1813  tr( "Loading Connections" ),
1814  tr( "Connection with name '%1' already exists. Overwrite?" )
1815  .arg( connectionName ),
1816  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1817 
1818  switch ( res )
1819  {
1820  case QMessageBox::Cancel:
1821  return;
1822  case QMessageBox::No:
1823  child = child.nextSiblingElement();
1824  continue;
1825  case QMessageBox::Yes:
1826  overwrite = true;
1827  break;
1828  case QMessageBox::YesToAll:
1829  prompt = false;
1830  overwrite = true;
1831  break;
1832  case QMessageBox::NoToAll:
1833  prompt = false;
1834  overwrite = false;
1835  break;
1836  }
1837  }
1838 
1839  if ( keys.contains( connectionName ) )
1840  {
1841  if ( !overwrite )
1842  {
1843  child = child.nextSiblingElement();
1844  continue;
1845  }
1846  }
1847  else
1848  {
1849  keys << connectionName;
1850  }
1851 
1852  settings.beginGroup( "qgis/connections-vector-tile/" + connectionName );
1853  settings.setValue( QStringLiteral( "url" ), child.attribute( QStringLiteral( "url" ) ) );
1854  settings.setValue( QStringLiteral( "zmin" ), child.attribute( QStringLiteral( "zmin" ) ) );
1855  settings.setValue( QStringLiteral( "zmax" ), child.attribute( QStringLiteral( "zmax" ) ) );
1856  settings.setValue( QStringLiteral( "serviceType" ), child.attribute( QStringLiteral( "serviceType" ) ) );
1857  settings.setValue( QStringLiteral( "authcfg" ), child.attribute( QStringLiteral( "authcfg" ) ) );
1858  settings.setValue( QStringLiteral( "username" ), child.attribute( QStringLiteral( "username" ) ) );
1859  settings.setValue( QStringLiteral( "password" ), child.attribute( QStringLiteral( "password" ) ) );
1860  settings.setValue( QStringLiteral( "referer" ), child.attribute( QStringLiteral( "referer" ) ) );
1861  settings.setValue( QStringLiteral( "styleUrl" ), child.attribute( QStringLiteral( "styleUrl" ) ) );
1862 
1863  settings.endGroup();
1864 
1865  child = child.nextSiblingElement();
1866  }
1867 }
1868 
1870 {
1871  listConnections->selectAll();
1872  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !listConnections->selectedItems().isEmpty() );
1873 }
1874 
1876 {
1877  listConnections->clearSelection();
1878  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
1879 }
QgsManageConnectionsDialog(QWidget *parent=nullptr, Mode mode=Export, Type type=WMS, const QString &fileName=QString())
Constructor for QgsManageConnectionsDialog.