LibreOffice Module dbaccess (master) 1
adminpages.cxx
Go to the documentation of this file.
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 "adminpages.hxx"
21#include <core_resource.hxx>
22#include <dbu_dlg.hxx>
23#include <IItemSetHelper.hxx>
24#include <strings.hrc>
25#include <svl/stritem.hxx>
26#include <svl/eitem.hxx>
27#include <svl/intitem.hxx>
28#include <dsitems.hxx>
29#include "dsselect.hxx"
30#include "odbcconfig.hxx"
31#include "optionalboolitem.hxx"
32#include <sqlmessage.hxx>
33#include <com/sun/star/sdbc/XConnection.hpp>
34#include <comphelper/types.hxx>
35#include <vcl/svapp.hxx>
36#include <vcl/weld.hxx>
37
38namespace dbaui
39{
40
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::sdbc;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::lang;
45 using namespace ::dbtools;
46
48 {
49 }
50
51 OGenericAdministrationPage::OGenericAdministrationPage(weld::Container* pPage, weld::DialogController* pController, const OUString& rUIXMLDescription, const OUString& rId, const SfxItemSet& rAttrSet)
52 : SfxTabPage(pPage, pController, rUIXMLDescription, rId, &rAttrSet)
53 , m_abEnableRoadmap(false)
54 , m_pAdminDialog(nullptr)
55 , m_pItemSetHelper(nullptr)
56 {
58
59 m_xContainer->set_size_request(m_xContainer->get_approximate_digit_width() * WIZARD_PAGE_X,
60 m_xContainer->get_text_height() * WIZARD_PAGE_Y);
61 }
62
64 {
65 if (_pSet)
66 {
67 if (!prepareLeave())
68 return DeactivateRC::KeepPage;
69 FillItemSet(_pSet);
70 }
71
72 return DeactivateRC::LeavePage;
73 }
74
76 {
77 implInitControls(*_rCoreAttrs, false);
78 }
79
81 {
83 OSL_ENSURE(m_pItemSetHelper,"NO ItemSetHelper set!");
84 if ( m_pItemSetHelper )
86 }
87
89 {
90 implInitControls(_rSet, true);
91 }
92
93 void OGenericAdministrationPage::getFlags(const SfxItemSet& _rSet, bool& _rValid, bool& _rReadonly)
94 {
95 const SfxBoolItem* pInvalid = _rSet.GetItem<SfxBoolItem>(DSID_INVALID_SELECTION);
96 _rValid = !pInvalid || !pInvalid->GetValue();
97 const SfxBoolItem* pReadonly = _rSet.GetItem<SfxBoolItem>(DSID_READONLY);
98 _rReadonly = !_rValid || (pReadonly && pReadonly->GetValue());
99 }
100
101 IMPL_LINK(OGenericAdministrationPage, OnControlModified, weld::Widget*, pCtrl, void)
102 {
103 callModifiedHdl(pCtrl);
104 }
105
106 IMPL_LINK(OGenericAdministrationPage, OnControlModifiedButtonClick, weld::Toggleable&, rCtrl, void)
107 {
108 callModifiedHdl(&rCtrl);
109 }
110
111 IMPL_LINK(OGenericAdministrationPage, OnControlEntryModifyHdl, weld::Entry&, rCtrl, void)
112 {
113 callModifiedHdl(&rCtrl);
114 }
115
116 IMPL_LINK(OGenericAdministrationPage, OnControlSpinButtonModifyHdl, weld::SpinButton&, rCtrl, void)
117 {
118 callModifiedHdl(&rCtrl);
119 }
120
121 bool OGenericAdministrationPage::getSelectedDataSource(OUString& _sReturn, OUString const & _sCurr)
122 {
123 // collect all ODBC data source names
124 std::set<OUString> aOdbcDatasources;
125 OOdbcEnumeration aEnumeration;
126 if (!aEnumeration.isLoaded())
127 {
128 // show an error message
129 OUString sError(DBA_RES(STR_COULD_NOT_LOAD_ODBC_LIB));
130 sError = sError.replaceFirst("#lib#", aEnumeration.getLibraryName());
131 std::unique_ptr<weld::MessageDialog> xDialog(Application::CreateMessageDialog(GetFrameWeld(),
132 VclMessageType::Warning, VclButtonsType::Ok,
133 sError));
134 xDialog->run();
135 return false;
136 }
137 else
138 {
139 aEnumeration.getDatasourceNames(aOdbcDatasources);
140 // execute the select dialog
141 ODatasourceSelectDialog aSelector(GetFrameWeld(), aOdbcDatasources);
142 if (!_sCurr.isEmpty())
143 aSelector.Select(_sCurr);
144 if (RET_OK == aSelector.run())
145 _sReturn = aSelector.GetSelected();
146 }
147 return true;
148 }
149
150 void OGenericAdministrationPage::implInitControls(const SfxItemSet& _rSet, bool _bSaveValue)
151 {
152 // check whether or not the selection is invalid or readonly (invalid implies readonly, but not vice versa)
153 bool bValid, bReadonly;
154 getFlags(_rSet, bValid, bReadonly);
155
156 std::vector< std::unique_ptr<ISaveValueWrapper> > aControlList;
157 if ( _bSaveValue )
158 {
159 fillControls(aControlList);
160 for( const auto& pValueWrapper : aControlList )
161 {
162 pValueWrapper->SaveValue();
163 }
164 }
165
166 if ( bReadonly )
167 {
168 fillWindows(aControlList);
169 for( const auto& pValueWrapper : aControlList )
170 {
171 pValueWrapper->Disable();
172 }
173 }
174 }
175
177 {
178 OSL_ENSURE(m_pItemSetHelper,"NO ItemSetHelper set!");
179 if ( m_pItemSetHelper )
181 }
183 {
184 return true;
185 }
187 {
188 return true;
189 }
190 void OGenericAdministrationPage::fillBool( SfxItemSet& _rSet, const weld::CheckButton* pCheckBox, sal_uInt16 _nID, bool bOptionalBool, bool& _bChangedSomething, bool _bRevertValue )
191 {
192 if (!(pCheckBox && pCheckBox->get_state_changed_from_saved()))
193 return;
194
195 bool bValue = pCheckBox->get_active();
196 if ( _bRevertValue )
197 bValue = !bValue;
198
199 if (bOptionalBool)
200 {
201 OptionalBoolItem aValue( _nID );
202 if ( pCheckBox->get_state() != TRISTATE_INDET )
203 aValue.SetValue( bValue );
204 _rSet.Put( aValue );
205 }
206 else
207 _rSet.Put( SfxBoolItem( _nID, bValue ) );
208
209 _bChangedSomething = true;
210 }
211 void OGenericAdministrationPage::fillInt32(SfxItemSet& _rSet, const weld::SpinButton* pEdit, TypedWhichId<SfxInt32Item> _nID, bool& _bChangedSomething)
212 {
213 if (pEdit && pEdit->get_value_changed_from_saved())
214 {
215 _rSet.Put(SfxInt32Item(_nID, pEdit->get_value()));
216 _bChangedSomething = true;
217 }
218 }
219 void OGenericAdministrationPage::fillString(SfxItemSet& _rSet, const weld::Entry* pEdit, TypedWhichId<SfxStringItem> _nID, bool& _bChangedSomething)
220 {
221 if (pEdit && pEdit->get_value_changed_from_saved())
222 {
223 _rSet.Put(SfxStringItem(_nID, pEdit->get_text().trim()));
224 _bChangedSomething = true;
225 }
226 }
228 {
229 if (pEdit && pEdit->get_value_changed_from_saved())
230 {
231 _rSet.Put(SfxStringItem(_nID, pEdit->GetText().trim()));
232 _bChangedSomething = true;
233 }
234 }
235
236 IMPL_LINK_NOARG(OGenericAdministrationPage, OnTestConnectionButtonClickHdl, weld::Button&, void)
237 {
238 OSL_ENSURE(m_pAdminDialog,"No Admin dialog set! ->GPF");
239 bool bSuccess = false;
240 if ( !m_pAdminDialog )
241 return;
242
243 m_pAdminDialog->saveDatasource();
244 OGenericAdministrationPage::implInitControls(*m_pItemSetHelper->getOutputSet(), true);
245 bool bShowMessage = true;
246 try
247 {
248 std::pair< Reference<XConnection>,bool> aConnectionPair = m_pAdminDialog->createConnection();
249 bShowMessage = aConnectionPair.second;
250 bSuccess = aConnectionPair.first.is();
251 ::comphelper::disposeComponent(aConnectionPair.first);
252 }
253 catch(Exception&)
254 {
255 }
256 if ( bShowMessage )
257 {
259 OUString aMessage,sTitle;
260 sTitle = DBA_RES(STR_CONNECTION_TEST);
261 if ( bSuccess )
262 {
263 aMessage = DBA_RES(STR_CONNECTION_SUCCESS);
264 }
265 else
266 {
267 eImage = MessageType::Error;
268 aMessage = DBA_RES(STR_CONNECTION_NO_SUCCESS);
269 }
270 OSQLMessageBox aMsg(GetFrameWeld(), sTitle, aMessage, MessBoxStyle::Ok, eImage);
271 aMsg.run();
272 }
273 if ( !bSuccess )
274 m_pAdminDialog->clearPassword();
275 }
276} // namespace dbaui
277
278/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
virtual void Activate()
bool GetValue() const
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
void SetExchangeSupport()
virtual bool FillItemSet(SfxItemSet *)
weld::Window * GetFrameWeld() const
virtual const SfxItemSet * getOutputSet() const =0
virtual ~ISaveValueWrapper()=0
Definition: adminpages.cxx:47
bool get_value_changed_from_saved() const
Definition: curledit.hxx:60
OUString GetText() const
Definition: curledit.cxx:76
OUString GetSelected() const
Definition: dsselect.hxx:42
void Select(const OUString &_rEntry)
Definition: dsselect.hxx:43
virtual short run() override
Definition: dsselect.cxx:59
OGenericAdministrationPage(weld::Container *pPage, weld::DialogController *pController, const OUString &rUIXMLDescription, const OUString &rId, const SfxItemSet &rAttrSet)
Definition: adminpages.cxx:51
virtual void fillWindows(std::vector< std::unique_ptr< ISaveValueWrapper > > &_rControlList)=0
will be called inside <method>implInitControls</method> to disable if necessary
virtual void Activate() override
Definition: adminpages.cxx:80
static void getFlags(const SfxItemSet &_rSet, bool &_rValid, bool &_rReadonly)
analyze the invalid and the readonly flag which may be present in the set
Definition: adminpages.cxx:93
virtual void ActivatePage(const SfxItemSet &_rSet) override
default implementation: call implInitControls with the given item set and _bSaveValue = sal_True
Definition: adminpages.cxx:88
virtual void Reset(const SfxItemSet *_rCoreAttrs) override
default implementation: call implInitControls with the given item set and _bSaveValue = sal_False
Definition: adminpages.cxx:75
virtual void fillControls(std::vector< std::unique_ptr< ISaveValueWrapper > > &_rControlList)=0
will be called inside <method>implInitControls</method> to save the value if necessary
virtual void initializePage() override
Definition: adminpages.cxx:176
static void fillString(SfxItemSet &_rSet, const weld::Entry *pEdit, TypedWhichId< SfxStringItem > _nID, bool &_bChangedSomething)
fills the String value into the item set when the value changed.
Definition: adminpages.cxx:219
virtual bool prepareLeave()
called from within DeactivatePage. The page is allowed to be deactivated if this method returns sal_T...
Definition: adminpages.hxx:160
virtual bool canAdvance() const override
Definition: adminpages.cxx:186
virtual DeactivateRC DeactivatePage(SfxItemSet *pSet) override
default implementation: call FillItemSet, call prepareLeave,
Definition: adminpages.cxx:63
virtual bool commitPage(::vcl::WizardTypes::CommitPageReason _eReason) override
Definition: adminpages.cxx:182
bool getSelectedDataSource(OUString &_sReturn, OUString const &_sCurr)
opens a dialog filled with all data sources available for this type and returns the selected on.
Definition: adminpages.cxx:121
virtual void implInitControls(const SfxItemSet &_rSet, bool _bSaveValue)
called from within Reset and ActivatePage, use to initialize the controls with the items from the giv...
Definition: adminpages.cxx:150
static void fillBool(SfxItemSet &_rSet, const weld::CheckButton *pCheckBox, sal_uInt16 _nID, bool bOptionalBool, bool &_bChangedSomething, bool _bRevertValue=false)
fills the Boolean value into the item set when the value changed.
Definition: adminpages.cxx:190
static void fillInt32(SfxItemSet &_rSet, const weld::SpinButton *pEdit, TypedWhichId< SfxInt32Item > _nID, bool &_bChangedSomething)
fills the int value into the item set when the value changed.
Definition: adminpages.cxx:211
const OUString & getLibraryName() const
Definition: odbcconfig.hxx:68
void getDatasourceNames(std::set< OUString > &_rNames)
Definition: odbcconfig.cxx:184
void SetValue(bool _bValue)
virtual short run()
virtual OUString get_text() const=0
bool get_value_changed_from_saved() const
virtual sal_Int64 get_value() const=0
virtual bool get_active() const=0
bool get_state_changed_from_saved() const
TriState get_state() const
#define DBA_RES(id)
#define WIZARD_PAGE_X
Definition: dbu_dlg.hxx:21
#define WIZARD_PAGE_Y
Definition: dbu_dlg.hxx:22
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
#define DSID_READONLY
Definition: dsitems.hxx:40
#define DSID_INVALID_SELECTION
Definition: dsitems.hxx:39
TRISTATE_INDET
@ Exception
IMPL_LINK_NOARG(OApplicationController, OnClipboardChanged, TransferableDataHelper *, void)
IMPL_LINK(OApplicationController, OnSelectContainer, void *, _pType, void)
Reference< XNameAccess > m_xContainer
Definition: objectnames.cxx:80
DeactivateRC
RET_OK