kexi

importwizard.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Adam Pigg <adam@piggz.co.uk>
00003    Copyright (C) 2004-2006 Jaroslaw Staniek <js@iidea.pl>
00004    Copyright (C) 2005 Martin Ellis <martin.ellis@kdemail.net>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "importwizard.h"
00023 #include "keximigrate.h"
00024 #include "importoptionsdlg.h"
00025 
00026 #include <qlabel.h>
00027 #include <qlayout.h>
00028 #include <qvbuttongroup.h>
00029 #include <qradiobutton.h>
00030 #include <qcheckbox.h>
00031 
00032 #include <kcombobox.h>
00033 #include <kmessagebox.h>
00034 #include <kpushbutton.h>
00035 #include <kdebug.h>
00036 #include <klineedit.h>
00037 #include <kiconloader.h>
00038 #include <kbuttonbox.h>
00039 
00040 #include <kexidb/drivermanager.h>
00041 #include <kexidb/driver.h>
00042 #include <kexidb/connectiondata.h>
00043 #include <kexidb/utils.h>
00044 #include <core/kexidbconnectionset.h>
00045 #include <core/kexi.h>
00046 #include <KexiConnSelector.h>
00047 #include <KexiProjectSelector.h>
00048 #include <KexiOpenExistingFile.h>
00049 #include <KexiDBTitlePage.h>
00050 #include <kexiutils/utils.h>
00051 #include <kexidbdrivercombobox.h>
00052 #include <kexitextmsghandler.h>
00053 #include <widget/kexicharencodingcombobox.h>
00054 #include <widget/kexiprjtypeselector.h>
00055 
00056 
00057 using namespace KexiMigration;
00058 
00059 //===========================================================
00060 //
00061 ImportWizard::ImportWizard(QWidget *parent, QMap<QString,QString>* args)
00062  : KWizard(parent)
00063  , m_args(args)
00064 {
00065     setCaption(i18n("Import Database"));
00066     setIcon(DesktopIcon("database_import"));
00067     m_prjSet = 0;
00068     m_fileBasedDstWasPresented = false;
00069     m_setupFileBasedSrcNeeded = true;
00070     m_importExecuted = false;
00071     m_srcTypeCombo = 0;
00072 
00073     setMinimumSize(400, 400);
00074     parseArguments();
00075     setupIntro();
00076 //  setupSrcType();
00077     setupSrcConn();
00078     setupSrcDB();
00079     setupDstType();
00080     setupDstTitle();
00081     setupDst();
00082     setupImportType();
00083     setupImporting();
00084     setupFinish();
00085 
00086     connect(this, SIGNAL(selected(const QString &)), this, SLOT(pageSelected(const QString &)));
00087     connect(this, SIGNAL(helpClicked()), this, SLOT(helpClicked()));
00088 
00089     if (m_predefinedConnectionData) {
00090         // setup wizard for predefined server source
00091         m_srcConn->showAdvancedConn();
00092         setAppropriate( m_srcConnPage, false );
00093         setAppropriate( m_srcDBPage, false );
00094     }
00095     else if (!m_predefinedDatabaseName.isEmpty()) {
00096         // setup wizard for predefined source
00097         // (used when external project type was opened in Kexi, e.g. mdb file)
00098 //      MigrateManager manager;
00099 //      QString driverName = manager.driverForMimeType( m_predefinedMimeType );
00100 //      m_srcTypeCombo->setCurrentText( driverName );
00101 
00102 //      showPage( m_srcConnPage );
00103         m_srcConn->showSimpleConn();
00104         m_srcConn->setSelectedFileName(m_predefinedDatabaseName);
00105 
00106         //disable all prev pages except "welcome" page
00107         for (int i=0; i<indexOf(m_dstTypePage); i++) {
00108             if (page(i)!=m_introPage)
00109                 setAppropriate( page(i), false );
00110         }
00111     }
00112 
00113     m_sourceDBEncoding = QString::fromLatin1(KGlobal::locale()->encoding()); //default
00114 }
00115 
00116 //===========================================================
00117 //
00118 ImportWizard::~ImportWizard()
00119 {
00120     delete m_prjSet;
00121 }
00122 
00123 //===========================================================
00124 //
00125 void ImportWizard::parseArguments()
00126 {
00127     m_predefinedConnectionData = 0;
00128     if (!m_args)
00129         return;
00130     if (!(*m_args)["databaseName"].isEmpty() && !(*m_args)["mimeType"].isEmpty()) {
00131         m_predefinedDatabaseName = (*m_args)["databaseName"];
00132         m_predefinedMimeType = (*m_args)["mimeType"];
00133         if (m_args->contains("connectionData")) {
00134             m_predefinedConnectionData = new KexiDB::ConnectionData();
00135             KexiDB::fromMap( 
00136                 KexiUtils::deserializeMap((*m_args)["connectionData"]), *m_predefinedConnectionData 
00137             );
00138         }
00139     }
00140     m_args->clear();
00141 }
00142 
00143 //===========================================================
00144 //
00145 void ImportWizard::setupIntro()
00146 {
00147     m_introPage = new QWidget(this);
00148     QVBoxLayout *vbox = new QVBoxLayout(m_introPage, KDialog::marginHint());
00149     
00150     QLabel *lblIntro = new QLabel(m_introPage);
00151     lblIntro->setAlignment( Qt::AlignTop | Qt::AlignLeft | Qt::WordBreak );
00152     QString msg;
00153     if (m_predefinedConnectionData) { //predefined import: server source
00154         msg = i18n("<qt>Database Importing wizard is about to import \"%1\" database "
00155         "<nobr>(connection %2)</nobr> into a Kexi database.</qt>")
00156             .arg(m_predefinedDatabaseName).arg(m_predefinedConnectionData->serverInfoString());
00157     }
00158     else if (!m_predefinedDatabaseName.isEmpty()) { //predefined import: file source
00160         KMimeType::Ptr mimeTypePtr = KMimeType::mimeType(m_predefinedMimeType);
00161         msg = i18n("<qt>Database Importing wizard is about to import <nobr>\"%1\"</nobr> file "
00162         "of type \"%2\" into a Kexi database.</qt>")
00163             .arg(QDir::convertSeparators(m_predefinedDatabaseName)).arg(mimeTypePtr->comment());
00164     }
00165     else {
00166         msg = i18n("Database Importing wizard allows you to import an existing database "
00167             "into a Kexi database.");
00168     }
00169     lblIntro->setText(msg+"\n\n"
00170         +i18n("Click \"Next\" button to continue or \"Cancel\" button to exit this wizard."));
00171     vbox->addWidget( lblIntro );
00172     addPage(m_introPage, i18n("Welcome to the Database Importing Wizard"));
00173 }
00174 
00175 //===========================================================
00176 //
00177 /*
00178 void ImportWizard::setupSrcType()
00179 {
00180     m_srcTypePage = new QWidget(this);
00181 
00183     QVBoxLayout *vbox = new QVBoxLayout(m_srcTypePage, KDialog::marginHint());
00184 
00185     QHBoxLayout *hbox = new QHBoxLayout(vbox);
00186     QLabel *lbl = new QLabel(i18n("Source database type:")+" ", m_srcTypePage);
00187     hbox->addWidget(lbl);
00188 
00189     m_srcTypeCombo = new KComboBox(m_srcTypePage);
00190     hbox->addWidget(m_srcTypeCombo);
00191     hbox->addStretch(1);
00192     vbox->addStretch(1);
00193     lbl->setBuddy(m_srcTypeCombo);
00194 
00195     MigrateManager manager;
00196     QStringList names = manager.driverNames();
00197 
00198     m_srcTypeCombo->insertStringList(names);
00199     addPage(m_srcTypePage, i18n("Select Source Database Type"));
00200 }
00201 */
00202 //===========================================================
00203 //
00204 void ImportWizard::setupSrcConn()
00205 {
00206     m_srcConnPage = new QWidget(this);
00207     QVBoxLayout *vbox = new QVBoxLayout(m_srcConnPage, KDialog::marginHint());
00208 
00209     m_srcConn = new KexiConnSelectorWidget(Kexi::connset(), 
00210         ":ProjectMigrationSourceDir", m_srcConnPage, "m_srcConnSelector");
00211 
00212     m_srcConn->hideConnectonIcon();
00213     m_srcConn->showSimpleConn();
00214 
00215     QStringList excludedFilters;
00217     excludedFilters += KexiDB::Driver::defaultFileBasedDriverMimeType();
00218     excludedFilters += "application/x-kexiproject-shortcut";
00219     excludedFilters += "application/x-kexi-connectiondata";
00220     m_srcConn->m_fileDlg->setExcludedFilters(excludedFilters);
00221 
00222 //  m_srcConn->hideHelpers();
00223     vbox->addWidget(m_srcConn);
00224     addPage(m_srcConnPage, i18n("Select Location for Source Database"));
00225 }
00226 
00227 //===========================================================
00228 //
00229 void ImportWizard::setupSrcDB()
00230 {
00231 //  arrivesrcdbPage creates widgets on that page
00232     m_srcDBPage = new QWidget(this);
00233     m_srcDBName = NULL;
00234     addPage(m_srcDBPage, i18n("Select Source Database"));
00235 }
00236 
00237 //===========================================================
00238 //
00239 void ImportWizard::setupDstType()
00240 {
00241     m_dstTypePage = new QWidget(this);
00242 
00243     KexiDB::DriverManager manager;
00244     KexiDB::Driver::InfoMap drvs = manager.driversInfo();
00245 
00246     QVBoxLayout *vbox = new QVBoxLayout(m_dstTypePage, KDialog::marginHint());
00247 
00248     QHBoxLayout *hbox = new QHBoxLayout(vbox);
00249     QLabel *lbl = new QLabel(i18n("Destination database type:")+" ", m_dstTypePage);
00250     lbl->setAlignment(Qt::AlignAuto|Qt::AlignTop);
00251     hbox->addWidget(lbl);
00252 
00253     m_dstPrjTypeSelector = new KexiPrjTypeSelector(m_dstTypePage);
00254     hbox->addWidget(m_dstPrjTypeSelector);
00255     m_dstPrjTypeSelector->option_file->setText(i18n("Database project stored in a file"));
00256     m_dstPrjTypeSelector->option_server->setText(i18n("Database project stored on a server"));
00257 
00258     QVBoxLayout *frame_server_vbox = new QVBoxLayout(m_dstPrjTypeSelector->frame_server, KDialog::spacingHint());
00259     m_dstServerTypeCombo = new KexiDBDriverComboBox(m_dstPrjTypeSelector->frame_server, drvs, 
00260         KexiDBDriverComboBox::ShowServerDrivers);
00261     frame_server_vbox->addWidget(m_dstServerTypeCombo);
00262     hbox->addStretch(1);
00263     vbox->addStretch(1);
00264     lbl->setBuddy(m_dstServerTypeCombo);
00265 
00267     //m_dstTypeCombo->setCurrentText("SQLite3");
00268     addPage(m_dstTypePage, i18n("Select Destination Database Type"));
00269 }
00270 
00271 //===========================================================
00272 //
00273 void ImportWizard::setupDstTitle()
00274 {
00275     m_dstTitlePage = new KexiDBTitlePage(i18n("Destination project's caption:"), 
00276         this, "KexiDBTitlePage");
00277     m_dstTitlePage->layout()->setMargin( KDialog::marginHint() );
00278     m_dstTitlePage->updateGeometry();
00279     m_dstNewDBNameLineEdit = m_dstTitlePage->le_caption;
00280     addPage(m_dstTitlePage, i18n("Select Destination Database Project's Caption"));
00281 }
00282 
00283 //===========================================================
00284 //
00285 void ImportWizard::setupDst()
00286 {
00287     m_dstPage = new QWidget(this);
00288     QVBoxLayout *vbox = new QVBoxLayout(m_dstPage, KDialog::marginHint());
00289 
00290     m_dstConn = new KexiConnSelectorWidget(Kexi::connset(), 
00291         ":ProjectMigrationDestinationDir", m_dstPage, "m_dstConnSelector");
00292     m_dstConn->hideHelpers();
00293     //me: Can't connect m_dstConn->m_fileDlg here, it doesn't exist yet
00294     //connect(this, SLOT(next()), m_dstConn->m_fileDlg, SIGNAL(accepted()));
00295 
00296     vbox->addWidget( m_dstConn );
00297     connect(m_dstConn,SIGNAL(connectionItemExecuted(ConnectionDataLVItem*)),
00298         this,SLOT(next()));
00299 
00300 //  m_dstConn->hideHelpers();
00301     m_dstConn->showSimpleConn();
00302     //anyway, db files will be _saved_
00303     m_dstConn->m_fileDlg->setMode( KexiStartupFileDialog::SavingFileBasedDB );
00304 //  m_dstConn->hideHelpers();
00305 //  m_dstConn->m_file->btn_advanced->hide();
00306 //  m_dstConn->m_file->label->hide();
00307 //  m_dstConn->m_file->lbl->hide();
00308     //m_dstConn->m_file->spacer7->hide();
00309 
00310 
00311     //js dstNewDBName = new KLineEdit(dstControls);
00312     //   dstNewDBName->setText(i18n("Enter new database name here"));
00313     addPage(m_dstPage, i18n("Select Location for Destination Database"));
00314 }
00315 
00316 //===========================================================
00317 //
00318 void ImportWizard::setupImportType()
00319 {
00320     m_importTypePage = new QWidget(this);
00321     QVBoxLayout *vbox = new QVBoxLayout(m_importTypePage, KDialog::marginHint());
00322     m_importTypeButtonGroup = new QVButtonGroup(m_importTypePage);
00323     m_importTypeButtonGroup->setLineWidth(0);
00324     vbox->addWidget( m_importTypeButtonGroup );
00325 
00326     (void)new QRadioButton(i18n("Structure and data"), m_importTypeButtonGroup);
00327     (void)new QRadioButton(i18n("Structure only"), m_importTypeButtonGroup);
00328 
00329     m_importTypeButtonGroup->setExclusive( true );
00330     m_importTypeButtonGroup->setButton( 0 );
00331     addPage(m_importTypePage, i18n("Select Type of Import"));
00332 }
00333 
00334 //===========================================================
00335 //
00336 void ImportWizard::setupImporting()
00337 {
00338     m_importingPage = new QWidget(this);
00339     m_importingPage->hide();
00340     QVBoxLayout *vbox = new QVBoxLayout(m_importingPage, KDialog::marginHint());
00341     m_lblImportingTxt = new QLabel(m_importingPage);
00342     m_lblImportingTxt->setAlignment( Qt::AlignTop | Qt::AlignLeft | Qt::WordBreak );
00343 
00344     m_lblImportingErrTxt = new QLabel(m_importingPage);
00345     m_lblImportingErrTxt->setAlignment( Qt::AlignTop | Qt::AlignLeft | Qt::WordBreak );
00346 
00347     m_progressBar = new KProgress(100, m_importingPage);
00348     m_progressBar->hide();
00349 
00350     vbox->addWidget( m_lblImportingTxt );
00351     vbox->addWidget( m_lblImportingErrTxt );
00352     vbox->addStretch(1);
00353 
00354     KButtonBox *optionsBox = new KButtonBox(m_importingPage);
00355     vbox->addWidget( optionsBox );
00356     m_importOptionsButton = optionsBox->addButton(i18n("Advanced Options"), this, SLOT(slotOptionsButtonClicked()));
00357     m_importOptionsButton->setIconSet(SmallIconSet("configure"));
00358     optionsBox->addStretch(1);
00359 
00360     vbox->addWidget( m_progressBar );
00361 
00362     vbox->addStretch(2);
00363 
00364     m_importingPage->show();
00365 
00366     addPage(m_importingPage, i18n("Importing"));
00367 }
00368 
00369 //===========================================================
00370 //
00371 void ImportWizard::setupFinish()
00372 {
00373     m_finishPage = new QWidget(this);
00374     m_finishPage->hide();
00375     QVBoxLayout *vbox = new QVBoxLayout(m_finishPage, KDialog::marginHint());
00376     m_finishLbl = new QLabel(m_finishPage);
00377     m_finishLbl->setAlignment( Qt::AlignTop | Qt::AlignLeft | Qt::WordBreak );
00378 
00379     vbox->addWidget( m_finishLbl );
00380     m_openImportedProjectCheckBox = new QCheckBox(i18n("Open imported project"), 
00381         m_finishPage, "openImportedProjectCheckBox");
00382     m_openImportedProjectCheckBox->setChecked(true);
00383     vbox->addSpacing( KDialog::spacingHint() );
00384     vbox->addWidget( m_openImportedProjectCheckBox );
00385     vbox->addStretch(1);
00386 
00387     addPage(m_finishPage, i18n("Success"));
00388 }
00389 
00390 //===========================================================
00391 //
00392 bool ImportWizard::checkUserInput()
00393 {
00394     QString finishtxt;
00395 
00396     if (m_dstNewDBNameLineEdit->text().isEmpty())
00397     {
00398         finishtxt = finishtxt + "<br>" + i18n("No new database name was entered.");
00399     }
00400 
00401     Kexi::ObjectStatus result;
00402     KexiMigrate* sourceDriver = prepareImport(result);
00403     if (sourceDriver && sourceDriver->isSourceAndDestinationDataSourceTheSame())
00404     {
00405         finishtxt = finishtxt + "<br>" + i18n("Source database is the same as destination.");
00406     }
00407 
00408     if (! finishtxt.isNull())
00409     {
00410         finishtxt = "<qt>" + i18n("Following problems were found with the data you entered:") +
00411                     "<br>" + finishtxt + "<br><br>" +
00412                     i18n("Please click 'Back' button and correct these errors.");
00413         m_lblImportingErrTxt->setText(finishtxt);
00414     }
00415 
00416     return finishtxt.isNull();
00417 }
00418 
00419 void ImportWizard::arriveSrcConnPage()
00420 {
00421     m_srcConnPage->hide();
00422 
00423 //  checkIfSrcTypeFileBased(m_srcTypeCombo->currentText());
00424 //  if (fileBasedSrcSelected()) {
00425 //moved     m_srcConn->showSimpleConn();
00428         if (m_setupFileBasedSrcNeeded) {
00429             m_setupFileBasedSrcNeeded = false;
00430             QStringList additionalMimeTypes;
00431     /* moved
00432             if (m_srcTypeCombo->currentText().contains("Access")) {
00434                 additionalMimeTypes << "application/x-msaccess";
00435             }*/
00436             m_srcConn->m_fileDlg->setMode(KexiStartupFileDialog::Opening);
00437             m_srcConn->m_fileDlg->setAdditionalFilters(additionalMimeTypes);
00438 /*moved         if (m_srcTypeCombo->currentText().contains("Access")) {
00440     #ifdef Q_WS_WIN
00441                 m_srcConn->m_fileDlg->setSelectedFilter("*.mdb");
00442     #else
00443                 m_srcConn->m_fileDlg->setFilter("*.mdb");
00444     #endif
00445             }*/
00446             //m_srcConn->m_file->label->hide();
00447             //m_srcConn->m_file->btn_advanced->hide();
00448             //m_srcConn->m_file->label->parentWidget()->hide();
00449         }
00450 //  } else {
00451 //      m_srcConn->showAdvancedConn();
00452 //  }
00454     m_srcConnPage->show();
00455 }
00456 
00457 void ImportWizard::arriveSrcDBPage()
00458 {
00459     if (fileBasedSrcSelected()) {
00461         //moved showPage(m_dstTypePage);
00462     }
00463     else if (!m_srcDBName) {
00464         m_srcDBPage->hide();
00465         kdDebug() << "Looks like we need a project selector widget!" << endl;
00466 
00467         KexiDB::ConnectionData* condata = m_srcConn->selectedConnectionData();
00468         if(condata) {
00469             m_prjSet = new KexiProjectSet(*condata);
00470             QVBoxLayout *vbox = new QVBoxLayout(m_srcDBPage, KDialog::marginHint());
00471             m_srcDBName = new KexiProjectSelectorWidget(m_srcDBPage,
00472                 "KexiMigrationProjectSelector", m_prjSet);
00473             vbox->addWidget( m_srcDBName );
00474             m_srcDBName->label->setText(i18n("Select source database you wish to import:"));
00475         }
00476         m_srcDBPage->show();
00477     }
00478 }
00479 
00480 void ImportWizard::arriveDstTitlePage()
00481 {
00482     if(fileBasedSrcSelected()) {
00483         QString suggestedDBName( QFileInfo(m_srcConn->selectedFileName()).fileName() );
00484         const QFileInfo fi( suggestedDBName );
00485         suggestedDBName = suggestedDBName.left(suggestedDBName.length() 
00486             - (fi.extension().length() ? (fi.extension().length()+1) : 0));
00487         m_dstNewDBNameLineEdit->setText( suggestedDBName );
00488     } else {
00489         if (m_predefinedConnectionData) {
00490             // server source db is predefined
00491             m_dstNewDBNameLineEdit->setText( m_predefinedDatabaseName );
00492         }
00493         else {
00494             if (!m_srcDBName || !m_srcDBName->selectedProjectData()) {
00495                 back(); //todo!
00496                 return;
00497             }
00498             m_dstNewDBNameLineEdit->setText( m_srcDBName->selectedProjectData()->databaseName() );
00499         }
00500     }
00501 }
00502 
00503 void ImportWizard::arriveDstPage()
00504 {
00505     m_dstPage->hide();
00506 
00507 //  checkIfDstTypeFileBased(m_dstTypeCombo->currentText());
00508     if(fileBasedDstSelected()) {
00509         m_dstConn->showSimpleConn();
00510         m_dstConn->m_fileDlg->setMode( KexiStartupFileDialog::SavingFileBasedDB );
00511         if (!m_fileBasedDstWasPresented) {
00512             //without extension - it will be added automatically
00513             m_dstConn->m_fileDlg->setLocationText(m_dstNewDBNameLineEdit->text());
00514         }
00515         m_fileBasedDstWasPresented = true;
00516     } else {
00517         m_dstConn->showAdvancedConn();
00518     }
00519     m_dstPage->show();
00520 }
00521 
00522 void ImportWizard::arriveImportingPage() {
00523 //  checkIfDstTypeFileBased(m_dstTypeCombo->currentText());
00524 /*moved if (m_fileBasedDstWasPresented) {
00525         if (!m_dstConn->m_fileDlg->checkFileName()) {
00526             back();
00527             return;
00528         }
00529     }*/
00530     m_importingPage->hide();
00531     if (checkUserInput()) {
00532         setNextEnabled(m_importingPage, true);
00533     }
00534     else {
00535         setNextEnabled(m_importingPage, false);
00536     }
00537 
00538     m_lblImportingTxt->setText(i18n(
00539                  "All required information has now "
00540                  "been gathered. Click \"Next\" button to start importing.\n\n"
00541                  "Depending on size of the database this may take some time."
00542                  /*"Note: You may be asked for extra "
00543                  "information such as field types if "
00544                  "the wizard could not automatically "
00545                  "determine this for you."*/));
00546 
00547 //todo
00548 
00549     //temp. hack for MS Access driver only
00552     bool showOptions = false;
00553     if (fileBasedSrcSelected()) {
00554         Kexi::ObjectStatus result;
00555         KexiMigrate* sourceDriver = prepareImport(result);
00556         if (sourceDriver) {
00557             showOptions = !result.error() 
00558                 && sourceDriver->propertyValue( "source_database_has_nonunicode_encoding" ).toBool();
00559             KexiMigration::Data *data = sourceDriver->data();
00560             sourceDriver->setData( 0 );
00561             delete data;
00562         }
00563     }
00564     if (showOptions)
00565         m_importOptionsButton->show();
00566     else
00567         m_importOptionsButton->hide();
00568 
00569     m_importingPage->show();
00570 }
00571 
00572 void ImportWizard::arriveFinishPage() {
00573 //  backButton()->hide();
00574 //  cancelButton()->setEnabled(false);
00575 //  m_finishLbl->setText(   m_successText.arg(m_dstNewDBNameLineEdit->text()) );
00576 }
00577 
00578 bool ImportWizard::fileBasedSrcSelected() const
00579 {
00580     if (m_predefinedConnectionData)
00581         return false;
00582 
00583 //  kdDebug() << (m_srcConn->selectedConnectionType()==KexiConnSelectorWidget::FileBased) << endl;
00584     return m_srcConn->selectedConnectionType()==KexiConnSelectorWidget::FileBased;
00585 }
00586 
00587 bool ImportWizard::fileBasedDstSelected() const
00588 {
00589 //  QString dstType(m_dstServerTypeCombo->currentText());
00590 
00591     return m_dstPrjTypeSelector->buttonGroup->selectedId() == 1;
00592 
00593 /*  if ((dstType == "PostgreSQL") || (dstType == "MySQL")) {
00594         return false;
00595     } else {
00596         return true;
00597     }*/
00598 }
00599 
00600 
00601 void ImportWizard::progressUpdated(int percent) {
00602     m_progressBar->setProgress(percent);
00603     KApplication::kApplication()->processEvents();
00604 }
00605 
00606 //===========================================================
00607 //
00608 QString ImportWizard::driverNameForSelectedSource()
00609 {
00610     if (fileBasedSrcSelected()) {
00611         KMimeType::Ptr ptr = KMimeType::findByFileContent( m_srcConn->selectedFileName() );
00612         if (!ptr || ptr.data()->name()=="application/octet-stream" || ptr.data()->name()=="text/plain") {
00613             //try by URL:
00614             ptr = KMimeType::findByURL( m_srcConn->selectedFileName() );
00615         }
00616         return ptr ? m_migrateManager.driverForMimeType( ptr.data()->name() ) : QString::null;
00617     }
00618 
00619     //server-based
00620     if (m_predefinedConnectionData) {
00621         return m_predefinedConnectionData->driverName;
00622     }
00623 
00624     return m_srcConn->selectedConnectionData() 
00625         ? m_srcConn->selectedConnectionData()->driverName : QString::null;
00626 }
00627 
00628 //===========================================================
00629 //
00630 void ImportWizard::accept()
00631 {
00632     /*moved
00633     backButton()->setEnabled(false);
00634     finishButton()->setEnabled(false);
00635 //  cancelButton()->setEnabled(false);
00636     acceptImport();
00637     backButton()->setEnabled(true);
00638     finishButton()->setEnabled(true);
00639 //  cancelButton()->setEnabled(true);
00640 */
00641     if (m_args) {
00642         if ((!fileBasedDstSelected() && !m_args->contains("destinationConnectionShortcut"))
00643             || !m_openImportedProjectCheckBox->isChecked())
00644         {
00645             //do not open dest db if used didn't want it
00646             //for server connections, destinationConnectionShortcut must be defined
00647             m_args->remove("destinationDatabaseName");
00648         }
00649     }
00650     KWizard::accept();
00651 }
00652 
00653 KexiMigrate* ImportWizard::prepareImport(Kexi::ObjectStatus& result)
00654 {
00655     KexiUtils::WaitCursor wait;
00656     
00657     // Start with a driver manager
00658     KexiDB::DriverManager manager;
00659 
00660     kdDebug() << "Creating destination driver..." << endl;
00661 
00662     // Get a driver to the destination database
00663     KexiDB::Driver *destDriver = manager.driver(
00664         m_dstConn->selectedConnectionData() ? m_dstConn->selectedConnectionData()->driverName //server based
00665          : KexiDB::Driver::defaultFileBasedDriverName()
00666         // : m_dstTypeCombo->currentText() //file based
00667     );
00668     if (!destDriver || manager.error())
00669     {
00670         result.setStatus(&manager);
00671         kdDebug() << "Manager error..." << endl;
00672         manager.debugError();
00673         result.setStatus(&manager);
00674     }
00675 
00676     // Set up destination connection data
00677     KexiDB::ConnectionData *cdata;
00678     bool cdataOwned = false;
00679     QString dbname;
00680     if (!result.error())
00681     {
00682         if (m_dstConn->selectedConnectionData())
00683         {
00684             //server-based project
00685             kdDebug() << "Server destination..." << endl;
00686             cdata = m_dstConn->selectedConnectionData();
00687             dbname = m_dstNewDBNameLineEdit->text();
00688         }
00689         else // if (m_dstTypeCombo->currentText().lower() == KexiDB::Driver::defaultFileBasedDriverName()) 
00690         {
00691             //file-based project
00692             kdDebug() << "File Destination..." << endl;
00693             cdata = new KexiDB::ConnectionData();
00694             cdataOwned = true;
00695             cdata->caption = m_dstNewDBNameLineEdit->text();
00696             cdata->driverName = KexiDB::Driver::defaultFileBasedDriverName();
00697             dbname = m_dstConn->selectedFileName();
00698             cdata->setFileName( dbname );
00699             kdDebug() << "Current file name: " << dbname << endl;
00700         }
00701 /*      else
00702         {
00703             //TODO This needs a better message
00704             //KMessageBox::error(this, 
00705             result.setStatus(i18n("No connection data is available. You did not select a destination filename."),"");
00706             //return false;
00707         } */
00708     }
00709 
00710     // Find a source (migration) driver name
00711     QString sourceDriverName;
00712     if (!result.error())
00713     {
00714         sourceDriverName = driverNameForSelectedSource();
00715         if (sourceDriverName.isEmpty())
00716             result.setStatus(i18n("No appropriate migration driver found."), 
00717                 m_migrateManager.possibleProblemsInfoMsg());
00718     }
00719 
00720     // Get a source (migration) driver
00721     KexiMigrate* sourceDriver = 0;
00722     if (!result.error())
00723     {
00724         sourceDriver = m_migrateManager.driver( sourceDriverName );
00725         if(!sourceDriver || m_migrateManager.error()) {
00726             kdDebug() << "Import migrate driver error..." << endl;
00727             result.setStatus(&m_migrateManager);
00728         }
00729     }
00730 
00731     KexiUtils::removeWaitCursor();
00732 
00733     // Set up source (migration) data required for connection
00734     if (sourceDriver && !result.error())
00735     {
00736         // Setup progress feedback for the GUI
00737         if(sourceDriver->progressSupported()) {
00738             m_progressBar->updateGeometry();
00739             disconnect(sourceDriver, SIGNAL(progressPercent(int)), 
00740                 this, SLOT(progressUpdated(int)));
00741             connect(sourceDriver, SIGNAL(progressPercent(int)),
00742                 this, SLOT(progressUpdated(int)));
00743             progressUpdated(0);
00744         }
00745 
00746         bool keepData;
00747         if (m_importTypeButtonGroup->selectedId() == 0)
00748         {
00749             kdDebug() << "Structure and data selected" << endl;
00750             keepData = true;
00751         }
00752         else if (m_importTypeButtonGroup->selectedId() == 1)
00753         {
00754             kdDebug() << "structure only selected" << endl;
00755             keepData = false;
00756         }
00757         else
00758         {
00759             kdDebug() << "Neither radio button is selected (not possible?) presume keep data" << endl;
00760             keepData = true;
00761         }
00762         
00763         KexiMigration::Data* md = new KexiMigration::Data();
00764     //  delete md->destination;
00765         md->destination = new KexiProjectData(*cdata, dbname);
00766         if(fileBasedSrcSelected()) {
00767             KexiDB::ConnectionData* conn_data = new KexiDB::ConnectionData();
00768             conn_data->setFileName(m_srcConn->selectedFileName());
00769             md->source = conn_data;
00770             md->sourceName = "";
00771         }
00772         else 
00773         {
00774             if (m_predefinedConnectionData)
00775                 md->source = m_predefinedConnectionData;
00776             else
00777                 md->source = m_srcConn->selectedConnectionData();
00778 
00779             if (!m_predefinedDatabaseName.isEmpty())
00780                 md->sourceName = m_predefinedDatabaseName;
00781             else
00782                 md->sourceName = m_srcDBName->selectedProjectData()->databaseName();
00784         }
00785         md->keepData = keepData;
00786         sourceDriver->setData(md);
00787         return sourceDriver;
00788     }
00789     return 0;
00790 }
00791 
00792 tristate ImportWizard::import()
00793 {
00794     m_importExecuted = true;
00795 
00796     Kexi::ObjectStatus result;
00797     KexiMigrate* sourceDriver = prepareImport(result);
00798 
00799     bool acceptingNeeded = false;
00800 
00801     // Perform import
00802     if (!result.error())
00803     {
00804         if (!m_sourceDBEncoding.isEmpty()) {
00805             sourceDriver->setPropertyValue( "source_database_nonunicode_encoding", 
00806                 QVariant(m_sourceDBEncoding.upper().replace(' ',"")) // "CP1250", not "cp 1250" 
00807             );
00808         }
00809 
00810         if (!sourceDriver->checkIfDestinationDatabaseOverwritingNeedsAccepting(&result, acceptingNeeded)) {
00811             kdDebug() << "Abort import cause checkIfDestinationDatabaseOverwritingNeedsAccepting returned false." << endl;
00812             return false;
00813         }
00814 
00815         kdDebug() << sourceDriver->data()->destination->databaseName() << endl;
00816         kdDebug() << "Performing import..." << endl;
00817     }
00818 
00819     if (!result.error() && acceptingNeeded) { // ok, the destination-db already exists...
00820         if (KMessageBox::Yes != KMessageBox::warningYesNo(this,
00821             "<qt>"+i18n("Database %1 already exists."
00822             "<p>Do you want to replace it with a new one?")
00823             .arg(sourceDriver->data()->destination->infoString()),
00824             0, KGuiItem(i18n("&Replace")), KGuiItem(i18n("No"))))
00825         {
00826             return cancelled;
00827         }
00828     }
00829 
00830     if (!result.error() && sourceDriver->progressSupported()) {
00831         m_progressBar->show();
00832     }
00833 
00834     if (!result.error() && sourceDriver->performImport(&result))
00835     {
00836         if (m_args) {
00837 //              if (fileBasedDstSelected()) {
00838             m_args->insert("destinationDatabaseName", 
00839                 sourceDriver->data()->destination->databaseName());
00840 //              }
00841             QString destinationConnectionShortcut( 
00842                 Kexi::connset().fileNameForConnectionData( m_dstConn->selectedConnectionData() ) );
00843             if (!destinationConnectionShortcut.isEmpty()) {
00844                 m_args->insert("destinationConnectionShortcut", destinationConnectionShortcut);
00845             }
00846         }
00847         setTitle(m_finishPage, i18n("Success"));
00848         return true;
00849     }
00850 
00851     if (result.error())
00852     {
00853         m_progressBar->setProgress(0);
00854         m_progressBar->hide();
00855 
00856         QString msg, details;
00857         KexiTextMessageHandler handler(msg, details);
00858         handler.showErrorMessage(&result);
00859 
00860         kdDebug() << msg << "\n" << details << endl;
00861         setTitle(m_finishPage, i18n("Failure"));
00862         m_finishLbl->setText(   
00863             i18n("<p>Import failed.</p>%1<p>%2</p><p>You can click \"Back\" button and try again.</p>")
00864             .arg(msg).arg(details));
00865         return false;
00866     }
00867 //  delete kexi_conn;
00868     return true;
00869 } 
00870 
00871 void ImportWizard::reject()
00872 {
00873     KWizard::reject();
00874 }
00875 
00876 //===========================================================
00877 //
00878 void ImportWizard::next()
00879 {
00880     if (currentPage() == m_srcConnPage) {
00881         if (fileBasedSrcSelected()
00882             && !QFileInfo(m_srcConn->selectedFileName()).isFile()) {
00883 
00884             KMessageBox::sorry(this,i18n("Select source database filename."));
00885             return;
00886         }
00887 
00888         if ( (! fileBasedSrcSelected()) && (! m_srcConn->selectedConnectionData()) ) {
00889             KMessageBox::sorry(this,i18n("Select source database."));
00890             return;
00891         }
00892 
00893         KexiMigrate* import = m_migrateManager.driver( driverNameForSelectedSource() );
00894         if(!import || m_migrateManager.error()) {
00895             QString dbname;
00896             if (fileBasedSrcSelected())
00897                 dbname = m_srcConn->selectedFileName();
00898             else
00899                 dbname = m_srcConn->selectedConnectionData() 
00900                     ? m_srcConn->selectedConnectionData()->serverInfoString() : QString::null;
00901                 if (!dbname.isEmpty())
00902                     dbname = QString(" \"%1\"").arg(dbname);
00903             KMessageBox::error(this, i18n("Could not import database%1. This type is not supported.")
00904                 .arg(dbname));
00905             return;
00906         }
00907     }
00908     else if (currentPage() == m_dstPage) {
00909         if (m_fileBasedDstWasPresented) {
00910             if (fileBasedDstSelected() && !m_dstConn->m_fileDlg->checkFileName())
00911                 return;
00912         }
00913     }
00914     else if (currentPage() == m_importingPage) {
00915         if (!m_importExecuted) {
00916             m_importOptionsButton->hide();
00917             nextButton()->setEnabled(false);
00918             finishButton()->setEnabled(false);
00919             backButton()->setEnabled(false);
00920             m_lblImportingTxt->setText(i18n("Importing in progress..."));
00921             tristate res = import();
00922             if (true == res) {
00923                 m_finishLbl->setText( 
00924                     i18n("Database has been imported into Kexi database project \"%1\".")
00925                     .arg(m_dstNewDBNameLineEdit->text()) );
00926                 cancelButton()->setEnabled(false);
00927                 setBackEnabled(m_finishPage, false);
00928                 setFinishEnabled(m_finishPage, true);
00929                 m_openImportedProjectCheckBox->show();
00930                 next();
00931                 return;
00932             }
00933 
00934             m_progressBar->hide();
00935             cancelButton()->setEnabled(true);
00936             setBackEnabled(m_finishPage, true);
00937             setFinishEnabled(m_finishPage, false);
00938             m_openImportedProjectCheckBox->hide();
00939             if (!res)
00940                 next();
00941             else if (~res) {
00942                 arriveImportingPage();
00943     //          back();
00944             }
00945             m_importExecuted = false;
00946             return;
00947         }
00948     }
00949 
00950     setAppropriate( m_srcDBPage, !fileBasedSrcSelected() && !m_predefinedConnectionData ); //skip m_srcDBPage
00951     KWizard::next();
00952 }
00953 
00954 void ImportWizard::back()
00955 {
00956     setAppropriate( m_srcDBPage, !fileBasedSrcSelected() && !m_predefinedConnectionData ); //skip m_srcDBPage
00957     KWizard::back();
00958 }
00959 
00960 void ImportWizard::pageSelected(const QString &)
00961 {
00962     if (currentPage() == m_introPage) {
00963     }
00964 //  else if (currentPage() == m_srcTypePage) {
00965 //  }
00966     else if (currentPage() == m_srcConnPage) {
00967         arriveSrcConnPage();
00968     }
00969     else if (currentPage() == m_srcDBPage) {
00970         arriveSrcDBPage();
00971     }
00972     else if (currentPage() == m_dstTypePage) {
00973     }
00974     else if (currentPage() == m_dstTitlePage) {
00975         arriveDstTitlePage();
00976     }
00977     else if (currentPage() == m_dstPage) {
00978         arriveDstPage();
00979     }
00980     else if (currentPage() == m_importingPage) {
00981         arriveImportingPage();
00982     }
00983     else if (currentPage() == m_finishPage) {
00984         arriveFinishPage();
00985     }
00986 }
00987 
00988 void ImportWizard::helpClicked()
00989 {
00990     if (currentPage() == m_introPage)
00991     {
00992         KMessageBox::information(this, i18n("No help is available for this page."), i18n("Help"));
00993     }
00994 /*  else if (currentPage() == m_srcTypePage)
00995     {
00996         KMessageBox::information(this, i18n("Here you can choose the type of data to import data from."), i18n("Help"));
00997     }*/
00998     else if (currentPage() == m_srcConnPage)
00999     {
01000         KMessageBox::information(this, i18n("Here you can choose the location to import data from."), i18n("Help"));
01001     }
01002     else if (currentPage() == m_srcDBPage)
01003     {
01004         KMessageBox::information(this, i18n("Here you can choose the actual database to import data from."), i18n("Help"));
01005     }
01006     else if (currentPage() == m_dstTypePage)
01007     {
01008         KMessageBox::information(this, i18n("Here you can choose the location to save the data."), i18n("Help"));
01009     }
01010     else if (currentPage() == m_dstPage)
01011     {
01012         KMessageBox::information(this, i18n("Here you can choose the location to save the data in and the new database name."), i18n("Help"));
01013     }
01014     else if (currentPage() == m_finishPage || currentPage() == m_importingPage)
01015     {
01016         KMessageBox::information(this, i18n("No help is available for this page."), i18n("Help"));
01017     }
01018 }
01019 
01020 void ImportWizard::slotOptionsButtonClicked()
01021 {
01022     OptionsDialog dlg(m_srcConn->selectedFileName(), m_sourceDBEncoding, this);
01023     if (QDialog::Accepted != dlg.exec())
01024         return;
01025 
01026     if (m_sourceDBEncoding != dlg.encodingComboBox()->selectedEncoding()) {
01027         m_sourceDBEncoding = dlg.encodingComboBox()->selectedEncoding();
01028     }
01029 }
01030 
01031 #include "importwizard.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys