|
| 1 | +/*************************************************************************** |
| 2 | + qgsnewogrconnection.cpp - description |
| 3 | + ------------------- |
| 4 | + begin : Mon Jan 2 2009 |
| 5 | + copyright : (C) 2009 by Godofredo Contreras Nava |
| 6 | + email : frdcn at hotmail.com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | +/* $Id: $ */ |
| 18 | +#include <iostream> |
| 19 | + |
| 20 | +#include <QSettings> |
| 21 | +#include <QMessageBox> |
| 22 | +#include <QInputDialog> |
| 23 | + |
| 24 | +#include "qgsnewogrconnection.h" |
| 25 | +#include "qgscontexthelp.h" |
| 26 | +#include "qgslogger.h" |
| 27 | +#include "qgsproviderregistry.h" |
| 28 | +#include "qgsogrhelperfunctions.h" |
| 29 | +#include <ogr_api.h> |
| 30 | +#include <cpl_error.h> |
| 31 | + |
| 32 | + |
| 33 | +QgsNewOgrConnection::QgsNewOgrConnection( QWidget *parent,const QString& connType, const QString& connName, Qt::WFlags fl ) |
| 34 | + : QDialog( parent, fl ) |
| 35 | +{ |
| 36 | + setupUi( this ); |
| 37 | + //add database drivers |
| 38 | + QStringList dbDrivers=QgsProviderRegistry::instance()->databaseDrivers().split(";"); |
| 39 | + for(int i=0;i<dbDrivers.count();i++) |
| 40 | + { |
| 41 | + QString dbDrive=dbDrivers.at(i); |
| 42 | + cmbDatabaseTypes->addItem(dbDrive.split(",").at(0)); |
| 43 | + } |
| 44 | + txtName->setEnabled(true); |
| 45 | + cmbDatabaseTypes->setEnabled(true); |
| 46 | + if ( !connName.isEmpty() ) |
| 47 | + { |
| 48 | + // populate the dialog with the information stored for the connection |
| 49 | + // populate the fields with the stored setting parameters |
| 50 | + QSettings settings; |
| 51 | + QString key = "/"+connType+"/connections/" + connName; |
| 52 | + txtHost->setText( settings.value( key + "/host" ).toString() ); |
| 53 | + txtDatabase->setText( settings.value( key + "/database" ).toString() ); |
| 54 | + QString port = settings.value( key + "/port" ).toString(); |
| 55 | + txtPort->setText( port ); |
| 56 | + txtUsername->setText( settings.value( key + "/username" ).toString() ); |
| 57 | + if ( settings.value( key + "/save" ).toString() == "true" ) |
| 58 | + { |
| 59 | + txtPassword->setText( settings.value( key + "/password" ).toString() ); |
| 60 | + chkStorePassword->setChecked( true ); |
| 61 | + } |
| 62 | + cmbDatabaseTypes->setCurrentIndex(cmbDatabaseTypes->findText(connType)); |
| 63 | + txtName->setText( connName ); |
| 64 | + txtName->setEnabled(false); |
| 65 | + cmbDatabaseTypes->setEnabled(false); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +QgsNewOgrConnection::~QgsNewOgrConnection() |
| 70 | +{ |
| 71 | +} |
| 72 | + |
| 73 | +void QgsNewOgrConnection::testConnection() |
| 74 | +{ |
| 75 | + QString uri; |
| 76 | + uri=createDatabaseURI(cmbDatabaseTypes->currentText(), txtHost->text(), |
| 77 | + txtDatabase->text(), txtPort->text(), |
| 78 | + txtUsername->text(), txtPassword->text() ); |
| 79 | + QgsDebugMsg( "Connecting using uri = " + uri ); |
| 80 | + OGRRegisterAll(); |
| 81 | + OGRDataSourceH poDS; |
| 82 | + OGRSFDriverH pahDriver; |
| 83 | + CPLErrorReset(); |
| 84 | + poDS = OGROpen( QFile::encodeName( uri ).constData(), FALSE, &pahDriver ); |
| 85 | + if( poDS == NULL ) |
| 86 | + { |
| 87 | + QMessageBox::information( this, tr( "Test connection" ), tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( CPLGetLastErrorMsg() ) ); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + QMessageBox::information( this, tr( "Test connection" ), tr( "Connection to %1 was successful" ).arg( uri ) ); |
| 92 | + } |
| 93 | + OGRReleaseDataSource(poDS); |
| 94 | +} |
| 95 | + |
| 96 | +void QgsNewOgrConnection::saveConnection() |
| 97 | +{ |
| 98 | + QSettings settings; |
| 99 | + QString baseKey = "/"+cmbDatabaseTypes->currentText()+"/connections/"; |
| 100 | + settings.setValue( baseKey + "selected", txtName->text() ); |
| 101 | + baseKey += txtName->text(); |
| 102 | + settings.setValue( baseKey + "/host", txtHost->text() ); |
| 103 | + settings.setValue( baseKey + "/database", txtDatabase->text() ); |
| 104 | + settings.setValue( baseKey + "/port", txtPort->text() ); |
| 105 | + settings.setValue( baseKey + "/username", txtUsername->text() ); |
| 106 | + settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" ); |
| 107 | + settings.setValue( baseKey + "/save", chkStorePassword->isChecked() ? "true" : "false" ); |
| 108 | + accept(); |
| 109 | +} |
| 110 | + |
| 111 | +void QgsNewOgrConnection::helpInfo() |
| 112 | +{ |
| 113 | + QgsContextHelp::run( context_id ); |
| 114 | +} |
| 115 | + |
| 116 | +/** Autoconnected SLOTS **/ |
| 117 | +void QgsNewOgrConnection::on_btnOk_clicked() |
| 118 | +{ |
| 119 | + saveConnection(); |
| 120 | +} |
| 121 | + |
| 122 | +void QgsNewOgrConnection::on_btnHelp_clicked() |
| 123 | +{ |
| 124 | + helpInfo(); |
| 125 | +} |
| 126 | + |
| 127 | +void QgsNewOgrConnection::on_btnConnect_clicked() |
| 128 | +{ |
| 129 | + testConnection(); |
| 130 | +} |
| 131 | + |
| 132 | +void QgsNewOgrConnection::on_btnCancel_clicked() |
| 133 | +{ |
| 134 | + reject(); |
| 135 | +} |
| 136 | +/** end Autoconnected SLOTS **/ |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
0 commit comments