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 <kactivelabel.h>
00039 #include <kbuttonbox.h>
00040 
00041 #include <kexidb/drivermanager.h>
00042 #include <kexidb/driver.h>
00043 #include <kexidb/connectiondata.h>
00044 #include <kexidb/utils.h>
00045 #include <core/kexidbconnectionset.h>
00046 #include <core/kexi.h>
00047 #include <KexiConnSelector.h>
00048 #include <KexiProjectSelector.h>
00049 #include <KexiOpenExistingFile.h>
00050 #include <KexiDBTitlePage.h>
00051 #include <kexiutils/utils.h>
00052 #include <kexidbdrivercombobox.h>
00053 #include <kexitextmsghandler.h>
00054 #include <widget/kexicharencodingcombobox.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     hbox->addWidget(lbl);
00251     m_dstTypeCombo = new KexiDBDriverComboBox(drvs, true, m_dstTypePage);
00252 
00253     hbox->addWidget(m_dstTypeCombo);
00254     hbox->addStretch(1);
00255     vbox->addStretch(1);
00256     lbl->setBuddy(m_dstTypeCombo);
00257 
00259     m_dstTypeCombo->setCurrentText("SQLite3");
00260     addPage(m_dstTypePage, i18n("Select Destination Database Type"));
00261 }
00262 
00263 //===========================================================
00264 //
00265 void ImportWizard::setupDstTitle()
00266 {
00267     m_dstTitlePage = new KexiDBTitlePage(i18n("Destination project's caption:"), 
00268         this, "KexiDBTitlePage");
00269     m_dstTitlePage->layout()->setMargin( KDialog::marginHint() );
00270     m_dstTitlePage->updateGeometry();
00271     m_dstNewDBNameLineEdit = m_dstTitlePage->le_caption;
00272     addPage(m_dstTitlePage, i18n("Select Destination Database Project's Caption"));
00273 }
00274 
00275 //===========================================================
00276 //
00277 void ImportWizard::setupDst()
00278 {
00279     m_dstPage = new QWidget(this);
00280     QVBoxLayout *vbox = new QVBoxLayout(m_dstPage, KDialog::marginHint());
00281 
00282     m_dstConn = new KexiConnSelectorWidget(Kexi::connset(), 
00283         ":ProjectMigrationDestinationDir", m_dstPage, "m_dstConnSelector");
00284     m_dstConn->hideHelpers();
00285     //me: Can't connect m_dstConn->m_fileDlg here, it doesn't exist yet
00286     //connect(this, SLOT(next()), m_dstConn->m_fileDlg, SIGNAL(accepted()));
00287 
00288     vbox->addWidget( m_dstConn );
00289     connect(m_dstConn,SIGNAL(connectionItemExecuted(ConnectionDataLVItem*)),
00290         this,SLOT(next()));
00291 
00292 //  m_dstConn->hideHelpers();
00293     m_dstConn->showSimpleConn();
00294     //anyway, db files will be _saved_
00295     m_dstConn->m_fileDlg->setMode( KexiStartupFileDialog::SavingFileBasedDB );
00296 //  m_dstConn->hideHelpers();
00297 //  m_dstConn->m_file->btn_advanced->hide();
00298 //  m_dstConn->m_file->label->hide();
00299 //  m_dstConn->m_file->lbl->hide();
00300     //m_dstConn->m_file->spacer7->hide();
00301 
00302 
00303     //js dstNewDBName = new KLineEdit(dstControls);
00304     //   dstNewDBName->setText(i18n("Enter new database name here"));
00305     addPage(m_dstPage, i18n("Select Location for Destination Database"));
00306 }
00307 
00308 //===========================================================
00309 //
00310 void ImportWizard::setupImportType()
00311 {
00312     m_importTypePage = new QWidget(this);
00313     QVBoxLayout *vbox = new QVBoxLayout(m_importTypePage, KDialog::marginHint());
00314     m_importTypeButtonGroup = new QVButtonGroup(m_importTypePage);
00315     m_importTypeButtonGroup->setLineWidth(0);
00316     vbox->addWidget( m_importTypeButtonGroup );
00317 
00318     (void)new QRadioButton(i18n("Structure and data"), m_importTypeButtonGroup);
00319     (void)new QRadioButton(i18n("Structure only"), m_importTypeButtonGroup);
00320 
00321     m_importTypeButtonGroup->setExclusive( true );
00322     m_importTypeButtonGroup->setButton( 0 );
00323     addPage(m_importTypePage, i18n("Select Type of Import"));
00324 }
00325 
00326 //===========================================================
00327 //
00328 void ImportWizard::setupImporting()
00329 {
00330     m_importingPage = new QWidget(this);
00331     m_importingPage->hide();
00332     QVBoxLayout *vbox = new QVBoxLayout(m_importingPage, KDialog::marginHint());
00333     m_lblImportingTxt = new QLabel(m_importingPage);
00334     m_lblImportingTxt->setAlignment( Qt::AlignTop | Qt::AlignLeft | Qt::WordBreak );
00335 
00336     m_lblImportingErrTxt = new KActiveLabel(m_importingPage);
00337     m_lblImportingErrTxt->setAlignment( Qt::AlignTop | Qt::AlignLeft | Qt::WordBreak );
00338 
00339     m_progressBar = new KProgress(100, m_importingPage);
00340     m_progressBar->hide();
00341 
00342     vbox->addWidget( m_lblImportingTxt );
00343     vbox->addWidget( m_lblImportingErrTxt );
00344     vbox->addStretch(1);
00345 
00346     KButtonBox *optionsBox = new KButtonBox(m_importingPage);
00347     vbox->addWidget( optionsBox );
00348     m_importOptionsButton = optionsBox->addButton(i18n("Advanced Options"), this, SLOT(slotOptionsButtonClicked()));
00349     m_importOptionsButton->setIconSet(SmallIconSet("configure"));
00350     optionsBox->addStretch(1);
00351 
00352     vbox->addWidget( m_progressBar );
00353 
00354     vbox->addStretch(2);
00355 
00356     m_importingPage->show();
00357 
00358     addPage(m_importingPage, i18n("Importing"));
00359 }
00360 
00361 //===========================================================
00362 //
00363 void ImportWizard::setupFinish()
00364 {
00365     m_finishPage = new QWidget(this);
00366     m_finishPage->hide();
00367     QVBoxLayout *vbox = new QVBoxLayout(m_finishPage, KDialog::marginHint());
00368     m_finishLbl = new KActiveLabel(m_finishPage);
00369     m_finishLbl->setAlignment( Qt::AlignTop | Qt::AlignLeft | Qt::WordBreak );
00370 
00371     vbox->addWidget( m_finishLbl );
00372     m_openImportedProjectCheckBox = new QCheckBox(i18n("Open imported project"), 
00373         m_finishPage, "openImportedProjectCheckBox");
00374     m_openImportedProjectCheckBox->setChecked(true);
00375     vbox->addSpacing( KDialog::spacingHint() );
00376     vbox->addWidget( m_openImportedProjectCheckBox );
00377     vbox->addStretch(1);
00378 
00379     addPage(m_finishPage, i18n("Success"));
00380 }
00381 
00382 //===========================================================
00383 //
00384 bool ImportWizard::checkUserInput()
00385 {
00386     QString finishtxt;
00387     bool problem;
00388 
00389     problem = false;
00390 //  if ((dstNewDBName->text() == "Enter new database name here" || dstNewDBName->text().isEmpty()))
00391     if (m_dstNewDBNameLineEdit->text().isEmpty())
00392     {
00393         problem = true;
00394         finishtxt = finishtxt + "\n" + i18n("No new database name was entered.");
00395     }
00396 
00397     if (problem)
00398     {
00399         finishtxt = i18n("Following problems were found with the data you entered:") +
00400                     "\n\n" +
00401                     finishtxt +
00402                     "\n\n" +
00403                     i18n("Please click 'Back' button and correct these errors.");
00404         m_lblImportingErrTxt->setText(finishtxt);
00405     }
00406 //  else
00407 //  {
00408 //it was weird      finishtxt = i18n("No problems were found with the data you entered.");
00409 //  }
00410 
00411     return !problem;
00412 }
00413 
00414 void ImportWizard::arriveSrcConnPage()
00415 {
00416     m_srcConnPage->hide();
00417 
00418 //  checkIfSrcTypeFileBased(m_srcTypeCombo->currentText());
00419 //  if (fileBasedSrcSelected()) {
00420 //moved     m_srcConn->showSimpleConn();
00423         if (m_setupFileBasedSrcNeeded) {
00424             m_setupFileBasedSrcNeeded = false;
00425             QStringList additionalMimeTypes;
00426     /* moved
00427             if (m_srcTypeCombo->currentText().contains("Access")) {
00429                 additionalMimeTypes << "application/x-msaccess";
00430             }*/
00431             m_srcConn->m_fileDlg->setMode(KexiStartupFileDialog::Opening);
00432             m_srcConn->m_fileDlg->setAdditionalFilters(additionalMimeTypes);
00433 /*moved         if (m_srcTypeCombo->currentText().contains("Access")) {
00435     #ifdef Q_WS_WIN
00436                 m_srcConn->m_fileDlg->setSelectedFilter("*.mdb");
00437     #else
00438                 m_srcConn->m_fileDlg->setFilter("*.mdb");
00439     #endif
00440             }*/
00441             //m_srcConn->m_file->label->hide();
00442             //m_srcConn->m_file->btn_advanced->hide();
00443             //m_srcConn->m_file->label->parentWidget()->hide();
00444         }
00445 //  } else {
00446 //      m_srcConn->showAdvancedConn();
00447 //  }
00449     m_srcConnPage->show();
00450 }
00451 
00452 void ImportWizard::arriveSrcDBPage()
00453 {
00454     if (fileBasedSrcSelected()) {
00456         //moved showPage(m_dstTypePage);
00457     }
00458     else if (!m_srcDBName) {
00459         m_srcDBPage->hide();
00460         kdDebug() << "Looks like we need a project selector widget!" << endl;
00461 
00462         KexiDB::ConnectionData* condata = m_srcConn->selectedConnectionData();
00463         if(condata) {
00464             m_prjSet = new KexiProjectSet(*condata);
00465             QVBoxLayout *vbox = new QVBoxLayout(m_srcDBPage, KDialog::marginHint());
00466             m_srcDBName = new KexiProjectSelectorWidget(m_srcDBPage,
00467                 "KexiMigrationProjectSelector", m_prjSet);
00468             vbox->addWidget( m_srcDBName );
00469             m_srcDBName->label->setText(i18n("Select source database you wish to import:"));
00470         }
00471         m_srcDBPage->show();
00472     }
00473 }
00474 
00475 void ImportWizard::arriveDstTitlePage()
00476 {
00477     if(fileBasedSrcSelected()) {
00478         QString suggestedDBName( QFileInfo(m_srcConn->selectedFileName()).fileName() );
00479         const QFileInfo fi( suggestedDBName );
00480         suggestedDBName = suggestedDBName.left(suggestedDBName.length() 
00481             - (fi.extension().length() ? (fi.extension().length()+1) : 0));
00482         m_dstNewDBNameLineEdit->setText( suggestedDBName );
00483     } else {
00484         if (m_predefinedConnectionData) {
00485             // server source db is predefined
00486             m_dstNewDBNameLineEdit->setText( m_predefinedDatabaseName );
00487         }
00488         else {
00489             if (!m_srcDBName || !m_srcDBName->selectedProjectData()) {
00490                 back(); //todo!
00491                 return;
00492             }
00493             m_dstNewDBNameLineEdit->setText( m_srcDBName->selectedProjectData()->databaseName() );
00494         }
00495     }
00496 }
00497 
00498 void ImportWizard::arriveDstPage()
00499 {
00500     m_dstPage->hide();
00501 
00502 //  checkIfDstTypeFileBased(m_dstTypeCombo->currentText());
00503     if(fileBasedDstSelected()) {
00504         m_dstConn->showSimpleConn();
00505         m_dstConn->m_fileDlg->setMode( KexiStartupFileDialog::SavingFileBasedDB );
00506         if (!m_fileBasedDstWasPresented) {
00507             //without extension - it will be added automatically
00508             m_dstConn->m_fileDlg->setLocationText(m_dstNewDBNameLineEdit->text());
00509         }
00510         m_fileBasedDstWasPresented = true;
00511     } else {
00512         m_dstConn->showAdvancedConn();
00513     }
00514     m_dstPage->show();
00515 }
00516 
00517 void ImportWizard::arriveImportingPage() {
00518 //  checkIfDstTypeFileBased(m_dstTypeCombo->currentText());
00519 /*moved if (m_fileBasedDstWasPresented) {
00520         if (!m_dstConn->m_fileDlg->checkFileName()) {
00521             back();
00522             return;
00523         }
00524     }*/
00525     m_importingPage->hide();
00526     if (checkUserInput()) {
00527         setNextEnabled(m_importingPage, true);
00528     }
00529     else {
00530         setNextEnabled(m_importingPage, false);
00531     }
00532 
00533     m_lblImportingTxt->setText(i18n(
00534                  "All required information has now "
00535                  "been gathered. Click \"Next\" button to start importing.\n\n"
00536                  "Depending on size of the database this may take some time."
00537                  /*"Note: You may be asked for extra "
00538                  "information such as field types if "
00539                  "the wizard could not automatically "
00540                  "determine this for you."*/));
00541 
00542 //todo
00543 
00544     //temp. hack for MS Access driver only
00547     bool showOptions = false;
00548     if (fileBasedSrcSelected()) {
00549         Kexi::ObjectStatus result;
00550         KexiMigrate* sourceDriver = prepareImport(result);
00551         if (sourceDriver) {
00552             showOptions = !result.error() 
00553                 && sourceDriver->propertyValue( "source_database_has_nonunicode_encoding" ).toBool();
00554             KexiMigration::Data *data = sourceDriver->data();
00555             sourceDriver->setData( 0 );
00556             delete data;
00557         }
00558     }
00559     if (showOptions)
00560         m_importOptionsButton->show();
00561     else
00562         m_importOptionsButton->hide();
00563 
00564     m_importingPage->show();
00565 }
00566 
00567 void ImportWizard::arriveFinishPage() {
00568 //  backButton()->hide();
00569 //  cancelButton()->setEnabled(false);
00570 //  m_finishLbl->setText(   m_successText.arg(m_dstNewDBNameLineEdit->text()) );
00571 }
00572 
00573 bool ImportWizard::fileBasedSrcSelected() const
00574 {
00575     if (m_predefinedConnectionData)
00576         return false;
00577 
00578 //  kdDebug() << (m_srcConn->selectedConnectionType()==KexiConnSelectorWidget::FileBased) << endl;
00579     return m_srcConn->selectedConnectionType()==KexiConnSelectorWidget::FileBased;
00580 }
00581 
00582 bool ImportWizard::fileBasedDstSelected() const
00583 {
00584     QString dstType(m_dstTypeCombo->currentText());
00585 
00587     KexiDB::DriverManager manager;
00588     return manager.driverInfo(dstType).fileBased;
00589 
00590 /*  if ((dstType == "PostgreSQL") || (dstType == "MySQL")) {
00591         return false;
00592     } else {
00593         return true;
00594     }*/
00595 }
00596 
00597 
00598 void ImportWizard::progressUpdated(int percent) {
00599     m_progressBar->setProgress(percent);
00600     KApplication::kApplication()->processEvents();
00601 }
00602 
00603 //===========================================================
00604 //
00605 QString ImportWizard::driverNameForSelectedSource()
00606 {
00607     if (fileBasedSrcSelected()) {
00608         KMimeType::Ptr ptr = KMimeType::findByFileContent( m_srcConn->selectedFileName() );
00609         if (!ptr || ptr.data()->name()=="application/octet-stream" || ptr.data()->name()=="text/plain") {
00610             //try by URL:
00611             ptr = KMimeType::findByURL( m_srcConn->selectedFileName() );
00612         }
00613         return ptr ? m_migrateManager.driverForMimeType( ptr.data()->name() ) : QString::null;
00614     }
00615 
00616     //server-based
00617     if (m_predefinedConnectionData) {
00618         return m_predefinedConnectionData->driverName;
00619     }
00620 
00621     return m_srcConn->selectedConnectionData() 
00622         ? m_srcConn->selectedConnectionData()->driverName : QString::null;
00623 }
00624 
00625 //===========================================================
00626 //
00627 void ImportWizard::accept()
00628 {
00629     /*moved
00630     backButton()->setEnabled(false);
00631     finishButton()->setEnabled(false);
00632 //  cancelButton()->setEnabled(false);
00633     acceptImport();
00634     backButton()->setEnabled(true);
00635     finishButton()->setEnabled(true);
00636 //  cancelButton()->setEnabled(true);
00637 */
00638     if (m_args) {
00639         if ((!fileBasedDstSelected() && !m_args->contains("destinationConnectionShortcut"))
00640             || !m_openImportedProjectCheckBox->isChecked())
00641         {
00642             //do not open dest db if used didn't want it
00643             //for server connections, destinationConnectionShortcut must be defined
00644             m_args->remove("destinationDatabaseName");
00645         }
00646     }
00647     KWizard::accept();
00648 }
00649 
00650 KexiMigrate* ImportWizard::prepareImport(Kexi::ObjectStatus& result)
00651 {
00652     KexiUtils::WaitCursor wait;
00653     
00654     // Start with a driver manager
00655     KexiDB::DriverManager manager;
00656 
00657     kdDebug() << "Creating destination driver..." << endl;
00658 
00659     // Get a driver to the destination database
00660     KexiDB::Driver *destDriver = manager.driver(
00661         m_dstConn->selectedConnectionData() ? m_dstConn->selectedConnectionData()->driverName //server based
00662          : m_dstTypeCombo->currentText() //file based
00663     );
00664     if (!destDriver || manager.error())
00665     {
00666         result.setStatus(&manager);
00667         kdDebug() << "Manager error..." << endl;
00668         manager.debugError();
00669         result.setStatus(&manager);
00670     }
00671 
00672     // Set up destination connection data
00673     KexiDB::ConnectionData *cdata;
00674     bool cdataOwned = false;
00675     QString dbname;
00676     if (!result.error())
00677     {
00678         if (m_dstConn->selectedConnectionData())
00679         {
00680             //server-based project
00681             kdDebug() << "Server destination..." << endl;
00682             cdata = m_dstConn->selectedConnectionData();
00683             dbname = m_dstNewDBNameLineEdit->text();
00684         }
00685         else if (m_dstTypeCombo->currentText().lower() == KexiDB::Driver::defaultFileBasedDriverName()) 
00686         {
00687             //file-based project
00688             kdDebug() << "File Destination..." << endl;
00689             cdata = new KexiDB::ConnectionData();
00690             cdataOwned = true;
00691             cdata->caption = m_dstNewDBNameLineEdit->text();
00692             cdata->driverName = KexiDB::Driver::defaultFileBasedDriverName();
00693             dbname = m_dstConn->selectedFileName();
00694             cdata->setFileName( dbname );
00695             kdDebug() << "Current file name: " << dbname << endl;
00696         }
00697         else
00698         {
00699             //TODO This needs a better message
00700             //KMessageBox::error(this, 
00701             result.setStatus(i18n("No connection data is available. You did not select a destination filename."),"");
00702             //return false;
00703         }
00704     }
00705 
00706     // Find a source (migration) driver name
00707     QString sourceDriverName;
00708     if (!result.error())
00709     {
00710         sourceDriverName = driverNameForSelectedSource();
00711         if (sourceDriverName.isEmpty())
00712             result.setStatus(i18n("No appropriate migration driver found."), 
00713                 m_migrateManager.possibleProblemsInfoMsg());
00714     }
00715 
00716     // Get a source (migration) driver
00717     KexiMigrate* sourceDriver = 0;
00718     if (!result.error())
00719     {
00720         sourceDriver = m_migrateManager.driver( sourceDriverName );
00721         if(!sourceDriver || m_migrateManager.error()) {
00722             kdDebug() << "Import migrate driver error..." << endl;
00723             result.setStatus(&m_migrateManager);
00724         }
00725     }
00726 
00727     KexiUtils::removeWaitCursor();
00728 
00729     // Set up source (migration) data required for connection
00730     if (sourceDriver && !result.error())
00731     {
00732         // Setup progress feedback for the GUI
00733         if(sourceDriver->progressSupported()) {
00734             m_progressBar->updateGeometry();
00735             disconnect(sourceDriver, SIGNAL(progressPercent(int)), 
00736                 this, SLOT(progressUpdated(int)));
00737             connect(sourceDriver, SIGNAL(progressPercent(int)),
00738                 this, SLOT(progressUpdated(int)));
00739             progressUpdated(0);
00740         }
00741 
00742         bool keepData;
00743         if (m_importTypeButtonGroup->selectedId() == 0)
00744         {
00745             kdDebug() << "Structure and data selected" << endl;
00746             keepData = true;
00747         }
00748         else if (m_importTypeButtonGroup->selectedId() == 1)
00749         {
00750             kdDebug() << "structure only selected" << endl;
00751             keepData = false;
00752         }
00753         else
00754         {
00755             kdDebug() << "Neither radio button is selected (not possible?) presume keep data" << endl;
00756             keepData = true;
00757         }
00758         
00759         KexiMigration::Data* md = new KexiMigration::Data();
00760     //  delete md->destination;
00761         md->destination = new KexiProjectData(*cdata, dbname);
00762         if(fileBasedSrcSelected()) {
00763             KexiDB::ConnectionData* conn_data = new KexiDB::ConnectionData();
00764             conn_data->setFileName(m_srcConn->selectedFileName());
00765             md->source = conn_data;
00766             md->sourceName = "";
00767         }
00768         else 
00769         {
00770             if (m_predefinedConnectionData)
00771                 md->source = m_predefinedConnectionData;
00772             else
00773                 md->source = m_srcConn->selectedConnectionData();
00774 
00775             if (!m_predefinedDatabaseName.isEmpty())
00776                 md->sourceName = m_predefinedDatabaseName;
00777             else
00778                 md->sourceName = m_srcDBName->selectedProjectData()->databaseName();
00780         }
00781         md->keepData = keepData;
00782         sourceDriver->setData(md);
00783         return sourceDriver;
00784     }
00785     return 0;
00786 }
00787 
00788 tristate ImportWizard::import()
00789 {
00790     m_importExecuted = true;
00791 
00792     Kexi::ObjectStatus result;
00793     KexiMigrate* sourceDriver = prepareImport(result);
00794 
00795     bool acceptingNeeded = false;
00796 
00797     // Perform import
00798     if (!result.error())
00799     {
00800         if (!m_sourceDBEncoding.isEmpty()) {
00801             sourceDriver->setPropertyValue( "source_database_nonunicode_encoding", 
00802                 QVariant(m_sourceDBEncoding.upper().replace(' ',"")) // "CP1250", not "cp 1250" 
00803             );
00804         }
00805 
00806         sourceDriver->checkIfDestinationDatabaseOverwritingNeedsAccepting(
00807             &result, acceptingNeeded);
00808 
00809         kdDebug() << sourceDriver->data()->destination->databaseName() << endl;
00810         kdDebug() << "Performing import..." << endl;
00811     }
00812 
00813     if (!result.error() && acceptingNeeded && KMessageBox::Yes != KMessageBox::warningYesNo(this,
00814         "<qt>"+i18n("Database %1 already exists."
00815         "<p>Do you want to replace it with a new one?")
00816         .arg(sourceDriver->data()->destination->infoString()),
00817         0, KGuiItem(i18n("&Replace")), KGuiItem(i18n("No"))))
00818     {
00819         return cancelled;
00820     }
00821 
00822     if (!result.error() && sourceDriver->progressSupported()) {
00823         m_progressBar->show();
00824     }
00825 
00826     if (!result.error() && sourceDriver->performImport(&result))
00827     {
00828         if (m_args) {
00829 //              if (fileBasedDstSelected()) {
00830             m_args->insert("destinationDatabaseName", 
00831                 sourceDriver->data()->destination->databaseName());
00832 //              }
00833             QString destinationConnectionShortcut( 
00834                 Kexi::connset().fileNameForConnectionData( m_dstConn->selectedConnectionData() ) );
00835             if (!destinationConnectionShortcut.isEmpty()) {
00836                 m_args->insert("destinationConnectionShortcut", destinationConnectionShortcut);
00837             }
00838         }
00839         setTitle(m_finishPage, i18n("Success"));
00840         return true;
00841     }
00842 
00843     if (result.error())
00844     {
00845         m_progressBar->setProgress(0);
00846         m_progressBar->hide();
00847 
00848         QString msg, details;
00849         KexiTextMessageHandler handler(msg, details);
00850         handler.showErrorMessage(&result);
00851 
00852         kdDebug() << msg << "\n" << details << endl;
00853         setTitle(m_finishPage, i18n("Failure"));
00854         m_finishLbl->setText(   
00855             i18n("<p>Import failed.</p>%1<p>%2</p><p>You can click \"Back\" button and try again.</p>")
00856             .arg(msg).arg(details));
00857         return false;
00858     }
00859 //  delete kexi_conn;
00860     return true;
00861 } 
00862 
00863 void ImportWizard::reject()
00864 {
00865     KWizard::reject();
00866 }
00867 
00868 //===========================================================
00869 //
00870 void ImportWizard::next()
00871 {
00872     if (currentPage() == m_srcConnPage) {
00873         if (fileBasedSrcSelected()
00874             && !QFileInfo(m_srcConn->selectedFileName()).isFile()) {
00875 
00876             KMessageBox::sorry(this,i18n("Select source database filename."));
00877             return;
00878         }
00879 
00880         if ( (! fileBasedSrcSelected()) && (! m_srcConn->selectedConnectionData()) ) {
00881             KMessageBox::sorry(this,i18n("Select source database."));
00882             return;
00883         }
00884 
00885         KexiMigrate* import = m_migrateManager.driver( driverNameForSelectedSource() );
00886         if(!import || m_migrateManager.error()) {
00887             QString dbname;
00888             if (fileBasedSrcSelected())
00889                 dbname = m_srcConn->selectedFileName();
00890             else
00891                 dbname = m_srcConn->selectedConnectionData() 
00892                     ? m_srcConn->selectedConnectionData()->serverInfoString() : QString::null;
00893                 if (!dbname.isEmpty())
00894                     dbname = QString(" \"%1\"").arg(dbname);
00895             KMessageBox::error(this, i18n("Could not import database%1. This type is not supported.")
00896                 .arg(dbname));
00897             return;
00898         }
00899     }
00900     else if (currentPage() == m_dstPage) {
00901         if (m_fileBasedDstWasPresented) {
00902             if (fileBasedDstSelected() && !m_dstConn->m_fileDlg->checkFileName())
00903                 return;
00904         }
00905     }
00906     else if (currentPage() == m_importingPage) {
00907         if (!m_importExecuted) {
00908             m_importOptionsButton->hide();
00909             nextButton()->setEnabled(false);
00910             finishButton()->setEnabled(false);
00911             backButton()->setEnabled(false);
00912             m_lblImportingTxt->setText(i18n("Importing in progress..."));
00913             tristate res = import();
00914             if (true == res) {
00915                 m_finishLbl->setText( 
00916                     i18n("Database has been imported into Kexi database project \"%1\".")
00917                     .arg(m_dstNewDBNameLineEdit->text()) );
00918                 cancelButton()->setEnabled(false);
00919                 setBackEnabled(m_finishPage, false);
00920                 setFinishEnabled(m_finishPage, true);
00921                 m_openImportedProjectCheckBox->show();
00922                 next();
00923                 return;
00924             }
00925 
00926             m_progressBar->hide();
00927             cancelButton()->setEnabled(true);
00928             setBackEnabled(m_finishPage, true);
00929             setFinishEnabled(m_finishPage, false);
00930             m_openImportedProjectCheckBox->hide();
00931             if (!res)
00932                 next();
00933             else if (~res) {
00934                 arriveImportingPage();
00935     //          back();
00936             }
00937             m_importExecuted = false;
00938             return;
00939         }
00940     }
00941 
00942     setAppropriate( m_srcDBPage, !fileBasedSrcSelected() && !m_predefinedConnectionData ); //skip m_srcDBPage
00943     KWizard::next();
00944 }
00945 
00946 void ImportWizard::back()
00947 {
00948     setAppropriate( m_srcDBPage, !fileBasedSrcSelected() && !m_predefinedConnectionData ); //skip m_srcDBPage
00949     KWizard::back();
00950 }
00951 
00952 void ImportWizard::pageSelected(const QString &)
00953 {
00954     if (currentPage() == m_introPage) {
00955     }
00956 //  else if (currentPage() == m_srcTypePage) {
00957 //  }
00958     else if (currentPage() == m_srcConnPage) {
00959         arriveSrcConnPage();
00960     }
00961     else if (currentPage() == m_srcDBPage) {
00962         arriveSrcDBPage();
00963     }
00964     else if (currentPage() == m_dstTypePage) {
00965     }
00966     else if (currentPage() == m_dstTitlePage) {
00967         arriveDstTitlePage();
00968     }
00969     else if (currentPage() == m_dstPage) {
00970         arriveDstPage();
00971     }
00972     else if (currentPage() == m_importingPage) {
00973         arriveImportingPage();
00974     }
00975     else if (currentPage() == m_finishPage) {
00976         arriveFinishPage();
00977     }
00978 }
00979 
00980 void ImportWizard::helpClicked()
00981 {
00982     if (currentPage() == m_introPage)
00983     {
00984         KMessageBox::information(this, i18n("No help is available for this page."), i18n("Help"));
00985     }
00986 /*  else if (currentPage() == m_srcTypePage)
00987     {
00988         KMessageBox::information(this, i18n("Here you can choose the type of data to import data from."), i18n("Help"));
00989     }*/
00990     else if (currentPage() == m_srcConnPage)
00991     {
00992         KMessageBox::information(this, i18n("Here you can choose the location to import data from."), i18n("Help"));
00993     }
00994     else if (currentPage() == m_srcDBPage)
00995     {
00996         KMessageBox::information(this, i18n("Here you can choose the actual database to import data from."), i18n("Help"));
00997     }
00998     else if (currentPage() == m_dstTypePage)
00999     {
01000         KMessageBox::information(this, i18n("Here you can choose the location to save the data."), i18n("Help"));
01001     }
01002     else if (currentPage() == m_dstPage)
01003     {
01004         KMessageBox::information(this, i18n("Here you can choose the location to save the data in and the new database name."), i18n("Help"));
01005     }
01006     else if (currentPage() == m_finishPage || currentPage() == m_importingPage)
01007     {
01008         KMessageBox::information(this, i18n("No help is available for this page."), i18n("Help"));
01009     }
01010 }
01011 
01012 void ImportWizard::slotOptionsButtonClicked()
01013 {
01014     OptionsDialog dlg(m_srcConn->selectedFileName(), m_sourceDBEncoding, this);
01015     if (QDialog::Accepted != dlg.exec())
01016         return;
01017 
01018     if (m_sourceDBEncoding != dlg.encodingComboBox()->selectedEncoding()) {
01019         m_sourceDBEncoding = dlg.encodingComboBox()->selectedEncoding();
01020     }
01021 }
01022 
01023 #include "importwizard.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys