Line data Source code
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 "formnavigation.hxx"
21 : #include "urltransformer.hxx"
22 : #include "controlfeatureinterception.hxx"
23 : #include "frm_strings.hxx"
24 :
25 : #include <com/sun/star/form/runtime/FormFeature.hpp>
26 :
27 : #include <tools/debug.hxx>
28 :
29 :
30 : //.........................................................................
31 : namespace frm
32 : {
33 : //.........................................................................
34 :
35 : using namespace ::com::sun::star::uno;
36 : using namespace ::com::sun::star::beans;
37 : using namespace ::com::sun::star::lang;
38 : using namespace ::com::sun::star::util;
39 : using namespace ::com::sun::star::frame;
40 : namespace FormFeature = ::com::sun::star::form::runtime::FormFeature;
41 :
42 : //==================================================================
43 : //= OFormNavigationHelper
44 : //==================================================================
45 : DBG_NAME( OFormNavigationHelper )
46 : //------------------------------------------------------------------
47 0 : OFormNavigationHelper::OFormNavigationHelper( const Reference< XMultiServiceFactory >& _rxORB )
48 : :m_xORB( _rxORB )
49 0 : ,m_nConnectedFeatures( 0 )
50 : {
51 : DBG_CTOR( OFormNavigationHelper, NULL );
52 0 : m_pFeatureInterception.reset( new ControlFeatureInterception( m_xORB ) );
53 0 : }
54 :
55 : //------------------------------------------------------------------
56 0 : OFormNavigationHelper::~OFormNavigationHelper()
57 : {
58 : DBG_DTOR( OFormNavigationHelper, NULL );
59 0 : }
60 :
61 : //------------------------------------------------------------------
62 0 : void SAL_CALL OFormNavigationHelper::dispose( ) throw( RuntimeException )
63 : {
64 0 : m_pFeatureInterception->dispose();
65 0 : disconnectDispatchers();
66 0 : }
67 :
68 : //------------------------------------------------------------------
69 0 : void OFormNavigationHelper::interceptorsChanged( )
70 : {
71 0 : updateDispatches();
72 0 : }
73 :
74 : //------------------------------------------------------------------
75 0 : void OFormNavigationHelper::featureStateChanged( sal_Int16 /*_nFeatureId*/, sal_Bool /*_bEnabled*/ )
76 : {
77 : // not interested in
78 0 : }
79 :
80 : //------------------------------------------------------------------
81 0 : void OFormNavigationHelper::allFeatureStatesChanged( )
82 : {
83 : // not interested in
84 0 : }
85 :
86 : //------------------------------------------------------------------
87 0 : void SAL_CALL OFormNavigationHelper::registerDispatchProviderInterceptor( const Reference< XDispatchProviderInterceptor >& _rxInterceptor ) throw (RuntimeException)
88 : {
89 0 : m_pFeatureInterception->registerDispatchProviderInterceptor( _rxInterceptor );
90 0 : interceptorsChanged();
91 0 : }
92 :
93 : //------------------------------------------------------------------
94 0 : void SAL_CALL OFormNavigationHelper::releaseDispatchProviderInterceptor( const Reference< XDispatchProviderInterceptor >& _rxInterceptor ) throw (RuntimeException)
95 : {
96 0 : m_pFeatureInterception->releaseDispatchProviderInterceptor( _rxInterceptor );
97 0 : interceptorsChanged();
98 0 : }
99 :
100 : //------------------------------------------------------------------
101 0 : void SAL_CALL OFormNavigationHelper::statusChanged( const FeatureStateEvent& _rState ) throw (RuntimeException)
102 : {
103 0 : for ( FeatureMap::iterator aFeature = m_aSupportedFeatures.begin();
104 0 : aFeature != m_aSupportedFeatures.end();
105 : ++aFeature
106 : )
107 : {
108 0 : if ( aFeature->second.aURL.Main == _rState.FeatureURL.Main )
109 : {
110 0 : if ( ( aFeature->second.bCachedState != _rState.IsEnabled )
111 0 : || ( aFeature->second.aCachedAdditionalState != _rState.State )
112 : )
113 : {
114 : // change the cached state
115 0 : aFeature->second.bCachedState = _rState.IsEnabled;
116 0 : aFeature->second.aCachedAdditionalState = _rState.State;
117 : // tell derivees what happened
118 0 : featureStateChanged( aFeature->first, _rState.IsEnabled );
119 : }
120 0 : return;
121 : }
122 : }
123 :
124 : // unreachable
125 : OSL_FAIL( "OFormNavigationHelper::statusChanged: huh? An invalid/unknown URL?" );
126 : }
127 :
128 : //------------------------------------------------------------------
129 0 : void SAL_CALL OFormNavigationHelper::disposing( const EventObject& _rSource ) throw (RuntimeException)
130 : {
131 : // was it one of our external dispatchers?
132 0 : if ( m_nConnectedFeatures )
133 : {
134 0 : for ( FeatureMap::iterator aFeature = m_aSupportedFeatures.begin();
135 0 : aFeature != m_aSupportedFeatures.end();
136 : ++aFeature
137 : )
138 : {
139 0 : if ( aFeature->second.xDispatcher == _rSource.Source )
140 : {
141 0 : aFeature->second.xDispatcher->removeStatusListener( static_cast< XStatusListener* >( this ), aFeature->second.aURL );
142 0 : aFeature->second.xDispatcher = NULL;
143 0 : aFeature->second.bCachedState = sal_False;
144 0 : aFeature->second.aCachedAdditionalState.clear();
145 0 : --m_nConnectedFeatures;
146 :
147 0 : featureStateChanged( aFeature->first, sal_False );
148 0 : break;
149 : }
150 : }
151 : }
152 0 : }
153 :
154 : //------------------------------------------------------------------
155 0 : void OFormNavigationHelper::updateDispatches()
156 : {
157 0 : if ( !m_nConnectedFeatures )
158 : { // we don't have any dispatchers yet -> do the initial connect
159 0 : connectDispatchers();
160 0 : return;
161 : }
162 :
163 0 : initializeSupportedFeatures();
164 :
165 0 : m_nConnectedFeatures = 0;
166 :
167 0 : Reference< XDispatch > xNewDispatcher;
168 0 : Reference< XDispatch > xCurrentDispatcher;
169 :
170 0 : for ( FeatureMap::iterator aFeature = m_aSupportedFeatures.begin();
171 0 : aFeature != m_aSupportedFeatures.end();
172 : ++aFeature
173 : )
174 : {
175 0 : xNewDispatcher = queryDispatch( aFeature->second.aURL );
176 0 : xCurrentDispatcher = aFeature->second.xDispatcher;
177 0 : if ( xNewDispatcher != xCurrentDispatcher )
178 : {
179 : // the dispatcher for this particular URL changed
180 0 : if ( xCurrentDispatcher.is() )
181 0 : xCurrentDispatcher->removeStatusListener( static_cast< XStatusListener* >( this ), aFeature->second.aURL );
182 :
183 0 : xCurrentDispatcher = aFeature->second.xDispatcher = xNewDispatcher;
184 :
185 0 : if ( xCurrentDispatcher.is() )
186 0 : xCurrentDispatcher->addStatusListener( static_cast< XStatusListener* >( this ), aFeature->second.aURL );
187 : }
188 :
189 0 : if ( xCurrentDispatcher.is() )
190 0 : ++m_nConnectedFeatures;
191 : else
192 0 : aFeature->second.bCachedState = sal_False;
193 : }
194 :
195 : // notify derivee that (potentially) all features changed their state
196 0 : allFeatureStatesChanged( );
197 : }
198 :
199 : //------------------------------------------------------------------
200 0 : void OFormNavigationHelper::connectDispatchers()
201 : {
202 0 : if ( m_nConnectedFeatures )
203 : { // already connected -> just do an update
204 0 : updateDispatches();
205 0 : return;
206 : }
207 :
208 0 : initializeSupportedFeatures();
209 :
210 0 : m_nConnectedFeatures = 0;
211 :
212 0 : for ( FeatureMap::iterator aFeature = m_aSupportedFeatures.begin();
213 0 : aFeature != m_aSupportedFeatures.end();
214 : ++aFeature
215 : )
216 : {
217 0 : aFeature->second.bCachedState = sal_False;
218 0 : aFeature->second.aCachedAdditionalState.clear();
219 0 : aFeature->second.xDispatcher = queryDispatch( aFeature->second.aURL );
220 0 : if ( aFeature->second.xDispatcher.is() )
221 : {
222 0 : ++m_nConnectedFeatures;
223 0 : aFeature->second.xDispatcher->addStatusListener( static_cast< XStatusListener* >( this ), aFeature->second.aURL );
224 : }
225 : }
226 :
227 : // notify derivee that (potentially) all features changed their state
228 0 : allFeatureStatesChanged( );
229 : }
230 :
231 : //------------------------------------------------------------------
232 0 : void OFormNavigationHelper::disconnectDispatchers()
233 : {
234 0 : if ( m_nConnectedFeatures )
235 : {
236 0 : for ( FeatureMap::iterator aFeature = m_aSupportedFeatures.begin();
237 0 : aFeature != m_aSupportedFeatures.end();
238 : ++aFeature
239 : )
240 : {
241 0 : if ( aFeature->second.xDispatcher.is() )
242 0 : aFeature->second.xDispatcher->removeStatusListener( static_cast< XStatusListener* >( this ), aFeature->second.aURL );
243 :
244 0 : aFeature->second.xDispatcher = NULL;
245 0 : aFeature->second.bCachedState = sal_False;
246 0 : aFeature->second.aCachedAdditionalState.clear();
247 : }
248 :
249 0 : m_nConnectedFeatures = 0;
250 : }
251 :
252 : // notify derivee that (potentially) all features changed their state
253 0 : allFeatureStatesChanged( );
254 0 : }
255 :
256 : //------------------------------------------------------------------
257 0 : void OFormNavigationHelper::initializeSupportedFeatures( )
258 : {
259 0 : if ( m_aSupportedFeatures.empty() )
260 : {
261 : // ask the derivee which feature ids it wants us to support
262 0 : ::std::vector< sal_Int16 > aFeatureIds;
263 0 : getSupportedFeatures( aFeatureIds );
264 :
265 0 : OFormNavigationMapper aUrlMapper( m_xORB );
266 :
267 0 : for ( ::std::vector< sal_Int16 >::const_iterator aLoop = aFeatureIds.begin();
268 0 : aLoop != aFeatureIds.end();
269 : ++aLoop
270 : )
271 : {
272 0 : FeatureInfo aFeatureInfo;
273 :
274 : bool bKnownId =
275 0 : aUrlMapper.getFeatureURL( *aLoop, aFeatureInfo.aURL );
276 : DBG_ASSERT( bKnownId, "OFormNavigationHelper::initializeSupportedFeatures: unknown feature id!" );
277 :
278 0 : if ( bKnownId )
279 : // add to our map
280 0 : m_aSupportedFeatures.insert( FeatureMap::value_type( *aLoop, aFeatureInfo ) );
281 0 : }
282 : }
283 0 : }
284 :
285 : //------------------------------------------------------------------
286 0 : Reference< XDispatch > OFormNavigationHelper::queryDispatch( const URL& _rURL )
287 : {
288 0 : return m_pFeatureInterception->queryDispatch( _rURL );
289 : }
290 :
291 : //------------------------------------------------------------------
292 0 : void OFormNavigationHelper::dispatchWithArgument( sal_Int16 _nFeatureId, const sal_Char* _pParamAsciiName,
293 : const Any& _rParamValue ) const
294 : {
295 0 : FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
296 0 : if ( m_aSupportedFeatures.end() != aInfo )
297 : {
298 0 : if ( aInfo->second.xDispatcher.is() )
299 : {
300 0 : Sequence< PropertyValue > aArgs( 1 );
301 0 : aArgs[0].Name = ::rtl::OUString::createFromAscii( _pParamAsciiName );
302 0 : aArgs[0].Value = _rParamValue;
303 :
304 0 : aInfo->second.xDispatcher->dispatch( aInfo->second.aURL, aArgs );
305 : }
306 : }
307 0 : }
308 :
309 : //------------------------------------------------------------------
310 0 : void OFormNavigationHelper::dispatch( sal_Int16 _nFeatureId ) const
311 : {
312 0 : FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
313 0 : if ( m_aSupportedFeatures.end() != aInfo )
314 : {
315 0 : if ( aInfo->second.xDispatcher.is() )
316 : {
317 0 : Sequence< PropertyValue > aEmptyArgs;
318 0 : aInfo->second.xDispatcher->dispatch( aInfo->second.aURL, aEmptyArgs );
319 : }
320 : }
321 0 : }
322 :
323 : //------------------------------------------------------------------
324 0 : bool OFormNavigationHelper::isEnabled( sal_Int16 _nFeatureId ) const
325 : {
326 0 : FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
327 0 : if ( m_aSupportedFeatures.end() != aInfo )
328 0 : return aInfo->second.bCachedState;
329 :
330 0 : return false;
331 : }
332 :
333 : //------------------------------------------------------------------
334 0 : bool OFormNavigationHelper::getBooleanState( sal_Int16 _nFeatureId ) const
335 : {
336 0 : sal_Bool bState = sal_False;
337 :
338 0 : FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
339 0 : if ( m_aSupportedFeatures.end() != aInfo )
340 0 : aInfo->second.aCachedAdditionalState >>= bState;
341 :
342 0 : return (bool)bState;
343 : }
344 :
345 : //------------------------------------------------------------------
346 0 : ::rtl::OUString OFormNavigationHelper::getStringState( sal_Int16 _nFeatureId ) const
347 : {
348 0 : ::rtl::OUString sState;
349 :
350 0 : FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
351 0 : if ( m_aSupportedFeatures.end() != aInfo )
352 0 : aInfo->second.aCachedAdditionalState >>= sState;
353 :
354 0 : return sState;
355 : }
356 :
357 : //------------------------------------------------------------------
358 0 : sal_Int32 OFormNavigationHelper::getIntegerState( sal_Int16 _nFeatureId ) const
359 : {
360 0 : sal_Int32 nState = 0;
361 :
362 0 : FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
363 0 : if ( m_aSupportedFeatures.end() != aInfo )
364 0 : aInfo->second.aCachedAdditionalState >>= nState;
365 :
366 0 : return nState;
367 : }
368 :
369 : //------------------------------------------------------------------
370 0 : void OFormNavigationHelper::invalidateSupportedFeaturesSet()
371 : {
372 0 : disconnectDispatchers( );
373 : // no supported features anymore:
374 0 : FeatureMap aEmpty;
375 0 : m_aSupportedFeatures.swap( aEmpty );
376 0 : }
377 :
378 : //==================================================================
379 : //= OFormNavigationMapper
380 : //==================================================================
381 : //------------------------------------------------------------------
382 0 : OFormNavigationMapper::OFormNavigationMapper( const Reference< XMultiServiceFactory >& _rxORB )
383 : {
384 0 : m_pUrlTransformer.reset( new UrlTransformer( _rxORB ) );
385 0 : }
386 :
387 : //------------------------------------------------------------------
388 0 : OFormNavigationMapper::~OFormNavigationMapper( )
389 : {
390 0 : }
391 :
392 : //------------------------------------------------------------------
393 0 : bool OFormNavigationMapper::getFeatureURL( sal_Int16 _nFeatureId, URL& /* [out] */ _rURL )
394 : {
395 : // get the ascii version of the URL
396 0 : const char* pAsciiURL = getFeatureURLAscii( _nFeatureId );
397 0 : if ( pAsciiURL )
398 0 : _rURL = m_pUrlTransformer->getStrictURLFromAscii( pAsciiURL );
399 :
400 0 : return ( pAsciiURL != NULL );
401 : }
402 :
403 : //------------------------------------------------------------------
404 : namespace
405 : {
406 : struct FeatureURL
407 : {
408 : const sal_Int16 nFormFeature;
409 : const sal_Char* pAsciiURL;
410 :
411 0 : FeatureURL( const sal_Int16 _nFormFeature, const sal_Char* _pAsciiURL )
412 : :nFormFeature( _nFormFeature )
413 0 : ,pAsciiURL( _pAsciiURL )
414 : {
415 0 : }
416 : };
417 0 : const FeatureURL* lcl_getFeatureTable()
418 : {
419 : static const FeatureURL s_aFeatureURLs[] =
420 : {
421 : FeatureURL( FormFeature::MoveAbsolute, URL_FORM_POSITION.ascii ),
422 : FeatureURL( FormFeature::TotalRecords, URL_FORM_RECORDCOUNT.ascii ),
423 : FeatureURL( FormFeature::MoveToFirst, URL_RECORD_FIRST.ascii ),
424 : FeatureURL( FormFeature::MoveToPrevious, URL_RECORD_PREV.ascii ),
425 : FeatureURL( FormFeature::MoveToNext, URL_RECORD_NEXT.ascii ),
426 : FeatureURL( FormFeature::MoveToLast, URL_RECORD_LAST.ascii ),
427 : FeatureURL( FormFeature::SaveRecordChanges, URL_RECORD_SAVE.ascii ),
428 : FeatureURL( FormFeature::UndoRecordChanges, URL_RECORD_UNDO.ascii ),
429 : FeatureURL( FormFeature::MoveToInsertRow, URL_RECORD_NEW.ascii ),
430 : FeatureURL( FormFeature::DeleteRecord, URL_RECORD_DELETE.ascii ),
431 : FeatureURL( FormFeature::ReloadForm, URL_FORM_REFRESH.ascii ),
432 : FeatureURL( FormFeature::RefreshCurrentControl, URL_FORM_REFRESH_CURRENT_CONTROL.ascii ),
433 : FeatureURL( FormFeature::SortAscending, URL_FORM_SORT_UP.ascii ),
434 : FeatureURL( FormFeature::SortDescending, URL_FORM_SORT_DOWN.ascii ),
435 : FeatureURL( FormFeature::InteractiveSort, URL_FORM_SORT.ascii ),
436 : FeatureURL( FormFeature::AutoFilter, URL_FORM_AUTO_FILTER.ascii ),
437 : FeatureURL( FormFeature::InteractiveFilter, URL_FORM_FILTER.ascii ),
438 : FeatureURL( FormFeature::ToggleApplyFilter, URL_FORM_APPLY_FILTER.ascii ),
439 : FeatureURL( FormFeature::RemoveFilterAndSort, URL_FORM_REMOVE_FILTER.ascii ),
440 : FeatureURL( 0, NULL )
441 0 : };
442 0 : return s_aFeatureURLs;
443 : }
444 : }
445 :
446 : //------------------------------------------------------------------
447 0 : const char* OFormNavigationMapper::getFeatureURLAscii( sal_Int16 _nFeatureId )
448 : {
449 0 : const FeatureURL* pFeatures = lcl_getFeatureTable();
450 0 : while ( pFeatures->pAsciiURL )
451 : {
452 0 : if ( pFeatures->nFormFeature == _nFeatureId )
453 0 : return pFeatures->pAsciiURL;
454 0 : ++pFeatures;
455 : }
456 0 : return NULL;
457 : }
458 :
459 : //------------------------------------------------------------------
460 0 : sal_Int16 OFormNavigationMapper::getFeatureId( const ::rtl::OUString& _rCompleteURL )
461 : {
462 0 : const FeatureURL* pFeatures = lcl_getFeatureTable();
463 0 : while ( pFeatures->pAsciiURL )
464 : {
465 0 : if ( _rCompleteURL.compareToAscii( pFeatures->pAsciiURL ) == 0 )
466 0 : return pFeatures->nFormFeature;
467 0 : ++pFeatures;
468 : }
469 0 : return -1;
470 : }
471 :
472 : //.........................................................................
473 : } // namespace frm
474 : //.........................................................................
475 :
476 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|