Skip to content

File tree

7 files changed

+102
-445
lines changed

7 files changed

+102
-445
lines changed
 

‎src/plugins/spit/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
SET (SPIT_SRCS
66
qgsspit.cpp
7-
qgsconnectiondialog.cpp
7+
../../app/qgsnewconnection.cpp
88
qgspgutil.cpp
99
qgsshapefile.cpp
1010
)
@@ -20,15 +20,15 @@ SET (SPIT_EXE_SRCS
2020
)
2121

2222
SET (SPIT_UIS
23-
qgsconnectiondialogbase.ui
23+
../../ui/qgsnewconnectionbase.ui
2424
qgsspitbase.ui
2525
../../ui/qgsmessageviewer.ui
2626
)
2727

2828
SET (SPIT_EXE_MOC_HDRS
2929
qgsspit.h
3030
qgsshapefile.h
31-
qgsconnectiondialog.h
31+
../../app/qgsnewconnection.h
3232
)
3333

3434
SET (SPIT_PLUGIN_MOC_HDRS
@@ -54,6 +54,8 @@ INCLUDE_DIRECTORIES(
5454
${CMAKE_CURRENT_BINARY_DIR}
5555
../../core ../../core/raster ../../core/renderer ../../core/symbology
5656
../../gui
57+
../../ui
58+
../../app
5759
..
5860
${POSTGRES_INCLUDE_DIR}
5961
${GDAL_INCLUDE_DIR}

‎src/plugins/spit/qgsconnectiondialog.cpp

Lines changed: 0 additions & 100 deletions
This file was deleted.

‎src/plugins/spit/qgsconnectiondialog.h

Lines changed: 0 additions & 45 deletions
This file was deleted.

‎src/plugins/spit/qgsconnectiondialogbase.ui

Lines changed: 0 additions & 196 deletions
This file was deleted.

‎src/plugins/spit/qgsspit.cpp

Lines changed: 81 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
#include <QTextCodec>
2929
#include <QList>
3030
#include <QTableWidgetItem>
31+
#include <QInputDialog>
3132

3233
#include <iostream>
3334

3435
#include "qgsencodingfiledialog.h"
3536

3637
#include "qgspgutil.h"
3738
#include "qgsspit.h"
38-
#include "qgsconnectiondialog.h"
39+
#include "qgsnewconnection.h"
3940
#include "qgsdatasourceuri.h"
4041
#include "qgsmessageviewer.h"
4142
#include "spiticon.xpm"
@@ -85,9 +86,8 @@ QgsSpit::QgsSpit( QWidget *parent, Qt::WFlags fl ) : QDialog( parent, fl )
8586
txtPrimaryKeyName->setText("gid");
8687

8788
schema_list << "public";
88-
gl_key = "/PostgreSQL/connections/";
89-
getSchema();
90-
89+
conn = NULL;
90+
9191
// Install a delegate that provides the combo box widget for
9292
// changing the schema (but there can only be one delegate per
9393
// table, so it also provides edit widgets for the textual columns).
@@ -102,6 +102,8 @@ QgsSpit::QgsSpit( QWidget *parent, Qt::WFlags fl ) : QDialog( parent, fl )
102102

103103
QgsSpit::~QgsSpit()
104104
{
105+
if(conn)
106+
PQfinish(conn);
105107
}
106108

107109
void QgsSpit::populateConnectionList()
@@ -119,25 +121,22 @@ void QgsSpit::populateConnectionList()
119121

120122
void QgsSpit::newConnection()
121123
{
122-
QgsConnectionDialog * con = new QgsConnectionDialog( this, tr("New Connection") );
124+
QgsNewConnection *nc = new QgsNewConnection(this);
123125

124-
if ( con->exec() )
126+
if (nc->exec())
125127
{
126128
populateConnectionList();
127-
getSchema();
128129
}
129-
delete con;
130130
}
131131

132132
void QgsSpit::editConnection()
133133
{
134-
QgsConnectionDialog * con = new QgsConnectionDialog( this, cmbConnections->currentText() );
135-
if ( con->exec() )
134+
QgsNewConnection *nc = new QgsNewConnection(this, cmbConnections->currentText());
135+
136+
if (nc->exec())
136137
{
137-
con->saveConnection();
138-
getSchema();
138+
nc->saveConnection();
139139
}
140-
delete con;
141140
}
142141

143142
void QgsSpit::removeConnection()
@@ -155,8 +154,7 @@ void QgsSpit::removeConnection()
155154
settings.removeEntry( key + "/password" );
156155
settings.removeEntry( key + "/save" );
157156

158-
cmbConnections->removeItem( cmbConnections->currentItem() );
159-
getSchema();
157+
cmbConnections->removeItem( cmbConnections->currentItem() );
160158
}
161159
}
162160

@@ -379,35 +377,65 @@ void QgsSpit::helpInfo()
379377
e->exec(); // deletes itself on close
380378
}
381379

382-
PGconn* QgsSpit::checkConnection()
380+
void QgsSpit::dbConnect()
383381
{
382+
if(conn)
383+
{
384+
PQfinish(conn);
385+
conn=NULL;
386+
}
387+
384388
QSettings settings;
385-
PGconn * pd;
386-
bool result = true;
387389
QString connName = cmbConnections->currentText();
388390
if ( connName.isEmpty() )
389391
{
390392
QMessageBox::warning( this, tr("Import Shapefiles"), tr("You need to specify a Connection first") );
391-
result = false;
393+
return;
392394
}
393-
else
395+
396+
QString key = "/PostgreSQL/connections/" + connName;
397+
QString database = settings.readEntry(key + "/database");
398+
QString username = settings.readEntry(key + "/username");
399+
QString password = settings.readEntry(key + "/password");
400+
401+
bool makeConnection = true;
402+
403+
if ( password.isEmpty() )
394404
{
395-
QgsDataSourceURI uri;
396-
uri.setConnection( settings.readEntry( gl_key + connName + "/host" ),
397-
settings.readEntry( gl_key + connName + "/port" ),
398-
settings.readEntry( gl_key + connName + "/database" ),
399-
settings.readEntry( gl_key + connName + "/username" ),
400-
settings.readEntry( gl_key + connName + "/password" ) );
405+
// get password from user
406+
password = QInputDialog::getText(tr("Password for ") + username,
407+
tr("Please enter your password:"),
408+
QLineEdit::Password, QString::null, &makeConnection, this);
409+
}
401410

402-
pd = PQconnectdb( ( const char * ) uri.connInfo() );
411+
if(makeConnection)
412+
{
413+
// allow null password entry in case its valid for the database
414+
QgsDataSourceURI uri;
415+
uri.setConnection( settings.readEntry(key + "/host"),
416+
settings.readEntry(key + "/port"),
417+
database,
418+
settings.readEntry(key + "/username"),
419+
password );
420+
421+
conn = PQconnectdb( ( const char * ) uri.connInfo() );
422+
}
403423

404-
if ( PQstatus( pd ) != CONNECTION_OK )
405-
{
406-
QMessageBox::warning( this, tr("Import Shapefiles"), tr("Connection failed - Check settings and try again") );
407-
result = false;
424+
if( conn==NULL || PQstatus(conn)!=CONNECTION_OK )
425+
{
426+
QMessageBox::warning( this, tr("Import Shapefiles"), tr("Connection failed - Check settings and try again") );
427+
if(conn) {
428+
PQfinish(conn);
429+
conn=0;
408430
}
431+
}
432+
433+
schema_list.clear();
434+
schema_list << "public";
409435

410-
int errcode=PQsetClientEncoding(pd, QString("UNICODE").toLocal8Bit());
436+
if(conn)
437+
{
438+
int errcode=PQsetClientEncoding(conn, QString("UNICODE").toLocal8Bit());
411439
if(errcode==0)
412440
{
413441
QgsDebugMsg("encoding successfully set");
@@ -420,48 +448,29 @@ PGconn* QgsSpit::checkConnection()
420448
{
421449
QgsDebugMsg("undefined return value from encoding setting");
422450
}
423-
}
424451

425-
if (result )
426-
{
427452
// Check that the database actually has postgis in it.
428453
QString sql1 = "SELECT postgis_lib_version()"; // available from v 0.9.0 onwards
429454
QString sql2 = "SELECT postgis_version()"; // depreciated
430455

431-
PGresult* ver = PQexec(pd, sql1.toUtf8() );
456+
PGresult* ver = PQexec(conn, sql1.toUtf8() );
432457
if ( PQresultStatus(ver) != PGRES_TUPLES_OK)
433458
{
434459
// In case the version of postgis is older than 0.9.0, try the
435460
// depreciated call before erroring out.
436461
PQclear(ver);
437-
ver = PQexec(pd, sql2.toUtf8() );
462+
ver = PQexec(conn, sql2.toUtf8() );
438463
if ( PQresultStatus(ver) != PGRES_TUPLES_OK)
439464
{
440465
QMessageBox::warning( this, tr("PostGIS not available"),
441466
tr("<p>The chosen database does not have PostGIS installed, "
442467
"but this is required for storage of spatial data.</p>"));
443-
return NULL;
444468
}
445469
}
446-
return pd;
447-
}
448-
else
449-
return NULL;
450-
}
451470

452-
void QgsSpit::getSchema()
453-
{
454-
QSettings settings;
455-
schema_list.clear();
456-
schema_list << "public";
457-
PGconn* pd = checkConnection();
458-
if ( pd != NULL )
459-
{
460-
QString connName = cmbConnections->currentText();
461-
QString user = settings.readEntry( gl_key + connName + "/username" );
462471
QString schemaSql = QString( "select nspname from pg_namespace,pg_user where nspowner=usesysid and usename=%1" )
463-
.arg( QgsPgUtil::quotedValue(user) );
464-
PGresult *schemas = PQexec( pd, schemaSql.toUtf8() );
472+
.arg( QgsPgUtil::quotedValue(username) );
473+
PGresult *schemas = PQexec( conn, schemaSql.toUtf8() );
465474
// get the schema names
466475
if ( PQresultStatus( schemas ) == PGRES_TUPLES_OK )
467476
{
@@ -474,8 +483,6 @@ void QgsSpit::getSchema()
474483
PQclear( schemas );
475484
}
476485

477-
PQfinish(pd);
478-
479486
// install a new delegate with an updated schema list (rather than
480487
// update the existing delegate because delegates don't seem able to
481488
// store modifiable data).
@@ -487,15 +494,6 @@ void QgsSpit::getSchema()
487494
cmbSchema->setCurrentIndex( 0 ); // index 0 is always "public"
488495
}
489496

490-
void QgsSpit::updateSchema()
491-
{
492-
// install a new delegate with an updated schema list (rather than
493-
// update the existing delegate because delegates don't seem able to
494-
// store modifiable data).
495-
ShapefileTableDelegate* delegate = new ShapefileTableDelegate(tblShapefiles, schema_list);
496-
tblShapefiles->setItemDelegate(delegate);
497-
}
498-
499497
void QgsSpit::import()
500498
{
501499
QList<QTableWidgetItem*> selected = tblShapefiles->selectedItems();
@@ -505,15 +503,14 @@ void QgsSpit::import()
505503
QString connName = cmbConnections->currentText();
506504
QSettings settings;
507505
bool canceled = false;
508-
PGconn* pd = checkConnection();
509-
QString query;
510506

507+
QString query;
511508
if ( total_features == 0 )
512509
{
513510
QMessageBox::warning( this, tr("Import Shapefiles"),
514511
tr("You need to add shapefiles to the list first") );
515512
}
516-
else if ( pd != NULL )
513+
else if ( conn != NULL )
517514
{
518515
PGresult * res;
519516
QProgressDialog pro( tr("Importing files"), tr("Cancel"),
@@ -585,7 +582,7 @@ void QgsSpit::import()
585582
query = QString("SELECT f_table_name FROM geometry_columns WHERE f_table_name=%1 AND f_table_schema=%2")
586583
.arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBRELATIONNAME )->text()) )
587584
.arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBSCHEMA )->text()) );
588-
res = PQexec( pd, query.toUtf8() );
585+
res = PQexec( conn, query.toUtf8() );
589586
rel_exists1 = ( PQntuples( res ) > 0 );
590587

591588
if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
@@ -605,7 +602,7 @@ void QgsSpit::import()
605602
query = QString("SELECT tablename FROM pg_tables WHERE tablename=%1 AND schemaname=%2")
606603
.arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBRELATIONNAME )->text() ) )
607604
.arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBSCHEMA )->text() ) );
608-
res = PQexec( pd, query.toUtf8() );
605+
res = PQexec( conn, query.toUtf8() );
609606

610607
rel_exists2 = ( PQntuples( res ) > 0 );
611608

@@ -628,7 +625,7 @@ void QgsSpit::import()
628625

629626
// begin session
630627
query = "BEGIN";
631-
res = PQexec( pd, query.toUtf8() );
628+
res = PQexec( conn, query.toUtf8() );
632629
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
633630
{
634631
QString err = PQresultErrorMessage( res );
@@ -649,7 +646,7 @@ void QgsSpit::import()
649646
if ( tblShapefiles->item( i, ColDBSCHEMA )->text() != "public" )
650647
query += QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBSCHEMA )->text() ) + ",";
651648
query += QgsPgUtil::quotedValue( "public" );
652-
res = PQexec( pd, query.toUtf8() );
649+
res = PQexec( conn, query.toUtf8() );
653650

654651
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
655652
{
@@ -684,7 +681,7 @@ void QgsSpit::import()
684681
query = QString("DROP TABLE %1")
685682
.arg( QgsPgUtil::quotedIdentifier( tblShapefiles->item( i, ColDBRELATIONNAME )->text() ) );
686683

687-
res = PQexec( pd, query.toUtf8() );
684+
res = PQexec( conn, query.toUtf8() );
688685
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
689686
{
690687
QString err = PQresultErrorMessage( res );
@@ -709,7 +706,7 @@ void QgsSpit::import()
709706
.arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBRELATIONNAME )->text()) );
710707

711708
QStringList columns;
712-
res = PQexec( pd, query.toUtf8() );
709+
res = PQexec( conn, query.toUtf8() );
713710
if( PQresultStatus( res ) != PGRES_TUPLES_OK )
714711
{
715712
for(int i=0; i<PQntuples(res); i++)
@@ -724,7 +721,7 @@ void QgsSpit::import()
724721
.arg( QgsPgUtil::quotedValue( tblShapefiles->item( i, ColDBRELATIONNAME )->text() ) )
725722
.arg( QgsPgUtil::quotedValue( columns[i] ) );
726723

727-
res = PQexec( pd, query.toUtf8() );
724+
res = PQexec( conn, query.toUtf8() );
728725
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
729726
{
730727
QString err = PQresultErrorMessage( res );
@@ -742,7 +739,7 @@ void QgsSpit::import()
742739
else
743740
{
744741
query = "ROLLBACK";
745-
res = PQexec( pd, query.toUtf8() );
742+
res = PQexec( conn, query.toUtf8() );
746743
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
747744
{
748745
QString err = PQresultErrorMessage( res );
@@ -762,16 +759,17 @@ void QgsSpit::import()
762759
int temp_progress = pro.value();
763760
canceled = false;
764761

765-
QString dbname = settings.readEntry( gl_key + connName + "/database" );
762+
QString key = "/PostgreSQL/connections/" + connName;
763+
QString dbname = settings.readEntry( key + "/database" );
766764
QString schema = tblShapefiles->item( i, ColDBSCHEMA )->text();
767765
QString srid = QString( "%1" ).arg( spinSrid->value() );
768766
QString errorText;
769767

770-
bool layerInserted = fileList[i]->insertLayer(dbname, schema, txtPrimaryKeyName->text(), txtGeomName->text(), srid, pd, pro, canceled, errorText );
768+
bool layerInserted = fileList[i]->insertLayer(dbname, schema, txtPrimaryKeyName->text(), txtGeomName->text(), srid, conn, pro, canceled, errorText );
771769
if ( layerInserted && !canceled )
772770
{ // if file has been imported successfully
773771
query = "COMMIT";
774-
res = PQexec( pd, query.toUtf8() );
772+
res = PQexec( conn, query.toUtf8() );
775773
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
776774
{
777775
QString err = PQresultErrorMessage( res );
@@ -805,7 +803,7 @@ void QgsSpit::import()
805803
QString errTxt = error + "\n" + errorText;
806804
QMessageBox::warning( this, tr("Import Shapefiles"), errTxt );
807805
query = "ROLLBACK";
808-
res = PQexec( pd, query.toUtf8() );
806+
res = PQexec( conn, query.toUtf8() );
809807
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
810808
{
811809
QString err = PQresultErrorMessage( res );
@@ -822,27 +820,18 @@ void QgsSpit::import()
822820
break;
823821
}
824822
}
825-
PQfinish( pd );
826823

827824
if(successes==count)
828825
accept();
829826
else
830827
QMessageBox::information( &pro, tr("Import Shapefiles"), QString( tr("%1 of %2 shapefiles could not be imported.") ).arg(count-successes).arg(count) );
831828
}
832-
}
833-
834-
void QgsSpit::editShapefile(int row, int col, int button, const QPoint& mousePos)
835-
{
836-
// FIXME Is this necessary any more?
837-
/*
838-
if (ColFEATURECLASS == col || ColDBRELATIONNAME == col)
829+
else
839830
{
840-
tblShapefiles->editCell(row, col, FALSE);
831+
QMessageBox::warning( this, tr("Import Shapefiles"), tr("You need to specify a Connection first") );
841832
}
842-
*/
843833
}
844834

845-
846835
QWidget *ShapefileTableDelegate::createEditor(QWidget *parent,
847836
const QStyleOptionViewItem &,
848837
const QModelIndex & index) const

‎src/plugins/spit/qgsspit.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class QgsSpit : public QDialog, private Ui::QgsSpitBase
4444
//! Populate the list of available database connections
4545
void populateConnectionList();
4646
//! Connect to the selected database
47-
void dbConnect() {};
47+
void dbConnect();
4848
//! Return a list of selected tables
4949
QStringList selectedTables();
5050
//! Return the connection info
@@ -67,13 +67,8 @@ class QgsSpit : public QDialog, private Ui::QgsSpitBase
6767
void useDefaultGeom();
6868
//! Show brief help
6969
void helpInfo();
70-
//! Get schemas available in the database
71-
void getSchema();
72-
void updateSchema();
7370
//! Import shapefiles into PostgreSQL
7471
void import();
75-
//! Edit import properties of a shapefile in the queue
76-
void editShapefile( int, int, int, const QPoint & );
7772

7873
public slots:
7974

@@ -95,7 +90,6 @@ public slots:
9590
void on_tblShapefiles_itemClicked(QTableWidgetItem* item)
9691
{ tblShapefiles->editItem(item); }
9792
// When the user changes the selected connection, update the schema list
98-
void on_cmbConnections_activated(int) { getSchema(); }
9993
void on_chkUseDefaultSrid_toggled(bool) { useDefaultSrid(); }
10094
void on_chkUseDefaultGeom_toggled(bool) { useDefaultGeom(); }
10195

@@ -120,7 +114,7 @@ public slots:
120114
QString defGeom;
121115
int defaultSridValue;
122116
QString defaultGeomValue;
123-
QString gl_key;
117+
PGconn *conn;
124118
};
125119

126120
// We want to provide combo boxes in the table of shape files to

‎src/plugins/spit/qgsspitbase.ui

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
</property>
5656
<layout class="QGridLayout" >
5757
<property name="margin" >
58-
<number>9</number>
58+
<number>11</number>
5959
</property>
6060
<property name="spacing" >
6161
<number>6</number>
@@ -122,6 +122,19 @@
122122
</property>
123123
</widget>
124124
</item>
125+
<item row="1" column="0" >
126+
<widget class="QPushButton" name="btnConnect" >
127+
<property name="toolTip" >
128+
<string>Connect to PostGIS</string>
129+
</property>
130+
<property name="whatsThis" >
131+
<string>Connect to PostGIS</string>
132+
</property>
133+
<property name="text" >
134+
<string>Connect</string>
135+
</property>
136+
</widget>
137+
</item>
125138
</layout>
126139
</widget>
127140
</item>

0 commit comments

Comments
 (0)
Please sign in to comment.