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 : #include <config_features.h>
21 :
22 : #include "moduledbu.hxx"
23 : #include "TextConnectionHelper.hxx"
24 : #include "sqlmessage.hxx"
25 : #include "dbu_dlg.hrc"
26 : #include "dbu_resource.hrc"
27 : #include <svl/itemset.hxx>
28 : #include <svl/stritem.hxx>
29 : #include <svl/eitem.hxx>
30 : #include <svl/intitem.hxx>
31 : #include "dsitems.hxx"
32 : #include "dbfindex.hxx"
33 : #include "dbaccess_helpid.hrc"
34 : #include "localresaccess.hxx"
35 : #include <vcl/layout.hxx>
36 : #include <vcl/mnemonic.hxx>
37 : #include <svl/cjkoptions.hxx>
38 : #if HAVE_FEATURE_JAVA
39 : #include <jvmaccess/virtualmachine.hxx>
40 : #endif
41 : #include <connectivity/CommonTools.hxx>
42 : #include "DriverSettings.hxx"
43 : #include "dbadmin.hxx"
44 : #include <comphelper/string.hxx>
45 : #include <comphelper/types.hxx>
46 : #include <com/sun/star/task/XInteractionHandler.hpp>
47 : #include <svl/filenotation.hxx>
48 : #include <unotools/localfilehelper.hxx>
49 : #include <unotools/ucbhelper.hxx>
50 : #include <ucbhelper/commandenvironment.hxx>
51 : #include "finteraction.hxx"
52 : #include "DBSetupConnectionPages.hxx"
53 : #include <unotools/pathoptions.hxx>
54 : #include <svtools/roadmapwizard.hxx>
55 :
56 : namespace dbaui
57 : {
58 :
59 0 : OTextConnectionHelper::OTextConnectionHelper( vcl::Window* pParent, const short _nAvailableSections )
60 : :TabPage(pParent, "TextPage", "dbaccess/ui/textpage.ui")
61 : ,m_aFieldSeparatorList (ModuleRes(STR_AUTOFIELDSEPARATORLIST))
62 : ,m_aTextSeparatorList (ModuleRes(STR_AUTOTEXTSEPARATORLIST))
63 : ,m_aTextNone (ModuleRes(STR_AUTOTEXT_FIELD_SEP_NONE))
64 0 : ,m_nAvailableSections( _nAvailableSections )
65 : {
66 0 : get(m_pExtensionHeader, "extensionheader");
67 0 : get(m_pAccessTextFiles, "textfile");
68 0 : get(m_pAccessCSVFiles, "csvfile");
69 0 : get(m_pAccessOtherFiles, "custom");
70 0 : get(m_pOwnExtension, "extension");
71 0 : get(m_pExtensionExample, "example");
72 0 : get(m_pFormatHeader, "formatlabel");
73 0 : get(m_pFieldSeparatorLabel, "fieldlabel");
74 0 : get(m_pFieldSeparator, "fieldseparator");
75 0 : get(m_pTextSeparatorLabel, "textlabel");
76 0 : get(m_pTextSeparator, "textseparator");
77 0 : get(m_pDecimalSeparatorLabel, "decimallabel");
78 0 : get(m_pDecimalSeparator, "decimalseparator");
79 0 : get(m_pThousandsSeparatorLabel, "thousandslabel");
80 0 : get(m_pThousandsSeparator, "thousandsseparator");
81 0 : get(m_pRowHeader, "containsheaders");
82 0 : get(m_pCharSetHeader, "charsetheader");
83 0 : get(m_pCharSetLabel, "charsetlabel");
84 0 : get(m_pCharSet, "charset");
85 :
86 0 : sal_Int32 nCnt = comphelper::string::getTokenCount(m_aFieldSeparatorList, '\t');
87 : sal_Int32 i;
88 :
89 0 : for( i = 0 ; i < nCnt ; i += 2 )
90 0 : m_pFieldSeparator->InsertEntry( m_aFieldSeparatorList.getToken( i, '\t' ) );
91 :
92 0 : nCnt = comphelper::string::getTokenCount(m_aTextSeparatorList, '\t');
93 0 : for( i=0 ; i<nCnt ; i+=2 )
94 0 : m_pTextSeparator->InsertEntry( m_aTextSeparatorList.getToken( i, '\t' ) );
95 0 : m_pTextSeparator->InsertEntry(m_aTextNone);
96 :
97 : // set the modify handlers
98 0 : m_pFieldSeparator->SetUpdateDataHdl(getControlModifiedLink());
99 0 : m_pFieldSeparator->SetSelectHdl(getControlModifiedLink());
100 0 : m_pTextSeparator->SetUpdateDataHdl(getControlModifiedLink());
101 0 : m_pTextSeparator->SetSelectHdl(getControlModifiedLink());
102 0 : m_pCharSet->SetSelectHdl(getControlModifiedLink());
103 :
104 0 : m_pFieldSeparator->SetModifyHdl(getControlModifiedLink());
105 0 : m_pTextSeparator->SetModifyHdl(getControlModifiedLink());
106 0 : m_pDecimalSeparator->SetModifyHdl(getControlModifiedLink());
107 0 : m_pThousandsSeparator->SetModifyHdl(getControlModifiedLink());
108 0 : m_pOwnExtension->SetModifyHdl(LINK(this, OTextConnectionHelper, OnEditModified));
109 0 : m_pAccessTextFiles->SetToggleHdl(LINK(this, OTextConnectionHelper, OnSetExtensionHdl));
110 0 : m_pAccessCSVFiles->SetToggleHdl(LINK(this, OTextConnectionHelper, OnSetExtensionHdl));
111 0 : m_pAccessOtherFiles->SetToggleHdl(LINK(this, OTextConnectionHelper, OnSetExtensionHdl));
112 0 : m_pAccessCSVFiles->Check(true);
113 :
114 : struct SectionDescriptor
115 : {
116 : short nFlag;
117 : vcl::Window* pFirstControl;
118 : } aSections[] = {
119 : { TC_EXTENSION, m_pExtensionHeader },
120 : { TC_SEPARATORS, m_pFormatHeader },
121 : { TC_HEADER, m_pRowHeader },
122 : { TC_CHARSET, m_pCharSetHeader },
123 : { 0, NULL }
124 0 : };
125 :
126 0 : for ( size_t section=0; section < sizeof( aSections ) / sizeof( aSections[0] ) - 1; ++section )
127 : {
128 0 : if ( ( m_nAvailableSections & aSections[section].nFlag ) != 0 )
129 : {
130 : // the section is visible, no need to do anything here
131 0 : continue;
132 : }
133 :
134 0 : vcl::Window* pThisSection = aSections[section].pFirstControl;
135 0 : vcl::Window* pNextSection = aSections[section+1].pFirstControl;
136 :
137 : // hide all elements from this section
138 0 : vcl::Window* pControl = pThisSection;
139 0 : while ( ( pControl != pNextSection ) && pControl )
140 : {
141 0 : vcl::Window* pRealWindow = pControl->GetWindow( WINDOW_CLIENT );
142 : #if OSL_DEBUG_LEVEL > 0
143 : OUString sWindowText( pRealWindow->GetText() );
144 : (void)sWindowText;
145 : #endif
146 0 : pRealWindow->Hide();
147 0 : pControl = pControl->GetWindow( WINDOW_NEXT );
148 : }
149 : }
150 :
151 0 : Show();
152 0 : }
153 :
154 0 : OTextConnectionHelper::~OTextConnectionHelper()
155 : {
156 :
157 0 : }
158 :
159 0 : IMPL_LINK(OTextConnectionHelper, OnControlModified, Control*, /*EMPTYARG*/)
160 : {
161 0 : callModifiedHdl();
162 0 : return 0L;
163 : }
164 :
165 0 : IMPL_LINK(OTextConnectionHelper, OnEditModified, Edit*, /*_pEdit*/)
166 : {
167 0 : m_aGetExtensionHandler.Call(this);
168 0 : return 0L;
169 : }
170 :
171 0 : IMPL_LINK(OTextConnectionHelper, OnSetExtensionHdl, RadioButton*, /*_pRadioButton*/)
172 : {
173 0 : bool bDoEnable = m_pAccessOtherFiles->IsChecked();
174 0 : m_pOwnExtension->Enable(bDoEnable);
175 0 : m_pExtensionExample->Enable(bDoEnable);
176 0 : m_aGetExtensionHandler.Call(this);
177 0 : return 0L;
178 : }
179 :
180 0 : void OTextConnectionHelper::fillControls(::std::vector< ISaveValueWrapper* >& _rControlList)
181 : {
182 0 : _rControlList.push_back(new OSaveValueWrapper<ComboBox>(m_pFieldSeparator));
183 0 : _rControlList.push_back(new OSaveValueWrapper<ComboBox>(m_pTextSeparator));
184 0 : _rControlList.push_back(new OSaveValueWrapper<ComboBox>(m_pDecimalSeparator));
185 0 : _rControlList.push_back(new OSaveValueWrapper<ComboBox>(m_pThousandsSeparator));
186 0 : _rControlList.push_back(new OSaveValueWrapper<CheckBox>(m_pRowHeader));
187 0 : _rControlList.push_back(new OSaveValueWrapper<ListBox>(m_pCharSet));
188 0 : }
189 :
190 0 : void OTextConnectionHelper::fillWindows(::std::vector< ISaveValueWrapper* >& _rControlList)
191 : {
192 0 : _rControlList.push_back(new ODisableWrapper<FixedText>(m_pFieldSeparatorLabel));
193 0 : _rControlList.push_back(new ODisableWrapper<FixedText>(m_pTextSeparatorLabel));
194 0 : _rControlList.push_back(new ODisableWrapper<FixedText>(m_pDecimalSeparatorLabel));
195 0 : _rControlList.push_back(new ODisableWrapper<FixedText>(m_pThousandsSeparatorLabel));
196 0 : _rControlList.push_back(new ODisableWrapper<FixedText>(m_pCharSetHeader));
197 0 : _rControlList.push_back(new ODisableWrapper<FixedText>(m_pCharSetLabel));
198 0 : _rControlList.push_back(new ODisableWrapper<ListBox>(m_pCharSet));
199 0 : }
200 :
201 0 : void OTextConnectionHelper::implInitControls(const SfxItemSet& _rSet, bool _bValid)
202 : {
203 0 : if ( !_bValid )
204 0 : return;
205 :
206 0 : SFX_ITEMSET_GET( _rSet, pDelItem, SfxStringItem, DSID_FIELDDELIMITER, true );
207 0 : SFX_ITEMSET_GET( _rSet, pStrItem, SfxStringItem, DSID_TEXTDELIMITER, true );
208 0 : SFX_ITEMSET_GET( _rSet, pDecdelItem, SfxStringItem, DSID_DECIMALDELIMITER, true );
209 0 : SFX_ITEMSET_GET( _rSet, pThodelItem, SfxStringItem, DSID_THOUSANDSDELIMITER, true );
210 0 : SFX_ITEMSET_GET( _rSet, pExtensionItem, SfxStringItem, DSID_TEXTFILEEXTENSION, true );
211 0 : SFX_ITEMSET_GET( _rSet, pCharsetItem, SfxStringItem, DSID_CHARSET, true );
212 :
213 0 : if ( ( m_nAvailableSections & TC_EXTENSION ) != 0 )
214 : {
215 0 : m_aOldExtension = pExtensionItem->GetValue();
216 0 : SetExtension( m_aOldExtension );
217 : }
218 :
219 0 : if ( ( m_nAvailableSections & TC_HEADER ) != 0 )
220 : {
221 0 : SFX_ITEMSET_GET( _rSet, pHdrItem, SfxBoolItem, DSID_TEXTFILEHEADER, true );
222 0 : m_pRowHeader->Check( pHdrItem->GetValue() );
223 : }
224 :
225 0 : if ( ( m_nAvailableSections & TC_SEPARATORS ) != 0 )
226 : {
227 0 : SetSeparator( *m_pFieldSeparator, m_aFieldSeparatorList, pDelItem->GetValue() );
228 0 : SetSeparator( *m_pTextSeparator, m_aTextSeparatorList, pStrItem->GetValue() );
229 0 : m_pDecimalSeparator->SetText( pDecdelItem->GetValue() );
230 0 : m_pThousandsSeparator->SetText( pThodelItem->GetValue() );
231 : }
232 :
233 0 : if ( ( m_nAvailableSections & TC_CHARSET ) != 0 )
234 : {
235 0 : m_pCharSet->SelectEntryByIanaName( pCharsetItem->GetValue() );
236 : }
237 : }
238 :
239 0 : bool OTextConnectionHelper::prepareLeave()
240 : {
241 0 : OUString sExtension = GetExtension();
242 0 : OUString aErrorText;
243 0 : Control* pErrorWin = NULL;
244 0 : OUString aDelText(m_pFieldSeparator->GetText());
245 0 : if(aDelText.isEmpty())
246 : { // No FieldSeparator
247 0 : aErrorText = ModuleRes(STR_AUTODELIMITER_MISSING);
248 0 : aErrorText = aErrorText.replaceFirst("#1",m_pFieldSeparatorLabel->GetText());
249 0 : pErrorWin = m_pFieldSeparator;
250 : }
251 0 : else if (m_pDecimalSeparator->GetText().isEmpty())
252 : { // No DecimalSeparator
253 0 : aErrorText = ModuleRes(STR_AUTODELIMITER_MISSING);
254 0 : aErrorText = aErrorText.replaceFirst("#1",m_pDecimalSeparatorLabel->GetText());
255 0 : pErrorWin = m_pDecimalSeparator;
256 : }
257 0 : else if (m_pTextSeparator->GetText() == m_pFieldSeparator->GetText())
258 : { // Field and TextSeparator must not be the same
259 0 : aErrorText = ModuleRes(STR_AUTODELIMITER_MUST_DIFFER);
260 0 : aErrorText = aErrorText.replaceFirst("#1",m_pTextSeparatorLabel->GetText());
261 0 : aErrorText = aErrorText.replaceFirst("#2",m_pFieldSeparatorLabel->GetText());
262 0 : pErrorWin = m_pTextSeparator;
263 : }
264 0 : else if (m_pDecimalSeparator->GetText() == m_pThousandsSeparator->GetText())
265 : { // Thousands and DecimalSeparator must not be the same
266 0 : aErrorText = ModuleRes(STR_AUTODELIMITER_MUST_DIFFER);
267 0 : aErrorText = aErrorText.replaceFirst("#1",m_pDecimalSeparatorLabel->GetText());
268 0 : aErrorText = aErrorText.replaceFirst("#2",m_pThousandsSeparatorLabel->GetText());
269 0 : pErrorWin = m_pDecimalSeparator;
270 : }
271 0 : else if (m_pFieldSeparator->GetText() == m_pThousandsSeparator->GetText())
272 : { // Thousands and FieldSeparator must not be the same
273 0 : aErrorText = ModuleRes(STR_AUTODELIMITER_MUST_DIFFER);
274 0 : aErrorText = aErrorText.replaceFirst("#1",m_pFieldSeparatorLabel->GetText());
275 0 : aErrorText = aErrorText.replaceFirst("#2",m_pThousandsSeparatorLabel->GetText());
276 0 : pErrorWin = m_pFieldSeparator;
277 : }
278 0 : else if (m_pFieldSeparator->GetText() == m_pDecimalSeparator->GetText())
279 : { // Tenner and FieldSeparator must not be the same
280 0 : aErrorText = ModuleRes(STR_AUTODELIMITER_MUST_DIFFER);
281 0 : aErrorText = aErrorText.replaceFirst("#1",m_pFieldSeparatorLabel->GetText());
282 0 : aErrorText = aErrorText.replaceFirst("#2",m_pDecimalSeparatorLabel->GetText());
283 0 : pErrorWin = m_pFieldSeparator;
284 : }
285 0 : else if (m_pTextSeparator->GetText() == m_pThousandsSeparator->GetText())
286 : { // Thousands and TextSeparator must not be the same
287 0 : aErrorText = ModuleRes(STR_AUTODELIMITER_MUST_DIFFER);
288 0 : aErrorText = aErrorText.replaceFirst("#1",m_pTextSeparatorLabel->GetText());
289 0 : aErrorText = aErrorText.replaceFirst("#2",m_pThousandsSeparatorLabel->GetText());
290 0 : pErrorWin = m_pTextSeparator;
291 : }
292 0 : else if (m_pTextSeparator->GetText() == m_pDecimalSeparator->GetText())
293 : { // Tenner and TextSeparator must not be the same
294 0 : aErrorText = ModuleRes(STR_AUTODELIMITER_MUST_DIFFER);
295 0 : aErrorText = aErrorText.replaceFirst("#1",m_pTextSeparatorLabel->GetText());
296 0 : aErrorText = aErrorText.replaceFirst("#2",m_pDecimalSeparatorLabel->GetText());
297 0 : pErrorWin = m_pTextSeparator;
298 : }
299 0 : else if ((sExtension.indexOf('*') != -1) || (sExtension.indexOf('?') != -1))
300 : {
301 0 : aErrorText = ModuleRes(STR_AUTONO_WILDCARDS);
302 0 : aErrorText = aErrorText.replaceFirst("#1",sExtension);
303 0 : pErrorWin = m_pOwnExtension;
304 : }
305 : else
306 0 : return true;
307 0 : MessageDialog(NULL, MnemonicGenerator::EraseAllMnemonicChars(aErrorText)).Execute();
308 0 : pErrorWin->GrabFocus();
309 0 : return false;
310 : }
311 :
312 0 : bool OTextConnectionHelper::FillItemSet( SfxItemSet& rSet, const bool _bChangedSomething )
313 : {
314 0 : bool bChangedSomething = _bChangedSomething;
315 :
316 0 : if ( ( m_nAvailableSections & TC_EXTENSION ) != 0 )
317 : {
318 0 : OUString sExtension = GetExtension();
319 0 : if( m_aOldExtension != sExtension )
320 : {
321 0 : rSet.Put( SfxStringItem( DSID_TEXTFILEEXTENSION, sExtension ) );
322 0 : bChangedSomething = true;
323 0 : }
324 : }
325 :
326 0 : if ( ( m_nAvailableSections & TC_HEADER ) != 0 )
327 : {
328 0 : if( m_pRowHeader->IsValueChangedFromSaved() )
329 : {
330 0 : rSet.Put(SfxBoolItem(DSID_TEXTFILEHEADER, m_pRowHeader->IsChecked()));
331 0 : bChangedSomething = true;
332 : }
333 : }
334 :
335 0 : if ( ( m_nAvailableSections & TC_SEPARATORS ) != 0 )
336 : {
337 0 : if( m_pFieldSeparator->IsValueChangedFromSaved() )
338 : {
339 0 : rSet.Put( SfxStringItem(DSID_FIELDDELIMITER, GetSeparator( *m_pFieldSeparator, m_aFieldSeparatorList) ) );
340 0 : bChangedSomething = true;
341 : }
342 0 : if( m_pTextSeparator->IsValueChangedFromSaved() )
343 : {
344 0 : rSet.Put( SfxStringItem(DSID_TEXTDELIMITER, GetSeparator( *m_pTextSeparator, m_aTextSeparatorList) ) );
345 0 : bChangedSomething = true;
346 : }
347 :
348 0 : if( m_pDecimalSeparator->IsValueChangedFromSaved() )
349 : {
350 0 : rSet.Put( SfxStringItem(DSID_DECIMALDELIMITER, m_pDecimalSeparator->GetText().copy(0, 1) ) );
351 0 : bChangedSomething = true;
352 : }
353 0 : if( m_pThousandsSeparator->IsValueChangedFromSaved() )
354 : {
355 0 : rSet.Put( SfxStringItem(DSID_THOUSANDSDELIMITER, m_pThousandsSeparator->GetText().copy(0,1) ) );
356 0 : bChangedSomething = true;
357 : }
358 : }
359 :
360 0 : if ( ( m_nAvailableSections & TC_CHARSET ) != 0 )
361 : {
362 0 : if ( m_pCharSet->StoreSelectedCharSet( rSet, DSID_CHARSET ) )
363 0 : bChangedSomething = true;
364 : }
365 :
366 0 : return bChangedSomething;
367 : }
368 :
369 0 : void OTextConnectionHelper::SetExtension(const OUString& _rVal)
370 : {
371 0 : if (_rVal == "txt")
372 0 : m_pAccessTextFiles->Check(true);
373 0 : else if (_rVal == "csv")
374 0 : m_pAccessCSVFiles->Check(true);
375 : else
376 : {
377 0 : m_pAccessOtherFiles->Check(true);
378 0 : m_pExtensionExample->SetText(_rVal);
379 : }
380 0 : }
381 :
382 0 : OUString OTextConnectionHelper::GetExtension()
383 : {
384 0 : OUString sExtension;
385 0 : if (m_pAccessTextFiles->IsChecked())
386 0 : sExtension = "txt";
387 0 : else if (m_pAccessCSVFiles->IsChecked())
388 0 : sExtension = "csv";
389 : else
390 : {
391 0 : sExtension = m_pOwnExtension->GetText();
392 0 : if ( comphelper::string::equals(sExtension.getToken(0,'.'), '*') )
393 0 : sExtension = sExtension.copy(2);
394 : }
395 0 : return sExtension;
396 : }
397 :
398 0 : OUString OTextConnectionHelper::GetSeparator( const ComboBox& rBox, const OUString& rList )
399 : {
400 0 : sal_Unicode nTok = '\t';
401 0 : sal_Int32 nPos(rBox.GetEntryPos( rBox.GetText() ));
402 :
403 0 : if( nPos == COMBOBOX_ENTRY_NOTFOUND )
404 0 : return rBox.GetText().copy(0);
405 :
406 0 : if ( !( m_pTextSeparator == &rBox && nPos == (rBox.GetEntryCount()-1) ) )
407 : return OUString(
408 0 : static_cast< sal_Unicode >( rList.getToken((nPos*2)+1, nTok ).toInt32() ));
409 : // somewhat strange ... translates for instance an "32" into " "
410 0 : return OUString();
411 : }
412 :
413 0 : void OTextConnectionHelper::SetSeparator( ComboBox& rBox, const OUString& rList, const OUString& rVal )
414 : {
415 0 : char nTok = '\t';
416 0 : sal_Int32 nCnt = comphelper::string::getTokenCount(rList, nTok);
417 : sal_Int32 i;
418 :
419 0 : for( i=0 ; i<nCnt ; i+=2 )
420 : {
421 : OUString sTVal(
422 0 : static_cast< sal_Unicode >( rList.getToken( (i+1), nTok ).toInt32() ));
423 :
424 0 : if( sTVal.equals(rVal) )
425 : {
426 0 : rBox.SetText( rList.getToken( i, nTok ) );
427 0 : break;
428 : }
429 0 : }
430 :
431 0 : if ( i >= nCnt )
432 : {
433 0 : if ( m_pTextSeparator == &rBox && rVal.isEmpty() )
434 0 : rBox.SetText(m_aTextNone);
435 : else
436 0 : rBox.SetText( rVal.copy(0, 1) );
437 : }
438 0 : }
439 72 : }
440 :
441 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|