kexi

KexiNewProjectWizard.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "KexiNewProjectWizard.h"
00021 
00022 #include "KexiConnSelector.h"
00023 #include "KexiConnSelectorBase.h"
00024 #include "KexiNewPrjTypeSelector.h"
00025 #include "KexiOpenExistingFile.h"
00026 #include "KexiDBTitlePage.h"
00027 #include "KexiServerDBNamePage.h"
00028 #include "KexiProjectSelector.h"
00029 #include "kexi.h"
00030 
00031 #include <kexiutils/identifier.h>
00032 #include <kexiutils/utils.h>
00033 #include <kexiguimsghandler.h>
00034 
00035 #include <kapplication.h>
00036 #include <kiconloader.h>
00037 #include <kmimetype.h>
00038 #include <klocale.h>
00039 #include <kdebug.h>
00040 #include <kconfig.h>
00041 #include <klistview.h>
00042 #include <kurlcombobox.h>
00043 #include <kmessagebox.h>
00044 #include <klineedit.h>
00045 
00046 #include <qobjectlist.h>
00047 #include <qlabel.h>
00048 #include <qpushbutton.h>
00049 #include <qlayout.h>
00050 #include <qcheckbox.h>
00051 #include <qheader.h>
00052 
00054 class KexiNewProjectWizardPrivate
00055 {
00056     public:
00057     KexiNewProjectWizardPrivate()
00058     {
00059         le_dbname_txtchanged_disable = false;
00060         le_dbname_autofill = true;
00061 //      conndata_to_show = 0;
00062 //      project_set_to_show = 0;
00063     }
00064     ~KexiNewProjectWizardPrivate()
00065     {
00066 //      delete conndata_to_show;
00067 //      delete project_set_to_show;
00068         delete msgHandler;
00069     }
00070 //  KListView *lv_types;
00071     KListViewItem *lvi_file, *lvi_server;
00072     QString chk_file_txt, chk_server_txt; 
00073 
00074     QString server_db_name_dblist_lbl_txt; 
00075 
00076     //for displaying db list of the selected conn.
00077     QGuardedPtr<KexiDB::ConnectionData> conndata_to_show;
00078     KexiProjectSet *project_set_to_show;
00079 
00080     KexiGUIMessageHandler* msgHandler;
00081 
00082     bool le_dbname_txtchanged_disable : 1;
00083     bool le_dbname_autofill : 1;
00084 };
00085 
00086 KexiNewProjectWizard::KexiNewProjectWizard(KexiDBConnectionSet& conn_set,
00087     QWidget *parent, const char *name, bool modal, WFlags f)
00088 : KWizard(parent, name, modal, f)
00089 , d(new KexiNewProjectWizardPrivate() )
00090 {
00091     d->msgHandler = new KexiGUIMessageHandler(this);
00092     setIcon( DesktopIcon("filenew") );
00093     setCaption( i18n("Creating New Project") );
00094     finishButton()->setText(i18n("Create"));
00095 
00096     //page: type selector
00097     m_prjtype_sel = new KexiNewPrjTypeSelector(this, "KexiNewPrjTypeSelector");
00098 //  lv_types = new KListView(m_prjtype_sel, "types listview");
00099 //  m_prjtype_sel->lv_types->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum, 0, 2));
00100 #if KDE_IS_VERSION(3,3,9)
00101     m_prjtype_sel->lv_types->setShadeSortColumn(false);
00102 #endif
00103     m_prjtype_sel->lv_types->header()->hide();
00104     m_prjtype_sel->lv_types->setSorting(-1);
00105     m_prjtype_sel->lv_types->setAlternateBackground(QColor()); //disable altering
00106     m_prjtype_sel->lv_types->setItemMargin( KDialogBase::marginHint() );
00107     QString none;
00108     d->lvi_file = new KListViewItem( m_prjtype_sel->lv_types, i18n("New Project Stored in File") );
00109     d->lvi_file->setPixmap(0, 
00110         KGlobal::iconLoader()->loadIcon( KMimeType::mimeType( 
00111             KexiDB::Driver::defaultFileBasedDriverMimeType() )->icon(none,0), KIcon::Desktop 
00112         )
00113     );
00114     d->lvi_file->setMultiLinesEnabled( true );
00115     d->lvi_server = new KListViewItem( m_prjtype_sel->lv_types, d->lvi_file, 
00116         i18n("New Project Stored on Database Server") );
00117     d->lvi_server->setPixmap(0, DesktopIcon("network") );
00118     d->lvi_server->setMultiLinesEnabled( true );
00119 //  m_prjtype_sel->lv_types->resize(d->m_prjtype_sel->lv_types->width(), d->lvi_file->height()*3);
00120     m_prjtype_sel->lv_types->setFocus();
00121 //  QString txt_dns = i18n("Don't show me this question again.");
00122 //  d->chk_file_txt = m_prjtype_sel->chk_always->text() +"\n"+txt_dns;
00123 //  d->chk_server_txt = i18n("Always &use database server for creating new projects.")
00124 //      +"\n"+txt_dns;
00125 
00126     connect(m_prjtype_sel->lv_types,SIGNAL(executed(QListViewItem*)),this,SLOT(slotLvTypesExecuted(QListViewItem*)));
00127     connect(m_prjtype_sel->lv_types,SIGNAL(returnPressed(QListViewItem*)),this,SLOT(slotLvTypesExecuted(QListViewItem*)));
00128     connect(m_prjtype_sel->lv_types,SIGNAL(selectionChanged( QListViewItem*)),this,SLOT(slotLvTypesSelected(QListViewItem*)));
00129 
00130 //  static_cast<QVBoxLayout*>(m_prjtype_sel->layout())->insertWidget(1,d->m_prjtype_sel->lv_types);
00131 //  static_cast<QVBoxLayout*>(m_prjtype_sel->layout())->insertStretch(3,1);
00132 //  updateGeometry();
00133 
00134     addPage(m_prjtype_sel, i18n("Select Storage Method"));
00135 //  d->m_prjtype_sel->lv_types->setMinimumHeight(QMAX(d->lvi_file->height(),d->lvi_server->height())+25);
00136 
00137     //page: db title
00138     m_db_title = new KexiDBTitlePage(QString::null, this, "KexiDBTitlePage");
00139     addPage(m_db_title, i18n("Select Project's Caption"));
00140 
00141     //page: connection selector
00142     m_conn_sel_widget = new QWidget(this);
00143     QVBoxLayout* conn_sel_lyr = new QVBoxLayout(m_conn_sel_widget);
00144     QLabel *conn_sel_label = new QLabel(i18n("Enter a new Kexi project's file name:"), m_conn_sel_widget);
00145     conn_sel_label->setAlignment(Qt::AlignAuto|Qt::AlignTop|Qt::WordBreak);
00146     conn_sel_lyr->addWidget( conn_sel_label );
00147     conn_sel_lyr->addSpacing(KDialogBase::spacingHint());
00148 
00149     m_conn_sel = new KexiConnSelectorWidget(conn_set, ":OpenExistingOrCreateNewProject", 
00150         m_conn_sel_widget, "KexiConnSelectorWidget");
00151     conn_sel_lyr->addWidget( m_conn_sel );
00152 
00153     //"Select database server connection"
00154 //  m_conn_sel->m_file->btn_advanced->hide();
00155 //  m_conn_sel->m_file->label->hide();
00156 //TODO  m_conn_sel->m_file->lbl->setText( i18n("Enter a new Kexi project's file name:") );
00157     m_conn_sel->hideHelpers();
00158 
00159     m_conn_sel->m_remote->label->setText(
00160      i18n("Select database server's connection you wish to use to create a new Kexi project. "
00161      "<p>Here you may also add, edit or remove connections from the list."));
00162 //  m_conn_sel->m_remote->label_back->hide();
00163 //  m_conn_sel->m_remote->btn_back->hide();
00164 
00165     m_conn_sel->showSimpleConn();
00166     //anyway, db files will be _saved_
00167     m_conn_sel->m_fileDlg->setMode( KexiStartupFileDialog::SavingFileBasedDB );
00168 //  m_conn_sel->m_fileDlg->setMode( KFile::LocalOnly | KFile::File );
00169 //  m_conn_sel->m_fileDlg->setOperationMode( KFileDialog::Saving );
00171 //  connect(m_conn_sel->m_fileDlg,SIGNAL(fileHighlighted(const QString&)),this,SLOT(slotFileHighlighted(const QString&)));
00172     connect(m_conn_sel->m_fileDlg,SIGNAL(accepted()),this,SLOT(accept()));
00173     m_conn_sel->showAdvancedConn();
00174     connect(m_conn_sel,SIGNAL(connectionItemExecuted(ConnectionDataLVItem*)),
00175         this,SLOT(next()));
00176 
00177     addPage(m_conn_sel_widget, i18n("Select Project's Location"));
00178 
00179     //page: server db name
00180     m_server_db_name = new KexiServerDBNamePage(this, "KexiServerDBNamePage");
00181     d->server_db_name_dblist_lbl_txt = i18n("Existing project databases on <b>%1</b> database server:");
00182     connect(m_server_db_name->le_caption, SIGNAL(textChanged(const QString&)),
00183         this,SLOT(slotServerDBCaptionTxtChanged(const QString&)));
00184     connect(m_server_db_name->le_dbname, SIGNAL(textChanged(const QString&)),
00185         this,SLOT(slotServerDBNameTxtChanged(const QString&)));
00186     connect(m_server_db_name->le_caption, SIGNAL(returnPressed()),
00187         this,SLOT(accept()));
00188     connect(m_server_db_name->le_dbname, SIGNAL(returnPressed()),
00189         this,SLOT(accept()));
00190     m_server_db_name->le_caption->setText(i18n("New database"));
00191     m_server_db_name->le_dbname->setValidator(new KexiUtils::IdentifierValidator(this, "id_val"));
00192     m_project_selector = new KexiProjectSelectorWidget(
00193         m_server_db_name->frm_dblist, "KexiProjectSelectorWidget", 0, false, false );
00194     GLUE_WIDGET(m_project_selector, m_server_db_name->frm_dblist);
00195     m_project_selector->setFocusPolicy(NoFocus);
00196     m_project_selector->setSelectable(false);
00197 
00198     addPage(m_server_db_name, i18n("Select Project's Caption & Database Name"));
00199 
00200     setFinishEnabled(m_prjtype_sel,false);
00201     setFinishEnabled(m_db_title,false);
00202     setFinishEnabled(m_server_db_name,true);
00203 
00204     //finish:
00205     updateGeometry();
00206 //  d->m_prjtype_sel->lv_types->setMaximumWidth(width()/2);
00207     m_prjtype_sel->lv_types->setSelected(d->lvi_file, true);
00208 
00209 #ifdef KEXI_SERVER_SUPPORT
00210 //  //get settings
00211 //  KGlobal::config()->setGroup("Startup");
00212 //  //"" means goto 1st page
00213 //  QString default_storage = KGlobal::config()->readEntry("DefaultStorageForNewProjects","");
00214 #else
00215 //  QString default_storage = "file";
00216     setBackEnabled(m_db_title, false);
00217     showPage(m_db_title);
00218 #endif
00219 /*  if (default_storage.lower()=="server") {
00220         m_prjtype_sel->lv_types->setSelected(d->lvi_server, true);
00221 //      m_prjtype_sel->chk_always->setChecked(true);
00222         m_conn_sel->showAdvancedConn();
00223         showPage(m_conn_sel);
00224     }
00225     else { //"file"
00226 //      m_prjtype_sel->chk_always->setChecked(true);
00227         showPage(m_db_title);
00228     }*/
00229 }
00230 
00231 KexiNewProjectWizard::~KexiNewProjectWizard()
00232 {
00233     delete d;
00234 }
00235 
00236 void KexiNewProjectWizard::show()
00237 {
00238     KDialog::centerOnScreen(this);
00239     KWizard::show();
00240 }
00241 
00242 void KexiNewProjectWizard::slotLvTypesExecuted(QListViewItem *)
00243 {
00244     next();
00245 }
00246 
00247 void KexiNewProjectWizard::slotLvTypesSelected(QListViewItem *item)
00248 {
00249 /*  if (item==d->lvi_file) {
00250         m_prjtype_sel->chk_always->setText(d->chk_file_txt);
00251     }
00252     else if (item==d->lvi_server) {
00253         m_prjtype_sel->chk_always->setText(d->chk_server_txt);
00254     }*/
00255     setAppropriate( m_db_title, item==d->lvi_file );
00256     setAppropriate( m_server_db_name, item==d->lvi_server );
00257 }
00258 
00259 void KexiNewProjectWizard::showPage(QWidget *page)
00260 {
00261     if (page==m_prjtype_sel) {//p 1
00262         m_prjtype_sel->lv_types->setFocus();
00263         m_prjtype_sel->lv_types->setCurrentItem(m_prjtype_sel->lv_types->currentItem());
00264     } else if (page==m_db_title) {//p 2
00265         if (m_db_title->le_caption->text().stripWhiteSpace().isEmpty())
00266             m_db_title->le_caption->setText(i18n("New database"));
00267         m_db_title->le_caption->selectAll();
00268         m_db_title->le_caption->setFocus();
00269     } else if (page==m_conn_sel_widget) {//p 3
00270         if (m_prjtype_sel->lv_types->currentItem()==d->lvi_file) {
00271             m_conn_sel->showSimpleConn();
00272             QString fn = KexiUtils::string2FileName( m_db_title->le_caption->text() );
00273             if (!fn.endsWith(".kexi"))
00274                 fn += ".kexi";
00275             m_conn_sel->m_fileDlg->setLocationText(fn);
00276             setFinishEnabled(m_conn_sel_widget,true);
00277             m_conn_sel->setFocus();
00278         }
00279         else {
00280             m_conn_sel->showAdvancedConn();
00281             setFinishEnabled(m_conn_sel_widget,false);
00282             m_conn_sel->setFocus();
00283             m_server_db_name->le_caption->selectAll();
00284         }
00285     } else if (page==m_server_db_name) {
00286         if (m_conn_sel->selectedConnectionData()
00287          && (static_cast<KexiDB::ConnectionData*>(d->conndata_to_show) != m_conn_sel->selectedConnectionData())) {
00288             m_project_selector->setProjectSet(0);
00289 //          delete d->project_set_to_show;
00290             d->conndata_to_show = 0;
00291             d->project_set_to_show = new KexiProjectSet(*m_conn_sel->selectedConnectionData(), d->msgHandler);
00292             if (d->project_set_to_show->error()) {
00293                 delete d->project_set_to_show;
00294                 d->project_set_to_show = 0;
00295                 return;
00296             }
00297             d->conndata_to_show = m_conn_sel->selectedConnectionData();
00298             //-refresh projects list
00299             m_project_selector->setProjectSet( d->project_set_to_show );
00300         }
00301     }
00302     KWizard::showPage(page);
00303 }
00304 
00305 void KexiNewProjectWizard::next()
00306 {
00307     //let's check if move to next page is allowed:
00308     if (currentPage()==m_db_title) { //pg 2
00309         if (m_db_title->le_caption->text().stripWhiteSpace().isEmpty()) {
00310             KMessageBox::information(this, i18n("Enter project caption."));
00311             m_db_title->le_caption->setText("");
00312             m_db_title->le_caption->setFocus();
00313             return;
00314         }
00315     } else if (currentPage()==m_conn_sel_widget) {//p 3
00316         if (m_prjtype_sel->lv_types->currentItem()==d->lvi_file) {
00317             //test for db file selection
00318         }
00319         else {
00320             //test for db conn selection
00321             if (!m_conn_sel->selectedConnectionData()) {
00322                 KMessageBox::information(this, i18n("Select server connection for a new project."));
00323                 return;
00324             }
00325             m_project_selector->label->setText(
00326                 d->server_db_name_dblist_lbl_txt.arg(m_conn_sel->selectedConnectionData()->serverInfoString(false)) );
00327             m_server_db_name->le_caption->setFocus();
00328 
00329         }
00330     }
00331     KWizard::next();
00332 }
00333 
00334 void KexiNewProjectWizard::accept()
00335 {
00336     if (m_prjtype_sel->lv_types->currentItem()==d->lvi_file) {//FILE:
00337         //check if new db file name is ok
00338         kdDebug() << "********** sender() " << sender()->className() << endl;
00339         if (sender()==finishButton()) { /*(only if signal does not come from filedialog)*/
00340             kdDebug() << "********** sender()==finishButton() ********" << endl;
00341 //          if (!m_conn_sel->m_fileDlg->checkURL()) {
00342             if (!m_conn_sel->m_fileDlg->checkFileName()) {
00343                 return;
00344             }
00345         }
00346     } else {//SERVER:
00347         //check if we have enough of data
00348         if (m_server_db_name->le_caption->text().stripWhiteSpace().isEmpty()) {
00349             KMessageBox::information(this, i18n("Enter project caption."));
00350             m_server_db_name->le_caption->setText("");
00351             m_server_db_name->le_caption->setFocus();
00352             return;
00353         }
00354         QString dbname = m_server_db_name->le_dbname->text().stripWhiteSpace();
00355         if (dbname.isEmpty()) {
00356             KMessageBox::information(this, i18n("Enter project's database name."));
00357             m_server_db_name->le_dbname->setText("");
00358             m_server_db_name->le_dbname->setFocus();
00359             return;
00360         }
00361         //check for duplicated dbname
00362         if (m_conn_sel->confirmOverwrites() && m_project_selector->projectSet() && m_project_selector->projectSet()
00363             ->findProject( m_server_db_name->le_dbname->text() )) {
00364             if (KMessageBox::Continue!=KMessageBox::warningContinueCancel( this, "<qt>"
00365                 +i18n("<b>A project with database name \"%1\" already exists</b>"
00366                 "<p>Do you want to delete it and create a new one?")
00367                 .arg( m_server_db_name->le_dbname->text() ), QString::null, KStdGuiItem::del(), 
00368                 QString::null, KMessageBox::Notify|KMessageBox::Dangerous ))
00369             {
00370                 m_server_db_name->le_dbname->setFocus();
00371                 return;
00372             }
00373         }
00374     }
00375 
00376     KWizard::accept();
00377 }
00378 
00379 void KexiNewProjectWizard::done(int r)
00380 {
00381 /*  //save state (always, no matter if dialog is accepted or not)
00382     KGlobal::config()->setGroup("Startup");
00383     if (!m_prjtype_sel->chk_always->isChecked())
00384         KGlobal::config()->deleteEntry("DefaultStorageForNewProjects");
00385     else if (m_prjtype_sel->lv_types->currentItem()==d->lvi_file)
00386         KGlobal::config()->writeEntry("DefaultStorageForNewProjects","File");
00387     else
00388         KGlobal::config()->writeEntry("DefaultStorageForNewProjects","Server");*/
00389 
00390     KGlobal::config()->sync();
00391     KWizard::done(r);
00392 }
00393 
00394 QString KexiNewProjectWizard::projectDBName() const
00395 {
00396     if (m_prjtype_sel->lv_types->currentItem()==d->lvi_server)
00397         return m_server_db_name->le_dbname->text();
00398     return m_conn_sel->selectedFileName();
00399 }
00400 
00401 QString KexiNewProjectWizard::projectCaption() const
00402 {
00403     if (m_prjtype_sel->lv_types->currentItem()==d->lvi_server) {
00404         return m_server_db_name->le_caption->text();
00405     }
00406     return m_db_title->le_caption->text();
00407 }
00408 
00409 KexiDB::ConnectionData* KexiNewProjectWizard::projectConnectionData() const
00410 {
00411     if (m_prjtype_sel->lv_types->currentItem()==d->lvi_file)
00412         return 0;
00413     return m_conn_sel->selectedConnectionData();
00414 }
00415 
00416 void KexiNewProjectWizard::slotServerDBCaptionTxtChanged(const QString &capt)
00417 {
00418     if (m_server_db_name->le_dbname->text().isEmpty())
00419         d->le_dbname_autofill=true;
00420     if (d->le_dbname_autofill) {
00421         d->le_dbname_txtchanged_disable = true;
00422         QString captionAsId = KexiUtils::string2Identifier(capt);
00423         m_server_db_name->le_dbname->setText(captionAsId);
00424         d->le_dbname_txtchanged_disable = false;
00425     }
00426 }
00427 
00428 void KexiNewProjectWizard::slotServerDBNameTxtChanged(const QString &)
00429 {
00430     if (d->le_dbname_txtchanged_disable)
00431         return;
00432     d->le_dbname_autofill = false;
00433 }
00434 
00437 void KexiNewProjectWizard::setConfirmOverwrites(bool set)
00438 {
00439     m_conn_sel->setConfirmOverwrites(set);
00440 }
00441 
00442 
00443 #include "KexiNewProjectWizard.moc"
00444 
KDE Home | KDE Accessibility Home | Description of Access Keys