Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #undef SC_DLLIMPLEMENTATION
22 :
23 : #include "global.hxx"
24 : #include "scresid.hxx"
25 : #include "impex.hxx"
26 : #include "scuiasciiopt.hxx"
27 : #include "asciiopt.hrc"
28 : #include <comphelper/string.hxx>
29 : #include <osl/thread.h>
30 : #include <rtl/tencinfo.h>
31 : #include <unotools/transliterationwrapper.hxx>
32 : #include "editutil.hxx"
33 :
34 : #include <optutil.hxx>
35 : #include <com/sun/star/uno/Any.hxx>
36 : #include <com/sun/star/uno/Sequence.hxx>
37 : #include "miscuno.hxx"
38 : #include <tools/urlobj.hxx>
39 :
40 : //! TODO make dynamic
41 : const SCSIZE ASCIIDLG_MAXROWS = MAXROWCOUNT;
42 :
43 :
44 : using namespace com::sun::star::uno;
45 :
46 :
47 : // Defines - CSV Import Preserve Options
48 : #define FIXED_WIDTH "FixedWidth"
49 : #define FROM_ROW "FromRow"
50 : #define CHAR_SET "CharSet"
51 : #define SEPARATORS "Separators"
52 : #define TEXT_SEPARATORS "TextSeparators"
53 : #define MERGE_DELIMITERS "MergeDelimiters"
54 : #define QUOTED_AS_TEXT "QuotedFieldAsText"
55 : #define DETECT_SPECIAL_NUM "DetectSpecialNumbers"
56 : #define LANGUAGE "Language"
57 : #define SEP_PATH "Office.Calc/Dialogs/CSVImport"
58 : #define SEP_PATH_CLPBRD "Office.Calc/Dialogs/ClipboardTextImport"
59 : #define SEP_PATH_TEXT2COL "Office.Calc/Dialogs/TextToColumnsImport"
60 :
61 0 : static void lcl_FillCombo( ComboBox& rCombo, const OUString& rList, sal_Unicode cSelect )
62 : {
63 : sal_Int32 i;
64 0 : sal_Int32 nCount = comphelper::string::getTokenCount(rList, '\t');
65 0 : for ( i=0; i<nCount; i+=2 )
66 0 : rCombo.InsertEntry( rList.getToken(i,'\t') );
67 :
68 0 : if ( cSelect )
69 : {
70 0 : OUString aStr;
71 0 : for ( i=0; i<nCount; i+=2 )
72 0 : if ( (sal_Unicode)rList.getToken(i+1,'\t').toInt32() == cSelect )
73 0 : aStr = rList.getToken(i,'\t');
74 0 : if (aStr.isEmpty())
75 0 : aStr = OUString(cSelect); // Ascii
76 :
77 0 : rCombo.SetText(aStr);
78 : }
79 0 : }
80 :
81 0 : static sal_Unicode lcl_CharFromCombo( ComboBox& rCombo, const OUString& rList )
82 : {
83 0 : sal_Unicode c = 0;
84 0 : OUString aStr = rCombo.GetText();
85 0 : if ( !aStr.isEmpty() )
86 : {
87 0 : sal_Int32 nCount = comphelper::string::getTokenCount(rList, '\t');
88 0 : for ( sal_Int32 i=0; i<nCount; i+=2 )
89 : {
90 0 : if ( ScGlobal::GetpTransliteration()->isEqual( aStr, rList.getToken(i,'\t') ) )
91 0 : c = (sal_Unicode)rList.getToken(i+1,'\t').toInt32();
92 : }
93 0 : if (!c && !aStr.isEmpty())
94 : {
95 0 : sal_Unicode cFirst = aStr[0];
96 : // #i24235# first try the first character of the string directly
97 0 : if( (aStr.getLength() == 1) || (cFirst < '0') || (cFirst > '9') )
98 0 : c = cFirst;
99 : else // keep old behaviour for compatibility (i.e. "39" -> "'")
100 0 : c = (sal_Unicode) aStr.toInt32(); // Ascii
101 : }
102 : }
103 0 : return c;
104 : }
105 :
106 0 : static void load_Separators( OUString &sFieldSeparators, OUString &sTextSeparators,
107 : bool &bMergeDelimiters, bool& bQuotedAsText, bool& bDetectSpecialNum,
108 : bool &bFixedWidth, sal_Int32 &nFromRow, sal_Int32 &nCharSet,
109 : sal_Int32& nLanguage, ScImportAsciiCall eCall )
110 : {
111 0 : Sequence<Any>aValues;
112 : const Any *pProperties;
113 0 : Sequence<OUString> aNames( eCall == SC_TEXTTOCOLUMNS ? 4 : 9 );
114 0 : OUString* pNames = aNames.getArray();
115 0 : OUString aSepPath;
116 0 : switch(eCall)
117 : {
118 : case SC_IMPORTFILE:
119 0 : aSepPath = SEP_PATH;
120 0 : break;
121 : case SC_PASTETEXT:
122 0 : aSepPath = SEP_PATH_CLPBRD;
123 0 : break;
124 : case SC_TEXTTOCOLUMNS:
125 : default:
126 0 : aSepPath = SEP_PATH_TEXT2COL;
127 0 : break;
128 : }
129 0 : ScLinkConfigItem aItem( aSepPath );
130 :
131 0 : pNames[0] = OUString( MERGE_DELIMITERS );
132 0 : pNames[1] = OUString( SEPARATORS );
133 0 : pNames[2] = OUString( TEXT_SEPARATORS );
134 0 : pNames[3] = OUString( FIXED_WIDTH );
135 0 : if (eCall != SC_TEXTTOCOLUMNS)
136 : {
137 0 : pNames[4] = OUString( FROM_ROW );
138 0 : pNames[5] = OUString( CHAR_SET );
139 0 : pNames[6] = OUString( QUOTED_AS_TEXT );
140 0 : pNames[7] = OUString( DETECT_SPECIAL_NUM );
141 0 : pNames[8] = OUString( LANGUAGE );
142 : }
143 0 : aValues = aItem.GetProperties( aNames );
144 0 : pProperties = aValues.getConstArray();
145 :
146 0 : if( pProperties[0].hasValue() )
147 0 : bMergeDelimiters = ScUnoHelpFunctions::GetBoolFromAny( pProperties[0] );
148 :
149 0 : if( pProperties[1].hasValue() )
150 0 : pProperties[1] >>= sFieldSeparators;
151 :
152 0 : if( pProperties[2].hasValue() )
153 0 : pProperties[2] >>= sTextSeparators;
154 :
155 0 : if( pProperties[3].hasValue() )
156 0 : bFixedWidth = ScUnoHelpFunctions::GetBoolFromAny( pProperties[3] );
157 :
158 0 : if (eCall != SC_TEXTTOCOLUMNS)
159 : {
160 0 : if( pProperties[4].hasValue() )
161 0 : pProperties[4] >>= nFromRow;
162 :
163 0 : if( pProperties[5].hasValue() )
164 0 : pProperties[5] >>= nCharSet;
165 :
166 0 : if ( pProperties[6].hasValue() )
167 0 : pProperties[6] >>= bQuotedAsText;
168 :
169 0 : if ( pProperties[7].hasValue() )
170 0 : pProperties[7] >>= bDetectSpecialNum;
171 :
172 0 : if ( pProperties[8].hasValue() )
173 0 : pProperties[8] >>= nLanguage;
174 0 : }
175 0 : }
176 :
177 0 : static void save_Separators(
178 : const OUString& maSeparators, const OUString& maTxtSep, bool bMergeDelimiters, bool bQuotedAsText,
179 : bool bDetectSpecialNum, bool bFixedWidth, sal_Int32 nFromRow,
180 : sal_Int32 nCharSet, sal_Int32 nLanguage, ScImportAsciiCall eCall )
181 : {
182 0 : OUString sFieldSeparators = OUString( maSeparators );
183 0 : OUString sTextSeparators = OUString( maTxtSep );
184 0 : Sequence<Any> aValues;
185 : Any *pProperties;
186 0 : Sequence<OUString> aNames( eCall == SC_TEXTTOCOLUMNS ? 4 : 9 );
187 0 : OUString* pNames = aNames.getArray();
188 0 : OUString aSepPath;
189 0 : switch(eCall)
190 : {
191 : case SC_IMPORTFILE:
192 0 : aSepPath = SEP_PATH;
193 0 : break;
194 : case SC_PASTETEXT:
195 0 : aSepPath = SEP_PATH_CLPBRD;
196 0 : break;
197 : case SC_TEXTTOCOLUMNS:
198 : default:
199 0 : aSepPath = SEP_PATH_TEXT2COL;
200 0 : break;
201 : }
202 0 : ScLinkConfigItem aItem( aSepPath );
203 :
204 0 : pNames[0] = OUString( MERGE_DELIMITERS );
205 0 : pNames[1] = OUString( SEPARATORS );
206 0 : pNames[2] = OUString( TEXT_SEPARATORS );
207 0 : pNames[3] = OUString( FIXED_WIDTH );
208 0 : if (eCall != SC_TEXTTOCOLUMNS)
209 : {
210 0 : pNames[4] = OUString( FROM_ROW );
211 0 : pNames[5] = OUString( CHAR_SET );
212 0 : pNames[6] = OUString( QUOTED_AS_TEXT );
213 0 : pNames[7] = OUString( DETECT_SPECIAL_NUM );
214 0 : pNames[8] = OUString( LANGUAGE );
215 : }
216 0 : aValues = aItem.GetProperties( aNames );
217 0 : pProperties = aValues.getArray();
218 0 : ScUnoHelpFunctions::SetBoolInAny( pProperties[0], bMergeDelimiters );
219 0 : pProperties[1] <<= sFieldSeparators;
220 0 : pProperties[2] <<= sTextSeparators;
221 0 : ScUnoHelpFunctions::SetBoolInAny( pProperties[3], bFixedWidth );
222 0 : if (eCall != SC_TEXTTOCOLUMNS)
223 : {
224 0 : pProperties[4] <<= nFromRow;
225 0 : pProperties[5] <<= nCharSet;
226 0 : pProperties[6] <<= static_cast<sal_Bool>(bQuotedAsText);
227 0 : pProperties[7] <<= static_cast<sal_Bool>(bDetectSpecialNum);
228 0 : pProperties[8] <<= nLanguage;
229 : }
230 :
231 0 : aItem.PutProperties(aNames, aValues);
232 0 : }
233 :
234 0 : ScImportAsciiDlg::ScImportAsciiDlg( Window* pParent, const OUString& aDatName,
235 : SvStream* pInStream, ScImportAsciiCall eCall ) :
236 : ModalDialog (pParent, "TextImportCsvDialog",
237 : "modules/scalc/ui/textimportcsv.ui"),
238 : mpDatStream ( pInStream ),
239 0 : mnStreamPos( pInStream ? pInStream->Tell() : 0 ),
240 :
241 : mpRowPosArray( NULL ),
242 : mnRowPosCount(0),
243 :
244 : aCharSetUser( ScResId( SCSTR_CHARSET_USER ) ),
245 : aColumnUser ( ScResId( SCSTR_COLUMN_USER ) ),
246 : aTextSepList( ScResId( SCSTR_TEXTSEP ) ),
247 : mcTextSep ( ScAsciiOptions::cDefaultTextSep ),
248 0 : meCall(eCall)
249 : {
250 0 : get(pFtCharSet, "textcharset");
251 0 : get(pLbCharSet, "charset");
252 0 : pLbCharSet->SetStyle(pLbCharSet->GetStyle() | WB_SORT);
253 0 : get(pFtCustomLang, "textlanguage");
254 0 : get(pLbCustomLang, "language");
255 0 : pLbCustomLang->SetStyle(pLbCustomLang->GetStyle() | WB_SORT);
256 0 : get(pFtRow, "textfromrow");
257 0 : get(pNfRow, "fromrow");
258 0 : get(pRbFixed, "tofixedwidth");
259 0 : get(pRbSeparated, "toseparatedby");
260 0 : get(pCkbTab, "tab");
261 0 : get(pCkbSemicolon, "semicolon");
262 0 : get(pCkbComma, "comma");
263 0 : get(pCkbSpace, "space");
264 0 : get(pCkbOther, "other");
265 0 : get(pEdOther, "inputother");
266 0 : get(pCkbAsOnce, "mergedelimiters");
267 0 : get(pFtTextSep, "texttextdelimiter");
268 0 : get(pCbTextSep, "textdelimiter");
269 0 : get(pCkbQuotedAsText, "quotedfieldastext");
270 0 : get(pCkbDetectNumber, "detectspecialnumbers");
271 0 : get(pFtType, "textcolumntype");
272 0 : get(pLbType, "columntype");
273 0 : get(mpTableBox, "scrolledwindowcolumntype");
274 :
275 0 : OUString aName = GetText();
276 0 : switch (meCall)
277 : {
278 : case SC_TEXTTOCOLUMNS:
279 0 : SetText( get<FixedText>("textalttitle")->GetText() );
280 0 : break;
281 : case SC_IMPORTFILE:
282 0 : aName += " - [" + aDatName + "]";
283 : default:
284 0 : SetText( aName );
285 : }
286 :
287 : // To be able to prefill the correct values based on the file extension
288 0 : bool bIsTSV = (aDatName.endsWithIgnoreAsciiCase(".tsv") || aDatName.endsWithIgnoreAsciiCase(".tab"));
289 :
290 : // Default options are set in officecfg/registry/schema/org/openoffice/Office/Calc.xcs
291 0 : OUString sFieldSeparators(",;\t");
292 0 : OUString sTextSeparators(mcTextSep);
293 0 : bool bMergeDelimiters = false;
294 0 : bool bFixedWidth = false;
295 0 : bool bQuotedFieldAsText = false;
296 0 : bool bDetectSpecialNum = true;
297 0 : sal_Int32 nFromRow = 1;
298 0 : sal_Int32 nCharSet = -1;
299 0 : sal_Int32 nLanguage = 0;
300 : load_Separators (sFieldSeparators, sTextSeparators, bMergeDelimiters,
301 0 : bQuotedFieldAsText, bDetectSpecialNum, bFixedWidth, nFromRow, nCharSet, nLanguage, meCall);
302 : // load from saved settings
303 0 : maFieldSeparators = OUString(sFieldSeparators);
304 :
305 0 : if( bMergeDelimiters && !bIsTSV )
306 0 : pCkbAsOnce->Check();
307 0 : if (bQuotedFieldAsText)
308 0 : pCkbQuotedAsText->Check();
309 0 : if (bDetectSpecialNum)
310 0 : pCkbDetectNumber->Check();
311 0 : if( bFixedWidth && !bIsTSV )
312 0 : pRbFixed->Check();
313 0 : if( nFromRow != 1 )
314 0 : pNfRow->SetValue( nFromRow );
315 :
316 0 : if ( bIsTSV )
317 0 : pCkbTab->Check();
318 : else
319 0 : SetSeparators(); // Set Separators in the dialog from maFieldSeparators (empty are not set)
320 :
321 : // Get Separators from the dialog (empty are set from default)
322 0 : maFieldSeparators = GetSeparators();
323 :
324 : // Clipboard is always Unicode, else detect.
325 0 : rtl_TextEncoding ePreselectUnicode = (meCall == SC_IMPORTFILE ?
326 0 : RTL_TEXTENCODING_DONTKNOW : RTL_TEXTENCODING_UNICODE);
327 : // Sniff for Unicode / not
328 0 : if( ePreselectUnicode == RTL_TEXTENCODING_DONTKNOW && mpDatStream )
329 : {
330 0 : Seek( 0 );
331 0 : mpDatStream->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW );
332 0 : sal_uLong nUniPos = mpDatStream->Tell();
333 0 : switch (nUniPos)
334 : {
335 : case 2:
336 0 : ePreselectUnicode = RTL_TEXTENCODING_UNICODE; // UTF-16
337 0 : break;
338 : case 3:
339 0 : ePreselectUnicode = RTL_TEXTENCODING_UTF8; // UTF-8
340 0 : break;
341 : case 0:
342 : {
343 : sal_uInt16 n;
344 0 : mpDatStream->ReadUInt16( n );
345 : // Assume that normal ASCII/ANSI/ISO/etc. text doesn't start with
346 : // control characters except CR,LF,TAB
347 0 : if ( (n & 0xff00) < 0x2000 )
348 : {
349 0 : switch ( n & 0xff00 )
350 : {
351 : case 0x0900 :
352 : case 0x0a00 :
353 : case 0x0d00 :
354 0 : break;
355 : default:
356 0 : ePreselectUnicode = RTL_TEXTENCODING_UNICODE; // UTF-16
357 : }
358 : }
359 0 : mpDatStream->Seek(0);
360 : }
361 0 : break;
362 : default:
363 : ; // nothing
364 : }
365 0 : mnStreamPos = mpDatStream->Tell();
366 : }
367 :
368 0 : pNfRow->SetModifyHdl( LINK( this, ScImportAsciiDlg, FirstRowHdl ) );
369 :
370 : // *** Separator characters ***
371 0 : lcl_FillCombo( *pCbTextSep, aTextSepList, mcTextSep );
372 0 : pCbTextSep->SetText( sTextSeparators );
373 :
374 0 : Link aSeparatorHdl =LINK( this, ScImportAsciiDlg, SeparatorHdl );
375 0 : pCbTextSep->SetSelectHdl( aSeparatorHdl );
376 0 : pCbTextSep->SetModifyHdl( aSeparatorHdl );
377 0 : pCkbTab->SetClickHdl( aSeparatorHdl );
378 0 : pCkbSemicolon->SetClickHdl( aSeparatorHdl );
379 0 : pCkbComma->SetClickHdl( aSeparatorHdl );
380 0 : pCkbAsOnce->SetClickHdl( aSeparatorHdl );
381 0 : pCkbQuotedAsText->SetClickHdl( aSeparatorHdl );
382 0 : pCkbDetectNumber->SetClickHdl( aSeparatorHdl );
383 0 : pCkbSpace->SetClickHdl( aSeparatorHdl );
384 0 : pCkbOther->SetClickHdl( aSeparatorHdl );
385 0 : pEdOther->SetModifyHdl( aSeparatorHdl );
386 :
387 : // *** text encoding ListBox ***
388 : // all encodings allowed, including Unicode, but subsets are excluded
389 0 : pLbCharSet->FillFromTextEncodingTable( true );
390 : // Insert one "SYSTEM" entry for compatibility in AsciiOptions and system
391 : // independent document linkage.
392 0 : pLbCharSet->InsertTextEncoding( RTL_TEXTENCODING_DONTKNOW, aCharSetUser );
393 : pLbCharSet->SelectTextEncoding( ePreselectUnicode == RTL_TEXTENCODING_DONTKNOW ?
394 0 : osl_getThreadTextEncoding() : ePreselectUnicode );
395 :
396 0 : if( nCharSet >= 0 && ePreselectUnicode == RTL_TEXTENCODING_DONTKNOW )
397 0 : pLbCharSet->SelectEntryPos( static_cast<sal_uInt16>(nCharSet) );
398 :
399 0 : SetSelectedCharSet();
400 0 : pLbCharSet->SetSelectHdl( LINK( this, ScImportAsciiDlg, CharSetHdl ) );
401 :
402 : pLbCustomLang->SetLanguageList(
403 0 : LANG_LIST_ALL | LANG_LIST_ONLY_KNOWN, false, false);
404 0 : pLbCustomLang->InsertLanguage(LANGUAGE_SYSTEM);
405 0 : pLbCustomLang->SelectLanguage(static_cast<LanguageType>(nLanguage), true);
406 :
407 : // *** column type ListBox ***
408 0 : sal_Int32 nCount = comphelper::string::getTokenCount(aColumnUser, ';');
409 0 : for (sal_Int32 i=0; i<nCount; i++)
410 0 : pLbType->InsertEntry( aColumnUser.getToken( i, ';' ) );
411 :
412 0 : pLbType->SetSelectHdl( LINK( this, ScImportAsciiDlg, LbColTypeHdl ) );
413 0 : pFtType->Disable();
414 0 : pLbType->Disable();
415 :
416 : // *** table box preview ***
417 0 : mpTableBox->Init();
418 0 : mpTableBox->SetUpdateTextHdl( LINK( this, ScImportAsciiDlg, UpdateTextHdl ) );
419 0 : mpTableBox->InitTypes( *pLbType );
420 0 : mpTableBox->SetColTypeHdl( LINK( this, ScImportAsciiDlg, ColTypeHdl ) );
421 :
422 0 : pRbSeparated->SetClickHdl( LINK( this, ScImportAsciiDlg, RbSepFixHdl ) );
423 0 : pRbFixed->SetClickHdl( LINK( this, ScImportAsciiDlg, RbSepFixHdl ) );
424 :
425 0 : SetupSeparatorCtrls();
426 0 : RbSepFixHdl( pRbFixed );
427 :
428 0 : UpdateVertical();
429 :
430 0 : mpTableBox->Execute( CSVCMD_NEWCELLTEXTS );
431 :
432 0 : pEdOther->SetAccessibleName(pCkbOther->GetText());
433 0 : pEdOther->SetAccessibleRelationLabeledBy(pCkbOther);
434 :
435 0 : if (meCall == SC_TEXTTOCOLUMNS)
436 : {
437 0 : pFtCharSet->Disable();
438 0 : pLbCharSet->Disable();
439 0 : pFtCustomLang->Disable();
440 0 : pLbCustomLang->SelectLanguage(LANGUAGE_SYSTEM);
441 0 : pLbCustomLang->Disable();
442 0 : pFtRow->Disable();
443 0 : pNfRow->Disable();
444 :
445 : // Quoted field as text option is not used for text-to-columns mode.
446 0 : pCkbQuotedAsText->Check(false);
447 0 : pCkbQuotedAsText->Disable();
448 :
449 : // Always detect special numbers for text-to-columns mode.
450 0 : pCkbDetectNumber->Check();
451 0 : pCkbDetectNumber->Disable();
452 0 : }
453 0 : }
454 :
455 :
456 0 : ScImportAsciiDlg::~ScImportAsciiDlg()
457 : {
458 0 : delete[] mpRowPosArray;
459 0 : }
460 :
461 :
462 0 : bool ScImportAsciiDlg::GetLine( sal_uLong nLine, OUString &rText )
463 : {
464 0 : if (nLine >= ASCIIDLG_MAXROWS || !mpDatStream)
465 0 : return false;
466 :
467 0 : bool bRet = true;
468 0 : bool bFixed = pRbFixed->IsChecked();
469 :
470 0 : if (!mpRowPosArray)
471 0 : mpRowPosArray = new sal_uLong[ASCIIDLG_MAXROWS + 2];
472 :
473 0 : if (!mnRowPosCount) // complete re-fresh
474 : {
475 0 : memset( mpRowPosArray, 0, sizeof(mpRowPosArray[0]) * (ASCIIDLG_MAXROWS+2));
476 :
477 0 : Seek(0);
478 0 : mpDatStream->StartReadingUnicodeText( mpDatStream->GetStreamCharSet() );
479 :
480 0 : mnStreamPos = mpDatStream->Tell();
481 0 : mpRowPosArray[mnRowPosCount] = mnStreamPos;
482 : }
483 :
484 0 : if (nLine >= mnRowPosCount)
485 : {
486 : // need to work out some more line information
487 0 : do
488 : {
489 0 : if (!Seek( mpRowPosArray[mnRowPosCount]) ||
490 0 : mpDatStream->GetError() != ERRCODE_NONE ||
491 0 : mpDatStream->IsEof())
492 : {
493 0 : bRet = false;
494 0 : break;
495 : }
496 0 : rText = ReadCsvLine(*mpDatStream, !bFixed, maFieldSeparators,
497 0 : mcTextSep);
498 0 : mnStreamPos = mpDatStream->Tell();
499 0 : mpRowPosArray[++mnRowPosCount] = mnStreamPos;
500 0 : } while (nLine >= mnRowPosCount &&
501 0 : mpDatStream->GetError() == ERRCODE_NONE &&
502 0 : !mpDatStream->IsEof());
503 0 : if (mpDatStream->IsEof() &&
504 0 : mnStreamPos == mpRowPosArray[mnRowPosCount-1])
505 : {
506 : // the very end, not even an empty line read
507 0 : bRet = false;
508 0 : --mnRowPosCount;
509 : }
510 : }
511 : else
512 : {
513 0 : Seek( mpRowPosArray[nLine]);
514 0 : rText = ReadCsvLine(*mpDatStream, !bFixed, maFieldSeparators, mcTextSep);
515 0 : mnStreamPos = mpDatStream->Tell();
516 : }
517 :
518 : // If the file content isn't unicode, ReadUniStringLine
519 : // may try to seek beyond the file's end and cause a CANTSEEK error
520 : // (depending on the stream type). The error code has to be cleared,
521 : // or further read operations (including non-unicode) will fail.
522 0 : if ( mpDatStream->GetError() == ERRCODE_IO_CANTSEEK )
523 0 : mpDatStream->ResetError();
524 :
525 0 : ScImportExport::EmbeddedNullTreatment( rText);
526 :
527 0 : return bRet;
528 : }
529 :
530 :
531 0 : void ScImportAsciiDlg::GetOptions( ScAsciiOptions& rOpt )
532 : {
533 0 : rOpt.SetCharSet( meCharSet );
534 0 : rOpt.SetCharSetSystem( mbCharSetSystem );
535 0 : rOpt.SetLanguage(pLbCustomLang->GetSelectLanguage());
536 0 : rOpt.SetFixedLen( pRbFixed->IsChecked() );
537 0 : rOpt.SetStartRow( (long)pNfRow->GetValue() );
538 0 : mpTableBox->FillColumnData( rOpt );
539 0 : if( pRbSeparated->IsChecked() )
540 : {
541 0 : rOpt.SetFieldSeps( GetSeparators() );
542 0 : rOpt.SetMergeSeps( pCkbAsOnce->IsChecked() );
543 0 : rOpt.SetTextSep( lcl_CharFromCombo( *pCbTextSep, aTextSepList ) );
544 : }
545 :
546 0 : rOpt.SetQuotedAsText(pCkbQuotedAsText->IsChecked());
547 0 : rOpt.SetDetectSpecialNumber(pCkbDetectNumber->IsChecked());
548 0 : }
549 :
550 0 : void ScImportAsciiDlg::SaveParameters()
551 : {
552 0 : save_Separators( maFieldSeparators, pCbTextSep->GetText(), pCkbAsOnce->IsChecked(),
553 0 : pCkbQuotedAsText->IsChecked(), pCkbDetectNumber->IsChecked(),
554 0 : pRbFixed->IsChecked(),
555 0 : static_cast<sal_Int32>(pNfRow->GetValue()),
556 : static_cast<sal_Int32>(pLbCharSet->GetSelectEntryPos()),
557 0 : static_cast<sal_Int32>(pLbCustomLang->GetSelectLanguage()), meCall );
558 0 : }
559 :
560 0 : void ScImportAsciiDlg::SetSeparators()
561 : {
562 : OString sString(OUStringToOString(maFieldSeparators,
563 0 : RTL_TEXTENCODING_MS_1252));
564 0 : const sal_Char *aSep = sString.getStr();
565 0 : sal_Int32 len = maFieldSeparators.getLength();
566 0 : for (int i = 0; i < len; ++i)
567 : {
568 0 : switch( aSep[i] )
569 : {
570 0 : case '\t': pCkbTab->Check(); break;
571 0 : case ';': pCkbSemicolon->Check(); break;
572 0 : case ',': pCkbComma->Check(); break;
573 0 : case ' ': pCkbSpace->Check(); break;
574 : default:
575 0 : pCkbOther->Check();
576 0 : pEdOther->SetText( pEdOther->GetText() + OUString( aSep[i] ) );
577 : }
578 0 : }
579 0 : }
580 :
581 0 : void ScImportAsciiDlg::SetSelectedCharSet()
582 : {
583 0 : meCharSet = pLbCharSet->GetSelectTextEncoding();
584 0 : mbCharSetSystem = (meCharSet == RTL_TEXTENCODING_DONTKNOW);
585 0 : if( mbCharSetSystem )
586 0 : meCharSet = osl_getThreadTextEncoding();
587 0 : }
588 :
589 0 : OUString ScImportAsciiDlg::GetSeparators() const
590 : {
591 0 : OUString aSepChars;
592 0 : if( pCkbTab->IsChecked() )
593 0 : aSepChars += "\t";
594 0 : if( pCkbSemicolon->IsChecked() )
595 0 : aSepChars += ";";
596 0 : if( pCkbComma->IsChecked() )
597 0 : aSepChars += ",";
598 0 : if( pCkbSpace->IsChecked() )
599 0 : aSepChars += " ";
600 0 : if( pCkbOther->IsChecked() )
601 0 : aSepChars += pEdOther->GetText();
602 0 : return aSepChars;
603 : }
604 :
605 0 : void ScImportAsciiDlg::SetupSeparatorCtrls()
606 : {
607 0 : sal_Bool bEnable = pRbSeparated->IsChecked();
608 0 : pCkbTab->Enable( bEnable );
609 0 : pCkbSemicolon->Enable( bEnable );
610 0 : pCkbComma->Enable( bEnable );
611 0 : pCkbSpace->Enable( bEnable );
612 0 : pCkbOther->Enable( bEnable );
613 0 : pEdOther->Enable( bEnable );
614 0 : pCkbAsOnce->Enable( bEnable );
615 0 : pFtTextSep->Enable( bEnable );
616 0 : pCbTextSep->Enable( bEnable );
617 0 : }
618 :
619 0 : void ScImportAsciiDlg::UpdateVertical()
620 : {
621 0 : mnRowPosCount = 0;
622 0 : if (mpDatStream)
623 0 : mpDatStream->SetStreamCharSet(meCharSet);
624 0 : }
625 :
626 :
627 0 : IMPL_LINK( ScImportAsciiDlg, RbSepFixHdl, RadioButton*, pButton )
628 : {
629 : OSL_ENSURE( pButton, "ScImportAsciiDlg::RbSepFixHdl - missing sender" );
630 :
631 0 : if( (pButton == pRbFixed) || (pButton == pRbSeparated) )
632 : {
633 0 : SetPointer( Pointer( POINTER_WAIT ) );
634 0 : if( pRbFixed->IsChecked() )
635 0 : mpTableBox->SetFixedWidthMode();
636 : else
637 0 : mpTableBox->SetSeparatorsMode();
638 0 : SetPointer( Pointer( POINTER_ARROW ) );
639 :
640 0 : SetupSeparatorCtrls();
641 : }
642 0 : return 0;
643 : }
644 :
645 0 : IMPL_LINK( ScImportAsciiDlg, SeparatorHdl, Control*, pCtrl )
646 : {
647 : OSL_ENSURE( pCtrl, "ScImportAsciiDlg::SeparatorHdl - missing sender" );
648 : OSL_ENSURE( !pRbFixed->IsChecked(), "ScImportAsciiDlg::SeparatorHdl - not allowed in fixed width" );
649 :
650 : /* #i41550# First update state of the controls. The GetSeparators()
651 : function needs final state of the check boxes. */
652 0 : if( (pCtrl == pCkbOther) && pCkbOther->IsChecked() )
653 0 : pEdOther->GrabFocus();
654 0 : else if( pCtrl == pEdOther )
655 0 : pCkbOther->Check( !pEdOther->GetText().isEmpty() );
656 :
657 0 : OUString aOldFldSeps( maFieldSeparators);
658 0 : maFieldSeparators = GetSeparators();
659 0 : sal_Unicode cOldSep = mcTextSep;
660 0 : mcTextSep = lcl_CharFromCombo( *pCbTextSep, aTextSepList );
661 : // Any separator changed may result in completely different lines due to
662 : // embedded line breaks.
663 0 : if (cOldSep != mcTextSep || aOldFldSeps != maFieldSeparators)
664 0 : UpdateVertical();
665 :
666 0 : mpTableBox->Execute( CSVCMD_NEWCELLTEXTS );
667 0 : return 0;
668 : }
669 :
670 0 : IMPL_LINK( ScImportAsciiDlg, CharSetHdl, SvxTextEncodingBox*, pCharSetBox )
671 : {
672 : OSL_ENSURE( pCharSetBox, "ScImportAsciiDlg::CharSetHdl - missing sender" );
673 :
674 0 : if( (pCharSetBox == pLbCharSet) && (pCharSetBox->GetSelectEntryCount() == 1) )
675 : {
676 0 : SetPointer( Pointer( POINTER_WAIT ) );
677 0 : rtl_TextEncoding eOldCharSet = meCharSet;
678 0 : SetSelectedCharSet();
679 : // switching char-set invalidates 8bit -> String conversions
680 0 : if (eOldCharSet != meCharSet)
681 0 : UpdateVertical();
682 :
683 0 : mpTableBox->Execute( CSVCMD_NEWCELLTEXTS );
684 0 : SetPointer( Pointer( POINTER_ARROW ) );
685 : }
686 0 : return 0;
687 : }
688 :
689 0 : IMPL_LINK( ScImportAsciiDlg, FirstRowHdl, NumericField*, pNumField )
690 : {
691 : OSL_ENSURE( pNumField, "ScImportAsciiDlg::FirstRowHdl - missing sender" );
692 0 : mpTableBox->Execute( CSVCMD_SETFIRSTIMPORTLINE, sal::static_int_cast<sal_Int32>( pNumField->GetValue() - 1 ) );
693 0 : return 0;
694 : }
695 :
696 0 : IMPL_LINK( ScImportAsciiDlg, LbColTypeHdl, ListBox*, pListBox )
697 : {
698 : OSL_ENSURE( pListBox, "ScImportAsciiDlg::LbColTypeHdl - missing sender" );
699 0 : if( pListBox == pLbType )
700 0 : mpTableBox->Execute( CSVCMD_SETCOLUMNTYPE, pListBox->GetSelectEntryPos() );
701 0 : return 0;
702 : }
703 :
704 0 : IMPL_LINK_NOARG(ScImportAsciiDlg, UpdateTextHdl)
705 : {
706 0 : sal_Int32 nBaseLine = mpTableBox->GetFirstVisLine();
707 0 : sal_Int32 nRead = mpTableBox->GetVisLineCount();
708 : // If mnRowPosCount==0, this is an initializing call, read ahead for row
709 : // count and resulting scroll bar size and position to be able to scroll at
710 : // all. When adding lines, read only the amount of next lines to be
711 : // displayed.
712 0 : if (!mnRowPosCount || nRead > CSV_PREVIEW_LINES)
713 0 : nRead = CSV_PREVIEW_LINES;
714 :
715 : sal_Int32 i;
716 0 : for (i = 0; i < nRead; i++)
717 : {
718 0 : if (!GetLine( nBaseLine + i, maPreviewLine[i]))
719 0 : break;
720 : }
721 0 : for (; i < CSV_PREVIEW_LINES; i++)
722 0 : maPreviewLine[i] = OUString();
723 :
724 0 : mpTableBox->Execute( CSVCMD_SETLINECOUNT, mnRowPosCount);
725 0 : bool bMergeSep = pCkbAsOnce->IsChecked();
726 0 : mpTableBox->SetUniStrings( maPreviewLine, maFieldSeparators, mcTextSep, bMergeSep);
727 :
728 0 : return 0;
729 : }
730 :
731 0 : IMPL_LINK( ScImportAsciiDlg, ColTypeHdl, ScCsvTableBox*, pTableBox )
732 : {
733 : OSL_ENSURE( pTableBox, "ScImportAsciiDlg::ColTypeHdl - missing sender" );
734 :
735 0 : sal_Int32 nType = pTableBox->GetSelColumnType();
736 0 : sal_Int32 nTypeCount = pLbType->GetEntryCount();
737 0 : bool bEmpty = (nType == CSV_TYPE_MULTI);
738 0 : bool bEnable = ((0 <= nType) && (nType < nTypeCount)) || bEmpty;
739 :
740 0 : pFtType->Enable( bEnable );
741 0 : pLbType->Enable( bEnable );
742 :
743 0 : Link aSelHdl = pLbType->GetSelectHdl();
744 0 : pLbType->SetSelectHdl( Link() );
745 0 : if( bEmpty )
746 0 : pLbType->SetNoSelection();
747 0 : else if( bEnable )
748 0 : pLbType->SelectEntryPos( static_cast< sal_uInt16 >( nType ) );
749 0 : pLbType->SetSelectHdl( aSelHdl );
750 :
751 0 : return 0;
752 : }
753 :
754 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|