~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to src/gui/qgsnewconnection.cpp

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                    qgsnewconnection.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: qgsnewconnection.cpp 6113 2006-11-18 20:44:53Z g_j_m $ */
 
18
#include <iostream>
 
19
 
 
20
#include <QSettings>
 
21
#include <QMessageBox>
 
22
 
 
23
#include "qgsnewconnection.h"
 
24
#include "qgscontexthelp.h"
 
25
extern "C"
 
26
{
 
27
#include <libpq-fe.h>
 
28
}
 
29
QgsNewConnection::QgsNewConnection(QWidget *parent, const QString& connName, Qt::WFlags fl)
 
30
: QDialog(parent, fl)
 
31
{
 
32
  setupUi(this);
 
33
  if (!connName.isEmpty())
 
34
    {
 
35
      // populate the dialog with the information stored for the connection
 
36
      // populate the fields with the stored setting parameters
 
37
      QSettings settings;
 
38
 
 
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){
 
44
        port = "5432";
 
45
      }
 
46
      txtPort->setText(port);
 
47
      txtUsername->setText(settings.readEntry(key + "/username"));
 
48
      Qt::CheckState s = Qt::Checked;
 
49
      if ( ! settings.readBoolEntry(key + "/publicOnly", false))
 
50
        s = Qt::Unchecked;
 
51
      cb_publicSchemaOnly->setCheckState(s);
 
52
      s = Qt::Checked;
 
53
      if ( ! settings.readBoolEntry(key + "/geometrycolumnsOnly", false))
 
54
        s = Qt::Unchecked;
 
55
      cb_geometryColumnsOnly->setCheckState(s);
 
56
      // Ensure that cb_plublicSchemaOnly is set correctly
 
57
      on_cb_geometryColumnsOnly_clicked();
 
58
 
 
59
      if (settings.readEntry(key + "/save") == "true")
 
60
        {
 
61
          txtPassword->setText(settings.readEntry(key + "/password"));
 
62
          chkStorePassword->setChecked(true);
 
63
        }
 
64
      txtName->setText(connName);
 
65
    }
 
66
}
 
67
/** Autoconnected SLOTS **/
 
68
void QgsNewConnection::on_btnOk_clicked()
 
69
{
 
70
  saveConnection();
 
71
}
 
72
void QgsNewConnection::on_btnHelp_clicked()
 
73
{
 
74
 helpInfo();
 
75
}
 
76
void QgsNewConnection::on_btnConnect_clicked()
 
77
{
 
78
  testConnection();
 
79
}
 
80
void QgsNewConnection::on_btnCancel_clicked(){
 
81
  // cancel the dialog
 
82
  reject();
 
83
}
 
84
void QgsNewConnection::on_cb_geometryColumnsOnly_clicked()
 
85
{
 
86
  if (cb_geometryColumnsOnly->checkState() == Qt::Checked)
 
87
    cb_publicSchemaOnly->setEnabled(false);
 
88
  else
 
89
    cb_publicSchemaOnly->setEnabled(true);
 
90
}
 
91
 
 
92
/** end  Autoconnected SLOTS **/
 
93
 
 
94
QgsNewConnection::~QgsNewConnection()
 
95
{
 
96
}
 
97
void QgsNewConnection::testConnection()
 
98
{
 
99
  // following line uses Qt SQL plugin - currently not used
 
100
  // QSqlDatabase *testCon = QSqlDatabase::addDatabase("QPSQL7","testconnection");
 
101
 
 
102
  // Need to escape the password to allow for single quotes and backslashes
 
103
  QString password = txtPassword->text();
 
104
  password.replace('\\', "\\\\");
 
105
  password.replace('\'', "\\'");
 
106
 
 
107
  QString connInfo =
 
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)
 
116
    {
 
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()));
 
119
  } else
 
120
    {
 
121
      QMessageBox::information(this, tr("Test connection"), tr("Connection failed - Check settings and try again.\n\nExtended error information:\n") + QString(PQerrorMessage(pd)) );
 
122
    }
 
123
  // free pg connection resources
 
124
  PQfinish(pd);
 
125
 
 
126
 
 
127
}
 
128
 
 
129
void QgsNewConnection::saveConnection()
 
130
{
 
131
  QSettings settings; 
 
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())
 
143
    {
 
144
      settings.writeEntry(baseKey + "/save", "true");
 
145
  } else
 
146
    {
 
147
      settings.writeEntry(baseKey + "/save", "false");
 
148
    }
 
149
  accept();
 
150
}
 
151
void QgsNewConnection::helpInfo()
 
152
{
 
153
  QgsContextHelp::run(context_id);
 
154
}
 
155
/* void QgsNewConnection::saveConnection()
 
156
{
 
157
        QSettings settings;
 
158
        QString baseKey = "/PostgreSQL/connections/";
 
159
        baseKey += txtName->text();
 
160
        settings.writeEntry(baseKey + "/host", txtHost->text());
 
161
        settings.writeEntry(baseKey + "/database", txtDatabase->text());
 
162
 
 
163
        settings.writeEntry(baseKey + "/username", txtUsername->text());
 
164
        if (chkStorePassword->isChecked()) {
 
165
                settings.writeEntry(baseKey + "/password", txtPassword->text());
 
166
        } else{
 
167
        settings.writeEntry(baseKey + "/password", "");
 
168
    }
 
169
 
 
170
  accept();
 
171
} */