00001 #include <QtGui/QCloseEvent>
00002 #include <QtGui/QMessageBox>
00003 #include <QtCore/QtDebug>
00004 #include <QtCore/QPoint>
00005 #include <QtCore/QSize>
00006 #include <QtCore/QSettings>
00007 #include <QtCore/QStringList>
00008 #include <QtCore/QObject>
00009
00010 #include "ACE/ConnectionDialog.h"
00011
00012 ConnectionDialog::ConnectionDialog( QWidget* parent, Qt::WindowFlags f ) :
00013 QDialog( parent, f ),
00014 dbConnectionQString("")
00015 {
00016 setupUi( this );
00017
00018 setConnections();
00019 loadConnectionSettings();
00020 }
00021
00022 ConnectionDialog::~ConnectionDialog()
00023 {
00024
00025 }
00026
00027 void ConnectionDialog::setConnections()
00028 {
00029 QObject::connect( pushButton_Connect, SIGNAL( clicked() ), this, SLOT( slotConnect() ) );
00030 QObject::connect( pushButton_RemoveAllHistory, SIGNAL( clicked() ), this, SLOT( slotRemoveAllHistory() ) );
00031 QObject::connect( pushButton_RemoveHistoryEntry, SIGNAL( clicked() ), this, SLOT( slotRemoveHistoryEntry() ) );
00032 QObject::connect( radioButton_PredefinedConnections, SIGNAL( toggled( bool ) ), this, SLOT( slotPredefinedCS( bool ) ) );
00033 QObject::connect( comboBox_DBType, SIGNAL( activated( int ) ), this, SLOT( slotPrepareDBType( int ) ) );
00034 QObject::connect( comboBox_DBType_2, SIGNAL( activated( int ) ), this, SLOT( slotPrepareDBType_2( int ) ) );
00035 }
00036
00037 void ConnectionDialog::closeEvent( QCloseEvent* event )
00038 {
00039 event->accept();
00040 }
00041
00042 QString ConnectionDialog::buildConnectionString()
00043 {
00044
00045 if ( tabWidget_ConnectionBuilder->currentIndex() == 1 )
00046 return comboBox_ConnectionHistory->currentText();
00047
00048 QString csComponent = "";
00049
00050 if ( stackedWidget_ConnectionBuilder->currentIndex() == 0 )
00051 {
00052 if ( checkBox_UseConnectionAlias->isChecked() )
00053 {
00054 if ( comboBox_ConnectionAlias->currentText().isEmpty() )
00055 {
00056 QMessageBox::warning(
00057 this,
00058 trUtf8( "Connection string invalid!" ),
00059 trUtf8( "Connection Alias is empty!" ),
00060 QMessageBox::Ok,
00061 QMessageBox::Ok );
00062 return QString();
00063 }
00064 csComponent.append( comboBox_ConnectionAlias->currentText() );
00065 }
00066 else
00067 {
00068 csComponent.append( comboBox_DBType_2->currentText() );
00069 csComponent.append( comboBox_Server_2->currentText() );
00070 if ( comboBox_Schema_2->isVisible() )
00071 {
00072 csComponent.append( "/" ).append( comboBox_Schema_2->currentText() );
00073 }
00074 }
00075 if ( !comboBox_Role->currentText().isEmpty() )
00076 {
00077 csComponent.append( "(" ).append( comboBox_Role->currentText() ).append( ")" );
00078 }
00079 if ( comboBox_DBName_2->currentText().isEmpty() )
00080 {
00081 QMessageBox::warning(
00082 this,
00083 trUtf8( "Connection string invalid!" ),
00084 trUtf8( "COOL database instance is empty!" ),
00085 QMessageBox::Ok,
00086 QMessageBox::Ok );
00087 return QString();
00088 }
00089 csComponent.append("/").append( comboBox_DBName_2->currentText() );
00090
00091
00092
00093
00094
00095
00096
00097
00098 }
00099 else
00100 {
00101 QStringList csList;
00102 csComponent = comboBox_Server->lineEdit()->text().trimmed();
00103 if ( csComponent.isEmpty() )
00104 csList << comboBox_DBType->currentText().append( "://");
00105 else
00106 csList << comboBox_DBType->currentText().append( "://").append( csComponent );
00107 csComponent = comboBox_Schema->lineEdit()->text().trimmed();
00108 if ( !csComponent.isEmpty() )
00109 {
00110 csList << QString( "schema=" ).append( csComponent );
00111 }
00112 csComponent = comboBox_DBName->lineEdit()->text().trimmed();
00113 if ( !csComponent.isEmpty() )
00114 {
00115 csList << QString( "dbname=" ).append( csComponent );
00116 }
00117 csComponent = comboBox_Login->lineEdit()->text().trimmed();
00118 if ( !csComponent.isEmpty() )
00119 {
00120 csList << QString( "user=" ).append( csComponent );
00121 }
00122 csComponent = lineEdit_Password->text().trimmed();
00123 if ( !csComponent.isEmpty() )
00124 {
00125 csList << QString( "password=" ).append( csComponent );
00126 }
00127 csComponent = csList.join( ";" );
00128 }
00129 return csComponent;
00130 }
00131
00132 void ConnectionDialog::slotPredefinedCS( bool enabled )
00133 {
00134 stackedWidget_ConnectionBuilder->setCurrentIndex( ( int ) ( !enabled ) );
00135 }
00136
00137 void ConnectionDialog::slotPrepareDBType_2( int choice )
00138 {
00139 switch ( choice )
00140 {
00141 case 0:
00142 label_wildcard_1->setText( trUtf8( "Server:", "Oracle server address" ) );
00143 label_wildcard_2->setVisible( true );
00144 label_wildcard_2->setText( trUtf8( "Schema:", "Oracle database schema" ) );
00145 comboBox_Schema_2->setVisible( true );
00146
00147 break;
00148 case 1:
00149 label_wildcard_1->setText( trUtf8( "Host:", "MySQL host address" ) );
00150 label_wildcard_2->setVisible( true );
00151 label_wildcard_2->setText( trUtf8( "Database:", "MySQL database" ) );
00152 comboBox_Schema_2->setVisible( true );
00153
00154
00155 break;
00156 case 2:
00157 label_wildcard_1->setText( trUtf8( "File:", "Full path to SQLite database file" ) );
00158 label_wildcard_2->setVisible( false );
00159 comboBox_Schema_2->setVisible( false );
00160
00161
00162 break;
00163 }
00164
00165 }
00166
00167 void ConnectionDialog::slotPrepareDBType( int )
00168 {
00169 return;
00170 }
00171
00172 void ConnectionDialog::slotConnect()
00173 {
00174
00175 cool::IDatabasePtr dbPtr;
00176 dbConnectionQString = buildConnectionString();
00177 if ( dbConnectionQString.isEmpty() )
00178 {
00179 return;
00180 }
00181 cool::DatabaseId dbConnectionId( dbConnectionQString.toStdString() );
00182
00183 if ( connectionHash.contains( dbConnectionQString ) )
00184 {
00185 QMessageBox::information(
00186 this,
00187 trUtf8( "Connection Error" ),
00188 trUtf8( "Connection already exists." ),
00189 QMessageBox::Ok,
00190 QMessageBox::Ok );
00191 return;
00192 }
00193
00194 if ( cool::openDatabase( dbConnectionId, dbPtr ) )
00195 {
00196 connectionHash[ dbConnectionQString ] = dbPtr;
00197 emit buildFolderTree( dbPtr, dbConnectionQString );
00198 }
00199 else
00200 {
00201 QMessageBox::warning(
00202 this,
00203 trUtf8( "Connection Error" ),
00204 trUtf8( "Could not establish connection to database." ),
00205 QMessageBox::Ok,
00206 QMessageBox::Ok );
00207
00208 return;
00209 }
00210 saveConnectionSettings();
00211 loadConnectionSettings( true );
00212 close();
00213 }
00214
00215 void ConnectionDialog::slotRemoveHistoryEntry()
00216 {
00217 comboBox_ConnectionHistory->removeItem( comboBox_ConnectionHistory->currentIndex() );
00218 }
00219
00220 void ConnectionDialog::slotRemoveAllHistory()
00221 {
00222 comboBox_ConnectionHistory->clear();
00223 }
00224
00225 void ConnectionDialog::loadConnectionSettings( bool reload )
00226 {
00227 QSettings settings;
00228 int aSize;
00229
00230
00231 settings.beginGroup( objectName() );
00232
00233 if ( !reload )
00234 {
00235
00236 resize( settings.value( "size", QSize( 400, 400 ) ).toSize() );
00237 move( settings.value( "pos", QPoint( 200, 200) ).toPoint() );
00238 tabWidget_ConnectionBuilder->setCurrentIndex( settings.value( "page" ).toInt() );
00239 }
00240
00241 settings.beginGroup( comboBox_ConnectionHistory->objectName() );
00242 aSize = settings.beginReadArray( "history" );
00243 comboBox_ConnectionHistory->clear();
00244 for ( int i = 0; i < aSize; ++i )
00245 {
00246 settings.setArrayIndex( i );
00247 comboBox_ConnectionHistory->addItem( settings.value( "item", "" ).toString() );
00248 }
00249 settings.endArray();
00250
00251 comboBox_ConnectionHistory->setCurrentIndex( settings.value( "index", 0 ).toInt() );
00252 settings.endGroup();
00253
00254 if (!reload)
00255 {
00256
00257 settings.beginGroup( comboBox_DBType->objectName() );
00258 comboBox_DBType->setCurrentIndex( settings.value( "index", 0 ).toInt() );
00259 settings.endGroup();
00260 }
00261
00262 settings.beginGroup( comboBox_Server->objectName() );
00263 aSize = settings.beginReadArray( "history" );
00264 comboBox_Server->clear();
00265 for ( int i = 0; i < aSize; ++i )
00266 {
00267 settings.setArrayIndex( i );
00268 comboBox_Server->addItem( settings.value( "item", "" ).toString() );
00269 }
00270 settings.endArray();
00271
00272 comboBox_Server->setCurrentIndex( 0 );
00273 settings.endGroup();
00274
00275
00276 settings.beginGroup( comboBox_Login->objectName() );
00277 aSize = settings.beginReadArray( "history" );
00278 comboBox_Login->clear();
00279 for ( int i = 0; i < aSize; ++i )
00280 {
00281 settings.setArrayIndex( i );
00282 comboBox_Login->addItem( settings.value( "item", "" ).toString() );
00283 }
00284 settings.endArray();
00285
00286 comboBox_Login->setCurrentIndex( 0 );
00287 settings.endGroup();
00288
00289
00290
00291 settings.beginGroup( comboBox_Schema->objectName() );
00292 aSize = settings.beginReadArray( "history" );
00293 comboBox_Schema->clear();
00294 for ( int i = 0; i < aSize; ++i )
00295 {
00296 settings.setArrayIndex( i );
00297 comboBox_Schema->addItem( settings.value( "item", "" ).toString() );
00298 }
00299 settings.endArray();
00300
00301 comboBox_Schema->setCurrentIndex( 0 );
00302 settings.endGroup();
00303
00304
00305
00306 settings.beginGroup( comboBox_DBName->objectName() );
00307 aSize = settings.beginReadArray( "history" );
00308 comboBox_DBName->clear();
00309 for ( int i = 0; i < aSize; ++i )
00310 {
00311 settings.setArrayIndex( i );
00312 comboBox_DBName->addItem( settings.value( "item", "" ).toString() );
00313 }
00314 settings.endArray();
00315
00316 comboBox_DBName->setCurrentIndex( 0 );
00317 settings.endGroup();
00318
00319 settings.endGroup();
00320 }
00321
00322 void ConnectionDialog::saveConnectionSettings()
00323 {
00324 QSettings settings;
00325 int aSize;
00326 QStringList historyList;
00327
00328
00329 settings.beginGroup( objectName() );
00330
00331
00332 settings.setValue( "size", size() );
00333 settings.setValue( "pos", pos() );
00334 settings.setValue( "page", tabWidget_ConnectionBuilder->currentIndex() );
00335
00336
00337
00338
00339 settings.remove( comboBox_ConnectionHistory->objectName() );
00340 settings.beginGroup( comboBox_ConnectionHistory->objectName() );
00341 aSize = comboBox_ConnectionHistory->count();
00342
00343 for ( int i = 0; i < aSize; ++i )
00344 {
00345 QString cStr( comboBox_ConnectionHistory->itemText( i ) );
00346 if ( !cStr.trimmed().isEmpty() && !historyList.contains( cStr ) )
00347 {
00348 historyList << cStr;
00349 }
00350 }
00351
00352 foreach ( QString cStr, connectionHash.keys() )
00353 {
00354 if ( !cStr.trimmed().isEmpty() && !historyList.contains( cStr ) )
00355 {
00356 historyList << cStr;
00357 }
00358 }
00359 settings.beginWriteArray( "history" );
00360 aSize = historyList.count();
00361 for ( int i = 0; i < aSize; ++i )
00362 {
00363 settings.setArrayIndex( i );
00364 settings.setValue( "item", historyList.at( i ) );
00365 }
00366 settings.endArray();
00367
00368 settings.setValue( "index", historyList.indexOf( dbConnectionQString ) );
00369 historyList.clear();
00370 settings.endGroup();
00371
00372
00373
00374
00375
00376 settings.remove( comboBox_DBType->objectName() );
00377 settings.beginGroup( comboBox_DBType->objectName() );
00378 settings.setValue( "index", comboBox_DBType->currentIndex() );
00379 settings.endGroup();
00380
00381
00382
00383
00384
00385 settings.remove( comboBox_Server->objectName() );
00386 settings.beginGroup( comboBox_Server->objectName() );
00387 aSize = comboBox_Server->count();
00388
00389 if ( comboBox_Server->lineEdit() )
00390 historyList << comboBox_Server->lineEdit()->text();
00391 for ( int i = 0; i < aSize; ++i )
00392 {
00393 QString cStr( comboBox_Server->itemText( i ) );
00394 if ( !cStr.trimmed().isEmpty() && !historyList.contains( cStr ) )
00395 {
00396 historyList << cStr;
00397 }
00398 }
00399 settings.beginWriteArray( "history" );
00400 aSize = historyList.count();
00401 for ( int i = 0; i < aSize; ++i )
00402 {
00403 settings.setArrayIndex( i );
00404 settings.setValue( "item", historyList.at( i ) );
00405 }
00406 historyList.clear();
00407 settings.endArray();
00408
00409 settings.setValue( "index", comboBox_Server->currentIndex() );
00410 settings.endGroup();
00411
00412
00413
00414
00415 settings.remove( comboBox_Login->objectName() );
00416 settings.beginGroup( comboBox_Login->objectName() );
00417 aSize = comboBox_Login->count();
00418
00419 if ( comboBox_Login->lineEdit() )
00420 historyList << comboBox_Login->lineEdit()->text();
00421 for ( int i = 0; i < aSize; ++i )
00422 {
00423 QString cStr( comboBox_Login->itemText( i ) );
00424 if ( !cStr.trimmed().isEmpty() && !historyList.contains( cStr ) )
00425 {
00426 historyList << cStr;
00427 }
00428 }
00429 settings.beginWriteArray( "history" );
00430 aSize = historyList.count();
00431 for ( int i = 0; i < aSize; ++i )
00432 {
00433 settings.setArrayIndex( i );
00434 settings.setValue( "item", historyList.at( i ) );
00435 }
00436 historyList.clear();
00437 settings.endArray();
00438
00439 settings.setValue( "index", comboBox_Login->currentIndex() );
00440 settings.endGroup();
00441
00442
00443
00444
00445 settings.remove( comboBox_Schema->objectName() );
00446 settings.beginGroup( comboBox_Schema->objectName() );
00447 aSize = comboBox_Schema->count();
00448
00449 if ( comboBox_Schema->lineEdit() )
00450 historyList << comboBox_Schema->lineEdit()->text();
00451 for ( int i = 0; i < aSize; ++i )
00452 {
00453 QString cStr( comboBox_Schema->itemText( i ) );
00454 if ( !cStr.trimmed().isEmpty() && !historyList.contains( cStr ) )
00455 {
00456 historyList << cStr;
00457 }
00458 }
00459 settings.beginWriteArray( "history" );
00460 aSize = historyList.count();
00461 for ( int i = 0; i < aSize; ++i )
00462 {
00463 settings.setArrayIndex( i );
00464 settings.setValue( "item", historyList.at( i ) );
00465 }
00466 historyList.clear();
00467 settings.endArray();
00468
00469 settings.setValue( "index", comboBox_Schema->currentIndex() );
00470 settings.endGroup();
00471
00472
00473
00474
00475 settings.remove( comboBox_DBName->objectName() );
00476 settings.beginGroup( comboBox_DBName->objectName() );
00477 aSize = comboBox_DBName->count();
00478
00479 if ( comboBox_DBName->lineEdit() )
00480 historyList << comboBox_DBName->lineEdit()->text();
00481 for ( int i = 0; i < aSize; ++i )
00482 {
00483 QString cStr( comboBox_DBName->itemText( i ) );
00484 if ( !cStr.trimmed().isEmpty() && !historyList.contains( cStr ) )
00485 {
00486 historyList << cStr;
00487 }
00488 }
00489 settings.beginWriteArray( "history" );
00490 aSize = historyList.count();
00491 for ( int i = 0; i < aSize; ++i )
00492 {
00493 settings.setArrayIndex( i );
00494 settings.setValue( "item", historyList.at( i ) );
00495 }
00496 historyList.clear();
00497 settings.endArray();
00498
00499 settings.setValue( "index", comboBox_DBName->currentIndex() );
00500 settings.endGroup();
00501
00502
00503 settings.endGroup();
00504 }
00505
00506 void ConnectionDialog::closeConnection( QString connectionString )
00507 {
00508 connectionHash.remove( connectionString );
00509 }
00510
00511 bool ConnectionDialog::isConnected( QString connectionString )
00512 {
00513 if ( connectionString.isEmpty() )
00514 {
00515 return !connectionHash.isEmpty();
00516 }
00517 else
00518 {
00519 return connectionHash.contains( connectionString );
00520 }
00521 }
00522