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] = MERGE_DELIMITERS;
129 0 : pNames[1] = SEPARATORS;
130 0 : pNames[2] = TEXT_SEPARATORS;
131 0 : pNames[3] = FIXED_WIDTH;
132 0 : if (eCall != SC_TEXTTOCOLUMNS)
133 : {
134 0 : pNames[4] = FROM_ROW;
135 0 : pNames[5] = CHAR_SET;
136 0 : pNames[6] = QUOTED_AS_TEXT;
137 0 : pNames[7] = DETECT_SPECIAL_NUM;
138 0 : pNames[8] = 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] = MERGE_DELIMITERS;
202 0 : pNames[1] = SEPARATORS;
203 0 : pNames[2] = TEXT_SEPARATORS;
204 0 : pNames[3] = FIXED_WIDTH;
205 0 : if (eCall != SC_TEXTTOCOLUMNS)
206 : {
207 0 : pNames[4] = FROM_ROW;
208 0 : pNames[5] = CHAR_SET;
209 0 : pNames[6] = QUOTED_AS_TEXT;
210 0 : pNames[7] = DETECT_SPECIAL_NUM;
211 0 : pNames[8] = 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 0 : pLbCustomLang->SetLanguageList(
413 0 : SvxLanguageListFlags::ALL | SvxLanguageListFlags::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 : disposeOnce();
468 0 : }
469 :
470 0 : void ScImportAsciiDlg::dispose()
471 : {
472 0 : delete[] mpRowPosArray;
473 0 : pFtCharSet.clear();
474 0 : pLbCharSet.clear();
475 0 : pFtCustomLang.clear();
476 0 : pLbCustomLang.clear();
477 0 : pFtRow.clear();
478 0 : pNfRow.clear();
479 0 : pRbFixed.clear();
480 0 : pRbSeparated.clear();
481 0 : pCkbTab.clear();
482 0 : pCkbSemicolon.clear();
483 0 : pCkbComma.clear();
484 0 : pCkbSpace.clear();
485 0 : pCkbOther.clear();
486 0 : pEdOther.clear();
487 0 : pCkbAsOnce.clear();
488 0 : pFtTextSep.clear();
489 0 : pCbTextSep.clear();
490 0 : pCkbQuotedAsText.clear();
491 0 : pCkbDetectNumber.clear();
492 0 : pFtType.clear();
493 0 : pLbType.clear();
494 0 : mpTableBox.clear();
495 0 : ModalDialog::dispose();
496 0 : }
497 :
498 0 : bool ScImportAsciiDlg::GetLine( sal_uLong nLine, OUString &rText )
499 : {
500 0 : if (nLine >= ASCIIDLG_MAXROWS || !mpDatStream)
501 0 : return false;
502 :
503 0 : bool bRet = true;
504 0 : bool bFixed = pRbFixed->IsChecked();
505 :
506 0 : if (!mpRowPosArray)
507 0 : mpRowPosArray = new sal_uLong[ASCIIDLG_MAXROWS + 2];
508 :
509 0 : if (!mnRowPosCount) // complete re-fresh
510 : {
511 0 : memset( mpRowPosArray, 0, sizeof(mpRowPosArray[0]) * (ASCIIDLG_MAXROWS+2));
512 :
513 0 : Seek(0);
514 0 : mpDatStream->StartReadingUnicodeText( mpDatStream->GetStreamCharSet() );
515 :
516 0 : mnStreamPos = mpDatStream->Tell();
517 0 : mpRowPosArray[mnRowPosCount] = mnStreamPos;
518 : }
519 :
520 0 : if (nLine >= mnRowPosCount)
521 : {
522 : // need to work out some more line information
523 0 : do
524 : {
525 0 : if (!Seek( mpRowPosArray[mnRowPosCount]) ||
526 0 : mpDatStream->GetError() != ERRCODE_NONE ||
527 0 : mpDatStream->IsEof())
528 : {
529 0 : bRet = false;
530 0 : break;
531 : }
532 0 : rText = ReadCsvLine(*mpDatStream, !bFixed, maFieldSeparators,
533 0 : mcTextSep);
534 0 : mnStreamPos = mpDatStream->Tell();
535 0 : mpRowPosArray[++mnRowPosCount] = mnStreamPos;
536 0 : } while (nLine >= mnRowPosCount &&
537 0 : mpDatStream->GetError() == ERRCODE_NONE &&
538 0 : !mpDatStream->IsEof());
539 0 : if (mpDatStream->IsEof() &&
540 0 : mnStreamPos == mpRowPosArray[mnRowPosCount-1])
541 : {
542 : // the very end, not even an empty line read
543 0 : bRet = false;
544 0 : --mnRowPosCount;
545 : }
546 : }
547 : else
548 : {
549 0 : Seek( mpRowPosArray[nLine]);
550 0 : rText = ReadCsvLine(*mpDatStream, !bFixed, maFieldSeparators, mcTextSep);
551 0 : mnStreamPos = mpDatStream->Tell();
552 : }
553 :
554 : // If the file content isn't unicode, ReadUniStringLine
555 : // may try to seek beyond the file's end and cause a CANTSEEK error
556 : // (depending on the stream type). The error code has to be cleared,
557 : // or further read operations (including non-unicode) will fail.
558 0 : if ( mpDatStream->GetError() == ERRCODE_IO_CANTSEEK )
559 0 : mpDatStream->ResetError();
560 :
561 0 : ScImportExport::EmbeddedNullTreatment( rText);
562 :
563 0 : return bRet;
564 : }
565 :
566 0 : void ScImportAsciiDlg::GetOptions( ScAsciiOptions& rOpt )
567 : {
568 0 : rOpt.SetCharSet( meCharSet );
569 0 : rOpt.SetCharSetSystem( mbCharSetSystem );
570 0 : rOpt.SetLanguage(pLbCustomLang->GetSelectLanguage());
571 0 : rOpt.SetFixedLen( pRbFixed->IsChecked() );
572 0 : rOpt.SetStartRow( (long)pNfRow->GetValue() );
573 0 : mpTableBox->FillColumnData( rOpt );
574 0 : if( pRbSeparated->IsChecked() )
575 : {
576 0 : rOpt.SetFieldSeps( GetSeparators() );
577 0 : rOpt.SetMergeSeps( pCkbAsOnce->IsChecked() );
578 0 : rOpt.SetTextSep( lcl_CharFromCombo( *pCbTextSep, aTextSepList ) );
579 : }
580 :
581 0 : rOpt.SetQuotedAsText(pCkbQuotedAsText->IsChecked());
582 0 : rOpt.SetDetectSpecialNumber(pCkbDetectNumber->IsChecked());
583 0 : }
584 :
585 0 : void ScImportAsciiDlg::SaveParameters()
586 : {
587 0 : save_Separators( maFieldSeparators, pCbTextSep->GetText(), pCkbAsOnce->IsChecked(),
588 0 : pCkbQuotedAsText->IsChecked(), pCkbDetectNumber->IsChecked(),
589 0 : pRbFixed->IsChecked(),
590 0 : static_cast<sal_Int32>(pNfRow->GetValue()),
591 0 : static_cast<sal_Int32>(pLbCharSet->GetSelectEntryPos()),
592 0 : static_cast<sal_Int32>(pLbCustomLang->GetSelectLanguage()), meCall );
593 0 : }
594 :
595 0 : void ScImportAsciiDlg::SetSeparators()
596 : {
597 : OString sString(OUStringToOString(maFieldSeparators,
598 0 : RTL_TEXTENCODING_MS_1252));
599 0 : const sal_Char *aSep = sString.getStr();
600 0 : sal_Int32 len = maFieldSeparators.getLength();
601 0 : for (int i = 0; i < len; ++i)
602 : {
603 0 : switch( aSep[i] )
604 : {
605 0 : case '\t': pCkbTab->Check(); break;
606 0 : case ';': pCkbSemicolon->Check(); break;
607 0 : case ',': pCkbComma->Check(); break;
608 0 : case ' ': pCkbSpace->Check(); break;
609 : default:
610 0 : pCkbOther->Check();
611 0 : pEdOther->SetText( pEdOther->GetText() + OUString( aSep[i] ) );
612 : }
613 0 : }
614 0 : }
615 :
616 0 : void ScImportAsciiDlg::SetSelectedCharSet()
617 : {
618 0 : meCharSet = pLbCharSet->GetSelectTextEncoding();
619 0 : mbCharSetSystem = (meCharSet == RTL_TEXTENCODING_DONTKNOW);
620 0 : if( mbCharSetSystem )
621 0 : meCharSet = osl_getThreadTextEncoding();
622 0 : }
623 :
624 0 : OUString ScImportAsciiDlg::GetSeparators() const
625 : {
626 0 : OUString aSepChars;
627 0 : if( pCkbTab->IsChecked() )
628 0 : aSepChars += "\t";
629 0 : if( pCkbSemicolon->IsChecked() )
630 0 : aSepChars += ";";
631 0 : if( pCkbComma->IsChecked() )
632 0 : aSepChars += ",";
633 0 : if( pCkbSpace->IsChecked() )
634 0 : aSepChars += " ";
635 0 : if( pCkbOther->IsChecked() )
636 0 : aSepChars += pEdOther->GetText();
637 0 : return aSepChars;
638 : }
639 :
640 0 : void ScImportAsciiDlg::SetupSeparatorCtrls()
641 : {
642 0 : bool bEnable = pRbSeparated->IsChecked();
643 0 : pCkbTab->Enable( bEnable );
644 0 : pCkbSemicolon->Enable( bEnable );
645 0 : pCkbComma->Enable( bEnable );
646 0 : pCkbSpace->Enable( bEnable );
647 0 : pCkbOther->Enable( bEnable );
648 0 : pEdOther->Enable( bEnable );
649 0 : pCkbAsOnce->Enable( bEnable );
650 0 : pFtTextSep->Enable( bEnable );
651 0 : pCbTextSep->Enable( bEnable );
652 0 : }
653 :
654 0 : void ScImportAsciiDlg::UpdateVertical()
655 : {
656 0 : mnRowPosCount = 0;
657 0 : if (mpDatStream)
658 0 : mpDatStream->SetStreamCharSet(meCharSet);
659 0 : }
660 :
661 0 : IMPL_LINK( ScImportAsciiDlg, RbSepFixHdl, RadioButton*, pButton )
662 : {
663 : OSL_ENSURE( pButton, "ScImportAsciiDlg::RbSepFixHdl - missing sender" );
664 :
665 0 : if( (pButton == pRbFixed) || (pButton == pRbSeparated) )
666 : {
667 0 : SetPointer( Pointer( PointerStyle::Wait ) );
668 0 : if( pRbFixed->IsChecked() )
669 0 : mpTableBox->SetFixedWidthMode();
670 : else
671 0 : mpTableBox->SetSeparatorsMode();
672 0 : SetPointer( Pointer( PointerStyle::Arrow ) );
673 :
674 0 : SetupSeparatorCtrls();
675 : }
676 0 : return 0;
677 : }
678 :
679 0 : IMPL_LINK( ScImportAsciiDlg, SeparatorHdl, Control*, pCtrl )
680 : {
681 : OSL_ENSURE( pCtrl, "ScImportAsciiDlg::SeparatorHdl - missing sender" );
682 : OSL_ENSURE( !pRbFixed->IsChecked(), "ScImportAsciiDlg::SeparatorHdl - not allowed in fixed width" );
683 :
684 : /* #i41550# First update state of the controls. The GetSeparators()
685 : function needs final state of the check boxes. */
686 0 : if( (pCtrl == pCkbOther) && pCkbOther->IsChecked() )
687 0 : pEdOther->GrabFocus();
688 0 : else if( pCtrl == pEdOther )
689 0 : pCkbOther->Check( !pEdOther->GetText().isEmpty() );
690 :
691 0 : OUString aOldFldSeps( maFieldSeparators);
692 0 : maFieldSeparators = GetSeparators();
693 0 : sal_Unicode cOldSep = mcTextSep;
694 0 : mcTextSep = lcl_CharFromCombo( *pCbTextSep, aTextSepList );
695 : // Any separator changed may result in completely different lines due to
696 : // embedded line breaks.
697 0 : if (cOldSep != mcTextSep || aOldFldSeps != maFieldSeparators)
698 0 : UpdateVertical();
699 :
700 0 : mpTableBox->Execute( CSVCMD_NEWCELLTEXTS );
701 0 : return 0;
702 : }
703 :
704 0 : IMPL_LINK( ScImportAsciiDlg, CharSetHdl, SvxTextEncodingBox*, pCharSetBox )
705 : {
706 : OSL_ENSURE( pCharSetBox, "ScImportAsciiDlg::CharSetHdl - missing sender" );
707 :
708 0 : if( (pCharSetBox == pLbCharSet) && (pCharSetBox->GetSelectEntryCount() == 1) )
709 : {
710 0 : SetPointer( Pointer( PointerStyle::Wait ) );
711 0 : rtl_TextEncoding eOldCharSet = meCharSet;
712 0 : SetSelectedCharSet();
713 : // switching char-set invalidates 8bit -> String conversions
714 0 : if (eOldCharSet != meCharSet)
715 0 : UpdateVertical();
716 :
717 0 : mpTableBox->Execute( CSVCMD_NEWCELLTEXTS );
718 0 : SetPointer( Pointer( PointerStyle::Arrow ) );
719 : }
720 0 : return 0;
721 : }
722 :
723 0 : IMPL_LINK( ScImportAsciiDlg, FirstRowHdl, NumericField*, pNumField )
724 : {
725 : OSL_ENSURE( pNumField, "ScImportAsciiDlg::FirstRowHdl - missing sender" );
726 0 : mpTableBox->Execute( CSVCMD_SETFIRSTIMPORTLINE, sal::static_int_cast<sal_Int32>( pNumField->GetValue() - 1 ) );
727 0 : return 0;
728 : }
729 :
730 0 : IMPL_LINK( ScImportAsciiDlg, LbColTypeHdl, ListBox*, pListBox )
731 : {
732 : OSL_ENSURE( pListBox, "ScImportAsciiDlg::LbColTypeHdl - missing sender" );
733 0 : if( pListBox == pLbType )
734 0 : mpTableBox->Execute( CSVCMD_SETCOLUMNTYPE, pListBox->GetSelectEntryPos() );
735 0 : return 0;
736 : }
737 :
738 0 : IMPL_LINK_NOARG(ScImportAsciiDlg, UpdateTextHdl)
739 : {
740 0 : sal_Int32 nBaseLine = mpTableBox->GetFirstVisLine();
741 0 : sal_Int32 nRead = mpTableBox->GetVisLineCount();
742 : // If mnRowPosCount==0, this is an initializing call, read ahead for row
743 : // count and resulting scroll bar size and position to be able to scroll at
744 : // all. When adding lines, read only the amount of next lines to be
745 : // displayed.
746 0 : if (!mnRowPosCount || nRead > CSV_PREVIEW_LINES)
747 0 : nRead = CSV_PREVIEW_LINES;
748 :
749 : sal_Int32 i;
750 0 : for (i = 0; i < nRead; i++)
751 : {
752 0 : if (!GetLine( nBaseLine + i, maPreviewLine[i]))
753 0 : break;
754 : }
755 0 : for (; i < CSV_PREVIEW_LINES; i++)
756 0 : maPreviewLine[i].clear();
757 :
758 0 : mpTableBox->Execute( CSVCMD_SETLINECOUNT, mnRowPosCount);
759 0 : bool bMergeSep = pCkbAsOnce->IsChecked();
760 0 : mpTableBox->SetUniStrings( maPreviewLine, maFieldSeparators, mcTextSep, bMergeSep);
761 :
762 0 : return 0;
763 : }
764 :
765 0 : IMPL_LINK( ScImportAsciiDlg, ColTypeHdl, ScCsvTableBox*, pTableBox )
766 : {
767 : OSL_ENSURE( pTableBox, "ScImportAsciiDlg::ColTypeHdl - missing sender" );
768 :
769 0 : sal_Int32 nType = pTableBox->GetSelColumnType();
770 0 : sal_Int32 nTypeCount = pLbType->GetEntryCount();
771 0 : bool bEmpty = (nType == CSV_TYPE_MULTI);
772 0 : bool bEnable = ((0 <= nType) && (nType < nTypeCount)) || bEmpty;
773 :
774 0 : pFtType->Enable( bEnable );
775 0 : pLbType->Enable( bEnable );
776 :
777 0 : Link<> aSelHdl = pLbType->GetSelectHdl();
778 0 : pLbType->SetSelectHdl( Link<>() );
779 0 : if( bEmpty )
780 0 : pLbType->SetNoSelection();
781 0 : else if( bEnable )
782 0 : pLbType->SelectEntryPos( static_cast< sal_uInt16 >( nType ) );
783 0 : pLbType->SetSelectHdl( aSelHdl );
784 :
785 0 : return 0;
786 0 : }
787 :
788 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|