~ubuntu-branches/ubuntu/utopic/qgis/utopic

« back to all changes in this revision

Viewing changes to src/app/postgres/qgspgnewconnection.cpp

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-04-24 15:12:20 UTC
  • mfrom: (3.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20120424151220-r88g00af5fpn5fc3
Tags: 1.7.4+1.7.5~20120320-1
The "Sometimes they come back" release.

* Branching from Qgis tree and adapting to current Debian Policy and
  standards. The target tree is currently set to release-1.7.
  (closes: #661491, #606304, #615683, #616182, #600308)
* Policy bumped to 3.9.3.
* Moving to debhelper compatibility level 9.
* Source format is now 3.0 with quilt support.
* Merged with 2bf42287 upstream git snapshot.
* Migrated to dh_python2 instead of python-central.
  (closes: #617048)
* Snapshot in qgis.org release-1.7: c936d031
* Added an automagic creation of a lintian override for sqlite embedding.
  This is required for uploading currently.
* Added missing ${misc:Depends} to make lintian happy.
* Copyright notes updated and debian/copyright moved to format 1.0.
* More licenses notices now reported in debian/copyright. Thanks ftpmasters.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                    qgspgnewconnection.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Sat Jun 22 2002
 
5
    copyright            : (C) 2002 by Gary E.Sherman
 
6
    email                : sherman at mrcc.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
 
 
19
#include <QSettings>
 
20
#include <QMessageBox>
 
21
#include <QInputDialog>
 
22
 
 
23
#include "qgspgnewconnection.h"
 
24
#include "qgscontexthelp.h"
 
25
#include "qgsdatasourceuri.h"
 
26
#include "qgslogger.h"
 
27
#include "qgscredentialdialog.h"
 
28
 
 
29
extern "C"
 
30
{
 
31
#include <libpq-fe.h>
 
32
}
 
33
 
 
34
QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName, Qt::WFlags fl )
 
35
    : QDialog( parent, fl ), mOriginalConnName( connName )
 
36
{
 
37
  setupUi( this );
 
38
 
 
39
  cbxSSLmode->addItem( tr( "disable" ), QgsDataSourceURI::SSLdisable );
 
40
  cbxSSLmode->addItem( tr( "allow" ), QgsDataSourceURI::SSLallow );
 
41
  cbxSSLmode->addItem( tr( "prefer" ), QgsDataSourceURI::SSLprefer );
 
42
  cbxSSLmode->addItem( tr( "require" ), QgsDataSourceURI::SSLrequire );
 
43
 
 
44
  if ( !connName.isEmpty() )
 
45
  {
 
46
    // populate the dialog with the information stored for the connection
 
47
    // populate the fields with the stored setting parameters
 
48
    QSettings settings;
 
49
 
 
50
    QString key = "/PostgreSQL/connections/" + connName;
 
51
    txtService->setText( settings.value( key + "/service" ).toString() );
 
52
    txtHost->setText( settings.value( key + "/host" ).toString() );
 
53
    QString port = settings.value( key + "/port" ).toString();
 
54
    if ( port.length() == 0 )
 
55
    {
 
56
      port = "5432";
 
57
    }
 
58
    txtPort->setText( port );
 
59
    txtDatabase->setText( settings.value( key + "/database" ).toString() );
 
60
    cb_publicSchemaOnly->setChecked( settings.value( key + "/publicOnly", false ).toBool() );
 
61
    cb_geometryColumnsOnly->setChecked( settings.value( key + "/geometrycolumnsOnly", false ).toBool() );
 
62
    cb_allowGeometrylessTables->setChecked( settings.value( key + "/allowGeometrylessTables", false ).toBool() );
 
63
    // Ensure that cb_publicSchemaOnly is set correctly
 
64
    on_cb_geometryColumnsOnly_clicked();
 
65
 
 
66
    cb_useEstimatedMetadata->setChecked( settings.value( key + "/estimatedMetadata", false ).toBool() );
 
67
 
 
68
    cbxSSLmode->setCurrentIndex( cbxSSLmode->findData( settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() ) );
 
69
 
 
70
    if ( settings.value( key + "/saveUsername" ).toString() == "true" )
 
71
    {
 
72
      txtUsername->setText( settings.value( key + "/username" ).toString() );
 
73
      chkStoreUsername->setChecked( true );
 
74
    }
 
75
 
 
76
    if ( settings.value( key + "/savePassword" ).toString() == "true" )
 
77
    {
 
78
      txtPassword->setText( settings.value( key + "/password" ).toString() );
 
79
      chkStorePassword->setChecked( true );
 
80
    }
 
81
 
 
82
    // Old save setting
 
83
    if ( settings.contains( key + "/save" ) )
 
84
    {
 
85
      txtUsername->setText( settings.value( key + "/username" ).toString() );
 
86
      chkStoreUsername->setChecked( !txtUsername->text().isEmpty() );
 
87
 
 
88
      if ( settings.value( key + "/save" ).toString() == "true" )
 
89
        txtPassword->setText( settings.value( key + "/password" ).toString() );
 
90
 
 
91
      chkStorePassword->setChecked( true );
 
92
    }
 
93
 
 
94
    txtName->setText( connName );
 
95
  }
 
96
}
 
97
/** Autoconnected SLOTS **/
 
98
void QgsPgNewConnection::accept()
 
99
{
 
100
  QSettings settings;
 
101
  QString baseKey = "/PostgreSQL/connections/";
 
102
  settings.setValue( baseKey + "selected", txtName->text() );
 
103
 
 
104
  // warn if entry was renamed to an existing connection
 
105
  if (( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
 
106
      ( settings.contains( baseKey + txtName->text() + "/service" ) ||
 
107
        settings.contains( baseKey + txtName->text() + "/host" ) ) &&
 
108
      QMessageBox::question( this,
 
109
                             tr( "Save connection" ),
 
110
                             tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
 
111
                             QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
 
112
  {
 
113
    return;
 
114
  }
 
115
 
 
116
  // on rename delete the original entry first
 
117
  if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
 
118
  {
 
119
 
 
120
    settings.remove( baseKey + mOriginalConnName );
 
121
  }
 
122
 
 
123
  baseKey += txtName->text();
 
124
  settings.setValue( baseKey + "/service", txtService->text() );
 
125
  settings.setValue( baseKey + "/host", txtHost->text() );
 
126
  settings.setValue( baseKey + "/port", txtPort->text() );
 
127
  settings.setValue( baseKey + "/database", txtDatabase->text() );
 
128
  settings.setValue( baseKey + "/username", chkStoreUsername->isChecked() ? txtUsername->text() : "" );
 
129
  settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
 
130
  settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() );
 
131
  settings.setValue( baseKey + "/geometryColumnsOnly", cb_geometryColumnsOnly->isChecked() );
 
132
  settings.setValue( baseKey + "/allowGeometrylessTables", cb_allowGeometrylessTables->isChecked() );
 
133
  settings.setValue( baseKey + "/sslmode", cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
 
134
  settings.setValue( baseKey + "/saveUsername", chkStoreUsername->isChecked() ? "true" : "false" );
 
135
  settings.setValue( baseKey + "/savePassword", chkStorePassword->isChecked() ? "true" : "false" );
 
136
  settings.setValue( baseKey + "/estimatedMetadata", cb_useEstimatedMetadata->isChecked() );
 
137
 
 
138
  // remove old save setting
 
139
  settings.remove( baseKey + "/save" );
 
140
 
 
141
  QDialog::accept();
 
142
}
 
143
 
 
144
void QgsPgNewConnection::on_btnConnect_clicked()
 
145
{
 
146
  testConnection();
 
147
}
 
148
 
 
149
void QgsPgNewConnection::on_cb_geometryColumnsOnly_clicked()
 
150
{
 
151
  if ( cb_geometryColumnsOnly->checkState() == Qt::Checked )
 
152
    cb_publicSchemaOnly->setEnabled( false );
 
153
  else
 
154
    cb_publicSchemaOnly->setEnabled( true );
 
155
}
 
156
 
 
157
/** end  Autoconnected SLOTS **/
 
158
 
 
159
QgsPgNewConnection::~QgsPgNewConnection()
 
160
{
 
161
}
 
162
 
 
163
void QgsPgNewConnection::testConnection()
 
164
{
 
165
  QgsDataSourceURI uri;
 
166
  if ( !txtService->text().isEmpty() )
 
167
  {
 
168
    uri.setConnection( txtService->text(), txtDatabase->text(),
 
169
                       txtUsername->text(), txtPassword->text(),
 
170
                       ( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
 
171
  }
 
172
  else
 
173
  {
 
174
    uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(),
 
175
                       txtUsername->text(), txtPassword->text(),
 
176
                       ( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
 
177
  }
 
178
  QString conninfo = uri.connectionInfo();
 
179
  QgsDebugMsg( "PQconnectdb(\"" + conninfo + "\");" );
 
180
 
 
181
  PGconn *pd = PQconnectdb( conninfo.toLocal8Bit() );  // use what is set based on locale; after connecting, use Utf8
 
182
  // check the connection status
 
183
  if ( PQstatus( pd ) != CONNECTION_OK )
 
184
  {
 
185
    QString username = txtUsername->text();
 
186
    QString password = txtPassword->text();
 
187
 
 
188
    uri.setUsername( "" );
 
189
    uri.setPassword( "" );
 
190
 
 
191
    while ( PQstatus( pd ) != CONNECTION_OK )
 
192
    {
 
193
      bool ok = QgsCredentials::instance()->get( conninfo, username, password, QString::fromUtf8( PQerrorMessage( pd ) ) );
 
194
      if ( !ok )
 
195
        break;
 
196
 
 
197
      ::PQfinish( pd );
 
198
 
 
199
      QgsDataSourceURI uri( conninfo );
 
200
 
 
201
      if ( !username.isEmpty() )
 
202
        uri.setUsername( username );
 
203
 
 
204
      if ( !password.isEmpty() )
 
205
        uri.setPassword( password );
 
206
 
 
207
      QgsDebugMsg( "PQconnectdb(\"" + uri.connectionInfo() + "\");" );
 
208
      pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() );
 
209
    }
 
210
 
 
211
    if ( PQstatus( pd ) == CONNECTION_OK )
 
212
    {
 
213
      if ( chkStoreUsername->isChecked() )
 
214
        txtUsername->setText( username );
 
215
      if ( chkStorePassword->isChecked() )
 
216
        txtPassword->setText( password );
 
217
 
 
218
      QgsCredentials::instance()->put( conninfo, username, password );
 
219
    }
 
220
  }
 
221
 
 
222
  if ( PQstatus( pd ) == CONNECTION_OK )
 
223
  {
 
224
    // Database successfully opened; we can now issue SQL commands.
 
225
    QMessageBox::information( this,
 
226
                              tr( "Test connection" ),
 
227
                              tr( "Connection to %1 was successful" ).arg( txtDatabase->text() ) );
 
228
  }
 
229
  else
 
230
  {
 
231
    QMessageBox::information( this,
 
232
                              tr( "Test connection" ),
 
233
                              tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" )
 
234
                              .arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) );
 
235
  }
 
236
  // free pg connection resources
 
237
  PQfinish( pd );
 
238
}