23 #include <tqbuttongroup.h>
24 #include <tqcheckbox.h>
25 #include <tqcombobox.h>
28 #include <tqlineedit.h>
29 #include <tqpushbutton.h>
30 #include <tqradiobutton.h>
32 #include <tqtextcodec.h>
33 #include <tqtooltip.h>
35 #include <tdeapplication.h>
37 #include <kdialogbase.h>
38 #include <tdefiledialog.h>
39 #include <klineedit.h>
40 #include <tdelocale.h>
41 #include <kinputdialog.h>
42 #include <tdemessagebox.h>
43 #include <kprogress.h>
44 #include <kstandarddirs.h>
45 #include <kurlrequester.h>
47 #include "dateparser.h"
49 #include "csvimportdialog.h"
51 enum { Local = 0, Guess = 1, Latin1 = 2, Uni = 3, MSBug = 4, Codec = 5 };
53 CSVImportDialog::CSVImportDialog( TDEABC::AddressBook *ab, TQWidget *parent,
55 : KDialogBase( Plain, i18n (
"CSV Import Dialog" ), Ok | Cancel | User1 |
56 User2, Ok, parent, name, true, true ),
65 mTypeMap.insert( i18n(
"Undefined" ), Undefined );
66 mTypeMap.insert( TDEABC::Addressee::formattedNameLabel(), FormattedName );
67 mTypeMap.insert( TDEABC::Addressee::familyNameLabel(), FamilyName );
68 mTypeMap.insert( TDEABC::Addressee::givenNameLabel(), GivenName );
69 mTypeMap.insert( TDEABC::Addressee::additionalNameLabel(), AdditionalName );
70 mTypeMap.insert( TDEABC::Addressee::prefixLabel(), Prefix );
71 mTypeMap.insert( TDEABC::Addressee::suffixLabel(), Suffix );
72 mTypeMap.insert( TDEABC::Addressee::nickNameLabel(), NickName );
73 mTypeMap.insert( TDEABC::Addressee::birthdayLabel(), Birthday );
75 mTypeMap.insert( TDEABC::Addressee::homeAddressStreetLabel(), HomeAddressStreet );
76 mTypeMap.insert( TDEABC::Addressee::homeAddressLocalityLabel(),
77 HomeAddressLocality );
78 mTypeMap.insert( TDEABC::Addressee::homeAddressRegionLabel(), HomeAddressRegion );
79 mTypeMap.insert( TDEABC::Addressee::homeAddressPostalCodeLabel(),
80 HomeAddressPostalCode );
81 mTypeMap.insert( TDEABC::Addressee::homeAddressCountryLabel(),
83 mTypeMap.insert( TDEABC::Addressee::homeAddressLabelLabel(), HomeAddressLabel );
85 mTypeMap.insert( TDEABC::Addressee::businessAddressStreetLabel(),
86 BusinessAddressStreet );
87 mTypeMap.insert( TDEABC::Addressee::businessAddressLocalityLabel(),
88 BusinessAddressLocality );
89 mTypeMap.insert( TDEABC::Addressee::businessAddressRegionLabel(),
90 BusinessAddressRegion );
91 mTypeMap.insert( TDEABC::Addressee::businessAddressPostalCodeLabel(),
92 BusinessAddressPostalCode );
93 mTypeMap.insert( TDEABC::Addressee::businessAddressCountryLabel(),
94 BusinessAddressCountry );
95 mTypeMap.insert( TDEABC::Addressee::businessAddressLabelLabel(),
96 BusinessAddressLabel );
98 mTypeMap.insert( TDEABC::Addressee::homePhoneLabel(), HomePhone );
99 mTypeMap.insert( TDEABC::Addressee::businessPhoneLabel(), BusinessPhone );
100 mTypeMap.insert( TDEABC::Addressee::mobilePhoneLabel(), MobilePhone );
101 mTypeMap.insert( TDEABC::Addressee::homeFaxLabel(), HomeFax );
102 mTypeMap.insert( TDEABC::Addressee::businessFaxLabel(), BusinessFax );
103 mTypeMap.insert( TDEABC::Addressee::carPhoneLabel(), CarPhone );
104 mTypeMap.insert( TDEABC::Addressee::isdnLabel(), Isdn );
105 mTypeMap.insert( TDEABC::Addressee::pagerLabel(), Pager );
106 mTypeMap.insert( TDEABC::Addressee::emailLabel(), Email );
107 mTypeMap.insert( TDEABC::Addressee::mailerLabel(), Mailer );
108 mTypeMap.insert( TDEABC::Addressee::titleLabel(), Title );
109 mTypeMap.insert( TDEABC::Addressee::roleLabel(), Role );
110 mTypeMap.insert( TDEABC::Addressee::organizationLabel(), Organization );
111 mTypeMap.insert( TDEABC::Addressee::noteLabel(), Note );
112 mTypeMap.insert( TDEABC::Addressee::urlLabel(), URL );
114 mCustomCounter = mTypeMap.count();
115 int count = mCustomCounter;
117 TDEABC::Field::List fields = mAddressBook->fields( TDEABC::Field::CustomCategory );
118 TDEABC::Field::List::Iterator it;
119 for ( it = fields.begin(); it != fields.end(); ++it, ++count )
120 mTypeMap.insert( (*it)->label(), count );
124 connect( mDelimiterBox, TQ_SIGNAL( clicked(
int ) ),
125 this, TQ_SLOT( delimiterClicked(
int ) ) );
126 connect( mDelimiterEdit, TQ_SIGNAL( returnPressed() ),
127 this, TQ_SLOT( returnPressed() ) );
128 connect( mDelimiterEdit, TQ_SIGNAL( textChanged (
const TQString& ) ),
129 this, TQ_SLOT( textChanged (
const TQString& ) ) );
130 connect( mComboLine, TQ_SIGNAL( activated(
const TQString& ) ),
131 this, TQ_SLOT( lineSelected(
const TQString& ) ) );
132 connect( mComboQuote, TQ_SIGNAL( activated(
const TQString& ) ),
133 this, TQ_SLOT( textquoteSelected(
const TQString& ) ) );
134 connect( mIgnoreDuplicates, TQ_SIGNAL( stateChanged(
int ) ),
135 this, TQ_SLOT( ignoreDuplicatesChanged(
int ) ) );
136 connect( mCodecCombo, TQ_SIGNAL( activated(
const TQString& ) ),
137 this, TQ_SLOT( codecChanged() ) );
139 connect( mUrlRequester, TQ_SIGNAL( returnPressed(
const TQString& ) ),
140 this, TQ_SLOT( setFile(
const TQString& ) ) );
141 connect( mUrlRequester, TQ_SIGNAL( urlSelected(
const TQString& ) ),
142 this, TQ_SLOT( setFile(
const TQString& ) ) );
143 connect( mUrlRequester->lineEdit(), TQ_SIGNAL( textChanged (
const TQString& ) ),
144 this, TQ_SLOT( urlChanged(
const TQString& ) ) );
146 connect(
this, TQ_SIGNAL( user1Clicked() ),
147 this, TQ_SLOT( applyTemplate() ) );
149 connect(
this, TQ_SIGNAL( user2Clicked() ),
150 this, TQ_SLOT( saveTemplate() ) );
153 CSVImportDialog::~CSVImportDialog()
158 TDEABC::AddresseeList CSVImportDialog::contacts()
const
160 DateParser dateParser( mDatePatternEdit->text() );
161 TDEABC::AddresseeList contacts;
163 KProgressDialog progressDialog( mPage );
164 progressDialog.setAutoClose(
true );
165 progressDialog.progressBar()->setTotalSteps( mTable->numRows() );
166 progressDialog.setLabel( i18n(
"Importing contacts" ) );
167 progressDialog.show();
169 kapp->processEvents();
171 for (
int row = 1; row < mTable->numRows(); ++row ) {
173 bool emptyRow =
true;
174 TDEABC::Address addrHome( TDEABC::Address::Home );
175 TDEABC::Address addrWork( TDEABC::Address::Work );
176 for (
int col = 0; col < mTable->numCols(); ++col ) {
177 TQComboTableItem *item =
static_cast<TQComboTableItem*
>( mTable->item( 0,
180 kdError() <<
"ERROR: item cast failed" << endl;
184 TQString value = mTable->text( row, col );
185 if ( 1 == row &&
static_cast<TQTableItem *
>(item)->text() == value )
189 if ( !value.isEmpty() )
192 switch ( posToType( item->currentItem() ) ) {
197 a.setFormattedName( value );
200 a.setGivenName( value );
203 a.setFamilyName( value );
206 a.setAdditionalName( value );
209 a.setPrefix( value );
212 a.setSuffix( value );
215 a.setNickName( value );
218 a.setBirthday( dateParser.parse( value ) );
221 if ( !value.isEmpty() )
222 a.insertEmail( value,
true );
231 a.setMailer( value );
234 a.setUrl( KURL( value ) );
237 a.setOrganization( value );
240 a.setNote( a.note() + value +
"\n" );
244 if ( !value.isEmpty() ) {
245 TDEABC::PhoneNumber number( value, TDEABC::PhoneNumber::Home );
246 a.insertPhoneNumber( number );
250 if ( !value.isEmpty() ) {
251 TDEABC::PhoneNumber number( value, TDEABC::PhoneNumber::Work );
252 a.insertPhoneNumber( number );
256 if ( !value.isEmpty() ) {
257 TDEABC::PhoneNumber number( value, TDEABC::PhoneNumber::Cell );
258 a.insertPhoneNumber( number );
262 if ( !value.isEmpty() ) {
263 TDEABC::PhoneNumber number( value, TDEABC::PhoneNumber::Home |
264 TDEABC::PhoneNumber::Fax );
265 a.insertPhoneNumber( number );
269 if ( !value.isEmpty() ) {
270 TDEABC::PhoneNumber number( value, TDEABC::PhoneNumber::Work |
271 TDEABC::PhoneNumber::Fax );
272 a.insertPhoneNumber( number );
276 if ( !value.isEmpty() ) {
277 TDEABC::PhoneNumber number( value, TDEABC::PhoneNumber::Car );
278 a.insertPhoneNumber( number );
282 if ( !value.isEmpty() ) {
283 TDEABC::PhoneNumber number( value, TDEABC::PhoneNumber::Isdn );
284 a.insertPhoneNumber( number );
288 if ( !value.isEmpty() ) {
289 TDEABC::PhoneNumber number( value, TDEABC::PhoneNumber::Pager );
290 a.insertPhoneNumber( number );
294 case HomeAddressStreet:
295 addrHome.setStreet( value );
297 case HomeAddressLocality:
298 addrHome.setLocality( value );
300 case HomeAddressRegion:
301 addrHome.setRegion( value );
303 case HomeAddressPostalCode:
304 addrHome.setPostalCode( value );
306 case HomeAddressCountry:
307 addrHome.setCountry( value );
309 case HomeAddressLabel:
310 addrHome.setLabel( value );
313 case BusinessAddressStreet:
314 addrWork.setStreet( value );
316 case BusinessAddressLocality:
317 addrWork.setLocality( value );
319 case BusinessAddressRegion:
320 addrWork.setRegion( value );
322 case BusinessAddressPostalCode:
323 addrWork.setPostalCode( value );
325 case BusinessAddressCountry:
326 addrWork.setCountry( value );
328 case BusinessAddressLabel:
329 addrWork.setLabel( value );
332 TDEABC::Field::List fields = mAddressBook->fields( TDEABC::Field::CustomCategory );
333 TDEABC::Field::List::Iterator it;
336 for ( it = fields.begin(); it != fields.end(); ++it ) {
337 if ( counter == (
int)( posToType( item->currentItem() ) - mCustomCounter ) ) {
338 (*it)->setValue( a, value );
347 kapp->processEvents();
349 if ( progressDialog.wasCancelled() )
350 return TDEABC::AddresseeList();
352 progressDialog.progressBar()->advance( 1 );
354 if ( !addrHome.isEmpty() )
355 a.insertAddress( addrHome );
356 if ( !addrWork.isEmpty() )
357 a.insertAddress( addrWork );
359 if ( !emptyRow && !a.isEmpty() )
360 contacts.append( a );
366 void CSVImportDialog::initGUI()
370 TQGridLayout *layout =
new TQGridLayout( mPage, 1, 1, marginHint(),
372 TQHBoxLayout *hbox =
new TQHBoxLayout();
373 hbox->setSpacing( spacingHint() );
375 TQLabel *label =
new TQLabel( i18n(
"File to import:" ), mPage );
376 hbox->addWidget( label );
378 mUrlRequester =
new KURLRequester( mPage );
379 mUrlRequester->setFilter(
"*.csv" );
380 hbox->addWidget( mUrlRequester );
382 layout->addMultiCellLayout( hbox, 0, 0, 0, 4 );
385 mDelimiterBox =
new TQButtonGroup( i18n(
"Delimiter" ), mPage );
386 mDelimiterBox->setColumnLayout( 0, TQt::Vertical );
387 mDelimiterBox->layout()->setSpacing( spacingHint() );
388 mDelimiterBox->layout()->setMargin( marginHint() );
389 TQGridLayout *delimiterLayout =
new TQGridLayout( mDelimiterBox->layout() );
390 delimiterLayout->setAlignment( TQt::AlignTop );
391 layout->addMultiCellWidget( mDelimiterBox, 1, 4, 0, 0 );
393 mRadioComma =
new TQRadioButton( i18n(
"Comma" ), mDelimiterBox );
394 mRadioComma->setChecked(
true );
395 delimiterLayout->addWidget( mRadioComma, 0, 0 );
397 mRadioSemicolon =
new TQRadioButton( i18n(
"Semicolon" ), mDelimiterBox );
398 delimiterLayout->addWidget( mRadioSemicolon, 0, 1 );
400 mRadioTab =
new TQRadioButton( i18n(
"Tabulator" ), mDelimiterBox );
401 delimiterLayout->addWidget( mRadioTab, 1, 0 );
403 mRadioSpace =
new TQRadioButton( i18n(
"Space" ), mDelimiterBox );
404 delimiterLayout->addWidget( mRadioSpace, 1, 1 );
406 mRadioOther =
new TQRadioButton( i18n(
"Other" ), mDelimiterBox );
407 delimiterLayout->addWidget( mRadioOther, 0, 2 );
409 mDelimiterEdit =
new TQLineEdit( mDelimiterBox );
410 delimiterLayout->addWidget( mDelimiterEdit, 1, 2 );
412 mComboLine =
new TQComboBox(
false, mPage );
413 mComboLine->insertItem( i18n(
"1" ) );
414 layout->addWidget( mComboLine, 2, 3 );
416 mComboQuote =
new TQComboBox(
false, mPage );
417 mComboQuote->insertItem( i18n(
"\"" ), 0 );
418 mComboQuote->insertItem( i18n(
"'" ), 1 );
419 mComboQuote->insertItem( i18n(
"None" ), 2 );
420 layout->addWidget( mComboQuote, 2, 2 );
422 mDatePatternEdit =
new TQLineEdit( mPage );
423 mDatePatternEdit->setText(
"Y-M-D" );
424 TQToolTip::add( mDatePatternEdit, i18n(
"<ul><li>y: year with 2 digits</li>"
425 "<li>Y: year with 4 digits</li>"
426 "<li>m: month with 1 or 2 digits</li>"
427 "<li>M: month with 2 digits</li>"
428 "<li>d: day with 1 or 2 digits</li>"
429 "<li>D: day with 2 digits</li></ul>" ) );
430 layout->addWidget( mDatePatternEdit, 2, 4 );
432 label =
new TQLabel( i18n(
"Start at line:" ), mPage );
433 layout->addWidget( label, 1, 3 );
435 label =
new TQLabel( i18n(
"Textquote:" ), mPage );
436 layout->addWidget( label, 1, 2 );
438 label =
new TQLabel( i18n(
"Date format:" ), mPage );
439 layout->addWidget( label, 1, 4 );
441 mIgnoreDuplicates =
new TQCheckBox( mPage );
442 mIgnoreDuplicates->setText( i18n(
"Ignore duplicate delimiters" ) );
443 layout->addMultiCellWidget( mIgnoreDuplicates, 3, 3, 2, 4 );
445 mCodecCombo =
new TQComboBox( mPage );
446 layout->addMultiCellWidget( mCodecCombo, 4, 4, 2, 4 );
448 mTable =
new TQTable( 0, 0, mPage );
449 mTable->setSelectionMode( TQTable::NoSelection );
450 mTable->horizontalHeader()->hide();
451 layout->addMultiCellWidget( mTable, 5, 5, 0, 4 );
453 setButtonText( User1, i18n(
"Apply Template..." ) );
454 setButtonText( User2, i18n(
"Save Template..." ) );
456 enableButtonOK(
false );
457 actionButton( User1 )->setEnabled(
false );
458 actionButton( User2 )->setEnabled(
false );
463 void CSVImportDialog::fillTable()
466 bool lastCharDelimiter =
false;
467 bool ignoreDups = mIgnoreDuplicates->isChecked();
468 enum { S_START, S_QUOTED_FIELD, S_MAYBE_END_OF_QUOTED_FIELD, S_END_OF_QUOTED_FIELD,
469 S_MAYBE_NORMAL_FIELD, S_NORMAL_FIELD } state = S_START;
476 for ( column = 0; column < mTable->numCols(); ++column ) {
477 TQComboTableItem *item =
static_cast<TQComboTableItem*
>( mTable->item( 0,
479 if ( !item || mClearTypeStore )
480 mTypeStore.append( typeToPos( Undefined ) );
482 mTypeStore.append( item->currentItem() );
489 TQTextStream inputStream( mFileArray, IO_ReadOnly );
492 int code = mCodecCombo->currentItem();
494 inputStream.setEncoding( TQTextStream::Locale );
495 else if ( code >= Codec )
496 inputStream.setCodec( mCodecs.at( code - Codec ) );
497 else if ( code == Uni )
498 inputStream.setEncoding( TQTextStream::Unicode );
499 else if ( code == MSBug )
500 inputStream.setEncoding( TQTextStream::UnicodeReverse );
501 else if ( code == Latin1 )
502 inputStream.setEncoding( TQTextStream::Latin1 );
503 else if ( code == Guess ) {
504 TQTextCodec* codec = TQTextCodec::codecForContent( mFileArray.data(), mFileArray.size() );
506 KMessageBox::information(
this, i18n(
"Using codec '%1'" ).arg( codec->name() ), i18n(
"Encoding" ) );
507 inputStream.setCodec( codec );
512 while ( !inputStream.atEnd() ) {
515 if ( x ==
'\r' ) inputStream >> x;
519 if ( x == mTextQuote ) {
520 state = S_QUOTED_FIELD;
521 }
else if ( x == mDelimiter ) {
522 if ( ( ignoreDups ==
false ) || ( lastCharDelimiter ==
false ) )
524 lastCharDelimiter =
true;
525 }
else if ( x ==
'\n' ) {
530 state = S_MAYBE_NORMAL_FIELD;
533 case S_QUOTED_FIELD :
534 if ( x == mTextQuote ) {
535 state = S_MAYBE_END_OF_QUOTED_FIELD;
536 }
else if ( x ==
'\n' && mTextQuote.isNull() ) {
537 setText( row - mStartLine + 1, column, field );
543 if ( ( ignoreDups ==
false ) || ( lastCharDelimiter ==
false ) )
545 lastCharDelimiter =
true;
552 case S_MAYBE_END_OF_QUOTED_FIELD :
553 if ( x == mTextQuote ) {
555 state = S_QUOTED_FIELD;
556 }
else if ( x == mDelimiter || x ==
'\n' ) {
557 setText( row - mStartLine + 1, column, field );
563 if ( ( ignoreDups ==
false ) || ( lastCharDelimiter ==
false ) )
565 lastCharDelimiter =
true;
569 state = S_END_OF_QUOTED_FIELD;
572 case S_END_OF_QUOTED_FIELD :
573 if ( x == mDelimiter || x ==
'\n' ) {
574 setText( row - mStartLine + 1, column, field );
580 if ( ( ignoreDups ==
false ) || ( lastCharDelimiter ==
false ) )
582 lastCharDelimiter =
true;
586 state = S_END_OF_QUOTED_FIELD;
589 case S_MAYBE_NORMAL_FIELD :
590 if ( x == mTextQuote ) {
592 state = S_QUOTED_FIELD;
595 case S_NORMAL_FIELD :
596 if ( x == mDelimiter || x ==
'\n' ) {
597 setText( row - mStartLine + 1, column, field );
603 if ( ( ignoreDups ==
false ) || ( lastCharDelimiter ==
false ) )
605 lastCharDelimiter =
true;
612 if ( x != mDelimiter )
613 lastCharDelimiter =
false;
615 if ( column > maxColumn )
620 if ( field.length() > 0 ) {
621 setText( row - mStartLine + 1, column, field );
626 adjustRows( row - mStartLine );
627 mTable->setNumCols( maxColumn );
629 for ( column = 0; column < mTable->numCols(); ++column ) {
630 TQComboTableItem *item =
new TQComboTableItem( mTable, mTypeMap.keys() );
631 mTable->setItem( 0, column, item );
632 if ( column < (
int)mTypeStore.count() )
633 item->setCurrentItem( mTypeStore[ column ] );
635 item->setCurrentItem( typeToPos( Undefined ) );
636 mTable->adjustColumn( column );
642 void CSVImportDialog::clearTable()
644 for (
int row = 0; row < mTable->numRows(); ++row )
645 for (
int column = 0; column < mTable->numCols(); ++column )
646 mTable->clearCell( row, column );
649 void CSVImportDialog::fillComboBox()
652 for (
int row = 1; row < mTable->numRows() + 1; ++row )
653 mComboLine->insertItem( TQString::number( row ), row - 1 );
656 void CSVImportDialog::reloadCodecs()
658 mCodecCombo->clear();
663 for (
int i = 0; ( codec = TQTextCodec::codecForIndex( i ) ); i++ )
664 mCodecs.append( codec );
666 mCodecCombo->insertItem( i18n(
"Local (%1)" ).arg( TQTextCodec::codecForLocale()->name() ), Local );
667 mCodecCombo->insertItem( i18n(
"[guess]" ), Guess );
668 mCodecCombo->insertItem( i18n(
"Latin1" ), Latin1 );
669 mCodecCombo->insertItem( i18n(
"Unicode" ), Uni );
670 mCodecCombo->insertItem( i18n(
"Microsoft Unicode" ), MSBug );
672 for ( uint i = 0; i < mCodecs.count(); i++ )
673 mCodecCombo->insertItem( mCodecs.at( i )->name(), Codec + i );
676 void CSVImportDialog::setText(
int row,
int col,
const TQString& text )
681 if ( mTable->numRows() < row ) {
682 mTable->setNumRows( row + 5000 );
686 if ( mTable->numCols() < col )
687 mTable->setNumCols( col + 50 );
689 mTable->setText( row - 1, col - 1, text );
695 void CSVImportDialog::adjustRows(
int rows )
698 mTable->setNumRows( rows );
703 void CSVImportDialog::resizeColumns()
705 TQFontMetrics fm = fontMetrics();
708 TQMap<TQString, uint>::ConstIterator it;
709 for ( it = mTypeMap.begin(); it != mTypeMap.end(); ++it ) {
710 width = TQMAX( width, fm.width( it.key() ) );
713 for (
int i = 0; i < mTable->numCols(); ++i )
714 mTable->setColumnWidth( i, TQMAX( width + 15, mTable->columnWidth( i ) ) );
717 void CSVImportDialog::returnPressed()
719 if ( mDelimiterBox->id( mDelimiterBox->selected() ) != 4 )
722 mDelimiter = mDelimiterEdit->text();
726 void CSVImportDialog::textChanged (
const TQString& )
728 mRadioOther->setChecked (
true );
729 delimiterClicked( 4 );
732 void CSVImportDialog::delimiterClicked(
int id )
739 mDelimiter = mDelimiterEdit->text();
755 void CSVImportDialog::textquoteSelected(
const TQString& mark )
757 if ( mComboQuote->currentItem() == 2 )
760 mTextQuote = mark[ 0 ];
765 void CSVImportDialog::lineSelected(
const TQString& line )
767 mStartLine = line.toInt() - 1;
771 void CSVImportDialog::slotOk()
773 bool assigned =
false;
775 for (
int column = 0; column < mTable->numCols(); ++column ) {
776 TQComboTableItem *item =
static_cast<TQComboTableItem*
>( mTable->item( 0,
778 if ( item && posToType( item->currentItem() ) != Undefined )
783 KDialogBase::slotOk();
785 KMessageBox::sorry(
this, i18n(
"You have to assign at least one column." ) );
788 void CSVImportDialog::applyTemplate()
790 TQMap<uint,int> columnMap;
791 TQMap<TQString, TQString> fileMap;
792 TQStringList templates;
795 TQStringList list = TDEGlobal::dirs()->findAllResources(
"data" , TQString( kapp->name() ) +
796 "/csv-templates/*.desktop",
true,
true );
798 for ( TQStringList::iterator it = list.begin(); it != list.end(); ++it )
800 KSimpleConfig config( *it,
true );
802 if ( !config.hasGroup(
"csv column map" ) )
805 config.setGroup(
"Misc" );
806 templates.append( config.readEntry(
"Name" ) );
807 fileMap.insert( config.readEntry(
"Name" ), *it );
813 tmp = KInputDialog::getItem( i18n(
"Template Selection" ),
814 i18n(
"Please select a template, that matches the CSV file:" ),
815 templates, 0,
false, &ok,
this );
820 KSimpleConfig config( fileMap[ tmp ],
true );
821 config.setGroup(
"General" );
822 mDatePatternEdit->setText( config.readEntry(
"DatePattern",
"Y-M-D" ) );
823 uint numColumns = config.readUnsignedNumEntry(
"Columns" );
824 mDelimiterEdit->setText( config.readEntry(
"DelimiterOther" ) );
825 mDelimiterBox->setButton( config.readNumEntry(
"DelimiterType" ) );
826 delimiterClicked( config.readNumEntry(
"DelimiterType" ) );
827 int quoteType = config.readNumEntry(
"QuoteType" );
828 mComboQuote->setCurrentItem( quoteType );
829 textquoteSelected( mComboQuote->currentText() );
832 config.setGroup(
"csv column map" );
833 for ( uint i = 0; i < numColumns; ++i ) {
834 int col = config.readNumEntry( TQString::number( i ) );
835 columnMap.insert( i, col );
839 for ( uint column = 0; column < columnMap.count(); ++column ) {
840 int type = columnMap[ column ];
841 TQComboTableItem *item =
static_cast<TQComboTableItem*
>( mTable->item( 0,
844 item->setCurrentItem( typeToPos( type ) );
848 void CSVImportDialog::saveTemplate()
850 TQString fileName = KFileDialog::getSaveFileName(
851 locateLocal(
"data", TQString( kapp->name() ) +
"/csv-templates/" ),
854 if ( fileName.isEmpty() )
857 if ( !fileName.contains(
".desktop" ) )
858 fileName +=
".desktop";
860 if( TQFileInfo(fileName).exists() ) {
861 if(KMessageBox::questionYesNo(
this, i18n(
"Do you want to overwrite file \"%1\"").arg(fileName) ) == KMessageBox::No)
864 TQString name = KInputDialog::getText( i18n(
"Template Name" ), i18n(
"Please enter a name for the template:" ) );
866 if ( name.isEmpty() )
869 TDEConfig config( fileName );
870 config.setGroup(
"General" );
871 config.writeEntry(
"DatePattern", mDatePatternEdit->text() );
872 config.writeEntry(
"Columns", mTable->numCols() );
873 config.writeEntry(
"DelimiterType", mDelimiterBox->id( mDelimiterBox->selected() ) );
874 config.writeEntry(
"DelimiterOther", mDelimiterEdit->text() );
875 config.writeEntry(
"QuoteType", mComboQuote->currentItem() );
877 config.setGroup(
"Misc" );
878 config.writeEntry(
"Name", name );
880 config.setGroup(
"csv column map" );
882 for (
int column = 0; column < mTable->numCols(); ++column ) {
883 TQComboTableItem *item =
static_cast<TQComboTableItem*
>( mTable->item( 0,
886 config.writeEntry( TQString::number( column ), posToType(
887 item->currentItem() ) );
889 config.writeEntry( TQString::number( column ), 0 );
895 TQString CSVImportDialog::getText(
int row,
int col )
897 return mTable->text( row, col );
900 uint CSVImportDialog::posToType(
int pos )
const
903 TQMap<TQString, uint>::ConstIterator it;
904 for ( it = mTypeMap.begin(); it != mTypeMap.end(); ++it, ++counter )
905 if ( counter == (uint)pos )
911 int CSVImportDialog::typeToPos( uint type )
const
914 TQMap<TQString, uint>::ConstIterator it;
915 for ( it = mTypeMap.begin(); it != mTypeMap.end(); ++it, ++counter )
916 if ( it.data() == type )
922 void CSVImportDialog::ignoreDuplicatesChanged(
int )
927 void CSVImportDialog::setFile(
const TQString &fileName )
929 if ( fileName.isEmpty() )
932 TQFile file( fileName );
933 if ( !file.open( IO_ReadOnly ) ) {
934 KMessageBox::sorry(
this, i18n(
"Cannot open input file." ) );
939 mFileArray = file.readAll();
942 mClearTypeStore =
true;
944 mTable->setNumCols( 0 );
945 mTable->setNumRows( 0 );
947 mClearTypeStore =
false;
952 void CSVImportDialog::urlChanged(
const TQString &file )
954 bool state = !file.isEmpty();
956 enableButtonOK( state );
957 actionButton( User1 )->setEnabled( state );
958 actionButton( User2 )->setEnabled( state );
961 void CSVImportDialog::codecChanged()
966 #include <csvimportdialog.moc>
This class parses the date out of a given string with the help of a pattern.