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
00039
00040 #include <qdir.h>
00041 #include <qstring.h>
00042 #include <qfiledialog.h>
00043 #include <qlineedit.h>
00044 #include <qstring.h>
00045 #include <qdom.h>
00046
00047 #include "deditrc.h"
00048 #include "dimportdb.h"
00049 #include "acfgrc.h"
00050 #include "atests.h"
00051 #include "adataexchange.h"
00052 #include "alog.h"
00053 #include "binreloc.h"
00054
00055 void dSelectDB::init()
00056 {
00057 aLog::init();
00058 createMenu();
00059 progressBar1->hide();
00060 dbPath->setText("");
00061 QString fname;
00062 withgroups = 1;
00063 settings.insertSearchPath( QSettings::Unix, QString(QDir::homeDirPath())+QString("/.ananas"));
00064 settings.insertSearchPath( QSettings::Windows, "/ananasgroup/ananas" );
00065
00066 local = settings.entryList("/groups").count();
00067 changes = false;
00068 if(!local)
00069 {
00070
00071 aLog::print(aLog::MT_DEBUG, tr("dSelectDB local settings not found"));
00072
00073 #ifdef Q_OS_WIN32
00074 QStringList subkeys = settings.subkeyList("/ananasgroup/ananas/globalsettings");
00075 settings.insertSearchPath(QSettings::Windows,"/ananasgroup/ananas/globalsettings");
00076 QStringList entryGroup;
00077 for(uint i=0; i<subkeys.count();i++)
00078 {
00079 QString str =subkeys[i];
00080
00081 entryGroup =settings.entryList("/"+str);
00082 settings.beginGroup("/"+str);
00083 readSettings(entryGroup);
00084 settings.endGroup();
00085 }
00086
00087 #else
00088 QString suff = "*_grouprc";
00089 BrInitError error;
00090 if (br_init_lib(&error) == 0 && error != BR_INIT_ERROR_DISABLED) {
00091 aLog::print(aLog::MT_INFO, tr("Warning: BinReloc failed to initialize (error code %1)\n").arg(error));
00092 aLog::print(aLog::MT_INFO, tr("Will fallback to hardcoded default path.\n"));
00093 }
00094 QString etcDir;
00095 if ( QString(br_find_prefix("/usr")) == "/usr" ) {
00096 etcDir= "/etc";
00097 } else {
00098 etcDir= br_find_etc_dir("/etc");
00099 }
00100 aLog::print(aLog::MT_DEBUG, tr("dSelectDB::init BinReloc path to etc dir is '%1'\n").arg( etcDir ));
00101
00102 QDir dir( etcDir + QString("/ananas"),suff,QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::Readable);
00103 QStringList subkeys = dir.entryList(suff);
00104 settings.insertSearchPath(QSettings::Unix,etcDir + QString("/ananas/"));
00105 QStringList entryGroup;
00106 for(uint i=0; i<subkeys.count();i++)
00107 {
00108 QString str =subkeys[i];
00109 str = str.left(str.length()-2);
00110 entryGroup =settings.entryList("/"+str);
00111 settings.beginGroup("/"+str);
00112 readSettings(entryGroup);
00113 settings.endGroup();
00114 }
00115 #endif
00116 }
00117 else
00118 {
00119 QStringList lst = settings.entryList("/groups");
00120 settings.beginGroup("/groups");
00121 readSettings(lst);
00122 settings.endGroup();
00123 }
00124 setIcon( rcIcon("ananas-32x32.png"));
00125 listDBRC->hideColumn( 1 );
00126 listDBRC->setSorting( -1 );
00127 listDBRC->header()->hide();
00128 listDBRC->setRootIsDecorated(1);
00129 buttonOk->setEnabled( FALSE );
00130 return;
00131 }
00132
00133 void dSelectDB::readSettings(QStringList entryGroup)
00134 {
00135 rcfile="";
00136 ro_mode=0;
00137 QFile f, fc;
00138 QString s, sp, sn, rc;
00139 rcListViewItem *item, *gitem=NULL;
00140 for(uint j=0; j<entryGroup.count();j++)
00141 {
00142 QStringList eitems;
00143 eitems = settings.entryList(entryGroup[j]);
00144 QString groupName = "unknown group";
00145 groupName = settings.readEntry(entryGroup[j]);
00146 aLog::print(aLog::MT_DEBUG, tr("dSelectDB read settings for group with name %1").arg(groupName));
00147 rcListViewItem * lastIt = (rcListViewItem *) listDBRC->lastItem();
00148 if(lastIt!=NULL)
00149 {
00150 while(lastIt->depth()>0)
00151 {
00152 lastIt =(rcListViewItem *) lastIt->parent();
00153 }
00154 gitem= new rcListViewItem(listDBRC, lastIt, groupName, "", true );
00155 gitem->setOpen(true);
00156 }
00157 else
00158 {
00159 gitem= new rcListViewItem(listDBRC, groupName, "", true );
00160 gitem->setOpen(true);
00161 }
00162
00163 QMap<QString,QString> cfg;
00164 for(int k = eitems.count()-1; k>=0; k--)
00165 {
00166 if(k<0) break;
00167 rc = settings.readEntry(entryGroup[j]+"/"+eitems[k]);
00168 cfg = aTests::readConfig(QDir::convertSeparators(rc));
00169 sn=cfg["dbtitle"];
00170 if (gitem) item= new rcListViewItem( gitem, sn, rc );
00171 else item= new rcListViewItem( listDBRC, sn, rc );
00172 }
00173 }
00174 }
00175
00176
00177 void dSelectDB::itemSelect( QListViewItem *item )
00178 {
00179 if (! item ) return;
00180 rcListViewItem *i = ( rcListViewItem *) item ;
00181 buttonOk->setEnabled( !i->group );
00182 dbPath->setText(QDir::convertSeparators(i->rcfile));
00183
00184
00185 }
00186
00187
00188 void dSelectDB::newGroup()
00189 {
00190 QListViewItem *gitem;
00191 gitem = new rcListViewItem(listDBRC, tr("New group"), "", true );
00192 listDBRC->setRootIsDecorated(1);
00193 gitem->setOpen(true);
00194 listDBRC->setSelected(gitem, true);
00195 editItem();
00196 withgroups=1;
00197 changes = true;
00198
00199 }
00200
00201
00202 void dSelectDB::newItem()
00203 {
00204 rcListViewItem *item, *gitem;
00205 QString rc;
00206 item= ( rcListViewItem *) listDBRC->selectedItem();
00207 if (!item) return;
00208 if (withgroups) {
00209 if (item->parent()) gitem = ( rcListViewItem *) item->parent();
00210 else gitem=item;
00211 item = new rcListViewItem(gitem, tr("New shema"), "myrc");
00212 gitem->setOpen(true);
00213
00214 } else {
00215 item = new rcListViewItem(listDBRC, tr("New shema"), "myrc");
00216 }
00217 listDBRC->setSelected(item, true);
00218 editItem();
00219 changes = true;
00220 }
00221
00222
00223 void dSelectDB::editItem()
00224 {
00225 rcListViewItem *item;
00226 dEditRC *d = new dEditRC(this);
00227
00228 item = ( rcListViewItem *) listDBRC->selectedItem();
00229 if (!item) return;
00230 if ( item->group )
00231 {
00232
00233 item->setRenameEnabled(0, true);
00234 item->startRename(0);
00235 changes = true;
00236 }
00237 else
00238 {
00239
00240 d->setdata( QDir::convertSeparators(item->rcfile), item );
00241 if ( d->exec() == QDialog::Accepted )
00242 {
00243 changes = true;
00244 }
00245 }
00246 }
00247
00248
00249
00250 void dSelectDB::deleteItem()
00251 {
00252 QListViewItem *item;
00253 item=listDBRC->selectedItem();
00254 if (item)
00255 {
00256 QString msg = tr("Delete item?");
00257 if(QMessageBox::question(this,tr("confirm delete"),msg,QMessageBox::Ok,QMessageBox::Cancel)!=QMessageBox::Ok)
00258 {
00259 return;
00260 }
00261
00262 delete item;
00263 if (listDBRC->childCount()==0) withgroups=0;
00264 changes = true;
00265 }
00266 }
00267
00268
00269 void dSelectDB::saveRC()
00270 {
00271 rcListViewItem *item, *gitem;
00272 QFile f( rcfile );
00273 aLog::print(aLog::MT_INFO, tr("dSelectDB save settings"));
00274 if (!local)
00275 {
00276 if(!changes) return;
00277 QString home = QDir::homeDirPath();
00278 #ifndef Q_OS_WIN32
00279 QString msg = QString("Все внесенные изменения будут сохранены локально в вашем домашнем каталоге.\nПри очередном запуске программы будут использованы локальные настройки из каталога\n%1/.ananas.\nЕсли Вы хотите использовать глобальные настройки, вам следует удалить каталог\n%2/.ananas").arg(home).arg(home);
00280 #else
00281 QString msg = tr("save settings local?");
00282 #endif
00283 if(QMessageBox::question(this,tr("save"),QString::fromUtf8(msg),QMessageBox::Ok,QMessageBox::Cancel)!=QMessageBox::Ok)
00284 {
00285 return;
00286 }
00287 }
00288 BrInitError error;
00289 if (br_init_lib(&error) == 0 && error != BR_INIT_ERROR_DISABLED) {
00290 aLog::print(aLog::MT_INFO, tr("Warning: BinReloc failed to initialize (error code %1)\n").arg(error));
00291 aLog::print(aLog::MT_INFO, tr("Will fallback to hardcoded default path.\n"));
00292 }
00293
00294 QString etcDir;
00295 if ( QString(br_find_prefix("/usr")) == "/usr" ) {
00296 etcDir= "/etc";
00297 } else {
00298 etcDir= br_find_etc_dir("/etc");
00299 }
00300 aLog::print(aLog::MT_DEBUG, tr("dSelectDB::saveRC BinReloc path to etc dir is '%1'\n").arg(etcDir) );
00301
00302 settings.removeSearchPath( QSettings::Unix, etcDir + QString("/ananas") );
00303 settings.removeSearchPath( QSettings::Windows, "/ananasgroup/ananas/globalsettings");
00304
00305
00306 gitem= ( rcListViewItem *) listDBRC->firstChild();
00307 uint gcount=0;
00308 uint ecount=0;
00309 clearSettings();
00310
00311 settings.beginGroup("/groups");
00312
00313 while (gitem)
00314 {
00315 if (withgroups)
00316 {
00317 settings.writeEntry(QString::number(gcount),gitem->text(0));
00318 if(gitem->childCount())
00319 {
00320 item = ( rcListViewItem *) gitem->firstChild();
00321 while (item)
00322 {
00323 settings.writeEntry(QString::number(gcount)+"/"+QString::number(ecount),item->rcfile);
00324 item = ( rcListViewItem *) item->nextSibling();
00325 ++ecount;
00326 }
00327 }
00328 }
00329 ++gcount;
00330 gitem = ( rcListViewItem *) gitem->nextSibling();
00331 }
00332 settings.endGroup();
00333 }
00334
00335
00336 void dSelectDB::onOK()
00337 {
00338 rcListViewItem *item;
00339
00340 saveRC();
00341 item = ( rcListViewItem *) listDBRC->selectedItem();
00342 if (item) {
00343 if ( !item->rcfile.isEmpty() ) {
00344 rcfile = item->rcfile;
00345 accept();
00346 }
00347 }
00348 }
00349
00350
00351 void dSelectDB::onCancel()
00352 {
00353
00354 reject();
00355 }
00356
00357
00358 void dSelectDB::ItemRenamed(QListViewItem *item, int col)
00359 {
00360 if (!item) return;
00361 if (col>1) return;
00362 }
00363
00364
00365 void dSelectDB::clearSettings()
00366 {
00367 QStringList eitems;
00368 QStringList entryGroup =settings.entryList("/groups");
00369 settings.beginGroup("/groups");
00370 for(uint j=0; j<entryGroup.count();j++)
00371 {
00372 eitems = settings.entryList(entryGroup[j]);
00373 settings.removeEntry(entryGroup[j]);
00374 for(int k = eitems.count()-1; k>=0; k--)
00375 {
00376 if(k<0) break;
00377 settings.removeEntry(entryGroup[j]+"/"+eitems[k]);
00378 }
00379 }
00380 settings.endGroup();
00381 }
00382
00383
00384 void dSelectDB::onHelp()
00385 {
00386 HelpCfgForm f;
00387 f.exec();
00388 }
00389
00390 void dSelectDB::createMenu()
00391 {
00392
00393 menuBar = new QMenuBar(this);
00394 QPopupMenu *menuFile = new QPopupMenu(this);
00395 menuFile->insertItem(tr("E&xit"), this, SLOT(onCancel()));
00396
00397 QPopupMenu *menuEdit = new QPopupMenu(this);
00398 menuEdit->insertItem(tr("New &group"), this, SLOT(newGroup()));
00399 menuEdit->insertItem(tr("&New shema"), this, SLOT(newItem()));
00400 menuEdit->insertSeparator();
00401 menuEdit->insertItem(tr("Edi&t"), this, SLOT(editItem()));
00402 menuEdit->insertSeparator();
00403 menuEdit->insertItem(tr("&Delete"), this, SLOT(deleteItem()));
00404
00405 QPopupMenu *menuAction = new QPopupMenu(this);
00406 menuAction->insertItem(tr("&Import"), this, SLOT(importItem()));
00407 menuAction->insertItem(tr("&Export"), this, SLOT(exportItem()));
00408
00409 menuBar->insertItem(tr("Program"), menuFile);
00410 menuBar->insertItem(tr("Actions"), menuEdit);
00411 menuBar->insertItem(tr("Service"), menuAction);
00412 layout()->setMenuBar(menuBar);
00413 menuBar->show();
00414 }
00415
00416
00417 void dSelectDB::importItem()
00418 {
00419 rcListViewItem *item,*gitem;
00420 dImportDB *d = new dImportDB(this);
00421 item = (rcListViewItem *)listDBRC->selectedItem();
00422 if (!item) return;
00423 if (withgroups)
00424 {
00425 if (item->parent()) gitem = ( rcListViewItem *) item->parent();
00426 else gitem=item;
00427 item = new rcListViewItem(gitem, tr("New shema"), "myrc");
00428 gitem->setOpen(true);
00429
00430 }
00431 else
00432 {
00433 item = new rcListViewItem(listDBRC, tr("New shema"), "myrc");
00434 }
00435 listDBRC->setSelected(item, true);
00436 d->setdata("", item );
00437 if ( d->exec() == QDialog::Accepted )
00438 {
00439 bool res;
00440 QString rcfile = item->rcfile;
00441 QString filename = d->eCfgName->text();
00442 dbPath->hide();
00443 progressBar1->show();
00444 progressBar1->reset();
00445 aBackup newBase;
00446 connect (&newBase, SIGNAL(progress(int,int)), progressBar1, SLOT( setProgress(int,int)));
00447 if(newBase.importData(rcfile, filename, true)==false)
00448 {
00449 QMessageBox::information(this,tr("Backup"),newBase.lastError(),QMessageBox::NoButton);
00450 progressBar1->hide();
00451 dbPath->show();
00452 aLog::print(aLog::MT_INFO, tr("dSelectDB business schema import"));
00453 }
00454 else
00455 {
00456 aLog::print(aLog::MT_ERROR, tr("dSelectDB restore: %1").arg(newBase.lastError()));
00457 QMessageBox::critical(this,tr("Backup"),newBase.lastError(),0, QMessageBox::NoButton);
00458 progressBar1->hide();
00459 delete item;
00460 item =0;
00461 return;
00462 }
00463
00464
00465
00466 changes = true;
00467 }
00468 else
00469 {
00470 delete item;
00471 item = 0;
00472 return;
00473 }
00474 }
00475
00476
00477 void dSelectDB::exportItem()
00478 {
00479
00480 QString dir,rcfile,filename;
00481 #ifndef Q_OS_WIN32
00482 dir = "/home";
00483 #endif
00484
00485 rcListViewItem *item = ( rcListViewItem *) listDBRC->selectedItem();
00486 if (item)
00487 {
00488 if ( !item->rcfile.isEmpty() && !item->group )
00489 {
00490 rcfile = item->rcfile;
00491 }
00492 else
00493 {
00494 QMessageBox::information(this,tr("Select item"),tr("Please, select item for export"),QMessageBox::NoButton);
00495 return;
00496 }
00497
00498 QFileDialog *fdlg = new QFileDialog(this, "fileDialog",true);
00499 fdlg->setMode ( QFileDialog::AnyFile );
00500 if(fdlg->exec()==QDialog::Accepted)
00501 {
00502 filename = fdlg->selectedFile();
00503 }
00504 else
00505 {
00506 return;
00507 }
00508 delete fdlg;
00509 fdlg = 0;
00510
00511 aBackup backupBase;
00512 dbPath->hide();
00513 progressBar1->show();
00514 progressBar1->reset();
00515 connect (&backupBase, SIGNAL(progress(int,int)), progressBar1, SLOT( setProgress(int,int)));
00516 if(backupBase.exportData(rcfile, filename, true )==0)
00517 {
00518 aLog::print(aLog::MT_INFO, tr("dSelectDB backup ok"));
00519 QMessageBox::information(this,tr("Backup"),backupBase.lastError(),QMessageBox::NoButton);
00520 progressBar1->hide();
00521 dbPath->show();
00522 }
00523 else
00524 {
00525 aLog::print(aLog::MT_DEBUG, tr("dSelectDB backup: %1").arg(backupBase.lastError()));
00526 QMessageBox::critical(this,tr("Backup"),backupBase.lastError(),0, QMessageBox::NoButton);
00527 progressBar1->hide();
00528 return;
00529 }
00530 }
00531 else
00532 {
00533 QMessageBox::information(this,tr("Select item"),tr("Please, select item for export"),QMessageBox::NoButton);
00534 return;
00535 }
00536
00537 }
00538
00539
00540 void dSelectDB::onDblClick( QListViewItem *item)
00541 {
00542
00543 if (! (( rcListViewItem *)item)->group )
00544 {
00545 onOK();
00546
00547 }
00548 }
00549
00550
00551 void dSelectDB::comboBox1_activated( int )
00552 {
00553 int id = comboBox1->currentItem();
00554 switch ( id )
00555 {
00556 case 1:
00557 newGroup();
00558 break;
00559 case 2:
00560 newItem();
00561 break;
00562 }
00563 comboBox1->setCurrentItem(0);
00564 }