@@ -35,7 +36,7 @@
#include "qgspgutil.h"
#include "qgsspit.h"
-#include "qgsconnectiondialog.h"
+#include "qgsnewconnection.h"
#include "qgsdatasourceuri.h"
#include "qgsmessageviewer.h"
#include "spiticon.xpm"
@@ -85,9 +86,8 @@
txtPrimaryKeyName->setText("gid");
schema_list << "public";
- gl_key = "/PostgreSQL/connections/";
- getSchema();
-
+ conn = NULL;
+
// Install a delegate that provides the combo box widget for
// changing the schema (but there can only be one delegate per
// table, so it also provides edit widgets for the textual columns).
@@ -102,6 +102,8 @@
QgsSpit::~QgsSpit()
{
+ if(conn)
+ PQfinish(conn);
}
void QgsSpit::populateConnectionList()
@@ -119,25 +121,22 @@
void QgsSpit::newConnection()
{
- QgsConnectionDialog * con = new QgsConnectionDialog( this, tr("New Connection") );
+ QgsNewConnection *nc = new QgsNewConnection(this);
- if ( con->exec() )
+ if (nc->exec())
{
populateConnectionList();
- getSchema();
}
- delete con;
}
void QgsSpit::editConnection()
{
- QgsConnectionDialog * con = new QgsConnectionDialog( this, cmbConnections->currentText() );
- if ( con->exec() )
+ QgsNewConnection *nc = new QgsNewConnection(this, cmbConnections->currentText());
+
+ if (nc->exec())
{
- con->saveConnection();
- getSchema();
+ nc->saveConnection();
}
- delete con;
}
void QgsSpit::removeConnection()
@@ -155,8 +154,7 @@
settings.removeEntry( key + "/password" );
settings.removeEntry( key + "/save" );
- cmbConnections->removeItem( cmbConnections->currentItem() );
- getSchema();
+ cmbConnections->removeItem( cmbConnections->currentItem() );
}
}
@@ -379,35 +377,65 @@
e->exec(); // deletes itself on close
}
-PGconn* QgsSpit::checkConnection()
+void QgsSpit::dbConnect()
{
+ if(conn)
+ {
+ PQfinish(conn);
+ conn=NULL;
+ }
+
QSettings settings;
- PGconn * pd;
- bool result = true;
QString connName = cmbConnections->currentText();
if ( connName.isEmpty() )
{
QMessageBox::warning( this, tr("Import Shapefiles"), tr("You need to specify a Connection first") );
- result = false;
+ return;
}
- else
+
+ QString key = "/PostgreSQL/connections/" + connName;
+ QString database = settings.readEntry(key + "/database");
+ QString username = settings.readEntry(key + "/username");
+ QString password = settings.readEntry(key + "/password");
+
+ bool makeConnection = true;
+
+ if ( password.isEmpty() )
{
+ // get password from user
+ password = QInputDialog::getText(tr("Password for ") + username,
+ tr("Please enter your password:"),
+ QLineEdit::Password, QString::null, &makeConnection, this);
+ }
+
+ if(makeConnection)
+ {
+ // allow null password entry in case its valid for the database
QgsDataSourceURI uri;
- uri.setConnection( settings.readEntry( gl_key + connName + "/host" ),
- settings.readEntry( gl_key + connName + "/port" ),
- settings.readEntry( gl_key + connName + "/database" ),
- settings.readEntry( gl_key + connName + "/username" ),
- settings.readEntry( gl_key + connName + "/password" ) );
+ uri.setConnection( settings.readEntry(key + "/host"),
+ settings.readEntry(key + "/port"),
+ database,
+ settings.readEntry(key + "/username"),
+ password );
+
+ conn = PQconnectdb( ( const char * ) uri.connInfo() );
+ }
- pd = PQconnectdb( ( const char * ) uri.connInfo() );
-
- if ( PQstatus( pd ) != CONNECTION_OK )
- {
- QMessageBox::warning( this, tr("Import Shapefiles"), tr("Connection failed - Check settings and try again") );
- result = false;
+ if( conn==NULL || PQstatus(conn)!=CONNECTION_OK )
+ {
+ QMessageBox::warning( this, tr("Import Shapefiles"), tr("Connection failed - Check settings and try again") );
+ if(conn) {
+ PQfinish(conn);
+ conn=0;
}
+ }
- int errcode=PQsetClientEncoding(pd, QString("UNICODE").toLocal8Bit());
+ schema_list.clear();
+ schema_list << "public";
+
+ if(conn)
+ {
+ int errcode=PQsetClientEncoding(conn, QString("UNICODE").toLocal8Bit());
if(errcode==0)
{
QgsDebugMsg("encoding successfully set");
@@ -420,48 +448,29 @@
{
QgsDebugMsg("undefined return value from encoding setting");
}
- }
- if (result )
- {
// Check that the database actually has postgis in it.
QString sql1 = "SELECT postgis_lib_version()"; // available from v 0.9.0 onwards
QString sql2 = "SELECT postgis_version()"; // depreciated
- PGresult* ver = PQexec(pd, sql1.toUtf8() );
+ PGresult* ver = PQexec(conn, sql1.toUtf8() );
if ( PQresultStatus(ver) != PGRES_TUPLES_OK)
{
// In case the version of postgis is older than 0.9.0, try the
// depreciated call before erroring out.
PQclear(ver);
- ver = PQexec(pd, sql2.toUtf8() );
+ ver = PQexec(conn, sql2.toUtf8() );
if ( PQresultStatus(ver) != PGRES_TUPLES_OK)
{
QMessageBox::warning( this, tr("PostGIS not available"),
tr("The chosen database does not have PostGIS installed, "
"but this is required for storage of spatial data.
"));
- return NULL;
}
}
- return pd;
- }
- else
- return NULL;
-}
-void QgsSpit::getSchema()
-{
- QSettings settings;
- schema_list.clear();
- schema_list << "public";
- PGconn* pd = checkConnection();
- if ( pd != NULL )
- {
- QString connName = cmbConnections->currentText();
- QString user = settings.readEntry( gl_key + connName + "/username" );
QString schemaSql = QString( "select nspname from pg_namespace,pg_user where nspowner=usesysid and usename=%1" )
- .arg( QgsPgUtil::quotedValue(user) );
- PGresult *schemas = PQexec( pd, schemaSql.toUtf8() );
+ .arg( QgsPgUtil::quotedValue(username) );
+ PGresult *schemas = PQexec( conn, schemaSql.toUtf8() );
// get the schema names
if ( PQresultStatus( schemas ) == PGRES_TUPLES_OK )
{
@@ -474,8 +483,6 @@
PQclear( schemas );
}
- PQfinish(pd);
-
// install a new delegate with an updated schema list (rather than
// update the existing delegate because delegates don't seem able to
// store modifiable data).
@@ -487,15 +494,6 @@
cmbSchema->setCurrentIndex( 0 ); // index 0 is always "public"
}
-void QgsSpit::updateSchema()
-{
- // install a new delegate with an updated schema list (rather than
- // update the existing delegate because delegates don't seem able to
- // store modifiable data).
- ShapefileTableDelegate* delegate = new ShapefileTableDelegate(tblShapefiles, schema_list);
- tblShapefiles->setItemDelegate(delegate);
-}
-
void QgsSpit::import()
{
QList selected = tblShapefiles->selectedItems();
@@ -505,15 +503,14 @@
QString connName = cmbConnections->currentText();
QSettings settings;
bool canceled = false;
- PGconn* pd = checkConnection();
- QString query;
+ QString query;
if ( total_features == 0 )
{
QMessageBox::warning( this, tr("Import Shapefiles"),
tr("You need to add shapefiles to the list first") );
}
- else if ( pd != NULL )
+ else if ( conn != NULL )
{
PGresult * res;
QProgressDialog pro( tr("Importing files"), tr("Cancel"),
@@ -585,7 +582,7 @@
query = QString("SELECT f_table_name FROM geometry_columns WHERE f_table_name=%1 AND f_table_schema=%2")
.arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBRELATIONNAME )->text()) )
.arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBSCHEMA )->text()) );
- res = PQexec( pd, query.toUtf8() );
+ res = PQexec( conn, query.toUtf8() );
rel_exists1 = ( PQntuples( res ) > 0 );
if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
@@ -605,7 +602,7 @@
query = QString("SELECT tablename FROM pg_tables WHERE tablename=%1 AND schemaname=%2")
.arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBRELATIONNAME )->text() ) )
.arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBSCHEMA )->text() ) );
- res = PQexec( pd, query.toUtf8() );
+ res = PQexec( conn, query.toUtf8() );
rel_exists2 = ( PQntuples( res ) > 0 );
@@ -628,7 +625,7 @@
// begin session
query = "BEGIN";
- res = PQexec( pd, query.toUtf8() );
+ res = PQexec( conn, query.toUtf8() );
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
{
QString err = PQresultErrorMessage( res );
@@ -649,7 +646,7 @@
if ( tblShapefiles->item( i, ColDBSCHEMA )->text() != "public" )
query += QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBSCHEMA )->text() ) + ",";
query += QgsPgUtil::quotedValue( "public" );
- res = PQexec( pd, query.toUtf8() );
+ res = PQexec( conn, query.toUtf8() );
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
{
@@ -684,7 +681,7 @@
query = QString("DROP TABLE %1")
.arg( QgsPgUtil::quotedIdentifier( tblShapefiles->item( i, ColDBRELATIONNAME )->text() ) );
- res = PQexec( pd, query.toUtf8() );
+ res = PQexec( conn, query.toUtf8() );
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
{
QString err = PQresultErrorMessage( res );
@@ -709,7 +706,7 @@
.arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBRELATIONNAME )->text()) );
QStringList columns;
- res = PQexec( pd, query.toUtf8() );
+ res = PQexec( conn, query.toUtf8() );
if( PQresultStatus( res ) != PGRES_TUPLES_OK )
{
for(int i=0; iitem( i, ColDBRELATIONNAME )->text() ) )
.arg( QgsPgUtil::quotedValue( columns[i] ) );
- res = PQexec( pd, query.toUtf8() );
+ res = PQexec( conn, query.toUtf8() );
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
{
QString err = PQresultErrorMessage( res );
@@ -742,7 +739,7 @@
else
{
query = "ROLLBACK";
- res = PQexec( pd, query.toUtf8() );
+ res = PQexec( conn, query.toUtf8() );
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
{
QString err = PQresultErrorMessage( res );
@@ -762,16 +759,17 @@
int temp_progress = pro.value();
canceled = false;
- QString dbname = settings.readEntry( gl_key + connName + "/database" );
+ QString key = "/PostgreSQL/connections/" + connName;
+ QString dbname = settings.readEntry( key + "/database" );
QString schema = tblShapefiles->item( i, ColDBSCHEMA )->text();
QString srid = QString( "%1" ).arg( spinSrid->value() );
QString errorText;
- bool layerInserted = fileList[i]->insertLayer(dbname, schema, txtPrimaryKeyName->text(), txtGeomName->text(), srid, pd, pro, canceled, errorText );
+ bool layerInserted = fileList[i]->insertLayer(dbname, schema, txtPrimaryKeyName->text(), txtGeomName->text(), srid, conn, pro, canceled, errorText );
if ( layerInserted && !canceled )
{ // if file has been imported successfully
query = "COMMIT";
- res = PQexec( pd, query.toUtf8() );
+ res = PQexec( conn, query.toUtf8() );
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
{
QString err = PQresultErrorMessage( res );
@@ -805,7 +803,7 @@
QString errTxt = error + "\n" + errorText;
QMessageBox::warning( this, tr("Import Shapefiles"), errTxt );
query = "ROLLBACK";
- res = PQexec( pd, query.toUtf8() );
+ res = PQexec( conn, query.toUtf8() );
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
{
QString err = PQresultErrorMessage( res );
@@ -822,27 +820,18 @@
break;
}
}
- PQfinish( pd );
if(successes==count)
accept();
else
QMessageBox::information( &pro, tr("Import Shapefiles"), QString( tr("%1 of %2 shapefiles could not be imported.") ).arg(count-successes).arg(count) );
}
-}
-
-void QgsSpit::editShapefile(int row, int col, int button, const QPoint& mousePos)
-{
- // FIXME Is this necessary any more?
- /*
- if (ColFEATURECLASS == col || ColDBRELATIONNAME == col)
+ else
{
- tblShapefiles->editCell(row, col, FALSE);
+ QMessageBox::warning( this, tr("Import Shapefiles"), tr("You need to specify a Connection first") );
}
- */
}
-
QWidget *ShapefileTableDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &,
const QModelIndex & index) const
Index: qgsconnectiondialog.cpp
===================================================================
--- qgsconnectiondialog.cpp (revision 8346)
+++ qgsconnectiondialog.cpp (working copy)
@@ -1,100 +0,0 @@
-/***************************************************************************
- qgsconnectiondialog.cpp - description
- -------------------
- begin : Thu Dec 10 2003
- copyright : (C) 2003 by Denis Antipov
- email :
- ***************************************************************************/
-
-/***************************************************************************
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-
-// $Id$
-
-#include
-
-#include
-#include
-
-extern "C"
-{
- #include
-}
-
-#include "qgsconnectiondialog.h"
-#include "qgsmessageviewer.h"
-#include "qgsdatasourceuri.h"
-
-QgsConnectionDialog::QgsConnectionDialog(QWidget *parent, const QString& connName, Qt::WFlags fl)
- : QDialog(parent, fl)
-{
- setupUi(this);
- if (!connName.isEmpty()) {
- QSettings settings;
- QString key = "/PostgreSQL/connections/" + connName;
- txtHost->setText(settings.readEntry(key + "/host"));
- txtDatabase->setText(settings.readEntry(key + "/database"));
- if(settings.readEntry(key + "/port").length() ==0){
- txtPort->setText("5432");
- }
- else {
- txtPort->setText(settings.readEntry(key + "/port"));
- }
- txtUsername->setText(settings.readEntry(key + "/username"));
- if(settings.readEntry(key + "/save") == "true"){
- txtPassword->setText(settings.readEntry(key + "/password"));
- chkStorePassword->setChecked(true);
- }
- txtName->setText(connName);
- }
-}
-
-QgsConnectionDialog::~QgsConnectionDialog()
-{
-
-}
-
-
-void QgsConnectionDialog::testConnection()
-{
- QgsDataSourceURI uri;
- uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(), txtUsername->text(), txtPassword->text() );
- PGconn *pd = PQconnectdb((const char *) uri.connInfo() );
-
- if (PQstatus(pd) == CONNECTION_OK) {
- // Database successfully opened; we can now issue SQL commands.
- QMessageBox::information(this, tr("Test connection"), tr("Connection to ") + txtDatabase->text() + tr(" was successfull"));
- } else {
- QMessageBox::information(this, tr("Test connection"), tr("Connection failed - Check settings and try again "));
- }
-
- PQfinish(pd);
-}
-
-void QgsConnectionDialog::saveConnection()
-{
- QSettings settings;
- QString baseKey = "/PostgreSQL/connections/";
- baseKey += txtName->text();
- settings.writeEntry(baseKey + "/host", txtHost->text());
- settings.writeEntry(baseKey + "/database", txtDatabase->text());
- settings.writeEntry(baseKey + "/port", txtPort->text());
- settings.writeEntry(baseKey + "/username", txtUsername->text());
- settings.writeEntry(baseKey + "/password", txtPassword->text());
- if(chkStorePassword->isChecked()) settings.writeEntry(baseKey + "/save", "true");
- else settings.writeEntry(baseKey + "/save", "false");
- accept();
-}
-
-void QgsConnectionDialog::helpInfo(){
- QString message = tr("General Interface Help:\n\n");
- QgsMessageViewer * e = new QgsMessageViewer(this);
- e->setMessageAsPlainText(message);
- e->exec(); // deletes itself on close
-}
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt (revision 8346)
+++ CMakeLists.txt (working copy)
@@ -4,7 +4,7 @@
SET (SPIT_SRCS
qgsspit.cpp
- qgsconnectiondialog.cpp
+ ../../app/qgsnewconnection.cpp
qgspgutil.cpp
qgsshapefile.cpp
)
@@ -20,7 +20,7 @@
)
SET (SPIT_UIS
- qgsconnectiondialogbase.ui
+ ../../ui/qgsnewconnectionbase.ui
qgsspitbase.ui
../../ui/qgsmessageviewer.ui
)
@@ -28,7 +28,7 @@
SET (SPIT_EXE_MOC_HDRS
qgsspit.h
qgsshapefile.h
- qgsconnectiondialog.h
+ ../../app/qgsnewconnection.h
)
SET (SPIT_PLUGIN_MOC_HDRS
@@ -54,6 +54,8 @@
${CMAKE_CURRENT_BINARY_DIR}
../../core ../../core/raster ../../core/renderer ../../core/symbology
../../gui
+ ../../ui
+ ../../app
..
${POSTGRES_INCLUDE_DIR}
${GDAL_INCLUDE_DIR}
Index: .cvsignore
===================================================================
--- .cvsignore (revision 8346)
+++ .cvsignore (working copy)
@@ -16,4 +16,3 @@
qgsscangeometries.h
qgsspitbase.cpp
qgsspitbase.h
-