summaryrefslogtreecommitdiffstats
path: root/filters/kspread/csv/csvexportdialog.cpp
blob: fe1a7c80b9a6da23de67e2936498d262f67fbc9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* This file is part of the KDE project
   Copyright (C) 1999 David Faure <faure@kde.org>
   Copyright (C) 2004 Nicolas GOUTTE <goutte@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <csvexportdialog.h>
#include <exportdialogui.h>

#include <kspread_map.h>
#include <kspread_sheet.h>

#include <tqbuttongroup.h>
#include <tqcheckbox.h>
#include <tqcombobox.h>
#include <tqcursor.h>
#include <tqlabel.h>
#include <tqlineedit.h>
#include <tqlistview.h>
#include <tqptrlist.h>
#include <tqradiobutton.h>
#include <tqtextstream.h>
#include <tqtabwidget.h>
#include <tqtextcodec.h>
#include <tqvalidator.h>

#include <tdeapplication.h>
#include <tdeconfig.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <kcombobox.h>
#include <tdemessagebox.h>
#include <kcharsets.h>

using namespace KSpread;

CSVExportDialog::CSVExportDialog( TQWidget * parent )
  : KDialogBase( parent, 0, true, TQString(), Ok | Cancel, No, true ),
    m_dialog( new ExportDialogUI( this ) ),
    m_delimiter( "," ),
    m_textquote('"')
{
  kapp->restoreOverrideCursor();

  TQStringList encodings;
  encodings << i18n( "Descriptive encoding name", "Recommended ( %1 )" ).arg( "UTF-8" );
  encodings << i18n( "Descriptive encoding name", "Locale ( %1 )" ).arg( TQTextCodec::codecForLocale()->name() );
  encodings += TDEGlobal::charsets()->descriptiveEncodingNames();
  // Add a few non-standard encodings, which might be useful for text files
  const TQString description(i18n("Descriptive encoding name","Other ( %1 )"));
  encodings << description.arg("Apple Roman"); // Apple
  encodings << description.arg("IBM 850") << description.arg("IBM 866"); // MS DOS
  encodings << description.arg("CP 1258"); // Windows

  m_dialog->comboBoxEncoding->insertStringList(encodings);

  setButtonBoxOrientation (TQt::Vertical );

  setMainWidget(m_dialog);

  // Invalid 'Other' delimiters
  // - Quotes
  // - CR,LF,Vetical-tab,Formfeed,ASCII bel
  TQRegExp rx( "^[^\"'\r\n\v\f\a]{0,1}$" );
  m_delimiterValidator = new TQRegExpValidator( rx, TQT_TQOBJECT(m_dialog->m_delimiterBox) );
  m_dialog->m_delimiterEdit->setValidator( m_delimiterValidator );

  connect( m_dialog->m_delimiterBox, TQT_SIGNAL( clicked(int) ),
           this, TQT_SLOT( delimiterClicked( int ) ) );
  connect( m_dialog->m_delimiterEdit, TQT_SIGNAL( returnPressed() ),
           this, TQT_SLOT( returnPressed() ) );
  connect( m_dialog->m_delimiterEdit, TQT_SIGNAL( textChanged ( const TQString & ) ),
           this, TQT_SLOT(textChanged ( const TQString & ) ) );
  connect( m_dialog->m_comboQuote, TQT_SIGNAL( activated( const TQString & ) ),
           this, TQT_SLOT( textquoteSelected( const TQString & ) ) );
  connect( m_dialog->m_selectionOnly, TQT_SIGNAL( toggled( bool ) ),
           this, TQT_SLOT( selectionOnlyChanged( bool ) ) );

  loadSettings();
}

CSVExportDialog::~CSVExportDialog()
{
  saveSettings();
  kapp->setOverrideCursor(TQt::waitCursor);
  delete m_delimiterValidator;
}

void CSVExportDialog::loadSettings()
{
    TDEConfig *config = kapp->config();
    config->setGroup("CSVDialog Settings");
    m_textquote = config->readEntry("textquote", "\"")[0];
    m_delimiter = config->readEntry("delimiter", ",");
    const TQString codecText = config->readEntry("codec", "");
    bool selectionOnly = config->readBoolEntry("selectionOnly", false);
    const TQString sheetDelim = config->readEntry("sheetDelimiter", m_dialog->m_sheetDelimiter->text());
    bool delimAbove = config->readBoolEntry("sheetDelimiterAbove", false);
    const TQString eol = config->readEntry("eol", "\r\n");

    // update widgets
    if (!codecText.isEmpty()) {
      m_dialog->comboBoxEncoding->setCurrentText(codecText);
    }
    if (m_delimiter == ",") m_dialog->m_radioComma->setChecked(true);
    else if (m_delimiter == "\t") m_dialog->m_radioTab->setChecked(true);
    else if (m_delimiter == " ") m_dialog->m_radioSpace->setChecked(true);
    else if (m_delimiter == ";") m_dialog->m_radioSemicolon->setChecked(true);
    else {
        m_dialog->m_radioOther->setChecked(true);
        m_dialog->m_delimiterEdit->setText(m_delimiter);
    }
    m_dialog->m_comboQuote->setCurrentItem(m_textquote == '\'' ? 1
        : m_textquote == '"' ? 0 : 2);
    m_dialog->m_selectionOnly->setChecked(selectionOnly);
    m_dialog->m_sheetDelimiter->setText(sheetDelim);
    m_dialog->m_delimiterAboveAll->setChecked(delimAbove);
    if (eol == "\r\n") m_dialog->radioEndOfLineCRLF->setChecked(true);
    else if (eol == "\r") m_dialog->radioEndOfLineCR->setChecked(true);
    else m_dialog->radioEndOfLineLF->setChecked(true);
}

void CSVExportDialog::saveSettings()
{
    TDEConfig *config = kapp->config();
    config->setGroup("CSVDialog Settings");
    TQString q = m_textquote;
    config->writeEntry("textquote", q);
    config->writeEntry("delimiter", m_delimiter);
    config->writeEntry("codec", m_dialog->comboBoxEncoding->currentText());
    config->writeEntry("selectionOnly", exportSelectionOnly());
    config->writeEntry("sheetDelimiter", getSheetDelimiter());
    config->writeEntry("sheetDelimiterAbove", printAlwaysSheetDelimiter());
    config->writeEntry("eol", getEndOfLine());
    config->sync();
}

void CSVExportDialog::fillSheet( Map * map )
{
  m_dialog->m_sheetList->clear();
  TQCheckListItem * item;

  TQPtrListIterator<Sheet> it( map->sheetList() );
  for( ; it.current(); ++it )
  {
    item = new TQCheckListItem( m_dialog->m_sheetList,
                               it.current()->sheetName(),
                               TQCheckListItem::CheckBox );
    item->setOn(true);
    m_dialog->m_sheetList->insertItem( item );
  }

  m_dialog->m_sheetList->setSorting(0, true);
  m_dialog->m_sheetList->sort();
  m_dialog->m_sheetList->setSorting( -1 );
}

TQChar CSVExportDialog::getDelimiter() const
{
  return m_delimiter[0];
}

TQChar CSVExportDialog::getTextQuote() const
{
  return m_textquote;
}

bool CSVExportDialog::printAlwaysSheetDelimiter() const
{
  return m_dialog->m_delimiterAboveAll->isChecked();
}

TQString CSVExportDialog::getSheetDelimiter() const
{
  return m_dialog->m_sheetDelimiter->text();
}

bool CSVExportDialog::exportSheet(TQString const & sheetName) const
{
  for (TQListViewItem * item = m_dialog->m_sheetList->firstChild(); item; item = item->nextSibling())
  {
    if (((TQCheckListItem * ) item)->isOn())
    {
      if ( ((TQCheckListItem * ) item)->text() == sheetName )
        return true;
    }
  }
  return false;
}

void CSVExportDialog::slotOk()
{
  accept();
}

void CSVExportDialog::slotCancel()
{
  reject();
}

void CSVExportDialog::returnPressed()
{
  if ( m_dialog->m_delimiterBox->id( m_dialog->m_delimiterBox->selected() ) != 4 )
    return;

  m_delimiter = m_dialog->m_delimiterEdit->text();
}

void CSVExportDialog::textChanged ( const TQString & )
{

  if ( m_dialog->m_delimiterEdit->text().isEmpty() )
  {
    enableButtonOK( ! m_dialog->m_radioOther->isChecked() );
    return;
  }

  m_dialog->m_radioOther->setChecked ( true );
  delimiterClicked(4);
}

void CSVExportDialog::delimiterClicked( int id )
{
  enableButtonOK( true );

  //Erase "Other Delimiter" text box if the user has selected one of 
  //the standard options instead (comma, semicolon, tab or space)
  if (id != 4)
  	m_dialog->m_delimiterEdit->setText("");
  
  switch (id)
  {
    case 0: // comma
      m_delimiter = ",";
      break;
    case 1: // semicolon
      m_delimiter = ";";
      break;
    case 2: // tab
      m_delimiter = "\t";
      break;
    case 3: // space
      m_delimiter = " ";
      break;
    case 4: // other
      enableButtonOK( ! m_dialog->m_delimiterEdit->text().isEmpty() );
      m_delimiter = m_dialog->m_delimiterEdit->text();
      break;
  }
}

void CSVExportDialog::textquoteSelected( const TQString & mark )
{
  m_textquote = mark[0];
}

void CSVExportDialog::selectionOnlyChanged( bool on )
{
  m_dialog->m_sheetList->setEnabled( !on );
  m_dialog->m_delimiterLineBox->setEnabled( !on );

  if ( on )
    m_dialog->m_tabWidget->setCurrentPage( 1 );
}

bool CSVExportDialog::exportSelectionOnly() const
{
  return m_dialog->m_selectionOnly->isChecked();
}

TQTextCodec* CSVExportDialog::getCodec(void) const
{
    const TQString strCodec( TDEGlobal::charsets()->encodingForName( m_dialog->comboBoxEncoding->currentText() ) );
    kdDebug(30502) << "Encoding: " << strCodec << endl;

    bool ok = false;
    TQTextCodec* codec = TQTextCodec::codecForName( strCodec.utf8() );

    // If TQTextCodec has not found a valid encoding, so try with KCharsets.
    if ( codec )
    {
        ok = true;
    }
    else
    {
        codec = TDEGlobal::charsets()->codecForName( strCodec, ok );
    }

    // Still nothing?
    if ( !codec || !ok )
    {
        // Default: UTF-8
        kdWarning(30502) << "Cannot find encoding:" << strCodec << endl;
        // ### TODO: what parent to use?
        KMessageBox::error( 0, i18n("Cannot find encoding: %1").arg( strCodec ) );
        return 0;
    }

    return codec;
}

TQString CSVExportDialog::getEndOfLine(void) const
{
    TQString strReturn;
    if (m_dialog->radioEndOfLineLF==m_dialog->buttonGroupEndOfLine->selected())
        strReturn="\n";
    else if (m_dialog->radioEndOfLineCRLF==m_dialog->buttonGroupEndOfLine->selected())
        strReturn="\r\n";
    else if (m_dialog->radioEndOfLineCR==m_dialog->buttonGroupEndOfLine->selected())
        strReturn="\r";
    else
        strReturn="\n";

    return strReturn;
}

#include "csvexportdialog.moc"