1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbui.hxx"
26 
27 #include "adminpages.hxx"
28 #include "dbadmin.hrc"
29 #include "dbadmin.hxx"
30 #include "dbu_dlg.hrc"
31 #include <svl/stritem.hxx>
32 #include <svl/eitem.hxx>
33 #include <svl/intitem.hxx>
34 #include "dbustrings.hrc"
35 #include "dsitems.hxx"
36 #include "dsselect.hxx"
37 #include "localresaccess.hxx"
38 #include "odbcconfig.hxx"
39 #include "optionalboolitem.hxx"
40 #include "sqlmessage.hxx"
41 
42 #include <osl/file.hxx>
43 #include <svl/eitem.hxx>
44 #include <svl/intitem.hxx>
45 #include <svl/stritem.hxx>
46 #include <vcl/accel.hxx>
47 #include <vcl/button.hxx>
48 #include <vcl/edit.hxx>
49 #include <vcl/field.hxx>
50 #include <vcl/lstbox.hxx>
51 #include <vcl/msgbox.hxx>
52 
53 #include <algorithm>
54 #include <stdlib.h>
55 
56 //.........................................................................
57 namespace dbaui
58 {
59 //.........................................................................
60 
61 	using namespace ::com::sun::star::uno;
62 	using namespace ::com::sun::star::sdbc;
63 	using namespace ::com::sun::star::beans;
64 	using namespace ::com::sun::star::lang;
65 	using namespace ::dbtools;
66 	using namespace ::svt;
67 
68 
69 	//=========================================================================
70 	//= OGenericAdministrationPage
71 	//=========================================================================
DBG_NAME(OGenericAdministrationPage)72     DBG_NAME(OGenericAdministrationPage)
73     //-------------------------------------------------------------------------
74 	OGenericAdministrationPage::OGenericAdministrationPage(Window* _pParent, const ResId& _rId, const SfxItemSet& _rAttrSet)
75 		:SfxTabPage(_pParent, _rId, _rAttrSet)
76 		,m_abEnableRoadmap(sal_False)
77 		,m_pAdminDialog(NULL)
78 		,m_pItemSetHelper(NULL)
79 		,m_pFT_HeaderText(NULL)
80 	{
81         DBG_CTOR(OGenericAdministrationPage,NULL);
82 
83 		SetExchangeSupport(sal_True);
84 	}
85 
86 	//-------------------------------------------------------------------------
~OGenericAdministrationPage()87 	OGenericAdministrationPage::~OGenericAdministrationPage()
88 	{
89 		DELETEZ(m_pFT_HeaderText);
90 
91         DBG_DTOR(OGenericAdministrationPage,NULL);
92     }
93 
94 	//-------------------------------------------------------------------------
DeactivatePage(SfxItemSet * _pSet)95 	int OGenericAdministrationPage::DeactivatePage(SfxItemSet* _pSet)
96 	{
97 		if (_pSet)
98 		{
99 			if (!prepareLeave())
100 				return KEEP_PAGE;
101 			FillItemSet(*_pSet);
102 		}
103 
104 		return LEAVE_PAGE;
105 	}
106 
107 	//-------------------------------------------------------------------------
Reset(const SfxItemSet & _rCoreAttrs)108 	void OGenericAdministrationPage::Reset(const SfxItemSet& _rCoreAttrs)
109 	{
110 		implInitControls(_rCoreAttrs, sal_False);
111 	}
112 	//-------------------------------------------------------------------------
ActivatePage()113 	void OGenericAdministrationPage::ActivatePage()
114 	{
115 		TabPage::ActivatePage();
116 		OSL_ENSURE(m_pItemSetHelper,"NO ItemSetHelper set!");
117 		if ( m_pItemSetHelper )
118 			ActivatePage(*m_pItemSetHelper->getOutputSet());
119 	}
120 	//-------------------------------------------------------------------------
ActivatePage(const SfxItemSet & _rSet)121 	void OGenericAdministrationPage::ActivatePage(const SfxItemSet& _rSet)
122 	{
123 		implInitControls(_rSet, sal_True);
124 	}
125 
126 	// -----------------------------------------------------------------------
getFlags(const SfxItemSet & _rSet,sal_Bool & _rValid,sal_Bool & _rReadonly)127 	void OGenericAdministrationPage::getFlags(const SfxItemSet& _rSet, sal_Bool& _rValid, sal_Bool& _rReadonly)
128 	{
129 		SFX_ITEMSET_GET(_rSet, pInvalid, SfxBoolItem, DSID_INVALID_SELECTION, sal_True);
130 		_rValid = !pInvalid || !pInvalid->GetValue();
131 		SFX_ITEMSET_GET(_rSet, pReadonly, SfxBoolItem, DSID_READONLY, sal_True);
132 		_rReadonly = !_rValid || (pReadonly && pReadonly->GetValue());
133 	}
134 
135 
136 	// -----------------------------------------------------------------------
IMPL_LINK(OGenericAdministrationPage,OnControlModified,Control *,EMPTYARG)137 	IMPL_LINK(OGenericAdministrationPage, OnControlModified, Control*, EMPTYARG)
138 	{
139 		callModifiedHdl();
140 		return 0L;
141 	}
142 	// -----------------------------------------------------------------------
getSelectedDataSource(::rtl::OUString & _sReturn,::rtl::OUString & _sCurr)143 	sal_Bool OGenericAdministrationPage::getSelectedDataSource(::rtl::OUString& _sReturn,::rtl::OUString& _sCurr)
144 	{
145 		// collect all ODBC data source names
146 		StringBag aOdbcDatasources;
147 		OOdbcEnumeration aEnumeration;
148 		if (!aEnumeration.isLoaded())
149 		{
150 			// show an error message
151             LocalResourceAccess aLocRes( PAGE_GENERAL, RSC_TABPAGE );
152 			String sError(ModuleRes(STR_COULDNOTLOAD_ODBCLIB));
153 			sError.SearchAndReplaceAscii("#lib#", aEnumeration.getLibraryName());
154 			ErrorBox aDialog(this, WB_OK, sError);
155 			aDialog.Execute();
156 			return sal_False;
157 		}
158 		else
159 		{
160 			aEnumeration.getDatasourceNames(aOdbcDatasources);
161 			// excute the select dialog
162 			ODatasourceSelectDialog aSelector(GetParent(), aOdbcDatasources, false);
163 			if (_sCurr.getLength())
164 				aSelector.Select(_sCurr);
165 			if ( RET_OK == aSelector.Execute() )
166 				_sReturn = aSelector.GetSelected();
167 		}
168 		return sal_True;
169 	}
170 
171 	// -----------------------------------------------------------------------
implInitControls(const SfxItemSet & _rSet,sal_Bool _bSaveValue)172 	void OGenericAdministrationPage::implInitControls(const SfxItemSet& _rSet, sal_Bool _bSaveValue)
173 	{
174 		// check whether or not the selection is invalid or readonly (invalid implies readonly, but not vice versa)
175 		sal_Bool bValid, bReadonly;
176 		getFlags(_rSet, bValid, bReadonly);
177 
178 		::std::vector< ISaveValueWrapper* > aControlList;
179 		if ( _bSaveValue )
180 		{
181 			fillControls(aControlList);
182 			::std::for_each(aControlList.begin(),aControlList.end(),TSaveValueWrapperFunctor());
183 		}
184 
185 		if ( bReadonly )
186 		{
187 			fillWindows(aControlList);
188 			::std::for_each(aControlList.begin(),aControlList.end(),TDisableWrapperFunctor());
189 		}
190 
191 		::std::for_each(aControlList.begin(),aControlList.end(),TDeleteWrapperFunctor());
192 		aControlList.clear();
193 	}
194 
195     // -----------------------------------------------------------------------
initializePage()196 	void OGenericAdministrationPage::initializePage()
197 	{
198 		OSL_ENSURE(m_pItemSetHelper,"NO ItemSetHelper set!");
199 		if ( m_pItemSetHelper )
200 			Reset(*m_pItemSetHelper->getOutputSet());
201 	}
202 	// -----------------------------------------------------------------------
commitPage(::svt::WizardTypes::CommitPageReason)203 	sal_Bool OGenericAdministrationPage::commitPage( ::svt::WizardTypes::CommitPageReason )
204 	{
205 		return sal_True;
206 	}
207 	// -----------------------------------------------------------------------
canAdvance() const208     bool OGenericAdministrationPage::canAdvance() const
209     {
210         return true;
211     }
212 	// -----------------------------------------------------------------------
fillBool(SfxItemSet & _rSet,CheckBox * _pCheckBox,sal_uInt16 _nID,sal_Bool & _bChangedSomething,bool _bRevertValue)213 	void OGenericAdministrationPage::fillBool( SfxItemSet& _rSet, CheckBox* _pCheckBox, sal_uInt16 _nID, sal_Bool& _bChangedSomething, bool _bRevertValue )
214 	{
215 		if ( (_pCheckBox != NULL ) && ( _pCheckBox->GetState() != _pCheckBox->GetSavedValue() ) )
216 		{
217             sal_Bool bValue = _pCheckBox->IsChecked();
218             if ( _bRevertValue )
219                 bValue = !bValue;
220 
221             if ( _pCheckBox->IsTriStateEnabled() )
222             {
223                 OptionalBoolItem aValue( _nID );
224                 if ( _pCheckBox->GetState() != STATE_DONTKNOW )
225                     aValue.SetValue( bValue );
226                 _rSet.Put( aValue );
227             }
228             else
229                 _rSet.Put( SfxBoolItem( _nID, bValue ) );
230 
231 			_bChangedSomething = sal_True;
232 		}
233 	}
234 	// -----------------------------------------------------------------------
fillInt32(SfxItemSet & _rSet,NumericField * _pEdit,sal_uInt16 _nID,sal_Bool & _bChangedSomething)235 	void OGenericAdministrationPage::fillInt32(SfxItemSet& _rSet,NumericField* _pEdit,sal_uInt16 _nID,sal_Bool& _bChangedSomething)
236 	{
237 		if( (_pEdit != NULL) && (_pEdit->GetValue() != _pEdit->GetSavedValue().ToInt32()) )
238 		{
239 			_rSet.Put(SfxInt32Item(_nID, static_cast<sal_Int32>(_pEdit->GetValue())));
240 			_bChangedSomething = sal_True;
241 		}
242 	}
243 	// -----------------------------------------------------------------------
fillString(SfxItemSet & _rSet,Edit * _pEdit,sal_uInt16 _nID,sal_Bool & _bChangedSomething)244 	void OGenericAdministrationPage::fillString(SfxItemSet& _rSet,Edit* _pEdit,sal_uInt16 _nID,sal_Bool& _bChangedSomething)
245 	{
246 		if( (_pEdit != NULL) && (_pEdit->GetText() != _pEdit->GetSavedValue()) )
247 		{
248 			_rSet.Put(SfxStringItem(_nID, _pEdit->GetText()));
249 			_bChangedSomething = sal_True;
250 		}
251 	}
252 
SetControlFontWeight(Window * _pWindow,FontWeight _eWeight)253     void OGenericAdministrationPage::SetControlFontWeight(Window* _pWindow, FontWeight _eWeight)
254     {
255         Font aFont = _pWindow->GetControlFont();
256 	    aFont.SetWeight( _eWeight );
257 	    _pWindow->SetControlFont( aFont );
258     }
259 
260     // -----------------------------------------------------------------------
261 	IMPL_LINK(OGenericAdministrationPage, OnTestConnectionClickHdl, PushButton*, /*_pButton*/)
262 	{
263 		OSL_ENSURE(m_pAdminDialog,"No Admin dialog set! ->GPF");
264 		sal_Bool bSuccess = sal_False;
265 		if ( m_pAdminDialog )
266 		{
267 			m_pAdminDialog->saveDatasource();
268 			OGenericAdministrationPage::implInitControls(*m_pItemSetHelper->getOutputSet(), sal_True);
269 			sal_Bool bShowMessage = sal_True;
270 			try
271 			{
272 				::std::pair< Reference<XConnection>,sal_Bool> xConnection = m_pAdminDialog->createConnection();
273 				bShowMessage = xConnection.second;
274 				bSuccess = xConnection.first.is();
275 				::comphelper::disposeComponent(xConnection.first);
276 			}
277 			catch(Exception&)
278 			{
279 			}
280 			if ( bShowMessage )
281 			{
282 				OSQLMessageBox::MessageType eImage = OSQLMessageBox::Info;
283 				String aMessage,sTitle;
284 				sTitle = String (ModuleRes(STR_CONNECTION_TEST));
285 				if ( bSuccess )
286 				{
287 					aMessage = String(ModuleRes(STR_CONNECTION_SUCCESS));
288 				}
289 				else
290 				{
291 					eImage = OSQLMessageBox::Error;
292 					aMessage = String(ModuleRes(STR_CONNECTION_NO_SUCCESS));
293 				}
294 				OSQLMessageBox aMsg( this, sTitle, aMessage, WB_OK, eImage );
295 				aMsg.Execute();
296 			}
297             if ( !bSuccess )
298                 m_pAdminDialog->clearPassword();
299 		}
300 		return 0L;
301 	}
302 
SetHeaderText(sal_uInt16 _nFTResId,sal_uInt16 _StringResId)303     void OGenericAdministrationPage::SetHeaderText( sal_uInt16 _nFTResId, sal_uInt16 _StringResId)
304     {
305         delete(m_pFT_HeaderText);
306         m_pFT_HeaderText = new FixedText(this, ModuleRes(_nFTResId));
307         String sHeaderText = String(ModuleRes(_StringResId));
308         m_pFT_HeaderText->SetText(sHeaderText);
309         SetControlFontWeight(m_pFT_HeaderText);
310     }
311 
312 	//=========================================================================
313 	//= LayoutHelper
314 	//=========================================================================
315 	//-------------------------------------------------------------------------
positionBelow(const Control & _rReference,Control & _rControl,const ControlRelation _eRelation,const long _nIndentAppFont)316     void LayoutHelper::positionBelow( const Control& _rReference, Control& _rControl, const ControlRelation _eRelation,
317         const long _nIndentAppFont )
318     {
319         Point aReference = _rReference.GetPosPixel();
320         aReference.Y() += _rReference.GetSizePixel().Height();
321 
322         const Window* pConverter = _rControl.GetParent();
323         Size aOffset = pConverter->LogicToPixel( Size( _nIndentAppFont, ( _eRelation == RelatedControls ? 3 : 6 ) ), MAP_APPFONT );
324 
325         Point aControlPos( aReference.X() + aOffset.Width(), aReference.Y() + aOffset.Height() );
326         _rControl.SetPosPixel( aControlPos );
327     }
328 
329 	//-------------------------------------------------------------------------
fitSizeRightAligned(PushButton & io_button)330     void LayoutHelper::fitSizeRightAligned( PushButton& io_button )
331     {
332         const Point aOldPos = io_button.GetPosPixel();
333         const Size aOldSize = io_button.GetSizePixel();
334         const Size aMinSize( io_button.CalcMinimumSize() );
335         if ( aMinSize.Width() > aOldSize.Width() )
336         {
337             io_button.SetPosSizePixel(
338                 aOldPos.X() + aOldSize.Width() - aMinSize.Width(),
339                 0,
340                 aMinSize.Width(),
341                 0,
342                 WINDOW_POSSIZE_X | WINDOW_POSSIZE_WIDTH
343             );
344         }
345     }
346 
347 //.........................................................................
348 }	// namespace dbaui
349 //.........................................................................
350 
351