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