QGIS API Documentation  2.99.0-Master (19b062c)
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  }
137 
138  QFile file( mFileName );
139  if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
140  {
141  QMessageBox::warning( this, tr( "Saving Connections" ),
142  tr( "Cannot write file %1:\n%2." )
143  .arg( mFileName,
144  file.errorString() ) );
145  return;
146  }
147 
148  QTextStream out( &file );
149  doc.save( out, 4 );
150  }
151  else // import connections
152  {
153  QFile file( mFileName );
154  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
155  {
156  QMessageBox::warning( this, tr( "Loading Connections" ),
157  tr( "Cannot read file %1:\n%2." )
158  .arg( mFileName,
159  file.errorString() ) );
160  return;
161  }
162 
163  QDomDocument doc;
164  QString errorStr;
165  int errorLine;
166  int errorColumn;
167 
168  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
169  {
170  QMessageBox::warning( this, tr( "Loading Connections" ),
171  tr( "Parse error at line %1, column %2:\n%3" )
172  .arg( errorLine )
173  .arg( errorColumn )
174  .arg( errorStr ) );
175  return;
176  }
177 
178  switch ( mConnectionType )
179  {
180  case WMS:
181  loadOWSConnections( doc, items, QStringLiteral( "WMS" ) );
182  break;
183  case WFS:
184  loadWfsConnections( doc, items );
185  break;
186  case PostGIS:
187  loadPgConnections( doc, items );
188  break;
189  case MSSQL:
190  loadMssqlConnections( doc, items );
191  break;
192  case WCS:
193  loadOWSConnections( doc, items, QStringLiteral( "WCS" ) );
194  break;
195  case Oracle:
196  loadOracleConnections( doc, items );
197  break;
198  case DB2:
199  loadDb2Connections( doc, items );
200  break;
201  case GeoNode:
202  loadGeonodeConnections( doc, items );
203  break;
204  }
205  // clear connections list and close window
206  listConnections->clear();
207  accept();
208  }
209 
210  mFileName.clear();
211 }
212 
213 bool QgsManageConnectionsDialog::populateConnections()
214 {
215  // Export mode. Populate connections list from settings
216  if ( mDialogMode == Export )
217  {
218  QgsSettings settings;
219  switch ( mConnectionType )
220  {
221  case WMS:
222  settings.beginGroup( QStringLiteral( "/qgis/connections-wms" ) );
223  break;
224  case WFS:
225  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
226  break;
227  case WCS:
228  settings.beginGroup( QStringLiteral( "/qgis/connections-wcs" ) );
229  break;
230  case PostGIS:
231  settings.beginGroup( QStringLiteral( "/PostgreSQL/connections" ) );
232  break;
233  case MSSQL:
234  settings.beginGroup( QStringLiteral( "/MSSQL/connections" ) );
235  break;
236  case Oracle:
237  settings.beginGroup( QStringLiteral( "/Oracle/connections" ) );
238  break;
239  case DB2:
240  settings.beginGroup( QStringLiteral( "/DB2/connections" ) );
241  break;
242  case GeoNode:
243  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
244  break;
245  }
246  QStringList keys = settings.childGroups();
247  QStringList::Iterator it = keys.begin();
248  while ( it != keys.end() )
249  {
250  QListWidgetItem *item = new QListWidgetItem();
251  item->setText( *it );
252  listConnections->addItem( item );
253  ++it;
254  }
255  settings.endGroup();
256  }
257  // Import mode. Populate connections list from file
258  else
259  {
260  QFile file( mFileName );
261  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
262  {
263  QMessageBox::warning( this, tr( "Loading Connections" ),
264  tr( "Cannot read file %1:\n%2." )
265  .arg( mFileName,
266  file.errorString() ) );
267  return false;
268  }
269 
270  QDomDocument doc;
271  QString errorStr;
272  int errorLine;
273  int errorColumn;
274 
275  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
276  {
277  QMessageBox::warning( this, tr( "Loading Connections" ),
278  tr( "Parse error at line %1, column %2:\n%3" )
279  .arg( errorLine )
280  .arg( errorColumn )
281  .arg( errorStr ) );
282  return false;
283  }
284 
285  QDomElement root = doc.documentElement();
286  switch ( mConnectionType )
287  {
288  case WMS:
289  if ( root.tagName() != QLatin1String( "qgsWMSConnections" ) )
290  {
291  QMessageBox::information( this, tr( "Loading Connections" ),
292  tr( "The file is not a WMS connections exchange file." ) );
293  return false;
294  }
295  break;
296 
297  case WFS:
298  if ( root.tagName() != QLatin1String( "qgsWFSConnections" ) )
299  {
300  QMessageBox::information( this, tr( "Loading Connections" ),
301  tr( "The file is not a WFS connections exchange file." ) );
302  return false;
303  }
304  break;
305 
306  case WCS:
307  if ( root.tagName() != QLatin1String( "qgsWCSConnections" ) )
308  {
309  QMessageBox::information( this, tr( "Loading Connections" ),
310  tr( "The file is not a WCS connections exchange file." ) );
311  return false;
312  }
313  break;
314 
315  case PostGIS:
316  if ( root.tagName() != QLatin1String( "qgsPgConnections" ) )
317  {
318  QMessageBox::information( this, tr( "Loading Connections" ),
319  tr( "The file is not a PostGIS connections exchange file." ) );
320  return false;
321  }
322  break;
323 
324  case MSSQL:
325  if ( root.tagName() != QLatin1String( "qgsMssqlConnections" ) )
326  {
327  QMessageBox::information( this, tr( "Loading Connections" ),
328  tr( "The file is not a MSSQL connections exchange file." ) );
329  return false;
330  }
331  break;
332  case Oracle:
333  if ( root.tagName() != QLatin1String( "qgsOracleConnections" ) )
334  {
335  QMessageBox::information( this, tr( "Loading Connections" ),
336  tr( "The file is not an Oracle connections exchange file." ) );
337  return false;
338  }
339  break;
340  case DB2:
341  if ( root.tagName() != QLatin1String( "qgsDb2Connections" ) )
342  {
343  QMessageBox::information( this, tr( "Loading Connections" ),
344  tr( "The file is not a DB2 connections exchange file." ) );
345  return false;
346  }
347  break;
348  case GeoNode:
349  if ( root.tagName() != QLatin1String( "qgsGeoNodeConnections" ) )
350  {
351  QMessageBox::information( this, tr( "Loading Connections" ),
352  tr( "The file is not a GeoNode connections exchange file." ) );
353  return false;
354  }
355  break;
356  }
357 
358  QDomElement child = root.firstChildElement();
359  while ( !child.isNull() )
360  {
361  QListWidgetItem *item = new QListWidgetItem();
362  item->setText( child.attribute( QStringLiteral( "name" ) ) );
363  listConnections->addItem( item );
364  child = child.nextSiblingElement();
365  }
366  }
367  return true;
368 }
369 
370 QDomDocument QgsManageConnectionsDialog::saveOWSConnections( const QStringList &connections, const QString &service )
371 {
372  QDomDocument doc( QStringLiteral( "connections" ) );
373  QDomElement root = doc.createElement( "qgs" + service.toUpper() + "Connections" );
374  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
375  doc.appendChild( root );
376 
377  QgsSettings settings;
378  QString path;
379  for ( int i = 0; i < connections.count(); ++i )
380  {
381  path = "/qgis/connections-" + service.toLower() + '/';
382  QDomElement el = doc.createElement( service.toLower() );
383  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
384  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url", "" ).toString() );
385 
386  if ( service == QLatin1String( "WMS" ) )
387  {
388  el.setAttribute( QStringLiteral( "ignoreGetMapURI" ), settings.value( path + connections[i] + "/ignoreGetMapURI", false ).toBool() ? "true" : "false" );
389  el.setAttribute( QStringLiteral( "ignoreGetFeatureInfoURI" ), settings.value( path + connections[i] + "/ignoreGetFeatureInfoURI", false ).toBool() ? "true" : "false" );
390  el.setAttribute( QStringLiteral( "ignoreAxisOrientation" ), settings.value( path + connections[i] + "/ignoreAxisOrientation", false ).toBool() ? "true" : "false" );
391  el.setAttribute( QStringLiteral( "invertAxisOrientation" ), settings.value( path + connections[i] + "/invertAxisOrientation", false ).toBool() ? "true" : "false" );
392  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + connections[ i ] + "/referer", "" ).toString() );
393  el.setAttribute( QStringLiteral( "smoothPixmapTransform" ), settings.value( path + connections[i] + "/smoothPixmapTransform", false ).toBool() ? "true" : "false" );
394  el.setAttribute( QStringLiteral( "dpiMode" ), settings.value( path + connections[i] + "/dpiMode", "7" ).toInt() );
395  }
396 
397  path = "/qgis/" + service.toUpper() + '/';
398  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username", "" ).toString() );
399  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password", "" ).toString() );
400  root.appendChild( el );
401  }
402 
403  return doc;
404 }
405 
406 QDomDocument QgsManageConnectionsDialog::saveWfsConnections( const QStringList &connections )
407 {
408  QDomDocument doc( QStringLiteral( "connections" ) );
409  QDomElement root = doc.createElement( QStringLiteral( "qgsWFSConnections" ) );
410  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
411  doc.appendChild( root );
412 
413  QgsSettings settings;
414  QString path;
415  for ( int i = 0; i < connections.count(); ++i )
416  {
417  path = QStringLiteral( "/qgis/connections-wfs/" );
418  QDomElement el = doc.createElement( QStringLiteral( "wfs" ) );
419  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
420  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url", "" ).toString() );
421 
422  el.setAttribute( QStringLiteral( "referer" ), settings.value( path + connections[ i ] + "/referer", "" ).toString() );
423 
424  path = QStringLiteral( "/qgis/WFS/" );
425  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username", "" ).toString() );
426  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password", "" ).toString() );
427  root.appendChild( el );
428  }
429 
430  return doc;
431 }
432 
433 QDomDocument QgsManageConnectionsDialog::savePgConnections( const QStringList &connections )
434 {
435  QDomDocument doc( QStringLiteral( "connections" ) );
436  QDomElement root = doc.createElement( QStringLiteral( "qgsPgConnections" ) );
437  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
438  doc.appendChild( root );
439 
440  QgsSettings settings;
441  QString path;
442  for ( int i = 0; i < connections.count(); ++i )
443  {
444  path = "/PostgreSQL/connections/" + connections[ i ];
445  QDomElement el = doc.createElement( QStringLiteral( "postgis" ) );
446  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
447  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host", "" ).toString() );
448  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port", "" ).toString() );
449  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database", "" ).toString() );
450  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service", "" ).toString() );
451  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
452  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
453 
454  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
455 
456  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
457  {
458  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", "" ).toString() );
459  }
460 
461  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
462 
463  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
464  {
465  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", "" ).toString() );
466  }
467 
468  root.appendChild( el );
469  }
470 
471  return doc;
472 }
473 
474 QDomDocument QgsManageConnectionsDialog::saveMssqlConnections( const QStringList &connections )
475 {
476  QDomDocument doc( QStringLiteral( "connections" ) );
477  QDomElement root = doc.createElement( QStringLiteral( "qgsMssqlConnections" ) );
478  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
479  doc.appendChild( root );
480 
481  QgsSettings settings;
482  QString path;
483  for ( int i = 0; i < connections.count(); ++i )
484  {
485  path = "/MSSQL/connections/" + connections[ i ];
486  QDomElement el = doc.createElement( QStringLiteral( "mssql" ) );
487  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
488  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host", "" ).toString() );
489  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port", "" ).toString() );
490  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database", "" ).toString() );
491  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service", "" ).toString() );
492  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
493  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
494 
495  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
496 
497  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
498  {
499  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", "" ).toString() );
500  }
501 
502  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
503 
504  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
505  {
506  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", "" ).toString() );
507  }
508 
509  root.appendChild( el );
510  }
511 
512  return doc;
513 }
514 
515 QDomDocument QgsManageConnectionsDialog::saveOracleConnections( const QStringList &connections )
516 {
517  QDomDocument doc( QStringLiteral( "connections" ) );
518  QDomElement root = doc.createElement( QStringLiteral( "qgsOracleConnections" ) );
519  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
520  doc.appendChild( root );
521 
522  QgsSettings settings;
523  QString path;
524  for ( int i = 0; i < connections.count(); ++i )
525  {
526  path = "/Oracle/connections/" + connections[ i ];
527  QDomElement el = doc.createElement( QStringLiteral( "oracle" ) );
528  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
529  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host", "" ).toString() );
530  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port", "" ).toString() );
531  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database", "" ).toString() );
532  el.setAttribute( QStringLiteral( "dboptions" ), settings.value( path + "/dboptions", "" ).toString() );
533  el.setAttribute( QStringLiteral( "dbworkspace" ), settings.value( path + "/dbworkspace", "" ).toString() );
534  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
535  el.setAttribute( QStringLiteral( "userTablesOnly" ), settings.value( path + "/userTablesOnly", "0" ).toString() );
536  el.setAttribute( QStringLiteral( "geometryColumnsOnly" ), settings.value( path + "/geometryColumnsOnly", "0" ).toString() );
537  el.setAttribute( QStringLiteral( "allowGeometrylessTables" ), settings.value( path + "/allowGeometrylessTables", "0" ).toString() );
538 
539  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
540 
541  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
542  {
543  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", "" ).toString() );
544  }
545 
546  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
547 
548  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
549  {
550  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", "" ).toString() );
551  }
552 
553  root.appendChild( el );
554  }
555 
556  return doc;
557 }
558 
559 QDomDocument QgsManageConnectionsDialog::saveDb2Connections( const QStringList &connections )
560 {
561  QDomDocument doc( QStringLiteral( "connections" ) );
562  QDomElement root = doc.createElement( QStringLiteral( "qgsDb2Connections" ) );
563  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
564  doc.appendChild( root );
565 
566  QgsSettings settings;
567  QString path;
568  for ( int i = 0; i < connections.count(); ++i )
569  {
570  path = "/DB2/connections/" + connections[ i ];
571  QDomElement el = doc.createElement( QStringLiteral( "db2" ) );
572  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
573  el.setAttribute( QStringLiteral( "host" ), settings.value( path + "/host", "" ).toString() );
574  el.setAttribute( QStringLiteral( "port" ), settings.value( path + "/port", "" ).toString() );
575  el.setAttribute( QStringLiteral( "database" ), settings.value( path + "/database", "" ).toString() );
576  el.setAttribute( QStringLiteral( "service" ), settings.value( path + "/service", "" ).toString() );
577  el.setAttribute( QStringLiteral( "sslmode" ), settings.value( path + "/sslmode", "1" ).toString() );
578  el.setAttribute( QStringLiteral( "estimatedMetadata" ), settings.value( path + "/estimatedMetadata", "0" ).toString() );
579 
580  el.setAttribute( QStringLiteral( "saveUsername" ), settings.value( path + "/saveUsername", "false" ).toString() );
581 
582  if ( settings.value( path + "/saveUsername", "false" ).toString() == QLatin1String( "true" ) )
583  {
584  el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username", "" ).toString() );
585  }
586 
587  el.setAttribute( QStringLiteral( "savePassword" ), settings.value( path + "/savePassword", "false" ).toString() );
588 
589  if ( settings.value( path + "/savePassword", "false" ).toString() == QLatin1String( "true" ) )
590  {
591  el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password", "" ).toString() );
592  }
593 
594  root.appendChild( el );
595  }
596 
597  return doc;
598 }
599 
600 QDomDocument QgsManageConnectionsDialog::saveGeonodeConnections( const QStringList &connections )
601 {
602  QDomDocument doc( QStringLiteral( "connections" ) );
603  QDomElement root = doc.createElement( QStringLiteral( "qgsGeoNodeConnections" ) );
604  root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
605  doc.appendChild( root );
606 
607  QgsSettings settings;
608  QString path;
609  for ( int i = 0; i < connections.count(); ++i )
610  {
611  path = QStringLiteral( "/qgis/connections-geonode/" );
612  QDomElement el = doc.createElement( QStringLiteral( "geonode" ) );
613  el.setAttribute( QStringLiteral( "name" ), connections[ i ] );
614  el.setAttribute( QStringLiteral( "url" ), settings.value( path + connections[ i ] + "/url", "" ).toString() );
615 
616  path = QStringLiteral( "/qgis/GeoNode/" );
617  el.setAttribute( QStringLiteral( "username" ), settings.value( path + connections[ i ] + "/username", "" ).toString() );
618  el.setAttribute( QStringLiteral( "password" ), settings.value( path + connections[ i ] + "/password", "" ).toString() );
619  root.appendChild( el );
620  }
621 
622  return doc;
623 }
624 
625 void QgsManageConnectionsDialog::loadOWSConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
626 {
627  QDomElement root = doc.documentElement();
628  if ( root.tagName() != "qgs" + service.toUpper() + "Connections" )
629  {
630  QMessageBox::information( this, tr( "Loading Connections" ),
631  tr( "The file is not a %1 connections exchange file." ).arg( service ) );
632  return;
633  }
634 
635  QString connectionName;
636  QgsSettings settings;
637  settings.beginGroup( "/qgis/connections-" + service.toLower() );
638  QStringList keys = settings.childGroups();
639  settings.endGroup();
640  QDomElement child = root.firstChildElement();
641  bool prompt = true;
642  bool overwrite = true;
643 
644  while ( !child.isNull() )
645  {
646  connectionName = child.attribute( QStringLiteral( "name" ) );
647  if ( !items.contains( connectionName ) )
648  {
649  child = child.nextSiblingElement();
650  continue;
651  }
652 
653  // check for duplicates
654  if ( keys.contains( connectionName ) && prompt )
655  {
656  int res = QMessageBox::warning( this,
657  tr( "Loading Connections" ),
658  tr( "Connection with name '%1' already exists. Overwrite?" )
659  .arg( connectionName ),
660  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
661 
662  switch ( res )
663  {
664  case QMessageBox::Cancel:
665  return;
666  case QMessageBox::No:
667  child = child.nextSiblingElement();
668  continue;
669  case QMessageBox::Yes:
670  overwrite = true;
671  break;
672  case QMessageBox::YesToAll:
673  prompt = false;
674  overwrite = true;
675  break;
676  case QMessageBox::NoToAll:
677  prompt = false;
678  overwrite = false;
679  break;
680  }
681  }
682 
683  if ( keys.contains( connectionName ) && !overwrite )
684  {
685  child = child.nextSiblingElement();
686  continue;
687  }
688 
689  // no dups detected or overwrite is allowed
690  settings.beginGroup( "/qgis/connections-" + service.toLower() );
691  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
692  settings.setValue( QString( '/' + connectionName + "/ignoreGetMapURI" ), child.attribute( QStringLiteral( "ignoreGetMapURI" ) ) == QLatin1String( "true" ) );
693  settings.setValue( QString( '/' + connectionName + "/ignoreGetFeatureInfoURI" ), child.attribute( QStringLiteral( "ignoreGetFeatureInfoURI" ) ) == QLatin1String( "true" ) );
694  settings.setValue( QString( '/' + connectionName + "/ignoreAxisOrientation" ), child.attribute( QStringLiteral( "ignoreAxisOrientation" ) ) == QLatin1String( "true" ) );
695  settings.setValue( QString( '/' + connectionName + "/invertAxisOrientation" ), child.attribute( QStringLiteral( "invertAxisOrientation" ) ) == QLatin1String( "true" ) );
696  settings.setValue( QString( '/' + connectionName + "/referer" ), child.attribute( QStringLiteral( "referer" ) ) );
697  settings.setValue( QString( '/' + connectionName + "/smoothPixmapTransform" ), child.attribute( QStringLiteral( "smoothPixmapTransform" ) ) == QLatin1String( "true" ) );
698  settings.setValue( QString( '/' + connectionName + "/dpiMode" ), child.attribute( QStringLiteral( "dpiMode" ), QStringLiteral( "7" ) ).toInt() );
699  settings.endGroup();
700 
701  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
702  {
703  settings.beginGroup( "/qgis/" + service.toUpper() + '/' + connectionName );
704  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
705  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
706  settings.endGroup();
707  }
708  child = child.nextSiblingElement();
709  }
710 }
711 
712 void QgsManageConnectionsDialog::loadWfsConnections( const QDomDocument &doc, const QStringList &items )
713 {
714  QDomElement root = doc.documentElement();
715  if ( root.tagName() != QLatin1String( "qgsWFSConnections" ) )
716  {
717  QMessageBox::information( this, tr( "Loading Connections" ),
718  tr( "The file is not a WFS connections exchange file." ) );
719  return;
720  }
721 
722  QString connectionName;
723  QgsSettings settings;
724  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
725  QStringList keys = settings.childGroups();
726  settings.endGroup();
727  QDomElement child = root.firstChildElement();
728  bool prompt = true;
729  bool overwrite = true;
730 
731  while ( !child.isNull() )
732  {
733  connectionName = child.attribute( QStringLiteral( "name" ) );
734  if ( !items.contains( connectionName ) )
735  {
736  child = child.nextSiblingElement();
737  continue;
738  }
739 
740  // check for duplicates
741  if ( keys.contains( connectionName ) && prompt )
742  {
743  int res = QMessageBox::warning( this,
744  tr( "Loading Connections" ),
745  tr( "Connection with name '%1' already exists. Overwrite?" )
746  .arg( connectionName ),
747  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
748 
749  switch ( res )
750  {
751  case QMessageBox::Cancel:
752  return;
753  case QMessageBox::No:
754  child = child.nextSiblingElement();
755  continue;
756  case QMessageBox::Yes:
757  overwrite = true;
758  break;
759  case QMessageBox::YesToAll:
760  prompt = false;
761  overwrite = true;
762  break;
763  case QMessageBox::NoToAll:
764  prompt = false;
765  overwrite = false;
766  break;
767  }
768  }
769 
770  if ( keys.contains( connectionName ) && !overwrite )
771  {
772  child = child.nextSiblingElement();
773  continue;
774  }
775 
776  // no dups detected or overwrite is allowed
777  settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) );
778  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
779  settings.endGroup();
780 
781  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
782  {
783  settings.beginGroup( "/qgis/WFS/" + connectionName );
784  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
785  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
786  settings.endGroup();
787  }
788  child = child.nextSiblingElement();
789  }
790 }
791 
792 
793 void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, const QStringList &items )
794 {
795  QDomElement root = doc.documentElement();
796  if ( root.tagName() != QLatin1String( "qgsPgConnections" ) )
797  {
798  QMessageBox::information( this,
799  tr( "Loading Connections" ),
800  tr( "The file is not a PostGIS connections exchange file." ) );
801  return;
802  }
803 
804  QString connectionName;
805  QgsSettings settings;
806  settings.beginGroup( QStringLiteral( "/PostgreSQL/connections" ) );
807  QStringList keys = settings.childGroups();
808  settings.endGroup();
809  QDomElement child = root.firstChildElement();
810  bool prompt = true;
811  bool overwrite = true;
812 
813  while ( !child.isNull() )
814  {
815  connectionName = child.attribute( QStringLiteral( "name" ) );
816  if ( !items.contains( connectionName ) )
817  {
818  child = child.nextSiblingElement();
819  continue;
820  }
821 
822  // check for duplicates
823  if ( keys.contains( connectionName ) && prompt )
824  {
825  int res = QMessageBox::warning( this,
826  tr( "Loading Connections" ),
827  tr( "Connection with name '%1' already exists. Overwrite?" )
828  .arg( connectionName ),
829  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
830  switch ( res )
831  {
832  case QMessageBox::Cancel:
833  return;
834  case QMessageBox::No:
835  child = child.nextSiblingElement();
836  continue;
837  case QMessageBox::Yes:
838  overwrite = true;
839  break;
840  case QMessageBox::YesToAll:
841  prompt = false;
842  overwrite = true;
843  break;
844  case QMessageBox::NoToAll:
845  prompt = false;
846  overwrite = false;
847  break;
848  }
849  }
850 
851  if ( keys.contains( connectionName ) && !overwrite )
852  {
853  child = child.nextSiblingElement();
854  continue;
855  }
856 
857  //no dups detected or overwrite is allowed
858  settings.beginGroup( "/PostgreSQL/connections/" + connectionName );
859 
860  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
861  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
862  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
863  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
864  {
865  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
866  }
867  else
868  {
869  settings.setValue( QStringLiteral( "/service" ), "" );
870  }
871  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
872  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
873  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
874  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
875  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
876  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
877  settings.endGroup();
878 
879  child = child.nextSiblingElement();
880  }
881 }
882 
883 void QgsManageConnectionsDialog::loadMssqlConnections( const QDomDocument &doc, const QStringList &items )
884 {
885  QDomElement root = doc.documentElement();
886  if ( root.tagName() != QLatin1String( "qgsMssqlConnections" ) )
887  {
888  QMessageBox::information( this,
889  tr( "Loading Connections" ),
890  tr( "The file is not a MSSQL connections exchange file." ) );
891  return;
892  }
893 
894  QString connectionName;
895  QgsSettings settings;
896  settings.beginGroup( QStringLiteral( "/MSSQL/connections" ) );
897  QStringList keys = settings.childGroups();
898  settings.endGroup();
899  QDomElement child = root.firstChildElement();
900  bool prompt = true;
901  bool overwrite = true;
902 
903  while ( !child.isNull() )
904  {
905  connectionName = child.attribute( QStringLiteral( "name" ) );
906  if ( !items.contains( connectionName ) )
907  {
908  child = child.nextSiblingElement();
909  continue;
910  }
911 
912  // check for duplicates
913  if ( keys.contains( connectionName ) && prompt )
914  {
915  int res = QMessageBox::warning( this,
916  tr( "Loading Connections" ),
917  tr( "Connection with name '%1' already exists. Overwrite?" )
918  .arg( connectionName ),
919  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
920  switch ( res )
921  {
922  case QMessageBox::Cancel:
923  return;
924  case QMessageBox::No:
925  child = child.nextSiblingElement();
926  continue;
927  case QMessageBox::Yes:
928  overwrite = true;
929  break;
930  case QMessageBox::YesToAll:
931  prompt = false;
932  overwrite = true;
933  break;
934  case QMessageBox::NoToAll:
935  prompt = false;
936  overwrite = false;
937  break;
938  }
939  }
940 
941  if ( keys.contains( connectionName ) && !overwrite )
942  {
943  child = child.nextSiblingElement();
944  continue;
945  }
946 
947  //no dups detected or overwrite is allowed
948  settings.beginGroup( "/MSSQL/connections/" + connectionName );
949 
950  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
951  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
952  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
953  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
954  {
955  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
956  }
957  else
958  {
959  settings.setValue( QStringLiteral( "/service" ), "" );
960  }
961  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
962  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
963  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
964  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
965  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
966  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
967  settings.endGroup();
968 
969  child = child.nextSiblingElement();
970  }
971 }
972 
973 void QgsManageConnectionsDialog::loadOracleConnections( const QDomDocument &doc, const QStringList &items )
974 {
975  QDomElement root = doc.documentElement();
976  if ( root.tagName() != QLatin1String( "qgsOracleConnections" ) )
977  {
978  QMessageBox::information( this,
979  tr( "Loading Connections" ),
980  tr( "The file is not an Oracle connections exchange file." ) );
981  return;
982  }
983 
984  QString connectionName;
985  QgsSettings settings;
986  settings.beginGroup( QStringLiteral( "/Oracle/connections" ) );
987  QStringList keys = settings.childGroups();
988  settings.endGroup();
989  QDomElement child = root.firstChildElement();
990  bool prompt = true;
991  bool overwrite = true;
992 
993  while ( !child.isNull() )
994  {
995  connectionName = child.attribute( QStringLiteral( "name" ) );
996  if ( !items.contains( connectionName ) )
997  {
998  child = child.nextSiblingElement();
999  continue;
1000  }
1001 
1002  // check for duplicates
1003  if ( keys.contains( connectionName ) && prompt )
1004  {
1005  int res = QMessageBox::warning( this,
1006  tr( "Loading Connections" ),
1007  tr( "Connection with name '%1' already exists. Overwrite?" )
1008  .arg( connectionName ),
1009  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1010  switch ( res )
1011  {
1012  case QMessageBox::Cancel:
1013  return;
1014  case QMessageBox::No:
1015  child = child.nextSiblingElement();
1016  continue;
1017  case QMessageBox::Yes:
1018  overwrite = true;
1019  break;
1020  case QMessageBox::YesToAll:
1021  prompt = false;
1022  overwrite = true;
1023  break;
1024  case QMessageBox::NoToAll:
1025  prompt = false;
1026  overwrite = false;
1027  break;
1028  }
1029  }
1030 
1031  if ( keys.contains( connectionName ) && !overwrite )
1032  {
1033  child = child.nextSiblingElement();
1034  continue;
1035  }
1036 
1037  //no dups detected or overwrite is allowed
1038  settings.beginGroup( "/Oracle/connections/" + connectionName );
1039 
1040  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1041  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1042  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1043  settings.setValue( QStringLiteral( "/dboptions" ), child.attribute( QStringLiteral( "dboptions" ) ) );
1044  settings.setValue( QStringLiteral( "/dbworkspace" ), child.attribute( QStringLiteral( "dbworkspace" ) ) );
1045  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1046  settings.setValue( QStringLiteral( "/userTablesOnly" ), child.attribute( QStringLiteral( "userTablesOnly" ) ) );
1047  settings.setValue( QStringLiteral( "/geometryColumnsOnly" ), child.attribute( QStringLiteral( "geometryColumnsOnly" ) ) );
1048  settings.setValue( QStringLiteral( "/allowGeometrylessTables" ), child.attribute( QStringLiteral( "allowGeometrylessTables" ) ) );
1049  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1050  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1051  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1052  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1053  settings.endGroup();
1054 
1055  child = child.nextSiblingElement();
1056  }
1057 }
1058 
1059 void QgsManageConnectionsDialog::loadDb2Connections( const QDomDocument &doc, const QStringList &items )
1060 {
1061  QDomElement root = doc.documentElement();
1062  if ( root.tagName() != QLatin1String( "qgsDb2Connections" ) )
1063  {
1064  QMessageBox::information( this,
1065  tr( "Loading Connections" ),
1066  tr( "The file is not a DB2 connections exchange file." ) );
1067  return;
1068  }
1069 
1070  QString connectionName;
1071  QgsSettings settings;
1072  settings.beginGroup( QStringLiteral( "/DB2/connections" ) );
1073  QStringList keys = settings.childGroups();
1074  settings.endGroup();
1075  QDomElement child = root.firstChildElement();
1076  bool prompt = true;
1077  bool overwrite = true;
1078 
1079  while ( !child.isNull() )
1080  {
1081  connectionName = child.attribute( QStringLiteral( "name" ) );
1082  if ( !items.contains( connectionName ) )
1083  {
1084  child = child.nextSiblingElement();
1085  continue;
1086  }
1087 
1088  // check for duplicates
1089  if ( keys.contains( connectionName ) && prompt )
1090  {
1091  int res = QMessageBox::warning( this,
1092  tr( "Loading Connections" ),
1093  tr( "Connection with name '%1' already exists. Overwrite?" )
1094  .arg( connectionName ),
1095  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1096  switch ( res )
1097  {
1098  case QMessageBox::Cancel:
1099  return;
1100  case QMessageBox::No:
1101  child = child.nextSiblingElement();
1102  continue;
1103  case QMessageBox::Yes:
1104  overwrite = true;
1105  break;
1106  case QMessageBox::YesToAll:
1107  prompt = false;
1108  overwrite = true;
1109  break;
1110  case QMessageBox::NoToAll:
1111  prompt = false;
1112  overwrite = false;
1113  break;
1114  }
1115  }
1116 
1117  if ( keys.contains( connectionName ) && !overwrite )
1118  {
1119  child = child.nextSiblingElement();
1120  continue;
1121  }
1122 
1123  //no dups detected or overwrite is allowed
1124  settings.beginGroup( "/DB2/connections/" + connectionName );
1125 
1126  settings.setValue( QStringLiteral( "/host" ), child.attribute( QStringLiteral( "host" ) ) );
1127  settings.setValue( QStringLiteral( "/port" ), child.attribute( QStringLiteral( "port" ) ) );
1128  settings.setValue( QStringLiteral( "/database" ), child.attribute( QStringLiteral( "database" ) ) );
1129  if ( child.hasAttribute( QStringLiteral( "service" ) ) )
1130  {
1131  settings.setValue( QStringLiteral( "/service" ), child.attribute( QStringLiteral( "service" ) ) );
1132  }
1133  else
1134  {
1135  settings.setValue( QStringLiteral( "/service" ), "" );
1136  }
1137  settings.setValue( QStringLiteral( "/sslmode" ), child.attribute( QStringLiteral( "sslmode" ) ) );
1138  settings.setValue( QStringLiteral( "/estimatedMetadata" ), child.attribute( QStringLiteral( "estimatedMetadata" ) ) );
1139  settings.setValue( QStringLiteral( "/saveUsername" ), child.attribute( QStringLiteral( "saveUsername" ) ) );
1140  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1141  settings.setValue( QStringLiteral( "/savePassword" ), child.attribute( QStringLiteral( "savePassword" ) ) );
1142  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1143  settings.endGroup();
1144 
1145  child = child.nextSiblingElement();
1146  }
1147 }
1148 
1149 void QgsManageConnectionsDialog::loadGeonodeConnections( const QDomDocument &doc, const QStringList &items )
1150 {
1151  QDomElement root = doc.documentElement();
1152  if ( root.tagName() != QLatin1String( "qgsGeoNodeConnections" ) )
1153  {
1154  QMessageBox::information( this, tr( "Loading Connections" ),
1155  tr( "The file is not a GeoNode connections exchange file." ) );
1156  return;
1157  }
1158 
1159  QString connectionName;
1160  QgsSettings settings;
1161  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
1162  QStringList keys = settings.childGroups();
1163  settings.endGroup();
1164  QDomElement child = root.firstChildElement();
1165  bool prompt = true;
1166  bool overwrite = true;
1167 
1168  while ( !child.isNull() )
1169  {
1170  connectionName = child.attribute( QStringLiteral( "name" ) );
1171  if ( !items.contains( connectionName ) )
1172  {
1173  child = child.nextSiblingElement();
1174  continue;
1175  }
1176 
1177  // check for duplicates
1178  if ( keys.contains( connectionName ) && prompt )
1179  {
1180  int res = QMessageBox::warning( this,
1181  tr( "Loading Connections" ),
1182  tr( "Connection with name '%1' already exists. Overwrite?" )
1183  .arg( connectionName ),
1184  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
1185 
1186  switch ( res )
1187  {
1188  case QMessageBox::Cancel:
1189  return;
1190  case QMessageBox::No:
1191  child = child.nextSiblingElement();
1192  continue;
1193  case QMessageBox::Yes:
1194  overwrite = true;
1195  break;
1196  case QMessageBox::YesToAll:
1197  prompt = false;
1198  overwrite = true;
1199  break;
1200  case QMessageBox::NoToAll:
1201  prompt = false;
1202  overwrite = false;
1203  break;
1204  }
1205  }
1206 
1207  if ( keys.contains( connectionName ) && !overwrite )
1208  {
1209  child = child.nextSiblingElement();
1210  continue;
1211  }
1212 
1213  // no dups detected or overwrite is allowed
1214  settings.beginGroup( QStringLiteral( "/qgis/connections-geonode" ) );
1215  settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) );
1216  settings.endGroup();
1217 
1218  if ( !child.attribute( QStringLiteral( "username" ) ).isEmpty() )
1219  {
1220  settings.beginGroup( "/qgis/GeoNode/" + connectionName );
1221  settings.setValue( QStringLiteral( "/username" ), child.attribute( QStringLiteral( "username" ) ) );
1222  settings.setValue( QStringLiteral( "/password" ), child.attribute( QStringLiteral( "password" ) ) );
1223  settings.endGroup();
1224  }
1225  child = child.nextSiblingElement();
1226  }
1227 }
1228 
1230 {
1231  listConnections->selectAll();
1232  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !listConnections->selectedItems().isEmpty() );
1233 }
1234 
1236 {
1237  listConnections->clearSelection();
1238  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
1239 }
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:96
This class is a composition of two QSettings instances:
Definition: qgssettings.h:55
void beginGroup(const QString &prefix, const QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:86
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QgsManageConnectionsDialog(QWidget *parent=nullptr, Mode mode=Export, Type type=WMS, const QString &fileName=QString())
Constructor for QgsManageConnectionsDialog.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.