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