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_cui.hxx"
26 
27 #include "connpooloptions.hxx"
28 #include "connpooloptions.hrc"
29 #include <svtools/editbrowsebox.hxx>
30 #include <vcl/field.hxx>
31 #include "connpoolsettings.hxx"
32 #include <svl/eitem.hxx>
33 #include <cuires.hrc>
34 #include "helpid.hrc"
35 #include <dialmgr.hxx>
36 
37 //........................................................................
38 namespace offapp
39 {
40 //........................................................................
41 
42 	//====================================================================
43 	//= DriverListControl
44 	//====================================================================
45 	typedef ::svt::EditBrowseBox DriverListControl_Base;
46 	class DriverListControl : public DriverListControl_Base
47 	{
48 		using Window::Update;
49 	protected:
50 		DriverPoolingSettings					m_aSavedSettings;
51 		DriverPoolingSettings					m_aSettings;
52 		DriverPoolingSettings::const_iterator	m_aSeekRow;
53 
54 		String									m_sYes;
55 		String									m_sNo;
56 
57 		Link									m_aRowChangeHandler;
58 
59 	public:
60 		DriverListControl( Window* _pParent, const ResId& _rId);
61 
62 		virtual	void Init();
63 				void Update(const DriverPoolingSettings& _rSettings);
64 		virtual String GetCellText( long nRow, sal_uInt16 nColId ) const;
65 
66 		// the handler will be called with a DriverPoolingSettings::const_iterator as parameter,
67 		// or NULL if no valid current row exists
SetRowChangeHandler(const Link & _rHdl)68 		void SetRowChangeHandler(const Link& _rHdl) { m_aRowChangeHandler = _rHdl; }
GetRowChangeHandler() const69 		Link GetRowChangeHandler() const { return m_aRowChangeHandler; }
70 
71 		const DriverPooling* getCurrentRow() const;
72 		DriverPooling* getCurrentRow();
73 		void									updateCurrentRow();
74 
getSettings() const75 		const DriverPoolingSettings& getSettings() const { return m_aSettings; }
76 
saveValue()77 		void		saveValue()				{ m_aSavedSettings = m_aSettings; }
78 		sal_Bool	isModified() const;
79 
80 	protected:
81 		virtual void InitController( ::svt::CellControllerRef& rController, long nRow, sal_uInt16 nCol );
82 		virtual ::svt::CellController* GetController( long nRow, sal_uInt16 nCol );
83 
84 		virtual void PaintCell( OutputDevice& rDev, const Rectangle& rRect, sal_uInt16 nColId ) const;
85 
86 		virtual sal_Bool SeekRow( long nRow );
87 		virtual sal_Bool SaveModified();
88 
89 		virtual sal_Bool IsTabAllowed(sal_Bool _bForward) const;
90 
91 		virtual void StateChanged( StateChangedType nStateChange );
92 
93 		virtual void CursorMoved();
94 
95 	protected:
96 		virtual sal_uInt32 GetTotalCellWidth(long nRow, sal_uInt16 nColId);
97 
98 
99 	private:
100 		String implGetCellText(DriverPoolingSettings::const_iterator _rPos, sal_uInt16 _nColId) const;
101 	};
102 
103 	//--------------------------------------------------------------------
DriverListControl(Window * _pParent,const ResId & _rId)104 	DriverListControl::DriverListControl( Window* _pParent, const ResId& _rId)
105 //		:DriverListControl_Base(_pParent, _rId, DBBF_NOROWPICTURE, BROWSER_AUTO_VSCROLL | BROWSER_AUTO_HSCROLL | BROWSER_COLUMNSELECTION | BROWSER_HLINESFULL | BROWSER_VLINESFULL | BROWSER_HIDESELECT | BROWSER_CURSOR_WO_FOCUS)
106 		:DriverListControl_Base(_pParent, _rId, EBBF_NOROWPICTURE, BROWSER_AUTO_VSCROLL | BROWSER_AUTO_HSCROLL | BROWSER_HIDECURSOR | BROWSER_AUTOSIZE_LASTCOL)
107 		,m_aSeekRow(m_aSettings.end())
108 		,m_sYes(ResId(STR_YES,*_rId.GetResMgr()))
109 		,m_sNo(ResId(STR_NO,*_rId.GetResMgr()))
110 	{
111 		SetStyle((GetStyle() & ~WB_HSCROLL) | WB_AUTOHSCROLL);
112 
113 		SetUniqueId(UID_OFA_CONNPOOL_DRIVERLIST_BACK);
114 		GetDataWindow().SetHelpId(HID_OFA_CONNPOOL_DRIVERLIST);
115 	}
116 
117 	//--------------------------------------------------------------------
IsTabAllowed(sal_Bool) const118     sal_Bool DriverListControl::IsTabAllowed(sal_Bool /*_bForward*/) const
119 	{
120 		// no travinling within the fields via RETURN and TAB
121 		return sal_False;
122 	}
123 
124 	//--------------------------------------------------------------------
isModified() const125 	sal_Bool DriverListControl::isModified() const
126 	{
127 		if (m_aSettings.size() != m_aSavedSettings.size())
128 			return sal_True;
129 
130 		DriverPoolingSettings::const_iterator aCurrent = m_aSettings.begin();
131 		DriverPoolingSettings::const_iterator aCurrentEnd = m_aSettings.end();
132 		DriverPoolingSettings::const_iterator aSaved = m_aSavedSettings.begin();
133 		for (;aCurrent != aCurrentEnd; ++aCurrent, ++aSaved)
134 		{
135 			if (*aCurrent != *aSaved)
136 				return sal_True;
137 		}
138 
139 		return sal_False;
140 	}
141 
142 	//--------------------------------------------------------------------
Init()143 	void DriverListControl::Init()
144 	{
145 		DriverListControl_Base::Init();
146 
147 		Size aColWidth = LogicToPixel(Size(160, 0), MAP_APPFONT);
148 		InsertDataColumn(1, String(CUI_RES(STR_DRIVER_NAME)), aColWidth.Width());
149 		aColWidth = LogicToPixel(Size(30, 0), MAP_APPFONT);
150 		InsertDataColumn(2, String(CUI_RES(STR_POOLED_FLAG)), aColWidth.Width());
151 		aColWidth = LogicToPixel(Size(60, 0), MAP_APPFONT);
152 		InsertDataColumn(3, String(CUI_RES(STR_POOL_TIMEOUT)), aColWidth.Width());
153 			// Attention: the resource of the string is local to the resource of the enclosing dialog!
154 	}
155 
156 	//--------------------------------------------------------------------
CursorMoved()157 	void DriverListControl::CursorMoved()
158 	{
159 		DriverListControl_Base::CursorMoved();
160 
161 		// call the row change handler
162 		if ( m_aRowChangeHandler.IsSet() )
163         {
164             if ( GetCurRow() >= 0 )
165             {   // == -1 may happen in case the browse box has just been cleared
166     			m_aRowChangeHandler.Call( getCurrentRow() );
167             }
168         }
169 	}
170 
171 	//--------------------------------------------------------------------
getCurrentRow() const172 	const DriverPooling* DriverListControl::getCurrentRow() const
173 	{
174 		OSL_ENSURE( ( GetCurRow() < m_aSettings.size() ) && ( GetCurRow() >= 0 ),
175             "DriverListControl::getCurrentRow: invalid current row!");
176 
177         if ( ( GetCurRow() >= 0 ) && ( GetCurRow() < m_aSettings.size() ) )
178 			return &(*(m_aSettings.begin() + GetCurRow()));
179 
180 		return NULL;
181 	}
182 
183 	//--------------------------------------------------------------------
getCurrentRow()184 	DriverPooling* DriverListControl::getCurrentRow()
185 	{
186 		OSL_ENSURE( ( GetCurRow() < m_aSettings.size() ) && ( GetCurRow() >= 0 ),
187 		    "DriverListControl::getCurrentRow: invalid current row!");
188 
189         if ( ( GetCurRow() >= 0 ) && ( GetCurRow() < m_aSettings.size() ) )
190 			return &(*(m_aSettings.begin() + GetCurRow()));
191 
192 		return NULL;
193 	}
194 
195 	//--------------------------------------------------------------------
updateCurrentRow()196 	void DriverListControl::updateCurrentRow()
197 	{
198 		Window::Invalidate( GetRowRectPixel( GetCurRow() ), INVALIDATE_UPDATE );
199 	}
200 
201 	//--------------------------------------------------------------------
Update(const DriverPoolingSettings & _rSettings)202 	void DriverListControl::Update(const DriverPoolingSettings& _rSettings)
203 	{
204 		m_aSettings = _rSettings;
205 
206 		SetUpdateMode(sal_False);
207 		RowRemoved(0, GetRowCount());
208 		RowInserted(0, m_aSettings.size());
209 		SetUpdateMode(sal_True);
210 
211 		ActivateCell(1, 0);
212 	}
213 
214 	//--------------------------------------------------------------------
GetTotalCellWidth(long nRow,sal_uInt16 nColId)215 	sal_uInt32 DriverListControl::GetTotalCellWidth(long nRow, sal_uInt16 nColId)
216 	{
217 		return GetDataWindow().GetTextWidth(GetCellText(nRow, nColId));
218 	}
219 
220 	//--------------------------------------------------------------------
implGetCellText(DriverPoolingSettings::const_iterator _rPos,sal_uInt16 _nColId) const221 	String DriverListControl::implGetCellText(DriverPoolingSettings::const_iterator _rPos, sal_uInt16 _nColId) const
222 	{
223 		OSL_ENSURE(_rPos < m_aSettings.end(), "DriverListControl::implGetCellText: invalid position!");
224 
225 		String sReturn;
226 		switch (_nColId)
227 		{
228 			case 1:
229 				sReturn = _rPos->sName;
230 				break;
231 			case 2:
232 				sReturn = _rPos->bEnabled ? m_sYes : m_sNo;
233 				break;
234 			case 3:
235 				if (_rPos->bEnabled)
236 					sReturn = String::CreateFromInt32(_rPos->nTimeoutSeconds);
237 				break;
238 			default:
239 				OSL_ENSURE(sal_False, "DriverListControl::implGetCellText: invalid column id!");
240 		}
241 		return sReturn;
242 	}
243 
244 	//--------------------------------------------------------------------
StateChanged(StateChangedType nStateChange)245 	void DriverListControl::StateChanged( StateChangedType nStateChange )
246 	{
247 		if (STATE_CHANGE_ENABLE == nStateChange)
248 			Window::Invalidate(INVALIDATE_UPDATE);
249 		DriverListControl_Base::StateChanged( nStateChange );
250 	}
251 
252 	//--------------------------------------------------------------------
GetCellText(long nRow,sal_uInt16 nColId) const253 	String DriverListControl::GetCellText( long nRow, sal_uInt16 nColId ) const
254 	{
255 		String sReturn;
256 		if (nRow > m_aSettings.size())
257 		{
258 			OSL_ENSURE(sal_False, "DriverListControl::GetCellText: don't ask me for such rows!");
259 		}
260 		else
261 		{
262 			sReturn = implGetCellText(m_aSettings.begin() + nRow, nColId);
263 		}
264 		return sReturn;
265 	}
266 
267 	//--------------------------------------------------------------------
InitController(::svt::CellControllerRef & rController,long nRow,sal_uInt16 nCol)268 	void DriverListControl::InitController( ::svt::CellControllerRef& rController, long nRow, sal_uInt16 nCol )
269 	{
270 		rController->GetWindow().SetText(GetCellText(nRow, nCol));
271 	}
272 
273 	//--------------------------------------------------------------------
GetController(long,sal_uInt16)274     ::svt::CellController* DriverListControl::GetController( long /*nRow*/, sal_uInt16 /*nCol*/ )
275 	{
276 		return NULL;
277 	}
278 
279 	//--------------------------------------------------------------------
SaveModified()280 	sal_Bool DriverListControl::SaveModified()
281 	{
282 		return sal_True;
283 	}
284 
285 	//--------------------------------------------------------------------
SeekRow(long _nRow)286 	sal_Bool DriverListControl::SeekRow( long _nRow )
287 	{
288 		DriverListControl_Base::SeekRow(_nRow);
289 
290 		if (_nRow < m_aSettings.size())
291 			m_aSeekRow = m_aSettings.begin() + _nRow;
292 		else
293 			m_aSeekRow = m_aSettings.end();
294 
295 		return m_aSeekRow != m_aSettings.end();
296 	}
297 
298 	//--------------------------------------------------------------------
PaintCell(OutputDevice & rDev,const Rectangle & rRect,sal_uInt16 nColId) const299 	void DriverListControl::PaintCell( OutputDevice& rDev, const Rectangle& rRect, sal_uInt16 nColId ) const
300 	{
301 		OSL_ENSURE(m_aSeekRow != m_aSettings.end(), "DriverListControl::PaintCell: invalid row!");
302 
303 		if (m_aSeekRow != m_aSettings.end())
304 		{
305 			rDev.SetClipRegion(rRect);
306 
307 			sal_uInt16 nStyle = TEXT_DRAW_CLIP;
308 			if (!IsEnabled())
309 				nStyle |= TEXT_DRAW_DISABLE;
310 			switch (nColId)
311 			{
312 				case 1: nStyle |= TEXT_DRAW_LEFT; break;
313 				case 2:
314 				case 3: nStyle |= TEXT_DRAW_CENTER; break;
315 			}
316 
317 			rDev.DrawText(rRect, implGetCellText(m_aSeekRow, nColId), nStyle);
318 
319 			rDev.SetClipRegion();
320 		}
321 	}
322 
323 	//====================================================================
324 	//= ConnectionPoolOptionsPage
325 	//====================================================================
326 	//--------------------------------------------------------------------
ConnectionPoolOptionsPage(Window * _pParent,const SfxItemSet & _rAttrSet)327 	ConnectionPoolOptionsPage::ConnectionPoolOptionsPage(Window* _pParent, const SfxItemSet& _rAttrSet)
328 		:SfxTabPage(_pParent, CUI_RES(RID_OFAPAGE_CONNPOOLOPTIONS ), _rAttrSet)
329 		,m_aFrame				(this,				CUI_RES(FL_POOLING))
330 		,m_aEnablePooling		(this,		CUI_RES(CB_POOL_CONNS))
331 		,m_aDriversLabel		(this,		CUI_RES(FT_DRIVERS))
332 		,m_pDriverList(new DriverListControl(this, CUI_RES(CTRL_DRIVER_LIST)))
333 		,m_aDriverLabel			(this,		CUI_RES(FT_DRIVERLABEL))
334 		,m_aDriver				(this,		CUI_RES(FT_DRIVER))
335 		,m_aDriverPoolingEnabled(this,		CUI_RES(CB_DRIVERPOOLING))
336 		,m_aTimeoutLabel		(this,		CUI_RES(FT_TIMEOUT))
337 		,m_aTimeout				(this,		CUI_RES(NF_TIMEOUT))
338 	{
339 		m_pDriverList->Init();
340 		m_pDriverList->Show();
341 
342 		FreeResource();
343 
344 		m_aEnablePooling.SetClickHdl( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );
345 		m_aDriverPoolingEnabled.SetClickHdl( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );
346 
347 		m_pDriverList->SetRowChangeHandler( LINK(this, ConnectionPoolOptionsPage, OnDriverRowChanged) );
348 	}
349 
350 	//--------------------------------------------------------------------
Create(Window * _pParent,const SfxItemSet & _rAttrSet)351 	SfxTabPage* ConnectionPoolOptionsPage::Create(Window* _pParent, const SfxItemSet& _rAttrSet)
352 	{
353 		return new ConnectionPoolOptionsPage(_pParent, _rAttrSet);
354 	}
355 
356 	//--------------------------------------------------------------------
~ConnectionPoolOptionsPage()357 	ConnectionPoolOptionsPage::~ConnectionPoolOptionsPage()
358 	{
359 		delete m_pDriverList;
360 	}
361 
362 	//--------------------------------------------------------------------
implInitControls(const SfxItemSet & _rSet,sal_Bool)363 	void ConnectionPoolOptionsPage::implInitControls(const SfxItemSet& _rSet, sal_Bool /*_bFromReset*/)
364 	{
365 		// the enabled flag
366 		SFX_ITEMSET_GET( _rSet, pEnabled, SfxBoolItem, SID_SB_POOLING_ENABLED, sal_True );
367 		OSL_ENSURE(pEnabled, "ConnectionPoolOptionsPage::implInitControls: missing the Enabled item!");
368 		m_aEnablePooling.Check(pEnabled ? pEnabled->GetValue() : sal_True);
369 
370 		m_aEnablePooling.SaveValue();
371 
372 		// the settings for the single drivers
373 		SFX_ITEMSET_GET( _rSet, pDriverSettings, DriverPoolingSettingsItem, SID_SB_DRIVER_TIMEOUTS, sal_True );
374 		if (pDriverSettings)
375 			m_pDriverList->Update(pDriverSettings->getSettings());
376 		else
377 		{
378 			OSL_ENSURE(sal_False, "ConnectionPoolOptionsPage::implInitControls: missing the DriverTimeouts item!");
379 			m_pDriverList->Update(DriverPoolingSettings());
380 		}
381 		m_pDriverList->saveValue();
382 
383 		// reflect the new settings
384 		OnEnabledDisabled(&m_aEnablePooling);
385 	}
386 
387 	//--------------------------------------------------------------------
Notify(NotifyEvent & _rNEvt)388 	long ConnectionPoolOptionsPage::Notify( NotifyEvent& _rNEvt )
389 	{
390 		if (EVENT_LOSEFOCUS == _rNEvt.GetType())
391 			if (m_aTimeout.IsWindowOrChild(_rNEvt.GetWindow()))
392 				commitTimeoutField();
393 
394 		return SfxTabPage::Notify(_rNEvt);
395 	}
396 
397 	//--------------------------------------------------------------------
FillItemSet(SfxItemSet & _rSet)398 	sal_Bool ConnectionPoolOptionsPage::FillItemSet(SfxItemSet& _rSet)
399 	{
400 		commitTimeoutField();
401 
402 		sal_Bool bModified = sal_False;
403 		// the enabled flag
404 		if (m_aEnablePooling.GetSavedValue() != m_aEnablePooling.IsChecked())
405 		{
406 			_rSet.Put(SfxBoolItem(SID_SB_POOLING_ENABLED, m_aEnablePooling.IsChecked()), SID_SB_POOLING_ENABLED);
407 			bModified = sal_True;
408 		}
409 
410 		// the settings for the single drivers
411 		if (m_pDriverList->isModified())
412 		{
413 			_rSet.Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, m_pDriverList->getSettings()), SID_SB_DRIVER_TIMEOUTS);
414 			bModified = sal_True;
415 		}
416 
417 		return bModified;
418 	}
419 
420 	//--------------------------------------------------------------------
ActivatePage(const SfxItemSet & _rSet)421 	void ConnectionPoolOptionsPage::ActivatePage( const SfxItemSet& _rSet)
422 	{
423 		SfxTabPage::ActivatePage(_rSet);
424 		implInitControls(_rSet, sal_False);
425 	}
426 
427 	//--------------------------------------------------------------------
Reset(const SfxItemSet & _rSet)428 	void ConnectionPoolOptionsPage::Reset(const SfxItemSet& _rSet)
429 	{
430 		implInitControls(_rSet, sal_True);
431 	}
432 
433 	//--------------------------------------------------------------------
IMPL_LINK(ConnectionPoolOptionsPage,OnDriverRowChanged,const void *,_pRowIterator)434 	IMPL_LINK( ConnectionPoolOptionsPage, OnDriverRowChanged, const void*, _pRowIterator )
435 	{
436 		sal_Bool bValidRow = (NULL != _pRowIterator);
437 		m_aDriverPoolingEnabled.Enable(bValidRow && m_aEnablePooling.IsChecked());
438 		m_aTimeoutLabel.Enable(bValidRow);
439 		m_aTimeout.Enable(bValidRow);
440 
441 		if (!bValidRow)
442 		{	// positioned on an invalid row
443 			m_aDriver.SetText(String());
444 		}
445 		else
446 		{
447 			const DriverPooling *pDriverPos = static_cast<const DriverPooling*>(_pRowIterator);
448 
449 			m_aDriver.SetText(pDriverPos->sName);
450 			m_aDriverPoolingEnabled.Check(pDriverPos->bEnabled);
451 			m_aTimeout.SetText(String::CreateFromInt32(pDriverPos->nTimeoutSeconds));
452 
453 			OnEnabledDisabled(&m_aDriverPoolingEnabled);
454 		}
455 
456 		return 0L;
457 	}
458 
459 	//--------------------------------------------------------------------
commitTimeoutField()460 	void ConnectionPoolOptionsPage::commitTimeoutField()
461 	{
462 		if (DriverPooling* pCurrentDriver = m_pDriverList->getCurrentRow())
463 		{
464 			pCurrentDriver->nTimeoutSeconds = static_cast<long>(m_aTimeout.GetValue());
465 			m_pDriverList->updateCurrentRow();
466 		}
467 	}
468 
469 	//--------------------------------------------------------------------
IMPL_LINK(ConnectionPoolOptionsPage,OnEnabledDisabled,const CheckBox *,_pCheckBox)470 	IMPL_LINK( ConnectionPoolOptionsPage, OnEnabledDisabled, const CheckBox*, _pCheckBox )
471 	{
472 		sal_Bool bGloballyEnabled = m_aEnablePooling.IsChecked();
473 		sal_Bool bLocalDriverChanged = &m_aDriverPoolingEnabled == _pCheckBox;
474 
475 		if (&m_aEnablePooling == _pCheckBox)
476 		{
477 			m_aDriversLabel.Enable(bGloballyEnabled);
478 			m_pDriverList->Enable(bGloballyEnabled);
479 			m_aDriverLabel.Enable(bGloballyEnabled);
480 			m_aDriver.Enable(bGloballyEnabled);
481 			m_aDriverPoolingEnabled.Enable(bGloballyEnabled);
482 		}
483 		else
484 			OSL_ENSURE(bLocalDriverChanged, "ConnectionPoolOptionsPage::OnEnabledDisabled: where did this come from?");
485 
486 		m_aTimeoutLabel.Enable(bGloballyEnabled && m_aDriverPoolingEnabled.IsChecked());
487 		m_aTimeout.Enable(bGloballyEnabled && m_aDriverPoolingEnabled.IsChecked());
488 
489 		if (bLocalDriverChanged)
490 		{
491 			// update the list
492 			m_pDriverList->getCurrentRow()->bEnabled = m_aDriverPoolingEnabled.IsChecked();
493 			m_pDriverList->updateCurrentRow();
494 		}
495 
496 		return 0L;
497 	}
498 
499 //........................................................................
500 }	// namespace offapp
501 //........................................................................
502 
503 
504