28
28
#include < QTextCodec>
29
29
#include < QList>
30
30
#include < QTableWidgetItem>
31
+ #include < QInputDialog>
31
32
32
33
#include < iostream>
33
34
34
35
#include " qgsencodingfiledialog.h"
35
36
36
37
#include " qgspgutil.h"
37
38
#include " qgsspit.h"
38
- #include " qgsconnectiondialog .h"
39
+ #include " qgsnewconnection .h"
39
40
#include " qgsdatasourceuri.h"
40
41
#include " qgsmessageviewer.h"
41
42
#include " spiticon.xpm"
@@ -85,9 +86,8 @@ QgsSpit::QgsSpit( QWidget *parent, Qt::WFlags fl ) : QDialog( parent, fl )
85
86
txtPrimaryKeyName->setText (" gid" );
86
87
87
88
schema_list << " public" ;
88
- gl_key = " /PostgreSQL/connections/" ;
89
- getSchema ();
90
-
89
+ conn = NULL ;
90
+
91
91
// Install a delegate that provides the combo box widget for
92
92
// changing the schema (but there can only be one delegate per
93
93
// 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 )
102
102
103
103
QgsSpit::~QgsSpit ()
104
104
{
105
+ if (conn)
106
+ PQfinish (conn);
105
107
}
106
108
107
109
void QgsSpit::populateConnectionList ()
@@ -119,25 +121,22 @@ void QgsSpit::populateConnectionList()
119
121
120
122
void QgsSpit::newConnection ()
121
123
{
122
- QgsConnectionDialog * con = new QgsConnectionDialog ( this , tr ( " New Connection " ) );
124
+ QgsNewConnection *nc = new QgsNewConnection ( this );
123
125
124
- if ( con ->exec () )
126
+ if (nc ->exec ())
125
127
{
126
128
populateConnectionList ();
127
- getSchema ();
128
129
}
129
- delete con;
130
130
}
131
131
132
132
void QgsSpit::editConnection ()
133
133
{
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 ())
136
137
{
137
- con->saveConnection ();
138
- getSchema ();
138
+ nc->saveConnection ();
139
139
}
140
- delete con;
141
140
}
142
141
143
142
void QgsSpit::removeConnection ()
@@ -155,8 +154,7 @@ void QgsSpit::removeConnection()
155
154
settings.removeEntry ( key + " /password" );
156
155
settings.removeEntry ( key + " /save" );
157
156
158
- cmbConnections->removeItem ( cmbConnections->currentItem () );
159
- getSchema ();
157
+ cmbConnections->removeItem ( cmbConnections->currentItem () );
160
158
}
161
159
}
162
160
@@ -379,35 +377,65 @@ void QgsSpit::helpInfo()
379
377
e->exec (); // deletes itself on close
380
378
}
381
379
382
- PGconn* QgsSpit::checkConnection ()
380
+ void QgsSpit::dbConnect ()
383
381
{
382
+ if (conn)
383
+ {
384
+ PQfinish (conn);
385
+ conn=NULL ;
386
+ }
387
+
384
388
QSettings settings;
385
- PGconn * pd;
386
- bool result = true ;
387
389
QString connName = cmbConnections->currentText ();
388
390
if ( connName.isEmpty () )
389
391
{
390
392
QMessageBox::warning ( this , tr (" Import Shapefiles" ), tr (" You need to specify a Connection first" ) );
391
- result = false ;
393
+ return ;
392
394
}
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 () )
394
404
{
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
+ }
401
410
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
+ }
403
423
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 ;
408
430
}
431
+ }
432
+
433
+ schema_list.clear ();
434
+ schema_list << " public" ;
409
435
410
- int errcode=PQsetClientEncoding (pd, QString (" UNICODE" ).toLocal8Bit ());
436
+ if (conn)
437
+ {
438
+ int errcode=PQsetClientEncoding (conn, QString (" UNICODE" ).toLocal8Bit ());
411
439
if (errcode==0 )
412
440
{
413
441
QgsDebugMsg (" encoding successfully set" );
@@ -420,48 +448,29 @@ PGconn* QgsSpit::checkConnection()
420
448
{
421
449
QgsDebugMsg (" undefined return value from encoding setting" );
422
450
}
423
- }
424
451
425
- if (result )
426
- {
427
452
// Check that the database actually has postgis in it.
428
453
QString sql1 = " SELECT postgis_lib_version()" ; // available from v 0.9.0 onwards
429
454
QString sql2 = " SELECT postgis_version()" ; // depreciated
430
455
431
- PGresult* ver = PQexec (pd , sql1.toUtf8 () );
456
+ PGresult* ver = PQexec (conn , sql1.toUtf8 () );
432
457
if ( PQresultStatus (ver) != PGRES_TUPLES_OK)
433
458
{
434
459
// In case the version of postgis is older than 0.9.0, try the
435
460
// depreciated call before erroring out.
436
461
PQclear (ver);
437
- ver = PQexec (pd , sql2.toUtf8 () );
462
+ ver = PQexec (conn , sql2.toUtf8 () );
438
463
if ( PQresultStatus (ver) != PGRES_TUPLES_OK)
439
464
{
440
465
QMessageBox::warning ( this , tr (" PostGIS not available" ),
441
466
tr (" <p>The chosen database does not have PostGIS installed, "
442
467
" but this is required for storage of spatial data.</p>" ));
443
- return NULL ;
444
468
}
445
469
}
446
- return pd;
447
- }
448
- else
449
- return NULL ;
450
- }
451
470
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" );
462
471
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 () );
465
474
// get the schema names
466
475
if ( PQresultStatus ( schemas ) == PGRES_TUPLES_OK )
467
476
{
@@ -474,8 +483,6 @@ void QgsSpit::getSchema()
474
483
PQclear ( schemas );
475
484
}
476
485
477
- PQfinish (pd);
478
-
479
486
// install a new delegate with an updated schema list (rather than
480
487
// update the existing delegate because delegates don't seem able to
481
488
// store modifiable data).
@@ -487,15 +494,6 @@ void QgsSpit::getSchema()
487
494
cmbSchema->setCurrentIndex ( 0 ); // index 0 is always "public"
488
495
}
489
496
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
-
499
497
void QgsSpit::import ()
500
498
{
501
499
QList<QTableWidgetItem*> selected = tblShapefiles->selectedItems ();
@@ -505,15 +503,14 @@ void QgsSpit::import()
505
503
QString connName = cmbConnections->currentText ();
506
504
QSettings settings;
507
505
bool canceled = false ;
508
- PGconn* pd = checkConnection ();
509
- QString query;
510
506
507
+ QString query;
511
508
if ( total_features == 0 )
512
509
{
513
510
QMessageBox::warning ( this , tr (" Import Shapefiles" ),
514
511
tr (" You need to add shapefiles to the list first" ) );
515
512
}
516
- else if ( pd != NULL )
513
+ else if ( conn != NULL )
517
514
{
518
515
PGresult * res;
519
516
QProgressDialog pro ( tr (" Importing files" ), tr (" Cancel" ),
@@ -585,7 +582,7 @@ void QgsSpit::import()
585
582
query = QString (" SELECT f_table_name FROM geometry_columns WHERE f_table_name=%1 AND f_table_schema=%2" )
586
583
.arg ( QgsPgUtil::quotedValue ( tblShapefiles->item ( i, ColDBRELATIONNAME )->text ()) )
587
584
.arg ( QgsPgUtil::quotedValue ( tblShapefiles->item ( i, ColDBSCHEMA )->text ()) );
588
- res = PQexec ( pd , query.toUtf8 () );
585
+ res = PQexec ( conn , query.toUtf8 () );
589
586
rel_exists1 = ( PQntuples ( res ) > 0 );
590
587
591
588
if ( PQresultStatus ( res ) != PGRES_TUPLES_OK )
@@ -605,7 +602,7 @@ void QgsSpit::import()
605
602
query = QString (" SELECT tablename FROM pg_tables WHERE tablename=%1 AND schemaname=%2" )
606
603
.arg ( QgsPgUtil::quotedValue ( tblShapefiles->item ( i, ColDBRELATIONNAME )->text () ) )
607
604
.arg ( QgsPgUtil::quotedValue ( tblShapefiles->item ( i, ColDBSCHEMA )->text () ) );
608
- res = PQexec ( pd , query.toUtf8 () );
605
+ res = PQexec ( conn , query.toUtf8 () );
609
606
610
607
rel_exists2 = ( PQntuples ( res ) > 0 );
611
608
@@ -628,7 +625,7 @@ void QgsSpit::import()
628
625
629
626
// begin session
630
627
query = " BEGIN" ;
631
- res = PQexec ( pd , query.toUtf8 () );
628
+ res = PQexec ( conn , query.toUtf8 () );
632
629
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
633
630
{
634
631
QString err = PQresultErrorMessage ( res );
@@ -649,7 +646,7 @@ void QgsSpit::import()
649
646
if ( tblShapefiles->item ( i, ColDBSCHEMA )->text () != " public" )
650
647
query += QgsPgUtil::quotedValue ( tblShapefiles->item ( i, ColDBSCHEMA )->text () ) + " ," ;
651
648
query += QgsPgUtil::quotedValue ( " public" );
652
- res = PQexec ( pd , query.toUtf8 () );
649
+ res = PQexec ( conn , query.toUtf8 () );
653
650
654
651
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
655
652
{
@@ -684,7 +681,7 @@ void QgsSpit::import()
684
681
query = QString (" DROP TABLE %1" )
685
682
.arg ( QgsPgUtil::quotedIdentifier ( tblShapefiles->item ( i, ColDBRELATIONNAME )->text () ) );
686
683
687
- res = PQexec ( pd , query.toUtf8 () );
684
+ res = PQexec ( conn , query.toUtf8 () );
688
685
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
689
686
{
690
687
QString err = PQresultErrorMessage ( res );
@@ -709,7 +706,7 @@ void QgsSpit::import()
709
706
.arg ( QgsPgUtil::quotedValue ( tblShapefiles->item ( i, ColDBRELATIONNAME )->text ()) );
710
707
711
708
QStringList columns;
712
- res = PQexec ( pd , query.toUtf8 () );
709
+ res = PQexec ( conn , query.toUtf8 () );
713
710
if ( PQresultStatus ( res ) != PGRES_TUPLES_OK )
714
711
{
715
712
for (int i=0 ; i<PQntuples (res); i++)
@@ -724,7 +721,7 @@ void QgsSpit::import()
724
721
.arg ( QgsPgUtil::quotedValue ( tblShapefiles->item ( i, ColDBRELATIONNAME )->text () ) )
725
722
.arg ( QgsPgUtil::quotedValue ( columns[i] ) );
726
723
727
- res = PQexec ( pd , query.toUtf8 () );
724
+ res = PQexec ( conn , query.toUtf8 () );
728
725
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
729
726
{
730
727
QString err = PQresultErrorMessage ( res );
@@ -742,7 +739,7 @@ void QgsSpit::import()
742
739
else
743
740
{
744
741
query = " ROLLBACK" ;
745
- res = PQexec ( pd , query.toUtf8 () );
742
+ res = PQexec ( conn , query.toUtf8 () );
746
743
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
747
744
{
748
745
QString err = PQresultErrorMessage ( res );
@@ -762,16 +759,17 @@ void QgsSpit::import()
762
759
int temp_progress = pro.value ();
763
760
canceled = false ;
764
761
765
- QString dbname = settings.readEntry ( gl_key + connName + " /database" );
762
+ QString key = " /PostgreSQL/connections/" + connName;
763
+ QString dbname = settings.readEntry ( key + " /database" );
766
764
QString schema = tblShapefiles->item ( i, ColDBSCHEMA )->text ();
767
765
QString srid = QString ( " %1" ).arg ( spinSrid->value () );
768
766
QString errorText;
769
767
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 );
771
769
if ( layerInserted && !canceled )
772
770
{ // if file has been imported successfully
773
771
query = " COMMIT" ;
774
- res = PQexec ( pd , query.toUtf8 () );
772
+ res = PQexec ( conn , query.toUtf8 () );
775
773
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
776
774
{
777
775
QString err = PQresultErrorMessage ( res );
@@ -805,7 +803,7 @@ void QgsSpit::import()
805
803
QString errTxt = error + " \n " + errorText;
806
804
QMessageBox::warning ( this , tr (" Import Shapefiles" ), errTxt );
807
805
query = " ROLLBACK" ;
808
- res = PQexec ( pd , query.toUtf8 () );
806
+ res = PQexec ( conn , query.toUtf8 () );
809
807
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
810
808
{
811
809
QString err = PQresultErrorMessage ( res );
@@ -822,27 +820,18 @@ void QgsSpit::import()
822
820
break ;
823
821
}
824
822
}
825
- PQfinish ( pd );
826
823
827
824
if (successes==count)
828
825
accept ();
829
826
else
830
827
QMessageBox::information ( &pro, tr (" Import Shapefiles" ), QString ( tr (" %1 of %2 shapefiles could not be imported." ) ).arg (count-successes).arg (count) );
831
828
}
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
839
830
{
840
- tblShapefiles->editCell(row, col, FALSE );
831
+ QMessageBox::warning ( this , tr ( " Import Shapefiles " ), tr ( " You need to specify a Connection first " ) );
841
832
}
842
- */
843
833
}
844
834
845
-
846
835
QWidget *ShapefileTableDelegate::createEditor (QWidget *parent,
847
836
const QStyleOptionViewItem &,
848
837
const QModelIndex & index) const
0 commit comments