QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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, 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  }
50  else
51  {
52  //label->setText( tr( "Select connections to export" ) );
53  buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Export" ) );
54  }
55 
56  if ( !populateConnections() )
57  {
58  QApplication::postEvent( this, new QCloseEvent() );
59  }
60 
61  // use Ok button for starting import and export operations
62  disconnect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
63  connect( buttonBox, SIGNAL( accepted() ), this, SLOT( doExportImport() ) );
64 }
65 
67 {
68  QList<QListWidgetItem *> selection = listConnections->selectedItems();
69  if ( selection.isEmpty() )
70  {
71  QMessageBox::warning( this, tr( "Export/import error" ),
72  tr( "You should select at least one connection from list." ) );
73  return;
74  }
75 
76  QStringList items;
77  for ( int i = 0; i < selection.size(); ++i )
78  {
79  items.append( selection.at( i )->text() );
80  }
81 
82  if ( mDialogMode == Export )
83  {
84  QString fileName = QFileDialog::getSaveFileName( this, tr( "Save connections" ), ".",
85  tr( "XML files (*.xml *.XML)" ) );
86  if ( fileName.isEmpty() )
87  {
88  return;
89  }
90 
91  // ensure the user never ommited the extension from the file name
92  if ( !fileName.toLower().endsWith( ".xml" ) )
93  {
94  fileName += ".xml";
95  }
96 
97  mFileName = fileName;
98 
99  QDomDocument doc;
100  switch ( mConnectionType )
101  {
102  case WMS:
103  doc = saveOWSConnections( items, "WMS" );
104  break;
105  case WFS:
106  doc = saveWFSConnections( items );
107  break;
108  case PostGIS:
109  doc = savePgConnections( items );
110  break;
111  case MSSQL:
112  doc = saveMssqlConnections( items );
113  break;
114  case WCS:
115  doc = saveOWSConnections( items, "WCS" );
116  break;
117  case Oracle:
118  doc = saveOracleConnections( items );
119  break;
120  }
121 
122  QFile file( mFileName );
123  if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
124  {
125  QMessageBox::warning( this, tr( "Saving connections" ),
126  tr( "Cannot write file %1:\n%2." )
127  .arg( mFileName )
128  .arg( file.errorString() ) );
129  return;
130  }
131 
132  QTextStream out( &file );
133  doc.save( out, 4 );
134  }
135  else // import connections
136  {
137  QFile file( mFileName );
138  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
139  {
140  QMessageBox::warning( this, tr( "Loading connections" ),
141  tr( "Cannot read file %1:\n%2." )
142  .arg( mFileName )
143  .arg( file.errorString() ) );
144  return;
145  }
146 
147  QDomDocument doc;
148  QString errorStr;
149  int errorLine;
150  int errorColumn;
151 
152  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
153  {
154  QMessageBox::warning( this, tr( "Loading connections" ),
155  tr( "Parse error at line %1, column %2:\n%3" )
156  .arg( errorLine )
157  .arg( errorColumn )
158  .arg( errorStr ) );
159  return;
160  }
161 
162  switch ( mConnectionType )
163  {
164  case WMS:
165  loadOWSConnections( doc, items, "WMS" );
166  break;
167  case WFS:
168  loadWFSConnections( doc, items );
169  break;
170  case PostGIS:
171  loadPgConnections( doc, items );
172  break;
173  case MSSQL:
174  loadMssqlConnections( doc, items );
175  break;
176  case WCS:
177  loadOWSConnections( doc, items, "WCS" );
178  break;
179  case Oracle:
180  loadOracleConnections( doc, items );
181  break;
182  }
183  // clear connections list and close window
184  listConnections->clear();
185  accept();
186  }
187 
188  mFileName = "";
189 }
190 
192 {
193  // Export mode. Populate connections list from settings
194  if ( mDialogMode == Export )
195  {
196  QSettings settings;
197  switch ( mConnectionType )
198  {
199  case WMS:
200  settings.beginGroup( "/Qgis/connections-wms" );
201  break;
202  case WFS:
203  settings.beginGroup( "/Qgis/connections-wfs" );
204  break;
205  case WCS:
206  settings.beginGroup( "/Qgis/connections-wcs" );
207  break;
208  case PostGIS:
209  settings.beginGroup( "/PostgreSQL/connections" );
210  break;
211  case MSSQL:
212  settings.beginGroup( "/MSSQL/connections" );
213  break;
214  case Oracle:
215  settings.beginGroup( "/Oracle/connections" );
216  break;
217  }
218  QStringList keys = settings.childGroups();
219  QStringList::Iterator it = keys.begin();
220  while ( it != keys.end() )
221  {
222  QListWidgetItem *item = new QListWidgetItem();
223  item->setText( *it );
224  listConnections->addItem( item );
225  ++it;
226  }
227  settings.endGroup();
228  }
229  // Import mode. Populate connections list from file
230  else
231  {
232  QFile file( mFileName );
233  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
234  {
235  QMessageBox::warning( this, tr( "Loading connections" ),
236  tr( "Cannot read file %1:\n%2." )
237  .arg( mFileName )
238  .arg( file.errorString() ) );
239  return false;
240  }
241 
242  QDomDocument doc;
243  QString errorStr;
244  int errorLine;
245  int errorColumn;
246 
247  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
248  {
249  QMessageBox::warning( this, tr( "Loading connections" ),
250  tr( "Parse error at line %1, column %2:\n%3" )
251  .arg( errorLine )
252  .arg( errorColumn )
253  .arg( errorStr ) );
254  return false;
255  }
256 
257  QDomElement root = doc.documentElement();
258  switch ( mConnectionType )
259  {
260  case WMS:
261  if ( root.tagName() != "qgsWMSConnections" )
262  {
263  QMessageBox::information( this, tr( "Loading connections" ),
264  tr( "The file is not an WMS connections exchange file." ) );
265  return false;
266  }
267  break;
268 
269  case WFS:
270  if ( root.tagName() != "qgsWFSConnections" )
271  {
272  QMessageBox::information( this, tr( "Loading connections" ),
273  tr( "The file is not an WFS connections exchange file." ) );
274  return false;
275  }
276  break;
277 
278  case WCS:
279  if ( root.tagName() != "qgsWCSConnections" )
280  {
281  QMessageBox::information( this, tr( "Loading connections" ),
282  tr( "The file is not an WCS connections exchange file." ) );
283  return false;
284  }
285  break;
286 
287  case PostGIS:
288  if ( root.tagName() != "qgsPgConnections" )
289  {
290  QMessageBox::information( this, tr( "Loading connections" ),
291  tr( "The file is not an PostGIS connections exchange file." ) );
292  return false;
293  }
294  break;
295 
296  case MSSQL:
297  if ( root.tagName() != "qgsMssqlConnections" )
298  {
299  QMessageBox::information( this, tr( "Loading connections" ),
300  tr( "The file is not an MSSQL connections exchange file." ) );
301  return false;
302  }
303  break;
304  case Oracle:
305  if ( root.tagName() != "qgsOracleConnections" )
306  {
307  QMessageBox::information( this, tr( "Loading connections" ),
308  tr( "The file is not an Oracle connections exchange file." ) );
309  return false;
310  }
311  break;
312  }
313 
314  QDomElement child = root.firstChildElement();
315  while ( !child.isNull() )
316  {
317  QListWidgetItem *item = new QListWidgetItem();
318  item->setText( child.attribute( "name" ) );
319  listConnections->addItem( item );
320  child = child.nextSiblingElement();
321  }
322  }
323  return true;
324 }
325 
326 QDomDocument QgsManageConnectionsDialog::saveOWSConnections( const QStringList &connections, const QString & service )
327 {
328  QDomDocument doc( "connections" );
329  QDomElement root = doc.createElement( "qgs" + service.toUpper() + "Connections" );
330  root.setAttribute( "version", "1.0" );
331  doc.appendChild( root );
332 
333  QSettings settings;
334  QString path;
335  for ( int i = 0; i < connections.count(); ++i )
336  {
337  path = "/Qgis/connections-" + service.toLower() + "/";
338  QDomElement el = doc.createElement( service.toLower() );
339  el.setAttribute( "name", connections[ i ] );
340  el.setAttribute( "url", settings.value( path + connections[ i ] + "/url", "" ).toString() );
341 
342  if ( service == "WMS" )
343  {
344  el.setAttribute( "ignoreGetMapURI", settings.value( path + connections[i] + "/ignoreGetMapURI", false ).toBool() ? "true" : "false" );
345  el.setAttribute( "ignoreGetFeatureInfoURI", settings.value( path + connections[i] + "/ignoreGetFeatureInfoURI", false ).toBool() ? "true" : "false" );
346  el.setAttribute( "ignoreAxisOrientation", settings.value( path + connections[i] + "/ignoreAxisOrientation", false ).toBool() ? "true" : "false" );
347  el.setAttribute( "invertAxisOrientation", settings.value( path + connections[i] + "/invertAxisOrientation", false ).toBool() ? "true" : "false" );
348  el.setAttribute( "referer", settings.value( path + connections[ i ] + "/referer", "" ).toString() );
349  el.setAttribute( "smoothPixmapTransform", settings.value( path + connections[i] + "/smoothPixmapTransform", false ).toBool() ? "true" : "false" );
350  }
351 
352  path = "/Qgis/" + service.toUpper() + "/";
353  el.setAttribute( "username", settings.value( path + connections[ i ] + "/username", "" ).toString() );
354  el.setAttribute( "password", settings.value( path + connections[ i ] + "/password", "" ).toString() );
355  root.appendChild( el );
356  }
357 
358  return doc;
359 }
360 
361 QDomDocument QgsManageConnectionsDialog::saveWFSConnections( const QStringList &connections )
362 {
363  QDomDocument doc( "connections" );
364  QDomElement root = doc.createElement( "qgsWFSConnections" );
365  root.setAttribute( "version", "1.0" );
366  doc.appendChild( root );
367 
368  QSettings settings;
369  QString path;
370  for ( int i = 0; i < connections.count(); ++i )
371  {
372  path = "/Qgis/connections-wfs/";
373  QDomElement el = doc.createElement( "wfs" );
374  el.setAttribute( "name", connections[ i ] );
375  el.setAttribute( "url", settings.value( path + connections[ i ] + "/url", "" ).toString() );
376 
377  el.setAttribute( "referer", settings.value( path + connections[ i ] + "/referer", "" ).toString() );
378 
379  path = "/Qgis/WFS/";
380  el.setAttribute( "username", settings.value( path + connections[ i ] + "/username", "" ).toString() );
381  el.setAttribute( "password", settings.value( path + connections[ i ] + "/password", "" ).toString() );
382  root.appendChild( el );
383  }
384 
385  return doc;
386 }
387 
388 QDomDocument QgsManageConnectionsDialog::savePgConnections( const QStringList &connections )
389 {
390  QDomDocument doc( "connections" );
391  QDomElement root = doc.createElement( "qgsPgConnections" );
392  root.setAttribute( "version", "1.0" );
393  doc.appendChild( root );
394 
395  QSettings settings;
396  QString path;
397  for ( int i = 0; i < connections.count(); ++i )
398  {
399  path = "/PostgreSQL/connections/" + connections[ i ];
400  QDomElement el = doc.createElement( "postgis" );
401  el.setAttribute( "name", connections[ i ] );
402  el.setAttribute( "host", settings.value( path + "/host", "" ).toString() );
403  el.setAttribute( "port", settings.value( path + "/port", "" ).toString() );
404  el.setAttribute( "database", settings.value( path + "/database", "" ).toString() );
405  el.setAttribute( "service", settings.value( path + "/service", "" ).toString() );
406  el.setAttribute( "sslmode", settings.value( path + "/sslmode", "1" ).toString() );
407  el.setAttribute( "estimatedMetadata", settings.value( path + "/estimatedMetadata", "0" ).toString() );
408 
409  el.setAttribute( "saveUsername", settings.value( path + "/saveUsername", "false" ).toString() );
410 
411  if ( settings.value( path + "/saveUsername", "false" ).toString() == "true" )
412  {
413  el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
414  }
415 
416  el.setAttribute( "savePassword", settings.value( path + "/savePassword", "false" ).toString() );
417 
418  if ( settings.value( path + "/savePassword", "false" ).toString() == "true" )
419  {
420  el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
421  }
422 
423  root.appendChild( el );
424  }
425 
426  return doc;
427 }
428 
429 QDomDocument QgsManageConnectionsDialog::saveMssqlConnections( const QStringList &connections )
430 {
431  QDomDocument doc( "connections" );
432  QDomElement root = doc.createElement( "qgsMssqlConnections" );
433  root.setAttribute( "version", "1.0" );
434  doc.appendChild( root );
435 
436  QSettings settings;
437  QString path;
438  for ( int i = 0; i < connections.count(); ++i )
439  {
440  path = "/MSSQL/connections/" + connections[ i ];
441  QDomElement el = doc.createElement( "mssql" );
442  el.setAttribute( "name", connections[ i ] );
443  el.setAttribute( "host", settings.value( path + "/host", "" ).toString() );
444  el.setAttribute( "port", settings.value( path + "/port", "" ).toString() );
445  el.setAttribute( "database", settings.value( path + "/database", "" ).toString() );
446  el.setAttribute( "service", settings.value( path + "/service", "" ).toString() );
447  el.setAttribute( "sslmode", settings.value( path + "/sslmode", "1" ).toString() );
448  el.setAttribute( "estimatedMetadata", settings.value( path + "/estimatedMetadata", "0" ).toString() );
449 
450  el.setAttribute( "saveUsername", settings.value( path + "/saveUsername", "false" ).toString() );
451 
452  if ( settings.value( path + "/saveUsername", "false" ).toString() == "true" )
453  {
454  el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
455  }
456 
457  el.setAttribute( "savePassword", settings.value( path + "/savePassword", "false" ).toString() );
458 
459  if ( settings.value( path + "/savePassword", "false" ).toString() == "true" )
460  {
461  el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
462  }
463 
464  root.appendChild( el );
465  }
466 
467  return doc;
468 }
469 
470 QDomDocument QgsManageConnectionsDialog::saveOracleConnections( const QStringList &connections )
471 {
472  QDomDocument doc( "connections" );
473  QDomElement root = doc.createElement( "qgsOracleConnections" );
474  root.setAttribute( "version", "1.0" );
475  doc.appendChild( root );
476 
477  QSettings settings;
478  QString path;
479  for ( int i = 0; i < connections.count(); ++i )
480  {
481  path = "/Oracle/connections/" + connections[ i ];
482  QDomElement el = doc.createElement( "oracle" );
483  el.setAttribute( "name", connections[ i ] );
484  el.setAttribute( "host", settings.value( path + "/host", "" ).toString() );
485  el.setAttribute( "port", settings.value( path + "/port", "" ).toString() );
486  el.setAttribute( "database", settings.value( path + "/database", "" ).toString() );
487  el.setAttribute( "estimatedMetadata", settings.value( path + "/estimatedMetadata", "0" ).toString() );
488  el.setAttribute( "userTablesOnly", settings.value( path + "/userTablesOnly", "0" ).toString() );
489  el.setAttribute( "geometryColumnsOnly", settings.value( path + "/geometryColumnsOnly", "0" ).toString() );
490  el.setAttribute( "allowGeometrylessTables", settings.value( path + "/allowGeometrylessTables", "0" ).toString() );
491 
492  el.setAttribute( "saveUsername", settings.value( path + "/saveUsername", "false" ).toString() );
493 
494  if ( settings.value( path + "/saveUsername", "false" ).toString() == "true" )
495  {
496  el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
497  }
498 
499  el.setAttribute( "savePassword", settings.value( path + "/savePassword", "false" ).toString() );
500 
501  if ( settings.value( path + "/savePassword", "false" ).toString() == "true" )
502  {
503  el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
504  }
505 
506  root.appendChild( el );
507  }
508 
509  return doc;
510 }
511 
512 void QgsManageConnectionsDialog::loadOWSConnections( const QDomDocument &doc, const QStringList &items, const QString &service )
513 {
514  QDomElement root = doc.documentElement();
515  if ( root.tagName() != "qgs" + service.toUpper() + "Connections" )
516  {
517  QMessageBox::information( this, tr( "Loading connections" ),
518  tr( "The file is not an %1 connections exchange file." ).arg( service ) );
519  return;
520  }
521 
522  QString connectionName;
523  QSettings settings;
524  settings.beginGroup( "/Qgis/connections-" + service.toLower() );
525  QStringList keys = settings.childGroups();
526  settings.endGroup();
527  QDomElement child = root.firstChildElement();
528  bool prompt = true;
529  bool overwrite = true;
530 
531  while ( !child.isNull() )
532  {
533  connectionName = child.attribute( "name" );
534  if ( !items.contains( connectionName ) )
535  {
536  child = child.nextSiblingElement();
537  continue;
538  }
539 
540  // check for duplicates
541  if ( keys.contains( connectionName ) && prompt )
542  {
543  int res = QMessageBox::warning( this,
544  tr( "Loading connections" ),
545  tr( "Connection with name '%1' already exists. Overwrite?" )
546  .arg( connectionName ),
547  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
548 
549  switch ( res )
550  {
551  case QMessageBox::Cancel:
552  return;
553  case QMessageBox::No:
554  child = child.nextSiblingElement();
555  continue;
556  case QMessageBox::Yes:
557  overwrite = true;
558  break;
559  case QMessageBox::YesToAll:
560  prompt = false;
561  overwrite = true;
562  break;
563  case QMessageBox::NoToAll:
564  prompt = false;
565  overwrite = false;
566  break;
567  }
568  }
569 
570  if ( keys.contains( connectionName ) && !overwrite )
571  {
572  child = child.nextSiblingElement();
573  continue;
574  }
575 
576  // no dups detected or overwrite is allowed
577  settings.beginGroup( "/Qgis/connections-" + service.toLower() );
578  settings.setValue( QString( "/" + connectionName + "/url" ) , child.attribute( "url" ) );
579  settings.setValue( QString( "/" + connectionName + "/ignoreGetMapURI" ), child.attribute( "ignoreGetMapURI" ) == "true" );
580  settings.setValue( QString( "/" + connectionName + "/ignoreGetFeatureInfoURI" ), child.attribute( "ignoreGetFeatureInfoURI" ) == "true" );
581  settings.setValue( QString( "/" + connectionName + "/ignoreAxisOrientation" ), child.attribute( "ignoreAxisOrientation" ) == "true" );
582  settings.setValue( QString( "/" + connectionName + "/invertAxisOrientation" ), child.attribute( "invertAxisOrientation" ) == "true" );
583  settings.setValue( QString( "/" + connectionName + "/referer" ), child.attribute( "referer" ) );
584  settings.setValue( QString( "/" + connectionName + "/smoothPixmapTransform" ), child.attribute( "smoothPixmapTransform" ) == "true" );
585  settings.endGroup();
586 
587  if ( !child.attribute( "username" ).isEmpty() )
588  {
589  settings.beginGroup( "/Qgis/" + service.toUpper() + "/" + connectionName );
590  settings.setValue( "/username", child.attribute( "username" ) );
591  settings.setValue( "/password", child.attribute( "password" ) );
592  settings.endGroup();
593  }
594  child = child.nextSiblingElement();
595  }
596 }
597 
598 void QgsManageConnectionsDialog::loadWFSConnections( const QDomDocument &doc, const QStringList &items )
599 {
600  QDomElement root = doc.documentElement();
601  if ( root.tagName() != "qgsWFSConnections" )
602  {
603  QMessageBox::information( this, tr( "Loading connections" ),
604  tr( "The file is not an WFS connections exchange file." ) );
605  return;
606  }
607 
608  QString connectionName;
609  QSettings settings;
610  settings.beginGroup( "/Qgis/connections-wfs" );
611  QStringList keys = settings.childGroups();
612  settings.endGroup();
613  QDomElement child = root.firstChildElement();
614  bool prompt = true;
615  bool overwrite = true;
616 
617  while ( !child.isNull() )
618  {
619  connectionName = child.attribute( "name" );
620  if ( !items.contains( connectionName ) )
621  {
622  child = child.nextSiblingElement();
623  continue;
624  }
625 
626  // check for duplicates
627  if ( keys.contains( connectionName ) && prompt )
628  {
629  int res = QMessageBox::warning( this,
630  tr( "Loading connections" ),
631  tr( "Connection with name '%1' already exists. Overwrite?" )
632  .arg( connectionName ),
633  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
634 
635  switch ( res )
636  {
637  case QMessageBox::Cancel:
638  return;
639  case QMessageBox::No:
640  child = child.nextSiblingElement();
641  continue;
642  case QMessageBox::Yes:
643  overwrite = true;
644  break;
645  case QMessageBox::YesToAll:
646  prompt = false;
647  overwrite = true;
648  break;
649  case QMessageBox::NoToAll:
650  prompt = false;
651  overwrite = false;
652  break;
653  }
654  }
655 
656  if ( keys.contains( connectionName ) && !overwrite )
657  {
658  child = child.nextSiblingElement();
659  continue;
660  }
661 
662  // no dups detected or overwrite is allowed
663  settings.beginGroup( "/Qgis/connections-wfs" );
664  settings.setValue( QString( "/" + connectionName + "/url" ) , child.attribute( "url" ) );
665  settings.endGroup();
666 
667  if ( !child.attribute( "username" ).isEmpty() )
668  {
669  settings.beginGroup( "/Qgis/WFS/" + connectionName );
670  settings.setValue( "/username", child.attribute( "username" ) );
671  settings.setValue( "/password", child.attribute( "password" ) );
672  settings.endGroup();
673  }
674  child = child.nextSiblingElement();
675  }
676 }
677 
678 
679 void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, const QStringList &items )
680 {
681  QDomElement root = doc.documentElement();
682  if ( root.tagName() != "qgsPgConnections" )
683  {
684  QMessageBox::information( this,
685  tr( "Loading connections" ),
686  tr( "The file is not an PostGIS connections exchange file." ) );
687  return;
688  }
689 
690  QString connectionName;
691  QSettings settings;
692  settings.beginGroup( "/PostgreSQL/connections" );
693  QStringList keys = settings.childGroups();
694  settings.endGroup();
695  QDomElement child = root.firstChildElement();
696  bool prompt = true;
697  bool overwrite = true;
698 
699  while ( !child.isNull() )
700  {
701  connectionName = child.attribute( "name" );
702  if ( !items.contains( connectionName ) )
703  {
704  child = child.nextSiblingElement();
705  continue;
706  }
707 
708  // check for duplicates
709  if ( keys.contains( connectionName ) && prompt )
710  {
711  int res = QMessageBox::warning( this,
712  tr( "Loading connections" ),
713  tr( "Connection with name '%1' already exists. Overwrite?" )
714  .arg( connectionName ),
715  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
716  switch ( res )
717  {
718  case QMessageBox::Cancel:
719  return;
720  case QMessageBox::No:
721  child = child.nextSiblingElement();
722  continue;
723  case QMessageBox::Yes:
724  overwrite = true;
725  break;
726  case QMessageBox::YesToAll:
727  prompt = false;
728  overwrite = true;
729  break;
730  case QMessageBox::NoToAll:
731  prompt = false;
732  overwrite = false;
733  break;
734  }
735  }
736 
737  if ( keys.contains( connectionName ) && !overwrite )
738  {
739  child = child.nextSiblingElement();
740  continue;
741  }
742 
743  //no dups detected or overwrite is allowed
744  settings.beginGroup( "/PostgreSQL/connections/" + connectionName );
745 
746  settings.setValue( "/host", child.attribute( "host" ) );
747  settings.setValue( "/port", child.attribute( "port" ) );
748  settings.setValue( "/database", child.attribute( "database" ) );
749  if ( child.hasAttribute( "service" ) )
750  {
751  settings.setValue( "/service", child.attribute( "service" ) );
752  }
753  else
754  {
755  settings.setValue( "/service", "" );
756  }
757  settings.setValue( "/sslmode", child.attribute( "sslmode" ) );
758  settings.setValue( "/estimatedMetadata", child.attribute( "estimatedMetadata" ) );
759  settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
760  settings.setValue( "/username", child.attribute( "username" ) );
761  settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
762  settings.setValue( "/password", child.attribute( "password" ) );
763  settings.endGroup();
764 
765  child = child.nextSiblingElement();
766  }
767 }
768 
769 void QgsManageConnectionsDialog::loadMssqlConnections( const QDomDocument &doc, const QStringList &items )
770 {
771  QDomElement root = doc.documentElement();
772  if ( root.tagName() != "qgsMssqlConnections" )
773  {
774  QMessageBox::information( this,
775  tr( "Loading connections" ),
776  tr( "The file is not an MSSQL connections exchange file." ) );
777  return;
778  }
779 
780  QString connectionName;
781  QSettings settings;
782  settings.beginGroup( "/MSSQL/connections" );
783  QStringList keys = settings.childGroups();
784  settings.endGroup();
785  QDomElement child = root.firstChildElement();
786  bool prompt = true;
787  bool overwrite = true;
788 
789  while ( !child.isNull() )
790  {
791  connectionName = child.attribute( "name" );
792  if ( !items.contains( connectionName ) )
793  {
794  child = child.nextSiblingElement();
795  continue;
796  }
797 
798  // check for duplicates
799  if ( keys.contains( connectionName ) && prompt )
800  {
801  int res = QMessageBox::warning( this,
802  tr( "Loading connections" ),
803  tr( "Connection with name '%1' already exists. Overwrite?" )
804  .arg( connectionName ),
805  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
806  switch ( res )
807  {
808  case QMessageBox::Cancel:
809  return;
810  case QMessageBox::No:
811  child = child.nextSiblingElement();
812  continue;
813  case QMessageBox::Yes:
814  overwrite = true;
815  break;
816  case QMessageBox::YesToAll:
817  prompt = false;
818  overwrite = true;
819  break;
820  case QMessageBox::NoToAll:
821  prompt = false;
822  overwrite = false;
823  break;
824  }
825  }
826 
827  if ( keys.contains( connectionName ) && !overwrite )
828  {
829  child = child.nextSiblingElement();
830  continue;
831  }
832 
833  //no dups detected or overwrite is allowed
834  settings.beginGroup( "/MSSQL/connections/" + connectionName );
835 
836  settings.setValue( "/host", child.attribute( "host" ) );
837  settings.setValue( "/port", child.attribute( "port" ) );
838  settings.setValue( "/database", child.attribute( "database" ) );
839  if ( child.hasAttribute( "service" ) )
840  {
841  settings.setValue( "/service", child.attribute( "service" ) );
842  }
843  else
844  {
845  settings.setValue( "/service", "" );
846  }
847  settings.setValue( "/sslmode", child.attribute( "sslmode" ) );
848  settings.setValue( "/estimatedMetadata", child.attribute( "estimatedMetadata" ) );
849  settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
850  settings.setValue( "/username", child.attribute( "username" ) );
851  settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
852  settings.setValue( "/password", child.attribute( "password" ) );
853  settings.endGroup();
854 
855  child = child.nextSiblingElement();
856  }
857 }
858 
859 void QgsManageConnectionsDialog::loadOracleConnections( const QDomDocument &doc, const QStringList &items )
860 {
861  QDomElement root = doc.documentElement();
862  if ( root.tagName() != "qgsOracleConnections" )
863  {
864  QMessageBox::information( this,
865  tr( "Loading connections" ),
866  tr( "The file is not an Oracle connections exchange file." ) );
867  return;
868  }
869 
870  QString connectionName;
871  QSettings settings;
872  settings.beginGroup( "/Oracle/connections" );
873  QStringList keys = settings.childGroups();
874  settings.endGroup();
875  QDomElement child = root.firstChildElement();
876  bool prompt = true;
877  bool overwrite = true;
878 
879  while ( !child.isNull() )
880  {
881  connectionName = child.attribute( "name" );
882  if ( !items.contains( connectionName ) )
883  {
884  child = child.nextSiblingElement();
885  continue;
886  }
887 
888  // check for duplicates
889  if ( keys.contains( connectionName ) && prompt )
890  {
891  int res = QMessageBox::warning( this,
892  tr( "Loading connections" ),
893  tr( "Connection with name '%1' already exists. Overwrite?" )
894  .arg( connectionName ),
895  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
896  switch ( res )
897  {
898  case QMessageBox::Cancel:
899  return;
900  case QMessageBox::No:
901  child = child.nextSiblingElement();
902  continue;
903  case QMessageBox::Yes:
904  overwrite = true;
905  break;
906  case QMessageBox::YesToAll:
907  prompt = false;
908  overwrite = true;
909  break;
910  case QMessageBox::NoToAll:
911  prompt = false;
912  overwrite = false;
913  break;
914  }
915  }
916 
917  if ( keys.contains( connectionName ) && !overwrite )
918  {
919  child = child.nextSiblingElement();
920  continue;
921  }
922 
923  //no dups detected or overwrite is allowed
924  settings.beginGroup( "/Oracle/connections/" + connectionName );
925 
926  settings.setValue( "/host", child.attribute( "host" ) );
927  settings.setValue( "/port", child.attribute( "port" ) );
928  settings.setValue( "/database", child.attribute( "database" ) );
929  settings.setValue( "/estimatedMetadata", child.attribute( "estimatedMetadata" ) );
930  settings.setValue( "/userTablesOnly", child.attribute( "userTablesOnly" ) );
931  settings.setValue( "/geometryColumnsOnly", child.attribute( "geometryColumnsOnly" ) );
932  settings.setValue( "/allowGeometrylessTables", child.attribute( "allowGeometrylessTables" ) );
933  settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
934  settings.setValue( "/username", child.attribute( "username" ) );
935  settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
936  settings.setValue( "/password", child.attribute( "password" ) );
937  settings.endGroup();
938 
939  child = child.nextSiblingElement();
940  }
941 }
942 
944 {
945  listConnections->selectAll();
946 }
947 
949 {
950  listConnections->clearSelection();
951 }