kexi

importwizard.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Adam Pigg <adam@piggz.co.uk>
3 Copyright (C) 2004-2006 Jaroslaw Staniek <js@iidea.pl>
4 Copyright (C) 2005 Martin Ellis <martin.ellis@kdemail.net>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include "importwizard.h"
23#include "keximigrate.h"
24#include "importoptionsdlg.h"
25
26#include <tqlabel.h>
27#include <tqlayout.h>
28#include <tqvbuttongroup.h>
29#include <tqradiobutton.h>
30#include <tqcheckbox.h>
31
32#include <kcombobox.h>
33#include <tdemessagebox.h>
34#include <kpushbutton.h>
35#include <kdebug.h>
36#include <klineedit.h>
37#include <kiconloader.h>
38#include <kbuttonbox.h>
39
40#include <kexidb/drivermanager.h>
41#include <kexidb/driver.h>
42#include <kexidb/connectiondata.h>
43#include <kexidb/utils.h>
44#include <core/kexidbconnectionset.h>
45#include <core/kexi.h>
46#include <KexiConnSelector.h>
47#include <KexiProjectSelector.h>
48#include <KexiOpenExistingFile.h>
49#include <KexiDBTitlePage.h>
50#include <kexiutils/utils.h>
51#include <kexidbdrivercombobox.h>
52#include <kexitextmsghandler.h>
53#include <widget/kexicharencodingcombobox.h>
54#include <widget/kexiprjtypeselector.h>
55
56
57using namespace KexiMigration;
58
59//===========================================================
60//
61ImportWizard::ImportWizard(TQWidget *parent, TQMap<TQString,TQString>* args)
62 : KWizard(parent)
63 , m_args(args)
64{
65 setCaption(i18n("Import Database"));
66 setIcon(DesktopIcon("database_import"));
67 m_prjSet = 0;
68 m_fileBasedDstWasPresented = false;
69 m_setupFileBasedSrcNeeded = true;
70 m_importExecuted = false;
71 m_srcTypeCombo = 0;
72
73 setMinimumSize(400, 400);
74 parseArguments();
75 setupIntro();
76// setupSrcType();
77 setupSrcConn();
78 setupSrcDB();
79 setupDstType();
80 setupDstTitle();
81 setupDst();
82 setupImportType();
83 setupImporting();
84 setupFinish();
85
86 connect(this, TQ_SIGNAL(selected(const TQString &)), this, TQ_SLOT(pageSelected(const TQString &)));
87 connect(this, TQ_SIGNAL(helpClicked()), this, TQ_SLOT(helpClicked()));
88
89 if (m_predefinedConnectionData) {
90 // setup wizard for predefined server source
91 m_srcConn->showAdvancedConn();
92 setAppropriate( m_srcConnPage, false );
93 setAppropriate( m_srcDBPage, false );
94 }
95 else if (!m_predefinedDatabaseName.isEmpty()) {
96 // setup wizard for predefined source
97 // (used when external project type was opened in Kexi, e.g. mdb file)
98// MigrateManager manager;
99// TQString driverName = manager.driverForMimeType( m_predefinedMimeType );
100// m_srcTypeCombo->setCurrentText( driverName );
101
102// showPage( m_srcConnPage );
103 m_srcConn->showSimpleConn();
104 m_srcConn->setSelectedFileName(m_predefinedDatabaseName);
105
106 //disable all prev pages except "welcome" page
107 for (int i=0; i<indexOf(m_dstTypePage); i++) {
108 if (page(i)!=m_introPage)
109 setAppropriate( page(i), false );
110 }
111 }
112
113 m_sourceDBEncoding = TQString::fromLatin1(TDEGlobal::locale()->encoding()); //default
114}
115
116//===========================================================
117//
118ImportWizard::~ImportWizard()
119{
120 delete m_prjSet;
121}
122
123//===========================================================
124//
125void ImportWizard::parseArguments()
126{
127 m_predefinedConnectionData = 0;
128 if (!m_args)
129 return;
130 if (!(*m_args)["databaseName"].isEmpty() && !(*m_args)["mimeType"].isEmpty()) {
131 m_predefinedDatabaseName = (*m_args)["databaseName"];
132 m_predefinedMimeType = (*m_args)["mimeType"];
133 if (m_args->contains("connectionData")) {
134 m_predefinedConnectionData = new KexiDB::ConnectionData();
136 KexiUtils::deserializeMap((*m_args)["connectionData"]), *m_predefinedConnectionData
137 );
138 }
139 }
140 m_args->clear();
141}
142
143//===========================================================
144//
145void ImportWizard::setupIntro()
146{
147 m_introPage = new TQWidget(this);
148 TQVBoxLayout *vbox = new TQVBoxLayout(m_introPage, KDialog::marginHint());
149
150 TQLabel *lblIntro = new TQLabel(m_introPage);
151 lblIntro->setAlignment( TQt::AlignTop | TQt::AlignLeft | TQt::WordBreak );
152 TQString msg;
153 if (m_predefinedConnectionData) { //predefined import: server source
154 msg = i18n("<qt>Database Importing wizard is about to import \"%1\" database "
155 "<nobr>(connection %2)</nobr> into a Kexi database.</qt>")
156 .arg(m_predefinedDatabaseName).arg(m_predefinedConnectionData->serverInfoString());
157 }
158 else if (!m_predefinedDatabaseName.isEmpty()) { //predefined import: file source
160 KMimeType::Ptr mimeTypePtr = KMimeType::mimeType(m_predefinedMimeType);
161 msg = i18n("<qt>Database Importing wizard is about to import <nobr>\"%1\"</nobr> file "
162 "of type \"%2\" into a Kexi database.</qt>")
163 .arg(TQDir::convertSeparators(m_predefinedDatabaseName)).arg(mimeTypePtr->comment());
164 }
165 else {
166 msg = i18n("Database Importing wizard allows you to import an existing database "
167 "into a Kexi database.");
168 }
169 lblIntro->setText(msg+"\n\n"
170 +i18n("Click \"Next\" button to continue or \"Cancel\" button to exit this wizard."));
171 vbox->addWidget( lblIntro );
172 addPage(m_introPage, i18n("Welcome to the Database Importing Wizard"));
173}
174
175//===========================================================
176//
177/*
178void ImportWizard::setupSrcType()
179{
180 m_srcTypePage = new TQWidget(this);
181
183 TQVBoxLayout *vbox = new TQVBoxLayout(m_srcTypePage, KDialog::marginHint());
184
185 TQHBoxLayout *hbox = new TQHBoxLayout(vbox);
186 TQLabel *lbl = new TQLabel(i18n("Source database type:")+" ", m_srcTypePage);
187 hbox->addWidget(lbl);
188
189 m_srcTypeCombo = new KComboBox(m_srcTypePage);
190 hbox->addWidget(m_srcTypeCombo);
191 hbox->addStretch(1);
192 vbox->addStretch(1);
193 lbl->setBuddy(m_srcTypeCombo);
194
195 MigrateManager manager;
196 TQStringList names = manager.driverNames();
197
198 m_srcTypeCombo->insertStringList(names);
199 addPage(m_srcTypePage, i18n("Select Source Database Type"));
200}
201*/
202//===========================================================
203//
204void ImportWizard::setupSrcConn()
205{
206 m_srcConnPage = new TQWidget(this);
207 TQVBoxLayout *vbox = new TQVBoxLayout(m_srcConnPage, KDialog::marginHint());
208
209 m_srcConn = new KexiConnSelectorWidget(Kexi::connset(),
210 ":ProjectMigrationSourceDir", m_srcConnPage, "m_srcConnSelector");
211
212 m_srcConn->hideConnectonIcon();
213 m_srcConn->showSimpleConn();
214
215 TQStringList excludedFilters;
218 excludedFilters += "application/x-kexiproject-shortcut";
219 excludedFilters += "application/x-kexi-connectiondata";
220 m_srcConn->m_fileDlg->setExcludedFilters(excludedFilters);
221
222// m_srcConn->hideHelpers();
223 vbox->addWidget(m_srcConn);
224 addPage(m_srcConnPage, i18n("Select Location for Source Database"));
225}
226
227//===========================================================
228//
229void ImportWizard::setupSrcDB()
230{
231// arrivesrcdbPage creates widgets on that page
232 m_srcDBPage = new TQWidget(this);
233 m_srcDBName = NULL;
234 addPage(m_srcDBPage, i18n("Select Source Database"));
235}
236
237//===========================================================
238//
239void ImportWizard::setupDstType()
240{
241 m_dstTypePage = new TQWidget(this);
242
243 KexiDB::DriverManager manager;
244 KexiDB::Driver::InfoMap drvs = manager.driversInfo();
245
246 TQVBoxLayout *vbox = new TQVBoxLayout(m_dstTypePage, KDialog::marginHint());
247
248 TQHBoxLayout *hbox = new TQHBoxLayout(vbox);
249 TQLabel *lbl = new TQLabel(i18n("Destination database type:")+" ", m_dstTypePage);
250 lbl->setAlignment(TQt::AlignAuto|TQt::AlignTop);
251 hbox->addWidget(lbl);
252
253 m_dstPrjTypeSelector = new KexiPrjTypeSelector(m_dstTypePage);
254 hbox->addWidget(m_dstPrjTypeSelector);
255 m_dstPrjTypeSelector->option_file->setText(i18n("Database project stored in a file"));
256 m_dstPrjTypeSelector->option_server->setText(i18n("Database project stored on a server"));
257
258 TQVBoxLayout *frame_server_vbox = new TQVBoxLayout(m_dstPrjTypeSelector->frame_server, KDialog::spacingHint());
259 m_dstServerTypeCombo = new KexiDBDriverComboBox(m_dstPrjTypeSelector->frame_server, drvs,
260 KexiDBDriverComboBox::ShowServerDrivers);
261 frame_server_vbox->addWidget(m_dstServerTypeCombo);
262 hbox->addStretch(1);
263 vbox->addStretch(1);
264 lbl->setBuddy(m_dstServerTypeCombo);
265
267 //m_dstTypeCombo->setCurrentText("SQLite3");
268 addPage(m_dstTypePage, i18n("Select Destination Database Type"));
269}
270
271//===========================================================
272//
273void ImportWizard::setupDstTitle()
274{
275 m_dstTitlePage = new KexiDBTitlePage(i18n("Destination project's caption:"),
276 this, "KexiDBTitlePage");
277 m_dstTitlePage->layout()->setMargin( KDialog::marginHint() );
278 m_dstTitlePage->updateGeometry();
279 m_dstNewDBNameLineEdit = m_dstTitlePage->le_caption;
280 addPage(m_dstTitlePage, i18n("Select Destination Database Project's Caption"));
281}
282
283//===========================================================
284//
285void ImportWizard::setupDst()
286{
287 m_dstPage = new TQWidget(this);
288 TQVBoxLayout *vbox = new TQVBoxLayout(m_dstPage, KDialog::marginHint());
289
290 m_dstConn = new KexiConnSelectorWidget(Kexi::connset(),
291 ":ProjectMigrationDestinationDir", m_dstPage, "m_dstConnSelector");
292 m_dstConn->hideHelpers();
293 //me: Can't connect m_dstConn->m_fileDlg here, it doesn't exist yet
294 //connect(this, TQ_SLOT(next()), m_dstConn->m_fileDlg, TQ_SIGNAL(accepted()));
295
296 vbox->addWidget( m_dstConn );
297 connect(m_dstConn,TQ_SIGNAL(connectionItemExecuted(ConnectionDataLVItem*)),
298 this,TQ_SLOT(next()));
299
300// m_dstConn->hideHelpers();
301 m_dstConn->showSimpleConn();
302 //anyway, db files will be _saved_
303 m_dstConn->m_fileDlg->setMode( KexiStartupFileDialog::SavingFileBasedDB );
304// m_dstConn->hideHelpers();
305// m_dstConn->m_file->btn_advanced->hide();
306// m_dstConn->m_file->label->hide();
307// m_dstConn->m_file->lbl->hide();
308 //m_dstConn->m_file->spacer7->hide();
309
310
311 //js dstNewDBName = new KLineEdit(dstControls);
312 // dstNewDBName->setText(i18n("Enter new database name here"));
313 addPage(m_dstPage, i18n("Select Location for Destination Database"));
314}
315
316//===========================================================
317//
318void ImportWizard::setupImportType()
319{
320 m_importTypePage = new TQWidget(this);
321 TQVBoxLayout *vbox = new TQVBoxLayout(m_importTypePage, KDialog::marginHint());
322 m_importTypeButtonGroup = new TQVButtonGroup(m_importTypePage);
323 m_importTypeButtonGroup->setLineWidth(0);
324 vbox->addWidget( m_importTypeButtonGroup );
325
326 (void)new TQRadioButton(i18n("Structure and data"), m_importTypeButtonGroup);
327 (void)new TQRadioButton(i18n("Structure only"), m_importTypeButtonGroup);
328
329 m_importTypeButtonGroup->setExclusive( true );
330 m_importTypeButtonGroup->setButton( 0 );
331 addPage(m_importTypePage, i18n("Select Type of Import"));
332}
333
334//===========================================================
335//
336void ImportWizard::setupImporting()
337{
338 m_importingPage = new TQWidget(this);
339 m_importingPage->hide();
340 TQVBoxLayout *vbox = new TQVBoxLayout(m_importingPage, KDialog::marginHint());
341 m_lblImportingTxt = new TQLabel(m_importingPage);
342 m_lblImportingTxt->setAlignment( TQt::AlignTop | TQt::AlignLeft | TQt::WordBreak );
343
344 m_lblImportingErrTxt = new TQLabel(m_importingPage);
345 m_lblImportingErrTxt->setAlignment( TQt::AlignTop | TQt::AlignLeft | TQt::WordBreak );
346
347 m_progressBar = new KProgress(100, m_importingPage);
348 m_progressBar->hide();
349
350 vbox->addWidget( m_lblImportingTxt );
351 vbox->addWidget( m_lblImportingErrTxt );
352 vbox->addStretch(1);
353
354 KButtonBox *optionsBox = new KButtonBox(m_importingPage);
355 vbox->addWidget( optionsBox );
356 m_importOptionsButton = optionsBox->addButton(i18n("Advanced Options"), this, TQ_SLOT(slotOptionsButtonClicked()));
357 m_importOptionsButton->setIconSet(SmallIconSet("configure"));
358 optionsBox->addStretch(1);
359
360 vbox->addWidget( m_progressBar );
361
362 vbox->addStretch(2);
363
364 m_importingPage->show();
365
366 addPage(m_importingPage, i18n("Importing"));
367}
368
369//===========================================================
370//
371void ImportWizard::setupFinish()
372{
373 m_finishPage = new TQWidget(this);
374 m_finishPage->hide();
375 TQVBoxLayout *vbox = new TQVBoxLayout(m_finishPage, KDialog::marginHint());
376 m_finishLbl = new TQLabel(m_finishPage);
377 m_finishLbl->setAlignment( TQt::AlignTop | TQt::AlignLeft | TQt::WordBreak );
378
379 vbox->addWidget( m_finishLbl );
380 m_openImportedProjectCheckBox = new TQCheckBox(i18n("Open imported project"),
381 m_finishPage, "openImportedProjectCheckBox");
382 m_openImportedProjectCheckBox->setChecked(true);
383 vbox->addSpacing( KDialog::spacingHint() );
384 vbox->addWidget( m_openImportedProjectCheckBox );
385 vbox->addStretch(1);
386
387 addPage(m_finishPage, i18n("Success"));
388}
389
390//===========================================================
391//
392bool ImportWizard::checkUserInput()
393{
394 TQString finishtxt;
395
396 if (m_dstNewDBNameLineEdit->text().isEmpty())
397 {
398 finishtxt = finishtxt + "<br>" + i18n("No new database name was entered.");
399 }
400
401 Kexi::ObjectStatus result;
402 KexiMigrate* sourceDriver = prepareImport(result);
403 if (sourceDriver && sourceDriver->isSourceAndDestinationDataSourceTheSame())
404 {
405 finishtxt = finishtxt + "<br>" + i18n("Source database is the same as destination.");
406 }
407
408 if (! finishtxt.isNull())
409 {
410 finishtxt = "<qt>" + i18n("Following problems were found with the data you entered:") +
411 "<br>" + finishtxt + "<br><br>" +
412 i18n("Please click 'Back' button and correct these errors.");
413 m_lblImportingErrTxt->setText(finishtxt);
414 }
415
416 return finishtxt.isNull();
417}
418
419void ImportWizard::arriveSrcConnPage()
420{
421 m_srcConnPage->hide();
422
423// checkIfSrcTypeFileBased(m_srcTypeCombo->currentText());
424// if (fileBasedSrcSelected()) {
425//moved m_srcConn->showSimpleConn();
428 if (m_setupFileBasedSrcNeeded) {
429 m_setupFileBasedSrcNeeded = false;
430 TQStringList additionalMimeTypes;
431 /* moved
432 if (m_srcTypeCombo->currentText().contains("Access")) {
434 additionalMimeTypes << "application/x-msaccess";
435 }*/
436 m_srcConn->m_fileDlg->setMode(KexiStartupFileDialog::Opening);
437 m_srcConn->m_fileDlg->setAdditionalFilters(additionalMimeTypes);
438/*moved if (m_srcTypeCombo->currentText().contains("Access")) {
440 #ifdef TQ_WS_WIN
441 m_srcConn->m_fileDlg->setSelectedFilter("*.mdb");
442 #else
443 m_srcConn->m_fileDlg->setFilter("*.mdb");
444 #endif
445 }*/
446 //m_srcConn->m_file->label->hide();
447 //m_srcConn->m_file->btn_advanced->hide();
448 //m_srcConn->m_file->label->parentWidget()->hide();
449 }
450// } else {
451// m_srcConn->showAdvancedConn();
452// }
454 m_srcConnPage->show();
455}
456
457void ImportWizard::arriveSrcDBPage()
458{
459 if (fileBasedSrcSelected()) {
461 //moved showPage(m_dstTypePage);
462 }
463 else if (!m_srcDBName) {
464 m_srcDBPage->hide();
465 kdDebug() << "Looks like we need a project selector widget!" << endl;
466
467 KexiDB::ConnectionData* condata = m_srcConn->selectedConnectionData();
468 if(condata) {
469 m_prjSet = new KexiProjectSet(*condata);
470 TQVBoxLayout *vbox = new TQVBoxLayout(m_srcDBPage, KDialog::marginHint());
471 m_srcDBName = new KexiProjectSelectorWidget(m_srcDBPage,
472 "KexiMigrationProjectSelector", m_prjSet);
473 vbox->addWidget( m_srcDBName );
474 m_srcDBName->label->setText(i18n("Select source database you wish to import:"));
475 }
476 m_srcDBPage->show();
477 }
478}
479
480void ImportWizard::arriveDstTitlePage()
481{
482 if(fileBasedSrcSelected()) {
483 TQString suggestedDBName( TQFileInfo(m_srcConn->selectedFileName()).fileName() );
484 const TQFileInfo fi( suggestedDBName );
485 suggestedDBName = suggestedDBName.left(suggestedDBName.length()
486 - (fi.extension().length() ? (fi.extension().length()+1) : 0));
487 m_dstNewDBNameLineEdit->setText( suggestedDBName );
488 } else {
489 if (m_predefinedConnectionData) {
490 // server source db is predefined
491 m_dstNewDBNameLineEdit->setText( m_predefinedDatabaseName );
492 }
493 else {
494 if (!m_srcDBName || !m_srcDBName->selectedProjectData()) {
495 back(); //todo!
496 return;
497 }
498 m_dstNewDBNameLineEdit->setText( m_srcDBName->selectedProjectData()->databaseName() );
499 }
500 }
501}
502
503void ImportWizard::arriveDstPage()
504{
505 m_dstPage->hide();
506
507// checkIfDstTypeFileBased(m_dstTypeCombo->currentText());
508 if(fileBasedDstSelected()) {
509 m_dstConn->showSimpleConn();
510 m_dstConn->m_fileDlg->setMode( KexiStartupFileDialog::SavingFileBasedDB );
511 if (!m_fileBasedDstWasPresented) {
512 //without extension - it will be added automatically
513 m_dstConn->m_fileDlg->setLocationText(m_dstNewDBNameLineEdit->text());
514 }
515 m_fileBasedDstWasPresented = true;
516 } else {
517 m_dstConn->showAdvancedConn();
518 }
519 m_dstPage->show();
520}
521
522void ImportWizard::arriveImportingPage() {
523// checkIfDstTypeFileBased(m_dstTypeCombo->currentText());
524/*moved if (m_fileBasedDstWasPresented) {
525 if (!m_dstConn->m_fileDlg->checkFileName()) {
526 back();
527 return;
528 }
529 }*/
530 m_importingPage->hide();
531 if (checkUserInput()) {
532 setNextEnabled(m_importingPage, true);
533 }
534 else {
535 setNextEnabled(m_importingPage, false);
536 }
537
538 m_lblImportingTxt->setText(i18n(
539 "All required information has now "
540 "been gathered. Click \"Next\" button to start importing.\n\n"
541 "Depending on size of the database this may take some time."
542 /*"Note: You may be asked for extra "
543 "information such as field types if "
544 "the wizard could not automatically "
545 "determine this for you."*/));
546
547//todo
548
549 //temp. hack for MS Access driver only
552 bool showOptions = false;
553 if (fileBasedSrcSelected()) {
554 Kexi::ObjectStatus result;
555 KexiMigrate* sourceDriver = prepareImport(result);
556 if (sourceDriver) {
557 showOptions = !result.error()
558 && sourceDriver->propertyValue( "source_database_has_nonunicode_encoding" ).toBool();
559 KexiMigration::Data *data = sourceDriver->data();
560 sourceDriver->setData( 0 );
561 delete data;
562 }
563 }
564 if (showOptions)
565 m_importOptionsButton->show();
566 else
567 m_importOptionsButton->hide();
568
569 m_importingPage->show();
570}
571
572void ImportWizard::arriveFinishPage() {
573// backButton()->hide();
574// cancelButton()->setEnabled(false);
575// m_finishLbl->setText( m_successText.arg(m_dstNewDBNameLineEdit->text()) );
576}
577
578bool ImportWizard::fileBasedSrcSelected() const
579{
580 if (m_predefinedConnectionData)
581 return false;
582
583// kdDebug() << (m_srcConn->selectedConnectionType()==KexiConnSelectorWidget::FileBased) << endl;
584 return m_srcConn->selectedConnectionType()==KexiConnSelectorWidget::FileBased;
585}
586
587bool ImportWizard::fileBasedDstSelected() const
588{
589// TQString dstType(m_dstServerTypeCombo->currentText());
590
591 return m_dstPrjTypeSelector->buttonGroup->selectedId() == 1;
592
593/* if ((dstType == "PostgreSQL") || (dstType == "MySQL")) {
594 return false;
595 } else {
596 return true;
597 }*/
598}
599
600
601void ImportWizard::progressUpdated(int percent) {
602 m_progressBar->setProgress(percent);
603 TDEApplication::kApplication()->processEvents();
604}
605
606//===========================================================
607//
608TQString ImportWizard::driverNameForSelectedSource()
609{
610 if (fileBasedSrcSelected()) {
611 KMimeType::Ptr ptr = KMimeType::findByFileContent( m_srcConn->selectedFileName() );
612 if (!ptr || ptr.data()->name()=="application/octet-stream" || ptr.data()->name()=="text/plain") {
613 //try by URL:
614 ptr = KMimeType::findByURL( m_srcConn->selectedFileName() );
615 }
616 return ptr ? m_migrateManager.driverForMimeType( ptr.data()->name() ) : TQString();
617 }
618
619 //server-based
620 if (m_predefinedConnectionData) {
621 return m_predefinedConnectionData->driverName;
622 }
623
624 return m_srcConn->selectedConnectionData()
625 ? m_srcConn->selectedConnectionData()->driverName : TQString();
626}
627
628//===========================================================
629//
630void ImportWizard::accept()
631{
632 /*moved
633 backButton()->setEnabled(false);
634 finishButton()->setEnabled(false);
635// cancelButton()->setEnabled(false);
636 acceptImport();
637 backButton()->setEnabled(true);
638 finishButton()->setEnabled(true);
639// cancelButton()->setEnabled(true);
640*/
641 if (m_args) {
642 if ((!fileBasedDstSelected() && !m_args->contains("destinationConnectionShortcut"))
643 || !m_openImportedProjectCheckBox->isChecked())
644 {
645 //do not open dest db if used didn't want it
646 //for server connections, destinationConnectionShortcut must be defined
647 m_args->remove("destinationDatabaseName");
648 }
649 }
650 KWizard::accept();
651}
652
653KexiMigrate* ImportWizard::prepareImport(Kexi::ObjectStatus& result)
654{
655 KexiUtils::WaitCursor wait;
656
657 // Start with a driver manager
658 KexiDB::DriverManager manager;
659
660 kdDebug() << "Creating destination driver..." << endl;
661
662 // Get a driver to the destination database
663 KexiDB::Driver *destDriver = manager.driver(
664 m_dstConn->selectedConnectionData() ? m_dstConn->selectedConnectionData()->driverName //server based
666 // : m_dstTypeCombo->currentText() //file based
667 );
668 if (!destDriver || manager.error())
669 {
670 result.setStatus(&manager);
671 kdDebug() << "Manager error..." << endl;
672 manager.debugError();
673// result.setStatus(&manager);
674 }
675
676 // Set up destination connection data
677 KexiDB::ConnectionData *cdata;
678 bool cdataOwned = false;
679 TQString dbname;
680 if (!result.error())
681 {
682 if (m_dstConn->selectedConnectionData())
683 {
684 //server-based project
685 kdDebug() << "Server destination..." << endl;
686 cdata = m_dstConn->selectedConnectionData();
687 dbname = m_dstNewDBNameLineEdit->text();
688 }
689 else // if (m_dstTypeCombo->currentText().lower() == KexiDB::Driver::defaultFileBasedDriverName())
690 {
691 //file-based project
692 kdDebug() << "File Destination..." << endl;
693 cdata = new KexiDB::ConnectionData();
694 cdataOwned = true;
695 cdata->caption = m_dstNewDBNameLineEdit->text();
697 dbname = m_dstConn->selectedFileName();
698 cdata->setFileName( dbname );
699 kdDebug() << "Current file name: " << dbname << endl;
700 }
701/* else
702 {
703 //TODO This needs a better message
704 //KMessageBox::error(this,
705 result.setStatus(i18n("No connection data is available. You did not select a destination filename."),"");
706 //return false;
707 } */
708 }
709
710 // Find a source (migration) driver name
711 TQString sourceDriverName;
712 if (!result.error())
713 {
714 sourceDriverName = driverNameForSelectedSource();
715 if (sourceDriverName.isEmpty())
716 result.setStatus(i18n("No appropriate migration driver found."),
717 m_migrateManager.possibleProblemsInfoMsg());
718 }
719
720 // Get a source (migration) driver
721 KexiMigrate* sourceDriver = 0;
722 if (!result.error())
723 {
724 sourceDriver = m_migrateManager.driver( sourceDriverName );
725 if(!sourceDriver || m_migrateManager.error()) {
726 kdDebug() << "Import migrate driver error..." << endl;
727 result.setStatus(&m_migrateManager);
728 }
729 }
730
732
733 // Set up source (migration) data required for connection
734 if (sourceDriver && !result.error())
735 {
736 // Setup progress feedback for the GUI
737 if(sourceDriver->progressSupported()) {
738 m_progressBar->updateGeometry();
739 disconnect(sourceDriver, TQ_SIGNAL(progressPercent(int)),
740 this, TQ_SLOT(progressUpdated(int)));
741 connect(sourceDriver, TQ_SIGNAL(progressPercent(int)),
742 this, TQ_SLOT(progressUpdated(int)));
743 progressUpdated(0);
744 }
745
746 bool keepData;
747 if (m_importTypeButtonGroup->selectedId() == 0)
748 {
749 kdDebug() << "Structure and data selected" << endl;
750 keepData = true;
751 }
752 else if (m_importTypeButtonGroup->selectedId() == 1)
753 {
754 kdDebug() << "structure only selected" << endl;
755 keepData = false;
756 }
757 else
758 {
759 kdDebug() << "Neither radio button is selected (not possible?) presume keep data" << endl;
760 keepData = true;
761 }
762
763 KexiMigration::Data* md = new KexiMigration::Data();
764 // delete md->destination;
765 md->destination = new KexiProjectData(*cdata, dbname);
766 if(fileBasedSrcSelected()) {
767 KexiDB::ConnectionData* conn_data = new KexiDB::ConnectionData();
768 conn_data->setFileName(m_srcConn->selectedFileName());
769 md->source = conn_data;
770 md->sourceName = "";
771 }
772 else
773 {
774 if (m_predefinedConnectionData)
775 md->source = m_predefinedConnectionData;
776 else
777 md->source = m_srcConn->selectedConnectionData();
778
779 if (!m_predefinedDatabaseName.isEmpty())
780 md->sourceName = m_predefinedDatabaseName;
781 else
782 md->sourceName = m_srcDBName->selectedProjectData()->databaseName();
784 }
785 md->keepData = keepData;
786 sourceDriver->setData(md);
787 return sourceDriver;
788 }
789 return 0;
790}
791
792tristate ImportWizard::import()
793{
794 m_importExecuted = true;
795
796 Kexi::ObjectStatus result;
797 KexiMigrate* sourceDriver = prepareImport(result);
798
799 bool acceptingNeeded = false;
800
801 // Perform import
802 if (sourceDriver && !result.error())
803 {
804 if (!m_sourceDBEncoding.isEmpty()) {
805 sourceDriver->setPropertyValue( "source_database_nonunicode_encoding",
806 TQVariant(m_sourceDBEncoding.upper().replace(' ',"")) // "CP1250", not "cp 1250"
807 );
808 }
809
810 if (!sourceDriver->checkIfDestinationDatabaseOverwritingNeedsAccepting(&result, acceptingNeeded)) {
811 kdDebug() << "Abort import cause checkIfDestinationDatabaseOverwritingNeedsAccepting returned false." << endl;
812 return false;
813 }
814
815 kdDebug() << sourceDriver->data()->destination->databaseName() << endl;
816 kdDebug() << "Performing import..." << endl;
817 }
818
819 if (sourceDriver && !result.error() && acceptingNeeded) { // ok, the destination-db already exists...
820 if (KMessageBox::Yes != KMessageBox::warningYesNo(this,
821 "<qt>"+i18n("Database %1 already exists."
822 "<p>Do you want to replace it with a new one?")
823 .arg(sourceDriver->data()->destination->infoString()),
824 0, KGuiItem(i18n("&Replace")), KGuiItem(i18n("No"))))
825 {
826 return cancelled;
827 }
828 }
829
830 if (sourceDriver && !result.error() && sourceDriver->progressSupported()) {
831 m_progressBar->show();
832 }
833
834 if (sourceDriver && !result.error() && sourceDriver->performImport(&result))
835 {
836 if (m_args) {
837// if (fileBasedDstSelected()) {
838 m_args->insert("destinationDatabaseName",
839 sourceDriver->data()->destination->databaseName());
840// }
841 TQString destinationConnectionShortcut(
842 Kexi::connset().fileNameForConnectionData( m_dstConn->selectedConnectionData() ) );
843 if (!destinationConnectionShortcut.isEmpty()) {
844 m_args->insert("destinationConnectionShortcut", destinationConnectionShortcut);
845 }
846 }
847 setTitle(m_finishPage, i18n("Success"));
848 return true;
849 }
850
851 if (!sourceDriver || result.error())
852 {
853 m_progressBar->setProgress(0);
854 m_progressBar->hide();
855
856 TQString msg, details;
857 KexiTextMessageHandler handler(msg, details);
858 handler.showErrorMessage(&result);
859
860 kdDebug() << msg << "\n" << details << endl;
861 setTitle(m_finishPage, i18n("Failure"));
862 m_finishLbl->setText(
863 i18n("<p>Import failed.</p>%1<p>%2</p><p>You can click \"Back\" button and try again.</p>")
864 .arg(msg).arg(details));
865 return false;
866 }
867// delete kexi_conn;
868 return true;
869}
870
871void ImportWizard::reject()
872{
873 KWizard::reject();
874}
875
876//===========================================================
877//
879{
880 if (currentPage() == m_srcConnPage) {
881 if (fileBasedSrcSelected()
882 && !TQFileInfo(m_srcConn->selectedFileName()).isFile()) {
883
884 KMessageBox::sorry(this,i18n("Select source database filename."));
885 return;
886 }
887
888 if ( (! fileBasedSrcSelected()) && (! m_srcConn->selectedConnectionData()) ) {
889 KMessageBox::sorry(this,i18n("Select source database."));
890 return;
891 }
892
893 KexiMigrate* import = m_migrateManager.driver( driverNameForSelectedSource() );
894 if(!import || m_migrateManager.error()) {
895 TQString dbname;
896 if (fileBasedSrcSelected())
897 dbname = m_srcConn->selectedFileName();
898 else
899 dbname = m_srcConn->selectedConnectionData()
900 ? m_srcConn->selectedConnectionData()->serverInfoString() : TQString();
901 if (!dbname.isEmpty())
902 dbname = TQString(" \"%1\"").arg(dbname);
903 KMessageBox::error(this, i18n("Could not import database%1. This type is not supported.")
904 .arg(dbname));
905 return;
906 }
907 }
908 else if (currentPage() == m_dstPage) {
909 if (m_fileBasedDstWasPresented) {
910 if (fileBasedDstSelected() && !m_dstConn->m_fileDlg->checkFileName())
911 return;
912 }
913 }
914 else if (currentPage() == m_importingPage) {
915 if (!m_importExecuted) {
916 m_importOptionsButton->hide();
917 nextButton()->setEnabled(false);
918 finishButton()->setEnabled(false);
919 backButton()->setEnabled(false);
920 m_lblImportingTxt->setText(i18n("Importing in progress..."));
921 tristate res = import();
922 if (true == res) {
923 m_finishLbl->setText(
924 i18n("Database has been imported into Kexi database project \"%1\".")
925 .arg(m_dstNewDBNameLineEdit->text()) );
926 cancelButton()->setEnabled(false);
927 setBackEnabled(m_finishPage, false);
928 setFinishEnabled(m_finishPage, true);
929 m_openImportedProjectCheckBox->show();
930 next();
931 return;
932 }
933
934 m_progressBar->hide();
935 cancelButton()->setEnabled(true);
936 setBackEnabled(m_finishPage, true);
937 setFinishEnabled(m_finishPage, false);
938 m_openImportedProjectCheckBox->hide();
939 if (!res)
940 next();
941 else if (~res) {
942 arriveImportingPage();
943 // back();
944 }
945 m_importExecuted = false;
946 return;
947 }
948 }
949
950 setAppropriate( m_srcDBPage, !fileBasedSrcSelected() && !m_predefinedConnectionData ); //skip m_srcDBPage
951 KWizard::next();
952}
953
954void ImportWizard::back()
955{
956 setAppropriate( m_srcDBPage, !fileBasedSrcSelected() && !m_predefinedConnectionData ); //skip m_srcDBPage
957 KWizard::back();
958}
959
960void ImportWizard::pageSelected(const TQString &)
961{
962 if (currentPage() == m_introPage) {
963 }
964// else if (currentPage() == m_srcTypePage) {
965// }
966 else if (currentPage() == m_srcConnPage) {
967 arriveSrcConnPage();
968 }
969 else if (currentPage() == m_srcDBPage) {
970 arriveSrcDBPage();
971 }
972 else if (currentPage() == m_dstTypePage) {
973 }
974 else if (currentPage() == m_dstTitlePage) {
975 arriveDstTitlePage();
976 }
977 else if (currentPage() == m_dstPage) {
978 arriveDstPage();
979 }
980 else if (currentPage() == m_importingPage) {
981 arriveImportingPage();
982 }
983 else if (currentPage() == m_finishPage) {
984 arriveFinishPage();
985 }
986}
987
988void ImportWizard::helpClicked()
989{
990 if (currentPage() == m_introPage)
991 {
992 KMessageBox::information(this, i18n("No help is available for this page."), i18n("Help"));
993 }
994/* else if (currentPage() == m_srcTypePage)
995 {
996 KMessageBox::information(this, i18n("Here you can choose the type of data to import data from."), i18n("Help"));
997 }*/
998 else if (currentPage() == m_srcConnPage)
999 {
1000 KMessageBox::information(this, i18n("Here you can choose the location to import data from."), i18n("Help"));
1001 }
1002 else if (currentPage() == m_srcDBPage)
1003 {
1004 KMessageBox::information(this, i18n("Here you can choose the actual database to import data from."), i18n("Help"));
1005 }
1006 else if (currentPage() == m_dstTypePage)
1007 {
1008 KMessageBox::information(this, i18n("Here you can choose the location to save the data."), i18n("Help"));
1009 }
1010 else if (currentPage() == m_dstPage)
1011 {
1012 KMessageBox::information(this, i18n("Here you can choose the location to save the data in and the new database name."), i18n("Help"));
1013 }
1014 else if (currentPage() == m_finishPage || currentPage() == m_importingPage)
1015 {
1016 KMessageBox::information(this, i18n("No help is available for this page."), i18n("Help"));
1017 }
1018}
1019
1020void ImportWizard::slotOptionsButtonClicked()
1021{
1022 OptionsDialog dlg(m_srcConn->selectedFileName(), m_sourceDBEncoding, this);
1023 if (TQDialog::Accepted != dlg.exec())
1024 return;
1025
1026 if (m_sourceDBEncoding != dlg.encodingComboBox()->selectedEncoding()) {
1027 m_sourceDBEncoding = dlg.encodingComboBox()->selectedEncoding();
1028 }
1029}
1030
1031#include "importwizard.moc"
TQString caption
The caption of the connection.
TQString driverName
the name of the driver that should be used to create a connection
Database specific connection data, e.g. host, port.
void setFileName(const TQString &fn)
Set the filename used by the connection.
const KexiDB::Driver::InfoMap driversInfo()
Driver * driver(const TQString &name)
static TQString defaultFileBasedDriverName()
Definition driver.cpp:140
static TQString defaultFileBasedDriverMimeType()
Definition driver.cpp:137
bool error() const
Definition object.h:38
void debugError()
Definition object.cpp:159
ImportWizard(TQWidget *parent=0, TQMap< TQString, TQString > *args=0)
Imports non-native databases into Kexi projects.
Definition keximigrate.h:79
bool performImport(Kexi::ObjectStatus *result=0)
Perform an import operation.
bool progressSupported()
Returns true if the migration driver supports progress updates.
virtual TQVariant propertyValue(const TQCString &propName)
bool isSourceAndDestinationDataSourceTheSame() const
bool checkIfDestinationDatabaseOverwritingNeedsAccepting(Kexi::ObjectStatus *result, bool &acceptingNeeded)
void setData(KexiMigration::Data *migrateData)
Data Setup. Requires two connection objects, a name and a bool.
void setPropertyValue(const TQCString &propName, const TQVariant &value)
KexiMigration::Data * data() const
Definition keximigrate.h:87
TQString infoString(bool nobr=true) const
TQString databaseName() const
3-state logical type with three values: true, false and cancelled and convenient operators.
Definition tristate.h:100
KEXI_DB_EXPORT void fromMap(const TQMap< TQString, TQString > &map, ConnectionData &data)
Framework for importing databases into native KexiDB databases.
KEXIUTILS_EXPORT TQMap< TQString, TQString > deserializeMap(const TQByteArray &array)
KEXIUTILS_EXPORT void removeWaitCursor()
KEXICORE_EXPORT KexiDBConnectionSet & connset()
A set of known connections.
Definition kexi.cpp:74