1
/***************************************************************************
2
qgsnewconnection.cpp - description
4
begin : Sat Jun 22 2002
5
copyright : (C) 2002 by Gary E.Sherman
6
email : sherman at mrcc.com
7
***************************************************************************/
9
/***************************************************************************
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. *
16
***************************************************************************/
17
/* $Id: qgsnewconnection.cpp 6113 2006-11-18 20:44:53Z g_j_m $ */
21
#include <QMessageBox>
23
#include "qgsnewconnection.h"
24
#include "qgscontexthelp.h"
29
QgsNewConnection::QgsNewConnection(QWidget *parent, const QString& connName, Qt::WFlags fl)
33
if (!connName.isEmpty())
35
// populate the dialog with the information stored for the connection
36
// populate the fields with the stored setting parameters
39
QString key = "/PostgreSQL/connections/" + connName;
40
txtHost->setText(settings.readEntry(key + "/host"));
41
txtDatabase->setText(settings.readEntry(key + "/database"));
42
QString port = settings.readEntry(key + "/port");
43
if(port.length() ==0){
46
txtPort->setText(port);
47
txtUsername->setText(settings.readEntry(key + "/username"));
48
Qt::CheckState s = Qt::Checked;
49
if ( ! settings.readBoolEntry(key + "/publicOnly", false))
51
cb_publicSchemaOnly->setCheckState(s);
53
if ( ! settings.readBoolEntry(key + "/geometrycolumnsOnly", false))
55
cb_geometryColumnsOnly->setCheckState(s);
56
// Ensure that cb_plublicSchemaOnly is set correctly
57
on_cb_geometryColumnsOnly_clicked();
59
if (settings.readEntry(key + "/save") == "true")
61
txtPassword->setText(settings.readEntry(key + "/password"));
62
chkStorePassword->setChecked(true);
64
txtName->setText(connName);
67
/** Autoconnected SLOTS **/
68
void QgsNewConnection::on_btnOk_clicked()
72
void QgsNewConnection::on_btnHelp_clicked()
76
void QgsNewConnection::on_btnConnect_clicked()
80
void QgsNewConnection::on_btnCancel_clicked(){
84
void QgsNewConnection::on_cb_geometryColumnsOnly_clicked()
86
if (cb_geometryColumnsOnly->checkState() == Qt::Checked)
87
cb_publicSchemaOnly->setEnabled(false);
89
cb_publicSchemaOnly->setEnabled(true);
92
/** end Autoconnected SLOTS **/
94
QgsNewConnection::~QgsNewConnection()
97
void QgsNewConnection::testConnection()
99
// following line uses Qt SQL plugin - currently not used
100
// QSqlDatabase *testCon = QSqlDatabase::addDatabase("QPSQL7","testconnection");
102
// Need to escape the password to allow for single quotes and backslashes
103
QString password = txtPassword->text();
104
password.replace('\\', "\\\\");
105
password.replace('\'', "\\'");
108
"host=" + txtHost->text() +
109
" dbname=" + txtDatabase->text() +
110
" port=" + txtPort->text() +
111
" user=" + txtUsername->text() +
112
" password='" + password + "'";
113
PGconn *pd = PQconnectdb(connInfo.toLocal8Bit().data());
114
// std::cout << pd->ErrorMessage();
115
if (PQstatus(pd) == CONNECTION_OK)
117
// Database successfully opened; we can now issue SQL commands.
118
QMessageBox::information(this, tr("Test connection"), tr("Connection to %1 was successfull").arg(txtDatabase->text()));
121
QMessageBox::information(this, tr("Test connection"), tr("Connection failed - Check settings and try again.\n\nExtended error information:\n") + QString(PQerrorMessage(pd)) );
123
// free pg connection resources
129
void QgsNewConnection::saveConnection()
132
QString baseKey = "/PostgreSQL/connections/";
133
settings.writeEntry(baseKey + "selected", txtName->text());
134
baseKey += txtName->text();
135
settings.writeEntry(baseKey + "/host", txtHost->text());
136
settings.writeEntry(baseKey + "/database", txtDatabase->text());
137
settings.writeEntry(baseKey + "/port", txtPort->text());
138
settings.writeEntry(baseKey + "/username", txtUsername->text());
139
settings.writeEntry(baseKey + "/password", txtPassword->text());
140
settings.writeEntry(baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked());
141
settings.writeEntry(baseKey + "/geometryColumnsOnly", cb_geometryColumnsOnly->isChecked());
142
if (chkStorePassword->isChecked())
144
settings.writeEntry(baseKey + "/save", "true");
147
settings.writeEntry(baseKey + "/save", "false");
151
void QgsNewConnection::helpInfo()
153
QgsContextHelp::run(context_id);
155
/* void QgsNewConnection::saveConnection()
158
QString baseKey = "/PostgreSQL/connections/";
159
baseKey += txtName->text();
160
settings.writeEntry(baseKey + "/host", txtHost->text());
161
settings.writeEntry(baseKey + "/database", txtDatabase->text());
163
settings.writeEntry(baseKey + "/username", txtUsername->text());
164
if (chkStorePassword->isChecked()) {
165
settings.writeEntry(baseKey + "/password", txtPassword->text());
167
settings.writeEntry(baseKey + "/password", "");