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