00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <qfiledialog.h>
00039 #include <qinputdialog.h>
00040 #include <qmessagebox.h>
00041 #include <qsqldatabase.h>
00042 #include <qsqlquery.h>
00043 #include <qinputdialog.h>
00044 #include <qvbuttongroup.h>
00045 #include <qradiobutton.h>
00046 #include "alog.h"
00047
00048
00055 void dEditRC::init()
00056 {
00057
00058 }
00059
00066 void dEditRC::setdata(QString nameRC, rcListViewItem *item)
00067 {
00068
00069
00070
00071 it = item;
00072 QMap<QString,QString> cfg;
00073
00074 cfg = aTests::readConfig(QDir::convertSeparators(nameRC));
00075 eRCFile->setText(QDir::convertSeparators(nameRC));
00076 eDBType->setCurrentItem(0);
00077 setCaption( tr("Resource file:") + cfg["dbtitle"]);
00078 eDBTitle->setText(cfg["dbtitle"]);
00079 eDBName->setText(cfg["dbname"]);
00080 eDBUserName->setText(cfg["dbuser"]);
00081 ePass->setText(cfg["dbpass"]);
00082 eDBHost->setText(cfg["dbhost"]);
00083 eDBPort->setText(cfg["dbport"]);
00084 eCfgName->setText(QDir::convertSeparators(cfg["configfile"]));
00085 if(cfg["dbtype"]=="postgres") eDBType->setCurrentItem(3);
00086 if(cfg["dbtype"]=="mysql") eDBType->setCurrentItem(2);
00087 if(cfg["dbtype"]=="internal") eDBType->setCurrentItem(1);
00088 eWorkDir->setText(QDir::convertSeparators(cfg["workdir"]));
00089 }
00090
00097 void dEditRC::updatecfg()
00098 {
00099 QMap<QString,QString> cfg;
00100
00101 cfg["dbtitle"] = eDBTitle->text();
00102 cfg["dbname"] = eDBName->text();
00103 if(eDBType->currentItem()==1) cfg["dbtype"] ="internal";
00104 if(eDBType->currentItem()==2) cfg["dbtype"] ="mysql";
00105 if(eDBType->currentItem()==3) cfg["dbtype"]= "postgres";
00106 cfg["dbuser"] = eDBUserName->text();
00107 cfg["dbpass"] = ePass->text();
00108 cfg["dbhost"] = eDBHost->text();
00109 cfg["dbport"] = eDBPort->text();
00110 cfg["workdir"] = QDir::convertSeparators(eWorkDir->text());
00111 cfg["configfile"]= QDir::convertSeparators(eCfgName->text());
00112
00113 aTests::writeConfig(QDir::convertSeparators(eRCFile->text()),cfg);
00114 }
00115
00122 void dEditRC::onOK()
00123 {
00124 updatecfg();
00125 it->rcfile = QDir::convertSeparators(eRCFile->text());
00126 it->setText(0,eDBTitle->text());
00127 accept();
00128 }
00129
00136 void dEditRC::onRCFile()
00137 {
00138 QFileDialog fd( QString::null,
00139 tr("ananas config resource (*.rc)"),
00140 0, 0, TRUE );
00141 fd. setMode ( QFileDialog::AnyFile );
00142 fd.setSelection( QDir::convertSeparators(eRCFile->text()));
00143 if ( fd.exec() == QDialog::Accepted ) {
00144 eRCFile->setText(QDir::convertSeparators(fd.selectedFile()));
00145 setdata(eRCFile->text(),it);
00146 } else {
00147 return;
00148 }
00149 }
00150
00157 void dEditRC::onCFGFile()
00158 {
00159 QFileDialog fd( QString::null,
00160 tr("ananas config file (*.cfg)"),
00161 0, 0, TRUE );
00162 fd. setMode ( QFileDialog::AnyFile );
00163 fd.setSelection( QDir::convertSeparators(eCfgName->text()));
00164 if ( fd.exec() == QDialog::Accepted ) {
00165 eCfgName->setText(QDir::convertSeparators(fd.selectedFile()));
00166 } else {
00167 return;
00168 }
00169 }
00170
00177 void dEditRC::on_wDirCFG()
00178 {
00179 QFileDialog wd( QString::null,
00180 tr("ananas working directory"),
00181 0, 0, TRUE );
00182 wd. setMode ( QFileDialog::DirectoryOnly );
00183 wd.setSelection( QDir::convertSeparators(eCfgName->text()));
00184 if ( wd.exec() == QDialog::Accepted ) {
00185 eWorkDir->setText(wd.dirPath());
00186 } else {
00187 return;
00188 }
00189 }
00190
00197 void dEditRC::bTest_clicked()
00198 {
00199 if (eDBHost->text() == "" || eDBName->text() == "" || eDBUserName->text() == "" || ePass->text() == "" )
00200 {
00201 QString err_mess = "";
00202 if (eDBHost->text() == "")
00203 {
00204 err_mess = err_mess + tr("- Enter HostName.\n");
00205 }
00206 if (eDBName->text() == "")
00207 {
00208 err_mess = err_mess + tr("- Enter DBName.\n");
00209 }
00210 if (eDBUserName->text() == "")
00211 {
00212 err_mess = err_mess + tr("- Enter DBUserName.\n");
00213 }
00214
00215 if (ePass->text() == "")
00216 {
00217 err_mess = err_mess + tr("- Enter DBPassword.\n");
00218 }
00219
00220 QMessageBox::warning( this, tr("Test connection"),
00221 QString(tr("Test imposible.\n"
00222 "No all data collected...\n"))+err_mess);
00223 return;
00224 }
00225
00226 eDBType_activated(eDBType->currentItem() );
00227 QString dbhost = eDBHost->text();
00228 QString dbname = eDBName->text();
00229 QString dbuser = eDBUserName->text();
00230 QString dbpass = ePass->text();
00231 QString dbport = eDBPort->text();
00232 dataBase->setUserName( dbuser );
00233 dataBase->setPassword( dbpass );
00234 dataBase->setHostName( dbhost );
00235 dataBase->setDatabaseName( dbname );
00236
00237 if ( !dbport.isEmpty() ) {
00238 dataBase->setPort( eDBPort->text().toInt() );
00239 }
00240
00241 if ( ! dataBase->open() )
00242 {
00243 QMessageBox::warning( this, "RC-file Editor",
00244 QString(tr("Unable to converse with a database.\n"
00245 "Reason:\n %1")).arg(dataBase->lastError().text()));
00246 aLog::print(aLog::MT_ERROR, QString("RC-Editor. Test Access to DataBase: %1").arg(dataBase->lastError().text()));
00247
00248 }else{
00249 QMessageBox::information( this, "RC-file Editor",
00250 QString(tr("User <b>%1</b> granted to use base <b>%2</b>.")).arg(dbuser).arg(dbname) );
00251 }
00252 }
00253
00260 void dEditRC::bFindBD_clicked()
00261 {
00262
00263 if ( prepareDB() )
00264 {
00265 int k;
00266 QSqlQuery q;
00267 QDialog *dlg = new QDialog ( );
00268 QVBoxLayout *vbox = new QVBoxLayout( dlg, 11, 6 );
00269 QVBoxLayout *box1 = new QVBoxLayout( vbox );
00270 QHBoxLayout *box2 = new QHBoxLayout( vbox );
00271 dlg->setModal (TRUE);
00272 dlg->setCaption(tr("Select"));
00273 QButtonGroup *bg = new QButtonGroup( 10,
00274 QGroupBox::Vertical, tr("Available DataBases"),dlg);
00275 box1->addWidget( bg );
00276 bg->setExclusive( TRUE );
00277 QRadioButton *createNew = new QRadioButton( tr("Create"), bg );
00278 createNew->setChecked( TRUE );
00279 switch ( eDBType->currentItem() ) {
00280 case 2:
00281 q = dataBase->exec("SHOW DATABASES;");
00282 break;
00283 case 3:
00284 q = dataBase->exec("SELECT * FROM pg_database;");
00285 break;
00286 default:
00287 q = dataBase->exec("SHOW DATABASES;");
00288 break;
00289 }
00290 while ( q.next() )
00291 {
00292 QString name = q.value(0).toString();
00293 bases.append(name);
00294 (void) new QRadioButton( QString("%1").arg(name), bg );
00295 }
00296
00297 QPushButton *okB = new QPushButton(tr("Select"), dlg);
00298 QPushButton *clB = new QPushButton(tr("Cancel"), dlg);
00299 box2->addWidget( okB );
00300 box2->addWidget( clB );
00301 connect( okB, SIGNAL( clicked() ), dlg, SLOT( accept() ) );
00302 connect( clB, SIGNAL( clicked() ), dlg, SLOT( close() ) );
00303 dlg->show ( );
00304 if ( dlg->exec() == QDialog::Accepted )
00305 {
00306 for (k=0; k < bg->count(); k++)
00307 {
00308 if (bg->find(k)->state()
00309 && bg->find(k)->text()
00310 != tr("Create") )
00311 {
00312 eDBName->setText(bg->find(k)->text());
00313 }
00314 if ( bg->find(k)->state()
00315 && bg->find(k)->text()
00316 == tr("Create"))
00317 {
00318 eDBName->setText("");
00319 bCreareBD_clicked();
00320 }
00321 }
00322 }
00323 }
00324 }
00325
00332 void dEditRC::eDBTitle_textChanged( const QString & )
00333 {
00334 setCaption( tr("Resource file: ") + eDBTitle->text() );
00335 }
00336
00343 bool dEditRC::prepareDB()
00344 {
00345 eDBType_activated( eDBType->currentItem() );
00346 QString prompt = tr("Enter a root password:");
00347 QString pass;
00348 bool ok;
00349 QString dbhost = eDBHost->text();
00350 QString dbport = eDBPort->text();
00351 QString dbname = eDBName->text();
00352
00353 dataBase->setHostName( dbhost );
00354 if ( !dbport.isEmpty() ) {
00355 dataBase->setPort( eDBPort->text().toInt() );
00356 }
00357 switch ( eDBType->currentItem() )
00358 {
00359 case 2:
00360 prompt = tr("Enter a <b>root</b> password:");
00361 dataBase->setUserName( "root");
00362 dataBase->setDatabaseName( "test" );
00363 break;
00364 case 3:
00365 prompt = tr("Enter a <b>postgres</b> password:");
00366 dataBase->setUserName( "postgres");
00367 dataBase->setDatabaseName( "postgres" );
00368 break;
00369 default:
00370 prompt = tr("Enter a <b>root</b> password:");
00371 dataBase->setUserName( "root");
00372 dataBase->setDatabaseName( "test" );
00373 break;
00374 }
00375
00376 if (rootpass == "")
00377 {
00378 pass = QInputDialog::getText(
00379 tr("Root access required"),
00380 prompt, QLineEdit::Password,
00381 QString::null, &ok, this );
00382 if ( ok && !pass.isEmpty() )
00383 {
00384 rootpass = pass;
00385 }else{
00386 return FALSE;
00387 }
00388 }
00389 dataBase->setPassword( rootpass );
00390 if ( ! dataBase->open() )
00391 {
00392 QMessageBox::information( this, "RC-file Editor",
00393 QString("Unable to converse with a database."
00394 "Reason: \n""%1").arg(dataBase->lastError().text()));
00395 aLog::print(aLog::MT_ERROR, QString("RC-Editor. Prepare DB error: %1").arg(dataBase->lastError().text()));
00396 return FALSE;
00397 }
00398 return TRUE;
00399 }
00400
00407 void dEditRC::bCreareBD_clicked()
00408 {
00409 if ( prepareDB() )
00410 {
00411 QString prompt = tr("Add new DataBase:");
00412 bool ok, yes=TRUE;
00413 QString db_name, query;
00414
00415 if ( ! dataBase->open() )
00416 {
00417 QMessageBox::information( this, "RC-file Editor",
00418 QString("Unable to converse with a database."
00419 "Reason: \n""%1").arg(dataBase->lastError().text()));
00420 aLog::print(aLog::MT_ERROR, QString("RC-Editor. Create DB error: %1").arg(dataBase->lastError().text()));
00421
00422 }else{
00423 while (yes)
00424 {
00425 db_name = QInputDialog::getText(
00426 tr("Enter a <b>new</b> database name:"),
00427 prompt, QLineEdit::Normal, QString::null, &ok, this );
00428 if ( ok && !db_name.isEmpty() )
00429 {
00430 if (bases.contains(db_name) == 0 )
00431 {
00432 yes=FALSE;
00433 }else
00434 {
00435 prompt = QString(tr("Name <b>%1</b> alredy exist.\nEnter another name:").arg(db_name));
00436 yes=TRUE;
00437 }
00438 }else{
00439 db_name = "";
00440 yes=FALSE;
00441 }
00442 }
00443
00444 }
00445 if ( db_name != "")
00446 {
00447 switch ( eDBType->currentItem() ) {
00448 case 2:
00449 query = QString("CREATE DATABASE %1 character set utf8").arg(db_name);
00450 break;
00451 case 3:
00452 query = QString("CREATE DATABASE %1 ENCODING = \'UTF8\'").arg(db_name);
00453 break;
00454 default:
00455 query = QString("");
00456 break;
00457 }
00458
00459
00460 QSqlQuery q = dataBase->exec(query);
00461 eDBName->setText(db_name);
00462 aLog::print(aLog::MT_INFO, QString("RC-Editor. Created new DataBase %1").arg(db_name));
00463 }
00464 }
00465 }
00466
00473 void dEditRC::bCreareUser_clicked()
00474 {
00475 if (prepareDB() )
00476 {
00477 QString prompt = tr("Add new User:");
00478 bool ok, yes=TRUE;
00479 QString user_name, pass, query;
00480 QSqlQuery q;
00481
00482
00483 prepareDB();
00484 if ( ! dataBase->open() )
00485 {
00486 QMessageBox::information( this, "RC-file Editor",
00487 QString("Unable to converse with a database."
00488 "Reason: \n""%1").arg(dataBase->lastError().text()));
00489 }else{
00490 while (yes)
00491 {
00492 user_name= QInputDialog::getText(
00493 tr("Enter a <b>new</b> user name:"),
00494 prompt, QLineEdit::Normal, QString::null, &ok, this );
00495 if ( ok && !user_name.isEmpty() )
00496 {
00497 if (users.contains(user_name) == 0 )
00498 {
00499 yes=FALSE;
00500 }else{
00501 prompt = QString(tr("Name <b>%1</b> alredy exist.\nEnter another name:")).arg(user_name);
00502 yes=TRUE;
00503 }
00504 }else{
00505 user_name = "";
00506 yes=FALSE;
00507 }
00508 }
00509 }
00510 if ( user_name != "")
00511 {
00512 prompt = tr("Set password:");
00513 pass = QInputDialog::getText(
00514 tr("Enter a password for this user:"),
00515 prompt, QLineEdit::Password, QString::null, &ok, this );
00516 if ( ok && !pass.isEmpty() )
00517 {
00518 eDBUserName->setText(user_name);
00519 ePass->setText(pass);
00520
00521 switch ( eDBType->currentItem() ) {
00522 case 2:
00523 query = QString("CREATE USER \'%1\'@\'%2\' IDENTIFIED BY \'%3\'").arg(eDBUserName->text()).arg(eDBHost->text()).arg(ePass->text());
00524 q = dataBase->exec(query);
00525 query = QString("GRANT ALL PRIVILEGES ON %1 .* TO \'%2\'@\'%3\' WITH GRANT OPTION").arg(eDBName->text()).arg(eDBUserName->text()).arg(eDBHost->text());
00526 q = dataBase->exec(query);
00527 query = QString("SET PASSWORD FOR \'%1\'@\'%2\' = PASSWORD(\'%3\'").arg(eDBUserName->text()).arg(eDBHost->text()).arg(pass);
00528 q = dataBase->exec(query);
00529 break;
00530 case 3:
00531 query = QString("CREATE ROLE %1 WITH LOGIN PASSWORD \'%2\'").arg(eDBUserName->text()).arg(pass);
00532 q = dataBase->exec(query);
00533 query = QString("GRANT ALL ON DATABASE %1 TO %2").arg(eDBName->text()).arg(eDBUserName->text());
00534 q = dataBase->exec(query);
00535 break;
00536 default:
00537 query = QString("");
00538 break;
00539 }
00540 }
00541 aLog::print(aLog::MT_INFO, QString("RC-Editor. Created new user %1 with ALL rights to %2").arg(eDBUserName->text()).arg(eDBName->text()));
00542 }
00543 }
00544 }
00545
00552 void dEditRC::eDBType_activated( int dbInd)
00553 {
00554 switch ( dbInd ) {
00555 case 0:
00556 bTest->setEnabled(FALSE);
00557 bFindBD->setEnabled(FALSE);
00558 bCreareUser->setEnabled(FALSE);
00559 break;
00560 case 1:
00561 dataBase = QSqlDatabase::addDatabase( "QSQLITE" );
00562 eDBPort->setText("");
00563 bFindBD->setEnabled(FALSE);
00564 bCreareUser->setEnabled(FALSE);
00565 bTest->setEnabled(FALSE);
00566 break;
00567 case 2:
00568 dataBase = QSqlDatabase::addDatabase( "QMYSQL3U" );
00569 eDBPort->setText("3306");
00570 bTest->setEnabled(TRUE);
00571 bFindBD->setEnabled(TRUE);
00572 bCreareUser->setEnabled(TRUE);
00573 break;
00574 case 3:
00575 dataBase = QSqlDatabase::addDatabase( "QPSQL7" );
00576 eDBPort->setText("5432");
00577 bTest->setEnabled(TRUE);
00578 bFindBD->setEnabled(TRUE);
00579 bCreareUser->setEnabled(TRUE);
00580 break;
00581 default:
00582 dataBase = QSqlDatabase::addDatabase( "QMYSQL3U" );
00583 eDBPort->setText("");
00584 bTest->setEnabled(FALSE);
00585 bFindBD->setEnabled(FALSE);
00586 bCreareUser->setEnabled(FALSE);
00587 break;
00588 }
00589 }
00590
00597 void dEditRC::bFindUser()
00598 {
00599
00600 if ( prepareDB() )
00601 {
00602 int k;
00603 QSqlQuery q;
00604 QString query;
00605 QDialog *dlg = new QDialog ( );
00606 QVBoxLayout *vbox = new QVBoxLayout( dlg, 11, 6 );
00607 QVBoxLayout *box1 = new QVBoxLayout( vbox );
00608 QHBoxLayout *box2 = new QHBoxLayout( vbox );
00609 dlg->setModal (TRUE);
00610 dlg->setCaption(tr("Select"));
00611 QButtonGroup *bg = new QButtonGroup( 10,
00612 QGroupBox::Vertical, tr("Available Users"),dlg);
00613 box1->addWidget( bg );
00614 bg->setExclusive( TRUE );
00615 QRadioButton *createNew = new QRadioButton( tr("Create"), bg );
00616 createNew->setChecked( TRUE );
00617 switch ( eDBType->currentItem() ) {
00618 case 2:
00619 q = dataBase->exec("SELECT User FROM mysql.user;");
00620 break;
00621 case 3:
00622 q = dataBase->exec("SELECT * FROM pg_roles;");
00623 break;
00624 default:
00625 q = dataBase->exec("SELECT User FROM mysql.user;");
00626 break;
00627 }
00628
00629 while ( q.next() )
00630 {
00631 if (q.value(0).toString() != "root")
00632 {
00633 QString name = q.value(0).toString();
00634 users.append(name);
00635 (void) new QRadioButton( QString("%1").arg(name), bg );
00636 }
00637 }
00638 QPushButton *okB = new QPushButton(tr("Select"), dlg);
00639 QPushButton *clB = new QPushButton(tr("Cancel"), dlg);
00640 box2->addWidget( okB );
00641 box2->addWidget( clB );
00642 connect( okB, SIGNAL( clicked() ), dlg, SLOT( accept() ) );
00643 connect( clB, SIGNAL( clicked() ), dlg, SLOT( close() ) );
00644 dlg->show ( );
00645 if ( dlg->exec() == QDialog::Accepted )
00646 {
00647 for (k=0; k < bg->count(); k++)
00648 {
00649 if (bg->find(k)->state()
00650 && bg->find(k)->text()
00651 != tr("Create") )
00652 {
00653 eDBUserName->setText(bg->find(k)->text());
00654 ePass->setText("");
00655 }
00656 if ( bg->find(k)->state()
00657 && bg->find(k)->text()
00658 == tr("Create"))
00659 {
00660 eDBUserName->setText("");
00661 ePass->setText("");
00662 bCreareUser_clicked();
00663 }
00664 }
00665 }
00666 }
00667 }