LibreOffice Module forms (master) 1
formnavigation.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 <formnavigation.hxx>
21#include <urltransformer.hxx>
23#include <frm_strings.hxx>
24
25#include <com/sun/star/form/runtime/FormFeature.hpp>
26
28#include <tools/debug.hxx>
29#include <o3tl/string_view.hxx>
30#include <osl/diagnose.h>
31
32
33namespace frm
34{
35
36
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::beans;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::util;
41 using namespace ::com::sun::star::frame;
42 namespace FormFeature = ::com::sun::star::form::runtime::FormFeature;
43
44 OFormNavigationHelper::OFormNavigationHelper( const Reference< XComponentContext >& _rxORB )
45 :m_xORB( _rxORB )
46 ,m_aFeatureInterception( m_xORB )
47 ,m_nConnectedFeatures( 0 )
48 {
49 }
50
51
53 {
54 }
55
56
58 {
61 }
62
63
65 {
67 }
68
69
70 void OFormNavigationHelper::featureStateChanged( sal_Int16 /*_nFeatureId*/, bool /*_bEnabled*/ )
71 {
72 // not interested in
73 }
74
75
77 {
78 // not interested in
79 }
80
81
82 void SAL_CALL OFormNavigationHelper::registerDispatchProviderInterceptor( const Reference< XDispatchProviderInterceptor >& _rxInterceptor )
83 {
86 }
87
88
89 void SAL_CALL OFormNavigationHelper::releaseDispatchProviderInterceptor( const Reference< XDispatchProviderInterceptor >& _rxInterceptor )
90 {
93 }
94
95
96 void SAL_CALL OFormNavigationHelper::statusChanged( const FeatureStateEvent& _rState )
97 {
98 for (auto & feature : m_aSupportedFeatures)
99 {
100 if ( feature.second.aURL.Main == _rState.FeatureURL.Main )
101 {
102 if ( ( feature.second.bCachedState != bool(_rState.IsEnabled) )
103 || ( feature.second.aCachedAdditionalState != _rState.State )
104 )
105 {
106 // change the cached state
107 feature.second.bCachedState = _rState.IsEnabled;
108 feature.second.aCachedAdditionalState = _rState.State;
109 // tell derivees what happened
110 featureStateChanged( feature.first, _rState.IsEnabled );
111 }
112 return;
113 }
114 }
115
116 // unreachable
117 OSL_FAIL( "OFormNavigationHelper::statusChanged: huh? An invalid/unknown URL?" );
118 }
119
120
121 void SAL_CALL OFormNavigationHelper::disposing( const EventObject& _rSource )
122 {
123 // was it one of our external dispatchers?
125 return;
126
127 for (auto & feature : m_aSupportedFeatures)
128 {
129 if ( feature.second.xDispatcher == _rSource.Source )
130 {
131 feature.second.xDispatcher->removeStatusListener( static_cast< XStatusListener* >( this ), feature.second.aURL );
132 feature.second.xDispatcher = nullptr;
133 feature.second.bCachedState = false;
134 feature.second.aCachedAdditionalState.clear();
136
137 featureStateChanged( feature.first, false );
138 break;
139 }
140 }
141 }
142
143
145 {
147 { // we don't have any dispatchers yet -> do the initial connect
149 return;
150 }
151
153
155
156 Reference< XDispatch > xNewDispatcher;
157 Reference< XDispatch > xCurrentDispatcher;
158
159 for (auto & feature : m_aSupportedFeatures)
160 {
161 xNewDispatcher = queryDispatch( feature.second.aURL );
162 xCurrentDispatcher = feature.second.xDispatcher;
163 if ( xNewDispatcher != xCurrentDispatcher )
164 {
165 // the dispatcher for this particular URL changed
166 if ( xCurrentDispatcher.is() )
167 xCurrentDispatcher->removeStatusListener( static_cast< XStatusListener* >( this ), feature.second.aURL );
168
169 xCurrentDispatcher = feature.second.xDispatcher = xNewDispatcher;
170
171 if ( xCurrentDispatcher.is() )
172 xCurrentDispatcher->addStatusListener( static_cast< XStatusListener* >( this ), feature.second.aURL );
173 }
174
175 if ( xCurrentDispatcher.is() )
177 else
178 feature.second.bCachedState = false;
179 }
180
181 // notify derivee that (potentially) all features changed their state
183 }
184
185
187 {
189 { // already connected -> just do an update
191 return;
192 }
193
195
197
198 for (auto & feature : m_aSupportedFeatures)
199 {
200 feature.second.bCachedState = false;
201 feature.second.aCachedAdditionalState.clear();
202 feature.second.xDispatcher = queryDispatch( feature.second.aURL );
203 if ( feature.second.xDispatcher.is() )
204 {
206 feature.second.xDispatcher->addStatusListener( static_cast< XStatusListener* >( this ), feature.second.aURL );
207 }
208 }
209
210 // notify derivee that (potentially) all features changed their state
212 }
213
214
216 {
218 {
219 for (auto & feature : m_aSupportedFeatures)
220 {
221 if ( feature.second.xDispatcher.is() )
222 feature.second.xDispatcher->removeStatusListener( static_cast< XStatusListener* >( this ), feature.second.aURL );
223
224 feature.second.xDispatcher = nullptr;
225 feature.second.bCachedState = false;
226 feature.second.aCachedAdditionalState.clear();
227 }
228
230 }
231
232 // notify derivee that (potentially) all features changed their state
234 }
235
236
238 {
239 if ( !m_aSupportedFeatures.empty() )
240 return;
241
242 // ask the derivee which feature ids it wants us to support
243 ::std::vector< sal_Int16 > aFeatureIds;
244 getSupportedFeatures( aFeatureIds );
245
246 OFormNavigationMapper aUrlMapper( m_xORB );
247
248 for (auto const& feature : aFeatureIds)
249 {
250 FeatureInfo aFeatureInfo;
251
252 bool bKnownId =
253 aUrlMapper.getFeatureURL( feature, aFeatureInfo.aURL );
254 DBG_ASSERT( bKnownId, "OFormNavigationHelper::initializeSupportedFeatures: unknown feature id!" );
255
256 if ( bKnownId )
257 // add to our map
258 m_aSupportedFeatures.emplace( feature, aFeatureInfo );
259 }
260 }
261
262
263 Reference< XDispatch > OFormNavigationHelper::queryDispatch( const URL& _rURL )
264 {
266 }
267
268
269 void OFormNavigationHelper::dispatchWithArgument( sal_Int16 _nFeatureId, const char* _pParamAsciiName,
270 const Any& _rParamValue ) const
271 {
272 FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
273 if ( m_aSupportedFeatures.end() != aInfo )
274 {
275 if ( aInfo->second.xDispatcher.is() )
276 {
277 Sequence< PropertyValue > aArgs{ comphelper::makePropertyValue(
278 OUString::createFromAscii( _pParamAsciiName ), _rParamValue) };
279
280 aInfo->second.xDispatcher->dispatch( aInfo->second.aURL, aArgs );
281 }
282 }
283 }
284
285
286 void OFormNavigationHelper::dispatch( sal_Int16 _nFeatureId ) const
287 {
288 FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
289 if ( m_aSupportedFeatures.end() != aInfo )
290 {
291 if ( aInfo->second.xDispatcher.is() )
292 {
293 Sequence< PropertyValue > aEmptyArgs;
294 aInfo->second.xDispatcher->dispatch( aInfo->second.aURL, aEmptyArgs );
295 }
296 }
297 }
298
299
300 bool OFormNavigationHelper::isEnabled( sal_Int16 _nFeatureId ) const
301 {
302 FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
303 if ( m_aSupportedFeatures.end() != aInfo )
304 return aInfo->second.bCachedState;
305
306 return false;
307 }
308
309
310 bool OFormNavigationHelper::getBooleanState( sal_Int16 _nFeatureId ) const
311 {
312 bool bState = false;
313
314 FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
315 if ( m_aSupportedFeatures.end() != aInfo )
316 aInfo->second.aCachedAdditionalState >>= bState;
317
318 return bState;
319 }
320
321
322 OUString OFormNavigationHelper::getStringState( sal_Int16 _nFeatureId ) const
323 {
324 OUString sState;
325
326 FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
327 if ( m_aSupportedFeatures.end() != aInfo )
328 aInfo->second.aCachedAdditionalState >>= sState;
329
330 return sState;
331 }
332
333
334 sal_Int32 OFormNavigationHelper::getIntegerState( sal_Int16 _nFeatureId ) const
335 {
336 sal_Int32 nState = 0;
337
338 FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
339 if ( m_aSupportedFeatures.end() != aInfo )
340 aInfo->second.aCachedAdditionalState >>= nState;
341
342 return nState;
343 }
344
345
347 {
349 // no supported features anymore:
351 }
352
353 OFormNavigationMapper::OFormNavigationMapper( const Reference< XComponentContext >& _rxORB )
354 {
355 m_pUrlTransformer.reset( new UrlTransformer( _rxORB ) );
356 }
357
358
360 {
361 }
362
363
364 bool OFormNavigationMapper::getFeatureURL( sal_Int16 _nFeatureId, URL& /* [out] */ _rURL )
365 {
366 // get the ascii version of the URL
367 const char* pAsciiURL = getFeatureURLAscii( _nFeatureId );
368 if ( pAsciiURL )
369 _rURL = m_pUrlTransformer->getStrictURLFromAscii( pAsciiURL );
370
371 return ( pAsciiURL != nullptr );
372 }
373
374
375 namespace
376 {
377 struct FeatureURL
378 {
379 const sal_Int16 nFormFeature;
380 const char* pAsciiURL;
381
382 FeatureURL( const sal_Int16 _nFormFeature, const char* _pAsciiURL )
383 :nFormFeature( _nFormFeature )
384 ,pAsciiURL( _pAsciiURL )
385 {
386 }
387 };
388 const FeatureURL* lcl_getFeatureTable()
389 {
390 static const FeatureURL s_aFeatureURLs[] =
391 {
392 FeatureURL( FormFeature::MoveAbsolute, URL_FORM_POSITION ),
393 FeatureURL( FormFeature::TotalRecords, URL_FORM_RECORDCOUNT ),
394 FeatureURL( FormFeature::MoveToFirst, URL_RECORD_FIRST ),
395 FeatureURL( FormFeature::MoveToPrevious, URL_RECORD_PREV ),
396 FeatureURL( FormFeature::MoveToNext, URL_RECORD_NEXT ),
397 FeatureURL( FormFeature::MoveToLast, URL_RECORD_LAST ),
398 FeatureURL( FormFeature::SaveRecordChanges, URL_RECORD_SAVE ),
399 FeatureURL( FormFeature::UndoRecordChanges, URL_RECORD_UNDO ),
400 FeatureURL( FormFeature::MoveToInsertRow, URL_RECORD_NEW ),
401 FeatureURL( FormFeature::DeleteRecord, URL_RECORD_DELETE ),
402 FeatureURL( FormFeature::ReloadForm, URL_FORM_REFRESH ),
403 FeatureURL( FormFeature::RefreshCurrentControl, URL_FORM_REFRESH_CURRENT_CONTROL ),
404 FeatureURL( FormFeature::SortAscending, URL_FORM_SORT_UP ),
405 FeatureURL( FormFeature::SortDescending, URL_FORM_SORT_DOWN ),
406 FeatureURL( FormFeature::InteractiveSort, URL_FORM_SORT ),
407 FeatureURL( FormFeature::AutoFilter, URL_FORM_AUTO_FILTER ),
408 FeatureURL( FormFeature::InteractiveFilter, URL_FORM_FILTER ),
409 FeatureURL( FormFeature::ToggleApplyFilter, URL_FORM_APPLY_FILTER ),
410 FeatureURL( FormFeature::RemoveFilterAndSort, URL_FORM_REMOVE_FILTER ),
411 FeatureURL( 0, nullptr )
412 };
413 return s_aFeatureURLs;
414 }
415 }
416
417
418 const char* OFormNavigationMapper::getFeatureURLAscii( sal_Int16 _nFeatureId )
419 {
420 const FeatureURL* pFeatures = lcl_getFeatureTable();
421 while ( pFeatures->pAsciiURL )
422 {
423 if ( pFeatures->nFormFeature == _nFeatureId )
424 return pFeatures->pAsciiURL;
425 ++pFeatures;
426 }
427 return nullptr;
428 }
429
430
431 sal_Int16 OFormNavigationMapper::getFeatureId( std::u16string_view _rCompleteURL )
432 {
433 const FeatureURL* pFeatures = lcl_getFeatureTable();
434 while ( pFeatures->pAsciiURL )
435 {
436 if ( o3tl::equalsAscii( _rCompleteURL, pFeatures->pAsciiURL ) )
437 return pFeatures->nFormFeature;
438 ++pFeatures;
439 }
440 return -1;
441 }
442
443
444} // namespace frm
445
446
447/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void releaseDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor > &Interceptor)
void registerDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor > &Interceptor)
css::uno::Reference< css::frame::XDispatch > queryDispatch(const css::util::URL &_rURL)
queries the interceptor chain for the given dispatch, with a blank target frame and no frame search f...
virtual void interceptorsChanged()
is called when the interceptors have.
virtual void dispatchWithArgument(sal_Int16 _nFeatureId, const char *_pParamName, const css::uno::Any &_rParamValue) const override
dispatches a feature, with an additional named parameter
void disconnectDispatchers()
disconnect from the dispatch interceptors
void updateDispatches()
update all our dispatches which are controlled by our dispatch interceptors
virtual OUString getStringState(sal_Int16 _nFeatureId) const override
returns the string state of a feature
virtual bool getBooleanState(sal_Int16 _nFeatureId) const override
returns the boolean state of a feature
OFormNavigationHelper(const css::uno::Reference< css::uno::XComponentContext > &_rxORB)
virtual void allFeatureStatesChanged()
notification for (potential) changes in the state of all features
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &State) override
virtual void SAL_CALL registerDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor > &Interceptor) override
virtual sal_Int32 getIntegerState(sal_Int16 _nFeatureId) const override
returns the integer state of a feature
void initializeSupportedFeatures()
initialize m_aSupportedFeatures, if necessary
virtual void SAL_CALL releaseDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor > &Interceptor) override
ControlFeatureInterception m_aFeatureInterception
css::uno::Reference< css::uno::XComponentContext > m_xORB
::std::map< sal_Int16, FeatureInfo > FeatureMap
css::uno::Reference< css::frame::XDispatch > queryDispatch(const css::util::URL &_rURL)
queries the interceptor chain for a dispatcher for the given URL
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual bool isEnabled(sal_Int16 _nFeatureId) const override
checks whether a given feature is enabled
void connectDispatchers()
connect to the dispatch interceptors
virtual void getSupportedFeatures(::std::vector< sal_Int16 > &_rFeatureIds)=0
retrieves the list of supported features
virtual void featureStateChanged(sal_Int16 _nFeatureId, bool _bEnabled)
called when the status of a feature changed
virtual void dispatch(sal_Int16 _nFeatureId) const override
dispatches a feature
void invalidateSupportedFeaturesSet()
invalidates the set of supported features
helper class mapping between feature ids and feature URLs
bool getFeatureURL(sal_Int16 _nFeatureId, css::util::URL &_rURL)
retrieves the feature URL belonging to a feature id
::std::unique_ptr< UrlTransformer > m_pUrlTransformer
static const char * getFeatureURLAscii(sal_Int16 _nFeatureId)
retrieves the ASCII representation of a feature URL belonging to an id
static sal_Int16 getFeatureId(std::u16string_view _rCompleteURL)
retrieves the feature id belonging to a feature URL
OFormNavigationMapper(const css::uno::Reference< css::uno::XComponentContext > &_rxORB)
#define DBG_ASSERT(sCon, aError)
sal_Int32 nState
const char * pAsciiURL
const sal_Int16 nFormFeature
#define URL_FORM_SORT
#define URL_RECORD_NEXT
#define URL_RECORD_PREV
#define URL_RECORD_LAST
#define URL_FORM_AUTO_FILTER
#define URL_RECORD_NEW
#define URL_FORM_APPLY_FILTER
#define URL_FORM_REMOVE_FILTER
#define URL_FORM_REFRESH
#define URL_RECORD_DELETE
#define URL_FORM_REFRESH_CURRENT_CONTROL
#define URL_FORM_SORT_UP
#define URL_FORM_POSITION
#define URL_RECORD_SAVE
#define URL_FORM_RECORDCOUNT
#define URL_FORM_SORT_DOWN
#define URL_RECORD_FIRST
#define URL_RECORD_UNDO
#define URL_FORM_FILTER
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
ListBox is a bit confusing / different from other form components, so here are a few notes:
Definition: BaseListBox.hxx:25
bool equalsAscii(std::u16string_view s1, std::string_view s2)