18 #include <QCloseEvent> 19 #include <QFileDialog> 20 #include <QMessageBox> 21 #include <QPushButton> 23 #include <QTextStream> 29 , mFileName( fileName )
31 , mConnectionType( type )
38 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
42 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
45 if ( mDialogMode ==
Import )
47 label->setText(
tr(
"Select connections to import" ) );
48 buttonBox->button( QDialogButtonBox::Ok )->setText(
tr(
"Import" ) );
49 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
54 buttonBox->button( QDialogButtonBox::Ok )->setText(
tr(
"Export" ) );
55 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
58 if ( !populateConnections() )
72 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !listConnections->selectedItems().isEmpty() );
81 tr(
"You should select at least one connection from list." ) );
87 for (
int i = 0; i < selection.
size(); ++i )
89 items.
append( selection.
at( i )->text() );
92 if ( mDialogMode ==
Export )
95 tr(
"XML files (*.xml *.XML)" ) );
102 if ( !fileName.
endsWith(
".xml", Qt::CaseInsensitive ) )
107 mFileName = fileName;
110 switch ( mConnectionType )
113 doc = saveOWSConnections( items,
"WMS" );
116 doc = saveWFSConnections( items );
119 doc = savePgConnections( items );
122 doc = saveMssqlConnections( items );
125 doc = saveOWSConnections( items,
"WCS" );
128 doc = saveOracleConnections( items );
131 doc = saveDb2Connections( items );
135 QFile file( mFileName );
136 if ( !file.
open( QIODevice::WriteOnly | QIODevice::Text ) )
139 tr(
"Cannot write file %1:\n%2." )
150 QFile file( mFileName );
151 if ( !file.
open( QIODevice::ReadOnly | QIODevice::Text ) )
154 tr(
"Cannot read file %1:\n%2." )
165 if ( !doc.
setContent( &file,
true, &errorStr, &errorLine, &errorColumn ) )
168 tr(
"Parse error at line %1, column %2:\n%3" )
175 switch ( mConnectionType )
178 loadOWSConnections( doc, items,
"WMS" );
181 loadWFSConnections( doc, items );
184 loadPgConnections( doc, items );
187 loadMssqlConnections( doc, items );
190 loadOWSConnections( doc, items,
"WCS" );
193 loadOracleConnections( doc, items );
196 loadDb2Connections( doc, items );
200 listConnections->clear();
207 bool QgsManageConnectionsDialog::populateConnections()
210 if ( mDialogMode ==
Export )
213 switch ( mConnectionType )
216 settings.
beginGroup(
"/Qgis/connections-wms" );
219 settings.
beginGroup(
"/Qgis/connections-wfs" );
222 settings.
beginGroup(
"/Qgis/connections-wcs" );
225 settings.
beginGroup(
"/PostgreSQL/connections" );
239 while ( it != keys.
end() )
243 listConnections->addItem( item );
251 QFile file( mFileName );
252 if ( !file.
open( QIODevice::ReadOnly | QIODevice::Text ) )
255 tr(
"Cannot read file %1:\n%2." )
266 if ( !doc.
setContent( &file,
true, &errorStr, &errorLine, &errorColumn ) )
269 tr(
"Parse error at line %1, column %2:\n%3" )
277 switch ( mConnectionType )
280 if ( root.
tagName() !=
"qgsWMSConnections" )
283 tr(
"The file is not an WMS connections exchange file." ) );
289 if ( root.
tagName() !=
"qgsWFSConnections" )
292 tr(
"The file is not an WFS connections exchange file." ) );
298 if ( root.
tagName() !=
"qgsWCSConnections" )
301 tr(
"The file is not an WCS connections exchange file." ) );
307 if ( root.
tagName() !=
"qgsPgConnections" )
310 tr(
"The file is not an PostGIS connections exchange file." ) );
316 if ( root.
tagName() !=
"qgsMssqlConnections" )
319 tr(
"The file is not an MSSQL connections exchange file." ) );
324 if ( root.
tagName() !=
"qgsOracleConnections" )
327 tr(
"The file is not an Oracle connections exchange file." ) );
332 if ( root.
tagName() !=
"qgsDb2Connections" )
335 tr(
"The file is not an DB2 connections exchange file." ) );
346 listConnections->addItem( item );
362 for (
int i = 0; i < connections.
count(); ++i )
364 path =
"/Qgis/connections-" + service.
toLower() +
'/';
369 if ( service ==
"WMS" )
371 el.
setAttribute(
"ignoreGetMapURI", settings.
value( path + connections[i] +
"/ignoreGetMapURI",
false ).
toBool() ?
"true" :
"false" );
372 el.
setAttribute(
"ignoreGetFeatureInfoURI", settings.
value( path + connections[i] +
"/ignoreGetFeatureInfoURI",
false ).
toBool() ?
"true" :
"false" );
373 el.
setAttribute(
"ignoreAxisOrientation", settings.
value( path + connections[i] +
"/ignoreAxisOrientation",
false ).
toBool() ?
"true" :
"false" );
374 el.
setAttribute(
"invertAxisOrientation", settings.
value( path + connections[i] +
"/invertAxisOrientation",
false ).
toBool() ?
"true" :
"false" );
376 el.
setAttribute(
"smoothPixmapTransform", settings.
value( path + connections[i] +
"/smoothPixmapTransform",
false ).
toBool() ?
"true" :
"false" );
380 path =
"/Qgis/" + service.
toUpper() +
'/';
398 for (
int i = 0; i < connections.
count(); ++i )
400 path =
"/Qgis/connections-wfs/";
425 for (
int i = 0; i < connections.
count(); ++i )
427 path =
"/PostgreSQL/connections/" + connections[ i ];
439 if ( settings.
value( path +
"/saveUsername",
"false" ).
toString() ==
"true" )
446 if ( settings.
value( path +
"/savePassword",
"false" ).
toString() ==
"true" )
466 for (
int i = 0; i < connections.
count(); ++i )
468 path =
"/MSSQL/connections/" + connections[ i ];
480 if ( settings.
value( path +
"/saveUsername",
"false" ).
toString() ==
"true" )
487 if ( settings.
value( path +
"/savePassword",
"false" ).
toString() ==
"true" )
507 for (
int i = 0; i < connections.
count(); ++i )
509 path =
"/Oracle/connections/" + connections[ i ];
524 if ( settings.
value( path +
"/saveUsername",
"false" ).
toString() ==
"true" )
531 if ( settings.
value( path +
"/savePassword",
"false" ).
toString() ==
"true" )
551 for (
int i = 0; i < connections.
count(); ++i )
553 path =
"/DB2/connections/" + connections[ i ];
565 if ( settings.
value( path +
"/saveUsername",
"false" ).
toString() ==
"true" )
572 if ( settings.
value( path +
"/savePassword",
"false" ).
toString() ==
"true" )
586 if ( root.
tagName() !=
"qgs" + service.
toUpper() +
"Connections" )
589 tr(
"The file is not an %1 connections exchange file." ).arg( service ) );
600 bool overwrite =
true;
604 connectionName = child.
attribute(
"name" );
605 if ( !items.
contains( connectionName ) )
612 if ( keys.
contains( connectionName ) && prompt )
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 );
622 case QMessageBox::Cancel:
624 case QMessageBox::No:
627 case QMessageBox::Yes:
630 case QMessageBox::YesToAll:
634 case QMessageBox::NoToAll:
641 if ( keys.
contains( connectionName ) && !overwrite )
650 settings.
setValue(
QString(
'/' + connectionName +
"/ignoreGetMapURI" ), child.
attribute(
"ignoreGetMapURI" ) ==
"true" );
651 settings.
setValue(
QString(
'/' + connectionName +
"/ignoreGetFeatureInfoURI" ), child.
attribute(
"ignoreGetFeatureInfoURI" ) ==
"true" );
652 settings.
setValue(
QString(
'/' + connectionName +
"/ignoreAxisOrientation" ), child.
attribute(
"ignoreAxisOrientation" ) ==
"true" );
653 settings.
setValue(
QString(
'/' + connectionName +
"/invertAxisOrientation" ), child.
attribute(
"invertAxisOrientation" ) ==
"true" );
655 settings.
setValue(
QString(
'/' + connectionName +
"/smoothPixmapTransform" ), child.
attribute(
"smoothPixmapTransform" ) ==
"true" );
673 if ( root.
tagName() !=
"qgsWFSConnections" )
676 tr(
"The file is not an WFS connections exchange file." ) );
682 settings.
beginGroup(
"/Qgis/connections-wfs" );
687 bool overwrite =
true;
691 connectionName = child.
attribute(
"name" );
692 if ( !items.
contains( connectionName ) )
699 if ( keys.
contains( connectionName ) && prompt )
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 );
709 case QMessageBox::Cancel:
711 case QMessageBox::No:
714 case QMessageBox::Yes:
717 case QMessageBox::YesToAll:
721 case QMessageBox::NoToAll:
728 if ( keys.
contains( connectionName ) && !overwrite )
735 settings.
beginGroup(
"/Qgis/connections-wfs" );
741 settings.
beginGroup(
"/Qgis/WFS/" + connectionName );
754 if ( root.
tagName() !=
"qgsPgConnections" )
757 tr(
"Loading connections" ),
758 tr(
"The file is not an PostGIS connections exchange file." ) );
764 settings.
beginGroup(
"/PostgreSQL/connections" );
769 bool overwrite =
true;
773 connectionName = child.
attribute(
"name" );
774 if ( !items.
contains( connectionName ) )
781 if ( keys.
contains( connectionName ) && prompt )
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 );
790 case QMessageBox::Cancel:
792 case QMessageBox::No:
795 case QMessageBox::Yes:
798 case QMessageBox::YesToAll:
802 case QMessageBox::NoToAll:
809 if ( keys.
contains( connectionName ) && !overwrite )
816 settings.
beginGroup(
"/PostgreSQL/connections/" + connectionName );
827 settings.
setValue(
"/service",
"" );
830 settings.
setValue(
"/estimatedMetadata", child.
attribute(
"estimatedMetadata" ) );
844 if ( root.
tagName() !=
"qgsMssqlConnections" )
847 tr(
"Loading connections" ),
848 tr(
"The file is not an MSSQL connections exchange file." ) );
859 bool overwrite =
true;
863 connectionName = child.
attribute(
"name" );
864 if ( !items.
contains( connectionName ) )
871 if ( keys.
contains( connectionName ) && prompt )
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 );
880 case QMessageBox::Cancel:
882 case QMessageBox::No:
885 case QMessageBox::Yes:
888 case QMessageBox::YesToAll:
892 case QMessageBox::NoToAll:
899 if ( keys.
contains( connectionName ) && !overwrite )
906 settings.
beginGroup(
"/MSSQL/connections/" + connectionName );
917 settings.
setValue(
"/service",
"" );
920 settings.
setValue(
"/estimatedMetadata", child.
attribute(
"estimatedMetadata" ) );
934 if ( root.
tagName() !=
"qgsOracleConnections" )
937 tr(
"Loading connections" ),
938 tr(
"The file is not an Oracle connections exchange file." ) );
949 bool overwrite =
true;
953 connectionName = child.
attribute(
"name" );
954 if ( !items.
contains( connectionName ) )
961 if ( keys.
contains( connectionName ) && prompt )
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 );
970 case QMessageBox::Cancel:
972 case QMessageBox::No:
975 case QMessageBox::Yes:
978 case QMessageBox::YesToAll:
982 case QMessageBox::NoToAll:
989 if ( keys.
contains( connectionName ) && !overwrite )
996 settings.
beginGroup(
"/Oracle/connections/" + connectionName );
1003 settings.
setValue(
"/estimatedMetadata", child.
attribute(
"estimatedMetadata" ) );
1005 settings.
setValue(
"/geometryColumnsOnly", child.
attribute(
"geometryColumnsOnly" ) );
1006 settings.
setValue(
"/allowGeometrylessTables", child.
attribute(
"allowGeometrylessTables" ) );
1020 if ( root.
tagName() !=
"qgsDb2Connections" )
1023 tr(
"Loading connections" ),
1024 tr(
"The file is not an DB2 connections exchange file." ) );
1035 bool overwrite =
true;
1037 while ( !child.
isNull() )
1039 connectionName = child.
attribute(
"name" );
1040 if ( !items.
contains( connectionName ) )
1047 if ( keys.
contains( connectionName ) && prompt )
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 );
1056 case QMessageBox::Cancel:
1058 case QMessageBox::No:
1061 case QMessageBox::Yes:
1064 case QMessageBox::YesToAll:
1068 case QMessageBox::NoToAll:
1075 if ( keys.
contains( connectionName ) && !overwrite )
1082 settings.
beginGroup(
"/DB2/connections/" + connectionName );
1093 settings.
setValue(
"/service",
"" );
1096 settings.
setValue(
"/estimatedMetadata", child.
attribute(
"estimatedMetadata" ) );
1108 listConnections->selectAll();
1109 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !listConnections->selectedItems().isEmpty() );
1114 listConnections->clearSelection();
1115 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
QDomNode appendChild(const QDomNode &newChild)
QString attribute(const QString &name, const QString &defValue) const
QString errorString() const
const T & at(int i) const
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QDomElement nextSiblingElement(const QString &tagName) const
QDomElement documentElement() const
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QString tr(const char *sourceText, const char *disambiguation, int n)
void setValue(const QString &key, const QVariant &value)
int count(const T &value) const
void append(const T &value)
int toInt(bool *ok) const
bool hasAttribute(const QString &name) const
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
QStringList childGroups() const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QVariant value(const QString &key, const QVariant &defaultValue) const
void save(QTextStream &str, int indent) const
QDomElement firstChildElement(const QString &tagName) const
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
QDomElement createElement(const QString &tagName)
QgsManageConnectionsDialog(QWidget *parent=nullptr, Mode mode=Export, Type type=WMS, const QString &fileName="")
void postEvent(QObject *receiver, QEvent *event)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void beginGroup(const QString &prefix)
void setText(const QString &text)
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)