QGIS API Documentation  3.13.0-Master (740be229cb)
qgsmanageconnectionsdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmanageconnectionsdialog.cpp
3  ---------------------
4  begin : Dec 2009
5  copyright : (C) 2009 by Alexander Bruy
6  email : alexander dot bruy at gmail dot com
7 
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include <QCloseEvent>
18 #include <QFileDialog>
19 #include <QMessageBox>
20 #include <QPushButton>
21 #include <QTextStream>
22 
23 #include "qgssettings.h"
25 
26 
27 QgsManageConnectionsDialog::QgsManageConnectionsDialog( QWidget *parent, Mode mode, Type type, const QString &fileName )
28  : QDialog( parent )
29  , mFileName( fileName )
30  , mDialogMode( mode )
31  , mConnectionType( type )
32 {
33  setupUi( this );
34 
35  // additional buttons
36  QPushButton *pb = nullptr;
37  pb = new QPushButton( tr( "Select all" ) );
38  buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
39  connect( pb, &QAbstractButton::clicked, this, &QgsManageConnectionsDialog::selectAll );
40 
41  pb = new QPushButton( tr( "Clear selection" ) );
42  buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
43  connect( pb, &QAbstractButton::clicked, this, &QgsManageConnectionsDialog::clearSelection );
44 
45  if ( mDialogMode == Import )
46  {
47  label->setText( tr( "Select connections to import" ) );
48  buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Import" ) );
49  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
50  }
51  else
52  {
53  //label->setText( tr( "Select connections to export" ) );
54  buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Export" ) );
55  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
56  }
57 
58  if ( !populateConnections() )
59  {
60  QApplication::postEvent( this, new QCloseEvent() );
61  }
62 
63  // use OK button for starting import and export operations
64  disconnect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
65  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsManageConnectionsDialog::doExportImport );
66 
67  connect( listConnections, &QListWidget::itemSelectionChanged, this, &QgsManageConnectionsDialog::selectionChanged );
68 }
69 
71 {
72  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !listConnections->selectedItems().isEmpty() );
73 }
74 
76 {
77  QList<QListWidgetItem *> selection = listConnections->selectedItems();
78  if ( selection.isEmpty() )
79  {
80  QMessageBox::warning( this, tr( "Export/Import Error" ),
81  tr( "You should select at least one connection from list." ) );
82  return;
83  }
84 
85  QStringList items;
86  items.reserve( selection.size() );
87  for ( int i = 0; i < selection.size(); ++i )
88  {
89  items.append( selection.at( i )->text() );
90  }
91 
92  if ( mDialogMode == Export )
93  {
94  QString fileName = QFileDialog::getSaveFileName( this, tr( "Save Connections" ), QDir::homePath(),
95  tr( "XML files (*.xml *.XML)" ) );
96  if ( fileName.isEmpty() )
97  {
98  return;
99  }
100 
101  // ensure the user never omitted the extension from the file name
102  if ( !fileName.endsWith( QLatin1String( ".xml" ), Qt::CaseInsensitive ) )
103  {
104  fileName += QLatin1String( ".xml" );
105  }
106 
107  mFileName = fileName;
108 
109  QDomDocument doc;
110  switch ( mConnectionType )
111  {
112  case WMS:
113  doc = saveOWSConnections( items, QStringLiteral( "WMS" ) );
114  break;
115  case WFS:
116  doc = saveWfsConnections( items );
117  break;
118  case PostGIS:
119  doc = savePgConnections( items );
120  break;
121  case MSSQL:
122  doc = saveMssqlConnections( items );
123  break;
124  case WCS:
125  doc = saveOWSConnections( items, QStringLiteral( "WCS" ) );
126  break;
127  case Oracle:
128  doc = saveOracleConnections( items );
129  break;
130  case DB2:
131  doc = saveDb2Connections( items );
132  break;
133  case GeoNode:
134  doc = saveGeonodeConnections( items );
135  break;
136  case XyzTiles:
137  doc = saveXyzTilesConnections( items );
138  break;
139  }
140 
141  QFile file( mFileName );
142  if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
143  {
144  QMessageBox::warning( this, tr( "Saving Connections" ),
145  tr( "Cannot write file %1:\n%2." )
146  .arg( mFileName,
147  file.errorString() ) );
148  return;
149  }
150 
151  QTextStream out( &file );
152  doc.save( out, 4 );
153  }
154  else // import connections
155  {
156  QFile file( mFileName );
157  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
158  {
159  QMessageBox::warning( this, tr( "Loading Connections" ),
160  tr( "Cannot read file %1:\n%2." )
161  .arg( mFileName,
162  file.errorString() ) );
163  return;
164  }
165 
166  QDomDocument doc;
167  QString errorStr;
168  int errorLine;
169  int errorColumn;
170 
171  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
172  {
173  QMessageBox::warning( this, tr( "Loading Connections" ),
174  tr( "Parse error at line %1, column %2:\n%3" )
175  .arg( errorLine )
176  .arg( errorColumn )
177  .arg( errorStr ) );
178  return;
179  }
180 
181  switch ( mConnectionType )
182  {
183  case WMS:
184  loadOWSConnections( doc, items, QStringLiteral( "WMS" ) );
185  break;
186  case WFS:
187  loadWfsConnections( doc, items );
188  break;
189  case PostGIS:
190  loadPgConnections( doc, items );
191  break;
192  case MSSQL:
193  loadMssqlConnections( doc, items );
194  break;
195  case WCS:
196  loadOWSConnections( doc, items, QStringLiteral( "WCS" ) );
197  break;
198  case Oracle:
199  loadOracleConnections( doc, items );
200  break;
201  case DB2:
202  loadDb2Connections( doc, items );
203  break;
204  case GeoNode:
205  loadGeonodeConnections( doc, items );
206  break;
207  case XyzTiles:
208  loadXyzTilesConnections( doc, items );
209  break;
210  }
211  // clear connections list and close window
212  listConnections->clear();
213  accept();
214  }
215 
216  mFileName.clear();
217 }
218 
219 bool QgsManageConnectionsDialog::populateConnections()
220 {
221  // Export mode. Populate connections list from settings
222  if ( mDialogMode == Export )
223  {
224  QgsSettings settings;
225  switch ( mConnectionType )
226  {
227  case WMS:
228  settings.beginGroup( QStringLiteral( "/qgis/connections-wms" ) );
229  break;
230  case WFS:
231  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
232  break;
233  case WCS:
234  settings.beginGroup( QStringLiteral( "/qgis/connections-wcs" ) );
235  break;
236  case PostGIS:
237  settings.beginGroup( QStringLiteral( "/PostgreSQL/connections" ) );
238  break;
239  case MSSQL:
240  settings.beginGroup( QStringLiteral( "/MSSQL/connections" ) );
241  break;
242  case Oracle:
243  settings.beginGroup( QStringLiteral( "/Oracle/connections" ) );
244  break;
245  case DB2:
246  settings.beginGroup( QStringLiteral( "/DB2/connections" ) );
247  break;
248  case GeoNode:
249  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
250  break;
251  case XyzTiles:
252  settings.beginGroup( QStringLiteral( "/qgis/connections-xyz" ) );
253  break;
254  }
255  QStringList keys = settings.childGroups();
256  QStringList::Iterator it = keys.begin();
257  while ( it != keys.end() )
258  {
259  QListWidgetItem *item = new QListWidgetItem();
260  item->setText( *it );
261  listConnections->addItem( item );
262  ++it;
263  }
264  settings.endGroup();
265  }
266  // Import mode. Populate connections list from file
267  else
268  {
269  QFile file( mFileName );
270  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
271  {
272  QMessageBox::warning( this, tr( "Loading Connections" ),
273  tr( "Cannot read file %1:\n%2." )
274  .arg( mFileName,
275  file.errorString() ) );
276  return false;
277  }
278 
279  QDomDocument doc;
280  QString errorStr;
281  int errorLine;
282  int errorColumn;
283 
284  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
285  {
286  QMessageBox::warning( this, tr( "Loading Connections" ),
287  tr( "Parse error at line %1, column %2:\n%3" )
288  .arg( errorLine )
289  .arg( errorColumn )
290  .arg( errorStr ) );
291  return false;
292  }
293 
294  QDomElement root = doc.documentElement();
295  switch ( mConnectionType )
296  {
297  case WMS:
298  if ( root.tagName() != QLatin1String( "qgsWMSConnections" ) )
299  {
300  QMessageBox::information( this, tr( "Loading Connections" ),
301  tr( "The file is not a WMS connections exchange file." ) );
302  return false;
303  }
304  break;
305 
306  case WFS:
307  if ( root.tagName() != QLatin1String( "qgsWFSConnections" ) )
308  {
309  QMessageBox::information( this, tr( "Loading Connections" ),
310  tr( "The file is not a WFS connections exchange file." ) );
311  return false;
312  }
313  break;
314 
315  case WCS:
316  if ( root.tagName() != QLatin1String( "qgsWCSConnections" ) )
317  {
318  QMessageBox::information( this, tr( "Loading Connections" ),
319  tr( "The file is not a WCS connections exchange file." ) );
320  return false;
321  }
322  break;
323 
324  case PostGIS:
325  if ( root.tagName() != QLatin1String( "qgsPgConnections" ) )
326  {
327  QMessageBox::information( this, tr( "Loading Connections" ),
328  tr( "The file is not a PostGIS connections exchange file." ) );
329  return false;
330  }
331  break;
332 
333  case MSSQL:
334  if ( root.tagName() != QLatin1String( "qgsMssqlConnections" ) )
335  {
336  QMessageBox::information( this, tr( "Loading Connections" ),
337  tr( "The file is not a MSSQL connections exchange file." ) );
338  return false;
339  }
340  break;
341  case Oracle:
342  if ( root.tagName() != QLatin1String( "qgsOracleConnections" ) )
343  {
344  QMessageBox::information( this, tr( "Loading Connections" ),
345  tr( "The file is not an Oracle connections exchange file." ) );
346  return false;
347  }
348  break;
349  case DB2:
350  if ( root.tagName() != QLatin1String( "qgsDb2Connections" ) )
351  {
352  QMessageBox::information( this, tr( "Loading Connections" ),
353  tr( "The file is not a DB2 connections exchange file." ) );
354  return false;
355  }
356  break;
357  case GeoNode:
358  if ( root.tagName() != QLatin1String( "qgsGeoNodeConnections" ) )
359  {
360  QMessageBox::information( this, tr( "Loading Connections" ),
361  tr( "The file is not a GeoNode connections exchange file." ) );
362  return false;
363  }
364  break;
365  case XyzTiles:
366  if ( root.tagName() != QLatin1String( "qgsXYZTilesConnections" ) )
367  {
368  QMessageBox::information( this, tr( "Loading Connections" ),
369  tr( "The file is not a XYZ Tiles connections exchange file." ) );
370  return false;
371  }
372  break;
373  }
374 
375  QDomElement child = root.firstChildElement();
376  while ( !child.isNull() )
377  {
378  QListWidgetItem *item = new QListWidgetItem();
379  item->setText( child.attribute( QStringLiteral( "name" ) ) );
380  listConnections->addItem( item );
381  child = child.nextSiblingElement();
382  }
383  }
384  return true;
385 }
386 
387 QDomDocument QgsManageConnectionsDialog::saveOWSConnections( const QStringList &connections, const QString &service )
388 {
389  QDomDocument doc( QStringLiteral( "connections" ) );
390  QDomElement root = doc.createElement( "qgs" + service.toUpper() + "Connections" );
391  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
392  doc.appendChild( root );
393 
394  QgsSettings settings;
395  QString path;
396  for ( int i = 0; i < connections.count(); ++i )
397  {
398  path = "/qgis/connections-" + service.toLower() + '/';
399  QDomElement el = doc.createElement( service.toLower() );
400  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
401  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url", "" ).toString() );
402 
403  if ( service == QLatin1String( "WMS" ) )
404  {
405  el.setAttribute( QStringLiteral( "ignoreGetMapURI" ), settings.value( path + connections[i] + "/ignoreGetMapURI", false ).toBool() ? "true" : "false" );
406  el.setAttribute( QStringLiteral( "ignoreGetFeatureInfoURI" ), settings.value( path + connections[i] + "/ignoreGetFeatureInfoURI", false ).toBool() ? "true" : "false" );
407  el.setAttribute( QStringLiteral( "ignoreAxisOrientation" ), settings.value( path + connections[i] + "/ignoreAxisOrientation", false ).toBool() ? "true" : "false" );
408  el.setAttribute( QStringLiteral( "invertAxisOrientation" ), settings.value( path + connections[i] + "/invertAxisOrientation", false ).toBool() ? "true" : "false" );
409  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + connections[ i ] + "/referer", "" ).toString() );
410  el.setAttribute( QStringLiteral( "smoothPixmapTransform" ), settings.value( path + connections[i] + "/smoothPixmapTransform", false ).toBool() ? "true" : "false" );
411  el.setAttribute( QStringLiteral( "dpiMode" ), settings.value( path + connections[i] + "/dpiMode", "7" ).toInt() );
412  }
413 
414  path = "/qgis/" + service.toUpper() + '/';
415  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username", "" ).toString() );
416  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password", "" ).toString() );
417  root.appendChild( el );
418  }
419 
420  return doc;
421 }
422 
423 QDomDocument QgsManageConnectionsDialog::saveWfsConnections( const QStringList &connections )
424 {
425  QDomDocument doc( QStringLiteral( "connections" ) );
426  QDomElement root = doc.createElement( QStringLiteral( "qgsWFSConnections" ) );
427  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
428  doc.appendChild( root );
429 
430  QgsSettings settings;
431  QString path;
432  for ( int i = 0; i < connections.count(); ++i )
433  {
434  path = QStringLiteral( "/qgis/connections-wfs/" );
435  QDomElement el = doc.createElement( QStringLiteral( "wfs" ) );
436  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
437  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url", "" ).toString() );
438 
439  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + connections[ i ] + "/referer", "" ).toString() );
440 
441  path = QStringLiteral( "/qgis/WFS/" );
442  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username", "" ).toString() );
443  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password", "" ).toString() );
444  root.appendChild( el );
445  }
446 
447  return doc;
448 }
449 
450 QDomDocument QgsManageConnectionsDialog::savePgConnections( const QStringList &connections )
451 {
452  QDomDocument doc( QStringLiteral( "connections" ) );
453  QDomElement root = doc.createElement( QStringLiteral( "qgsPgConnections" ) );
454  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
455  doc.appendChild( root );
456 
457  QgsSettings settings;
458  QString path;
459  for ( int i = 0; i < connections.count(); ++i )
460  {
461  path = "/PostgreSQL/connections/" + connections[ i ];
462  QDomElement el = doc.createElement( QStringLiteral( "postgis" ) );
463  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
464  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host", "" ).toString() );
465  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port", "" ).toString() );
466  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database", "" ).toString() );
467  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service", "" ).toString() );
468  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
469  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
470 
471  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
472 
473  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
474  {
475  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", "" ).toString() );
476  }
477 
478  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
479 
480  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
481  {
482  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", "" ).toString() );
483  }
484 
485  root.appendChild( el );
486  }
487 
488  return doc;
489 }
490 
491 QDomDocument QgsManageConnectionsDialog::saveMssqlConnections( const QStringList &connections )
492 {
493  QDomDocument doc( QStringLiteral( "connections" ) );
494  QDomElement root = doc.createElement( QStringLiteral( "qgsMssqlConnections" ) );
495  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
496  doc.appendChild( root );
497 
498  QgsSettings settings;
499  QString path;
500  for ( int i = 0; i < connections.count(); ++i )
501  {
502  path = "/MSSQL/connections/" + connections[ i ];
503  QDomElement el = doc.createElement( QStringLiteral( "mssql" ) );
504  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
505  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host", "" ).toString() );
506  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port", "" ).toString() );
507  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database", "" ).toString() );
508  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service", "" ).toString() );
509  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
510  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
511 
512  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
513 
514  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
515  {
516  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", "" ).toString() );
517  }
518 
519  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
520 
521  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
522  {
523  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", "" ).toString() );
524  }
525 
526  root.appendChild( el );
527  }
528 
529  return doc;
530 }
531 
532 QDomDocument QgsManageConnectionsDialog::saveOracleConnections( const QStringList &connections )
533 {
534  QDomDocument doc( QStringLiteral( "connections" ) );
535  QDomElement root = doc.createElement( QStringLiteral( "qgsOracleConnections" ) );
536  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
537  doc.appendChild( root );
538 
539  QgsSettings settings;
540  QString path;
541  for ( int i = 0; i < connections.count(); ++i )
542  {
543  path = "/Oracle/connections/" + connections[ i ];
544  QDomElement el = doc.createElement( QStringLiteral( "oracle" ) );
545  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
546  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host", "" ).toString() );
547  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port", "" ).toString() );
548  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database", "" ).toString() );
549  el.setAttribute( QStringLiteral( "dboptions" ), settings.value( path + "/dboptions", "" ).toString() );
550  el.setAttribute( QStringLiteral( "dbworkspace" ), settings.value( path + "/dbworkspace", "" ).toString() );
551  el.setAttribute( QStringLiteral( "schema" ), settings.value( path + "/schema", QString() ).toString() );
552  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
553  el.setAttribute( QStringLiteral( "userTablesOnly" ), settings.value( path + "/userTablesOnly", "0" ).toString() );
554  el.setAttribute( QStringLiteral( "geometryColumnsOnly" ), settings.value( path + "/geometryColumnsOnly", "0" ).toString() );
555  el.setAttribute( QStringLiteral( "allowGeometrylessTables" ), settings.value( path + "/allowGeometrylessTables", "0" ).toString() );
556 
557  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
558 
559  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
560  {
561  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", "" ).toString() );
562  }
563 
564  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
565 
566  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
567  {
568  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", "" ).toString() );
569  }
570 
571  root.appendChild( el );
572  }
573 
574  return doc;
575 }
576 
577 QDomDocument QgsManageConnectionsDialog::saveDb2Connections( const QStringList &connections )
578 {
579  QDomDocument doc( QStringLiteral( "connections" ) );
580  QDomElement root = doc.createElement( QStringLiteral( "qgsDb2Connections" ) );
581  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
582  doc.appendChild( root );
583 
584  QgsSettings settings;
585  QString path;
586  for ( int i = 0; i < connections.count(); ++i )
587  {
588  path = "/DB2/connections/" + connections[ i ];
589  QDomElement el = doc.createElement( QStringLiteral( "db2" ) );
590  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
591  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host", "" ).toString() );
592  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port", "" ).toString() );
593  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database", "" ).toString() );
594  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service", "" ).toString() );
595  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
596  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
597 
598  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
599 
600  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
601  {
602  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", "" ).toString() );
603  }
604 
605  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
606 
607  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
608  {
609  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", "" ).toString() );
610  }
611 
612  root.appendChild( el );
613  }
614 
615  return doc;
616 }
617 
618 QDomDocument QgsManageConnectionsDialog::saveGeonodeConnections( const QStringList &connections )
619 {
620  QDomDocument doc( QStringLiteral( "connections" ) );
621  QDomElement root = doc.createElement( QStringLiteral( "qgsGeoNodeConnections" ) );
622  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
623  doc.appendChild( root );
624 
625  QgsSettings settings;
626  QString path;
627  for ( int i = 0; i < connections.count(); ++i )
628  {
629  path = QStringLiteral( "/qgis/connections-geonode/" );
630  QDomElement el = doc.createElement( QStringLiteral( "geonode" ) );
631  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
632  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url", "" ).toString() );
633 
634  path = QStringLiteral( "/qgis/GeoNode/" );
635  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username", "" ).toString() );
636  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password", "" ).toString() );
637  root.appendChild( el );
638  }
639 
640  return doc;
641 }
642 
643 QDomDocument QgsManageConnectionsDialog::saveXyzTilesConnections( const QStringList &connections )
644 {
645  QDomDocument doc( QStringLiteral( "connections" ) );
646  QDomElement root = doc.createElement( QStringLiteral( "qgsXYZTilesConnections" ) );
647  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
648  doc.appendChild( root );
649 
650  QgsSettings settings;
651  QString path;
652  for ( int i = 0; i < connections.count(); ++i )
653  {
654  path = "qgis/connections-xyz/" + connections[ i ];
655  QDomElement el = doc.createElement( QStringLiteral( "xyztiles" ) );
656 
657  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
658  el.setAttribute( QStringLiteral( "url" ), settings.value( path + "/url", "" ).toString() );
659  el.setAttribute( QStringLiteral( "zmin" ), settings.value( path + "/zmin", -1 ).toInt() );
660  el.setAttribute( QStringLiteral( "zmax" ), settings.value( path + "/zmax", -1 ).toInt() );
661  el.setAttribute( QStringLiteral( "authcfg" ), settings.value( path + "/authcfg", "" ).toString() );
662  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", "" ).toString() );
663  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", "" ).toString() );
664  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + "/referer", "" ).toString() );
665 
666  root.appendChild( el );
667  }
668 
669  return doc;
670 }
671 
672 void QgsManageConnectionsDialog::loadOWSConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
673 {
674  QDomElement root = doc.documentElement();
675  if ( root.tagName() != "qgs" + service.toUpper() + "Connections" )
676  {
677  QMessageBox::information( this, tr( "Loading Connections" ),
678  tr( "The file is not a %1 connections exchange file." ).arg( service ) );
679  return;
680  }
681 
682  QString connectionName;
683  QgsSettings settings;
684  settings.beginGroup( "/qgis/connections-" + service.toLower() );
685  QStringList keys = settings.childGroups();
686  settings.endGroup();
687  QDomElement child = root.firstChildElement();
688  bool prompt = true;
689  bool overwrite = true;
690 
691  while ( !child.isNull() )
692  {
693  connectionName = child.attribute( QStringLiteral( "name" ) );
694  if ( !items.contains( connectionName ) )
695  {
696  child = child.nextSiblingElement();
697  continue;
698  }
699 
700  // check for duplicates
701  if ( keys.contains( connectionName ) && prompt )
702  {
703  int res = QMessageBox::warning( this,
704  tr( "Loading Connections" ),
705  tr( "Connection with name '%1' already exists. Overwrite?" )
706  .arg( connectionName ),
707  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
708 
709  switch ( res )
710  {
711  case QMessageBox::Cancel:
712  return;
713  case QMessageBox::No:
714  child = child.nextSiblingElement();
715  continue;
716  case QMessageBox::Yes:
717  overwrite = true;
718  break;
719  case QMessageBox::YesToAll:
720  prompt = false;
721  overwrite = true;
722  break;
723  case QMessageBox::NoToAll:
724  prompt = false;
725  overwrite = false;
726  break;
727  }
728  }
729 
730  if ( keys.contains( connectionName ) && !overwrite )
731  {
732  child = child.nextSiblingElement();
733  continue;
734  }
735 
736  // no dups detected or overwrite is allowed
737  settings.beginGroup( "/qgis/connections-" + service.toLower() );
738  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
739  settings.setValue( QString( '/' + connectionName + "/ignoreGetMapURI" ), child.attribute( QStringLiteral( "ignoreGetMapURI" ) ) == QLatin1String( "true" ) );
740  settings.setValue( QString( '/' + connectionName + "/ignoreGetFeatureInfoURI" ), child.attribute( QStringLiteral( "ignoreGetFeatureInfoURI" ) ) == QLatin1String( "true" ) );
741  settings.setValue( QString( '/' + connectionName + "/ignoreAxisOrientation" ), child.attribute( QStringLiteral( "ignoreAxisOrientation" ) ) == QLatin1String( "true" ) );
742  settings.setValue( QString( '/' + connectionName + "/invertAxisOrientation" ), child.attribute( QStringLiteral( "invertAxisOrientation" ) ) == QLatin1String( "true" ) );
743  settings.setValue( QString( '/' + connectionName + "/referer" ), child.attribute( QStringLiteral( "referer" ) ) );
744  settings.setValue( QString( '/' + connectionName + "/smoothPixmapTransform" ), child.attribute( QStringLiteral( "smoothPixmapTransform" ) ) == QLatin1String( "true" ) );
745  settings.setValue( QString( '/' + connectionName + "/dpiMode" ), child.attribute( QStringLiteral( "dpiMode" ), QStringLiteral( "7" ) ).toInt() );
746  settings.endGroup();
747 
748  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
749  {
750  settings.beginGroup( "/qgis/" + service.toUpper() + '/' + connectionName );
751  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
752  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
753  settings.endGroup();
754  }
755  child = child.nextSiblingElement();
756  }
757 }
758 
759 void QgsManageConnectionsDialog::loadWfsConnections( const QDomDocument &doc, const QStringList &items )
760 {
761  QDomElement root = doc.documentElement();
762  if ( root.tagName() != QLatin1String( "qgsWFSConnections" ) )
763  {
764  QMessageBox::information( this, tr( "Loading Connections" ),
765  tr( "The file is not a WFS connections exchange file." ) );
766  return;
767  }
768 
769  QString connectionName;
770  QgsSettings settings;
771  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
772  QStringList keys = settings.childGroups();
773  settings.endGroup();
774  QDomElement child = root.firstChildElement();
775  bool prompt = true;
776  bool overwrite = true;
777 
778  while ( !child.isNull() )
779  {
780  connectionName = child.attribute( QStringLiteral( "name" ) );
781  if ( !items.contains( connectionName ) )
782  {
783  child = child.nextSiblingElement();
784  continue;
785  }
786 
787  // check for duplicates
788  if ( keys.contains( connectionName ) && prompt )
789  {
790  int res = QMessageBox::warning( this,
791  tr( "Loading Connections" ),
792  tr( "Connection with name '%1' already exists. Overwrite?" )
793  .arg( connectionName ),
794  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
795 
796  switch ( res )
797  {
798  case QMessageBox::Cancel:
799  return;
800  case QMessageBox::No:
801  child = child.nextSiblingElement();
802  continue;
803  case QMessageBox::Yes:
804  overwrite = true;
805  break;
806  case QMessageBox::YesToAll:
807  prompt = false;
808  overwrite = true;
809  break;
810  case QMessageBox::NoToAll:
811  prompt = false;
812  overwrite = false;
813  break;
814  }
815  }
816 
817  if ( keys.contains( connectionName ) && !overwrite )
818  {
819  child = child.nextSiblingElement();
820  continue;
821  }
822 
823  // no dups detected or overwrite is allowed
824  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
825  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
826  settings.endGroup();
827 
828  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
829  {
830  settings.beginGroup( "/qgis/WFS/" + connectionName );
831  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
832  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
833  settings.endGroup();
834  }
835  child = child.nextSiblingElement();
836  }
837 }
838 
839 
840 void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, const QStringList &items )
841 {
842  QDomElement root = doc.documentElement();
843  if ( root.tagName() != QLatin1String( "qgsPgConnections" ) )
844  {
845  QMessageBox::information( this,
846  tr( "Loading Connections" ),
847  tr( "The file is not a PostGIS connections exchange file." ) );
848  return;
849  }
850 
851  QString connectionName;
852  QgsSettings settings;
853  settings.beginGroup( QStringLiteral( "/PostgreSQL/connections" ) );
854  QStringList keys = settings.childGroups();
855  settings.endGroup();
856  QDomElement child = root.firstChildElement();
857  bool prompt = true;
858  bool overwrite = true;
859 
860  while ( !child.isNull() )
861  {
862  connectionName = child.attribute( QStringLiteral( "name" ) );
863  if ( !items.contains( connectionName ) )
864  {
865  child = child.nextSiblingElement();
866  continue;
867  }
868 
869  // check for duplicates
870  if ( keys.contains( connectionName ) && prompt )
871  {
872  int res = QMessageBox::warning( this,
873  tr( "Loading Connections" ),
874  tr( "Connection with name '%1' already exists. Overwrite?" )
875  .arg( connectionName ),
876  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
877  switch ( res )
878  {
879  case QMessageBox::Cancel:
880  return;
881  case QMessageBox::No:
882  child = child.nextSiblingElement();
883  continue;
884  case QMessageBox::Yes:
885  overwrite = true;
886  break;
887  case QMessageBox::YesToAll:
888  prompt = false;
889  overwrite = true;
890  break;
891  case QMessageBox::NoToAll:
892  prompt = false;
893  overwrite = false;
894  break;
895  }
896  }
897 
898  if ( keys.contains( connectionName ) && !overwrite )
899  {
900  child = child.nextSiblingElement();
901  continue;
902  }
903 
904  //no dups detected or overwrite is allowed
905  settings.beginGroup( "/PostgreSQL/connections/" + connectionName );
906 
907  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
908  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
909  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
910  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
911  {
912  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
913  }
914  else
915  {
916  settings.setValue( QStringLiteral( "/service" ), "" );
917  }
918  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
919  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
920  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
921  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
922  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
923  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
924  settings.endGroup();
925 
926  child = child.nextSiblingElement();
927  }
928 }
929 
930 void QgsManageConnectionsDialog::loadMssqlConnections( const QDomDocument &doc, const QStringList &items )
931 {
932  QDomElement root = doc.documentElement();
933  if ( root.tagName() != QLatin1String( "qgsMssqlConnections" ) )
934  {
935  QMessageBox::information( this,
936  tr( "Loading Connections" ),
937  tr( "The file is not a MSSQL connections exchange file." ) );
938  return;
939  }
940 
941  QString connectionName;
942  QgsSettings settings;
943  settings.beginGroup( QStringLiteral( "/MSSQL/connections" ) );
944  QStringList keys = settings.childGroups();
945  settings.endGroup();
946  QDomElement child = root.firstChildElement();
947  bool prompt = true;
948  bool overwrite = true;
949 
950  while ( !child.isNull() )
951  {
952  connectionName = child.attribute( QStringLiteral( "name" ) );
953  if ( !items.contains( connectionName ) )
954  {
955  child = child.nextSiblingElement();
956  continue;
957  }
958 
959  // check for duplicates
960  if ( keys.contains( connectionName ) && prompt )
961  {
962  int res = QMessageBox::warning( this,
963  tr( "Loading Connections" ),
964  tr( "Connection with name '%1' already exists. Overwrite?" )
965  .arg( connectionName ),
966  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
967  switch ( res )
968  {
969  case QMessageBox::Cancel:
970  return;
971  case QMessageBox::No:
972  child = child.nextSiblingElement();
973  continue;
974  case QMessageBox::Yes:
975  overwrite = true;
976  break;
977  case QMessageBox::YesToAll:
978  prompt = false;
979  overwrite = true;
980  break;
981  case QMessageBox::NoToAll:
982  prompt = false;
983  overwrite = false;
984  break;
985  }
986  }
987 
988  if ( keys.contains( connectionName ) && !overwrite )
989  {
990  child = child.nextSiblingElement();
991  continue;
992  }
993 
994  //no dups detected or overwrite is allowed
995  settings.beginGroup( "/MSSQL/connections/" + connectionName );
996 
997  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
998  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
999  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1000  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1001  {
1002  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1003  }
1004  else
1005  {
1006  settings.setValue( QStringLiteral( "/service" ), "" );
1007  }
1008  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1009  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1010  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1011  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1012  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1013  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1014  settings.endGroup();
1015 
1016  child = child.nextSiblingElement();
1017  }
1018 }
1019 
1020 void QgsManageConnectionsDialog::loadOracleConnections( const QDomDocument &doc, const QStringList &items )
1021 {
1022  QDomElement root = doc.documentElement();
1023  if ( root.tagName() != QLatin1String( "qgsOracleConnections" ) )
1024  {
1025  QMessageBox::information( this,
1026  tr( "Loading Connections" ),
1027  tr( "The file is not an Oracle connections exchange file." ) );
1028  return;
1029  }
1030 
1031  QString connectionName;
1032  QgsSettings settings;
1033  settings.beginGroup( QStringLiteral( "/Oracle/connections" ) );
1034  QStringList keys = settings.childGroups();
1035  settings.endGroup();
1036  QDomElement child = root.firstChildElement();
1037  bool prompt = true;
1038  bool overwrite = true;
1039 
1040  while ( !child.isNull() )
1041  {
1042  connectionName = child.attribute( QStringLiteral( "name" ) );
1043  if ( !items.contains( connectionName ) )
1044  {
1045  child = child.nextSiblingElement();
1046  continue;
1047  }
1048 
1049  // check for duplicates
1050  if ( keys.contains( connectionName ) && prompt )
1051  {
1052  int res = QMessageBox::warning( this,
1053  tr( "Loading Connections" ),
1054  tr( "Connection with name '%1' already exists. Overwrite?" )
1055  .arg( connectionName ),
1056  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1057  switch ( res )
1058  {
1059  case QMessageBox::Cancel:
1060  return;
1061  case QMessageBox::No:
1062  child = child.nextSiblingElement();
1063  continue;
1064  case QMessageBox::Yes:
1065  overwrite = true;
1066  break;
1067  case QMessageBox::YesToAll:
1068  prompt = false;
1069  overwrite = true;
1070  break;
1071  case QMessageBox::NoToAll:
1072  prompt = false;
1073  overwrite = false;
1074  break;
1075  }
1076  }
1077 
1078  if ( keys.contains( connectionName ) && !overwrite )
1079  {
1080  child = child.nextSiblingElement();
1081  continue;
1082  }
1083 
1084  //no dups detected or overwrite is allowed
1085  settings.beginGroup( "/Oracle/connections/" + connectionName );
1086 
1087  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1088  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1089  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1090  settings.setValue( QStringLiteral( "/dboptions" ), child.attribute( QStringLiteral( "dboptions" ) ) );
1091  settings.setValue( QStringLiteral( "/dbworkspace" ), child.attribute( QStringLiteral( "dbworkspace" ) ) );
1092  settings.setValue( QStringLiteral( "/schema" ), child.attribute( QStringLiteral( "schema" ) ) );
1093  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1094  settings.setValue( QStringLiteral( "/userTablesOnly" ), child.attribute( QStringLiteral( "userTablesOnly" ) ) );
1095  settings.setValue( QStringLiteral( "/geometryColumnsOnly" ), child.attribute( QStringLiteral( "geometryColumnsOnly" ) ) );
1096  settings.setValue( QStringLiteral( "/allowGeometrylessTables" ), child.attribute( QStringLiteral( "allowGeometrylessTables" ) ) );
1097  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1098  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1099  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1100  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1101  settings.endGroup();
1102 
1103  child = child.nextSiblingElement();
1104  }
1105 }
1106 
1107 void QgsManageConnectionsDialog::loadDb2Connections( const QDomDocument &doc, const QStringList &items )
1108 {
1109  QDomElement root = doc.documentElement();
1110  if ( root.tagName() != QLatin1String( "qgsDb2Connections" ) )
1111  {
1112  QMessageBox::information( this,
1113  tr( "Loading Connections" ),
1114  tr( "The file is not a DB2 connections exchange file." ) );
1115  return;
1116  }
1117 
1118  QString connectionName;
1119  QgsSettings settings;
1120  settings.beginGroup( QStringLiteral( "/DB2/connections" ) );
1121  QStringList keys = settings.childGroups();
1122  settings.endGroup();
1123  QDomElement child = root.firstChildElement();
1124  bool prompt = true;
1125  bool overwrite = true;
1126 
1127  while ( !child.isNull() )
1128  {
1129  connectionName = child.attribute( QStringLiteral( "name" ) );
1130  if ( !items.contains( connectionName ) )
1131  {
1132  child = child.nextSiblingElement();
1133  continue;
1134  }
1135 
1136  // check for duplicates
1137  if ( keys.contains( connectionName ) && prompt )
1138  {
1139  int res = QMessageBox::warning( this,
1140  tr( "Loading Connections" ),
1141  tr( "Connection with name '%1' already exists. Overwrite?" )
1142  .arg( connectionName ),
1143  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1144  switch ( res )
1145  {
1146  case QMessageBox::Cancel:
1147  return;
1148  case QMessageBox::No:
1149  child = child.nextSiblingElement();
1150  continue;
1151  case QMessageBox::Yes:
1152  overwrite = true;
1153  break;
1154  case QMessageBox::YesToAll:
1155  prompt = false;
1156  overwrite = true;
1157  break;
1158  case QMessageBox::NoToAll:
1159  prompt = false;
1160  overwrite = false;
1161  break;
1162  }
1163  }
1164 
1165  if ( keys.contains( connectionName ) && !overwrite )
1166  {
1167  child = child.nextSiblingElement();
1168  continue;
1169  }
1170 
1171  //no dups detected or overwrite is allowed
1172  settings.beginGroup( "/DB2/connections/" + connectionName );
1173 
1174  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1175  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1176  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1177  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1178  {
1179  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1180  }
1181  else
1182  {
1183  settings.setValue( QStringLiteral( "/service" ), "" );
1184  }
1185  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1186  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1187  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1188  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1189  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1190  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1191  settings.endGroup();
1192 
1193  child = child.nextSiblingElement();
1194  }
1195 }
1196 
1197 void QgsManageConnectionsDialog::loadGeonodeConnections( const QDomDocument &doc, const QStringList &items )
1198 {
1199  QDomElement root = doc.documentElement();
1200  if ( root.tagName() != QLatin1String( "qgsGeoNodeConnections" ) )
1201  {
1202  QMessageBox::information( this, tr( "Loading Connections" ),
1203  tr( "The file is not a GeoNode connections exchange file." ) );
1204  return;
1205  }
1206 
1207  QString connectionName;
1208  QgsSettings settings;
1209  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
1210  QStringList keys = settings.childGroups();
1211  settings.endGroup();
1212  QDomElement child = root.firstChildElement();
1213  bool prompt = true;
1214  bool overwrite = true;
1215 
1216  while ( !child.isNull() )
1217  {
1218  connectionName = child.attribute( QStringLiteral( "name" ) );
1219  if ( !items.contains( connectionName ) )
1220  {
1221  child = child.nextSiblingElement();
1222  continue;
1223  }
1224 
1225  // check for duplicates
1226  if ( keys.contains( connectionName ) && prompt )
1227  {
1228  int res = QMessageBox::warning( this,
1229  tr( "Loading Connections" ),
1230  tr( "Connection with name '%1' already exists. Overwrite?" )
1231  .arg( connectionName ),
1232  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1233 
1234  switch ( res )
1235  {
1236  case QMessageBox::Cancel:
1237  return;
1238  case QMessageBox::No:
1239  child = child.nextSiblingElement();
1240  continue;
1241  case QMessageBox::Yes:
1242  overwrite = true;
1243  break;
1244  case QMessageBox::YesToAll:
1245  prompt = false;
1246  overwrite = true;
1247  break;
1248  case QMessageBox::NoToAll:
1249  prompt = false;
1250  overwrite = false;
1251  break;
1252  }
1253  }
1254 
1255  if ( keys.contains( connectionName ) && !overwrite )
1256  {
1257  child = child.nextSiblingElement();
1258  continue;
1259  }
1260 
1261  // no dups detected or overwrite is allowed
1262  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
1263  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
1264  settings.endGroup();
1265 
1266  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
1267  {
1268  settings.beginGroup( "/qgis/GeoNode/" + connectionName );
1269  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1270  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1271  settings.endGroup();
1272  }
1273  child = child.nextSiblingElement();
1274  }
1275 }
1276 
1277 void QgsManageConnectionsDialog::loadXyzTilesConnections( const QDomDocument &doc, const QStringList &items )
1278 {
1279  QDomElement root = doc.documentElement();
1280  if ( root.tagName() != QLatin1String( "qgsXYZTilesConnections" ) )
1281  {
1282  QMessageBox::information( this, tr( "Loading Connections" ),
1283  tr( "The file is not a XYZ Tiles connections exchange file." ) );
1284  return;
1285  }
1286 
1287  QString connectionName;
1288  QgsSettings settings;
1289  settings.beginGroup( QStringLiteral( "/qgis/connections-xyz" ) );
1290  QStringList keys = settings.childGroups();
1291  settings.endGroup();
1292  QDomElement child = root.firstChildElement();
1293  bool prompt = true;
1294  bool overwrite = true;
1295 
1296  while ( !child.isNull() )
1297  {
1298  connectionName = child.attribute( QStringLiteral( "name" ) );
1299  if ( !items.contains( connectionName ) )
1300  {
1301  child = child.nextSiblingElement();
1302  continue;
1303  }
1304 
1305  // check for duplicates
1306  if ( keys.contains( connectionName ) && prompt )
1307  {
1308  int res = QMessageBox::warning( this,
1309  tr( "Loading Connections" ),
1310  tr( "Connection with name '%1' already exists. Overwrite?" )
1311  .arg( connectionName ),
1312  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1313 
1314  switch ( res )
1315  {
1316  case QMessageBox::Cancel:
1317  return;
1318  case QMessageBox::No:
1319  child = child.nextSiblingElement();
1320  continue;
1321  case QMessageBox::Yes:
1322  overwrite = true;
1323  break;
1324  case QMessageBox::YesToAll:
1325  prompt = false;
1326  overwrite = true;
1327  break;
1328  case QMessageBox::NoToAll:
1329  prompt = false;
1330  overwrite = false;
1331  break;
1332  }
1333  }
1334 
1335  if ( keys.contains( connectionName ) && !overwrite )
1336  {
1337  child = child.nextSiblingElement();
1338  continue;
1339  }
1340 
1341  settings.beginGroup( "qgis/connections-xyz/" + connectionName );
1342  settings.setValue( QStringLiteral( "url" ), child.attribute( QStringLiteral( "url" ) ) );
1343  settings.setValue( QStringLiteral( "zmin" ), child.attribute( QStringLiteral( "zmin" ) ) );
1344  settings.setValue( QStringLiteral( "zmax" ), child.attribute( QStringLiteral( "zmax" ) ) );
1345  settings.setValue( QStringLiteral( "authcfg" ), child.attribute( QStringLiteral( "authcfg" ) ) );
1346  settings.setValue( QStringLiteral( "username" ), child.attribute( QStringLiteral( "username" ) ) );
1347  settings.setValue( QStringLiteral( "password" ), child.attribute( QStringLiteral( "password" ) ) );
1348  settings.setValue( QStringLiteral( "referer" ), child.attribute( QStringLiteral( "referer" ) ) );
1349  settings.endGroup();
1350 
1351  child = child.nextSiblingElement();
1352  }
1353 }
1354 
1355 
1357 {
1358  listConnections->selectAll();
1359  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !listConnections->selectedItems().isEmpty() );
1360 }
1361 
1363 {
1364  listConnections->clearSelection();
1365  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
1366 }
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:97
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsManageConnectionsDialog(QWidget *parent=nullptr, Mode mode=Export, Type type=WMS, const QString &fileName=QString())
Constructor for QgsManageConnectionsDialog.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:87
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.