Ananas Library 0.9.5
deditrc.ui.h
1/****************************************************************************
2** $Id: deditrc.ui.h,v 1.7 2007/10/16 07:36:45 app Exp $
3**
4** Code file of the Ananas edit resource file window
5** of Ananas Designer and Engine applications
6**
7** Created : 20031201
8**
9** Copyright (C) 2003-2004 Leader InfoTech. All rights reserved.
10**
11** This file is part of the Library of the Ananas
12** automation accounting system.
13**
14** This file may be distributed and/or modified under the terms of the
15** GNU General Public License version 2 as published by the Free Software
16** Foundation and appearing in the file LICENSE.GPL included in the
17** packaging of this file.
18**
19** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21**
22** See http://www.leaderit.ru/page=ananas or email sales@leaderit.ru
23** See http://www.leaderit.ru/gpl/ for GPL licensing information.
24**
25** Contact org@leaderit.ru if any conditions of this licensing are
26** not clear to you.
27**
28**********************************************************************/
29
30/****************************************************************************
31** ui.h extension file, included from the uic-generated form implementation.
32**
33** If you wish to add, delete or rename functions or slots use
34** Qt Designer which will update this file, preserving your code. Create an
35** init() function in place of a constructor, and a destroy() function in
36** place of a destructor.
37*****************************************************************************/
38#include <qfiledialog.h>
39#include <qinputdialog.h>
40#include <qmessagebox.h>
41#include <qsqldatabase.h>
42#include <qsqlquery.h>
43#include <qinputdialog.h>
44#include <qvbuttongroup.h>
45#include <qradiobutton.h>
46#include "alog.h"
47//#include <qstring.h>
48
55void dEditRC::init()
56{
57// dataBase = QSqlDatabase::addDatabase( "QMYSQL3U" );
58}
59
66void dEditRC::setdata(QString nameRC, rcListViewItem *item)
67{
68// QFile f;
69// QString s, sv, sn;
70// char buf[1025];
71 it = item;
72 QMap<QString,QString> cfg;
73
74 cfg = aTests::readConfig(QDir::convertSeparators(nameRC));
75 eRCFile->setText(QDir::convertSeparators(nameRC));
76 eDBType->setCurrentItem(0);
77 setCaption( tr("Resource file:") + cfg["dbtitle"]);
78 eDBTitle->setText(cfg["dbtitle"]);
79 eDBName->setText(cfg["dbname"]);
80 eDBUserName->setText(cfg["dbuser"]);
81 ePass->setText(cfg["dbpass"]);
82 eDBHost->setText(cfg["dbhost"]);
83 eDBPort->setText(cfg["dbport"]);
84 eCfgName->setText(QDir::convertSeparators(cfg["configfile"]));
85 if(cfg["dbtype"]=="postgres") eDBType->setCurrentItem(3);
86 if(cfg["dbtype"]=="mysql") eDBType->setCurrentItem(2);
87 if(cfg["dbtype"]=="internal") eDBType->setCurrentItem(1);
88 eWorkDir->setText(QDir::convertSeparators(cfg["workdir"]));
89}
90
97void dEditRC::updatecfg()
98{
99 QMap<QString,QString> cfg;
100
101 cfg["dbtitle"] = eDBTitle->text();
102 cfg["dbname"] = eDBName->text();
103 if(eDBType->currentItem()==1) cfg["dbtype"] ="internal";
104 if(eDBType->currentItem()==2) cfg["dbtype"] ="mysql";
105 if(eDBType->currentItem()==3) cfg["dbtype"]= "postgres";
106 cfg["dbuser"] = eDBUserName->text();
107 cfg["dbpass"] = ePass->text();
108 cfg["dbhost"] = eDBHost->text();
109 cfg["dbport"] = eDBPort->text();
110 cfg["workdir"] = QDir::convertSeparators(eWorkDir->text());
111 cfg["configfile"]= QDir::convertSeparators(eCfgName->text());
112
113 aTests::writeConfig(QDir::convertSeparators(eRCFile->text()),cfg);
114}
115
122void dEditRC::onOK()
123{
124 updatecfg();
125 it->rcfile = QDir::convertSeparators(eRCFile->text());
126 it->setText(0,eDBTitle->text());
127 accept();
128}
129
136void dEditRC::onRCFile()
137{
138 QFileDialog fd( QString::null,
139 tr("ananas config resource (*.rc)"),
140 0, 0, TRUE );
141 fd. setMode ( QFileDialog::AnyFile );
142 fd.setSelection( QDir::convertSeparators(eRCFile->text()));
143 if ( fd.exec() == QDialog::Accepted ) {
144 eRCFile->setText(QDir::convertSeparators(fd.selectedFile()));
145 setdata(eRCFile->text(),it);
146 } else {
147 return;
148 }
149}
150
157void dEditRC::onCFGFile()
158{
159 QFileDialog fd( QString::null,
160 tr("ananas config file (*.cfg)"),
161 0, 0, TRUE );
162 fd. setMode ( QFileDialog::AnyFile );
163 fd.setSelection( QDir::convertSeparators(eCfgName->text()));
164 if ( fd.exec() == QDialog::Accepted ) {
165 eCfgName->setText(QDir::convertSeparators(fd.selectedFile()));
166 } else {
167 return;
168 }
169}
170
177void dEditRC::on_wDirCFG()
178{
179 QFileDialog wd( QString::null,
180 tr("ananas working directory"),
181 0, 0, TRUE );
182 wd. setMode ( QFileDialog::DirectoryOnly );
183 wd.setSelection( QDir::convertSeparators(eCfgName->text()));
184 if ( wd.exec() == QDialog::Accepted ) {
185 eWorkDir->setText(wd.dirPath());
186 } else {
187 return;
188 }
189}
190
197void dEditRC::bTest_clicked()
198{
199 if (eDBHost->text() == "" || eDBName->text() == "" || eDBUserName->text() == "" || ePass->text() == "" )
200 {
201 QString err_mess = "";
202 if (eDBHost->text() == "")
203 {
204 err_mess = err_mess + tr("- Enter HostName.\n");
205 }
206 if (eDBName->text() == "")
207 {
208 err_mess = err_mess + tr("- Enter DBName.\n");
209 }
210 if (eDBUserName->text() == "")
211 {
212 err_mess = err_mess + tr("- Enter DBUserName.\n");
213 }
214
215 if (ePass->text() == "")
216 {
217 err_mess = err_mess + tr("- Enter DBPassword.\n");
218 }
219
220 QMessageBox::warning( this, tr("Test connection"),
221 QString(tr("Test imposible.\n"
222 "No all data collected...\n"))+err_mess);
223 return;
224 }
225
226 eDBType_activated(eDBType->currentItem() );
227 QString dbhost = eDBHost->text();
228 QString dbname = eDBName->text();
229 QString dbuser = eDBUserName->text();
230 QString dbpass = ePass->text();
231 QString dbport = eDBPort->text();
232 dataBase->setUserName( dbuser );
233 dataBase->setPassword( dbpass );
234 dataBase->setHostName( dbhost );
235 dataBase->setDatabaseName( dbname );
236
237 if ( !dbport.isEmpty() ) {
238 dataBase->setPort( eDBPort->text().toInt() );
239 }
240
241 if ( ! dataBase->open() )
242 {
243 QMessageBox::warning( this, "RC-file Editor",
244 QString(tr("Unable to converse with a database.\n"
245 "Reason:\n %1")).arg(dataBase->lastError().text()));
246 aLog::print(aLog::MT_ERROR, QString("RC-Editor. Test Access to DataBase: %1").arg(dataBase->lastError().text()));
247
248 }else{
249 QMessageBox::information( this, "RC-file Editor",
250 QString(tr("User <b>%1</b> granted to use base <b>%2</b>.")).arg(dbuser).arg(dbname) );
251 }
252}
253
260void dEditRC::bFindBD_clicked()
261{
262
263 if ( prepareDB() )
264 {
265 int k;
266 QSqlQuery q;
267 QDialog *dlg = new QDialog ( );
268 QVBoxLayout *vbox = new QVBoxLayout( dlg, 11, 6 );
269 QVBoxLayout *box1 = new QVBoxLayout( vbox );
270 QHBoxLayout *box2 = new QHBoxLayout( vbox );
271 dlg->setModal (TRUE);
272 dlg->setCaption(tr("Select"));
273 QButtonGroup *bg = new QButtonGroup( 10,
274 QGroupBox::Vertical, tr("Available DataBases"),dlg);
275 box1->addWidget( bg );
276 bg->setExclusive( TRUE );
277 QRadioButton *createNew = new QRadioButton( tr("Create"), bg );
278 createNew->setChecked( TRUE );
279 switch ( eDBType->currentItem() ) {
280 case 2:
281 q = dataBase->exec("SHOW DATABASES;");
282 break;
283 case 3:
284 q = dataBase->exec("SELECT * FROM pg_database;");
285 break;
286 default:
287 q = dataBase->exec("SHOW DATABASES;");
288 break;
289 }
290 while ( q.next() )
291 {
292 QString name = q.value(0).toString();
293 bases.append(name);
294 (void) new QRadioButton( QString("%1").arg(name), bg );
295 }
296 //
297 QPushButton *okB = new QPushButton(tr("Select"), dlg);
298 QPushButton *clB = new QPushButton(tr("Cancel"), dlg);
299 box2->addWidget( okB );
300 box2->addWidget( clB );
301 connect( okB, SIGNAL( clicked() ), dlg, SLOT( accept() ) );
302 connect( clB, SIGNAL( clicked() ), dlg, SLOT( close() ) );
303 dlg->show ( );
304 if ( dlg->exec() == QDialog::Accepted )
305 {
306 for (k=0; k < bg->count(); k++)
307 {
308 if (bg->find(k)->state()
309 && bg->find(k)->text()
310 != tr("Create") )
311 {
312 eDBName->setText(bg->find(k)->text());
313 }
314 if ( bg->find(k)->state()
315 && bg->find(k)->text()
316 == tr("Create"))
317 {
318 eDBName->setText("");
319 bCreareBD_clicked();
320 }
321 }
322 }
323}
324}
325
332void dEditRC::eDBTitle_textChanged( const QString & )
333{
334 setCaption( tr("Resource file: ") + eDBTitle->text() );
335}
336
343bool dEditRC::prepareDB()
344{
345 eDBType_activated( eDBType->currentItem() );
346 QString prompt = tr("Enter a root password:");
347 QString pass;
348 bool ok;
349 QString dbhost = eDBHost->text();
350 QString dbport = eDBPort->text();
351 QString dbname = eDBName->text();
352
353 dataBase->setHostName( dbhost );
354 if ( !dbport.isEmpty() ) {
355 dataBase->setPort( eDBPort->text().toInt() );
356 }
357 switch ( eDBType->currentItem() )
358 {
359 case 2:
360 prompt = tr("Enter a <b>root</b> password:");
361 dataBase->setUserName( "root");
362 dataBase->setDatabaseName( "test" );
363 break;
364 case 3:
365 prompt = tr("Enter a <b>postgres</b> password:");
366 dataBase->setUserName( "postgres");
367 dataBase->setDatabaseName( "postgres" );
368 break;
369 default:
370 prompt = tr("Enter a <b>root</b> password:");
371 dataBase->setUserName( "root");
372 dataBase->setDatabaseName( "test" );
373 break;
374 }
375
376 if (rootpass == "")
377 {
378 pass = QInputDialog::getText(
379 tr("Root access required"),
380 prompt, QLineEdit::Password,
381 QString::null, &ok, this );
382 if ( ok && !pass.isEmpty() )
383 {
384 rootpass = pass;
385 }else{
386 return FALSE;
387 }
388 }
389 dataBase->setPassword( rootpass );
390 if ( ! dataBase->open() )
391 {
392 QMessageBox::information( this, "RC-file Editor",
393 QString("Unable to converse with a database."
394 "Reason: \n""%1").arg(dataBase->lastError().text()));
395 aLog::print(aLog::MT_ERROR, QString("RC-Editor. Prepare DB error: %1").arg(dataBase->lastError().text()));
396 return FALSE;
397 }
398 return TRUE;
399}
400
407void dEditRC::bCreareBD_clicked()
408{
409 if ( prepareDB() )
410 {
411 QString prompt = tr("Add new DataBase:");
412 bool ok, yes=TRUE;
413 QString db_name, query;
414
415 if ( ! dataBase->open() )
416 {
417 QMessageBox::information( this, "RC-file Editor",
418 QString("Unable to converse with a database."
419 "Reason: \n""%1").arg(dataBase->lastError().text()));
420 aLog::print(aLog::MT_ERROR, QString("RC-Editor. Create DB error: %1").arg(dataBase->lastError().text()));
421
422 }else{
423 while (yes)
424 {
425 db_name = QInputDialog::getText(
426 tr("Enter a <b>new</b> database name:"),
427 prompt, QLineEdit::Normal, QString::null, &ok, this );
428 if ( ok && !db_name.isEmpty() )
429 {
430 if (bases.contains(db_name) == 0 )
431 {
432 yes=FALSE;
433 }else
434 {
435 prompt = QString(tr("Name <b>%1</b> alredy exist.\nEnter another name:").arg(db_name));
436 yes=TRUE;
437 }
438 }else{
439 db_name = "";
440 yes=FALSE;
441 }
442 }
443
444 }
445 if ( db_name != "")
446 {
447 switch ( eDBType->currentItem() ) {
448 case 2:
449 query = QString("CREATE DATABASE %1 character set utf8").arg(db_name);
450 break;
451 case 3:
452 query = QString("CREATE DATABASE %1 ENCODING = \'UTF8\'").arg(db_name);
453 break;
454 default:
455 query = QString("");
456 break;
457 }
458
459
460 QSqlQuery q = dataBase->exec(query);
461 eDBName->setText(db_name);
462 aLog::print(aLog::MT_INFO, QString("RC-Editor. Created new DataBase %1").arg(db_name));
463 }
464}
465}
466
473void dEditRC::bCreareUser_clicked()
474{
475 if (prepareDB() )
476 {
477 QString prompt = tr("Add new User:");
478 bool ok, yes=TRUE;
479 QString user_name, pass, query;
480 QSqlQuery q;
481
482// Check for existen users &
483 prepareDB();
484 if ( ! dataBase->open() )
485 {
486 QMessageBox::information( this, "RC-file Editor",
487 QString("Unable to converse with a database."
488 "Reason: \n""%1").arg(dataBase->lastError().text()));
489 }else{
490 while (yes)
491 {
492 user_name= QInputDialog::getText(
493 tr("Enter a <b>new</b> user name:"),
494 prompt, QLineEdit::Normal, QString::null, &ok, this );
495 if ( ok && !user_name.isEmpty() )
496 {
497 if (users.contains(user_name) == 0 )
498 {
499 yes=FALSE;
500 }else{
501 prompt = QString(tr("Name <b>%1</b> alredy exist.\nEnter another name:")).arg(user_name);
502 yes=TRUE;
503 }
504 }else{
505 user_name = "";
506 yes=FALSE;
507 }
508 }
509 }
510 if ( user_name != "")
511 {
512 prompt = tr("Set password:");
513 pass = QInputDialog::getText(
514 tr("Enter a password for this user:"),
515 prompt, QLineEdit::Password, QString::null, &ok, this );
516 if ( ok && !pass.isEmpty() )
517 {
518 eDBUserName->setText(user_name);
519 ePass->setText(pass);
520
521 switch ( eDBType->currentItem() ) {
522 case 2:
523 query = QString("CREATE USER \'%1\'@\'%2\' IDENTIFIED BY \'%3\'").arg(eDBUserName->text()).arg(eDBHost->text()).arg(ePass->text());
524 q = dataBase->exec(query);
525 query = QString("GRANT ALL PRIVILEGES ON %1 .* TO \'%2\'@\'%3\' WITH GRANT OPTION").arg(eDBName->text()).arg(eDBUserName->text()).arg(eDBHost->text());
526 q = dataBase->exec(query);
527 query = QString("SET PASSWORD FOR \'%1\'@\'%2\' = PASSWORD(\'%3\'").arg(eDBUserName->text()).arg(eDBHost->text()).arg(pass);
528 q = dataBase->exec(query);
529 break;
530 case 3:
531 query = QString("CREATE ROLE %1 WITH LOGIN PASSWORD \'%2\'").arg(eDBUserName->text()).arg(pass);
532 q = dataBase->exec(query);
533 query = QString("GRANT ALL ON DATABASE %1 TO %2").arg(eDBName->text()).arg(eDBUserName->text());
534 q = dataBase->exec(query);
535 break;
536 default:
537 query = QString("");
538 break;
539 }
540 }
541 aLog::print(aLog::MT_INFO, QString("RC-Editor. Created new user %1 with ALL rights to %2").arg(eDBUserName->text()).arg(eDBName->text()));
542 }
543 }
544}
545
552void dEditRC::eDBType_activated( int dbInd)
553{
554 switch ( dbInd ) {
555 case 0:
556 bTest->setEnabled(FALSE);
557 bFindBD->setEnabled(FALSE);
558 bCreareUser->setEnabled(FALSE);
559 break;
560 case 1:
561 dataBase = QSqlDatabase::addDatabase( "QSQLITE" );
562 eDBPort->setText("");
563 bFindBD->setEnabled(FALSE);
564 bCreareUser->setEnabled(FALSE);
565 bTest->setEnabled(FALSE);
566 break;
567 case 2:
568 dataBase = QSqlDatabase::addDatabase( "QMYSQL3U" );
569 eDBPort->setText("3306");
570 bTest->setEnabled(TRUE);
571 bFindBD->setEnabled(TRUE);
572 bCreareUser->setEnabled(TRUE);
573 break;
574 case 3:
575 dataBase = QSqlDatabase::addDatabase( "QPSQL7" );
576 eDBPort->setText("5432");
577 bTest->setEnabled(TRUE);
578 bFindBD->setEnabled(TRUE);
579 bCreareUser->setEnabled(TRUE);
580 break;
581 default:
582 dataBase = QSqlDatabase::addDatabase( "QMYSQL3U" );
583 eDBPort->setText("");
584 bTest->setEnabled(FALSE);
585 bFindBD->setEnabled(FALSE);
586 bCreareUser->setEnabled(FALSE);
587 break;
588 }
589}
590
597void dEditRC::bFindUser()
598{
599
600 if ( prepareDB() )
601 {
602 int k;
603 QSqlQuery q;
604 QString query;
605 QDialog *dlg = new QDialog ( );
606 QVBoxLayout *vbox = new QVBoxLayout( dlg, 11, 6 );
607 QVBoxLayout *box1 = new QVBoxLayout( vbox );
608 QHBoxLayout *box2 = new QHBoxLayout( vbox );
609 dlg->setModal (TRUE);
610 dlg->setCaption(tr("Select"));
611 QButtonGroup *bg = new QButtonGroup( 10,
612 QGroupBox::Vertical, tr("Available Users"),dlg);
613 box1->addWidget( bg );
614 bg->setExclusive( TRUE );
615 QRadioButton *createNew = new QRadioButton( tr("Create"), bg );
616 createNew->setChecked( TRUE );
617 switch ( eDBType->currentItem() ) {
618 case 2:
619 q = dataBase->exec("SELECT User FROM mysql.user;");
620 break;
621 case 3:
622 q = dataBase->exec("SELECT * FROM pg_roles;");
623 break;
624 default:
625 q = dataBase->exec("SELECT User FROM mysql.user;");
626 break;
627 }
628
629 while ( q.next() )
630 {
631 if (q.value(0).toString() != "root")
632 {
633 QString name = q.value(0).toString();
634 users.append(name);
635 (void) new QRadioButton( QString("%1").arg(name), bg );
636 }
637 }
638 QPushButton *okB = new QPushButton(tr("Select"), dlg);
639 QPushButton *clB = new QPushButton(tr("Cancel"), dlg);
640 box2->addWidget( okB );
641 box2->addWidget( clB );
642 connect( okB, SIGNAL( clicked() ), dlg, SLOT( accept() ) );
643 connect( clB, SIGNAL( clicked() ), dlg, SLOT( close() ) );
644 dlg->show ( );
645 if ( dlg->exec() == QDialog::Accepted )
646 {
647 for (k=0; k < bg->count(); k++)
648 {
649 if (bg->find(k)->state()
650 && bg->find(k)->text()
651 != tr("Create") )
652 {
653 eDBUserName->setText(bg->find(k)->text());
654 ePass->setText("");
655 }
656 if ( bg->find(k)->state()
657 && bg->find(k)->text()
658 == tr("Create"))
659 {
660 eDBUserName->setText("");
661 ePass->setText("");
662 bCreareUser_clicked();
663 }
664 }
665 }
666 }
667}
static void print(int status, const QString &text)
Definition alog.cpp:58
static bool writeConfig(const QString &cfg_name, QMap< QString, QString > cfg, const QString &log_name=QString::null)
Definition atests.cpp:140
static QMap< QString, QString > readConfig(const QString &cfg_name, const QString &log_name=QString::null)
Definition atests.cpp:89
Definition rclistviewitem.h:46