Index: qgsconnectiondialog.h =================================================================== --- qgsconnectiondialog.h (revision 8346) +++ qgsconnectiondialog.h (working copy) @@ -1,45 +0,0 @@ -/*************************************************************************** - qgsconnectiondialog.h - 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. * - * * - ***************************************************************************/ - -#ifndef QGSCONNECTIONDIALOG_H -#define QGSCONNECTIONDIALOG_H - -// $Id$ - -#include "ui_qgsconnectiondialogbase.h" -#include "qgisgui.h" - -class QgsConnectionDialog : public QDialog, private Ui::QgsConnectionDialogBase -{ - Q_OBJECT - public: - - QgsConnectionDialog(QWidget *parent = 0, const QString& connName = QString::null, Qt::WFlags fl = QgisGui::ModalDialogFlags); - ~QgsConnectionDialog(); - void testConnection(); - void saveConnection(); - void helpInfo(); - -public slots: - - void on_buttonBox_accepted() { saveConnection(); } - void on_buttonBox_rejected() { reject(); } - void on_buttonBox_helpRequested() { helpInfo(); } - void on_btnConnect_clicked() { testConnection(); } -}; - -#endif Index: qgsspit.h =================================================================== --- qgsspit.h (revision 8346) +++ qgsspit.h (working copy) @@ -44,7 +44,7 @@ //! Populate the list of available database connections void populateConnectionList(); //! Connect to the selected database - void dbConnect() {}; + void dbConnect(); //! Return a list of selected tables QStringList selectedTables(); //! Return the connection info @@ -67,13 +67,8 @@ void useDefaultGeom(); //! Show brief help void helpInfo(); - //! Get schemas available in the database - void getSchema(); - void updateSchema(); //! Import shapefiles into PostgreSQL void import(); - //! Edit import properties of a shapefile in the queue - void editShapefile( int, int, int, const QPoint & ); public slots: @@ -95,7 +90,6 @@ void on_tblShapefiles_itemClicked(QTableWidgetItem* item) { tblShapefiles->editItem(item); } // When the user changes the selected connection, update the schema list - void on_cmbConnections_activated(int) { getSchema(); } void on_chkUseDefaultSrid_toggled(bool) { useDefaultSrid(); } void on_chkUseDefaultGeom_toggled(bool) { useDefaultGeom(); } @@ -120,7 +114,7 @@ QString defGeom; int defaultSridValue; QString defaultGeomValue; - QString gl_key; + PGconn *conn; }; // We want to provide combo boxes in the table of shape files to Index: qgsconnectiondialogbase.ui =================================================================== --- qgsconnectiondialogbase.ui (revision 8346) +++ qgsconnectiondialogbase.ui (working copy) @@ -1,196 +0,0 @@ - - QgsConnectionDialogBase - - - - 0 - 0 - 348 - 332 - - - - Create a New PostGIS connection - - - true - - - true - - - - 9 - - - 6 - - - - - Connection Information - - - - 9 - - - 6 - - - - - 0 - - - 6 - - - - - Save Password - - - - - - - Test Connect - - - - - - - - - 0 - - - 6 - - - - - 0 - - - 6 - - - - - Name - - - - - - - Host - - - - - - - Database - - - - - - - Port - - - - - - - Username - - - - - - - Password - - - - - - - - - 0 - - - 6 - - - - - Name of the new connection - - - - - - - - - - - - - 5432 - - - - - - - - - - QLineEdit::Password - - - - - - - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::NoButton|QDialogButtonBox::Ok - - - - - - - qPixmapFromMimeSource - - txtName - txtHost - txtDatabase - txtPort - txtUsername - txtPassword - chkStorePassword - btnConnect - buttonBox - - - - Index: qgsspitbase.ui =================================================================== --- qgsspitbase.ui (revision 8346) +++ qgsspitbase.ui (working copy) @@ -55,7 +55,7 @@ - 9 + 11 6 @@ -122,6 +122,19 @@ + + + + Connect to PostGIS + + + Connect to PostGIS + + + Connect + + + Index: qgsspit.cpp =================================================================== --- qgsspit.cpp (revision 8346) +++ qgsspit.cpp (working copy) @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -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 -