kexi

kexicsvexportwizard.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005,2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexicsvexportwizard.h"
00021 #include "kexicsvwidgets.h"
00022 #include <main/startup/KexiStartupFileDialog.h>
00023 #include <kexidb/cursor.h>
00024 #include <kexidb/utils.h>
00025 #include <core/keximainwindow.h>
00026 #include <core/kexiproject.h>
00027 #include <core/kexipartinfo.h>
00028 #include <core/kexipartmanager.h>
00029 #include <core/kexiguimsghandler.h>
00030 #include <kexiutils/utils.h>
00031 #include <widget/kexicharencodingcombobox.h>
00032 
00033 #include <qcheckbox.h>
00034 #include <qgroupbox.h>
00035 #include <qclipboard.h>
00036 #include <kapplication.h>
00037 #include <klocale.h>
00038 #include <kiconloader.h>
00039 #include <kactivelabel.h>
00040 #include <kpushbutton.h>
00041 #include <kapplication.h>
00042 #include <kdebug.h>
00043 #include <ksavefile.h>
00044 
00045 
00046 KexiCSVExportWizard::KexiCSVExportWizard( const KexiCSVExport::Options& options,
00047     KexiMainWindow* mainWin, QWidget * parent, const char * name )
00048  : KWizard(parent, name)
00049  , m_options(options)
00050 // , m_mode(mode)
00051 // , m_itemId(itemId)
00052  , m_mainWin(mainWin)
00053  , m_fileSavePage(0)
00054  , m_defaultsBtn(0)
00055  , m_rowCount(-1)
00056  , m_rowCountDetermined(false)
00057  , m_cancelled(false)
00058 {
00059     if (m_options.mode==KexiCSVExport::Clipboard) {
00060         finishButton()->setText(i18n("Copy"));
00061         backButton()->hide();
00062     }
00063     else {
00064         finishButton()->setText(i18n("Export"));
00065     }
00066     helpButton()->hide();
00067 
00068     QString infoLblFromText;
00069     KexiGUIMessageHandler msgh(this);
00070     m_tableOrQuery = new KexiDB::TableOrQuerySchema(
00071         m_mainWin->project()->dbConnection(), m_options.itemId);
00072     if (m_tableOrQuery->table()) {
00073         if (m_options.mode==KexiCSVExport::Clipboard) {
00074             setCaption(i18n("Copy Data From Table to Clipboard"));
00075             infoLblFromText = i18n("Copying data from table:");
00076         }
00077         else {
00078             setCaption(i18n("Export Data From Table to CSV File"));
00079             infoLblFromText = i18n("Exporting data from table:");
00080         }
00081     }
00082     else if (m_tableOrQuery->query()) {
00083         if (m_options.mode==KexiCSVExport::Clipboard) {
00084             setCaption(i18n("Copy Data From Query to Clipboard"));
00085             infoLblFromText = i18n("Copying data from table:");
00086         }
00087         else {
00088             setCaption(i18n("Export Data From Query to CSV File"));
00089             infoLblFromText = i18n("Exporting data from query:");
00090         }
00091     }
00092     else {
00093         msgh.showErrorMessage(m_mainWin->project()->dbConnection(),
00094             i18n("Could not open data for exporting."));
00095         m_cancelled = true;
00096         return;
00097     }
00098     // OK, source data found.
00099 
00100     // Setup pages
00101 
00102     // 1. File Save Page
00103     if (m_options.mode==KexiCSVExport::File) {
00104         m_fileSavePage = new KexiStartupFileDialog(
00105             ":CSVImportExport", //startDir
00106             KexiStartupFileDialog::Custom | KexiStartupFileDialog::SavingFileBasedDB,
00107             this, "m_fileSavePage");
00108         m_fileSavePage->setMinimumHeight(kapp->desktop()->height()/2);
00109         m_fileSavePage->setAdditionalFilters( csvMimeTypes() );
00110         m_fileSavePage->setDefaultExtension("csv");
00111         m_fileSavePage->setLocationText( KexiUtils::stringToFileName(m_tableOrQuery->captionOrName()) );
00112         connect(m_fileSavePage, SIGNAL(rejected()), this, SLOT(reject()));
00113         addPage(m_fileSavePage, i18n("Enter Name of File You Want to Save Data To"));
00114     }
00115 
00116     // 2. Export options
00117     m_exportOptionsPage = new QWidget(this, "m_exportOptionsPage");
00118     QGridLayout *exportOptionsLyr = new QGridLayout( m_exportOptionsPage, 6, 3,
00119         KDialogBase::marginHint(), KDialogBase::spacingHint(), "exportOptionsLyr");
00120     m_infoLblFrom = new KexiCSVInfoLabel( infoLblFromText, m_exportOptionsPage );
00121     KexiPart::Info *partInfo = Kexi::partManager().infoForMimeType(
00122         m_tableOrQuery->table() ? "kexi/table" : "kexi/query");
00123     if (partInfo)
00124         m_infoLblFrom->setIcon(partInfo->itemIcon());
00125     m_infoLblFrom->separator()->hide();
00126     exportOptionsLyr->addMultiCellWidget(m_infoLblFrom, 0, 0, 0, 2);
00127 
00128     m_infoLblTo = new KexiCSVInfoLabel(
00129         (m_options.mode==KexiCSVExport::File) ? i18n("To CSV file:") : i18n("To clipboard:"),
00130         m_exportOptionsPage
00131     );
00132     if (m_options.mode==KexiCSVExport::Clipboard)
00133         m_infoLblTo->setIcon("editpaste");
00134     exportOptionsLyr->addMultiCellWidget(m_infoLblTo, 1, 1, 0, 2);
00135 
00136     m_showOptionsButton = new KPushButton(KGuiItem(i18n("Show Options >>"), "configure"),
00137         m_exportOptionsPage);
00138     connect(m_showOptionsButton, SIGNAL(clicked()), this, SLOT(slotShowOptionsButtonClicked()));
00139     exportOptionsLyr->addMultiCellWidget(m_showOptionsButton, 2, 2, 0, 0);
00140     m_showOptionsButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00141 
00142     // -<options section>
00143     m_exportOptionsSection = new QGroupBox(1, Vertical, i18n("Options"), m_exportOptionsPage,
00144         "m_exportOptionsSection");
00145     m_exportOptionsSection->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00146     exportOptionsLyr->addMultiCellWidget(m_exportOptionsSection, 3, 3, 0, 1);
00147     QWidget *exportOptionsSectionWidget
00148         = new QWidget(m_exportOptionsSection, "exportOptionsSectionWidget");
00149     QGridLayout *exportOptionsSectionLyr = new QGridLayout( exportOptionsSectionWidget, 5, 2,
00150         0, KDialogBase::spacingHint(), "exportOptionsLyr");
00151 
00152     // -delimiter
00153     m_delimiterWidget = new KexiCSVDelimiterWidget(false, 
00154         exportOptionsSectionWidget);
00155     m_delimiterWidget->setDelimiter(defaultDelimiter());
00156     exportOptionsSectionLyr->addWidget( m_delimiterWidget, 0, 1 );
00157     QLabel *delimiterLabel = new QLabel(m_delimiterWidget, i18n("Delimiter:"), exportOptionsSectionWidget);
00158     exportOptionsSectionLyr->addWidget( delimiterLabel, 0, 0 );
00159 
00160     // -text quote
00161     QWidget *textQuoteWidget = new QWidget(exportOptionsSectionWidget);
00162     QHBoxLayout *textQuoteLyr = new QHBoxLayout(textQuoteWidget);
00163     exportOptionsSectionLyr->addWidget(textQuoteWidget, 1, 1);
00164     m_textQuote = new KexiCSVTextQuoteComboBox( textQuoteWidget );
00165     m_textQuote->setTextQuote(defaultTextQuote());
00166     textQuoteLyr->addWidget( m_textQuote );
00167     textQuoteLyr->addStretch(0);
00168     QLabel *textQuoteLabel = new QLabel(m_textQuote, i18n("Text quote:"), exportOptionsSectionWidget);
00169     exportOptionsSectionLyr->addWidget( textQuoteLabel, 1, 0 );
00170 
00171     // - character encoding
00172     m_characterEncodingCombo = new KexiCharacterEncodingComboBox( exportOptionsSectionWidget );
00173     m_characterEncodingCombo->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00174     exportOptionsSectionLyr->addWidget( m_characterEncodingCombo, 2, 1 );
00175     QLabel *characterEncodingLabel = new QLabel(m_characterEncodingCombo, i18n("Text encoding:"),
00176         exportOptionsSectionWidget);
00177     exportOptionsSectionLyr->addWidget( characterEncodingLabel, 2, 0 );
00178 
00179     // - checkboxes
00180     m_addColumnNamesCheckBox = new QCheckBox(i18n("Add column names as the first row"),
00181         exportOptionsSectionWidget);
00182     m_addColumnNamesCheckBox->setChecked(true);
00183     exportOptionsSectionLyr->addWidget( m_addColumnNamesCheckBox, 3, 1 );
00185     m_alwaysUseCheckBox = new QCheckBox(i18n("Always use above options for exporting"),
00186         m_exportOptionsPage);
00187     exportOptionsLyr->addMultiCellWidget(m_alwaysUseCheckBox, 4, 4, 0, 1);
00188 //  exportOptionsSectionLyr->addWidget( m_alwaysUseCheckBox, 4, 1 );
00189     m_exportOptionsSection->hide();
00190     m_alwaysUseCheckBox->hide();
00191     // -</options section>
00192 
00193 //  exportOptionsLyr->setColStretch(3, 1);
00194     exportOptionsLyr->addMultiCell(
00195         new QSpacerItem( 0, 0, QSizePolicy::Preferred, QSizePolicy::MinimumExpanding), 5, 5, 0, 1 );
00196 
00197 //  addPage(m_exportOptionsPage, i18n("Set Export Options"));
00198     addPage(m_exportOptionsPage, m_options.mode==KexiCSVExport::Clipboard ? i18n("Copying") : i18n("Exporting"));
00199     setFinishEnabled(m_exportOptionsPage, true);
00200 
00201     // load settings
00202     kapp->config()->setGroup("ImportExport");
00203     if (m_options.mode!=KexiCSVExport::Clipboard && readBoolEntry("ShowOptionsInCSVExportDialog", false)) {
00204         show();
00205         slotShowOptionsButtonClicked();
00206     }
00207     if (readBoolEntry("StoreOptionsForCSVExportDialog", false)) {
00208         // load defaults:
00209         m_alwaysUseCheckBox->setChecked(true);
00210         QString s = readEntry("DefaultDelimiterForExportingCSVFiles", defaultDelimiter());
00211         if (!s.isEmpty())
00212             m_delimiterWidget->setDelimiter(s);
00213         s = readEntry("DefaultTextQuoteForExportingCSVFiles", defaultTextQuote());
00214         m_textQuote->setTextQuote(s); //will be invaliudated here, so not a problem
00215         s = readEntry("DefaultEncodingForExportingCSVFiles");
00216         if (!s.isEmpty())
00217             m_characterEncodingCombo->setSelectedEncoding(s);
00218         m_addColumnNamesCheckBox->setChecked(
00219             readBoolEntry("AddColumnNamesForExportingCSVFiles", true) );
00220     }
00221 
00222     updateGeometry();
00223 
00224     // -keep widths equal on page #2:
00225     int width = QMAX( m_infoLblFrom->leftLabel()->sizeHint().width(),
00226         m_infoLblTo->leftLabel()->sizeHint().width());
00227     m_infoLblFrom->leftLabel()->setFixedWidth(width);
00228     m_infoLblTo->leftLabel()->setFixedWidth(width);
00229 }
00230 
00231 KexiCSVExportWizard::~KexiCSVExportWizard()
00232 {
00233     delete m_tableOrQuery;
00234 }
00235 
00236 bool KexiCSVExportWizard::cancelled() const
00237 {
00238     return m_cancelled;
00239 }
00240 
00241 void KexiCSVExportWizard::showPage ( QWidget * page )
00242 {
00243     if (page == m_fileSavePage) {
00244         m_fileSavePage->setFocus();
00245     }
00246     else if (page==m_exportOptionsPage) {
00247         if (m_options.mode==KexiCSVExport::File)
00248             m_infoLblTo->setFileName( m_fileSavePage->currentFileName() );
00249         QString text = m_tableOrQuery->captionOrName();
00250         if (!m_rowCountDetermined) {
00251             //do this costly operation only once
00252             m_rowCount = KexiDB::rowCount(*m_tableOrQuery);
00253             m_rowCountDetermined = true;
00254         }
00255         int columns = KexiDB::fieldCount(*m_tableOrQuery);
00256         text += "\n";
00257         if (m_rowCount>0)
00258             text += i18n("(rows: %1, columns: %2)").arg(m_rowCount).arg(columns);
00259         else
00260             text += i18n("(columns: %1)").arg(columns);
00261         m_infoLblFrom->setLabelText(text);
00262         QFontMetrics fm(m_infoLblFrom->fileNameLabel()->font());
00263         m_infoLblFrom->fileNameLabel()->setFixedHeight( fm.height() * 2 + fm.lineSpacing() );
00264         if (m_defaultsBtn)
00265             m_defaultsBtn->show();
00266     }
00267 
00268     if (page!=m_exportOptionsPage) {
00269         if (m_defaultsBtn)
00270             m_defaultsBtn->hide();
00271     }
00272 
00273     KWizard::showPage(page);
00274 }
00275 
00276 void KexiCSVExportWizard::next()
00277 {
00278     if (currentPage() == m_fileSavePage) {
00279         if (!m_fileSavePage->checkFileName())
00280             return;
00281         KWizard::next();
00282         finishButton()->setFocus();
00283         return;
00284     }
00285     KWizard::next();
00286 }
00287 
00288 void KexiCSVExportWizard::done(int result)
00289 {
00290     if (QDialog::Accepted == result) {
00291         if (m_fileSavePage)
00292             m_options.fileName = m_fileSavePage->currentFileName();
00293         m_options.delimiter = m_delimiterWidget->delimiter();
00294         m_options.textQuote = m_textQuote->textQuote();
00295         m_options.addColumnNames = m_addColumnNamesCheckBox->isChecked();
00296         if (!KexiCSVExport::exportData(*m_tableOrQuery, m_options))
00297             return;
00298     }
00299     else if (QDialog::Rejected == result) {
00300         //nothing to do
00301     }
00302 
00303     //store options
00304     kapp->config()->setGroup("ImportExport");
00305     if (m_options.mode!=KexiCSVExport::Clipboard)
00306         writeEntry("ShowOptionsInCSVExportDialog", m_exportOptionsSection->isVisible());
00307     const bool store = m_alwaysUseCheckBox->isChecked();
00308     writeEntry("StoreOptionsForCSVExportDialog", store);
00309     // only save if an option differs from default
00310 
00311     if (store && m_delimiterWidget->delimiter()!=defaultDelimiter())
00312         writeEntry("DefaultDelimiterForExportingCSVFiles", m_delimiterWidget->delimiter());
00313     else
00314         deleteEntry("DefaultDelimiterForExportingCSVFiles");
00315     if (store && m_textQuote->textQuote()!=defaultTextQuote())
00316         writeEntry("DefaultTextQuoteForExportingCSVFiles", m_textQuote->textQuote());
00317     else
00318         deleteEntry("DefaultTextQuoteForExportingCSVFiles");
00319     if (store && !m_characterEncodingCombo->defaultEncodingSelected())
00320         writeEntry("DefaultEncodingForExportingCSVFiles", m_characterEncodingCombo->selectedEncoding());
00321     else
00322         deleteEntry("DefaultEncodingForExportingCSVFiles");
00323     if (store && !m_addColumnNamesCheckBox->isChecked())
00324         writeEntry("AddColumnNamesForExportingCSVFiles", m_addColumnNamesCheckBox->isChecked());
00325     else
00326         deleteEntry("AddColumnNamesForExportingCSVFiles");
00327 
00328     KWizard::done(result);
00329 }
00330 
00331 void KexiCSVExportWizard::slotShowOptionsButtonClicked()
00332 {
00333     if (m_exportOptionsSection->isVisible()) {
00334         m_showOptionsButton->setText(i18n("Show Options >>"));
00335         m_exportOptionsSection->hide();
00336         m_alwaysUseCheckBox->hide();
00337         if (m_defaultsBtn)
00338             m_defaultsBtn->hide();
00339     }
00340     else {
00341         m_showOptionsButton->setText(i18n("Hide Options <<"));
00342         m_exportOptionsSection->show();
00343         m_alwaysUseCheckBox->show();
00344         if (m_defaultsBtn)
00345             m_defaultsBtn->show();
00346     }
00347 }
00348 
00349 void KexiCSVExportWizard::layOutButtonRow( QHBoxLayout * layout )
00350 {
00351     QWizard::layOutButtonRow( layout );
00352 
00353     //find the last sublayout
00354     QLayout *l = 0;
00355     for (QLayoutIterator lit( layout->iterator() ); lit.current(); ++lit)
00356         l = lit.current()->layout();
00357     if (dynamic_cast<QBoxLayout*>(l)) {
00358         if (!m_defaultsBtn) {
00359             m_defaultsBtn = new KPushButton(i18n("Defaults"), this);
00360             QWidget::setTabOrder(backButton(), m_defaultsBtn);
00361             connect(m_defaultsBtn, SIGNAL(clicked()), this, SLOT(slotDefaultsButtonClicked()));
00362         }
00363         if (!m_exportOptionsSection->isVisible())
00364             m_defaultsBtn->hide();
00365         dynamic_cast<QBoxLayout*>(l)->insertWidget(0, m_defaultsBtn);
00366     }
00367 }
00368 
00369 void KexiCSVExportWizard::slotDefaultsButtonClicked()
00370 {
00371     m_delimiterWidget->setDelimiter(defaultDelimiter());
00372     m_textQuote->setTextQuote(defaultTextQuote());
00373     m_addColumnNamesCheckBox->setChecked(true);
00374     m_characterEncodingCombo->selectDefaultEncoding();
00375 }
00376 
00377 static QString convertKey(const char *key, KexiCSVExport::Mode mode)
00378 {
00379     QString _key(QString::fromLatin1(key));
00380     if (mode == KexiCSVExport::Clipboard) {
00381         _key.replace("Exporting", "Copying");
00382         _key.replace("Export", "Copy");
00383         _key.replace("CSVFiles", "CSVToClipboard");
00384     }
00385     return _key;
00386 }
00387 
00388 bool KexiCSVExportWizard::readBoolEntry(const char *key, bool defaultValue)
00389 {
00390     return kapp->config()->readBoolEntry(convertKey(key, m_options.mode), defaultValue);
00391 }
00392 
00393 QString KexiCSVExportWizard::readEntry(const char *key, const QString& defaultValue)
00394 {
00395     return kapp->config()->readEntry(convertKey(key, m_options.mode), defaultValue);
00396 }
00397 
00398 void KexiCSVExportWizard::writeEntry(const char *key, const QString& value)
00399 {
00400     kapp->config()->writeEntry(convertKey(key, m_options.mode), value);
00401 }
00402 
00403 void KexiCSVExportWizard::writeEntry(const char *key, bool value)
00404 {
00405     kapp->config()->writeEntry(convertKey(key, m_options.mode), value);
00406 }
00407 
00408 void KexiCSVExportWizard::deleteEntry(const char *key)
00409 {
00410     kapp->config()->deleteEntry(convertKey(key, m_options.mode));
00411 }
00412 
00413 QString KexiCSVExportWizard::defaultDelimiter() const
00414 {
00415     if (m_options.mode==KexiCSVExport::Clipboard) {
00416         if (!m_options.forceDelimiter.isEmpty())
00417             return m_options.forceDelimiter;
00418         else
00419             return KEXICSV_DEFAULT_CLIPBOARD_DELIMITER;
00420     }
00421     return KEXICSV_DEFAULT_FILE_DELIMITER;
00422 }
00423 
00424 QString KexiCSVExportWizard::defaultTextQuote() const
00425 {
00426     if (m_options.mode==KexiCSVExport::Clipboard)
00427         return KEXICSV_DEFAULT_CLIPBOARD_TEXT_QUOTE;
00428     return KEXICSV_DEFAULT_FILE_TEXT_QUOTE;
00429 }
00430 
00431 #include "kexicsvexportwizard.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys