QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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  const 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  const 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  const 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  const 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  const 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  el.setAttribute( QStringLiteral( "projectsInDatabase" ), settings.value( path + "/projectsInDatabase", "0" ).toString() );
544 
545  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
546 
547  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
548  {
549  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
550  }
551 
552  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
553 
554  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
555  {
556  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
557  }
558 
559  root.appendChild( el );
560  }
561 
562  return doc;
563 }
564 
565 QDomDocument QgsManageConnectionsDialog::saveMssqlConnections( const QStringList &connections )
566 {
567  QDomDocument doc( QStringLiteral( "connections" ) );
568  QDomElement root = doc.createElement( QStringLiteral( "qgsMssqlConnections" ) );
569  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
570  doc.appendChild( root );
571 
572  const QgsSettings settings;
573  QString path;
574  for ( int i = 0; i < connections.count(); ++i )
575  {
576  path = "/MSSQL/connections/" + connections[ i ];
577  QDomElement el = doc.createElement( QStringLiteral( "mssql" ) );
578  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
579  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host" ).toString() );
580  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port" ).toString() );
581  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database" ).toString() );
582  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service" ).toString() );
583  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
584  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
585 
586  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
587 
588  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
589  {
590  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
591  }
592 
593  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
594 
595  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
596  {
597  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
598  }
599 
600  root.appendChild( el );
601  }
602 
603  return doc;
604 }
605 
606 QDomDocument QgsManageConnectionsDialog::saveOracleConnections( const QStringList &connections )
607 {
608  QDomDocument doc( QStringLiteral( "connections" ) );
609  QDomElement root = doc.createElement( QStringLiteral( "qgsOracleConnections" ) );
610  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
611  doc.appendChild( root );
612 
613  const QgsSettings settings;
614  QString path;
615  for ( int i = 0; i < connections.count(); ++i )
616  {
617  path = "/Oracle/connections/" + connections[ i ];
618  QDomElement el = doc.createElement( QStringLiteral( "oracle" ) );
619  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
620  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host" ).toString() );
621  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port" ).toString() );
622  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database" ).toString() );
623  el.setAttribute( QStringLiteral( "dboptions" ), settings.value( path + "/dboptions" ).toString() );
624  el.setAttribute( QStringLiteral( "dbworkspace" ), settings.value( path + "/dbworkspace" ).toString() );
625  el.setAttribute( QStringLiteral( "schema" ), settings.value( path + "/schema" ).toString() );
626  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
627  el.setAttribute( QStringLiteral( "userTablesOnly" ), settings.value( path + "/userTablesOnly", "0" ).toString() );
628  el.setAttribute( QStringLiteral( "geometryColumnsOnly" ), settings.value( path + "/geometryColumnsOnly", "0" ).toString() );
629  el.setAttribute( QStringLiteral( "allowGeometrylessTables" ), settings.value( path + "/allowGeometrylessTables", "0" ).toString() );
630 
631  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
632 
633  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
634  {
635  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
636  }
637 
638  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
639 
640  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
641  {
642  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
643  }
644 
645  root.appendChild( el );
646  }
647 
648  return doc;
649 }
650 
651 QDomDocument QgsManageConnectionsDialog::saveDb2Connections( const QStringList &connections )
652 {
653  QDomDocument doc( QStringLiteral( "connections" ) );
654  QDomElement root = doc.createElement( QStringLiteral( "qgsDb2Connections" ) );
655  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
656  doc.appendChild( root );
657 
658  const QgsSettings settings;
659  QString path;
660  for ( int i = 0; i < connections.count(); ++i )
661  {
662  path = "/DB2/connections/" + connections[ i ];
663  QDomElement el = doc.createElement( QStringLiteral( "db2" ) );
664  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
665  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host" ).toString() );
666  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port" ).toString() );
667  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database" ).toString() );
668  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service" ).toString() );
669  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
670  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
671 
672  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
673 
674  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
675  {
676  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
677  }
678 
679  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
680 
681  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
682  {
683  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
684  }
685 
686  root.appendChild( el );
687  }
688 
689  return doc;
690 }
691 
692 QDomDocument QgsManageConnectionsDialog::saveHanaConnections( const QStringList &connections )
693 {
694  QDomDocument doc( QStringLiteral( "connections" ) );
695  QDomElement root = doc.createElement( QStringLiteral( "qgsHanaConnections" ) );
696  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
697  doc.appendChild( root );
698 
699  const QgsSettings settings;
700  QString path;
701  for ( int i = 0; i < connections.count(); ++i )
702  {
703  path = "/HANA/connections/" + connections[i];
704  QDomElement el = doc.createElement( QStringLiteral( "hana" ) );
705  el.setAttribute( QStringLiteral( "name" ), connections[i] );
706  el.setAttribute( QStringLiteral( "driver" ), settings.value( path + "/driver", QString() ).toString() );
707  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host", QString() ).toString() );
708  el.setAttribute( QStringLiteral( "identifierType" ), settings.value( path + "/identifierType", QString() ).toString() );
709  el.setAttribute( QStringLiteral( "identifier" ), settings.value( path + "/identifier", QString() ).toString() );
710  el.setAttribute( QStringLiteral( "multitenant" ), settings.value( path + "/multitenant", QString() ).toString() );
711  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database", QString() ).toString() );
712  el.setAttribute( QStringLiteral( "schema" ), settings.value( path + "/schema", QString() ).toString() );
713  el.setAttribute( QStringLiteral( "userTablesOnly" ), settings.value( path + "/userTablesOnly", QStringLiteral( "0" ) ).toString() );
714  el.setAttribute( QStringLiteral( "allowGeometrylessTables" ), settings.value( path + "/allowGeometrylessTables", QStringLiteral( "0" ) ).toString() );
715 
716  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", QStringLiteral( "false" ) ).toString() );
717  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
718  {
719  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", QString() ).toString() );
720  }
721 
722  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", QStringLiteral( "false" ) ).toString() );
723  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
724  {
725  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", QString() ).toString() );
726  }
727 
728  el.setAttribute( QStringLiteral( "sslEnabled" ), settings.value( path + "/sslEnabled", QStringLiteral( "false" ) ).toString() );
729  el.setAttribute( QStringLiteral( "sslCryptoProvider" ), settings.value( path + "/sslCryptoProvider", QStringLiteral( "openssl" ) ).toString() );
730  el.setAttribute( QStringLiteral( "sslKeyStore" ), settings.value( path + "/sslKeyStore", QString() ).toString() );
731  el.setAttribute( QStringLiteral( "sslTrustStore" ), settings.value( path + "/sslTrustStore", QString() ).toString() );
732  el.setAttribute( QStringLiteral( "sslValidateCertificate" ), settings.value( path + "/sslValidateCertificate", QStringLiteral( "false" ) ).toString() );
733  el.setAttribute( QStringLiteral( "sslHostNameInCertificate" ), settings.value( path + "/sslHostNameInCertificate", QString() ).toString() );
734 
735  root.appendChild( el );
736  }
737 
738  return doc;
739 }
740 
741 QDomDocument QgsManageConnectionsDialog::saveGeonodeConnections( const QStringList &connections )
742 {
743  QDomDocument doc( QStringLiteral( "connections" ) );
744  QDomElement root = doc.createElement( QStringLiteral( "qgsGeoNodeConnections" ) );
745  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
746  doc.appendChild( root );
747 
748  const QgsSettings settings;
749  QString path;
750  for ( int i = 0; i < connections.count(); ++i )
751  {
752  path = QStringLiteral( "/qgis/connections-geonode/" );
753  QDomElement el = doc.createElement( QStringLiteral( "geonode" ) );
754  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
755  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url" ).toString() );
756 
757  path = QStringLiteral( "/qgis/GeoNode/" );
758  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username" ).toString() );
759  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password" ).toString() );
760  root.appendChild( el );
761  }
762 
763  return doc;
764 }
765 
766 QDomDocument QgsManageConnectionsDialog::saveXyzTilesConnections( const QStringList &connections )
767 {
768  QDomDocument doc( QStringLiteral( "connections" ) );
769  QDomElement root = doc.createElement( QStringLiteral( "qgsXYZTilesConnections" ) );
770  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
771  doc.appendChild( root );
772 
773  const QgsSettings settings;
774  QString path;
775  for ( int i = 0; i < connections.count(); ++i )
776  {
777  path = "qgis/connections-xyz/" + connections[ i ];
778  QDomElement el = doc.createElement( QStringLiteral( "xyztiles" ) );
779 
780  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
781  el.setAttribute( QStringLiteral( "url" ), settings.value( path + "/url" ).toString() );
782  el.setAttribute( QStringLiteral( "zmin" ), settings.value( path + "/zmin", -1 ).toInt() );
783  el.setAttribute( QStringLiteral( "zmax" ), settings.value( path + "/zmax", -1 ).toInt() );
784  el.setAttribute( QStringLiteral( "authcfg" ), settings.value( path + "/authcfg" ).toString() );
785  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
786  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
787  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + "/referer" ).toString() );
788  el.setAttribute( QStringLiteral( "tilePixelRatio" ), settings.value( path + "/tilePixelRatio", 0 ).toDouble() );
789 
790  root.appendChild( el );
791  }
792 
793  return doc;
794 }
795 
796 QDomDocument QgsManageConnectionsDialog::saveArcgisConnections( const QStringList &connections, const QString &service )
797 {
798  QDomDocument doc( QStringLiteral( "connections" ) );
799  QDomElement root = doc.createElement( "qgs" + service.toUpper() + "Connections" );
800  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
801  doc.appendChild( root );
802 
803  const QgsSettings settings;
804  QString path;
805  for ( int i = 0; i < connections.count(); ++i )
806  {
807  path = "/qgis/connections-" + service.toLower() + '/';
808  QDomElement el = doc.createElement( service.toLower() );
809  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
810  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url" ).toString() );
811  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + connections[ i ] + "/referer" ).toString() );
812 
813  path = "/qgis/" + service.toUpper() + '/';
814  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username" ).toString() );
815  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password" ).toString() );
816  el.setAttribute( QStringLiteral( "authcfg" ), settings.value( path + connections[ i ] + "/authcfg" ).toString() );
817  root.appendChild( el );
818  }
819 
820  return doc;
821 }
822 
823 QDomDocument QgsManageConnectionsDialog::saveVectorTileConnections( const QStringList &connections )
824 {
825  QDomDocument doc( QStringLiteral( "connections" ) );
826  QDomElement root = doc.createElement( QStringLiteral( "qgsVectorTileConnections" ) );
827  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
828  doc.appendChild( root );
829 
830  const QgsSettings settings;
831  QString path;
832  for ( int i = 0; i < connections.count(); ++i )
833  {
834  path = "qgis/connections-vector-tile/" + connections[ i ];
835  QDomElement el = doc.createElement( QStringLiteral( "vectortile" ) );
836 
837  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
838  el.setAttribute( QStringLiteral( "url" ), settings.value( path + "/url" ).toString() );
839  el.setAttribute( QStringLiteral( "zmin" ), settings.value( path + "/zmin", -1 ).toInt() );
840  el.setAttribute( QStringLiteral( "zmax" ), settings.value( path + "/zmax", -1 ).toInt() );
841  el.setAttribute( QStringLiteral( "serviceType" ), settings.value( path + "/serviceType", QString() ).toString() );
842  el.setAttribute( QStringLiteral( "authcfg" ), settings.value( path + "/authcfg" ).toString() );
843  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
844  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
845  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + "/referer" ).toString() );
846  el.setAttribute( QStringLiteral( "styleUrl" ), settings.value( path + "/styleUrl" ).toString() );
847 
848  root.appendChild( el );
849  }
850 
851  return doc;
852 }
853 
854 void QgsManageConnectionsDialog::loadOWSConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
855 {
856  const QDomElement root = doc.documentElement();
857  if ( root.tagName() != "qgs" + service.toUpper() + "Connections" )
858  {
859  QMessageBox::information( this, tr( "Loading Connections" ),
860  tr( "The file is not a %1 connections exchange file." ).arg( service ) );
861  return;
862  }
863 
864  QString connectionName;
865  QgsSettings settings;
866  settings.beginGroup( "/qgis/connections-" + service.toLower() );
867  QStringList keys = settings.childGroups();
868  settings.endGroup();
869  QDomElement child = root.firstChildElement();
870  bool prompt = true;
871  bool overwrite = true;
872 
873  while ( !child.isNull() )
874  {
875  connectionName = child.attribute( QStringLiteral( "name" ) );
876  if ( !items.contains( connectionName ) )
877  {
878  child = child.nextSiblingElement();
879  continue;
880  }
881 
882  // check for duplicates
883  if ( keys.contains( connectionName ) && prompt )
884  {
885  const int res = QMessageBox::warning( this,
886  tr( "Loading Connections" ),
887  tr( "Connection with name '%1' already exists. Overwrite?" )
888  .arg( connectionName ),
889  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
890 
891  switch ( res )
892  {
893  case QMessageBox::Cancel:
894  return;
895  case QMessageBox::No:
896  child = child.nextSiblingElement();
897  continue;
898  case QMessageBox::Yes:
899  overwrite = true;
900  break;
901  case QMessageBox::YesToAll:
902  prompt = false;
903  overwrite = true;
904  break;
905  case QMessageBox::NoToAll:
906  prompt = false;
907  overwrite = false;
908  break;
909  }
910  }
911 
912  if ( keys.contains( connectionName ) )
913  {
914  if ( !overwrite )
915  {
916  child = child.nextSiblingElement();
917  continue;
918  }
919  }
920  else
921  {
922  keys << connectionName;
923  }
924 
925  // no dups detected or overwrite is allowed
926  settings.beginGroup( "/qgis/connections-" + service.toLower() );
927  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
928  settings.setValue( QString( '/' + connectionName + "/ignoreGetMapURI" ), child.attribute( QStringLiteral( "ignoreGetMapURI" ) ) == QLatin1String( "true" ) );
929  settings.setValue( QString( '/' + connectionName + "/ignoreGetFeatureInfoURI" ), child.attribute( QStringLiteral( "ignoreGetFeatureInfoURI" ) ) == QLatin1String( "true" ) );
930  settings.setValue( QString( '/' + connectionName + "/ignoreAxisOrientation" ), child.attribute( QStringLiteral( "ignoreAxisOrientation" ) ) == QLatin1String( "true" ) );
931  settings.setValue( QString( '/' + connectionName + "/invertAxisOrientation" ), child.attribute( QStringLiteral( "invertAxisOrientation" ) ) == QLatin1String( "true" ) );
932  settings.setValue( QString( '/' + connectionName + "/referer" ), child.attribute( QStringLiteral( "referer" ) ) );
933  settings.setValue( QString( '/' + connectionName + "/smoothPixmapTransform" ), child.attribute( QStringLiteral( "smoothPixmapTransform" ) ) == QLatin1String( "true" ) );
934  settings.setValue( QString( '/' + connectionName + "/dpiMode" ), child.attribute( QStringLiteral( "dpiMode" ), QStringLiteral( "7" ) ).toInt() );
935  settings.endGroup();
936 
937  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
938  {
939  settings.beginGroup( "/qgis/" + service.toUpper() + '/' + connectionName );
940  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
941  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
942  settings.endGroup();
943  }
944  child = child.nextSiblingElement();
945  }
946 }
947 
948 void QgsManageConnectionsDialog::loadWfsConnections( const QDomDocument &doc, const QStringList &items )
949 {
950  const QDomElement root = doc.documentElement();
951  if ( root.tagName() != QLatin1String( "qgsWFSConnections" ) )
952  {
953  QMessageBox::information( this, tr( "Loading Connections" ),
954  tr( "The file is not a WFS connections exchange file." ) );
955  return;
956  }
957 
958  QString connectionName;
959  QgsSettings settings;
960  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
961  QStringList keys = settings.childGroups();
962  settings.endGroup();
963  QDomElement child = root.firstChildElement();
964  bool prompt = true;
965  bool overwrite = true;
966 
967  while ( !child.isNull() )
968  {
969  connectionName = child.attribute( QStringLiteral( "name" ) );
970  if ( !items.contains( connectionName ) )
971  {
972  child = child.nextSiblingElement();
973  continue;
974  }
975 
976  // check for duplicates
977  if ( keys.contains( connectionName ) && prompt )
978  {
979  const int res = QMessageBox::warning( this,
980  tr( "Loading Connections" ),
981  tr( "Connection with name '%1' already exists. Overwrite?" )
982  .arg( connectionName ),
983  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
984 
985  switch ( res )
986  {
987  case QMessageBox::Cancel:
988  return;
989  case QMessageBox::No:
990  child = child.nextSiblingElement();
991  continue;
992  case QMessageBox::Yes:
993  overwrite = true;
994  break;
995  case QMessageBox::YesToAll:
996  prompt = false;
997  overwrite = true;
998  break;
999  case QMessageBox::NoToAll:
1000  prompt = false;
1001  overwrite = false;
1002  break;
1003  }
1004  }
1005 
1006  if ( keys.contains( connectionName ) )
1007  {
1008  if ( !overwrite )
1009  {
1010  child = child.nextSiblingElement();
1011  continue;
1012  }
1013  }
1014  else
1015  {
1016  keys << connectionName;
1017  }
1018 
1019  // no dups detected or overwrite is allowed
1020  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
1021  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
1022 
1023  settings.setValue( QString( '/' + connectionName + "/version" ), child.attribute( QStringLiteral( "version" ) ) );
1024  settings.setValue( QString( '/' + connectionName + "/maxnumfeatures" ), child.attribute( QStringLiteral( "maxnumfeatures" ) ) );
1025  settings.setValue( QString( '/' + connectionName + "/pagesize" ), child.attribute( QStringLiteral( "pagesize" ) ) );
1026  settings.setValue( QString( '/' + connectionName + "/pagingenabled" ), child.attribute( QStringLiteral( "pagingenabled" ) ) );
1027  settings.setValue( QString( '/' + connectionName + "/ignoreAxisOrientation" ), child.attribute( QStringLiteral( "ignoreAxisOrientation" ) ) );
1028  settings.setValue( QString( '/' + connectionName + "/invertAxisOrientation" ), child.attribute( QStringLiteral( "invertAxisOrientation" ) ) );
1029  settings.endGroup();
1030 
1031  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
1032  {
1033  settings.beginGroup( "/qgis/WFS/" + connectionName );
1034  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1035  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1036  settings.endGroup();
1037  }
1038  child = child.nextSiblingElement();
1039  }
1040 }
1041 
1042 void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, const QStringList &items )
1043 {
1044  const QDomElement root = doc.documentElement();
1045  if ( root.tagName() != QLatin1String( "qgsPgConnections" ) )
1046  {
1047  QMessageBox::information( this,
1048  tr( "Loading Connections" ),
1049  tr( "The file is not a PostGIS connections exchange file." ) );
1050  return;
1051  }
1052 
1053  QString connectionName;
1054  QgsSettings settings;
1055  settings.beginGroup( QStringLiteral( "/PostgreSQL/connections" ) );
1056  QStringList keys = settings.childGroups();
1057  settings.endGroup();
1058  QDomElement child = root.firstChildElement();
1059  bool prompt = true;
1060  bool overwrite = true;
1061 
1062  while ( !child.isNull() )
1063  {
1064  connectionName = child.attribute( QStringLiteral( "name" ) );
1065  if ( !items.contains( connectionName ) )
1066  {
1067  child = child.nextSiblingElement();
1068  continue;
1069  }
1070 
1071  // check for duplicates
1072  if ( keys.contains( connectionName ) && prompt )
1073  {
1074  const int res = QMessageBox::warning( this,
1075  tr( "Loading Connections" ),
1076  tr( "Connection with name '%1' already exists. Overwrite?" )
1077  .arg( connectionName ),
1078  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1079  switch ( res )
1080  {
1081  case QMessageBox::Cancel:
1082  return;
1083  case QMessageBox::No:
1084  child = child.nextSiblingElement();
1085  continue;
1086  case QMessageBox::Yes:
1087  overwrite = true;
1088  break;
1089  case QMessageBox::YesToAll:
1090  prompt = false;
1091  overwrite = true;
1092  break;
1093  case QMessageBox::NoToAll:
1094  prompt = false;
1095  overwrite = false;
1096  break;
1097  }
1098  }
1099 
1100  if ( keys.contains( connectionName ) )
1101  {
1102  if ( !overwrite )
1103  {
1104  child = child.nextSiblingElement();
1105  continue;
1106  }
1107  }
1108  else
1109  {
1110  keys << connectionName;
1111  }
1112 
1113  //no dups detected or overwrite is allowed
1114  settings.beginGroup( "/PostgreSQL/connections/" + connectionName );
1115 
1116  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1117  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1118  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1119  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1120  {
1121  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1122  }
1123  else
1124  {
1125  settings.setValue( QStringLiteral( "/service" ), "" );
1126  }
1127  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1128  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1129  settings.setValue( QStringLiteral( "/projectsInDatabase" ), child.attribute( QStringLiteral( "projectsInDatabase" ), 0 ) );
1130  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1131  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1132  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1133  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1134  settings.endGroup();
1135 
1136  child = child.nextSiblingElement();
1137  }
1138 }
1139 
1140 void QgsManageConnectionsDialog::loadMssqlConnections( const QDomDocument &doc, const QStringList &items )
1141 {
1142  const QDomElement root = doc.documentElement();
1143  if ( root.tagName() != QLatin1String( "qgsMssqlConnections" ) )
1144  {
1145  QMessageBox::information( this,
1146  tr( "Loading Connections" ),
1147  tr( "The file is not a MSSQL connections exchange file." ) );
1148  return;
1149  }
1150 
1151  QString connectionName;
1152  QgsSettings settings;
1153  settings.beginGroup( QStringLiteral( "/MSSQL/connections" ) );
1154  QStringList keys = settings.childGroups();
1155  settings.endGroup();
1156  QDomElement child = root.firstChildElement();
1157  bool prompt = true;
1158  bool overwrite = true;
1159 
1160  while ( !child.isNull() )
1161  {
1162  connectionName = child.attribute( QStringLiteral( "name" ) );
1163  if ( !items.contains( connectionName ) )
1164  {
1165  child = child.nextSiblingElement();
1166  continue;
1167  }
1168 
1169  // check for duplicates
1170  if ( keys.contains( connectionName ) && prompt )
1171  {
1172  const int res = QMessageBox::warning( this,
1173  tr( "Loading Connections" ),
1174  tr( "Connection with name '%1' already exists. Overwrite?" )
1175  .arg( connectionName ),
1176  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1177  switch ( res )
1178  {
1179  case QMessageBox::Cancel:
1180  return;
1181  case QMessageBox::No:
1182  child = child.nextSiblingElement();
1183  continue;
1184  case QMessageBox::Yes:
1185  overwrite = true;
1186  break;
1187  case QMessageBox::YesToAll:
1188  prompt = false;
1189  overwrite = true;
1190  break;
1191  case QMessageBox::NoToAll:
1192  prompt = false;
1193  overwrite = false;
1194  break;
1195  }
1196  }
1197 
1198  if ( keys.contains( connectionName ) )
1199  {
1200  if ( !overwrite )
1201  {
1202  child = child.nextSiblingElement();
1203  continue;
1204  }
1205  }
1206  else
1207  {
1208  keys << connectionName;
1209  }
1210 
1211  //no dups detected or overwrite is allowed
1212  settings.beginGroup( "/MSSQL/connections/" + connectionName );
1213 
1214  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1215  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1216  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1217  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1218  {
1219  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1220  }
1221  else
1222  {
1223  settings.setValue( QStringLiteral( "/service" ), "" );
1224  }
1225  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1226  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1227  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1228  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1229  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1230  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1231  settings.endGroup();
1232 
1233  child = child.nextSiblingElement();
1234  }
1235 }
1236 
1237 void QgsManageConnectionsDialog::loadOracleConnections( const QDomDocument &doc, const QStringList &items )
1238 {
1239  const QDomElement root = doc.documentElement();
1240  if ( root.tagName() != QLatin1String( "qgsOracleConnections" ) )
1241  {
1242  QMessageBox::information( this,
1243  tr( "Loading Connections" ),
1244  tr( "The file is not an Oracle connections exchange file." ) );
1245  return;
1246  }
1247 
1248  QString connectionName;
1249  QgsSettings settings;
1250  settings.beginGroup( QStringLiteral( "/Oracle/connections" ) );
1251  QStringList keys = settings.childGroups();
1252  settings.endGroup();
1253  QDomElement child = root.firstChildElement();
1254  bool prompt = true;
1255  bool overwrite = true;
1256 
1257  while ( !child.isNull() )
1258  {
1259  connectionName = child.attribute( QStringLiteral( "name" ) );
1260  if ( !items.contains( connectionName ) )
1261  {
1262  child = child.nextSiblingElement();
1263  continue;
1264  }
1265 
1266  // check for duplicates
1267  if ( keys.contains( connectionName ) && prompt )
1268  {
1269  const int res = QMessageBox::warning( this,
1270  tr( "Loading Connections" ),
1271  tr( "Connection with name '%1' already exists. Overwrite?" )
1272  .arg( connectionName ),
1273  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1274  switch ( res )
1275  {
1276  case QMessageBox::Cancel:
1277  return;
1278  case QMessageBox::No:
1279  child = child.nextSiblingElement();
1280  continue;
1281  case QMessageBox::Yes:
1282  overwrite = true;
1283  break;
1284  case QMessageBox::YesToAll:
1285  prompt = false;
1286  overwrite = true;
1287  break;
1288  case QMessageBox::NoToAll:
1289  prompt = false;
1290  overwrite = false;
1291  break;
1292  }
1293  }
1294 
1295  if ( keys.contains( connectionName ) )
1296  {
1297  if ( !overwrite )
1298  {
1299  child = child.nextSiblingElement();
1300  continue;
1301  }
1302  }
1303  else
1304  {
1305  keys << connectionName;
1306  }
1307 
1308  //no dups detected or overwrite is allowed
1309  settings.beginGroup( "/Oracle/connections/" + connectionName );
1310 
1311  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1312  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1313  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1314  settings.setValue( QStringLiteral( "/dboptions" ), child.attribute( QStringLiteral( "dboptions" ) ) );
1315  settings.setValue( QStringLiteral( "/dbworkspace" ), child.attribute( QStringLiteral( "dbworkspace" ) ) );
1316  settings.setValue( QStringLiteral( "/schema" ), child.attribute( QStringLiteral( "schema" ) ) );
1317  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1318  settings.setValue( QStringLiteral( "/userTablesOnly" ), child.attribute( QStringLiteral( "userTablesOnly" ) ) );
1319  settings.setValue( QStringLiteral( "/geometryColumnsOnly" ), child.attribute( QStringLiteral( "geometryColumnsOnly" ) ) );
1320  settings.setValue( QStringLiteral( "/allowGeometrylessTables" ), child.attribute( QStringLiteral( "allowGeometrylessTables" ) ) );
1321  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1322  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1323  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1324  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1325  settings.endGroup();
1326 
1327  child = child.nextSiblingElement();
1328  }
1329 }
1330 
1331 void QgsManageConnectionsDialog::loadDb2Connections( const QDomDocument &doc, const QStringList &items )
1332 {
1333  const QDomElement root = doc.documentElement();
1334  if ( root.tagName() != QLatin1String( "qgsDb2Connections" ) )
1335  {
1336  QMessageBox::information( this,
1337  tr( "Loading Connections" ),
1338  tr( "The file is not a DB2 connections exchange file." ) );
1339  return;
1340  }
1341 
1342  QString connectionName;
1343  QgsSettings settings;
1344  settings.beginGroup( QStringLiteral( "/DB2/connections" ) );
1345  QStringList keys = settings.childGroups();
1346  settings.endGroup();
1347  QDomElement child = root.firstChildElement();
1348  bool prompt = true;
1349  bool overwrite = true;
1350 
1351  while ( !child.isNull() )
1352  {
1353  connectionName = child.attribute( QStringLiteral( "name" ) );
1354  if ( !items.contains( connectionName ) )
1355  {
1356  child = child.nextSiblingElement();
1357  continue;
1358  }
1359 
1360  // check for duplicates
1361  if ( keys.contains( connectionName ) && prompt )
1362  {
1363  const int res = QMessageBox::warning( this,
1364  tr( "Loading Connections" ),
1365  tr( "Connection with name '%1' already exists. Overwrite?" )
1366  .arg( connectionName ),
1367  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1368  switch ( res )
1369  {
1370  case QMessageBox::Cancel:
1371  return;
1372  case QMessageBox::No:
1373  child = child.nextSiblingElement();
1374  continue;
1375  case QMessageBox::Yes:
1376  overwrite = true;
1377  break;
1378  case QMessageBox::YesToAll:
1379  prompt = false;
1380  overwrite = true;
1381  break;
1382  case QMessageBox::NoToAll:
1383  prompt = false;
1384  overwrite = false;
1385  break;
1386  }
1387  }
1388 
1389  if ( keys.contains( connectionName ) )
1390  {
1391  if ( !overwrite )
1392  {
1393  child = child.nextSiblingElement();
1394  continue;
1395  }
1396  }
1397  else
1398  {
1399  keys << connectionName;
1400  }
1401 
1402  //no dups detected or overwrite is allowed
1403  settings.beginGroup( "/DB2/connections/" + connectionName );
1404 
1405  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1406  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1407  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1408  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1409  {
1410  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1411  }
1412  else
1413  {
1414  settings.setValue( QStringLiteral( "/service" ), "" );
1415  }
1416  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1417  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1418  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1419  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1420  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1421  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1422  settings.endGroup();
1423 
1424  child = child.nextSiblingElement();
1425  }
1426 }
1427 
1428 void QgsManageConnectionsDialog::loadHanaConnections( const QDomDocument &doc, const QStringList &items )
1429 {
1430  QDomElement root = doc.documentElement();
1431  if ( root.tagName() != QLatin1String( "qgsHanaConnections" ) )
1432  {
1433  QMessageBox::warning( this,
1434  tr( "Loading Connections" ),
1435  tr( "The file is not a HANA connections exchange file." ) );
1436  return;
1437  }
1438 
1439  const QDomAttr version = root.attributeNode( "version" );
1440  if ( version.value() != QLatin1String( "1.0" ) )
1441  {
1442  QMessageBox::warning( this,
1443  tr( "Loading Connections" ),
1444  tr( "The HANA connections exchange file version '%1' is not supported." ).arg( version.value() ) );
1445  return;
1446  }
1447 
1448  QgsSettings settings;
1449  settings.beginGroup( QStringLiteral( "/HANA/connections" ) );
1450  QStringList keys = settings.childGroups();
1451  settings.endGroup();
1452  QDomElement child = root.firstChildElement();
1453  bool prompt = true;
1454  bool overwrite = true;
1455 
1456  while ( !child.isNull() )
1457  {
1458  const QString connectionName = child.attribute( QStringLiteral( "name" ) );
1459  if ( !items.contains( connectionName ) )
1460  {
1461  child = child.nextSiblingElement();
1462  continue;
1463  }
1464 
1465  // check for duplicates
1466  if ( keys.contains( connectionName ) && prompt )
1467  {
1468  const int res = QMessageBox::warning( this,
1469  tr( "Loading Connections" ),
1470  tr( "Connection with name '%1' already exists. Overwrite?" )
1471  .arg( connectionName ),
1472  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1473  switch ( res )
1474  {
1475  case QMessageBox::Cancel:
1476  return;
1477  case QMessageBox::No:
1478  child = child.nextSiblingElement();
1479  continue;
1480  case QMessageBox::Yes:
1481  overwrite = true;
1482  break;
1483  case QMessageBox::YesToAll:
1484  prompt = false;
1485  overwrite = true;
1486  break;
1487  case QMessageBox::NoToAll:
1488  prompt = false;
1489  overwrite = false;
1490  break;
1491  }
1492  }
1493 
1494  if ( keys.contains( connectionName ) )
1495  {
1496  if ( !overwrite )
1497  {
1498  child = child.nextSiblingElement();
1499  continue;
1500  }
1501  }
1502  else
1503  {
1504  keys << connectionName;
1505  }
1506 
1507  //no dups detected or overwrite is allowed
1508  settings.beginGroup( "/HANA/connections/" + connectionName );
1509 
1510  for ( const QString param :
1511  {"driver", "host", "database", "identifierType", "identifier", "multitenant", "schema", "userTablesOnly",
1512  "allowGeometrylessTables", "saveUsername", "username", "savePassword", "password", "sslEnabled",
1513  "sslCryptoProvider", "sslKeyStore", "sslTrustStore", "sslValidateCertificate", "sslHostNameInCertificate"
1514  } )
1515  settings.setValue( QStringLiteral( "/" ) + param, child.attribute( param ) );
1516 
1517  settings.endGroup();
1518 
1519  child = child.nextSiblingElement();
1520  }
1521 }
1522 
1523 void QgsManageConnectionsDialog::loadGeonodeConnections( const QDomDocument &doc, const QStringList &items )
1524 {
1525  const QDomElement root = doc.documentElement();
1526  if ( root.tagName() != QLatin1String( "qgsGeoNodeConnections" ) )
1527  {
1528  QMessageBox::information( this, tr( "Loading Connections" ),
1529  tr( "The file is not a GeoNode connections exchange file." ) );
1530  return;
1531  }
1532 
1533  QString connectionName;
1534  QgsSettings settings;
1535  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
1536  QStringList keys = settings.childGroups();
1537  settings.endGroup();
1538  QDomElement child = root.firstChildElement();
1539  bool prompt = true;
1540  bool overwrite = true;
1541 
1542  while ( !child.isNull() )
1543  {
1544  connectionName = child.attribute( QStringLiteral( "name" ) );
1545  if ( !items.contains( connectionName ) )
1546  {
1547  child = child.nextSiblingElement();
1548  continue;
1549  }
1550 
1551  // check for duplicates
1552  if ( keys.contains( connectionName ) && prompt )
1553  {
1554  const int res = QMessageBox::warning( this,
1555  tr( "Loading Connections" ),
1556  tr( "Connection with name '%1' already exists. Overwrite?" )
1557  .arg( connectionName ),
1558  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1559 
1560  switch ( res )
1561  {
1562  case QMessageBox::Cancel:
1563  return;
1564  case QMessageBox::No:
1565  child = child.nextSiblingElement();
1566  continue;
1567  case QMessageBox::Yes:
1568  overwrite = true;
1569  break;
1570  case QMessageBox::YesToAll:
1571  prompt = false;
1572  overwrite = true;
1573  break;
1574  case QMessageBox::NoToAll:
1575  prompt = false;
1576  overwrite = false;
1577  break;
1578  }
1579  }
1580 
1581  if ( keys.contains( connectionName ) )
1582  {
1583  if ( !overwrite )
1584  {
1585  child = child.nextSiblingElement();
1586  continue;
1587  }
1588  }
1589  else
1590  {
1591  keys << connectionName;
1592  }
1593 
1594  // no dups detected or overwrite is allowed
1595  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
1596  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
1597  settings.endGroup();
1598 
1599  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
1600  {
1601  settings.beginGroup( "/qgis/GeoNode/" + connectionName );
1602  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1603  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1604  settings.endGroup();
1605  }
1606  child = child.nextSiblingElement();
1607  }
1608 }
1609 
1610 void QgsManageConnectionsDialog::loadXyzTilesConnections( const QDomDocument &doc, const QStringList &items )
1611 {
1612  const QDomElement root = doc.documentElement();
1613  if ( root.tagName() != QLatin1String( "qgsXYZTilesConnections" ) )
1614  {
1615  QMessageBox::information( this, tr( "Loading Connections" ),
1616  tr( "The file is not a XYZ Tiles connections exchange file." ) );
1617  return;
1618  }
1619 
1620  QString connectionName;
1621  QgsSettings settings;
1622  settings.beginGroup( QStringLiteral( "/qgis/connections-xyz" ) );
1623  QStringList keys = settings.childGroups();
1624  settings.endGroup();
1625  QDomElement child = root.firstChildElement();
1626  bool prompt = true;
1627  bool overwrite = true;
1628 
1629  while ( !child.isNull() )
1630  {
1631  connectionName = child.attribute( QStringLiteral( "name" ) );
1632  if ( !items.contains( connectionName ) )
1633  {
1634  child = child.nextSiblingElement();
1635  continue;
1636  }
1637 
1638  // check for duplicates
1639  if ( keys.contains( connectionName ) && prompt )
1640  {
1641  const int res = QMessageBox::warning( this,
1642  tr( "Loading Connections" ),
1643  tr( "Connection with name '%1' already exists. Overwrite?" )
1644  .arg( connectionName ),
1645  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1646 
1647  switch ( res )
1648  {
1649  case QMessageBox::Cancel:
1650  return;
1651  case QMessageBox::No:
1652  child = child.nextSiblingElement();
1653  continue;
1654  case QMessageBox::Yes:
1655  overwrite = true;
1656  break;
1657  case QMessageBox::YesToAll:
1658  prompt = false;
1659  overwrite = true;
1660  break;
1661  case QMessageBox::NoToAll:
1662  prompt = false;
1663  overwrite = false;
1664  break;
1665  }
1666  }
1667 
1668  if ( keys.contains( connectionName ) )
1669  {
1670  if ( !overwrite )
1671  {
1672  child = child.nextSiblingElement();
1673  continue;
1674  }
1675  }
1676  else
1677  {
1678  keys << connectionName;
1679  }
1680 
1681  settings.beginGroup( "qgis/connections-xyz/" + connectionName );
1682  settings.setValue( QStringLiteral( "url" ), child.attribute( QStringLiteral( "url" ) ) );
1683  settings.setValue( QStringLiteral( "zmin" ), child.attribute( QStringLiteral( "zmin" ) ) );
1684  settings.setValue( QStringLiteral( "zmax" ), child.attribute( QStringLiteral( "zmax" ) ) );
1685  settings.setValue( QStringLiteral( "authcfg" ), child.attribute( QStringLiteral( "authcfg" ) ) );
1686  settings.setValue( QStringLiteral( "username" ), child.attribute( QStringLiteral( "username" ) ) );
1687  settings.setValue( QStringLiteral( "password" ), child.attribute( QStringLiteral( "password" ) ) );
1688  settings.setValue( QStringLiteral( "referer" ), child.attribute( QStringLiteral( "referer" ) ) );
1689  settings.setValue( QStringLiteral( "tilePixelRatio" ), child.attribute( QStringLiteral( "tilePixelRatio" ) ) );
1690  settings.endGroup();
1691 
1692  child = child.nextSiblingElement();
1693  }
1694 }
1695 
1696 void QgsManageConnectionsDialog::loadArcgisConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
1697 {
1698  const QDomElement root = doc.documentElement();
1699  if ( root.tagName() != "qgs" + service.toUpper() + "Connections" )
1700  {
1701  QMessageBox::information( this, tr( "Loading Connections" ),
1702  tr( "The file is not a %1 connections exchange file." ).arg( service ) );
1703  return;
1704  }
1705 
1706  QString connectionName;
1707  QgsSettings settings;
1708  settings.beginGroup( "/qgis/connections-" + service.toLower() );
1709  QStringList keys = settings.childGroups();
1710  settings.endGroup();
1711  QDomElement child = root.firstChildElement();
1712  bool prompt = true;
1713  bool overwrite = true;
1714 
1715  while ( !child.isNull() )
1716  {
1717  connectionName = child.attribute( QStringLiteral( "name" ) );
1718  if ( !items.contains( connectionName ) )
1719  {
1720  child = child.nextSiblingElement();
1721  continue;
1722  }
1723 
1724  // check for duplicates
1725  if ( keys.contains( connectionName ) && prompt )
1726  {
1727  const int res = QMessageBox::warning( this,
1728  tr( "Loading Connections" ),
1729  tr( "Connection with name '%1' already exists. Overwrite?" )
1730  .arg( connectionName ),
1731  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1732 
1733  switch ( res )
1734  {
1735  case QMessageBox::Cancel:
1736  return;
1737  case QMessageBox::No:
1738  child = child.nextSiblingElement();
1739  continue;
1740  case QMessageBox::Yes:
1741  overwrite = true;
1742  break;
1743  case QMessageBox::YesToAll:
1744  prompt = false;
1745  overwrite = true;
1746  break;
1747  case QMessageBox::NoToAll:
1748  prompt = false;
1749  overwrite = false;
1750  break;
1751  }
1752  }
1753 
1754  if ( keys.contains( connectionName ) )
1755  {
1756  if ( !overwrite )
1757  {
1758  child = child.nextSiblingElement();
1759  continue;
1760  }
1761  }
1762  else
1763  {
1764  keys << connectionName;
1765  }
1766 
1767  // no dups detected or overwrite is allowed
1768  settings.beginGroup( "/qgis/connections-" + service.toLower() );
1769  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
1770  settings.setValue( QString( '/' + connectionName + "/referer" ), child.attribute( QStringLiteral( "referer" ) ) );
1771  settings.endGroup();
1772 
1773  settings.beginGroup( "/qgis/" + service.toUpper() + '/' + connectionName );
1774  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1775  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1776  settings.setValue( QStringLiteral( "/authcfg" ), child.attribute( QStringLiteral( "authcfg" ) ) );
1777  settings.endGroup();
1778 
1779  child = child.nextSiblingElement();
1780  }
1781 }
1782 
1783 void QgsManageConnectionsDialog::loadVectorTileConnections( const QDomDocument &doc, const QStringList &items )
1784 {
1785  const QDomElement root = doc.documentElement();
1786  if ( root.tagName() != QLatin1String( "qgsVectorTileConnections" ) )
1787  {
1788  QMessageBox::information( this, tr( "Loading Connections" ),
1789  tr( "The file is not a Vector Tile connections exchange file." ) );
1790  return;
1791  }
1792 
1793  QString connectionName;
1794  QgsSettings settings;
1795  settings.beginGroup( QStringLiteral( "/qgis/connections-vector-tile" ) );
1796  QStringList keys = settings.childGroups();
1797  settings.endGroup();
1798  QDomElement child = root.firstChildElement();
1799  bool prompt = true;
1800  bool overwrite = true;
1801 
1802  while ( !child.isNull() )
1803  {
1804  connectionName = child.attribute( QStringLiteral( "name" ) );
1805  if ( !items.contains( connectionName ) )
1806  {
1807  child = child.nextSiblingElement();
1808  continue;
1809  }
1810 
1811  // check for duplicates
1812  if ( keys.contains( connectionName ) && prompt )
1813  {
1814  const int res = QMessageBox::warning( this,
1815  tr( "Loading Connections" ),
1816  tr( "Connection with name '%1' already exists. Overwrite?" )
1817  .arg( connectionName ),
1818  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1819 
1820  switch ( res )
1821  {
1822  case QMessageBox::Cancel:
1823  return;
1824  case QMessageBox::No:
1825  child = child.nextSiblingElement();
1826  continue;
1827  case QMessageBox::Yes:
1828  overwrite = true;
1829  break;
1830  case QMessageBox::YesToAll:
1831  prompt = false;
1832  overwrite = true;
1833  break;
1834  case QMessageBox::NoToAll:
1835  prompt = false;
1836  overwrite = false;
1837  break;
1838  }
1839  }
1840 
1841  if ( keys.contains( connectionName ) )
1842  {
1843  if ( !overwrite )
1844  {
1845  child = child.nextSiblingElement();
1846  continue;
1847  }
1848  }
1849  else
1850  {
1851  keys << connectionName;
1852  }
1853 
1854  settings.beginGroup( "qgis/connections-vector-tile/" + connectionName );
1855  settings.setValue( QStringLiteral( "url" ), child.attribute( QStringLiteral( "url" ) ) );
1856  settings.setValue( QStringLiteral( "zmin" ), child.attribute( QStringLiteral( "zmin" ) ) );
1857  settings.setValue( QStringLiteral( "zmax" ), child.attribute( QStringLiteral( "zmax" ) ) );
1858  settings.setValue( QStringLiteral( "serviceType" ), child.attribute( QStringLiteral( "serviceType" ) ) );
1859  settings.setValue( QStringLiteral( "authcfg" ), child.attribute( QStringLiteral( "authcfg" ) ) );
1860  settings.setValue( QStringLiteral( "username" ), child.attribute( QStringLiteral( "username" ) ) );
1861  settings.setValue( QStringLiteral( "password" ), child.attribute( QStringLiteral( "password" ) ) );
1862  settings.setValue( QStringLiteral( "referer" ), child.attribute( QStringLiteral( "referer" ) ) );
1863  settings.setValue( QStringLiteral( "styleUrl" ), child.attribute( QStringLiteral( "styleUrl" ) ) );
1864 
1865  settings.endGroup();
1866 
1867  child = child.nextSiblingElement();
1868  }
1869 }
1870 
1872 {
1873  listConnections->selectAll();
1874  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !listConnections->selectedItems().isEmpty() );
1875 }
1876 
1878 {
1879  listConnections->clearSelection();
1880  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
1881 }
QgsManageConnectionsDialog(QWidget *parent=nullptr, Mode mode=Export, Type type=WMS, const QString &fileName=QString())
Constructor for QgsManageConnectionsDialog.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
void endGroup()
Resets the group to what it was before the corresponding beginGroup() call.
Definition: qgssettings.cpp:99
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:89
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.