Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / dbaccess / source / ui / dlg / advancedsettings.cxx
blob09d7430fc9265d649314a38220dbe8e0b3e3b8bb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <memory>
24 #include "advancedsettings.hxx"
25 #include <advancedsettingsdlg.hxx>
26 #include <dsitems.hxx>
27 #include "DbAdminImpl.hxx"
28 #include "DriverSettings.hxx"
29 #include "optionalboolitem.hxx"
31 #include <svl/eitem.hxx>
32 #include <svl/intitem.hxx>
33 #include <svl/stritem.hxx>
35 namespace dbaui
38 using ::com::sun::star::uno::Reference;
39 using ::com::sun::star::uno::Any;
40 using ::com::sun::star::uno::XComponentContext;
41 using ::com::sun::star::beans::XPropertySet;
42 using ::com::sun::star::sdbc::XConnection;
43 using ::com::sun::star::sdbc::XDriver;
45 // SpecialSettingsPage
46 struct BooleanSettingDesc
48 std::unique_ptr<weld::CheckButton>& xControl; // the dialog's control which displays this setting
49 OUString sControlId; // the widget name of the control in the .ui
50 sal_uInt16 nItemId; // the ID of the item (in an SfxItemSet) which corresponds to this setting
51 bool bInvertedDisplay; // true if and only if the checkbox is checked when the item is sal_False, and vice versa
52 bool bOptionalBool; // type is OptionalBool
55 // SpecialSettingsPage
56 SpecialSettingsPage::SpecialSettingsPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& _rCoreAttrs, const DataSourceMetaData& _rDSMeta)
57 : OGenericAdministrationPage(pPage, pController, u"dbaccess/ui/specialsettingspage.ui"_ustr, u"SpecialSettingsPage"_ustr, _rCoreAttrs)
58 , m_aBooleanSettings {
59 { m_xIsSQL92Check, u"usesql92"_ustr, DSID_SQL92CHECK, false, false },
60 { m_xAppendTableAlias, u"append"_ustr, DSID_APPEND_TABLE_ALIAS, false, false },
61 { m_xAsBeforeCorrelationName, u"useas"_ustr, DSID_AS_BEFORE_CORRNAME, false, false },
62 { m_xEnableOuterJoin, u"useoj"_ustr, DSID_ENABLEOUTERJOIN, false, false },
63 { m_xIgnoreDriverPrivileges, u"ignoreprivs"_ustr, DSID_IGNOREDRIVER_PRIV, false, false },
64 { m_xParameterSubstitution, u"replaceparams"_ustr, DSID_PARAMETERNAMESUBST, false, false },
65 { m_xSuppressVersionColumn, u"displayver"_ustr, DSID_SUPPRESSVERSIONCL, true, false },
66 { m_xCatalog, u"usecatalogname"_ustr, DSID_CATALOG, false, false },
67 { m_xSchema, u"useschemaname"_ustr, DSID_SCHEMA, false, false },
68 { m_xIndexAppendix, u"createindex"_ustr, DSID_INDEXAPPENDIX, false, false },
69 { m_xDosLineEnds, u"eol"_ustr, DSID_DOSLINEENDS, false, false },
70 { m_xCheckRequiredFields, u"inputchecks"_ustr, DSID_CHECK_REQUIRED_FIELDS, false, false },
71 { m_xIgnoreCurrency, u"ignorecurrency"_ustr, DSID_IGNORECURRENCY, false, false },
72 { m_xEscapeDateTime, u"useodbcliterals"_ustr, DSID_ESCAPE_DATETIME, false, false },
73 { m_xPrimaryKeySupport, u"primarykeys"_ustr, DSID_PRIMARY_KEY_SUPPORT, false, false },
74 { m_xRespectDriverResultSetType, u"resulttype"_ustr, DSID_RESPECTRESULTSETTYPE, false, false } }
75 , m_bHasBooleanComparisonMode( _rDSMeta.getFeatureSet().has( DSID_BOOLEANCOMPARISON ) )
76 , m_bHasMaxRowScan( _rDSMeta.getFeatureSet().has( DSID_MAX_ROW_SCAN ) )
78 const FeatureSet& rFeatures( _rDSMeta.getFeatureSet() );
79 // create all the check boxes for the boolean settings
80 for (auto & booleanSetting : m_aBooleanSettings)
82 sal_uInt16 nItemId = booleanSetting.nItemId;
83 if ( rFeatures.has( nItemId ) )
85 // check whether this must be a tristate check box
86 const SfxPoolItem& rItem = _rCoreAttrs.Get(nItemId);
87 booleanSetting.bOptionalBool = dynamic_cast<const OptionalBoolItem*>(&rItem) != nullptr;
88 booleanSetting.xControl = m_xBuilder->weld_check_button(booleanSetting.sControlId);
89 if (booleanSetting.bOptionalBool)
90 booleanSetting.xControl->connect_toggled(LINK(this, SpecialSettingsPage, OnTriStateToggleHdl));
91 else
92 booleanSetting.xControl->connect_toggled(LINK(this, SpecialSettingsPage, OnToggleHdl));
93 booleanSetting.xControl->show();
97 // create the controls for the boolean comparison mode
98 if ( m_bHasBooleanComparisonMode )
100 m_xBooleanComparisonModeLabel = m_xBuilder->weld_label(u"comparisonft"_ustr);
101 m_xBooleanComparisonMode = m_xBuilder->weld_combo_box(u"comparison"_ustr);
102 m_xBooleanComparisonMode->connect_changed(LINK(this, SpecialSettingsPage, BooleanComparisonSelectHdl));
103 m_xBooleanComparisonModeLabel->show();
104 m_xBooleanComparisonMode->show();
106 // create the controls for the max row scan
107 if ( m_bHasMaxRowScan )
109 m_xMaxRowScanLabel = m_xBuilder->weld_label(u"rowsft"_ustr);
110 m_xMaxRowScan = m_xBuilder->weld_spin_button(u"rows"_ustr);
111 m_xMaxRowScan->connect_value_changed(LINK(this, OGenericAdministrationPage, OnControlSpinButtonModifyHdl));
112 m_xMaxRowScanLabel->show();
113 m_xMaxRowScan->show();
117 IMPL_LINK(SpecialSettingsPage, OnTriStateToggleHdl, weld::Toggleable&, rToggle, void)
119 auto eOldState = m_aTriStates[&rToggle];
120 switch (eOldState)
122 case TRISTATE_INDET:
123 rToggle.set_state(TRISTATE_FALSE);
124 break;
125 case TRISTATE_TRUE:
126 rToggle.set_state(TRISTATE_INDET);
127 break;
128 case TRISTATE_FALSE:
129 rToggle.set_state(TRISTATE_TRUE);
130 break;
132 m_aTriStates[&rToggle] = rToggle.get_state();
133 OnToggleHdl(rToggle);
136 IMPL_LINK(SpecialSettingsPage, OnToggleHdl, weld::Toggleable&, rBtn, void)
138 if (&rBtn == m_xAppendTableAlias.get() && m_xAsBeforeCorrelationName)
140 // make m_xAsBeforeCorrelationName depend on m_xAppendTableAlias
141 m_xAsBeforeCorrelationName->set_sensitive(m_xAppendTableAlias->get_active());
143 OnControlModifiedButtonClick(rBtn);
146 IMPL_LINK(SpecialSettingsPage, BooleanComparisonSelectHdl, weld::ComboBox&, rControl, void)
148 callModifiedHdl(&rControl);
151 SpecialSettingsPage::~SpecialSettingsPage()
155 void SpecialSettingsPage::fillWindows( std::vector< std::unique_ptr<ISaveValueWrapper> >& _rControlList )
157 if ( m_bHasBooleanComparisonMode )
159 _rControlList.emplace_back(new ODisableWidgetWrapper<weld::Label>(m_xBooleanComparisonModeLabel.get()));
161 if ( m_bHasMaxRowScan )
163 _rControlList.emplace_back(new ODisableWidgetWrapper<weld::Label>(m_xMaxRowScanLabel.get()));
167 void SpecialSettingsPage::fillControls(std::vector< std::unique_ptr<ISaveValueWrapper> >& _rControlList)
169 for (auto const& booleanSetting : m_aBooleanSettings)
171 if (booleanSetting.xControl)
173 _rControlList.emplace_back(new OSaveValueWidgetWrapper<weld::Toggleable>(booleanSetting.xControl.get()));
177 if ( m_bHasBooleanComparisonMode )
178 _rControlList.emplace_back(new OSaveValueWidgetWrapper<weld::ComboBox>(m_xBooleanComparisonMode.get()));
179 if ( m_bHasMaxRowScan )
180 _rControlList.emplace_back(new OSaveValueWidgetWrapper<weld::SpinButton>(m_xMaxRowScan.get()));
183 void SpecialSettingsPage::implInitControls(const SfxItemSet& _rSet, bool _bSaveValue)
185 // check whether or not the selection is invalid or readonly (invalid implies readonly, but not vice versa)
186 bool bValid, bReadonly;
187 getFlags( _rSet, bValid, bReadonly );
189 if ( !bValid )
191 OGenericAdministrationPage::implInitControls(_rSet, _bSaveValue);
192 return;
195 m_aTriStates.clear();
197 // the boolean items
198 for (auto const& booleanSetting : m_aBooleanSettings)
200 if (!booleanSetting.xControl)
201 continue;
203 bool bTriState = false;
205 std::optional<bool> aValue;
207 const SfxPoolItem* pItem = _rSet.GetItem<SfxPoolItem>(booleanSetting.nItemId);
208 if (const SfxBoolItem *pBoolItem = dynamic_cast<const SfxBoolItem*>( pItem) )
210 aValue = pBoolItem->GetValue();
212 else if (const OptionalBoolItem *pOptionalItem = dynamic_cast<const OptionalBoolItem*>( pItem) )
214 aValue = pOptionalItem->GetFullValue();
215 bTriState = true;
217 else
218 OSL_FAIL( "SpecialSettingsPage::implInitControls: unknown boolean item type!" );
220 if ( !aValue.has_value() )
222 booleanSetting.xControl->set_state(TRISTATE_INDET);
224 else
226 bool bValue = *aValue;
227 if ( booleanSetting.bInvertedDisplay )
228 bValue = !bValue;
229 booleanSetting.xControl->set_active(bValue);
231 if (bTriState)
232 m_aTriStates[booleanSetting.xControl.get()] = booleanSetting.xControl->get_state();
235 if (m_xAppendTableAlias && m_xAsBeforeCorrelationName)
237 // make m_xAsBeforeCorrelationName depend on m_xAppendTableAlias
238 m_xAsBeforeCorrelationName->set_sensitive(m_xAppendTableAlias->get_active());
241 // the non-boolean items
242 if ( m_bHasBooleanComparisonMode )
244 const SfxInt32Item* pBooleanComparison = _rSet.GetItem<SfxInt32Item>(DSID_BOOLEANCOMPARISON);
245 m_xBooleanComparisonMode->set_active(static_cast<sal_uInt16>(pBooleanComparison->GetValue()));
248 if ( m_bHasMaxRowScan )
250 const SfxInt32Item* pMaxRowScan = _rSet.GetItem<SfxInt32Item>(DSID_MAX_ROW_SCAN);
251 m_xMaxRowScan->set_value(pMaxRowScan->GetValue());
254 OGenericAdministrationPage::implInitControls(_rSet, _bSaveValue);
257 bool SpecialSettingsPage::FillItemSet( SfxItemSet* _rSet )
259 bool bChangedSomething = false;
261 // the boolean items
262 for (auto const& booleanSetting : m_aBooleanSettings)
264 if (!booleanSetting.xControl)
265 continue;
266 fillBool(*_rSet, booleanSetting.xControl.get(), booleanSetting.nItemId, booleanSetting.bOptionalBool, bChangedSomething, booleanSetting.bInvertedDisplay);
269 // the non-boolean items
270 if ( m_bHasBooleanComparisonMode )
272 if (m_xBooleanComparisonMode->get_value_changed_from_saved())
274 _rSet->Put(SfxInt32Item(DSID_BOOLEANCOMPARISON, m_xBooleanComparisonMode->get_active()));
275 bChangedSomething = true;
278 if ( m_bHasMaxRowScan )
280 fillInt32(*_rSet,m_xMaxRowScan.get(),DSID_MAX_ROW_SCAN,bChangedSomething);
282 return bChangedSomething;
285 // GeneratedValuesPage
286 GeneratedValuesPage::GeneratedValuesPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& _rCoreAttrs)
287 : OGenericAdministrationPage(pPage, pController, u"dbaccess/ui/generatedvaluespage.ui"_ustr, u"GeneratedValuesPage"_ustr, _rCoreAttrs)
288 , m_xAutoRetrievingEnabled(m_xBuilder->weld_check_button(u"autoretrieve"_ustr))
289 , m_xGrid(m_xBuilder->weld_widget(u"grid"_ustr))
290 , m_xAutoIncrement(m_xBuilder->weld_entry(u"statement"_ustr))
291 , m_xAutoRetrieving(m_xBuilder->weld_entry(u"query"_ustr))
293 m_xAutoRetrievingEnabled->connect_toggled(LINK(this, GeneratedValuesPage, OnAutoToggleHdl));
294 m_xAutoIncrement->connect_changed(LINK(this, OGenericAdministrationPage, OnControlEntryModifyHdl));
295 m_xAutoRetrieving->connect_changed(LINK(this, OGenericAdministrationPage, OnControlEntryModifyHdl));
298 IMPL_LINK(GeneratedValuesPage, OnAutoToggleHdl, weld::Toggleable&, rBtn, void)
300 m_xGrid->set_sensitive(rBtn.get_active());
301 OnControlModifiedButtonClick(rBtn);
304 GeneratedValuesPage::~GeneratedValuesPage()
308 void GeneratedValuesPage::fillWindows( std::vector< std::unique_ptr<ISaveValueWrapper> >& _rControlList )
310 _rControlList.emplace_back(new ODisableWidgetWrapper<weld::Widget>(m_xContainer.get()));
313 void GeneratedValuesPage::fillControls( std::vector< std::unique_ptr<ISaveValueWrapper> >& _rControlList )
315 _rControlList.emplace_back( new OSaveValueWidgetWrapper<weld::Toggleable>( m_xAutoRetrievingEnabled.get() ) );
316 _rControlList.emplace_back( new OSaveValueWidgetWrapper<weld::Entry>( m_xAutoIncrement.get() ) );
317 _rControlList.emplace_back( new OSaveValueWidgetWrapper<weld::Entry>( m_xAutoRetrieving.get() ) );
320 void GeneratedValuesPage::implInitControls( const SfxItemSet& _rSet, bool _bSaveValue )
322 // check whether or not the selection is invalid or readonly (invalid implies readonly, but not vice versa)
323 bool bValid, bReadonly;
324 getFlags(_rSet, bValid, bReadonly);
326 // collect the items
327 const SfxStringItem* pAutoIncrementItem = _rSet.GetItem<SfxStringItem>(DSID_AUTOINCREMENTVALUE);
328 const SfxStringItem* pAutoRetrieveValueItem = _rSet.GetItem<SfxStringItem>(DSID_AUTORETRIEVEVALUE);
329 const SfxBoolItem* pAutoRetrieveEnabledItem = _rSet.GetItem<SfxBoolItem>(DSID_AUTORETRIEVEENABLED);
331 // forward the values to the controls
332 if (bValid)
334 bool bEnabled = pAutoRetrieveEnabledItem->GetValue();
335 m_xAutoRetrievingEnabled->set_active(bEnabled);
337 m_xAutoIncrement->set_text(pAutoIncrementItem->GetValue());
338 m_xAutoIncrement->save_value();
339 m_xAutoRetrieving->set_text(pAutoRetrieveValueItem->GetValue());
340 m_xAutoRetrieving->save_value();
342 OGenericAdministrationPage::implInitControls( _rSet, _bSaveValue );
345 bool GeneratedValuesPage::FillItemSet(SfxItemSet* _rSet)
347 bool bChangedSomething = false;
349 fillString( *_rSet, m_xAutoIncrement.get(), DSID_AUTOINCREMENTVALUE, bChangedSomething );
350 fillBool( *_rSet, m_xAutoRetrievingEnabled.get(), DSID_AUTORETRIEVEENABLED, false, bChangedSomething );
351 fillString( *_rSet, m_xAutoRetrieving.get(), DSID_AUTORETRIEVEVALUE, bChangedSomething );
353 return bChangedSomething;
356 // AdvancedSettingsDialog
357 AdvancedSettingsDialog::AdvancedSettingsDialog(weld::Window* pParent, SfxItemSet* _pItems,
358 const Reference< XComponentContext >& _rxContext, const Any& _aDataSourceName )
359 : SfxTabDialogController(pParent, u"dbaccess/ui/advancedsettingsdialog.ui"_ustr, u"AdvancedSettingsDialog"_ustr, _pItems)
361 m_pImpl.reset(new ODbDataSourceAdministrationHelper(_rxContext, m_xDialog.get(), pParent, this));
362 m_pImpl->setDataSourceOrName(_aDataSourceName);
363 Reference< XPropertySet > xDatasource = m_pImpl->getCurrentDataSource();
364 m_pImpl->translateProperties(xDatasource, *_pItems);
365 SetInputSet(_pItems);
366 // propagate this set as our new input set and reset the example set
367 m_xExampleSet.reset(new SfxItemSet(*GetInputSetImpl()));
369 const OUString eType = dbaui::ODbDataSourceAdministrationHelper::getDatasourceType(*_pItems);
371 DataSourceMetaData aMeta( eType );
372 const FeatureSet& rFeatures( aMeta.getFeatureSet() );
374 // auto-generated values?
375 if (rFeatures.supportsGeneratedValues())
376 AddTabPage(u"generated"_ustr, ODriversSettings::CreateGeneratedValuesPage, nullptr);
377 else
378 RemoveTabPage(u"generated"_ustr);
380 // any "special settings"?
381 if (rFeatures.supportsAnySpecialSetting())
382 AddTabPage(u"special"_ustr, ODriversSettings::CreateSpecialSettingsPage, nullptr);
383 else
384 RemoveTabPage(u"special"_ustr);
386 // remove the reset button - it's meaning is much too ambiguous in this dialog
387 RemoveResetButton();
390 AdvancedSettingsDialog::~AdvancedSettingsDialog()
392 SetInputSet(nullptr);
395 bool AdvancedSettingsDialog::doesHaveAnyAdvancedSettings( const OUString& _sURL )
397 DataSourceMetaData aMeta( _sURL );
398 const FeatureSet& rFeatures( aMeta.getFeatureSet() );
399 return rFeatures.supportsGeneratedValues() || rFeatures.supportsAnySpecialSetting();
402 short AdvancedSettingsDialog::Ok()
404 short nRet = SfxTabDialogController::Ok();
405 if ( nRet == RET_OK )
407 m_xExampleSet->Put(*GetOutputItemSet());
408 m_pImpl->saveChanges(*m_xExampleSet);
410 return nRet;
413 void AdvancedSettingsDialog::PageCreated(const OUString& rId, SfxTabPage& _rPage)
415 // register ourself as modified listener
416 static_cast<OGenericAdministrationPage&>(_rPage).SetServiceFactory( getORB() );
417 static_cast<OGenericAdministrationPage&>(_rPage).SetAdminDialog(this,this);
418 SfxTabDialogController::PageCreated(rId, _rPage);
421 const SfxItemSet* AdvancedSettingsDialog::getOutputSet() const
423 return m_xExampleSet.get();
426 SfxItemSet* AdvancedSettingsDialog::getWriteOutputSet()
428 return m_xExampleSet.get();
431 std::pair< Reference< XConnection >, bool > AdvancedSettingsDialog::createConnection()
433 return m_pImpl->createConnection();
436 Reference< XComponentContext > AdvancedSettingsDialog::getORB() const
438 return m_pImpl->getORB();
441 Reference< XDriver > AdvancedSettingsDialog::getDriver()
443 return m_pImpl->getDriver();
446 OUString AdvancedSettingsDialog::getDatasourceType(const SfxItemSet& _rSet) const
448 return dbaui::ODbDataSourceAdministrationHelper::getDatasourceType(_rSet);
451 void AdvancedSettingsDialog::clearPassword()
453 m_pImpl->clearPassword();
456 void AdvancedSettingsDialog::setTitle(const OUString& _sTitle)
458 m_xDialog->set_title(_sTitle);
461 void AdvancedSettingsDialog::enableConfirmSettings( bool ) {}
463 void AdvancedSettingsDialog::saveDatasource()
465 PrepareLeaveCurrentPage();
468 } // namespace dbaui
470 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */