00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "KexiStartupDialog.h"
00021 #include "KexiStartupDialogTemplatesPage.h"
00022 #include "kexi.h"
00023 #include "KexiProjectSelector.h"
00024 #include "KexiOpenExistingFile.h"
00025 #include "KexiConnSelector.h"
00026 #include "KexiConnSelectorBase.h"
00027
00028 #include <qlayout.h>
00029 #include <qtabwidget.h>
00030 #include <qcombobox.h>
00031 #include <qcheckbox.h>
00032 #include <qpoint.h>
00033 #include <qobjectlist.h>
00034 #include <qvgroupbox.h>
00035 #include <qapplication.h>
00036 #include <qtooltip.h>
00037 #include <qwidgetstack.h>
00038
00039 #include <klocale.h>
00040 #include <kdeversion.h>
00041 #include <kinstance.h>
00042 #include <kdebug.h>
00043 #include <kpushbutton.h>
00044 #include <kjanuswidget.h>
00045 #include <kglobalsettings.h>
00046 #include <ktextedit.h>
00047 #include <kfileiconview.h>
00048 #include <kfileitem.h>
00049 #include <kmessagebox.h>
00050 #include <kapplication.h>
00051 #include <kmimetype.h>
00052 #include <ktextbrowser.h>
00053 #include <kconfig.h>
00054
00055 #ifdef KEXI_SHOW_UNIMPLEMENTED
00056 #define KEXI_STARTUP_SHOW_TEMPLATES
00057 #define KEXI_STARTUP_SHOW_RECENT
00058 #endif
00059
00061 class KexiStartupDialogPrivate {
00062 public:
00063 KexiStartupDialogPrivate()
00064 : pageTemplates(0), pageOpenExisting(0), pageOpenRecent(0)
00065 , pageTemplatesID(-1)
00066 , pageOpenExistingID(-1)
00067 , pageOpenRecentID(-1)
00068 {
00069 result = 0;
00070 QString none, iconname;
00071 iconname = KMimeType::mimeType( KexiDB::Driver::defaultFileBasedDriverMimeType() )->icon(none,0);
00072 kexi_sqlite_icon = KGlobal::iconLoader()->loadIcon( iconname, KIcon::Desktop );
00073 iconname = KMimeType::mimeType("application/x-kexiproject-shortcut")->icon(none,0);
00074 kexi_shortcut_icon = KGlobal::iconLoader()->loadIcon( iconname, KIcon::Desktop );
00075 prj_selector = 0;
00076 chkDoNotShow = 0;
00077 openExistingConnWidget = 0;
00078 templatesWidget = 0;
00079 templatesWidget_IconListBox = 0;
00080 }
00081 ~KexiStartupDialogPrivate()
00082 {}
00083
00084 int dialogType, dialogOptions;
00085
00086 QFrame *pageTemplates, *pageOpenExisting, *pageOpenRecent;
00087 int pageTemplatesID;
00088 int pageOpenExistingID, pageOpenRecentID;
00089 int templatesSectionID_blank, templatesSectionID_import;
00090 #ifdef DB_TEMPLATES
00091 int templatesSectionID_templates;
00092 #endif
00093 QCheckBox *chkDoNotShow;
00094
00095
00096 KJanusWidget* templatesWidget;
00097 QObject *templatesWidget_IconListBox;
00098
00099 QWidgetStack *viewBlankTempl;
00100 KexiStartupDialogTemplatesPage *viewTemplates;
00101
00102
00103 int result;
00104
00105 QPixmap kexi_sqlite_icon, kexi_shortcut_icon;
00106
00107
00108
00109
00111 KexiDBConnectionSet *connSet;
00112 KexiStartupFileDialog *openExistingFileDlg;
00113 KexiConnSelectorWidget *openExistingConnWidget;
00114 QString existingFileToOpen;
00115 KexiDB::ConnectionData* selectedExistingConnection;
00116
00118 KexiProjectSet *recentProjects;
00119 KexiProjectSelectorWidget* prj_selector;
00120
00122 bool singlePage : 1;
00123 };
00124
00125 bool dlgSinglePage(int type)
00126 {
00127 return (type==KexiStartupDialog::Templates)
00128 || (type==KexiStartupDialog::OpenExisting)
00129 || (type==KexiStartupDialog::OpenRecent);
00130 }
00131
00132 QString captionForDialogType(int type)
00133 {
00134 if (type==KexiStartupDialog::Templates)
00135 return i18n("Create Project");
00136 else if (type==KexiStartupDialog::OpenExisting)
00137 return i18n("Open Existing Project");
00138 else if (type==KexiStartupDialog::OpenRecent)
00139 return i18n("Open Recent Project");
00140
00141 return i18n("Choose Project");
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 KexiStartupDialog::KexiStartupDialog(
00153 int dialogType, int dialogOptions,
00154 KexiDBConnectionSet& connSet, KexiProjectSet& recentProjects,
00155 QWidget *parent, const char *name )
00156 : KDialogBase(
00157 dlgSinglePage(dialogType) ? Plain : Tabbed
00158 ,captionForDialogType(dialogType)
00159 ,Help | Ok | Cancel, Ok, parent, name )
00160 , d(new KexiStartupDialogPrivate())
00161 {
00162 d->recentProjects = &recentProjects;
00163 d->connSet = &connSet;
00164 d->dialogType = dialogType;
00165 d->dialogOptions = dialogOptions;
00166 d->singlePage = dlgSinglePage(dialogType);
00167
00168 if (dialogType==OpenExisting) {
00169 setIcon(DesktopIcon("fileopen"));
00170 } else {
00171 setIcon(d->kexi_sqlite_icon);
00172 }
00173
00174 setSizeGripEnabled(true);
00175 int id=0;
00176 if (d->dialogType & Templates) {
00177 setupPageTemplates();
00178 d->pageTemplatesID = id++;
00179 d->templatesWidget->setFocus();
00180 }
00181 if (d->dialogType & OpenExisting) {
00182 setupPageOpenExisting();
00183 d->pageOpenExistingID = id++;
00184 if (d->singlePage)
00185 d->openExistingConnWidget->setFocus();
00186 }
00187 #ifdef KEXI_STARTUP_SHOW_RECENT
00188 if (d->dialogType & OpenRecent) {
00189 setupPageOpenRecent();
00190 d->pageOpenRecentID = id++;
00191 if (d->singlePage)
00192 d->prj_selector->setFocus();
00193 }
00194 #endif
00195
00196 if (!d->singlePage) {
00197 connect(this, SIGNAL(aboutToShowPage(QWidget*)), this, SLOT(slotPageShown(QWidget*)));
00198 d->templatesWidget->setFocus();
00199 }
00200 showPage(0);
00201 adjustSize();
00202 }
00203
00204 KexiStartupDialog::~KexiStartupDialog()
00205 {
00206 delete d;
00207 }
00208
00209 bool KexiStartupDialog::shouldBeShown()
00210 {
00211 KGlobal::config()->setGroup("Startup");
00212 return KGlobal::config()->readBoolEntry("ShowStartupDialog",true);
00213 }
00214
00215 void KexiStartupDialog::show()
00216 {
00217
00218
00219 d->existingFileToOpen=QString::null;
00220 d->result=-1;
00221
00222 KDialog::centerOnScreen(this);
00223 KDialogBase::show();
00224 }
00225
00226 int KexiStartupDialog::result() const
00227 {
00228 return d->result;
00229 }
00230
00231 void KexiStartupDialog::done(int r)
00232 {
00233 if (d->result!=-1)
00234 return;
00235
00236
00237
00238
00239 if (r==QDialog::Rejected) {
00240 d->result = CancelResult;
00241 } else {
00242 const int idx = activePageIndex();
00243 if (idx == d->pageTemplatesID) {
00244 const int templateIdx = d->templatesWidget->activePageIndex();
00245 if (templateIdx == d->templatesSectionID_blank)
00246 d->result = CreateBlankResult;
00247 #ifdef DB_TEMPLATES
00248 else if (templateIdx == d->templatesSectionID_templates)
00249 d->result = CreateFromTemplateResult;
00250 #endif
00251 else if (templateIdx == d->templatesSectionID_import)
00252 d->result = ImportResult;
00253 }
00254 else if (idx == d->pageOpenExistingID) {
00255
00256 if (d->openExistingConnWidget->selectedConnectionType()==KexiConnSelectorWidget::FileBased) {
00257 if (!d->openExistingFileDlg->checkFileName())
00258 return;
00259 d->existingFileToOpen = d->openExistingFileDlg->currentFileName();
00260
00261 d->selectedExistingConnection = 0;
00262 } else {
00263 d->existingFileToOpen = QString::null;
00264 d->selectedExistingConnection
00265 = d->openExistingConnWidget->selectedConnectionData();
00266 }
00267 d->result = OpenExistingResult;
00268 }
00269 else {
00270 d->result = OpenRecentResult;
00271 }
00272 }
00273
00274
00275 KGlobal::config()->setGroup("Startup");
00276 if (d->openExistingConnWidget)
00277 KGlobal::config()->writeEntry("OpenExistingType",
00278 (d->openExistingConnWidget->selectedConnectionType() == KexiConnSelectorWidget::FileBased)
00279 ? "File" : "Server");
00280 if (d->chkDoNotShow)
00281 KGlobal::config()->writeEntry("ShowStartupDialog",!d->chkDoNotShow->isChecked());
00282
00283 KGlobal::config()->sync();
00284
00285 KDialogBase::done(r);
00286 }
00287
00288 void KexiStartupDialog::reject()
00289 {
00290
00291 KDialogBase::reject();
00292 }
00293
00294 void KexiStartupDialog::setupPageTemplates()
00295 {
00296 d->pageTemplates = addPage( i18n("&Create Project") );
00297 QVBoxLayout *lyr = new QVBoxLayout( d->pageTemplates, 0, KDialogBase::spacingHint() );
00298
00299 d->templatesWidget = new KJanusWidget(
00300 d->pageTemplates, "templatesWidget", KJanusWidget::IconList);
00301 {
00302 d->templatesWidget_IconListBox = d->templatesWidget->child(0,"KListBox");
00303 if (d->templatesWidget_IconListBox)
00304 d->templatesWidget_IconListBox->installEventFilter(this);
00305 }
00306 lyr->addWidget(d->templatesWidget);
00307 connect(d->templatesWidget, SIGNAL(aboutToShowPage(QWidget*)), this, SLOT(slotPageShown(QWidget*)));
00308
00309 if (d->dialogOptions & CheckBoxDoNotShowAgain) {
00310 d->chkDoNotShow = new QCheckBox(i18n("Don't show me this dialog again"), d->pageTemplates, "chkDoNotShow");
00311 lyr->addWidget(d->chkDoNotShow);
00312 }
00313
00314
00315 QFrame *templPageFrame;
00316 QVBoxLayout *tmplyr;
00317 int itemID = 0;
00318
00319
00320 d->templatesSectionID_blank = itemID++;
00321 QString clickMsg( "\n\n" + i18n("Click \"OK\" button to proceed.") );
00322 templPageFrame = d->templatesWidget->addPage(
00323 i18n("Blank Database"), i18n("New Blank Database Project"), DesktopIcon("empty") );
00324 tmplyr = new QVBoxLayout(templPageFrame, 0, KDialogBase::spacingHint());
00325 QLabel *lbl_blank = new QLabel(
00326 i18n("Kexi will create a new blank database project.")+clickMsg, templPageFrame );
00327 lbl_blank->setAlignment(Qt::AlignAuto|Qt::AlignTop|Qt::WordBreak);
00328 lbl_blank->setMargin(0);
00329 tmplyr->addWidget( lbl_blank );
00330 tmplyr->addStretch(1);
00331
00332 #ifdef DB_TEMPLATES
00333
00334 d->templatesSectionID_templates = itemID++;
00335 QString none;
00336 QString kexi_sqlite_icon_name
00337 = KMimeType::mimeType( KexiDB::Driver::defaultFileBasedDriverMimeType() )->icon(none,0);
00338 templPageFrame = d->templatesWidget->addPage (
00339 i18n("Keep this text narrow: split to multiple rows if needed", "Create From\nTemplate"),
00340 i18n("New Database Project From Template"), DesktopIcon(kexi_sqlite_icon_name) );
00341 tmplyr = new QVBoxLayout(templPageFrame, 0, KDialogBase::spacingHint());
00342 QLabel *lbl_templ = new QLabel(
00343 i18n("Kexi will create a new database project using selected template.\n"
00344 "Select template and click \"OK\" button to proceed."), templPageFrame );
00345 lbl_templ->setAlignment(Qt::AlignAuto|Qt::AlignTop|Qt::WordBreak);
00346 lbl_templ->setMargin(0);
00347 tmplyr->addWidget( lbl_templ );
00348
00349 d->viewTemplates = new KexiStartupDialogTemplatesPage( templPageFrame );
00350 tmplyr->addWidget( d->viewTemplates );
00351 connect(d->viewTemplates,SIGNAL(selected(const QString&)),
00352 this,SLOT(templateSelected(const QString&)));
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381 #endif //DB_TEMPLATES
00382
00383
00384 d->templatesSectionID_import = itemID++;
00385 templPageFrame = d->templatesWidget->addPage(
00386 i18n("Import Existing\nDatabase"), i18n("Import Existing Database as New Database Project"),
00387 DesktopIcon("database_import") );
00388 tmplyr = new QVBoxLayout(templPageFrame, 0, KDialogBase::spacingHint());
00389 QLabel *lbl_import = new QLabel(
00390 i18n("Kexi will import the structure and data of an existing database as a new database project.")
00391 +clickMsg, templPageFrame );
00392 lbl_import->setAlignment(Qt::AlignAuto|Qt::AlignTop|Qt::WordBreak);
00393 lbl_import->setMargin(0);
00394 tmplyr->addWidget( lbl_import );
00395 tmplyr->addStretch(1);
00396 }
00397
00398 void KexiStartupDialog::slotPageShown(QWidget *page)
00399 {
00400 int idx = d->templatesWidget->pageIndex(page);
00401
00402 if (idx==d->templatesSectionID_blank) {
00403
00404 }
00405 else if (idx==d->templatesSectionID_import) {
00406 }
00407 #ifdef DB_TEMPLATES
00408 else if (idx==d->templatesSectionID_templates) {
00409 d->viewTemplates->populate();
00410 }
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434 #endif
00435 updateDialogOKButton(d->pageTemplates);
00436 }
00437
00438 #if 0
00439 void KexiStartupDialog::templateItemSelected(QIconViewItem *)
00440 {
00441 updateDialogOKButton(d->pageTemplates);
00442 }
00443
00444 void KexiStartupDialog::templateItemExecuted(QIconViewItem *item)
00445 {
00446 if (!item)
00447 return;
00448
00449 #ifdef DB_TEMPLATES
00450 accept();
00451 #endif
00452 }
00453
00454 void KexiStartupDialog::updateSelectedTemplateKeyInfo()
00455 {
00456 if (activePageIndex()!=d->pageTemplatesID) {
00457 d->selectedTemplateKey=QString::null;
00458 return;
00459 }
00460 QIconViewItem *item;
00461 if (d->templatesWidget->activePageIndex()==d->templatesSectionID_blank) {
00462 d->selectedTemplateKey = "blank";
00463 }
00464 else if (d->templatesWidget->activePageIndex()==d->templatesSectionID_import) {
00465 d->selectedTemplateKey = "import";
00466 }
00467 #ifdef DB_TEMPLATES
00468 else if (d->templatesWidget->activePageIndex()==d->templatesSectionID_templates) {
00469 item = d->viewTemplates->templates->currentItem();
00470 if (!item) {
00471 d->selectedTemplateKey=QString::null;
00472 return;
00473 }
00474 d->selectedTemplateKey=QString("personal/")+static_cast<TemplateItem*>(item)->key;
00475 }
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485 #endif
00486 }
00487 #endif // 0
00488
00489 void KexiStartupDialog::tabShown(QWidget *w)
00490 {
00491
00492
00493 updateDialogOKButton(w);
00494
00495 if (w==d->pageOpenExisting) {
00496 d->openExistingConnWidget->setFocus();
00497 }
00498 }
00499
00500 void KexiStartupDialog::updateDialogOKButton(QWidget *w)
00501 {
00502 if (!w) {
00503 int idx = activePageIndex();
00504 if (idx==d->pageTemplatesID)
00505 w = d->pageTemplates;
00506 else if (idx==d->pageOpenExistingID)
00507 w = d->pageOpenExisting;
00508 else if (idx==d->pageOpenRecentID)
00509 w = d->pageOpenRecent;
00510
00511 if (!w)
00512 return;
00513 }
00514 bool enable = true;
00515 if (w==d->pageTemplates) {
00516 int t_id = d->templatesWidget->activePageIndex();
00517 #ifdef DB_TEMPLATES
00518 enable = (t_id==d->templatesSectionID_blank || d->templatesSectionID_import
00519 || (t_id==d->templatesSectionID_templates && !d->viewTemplates->selectedFileName().isEmpty()));
00520 #else
00521 enable = (t_id==d->templatesSectionID_blank || d->templatesSectionID_import);
00522 #endif
00523 }
00524 else if (w==d->pageOpenExisting) {
00525
00526 enable =
00527 (d->openExistingConnWidget->selectedConnectionType()==KexiConnSelectorWidget::FileBased)
00528 ? !d->openExistingFileDlg->currentFileName().isEmpty()
00529 : (bool)d->openExistingConnWidget->selectedConnectionData();
00530 }
00531 else if (w==d->pageOpenRecent) {
00532 enable = (d->prj_selector->selectedProjectData()!=0);
00533 }
00534 enableButton(Ok,enable);
00535 }
00536
00537
00538
00539
00540
00541
00542 void KexiStartupDialog::setupPageOpenExisting()
00543 {
00544 if (d->singlePage)
00545 d->pageOpenExisting = plainPage();
00546 else
00547 d->pageOpenExisting = addPage( i18n("Open &Existing Project") );
00548 QVBoxLayout *lyr = new QVBoxLayout( d->pageOpenExisting, 0, KDialogBase::spacingHint() );
00549
00550 d->openExistingConnWidget = new KexiConnSelectorWidget(*d->connSet,
00551 ":OpenExistingOrCreateNewProject",
00552 d->pageOpenExisting, "KexiConnSelectorWidget");
00553 d->openExistingConnWidget->hideConnectonIcon();
00554 lyr->addWidget( d->openExistingConnWidget );
00555 if (KGlobal::config()->readEntry("OpenExistingType","File")=="File")
00556 d->openExistingConnWidget->showSimpleConn();
00557 else {
00558 d->openExistingConnWidget->showSimpleConn();
00559 d->openExistingConnWidget->showAdvancedConn();
00560 }
00561 d->openExistingFileDlg = d->openExistingConnWidget->m_fileDlg;
00562 connect(d->openExistingFileDlg,SIGNAL(accepted()),this,SLOT(accept()));
00563 connect(d->openExistingConnWidget,SIGNAL(connectionItemExecuted(ConnectionDataLVItem*)),
00564 this,SLOT(connectionItemForOpenExistingExecuted(ConnectionDataLVItem*)));
00565 connect(d->openExistingConnWidget,SIGNAL(connectionItemHighlighted(ConnectionDataLVItem*)),
00566 this,SLOT(connectionItemForOpenExistingHighlighted(ConnectionDataLVItem*)));
00567 }
00568
00569 void KexiStartupDialog::connectionItemForOpenExistingExecuted(ConnectionDataLVItem *item)
00570 {
00571 if (!item)
00572 return;
00573 accept();
00574 }
00575
00576 void KexiStartupDialog::connectionItemForOpenExistingHighlighted(ConnectionDataLVItem *item)
00577 {
00578 actionButton(KDialogBase::Ok)->setEnabled(item);
00579 }
00580
00581 void KexiStartupDialog::slotOk() {
00582
00583 if (activePageIndex()==d->pageOpenExistingID) {
00584 if (d->openExistingFileDlg) {
00585 if (d->openExistingFileDlg->okButton())
00586 d->openExistingFileDlg->okButton()->animateClick();
00587
00588 }
00589 }
00590 KDialogBase::slotOk();
00591 }
00592
00593 void KexiStartupDialog::showSimpleConnForOpenExisting()
00594 {
00595
00596 d->openExistingConnWidget->showSimpleConn();
00597 }
00598
00599 void KexiStartupDialog::showAdvancedConnForOpenExisting()
00600 {
00601
00602 d->openExistingConnWidget->showAdvancedConn();
00603 }
00604
00605 QString KexiStartupDialog::selectedFileName() const
00606 {
00607 if (d->result == OpenExistingResult)
00608 return d->existingFileToOpen;
00609 else if (d->result == CreateFromTemplateResult && d->viewTemplates)
00610 return d->viewTemplates->selectedFileName();
00611 else
00612 return QString::null;
00613 }
00614
00615 KexiDB::ConnectionData* KexiStartupDialog::selectedExistingConnection() const
00616 {
00617 return d->selectedExistingConnection;
00618 }
00619
00620 void KexiStartupDialog::existingFileSelected(const QString &f)
00621 {
00622 if (f.isEmpty())
00623 return;
00624 d->existingFileToOpen=f;
00625 updateDialogOKButton(d->openExistingFileDlg);
00626 }
00627
00628 void KexiStartupDialog::setupPageOpenRecent()
00629 {
00630 #ifdef KEXI_STARTUP_SHOW_RECENT
00631 d->pageOpenRecent = addPage( i18n("Open &Recent Project") );
00632 QVBoxLayout *lyr = new QVBoxLayout( d->pageOpenRecent, 0, KDialogBase::spacingHint() );
00633 lyr->addWidget( d->prj_selector = new KexiProjectSelectorWidget(
00634 d->pageOpenRecent, "prj_selector", d->recentProjects ) );
00635 connect(d->prj_selector,SIGNAL(projectExecuted(KexiProjectData*)),
00636 this,SLOT(recentProjectItemExecuted(KexiProjectData*)));
00637 #endif
00638 }
00639
00640 KexiProjectData* KexiStartupDialog::selectedProjectData() const
00641 {
00642 if (activePageIndex()==d->pageOpenRecentID) {
00643 return d->prj_selector->selectedProjectData();
00644 }
00645 return 0;
00646 }
00647
00648 void KexiStartupDialog::recentProjectItemExecuted(KexiProjectData *data)
00649 {
00650 updateDialogOKButton(d->pageOpenRecent);
00651 if (!data)
00652 return;
00653 accept();
00654 }
00655
00657 bool KexiStartupDialog::eventFilter( QObject *o, QEvent *e )
00658 {
00659 if (o==d->templatesWidget_IconListBox && d->templatesWidget_IconListBox) {
00660 if (e->type()==QEvent::KeyPress
00661 && (static_cast<QKeyEvent*>(e)->key()==Key_Enter || static_cast<QKeyEvent*>(e)->key()==Key_Return)
00662 || e->type()==QEvent::MouseButtonDblClick)
00663 {
00664 const int t_id = d->templatesWidget->activePageIndex();
00665 if (t_id==d->templatesSectionID_blank || t_id==d->templatesSectionID_import) {
00666
00667 accept();
00668 }
00669 }
00670 }
00671 return KDialogBase::eventFilter(o,e);
00672 }
00673
00674
00675 int KexiStartupDialog::activePageIndex() const
00676 {
00677 if (!d->singlePage) {
00678
00679 return KDialogBase::activePageIndex();
00680 }
00681 kdDebug() << "int KexiStartupDialog::activePageIndex() == " << 0 << endl;
00682 return 0;
00683 }
00684
00685 void KexiStartupDialog::templateSelected(const QString& fileName)
00686 {
00687 if (!fileName.isEmpty())
00688 accept();
00689 }
00690
00691 QValueList<KexiProjectData::ObjectInfo> KexiStartupDialog::autoopenObjects() const
00692 {
00693 if (d->result != CreateFromTemplateResult || !d->viewTemplates)
00694 QValueList<KexiProjectData::ObjectInfo>();
00695
00696 return d->viewTemplates->autoopenObjectsForSelectedTemplate();
00697 }
00698
00699 #include "KexiStartupDialog.moc"