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