Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / dbaccess / genericcontroller.hxx
blob0438c091a86d95b05592c49cb4e09b78d6fad1e0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_DBACCESS_GENERICCONTROLLER_HXX
21 #define INCLUDED_DBACCESS_GENERICCONTROLLER_HXX
23 #include <sal/config.h>
25 #include <deque>
26 #include <map>
27 #include <memory>
28 #include <mutex>
29 #include <utility>
30 #include <vector>
32 #include <optional>
34 #include <com/sun/star/awt/XUserInputInterception.hpp>
35 #include <com/sun/star/frame/CommandGroup.hpp>
36 #include <com/sun/star/frame/DispatchInformation.hpp>
37 #include <com/sun/star/frame/XController2.hpp>
38 #include <com/sun/star/frame/XDispatch.hpp>
39 #include <com/sun/star/frame/XDispatchInformationProvider.hpp>
40 #include <com/sun/star/frame/XDispatchProviderInterceptor.hpp>
41 #include <com/sun/star/frame/XLayoutManager.hpp>
42 #include <com/sun/star/frame/XTitle.hpp>
43 #include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
44 #include <com/sun/star/lang/EventObject.hpp>
45 #include <com/sun/star/lang/XInitialization.hpp>
46 #include <com/sun/star/lang/XServiceInfo.hpp>
47 #include <com/sun/star/uno/Any.hxx>
48 #include <com/sun/star/uno/Reference.hxx>
49 #include <com/sun/star/uno/Sequence.hxx>
50 #include <com/sun/star/util/URL.hpp>
51 #include <com/sun/star/util/XModifyListener.hpp>
52 #include <comphelper/sharedmutex.hxx>
53 #include <comphelper/namedvaluecollection.hxx>
54 #include <connectivity/dbexception.hxx>
55 #include <cppuhelper/compbase.hxx>
56 #include <cppuhelper/interfacecontainer.h>
57 #include <dbaccess/AsynchronousLink.hxx>
58 #include <dbaccess/controllerframe.hxx>
59 #include <dbaccess/dbaccessdllapi.h>
60 #include <dbaccess/IController.hxx>
61 #include <rtl/ustring.hxx>
62 #include <sal/types.h>
63 #include <tools/link.hxx>
64 #include <vcl/vclptr.hxx>
65 #include <sfx2/userinputinterception.hxx>
67 namespace com::sun::star {
68 namespace awt { class XKeyHandler; }
69 namespace awt { class XMouseClickHandler; }
70 namespace awt { class XWindow; }
71 namespace beans { struct PropertyValue; }
72 namespace frame { class XController; }
73 namespace frame { class XDispatchProvider; }
74 namespace frame { class XFrame; }
75 namespace frame { class XFrameActionListener; }
76 namespace frame { class XModel; }
77 namespace frame { class XStatusListener; }
78 namespace frame { class XTitleChangeListener; }
79 namespace frame { struct DispatchDescriptor; }
80 namespace frame { struct FrameActionEvent; }
81 namespace lang { class XEventListener; }
82 namespace sdb { class XDatabaseContext; }
83 namespace sdbc { class XConnection; }
84 namespace sdbc { class XDataSource; }
85 namespace ui { class XSidebarProvider; }
86 namespace uno { class XComponentContext; }
87 namespace util { class XURLTransformer; }
90 namespace vcl { class Window; }
91 namespace weld { class Window; }
92 class NotifyEvent;
94 namespace dbaui
96 class ODataView;
98 template< typename T >
99 inline bool SAL_CALL operator >>= (const css::uno::Any& _any, std::optional< T >& _value)
101 _value.reset(); // de-init the optional value
103 T directValue = T();
104 if ( _any >>= directValue )
105 _value = directValue;
107 return !!_value;
111 // = FeatureState
113 /** describes the state of a feature
115 In opposite to the FeatureStateEvent in css.frame, this one allows for multiple states to be specified at once.
116 With this, you can for instance specify that a toolbox item is checked, and has a certain title, at the same
117 time.
119 struct FeatureState
121 bool bEnabled;
123 std::optional<bool> bChecked;
124 std::optional<bool> bInvisible;
125 css::uno::Any aValue;
126 std::optional<OUString> sTitle;
128 FeatureState() : bEnabled(false) { }
132 // = helper
135 struct ControllerFeature : public css::frame::DispatchInformation
137 sal_uInt16 nFeatureId;
141 typedef ::std::map < OUString
142 , ControllerFeature
143 , ::std::less< OUString >
144 > SupportedFeatures;
147 class CompareFeatureById
149 const sal_Int32 m_nId;
150 public:
151 CompareFeatureById(sal_Int32 _nId) : m_nId(_nId)
154 bool operator()( const SupportedFeatures::value_type& _aType ) const
156 return m_nId == _aType.second.nFeatureId;
161 struct FeatureListener
163 css::uno::Reference< css::frame::XStatusListener >
164 xListener;
165 sal_Int32 nId;
166 bool bForceBroadcast;
170 class FindFeatureListener
172 const css::uno::Reference< css::frame::XStatusListener >& m_xListener;
173 public:
174 FindFeatureListener(const css::uno::Reference< css::frame::XStatusListener >& _xListener)
175 : m_xListener(_xListener)
178 bool operator()( const FeatureListener& lhs ) const
180 return lhs.xListener == m_xListener;
185 typedef ::comphelper::SharedMutexBase OGenericUnoController_MBASE;
187 typedef ::cppu::WeakComponentImplHelper< css::frame::XDispatch
188 , css::frame::XDispatchProviderInterceptor
189 , css::util::XModifyListener
190 , css::frame::XFrameActionListener
191 , css::lang::XInitialization
192 , css::lang::XServiceInfo
193 , css::frame::XDispatchInformationProvider
194 , css::frame::XController2
195 , css::frame::XTitle
196 , css::frame::XTitleChangeBroadcaster
197 , css::awt::XUserInputInterception
198 > OGenericUnoController_Base;
200 struct OGenericUnoController_Data;
202 class DBACCESS_DLLPUBLIC OGenericUnoController
203 :public OGenericUnoController_MBASE
204 ,public OGenericUnoController_Base
205 ,public IController
207 private:
208 SupportedFeatures m_aSupportedFeatures;
209 ::comphelper::NamedValueCollection
210 m_aInitParameters;
211 ::sfx2::UserInputInterception m_aUserInputInterception;
212 VclPtr<ODataView> m_pView; // our (VCL) "main window"
214 #ifdef DBG_UTIL
215 bool m_bDescribingSupportedFeatures;
216 #endif
218 protected:
220 // attributes
221 struct DispatchTarget
223 css::util::URL aURL;
224 css::uno::Reference< css::frame::XStatusListener > xListener;
226 DispatchTarget(css::util::URL _aURL, css::uno::Reference< css::frame::XStatusListener > xRef) : aURL(std::move(_aURL)), xListener(std::move(xRef)) { }
229 typedef std::map<sal_uInt16, FeatureState> StateCache;
230 typedef std::vector<DispatchTarget> Dispatch;
232 ::std::deque< FeatureListener >
233 m_aFeaturesToInvalidate;
235 std::mutex m_aFeatureMutex; // locked when features are append to or remove from deque
236 StateCache m_aStateCache; // save the current status of feature state
237 Dispatch m_arrStatusListener; // all our listeners where we dispatch status changes
238 OAsynchronousLink m_aAsyncInvalidateAll;
239 OAsynchronousLink m_aAsyncCloseTask; // called when a task should be closed
241 css::uno::Reference< css::util::XURLTransformer > m_xUrlTransformer; // needed sometimes
242 css::uno::Reference< css::uno::XComponentContext > m_xContext;
243 ControllerFrame m_aCurrentFrame;
244 css::uno::Reference< css::frame::XDispatchProvider > m_xSlaveDispatcher; // for intercepting dispatches
245 css::uno::Reference< css::frame::XDispatchProvider > m_xMasterDispatcher; // ditto
246 css::uno::Reference< css::sdb::XDatabaseContext > m_xDatabaseContext;
247 css::uno::Reference< css::frame::XTitle > m_xTitleHelper;
249 bool m_bPreview;
250 bool m_bReadOnly;
252 bool m_bCurrentlyModified : 1;
253 bool m_bExternalTitle : 1;
256 // attribute access
257 using OGenericUnoController_MBASE::getMutex;
258 ::cppu::OBroadcastHelper& getBroadcastHelper() { return OGenericUnoController_Base::rBHelper; }
261 // methods
262 OGenericUnoController( const css::uno::Reference< css::uno::XComponentContext >& _rM );
263 OGenericUnoController() = delete;
264 const ::comphelper::NamedValueCollection&
265 getInitParams() const { return m_aInitParameters; }
267 // closes the task when possible
268 void closeTask();
270 // if getMenu returns a non empty string than this will be dispatched at the frame
271 virtual void loadMenu(const css::uno::Reference< css::frame::XFrame >& _xFrame);
273 /** called when our menu has been loaded into our frame, can be used to load sub toolbars
275 @param _xLayoutManager
276 The layout manager.
278 virtual void onLoadedMenu(const css::uno::Reference< css::frame::XLayoutManager >& _xLayoutManager);
280 // all the features which should be handled by this class
281 virtual void describeSupportedFeatures();
283 // state of a feature. 'feature' may be the handle of a css::util::URL somebody requested a dispatch interface for OR a toolbar slot.
284 virtual FeatureState GetState(sal_uInt16 nId) const;
285 // execute a feature
286 virtual void Execute(sal_uInt16 nId , const css::uno::Sequence< css::beans::PropertyValue>& aArgs);
288 /** describes a feature supported by the controller
290 Must not be called outside <member>describeSupportedFeatures</member>.
292 @param _rCommandURL
293 the URL of the feature command
294 @param _nFeatureId
295 the id of the feature. Later references to this feature usually happen by id, not by
297 @param _nCommandGroup
298 the command group of the feature. This is important for configuring the controller UI
299 by the user, see also <type scope="css::frame">CommandGroup</type>.
301 void implDescribeSupportedFeature(
302 const OUString& _rCommandURL,
303 sal_uInt16 _nFeatureId,
304 sal_Int16 _nCommandGroup = css::frame::CommandGroup::INTERNAL
307 /** returns <TRUE/> if the feature is supported, otherwise <FALSE/>
308 @param _nId
309 The ID of the feature.
311 bool isFeatureSupported( sal_Int32 _nId );
313 // gets the URL which the given id is assigned to
314 css::util::URL getURLForId(sal_Int32 _nId) const;
316 /** determines whether the given feature ID denotes a user-defined feature
318 @see IController::registerCommandURL
320 static bool isUserDefinedFeature( const sal_uInt16 nFeatureId );
322 /** determines whether the given feature URL denotes a user-defined feature
324 @see IController::registerCommandURL
326 bool isUserDefinedFeature( const OUString& _rFeatureURL ) const;
328 // connect to a datasource
329 css::uno::Reference< css::sdbc::XConnection > connect(
330 const css::uno::Reference< css::sdbc::XDataSource>& _xDataSource
333 // connect to a datasource
334 css::uno::Reference< css::sdbc::XConnection > connect(
335 const OUString& _rsDataSourceName,
336 const OUString& _rContextInformation,
337 ::dbtools::SQLExceptionInfo* _pErrorInfo
340 void startConnectionListening(const css::uno::Reference< css::sdbc::XConnection >& _rxConnection);
341 void stopConnectionListening(const css::uno::Reference< css::sdbc::XConnection >& _rxConnection);
343 /** return the container window of the top most frame
344 @return
345 The top most container window, nmay be <NULL/>.
347 css::uno::Reference< css::awt::XWindow> getTopMostContainerWindow() const;
349 // XInitialize will be called inside initialize
350 virtual void impl_initialize();
352 virtual OUString getPrivateTitle() const { return OUString(); }
354 css::uno::Reference< css::frame::XTitle > impl_getTitleHelper_throw(bool bCreateIfNecessary = true);
355 virtual css::uno::Reference< css::frame::XModel > getPrivateModel() const
357 return css::uno::Reference< css::frame::XModel >();
360 virtual void startFrameListening( const css::uno::Reference< css::frame::XFrame >& _rxFrame );
361 virtual void stopFrameListening( const css::uno::Reference< css::frame::XFrame >& _rxFrame );
363 void releaseNumberForComponent();
365 virtual ~OGenericUnoController() override;
367 private:
368 void fillSupportedFeatures();
370 void InvalidateAll_Impl();
371 void InvalidateFeature_Impl();
373 void ImplInvalidateFeature( sal_Int32 _nId, const css::uno::Reference< css::frame::XStatusListener >& _xListener, bool _bForceBroadcast );
375 void ImplBroadcastFeatureState(const OUString& _rFeature, const css::uno::Reference< css::frame::XStatusListener > & xListener, bool _bIgnoreCache);
377 void executeUserDefinedFeatures( const css::util::URL& _rFeatureURL, const css::uno::Sequence< css::beans::PropertyValue>& _rArgs );
379 // link methods
380 DECL_DLLPRIVATE_LINK(OnAsyncInvalidateAll, void*, void);
381 DECL_DLLPRIVATE_LINK(OnAsyncCloseTask, void*, void);
383 public:
384 const css::uno::Reference< css::uno::XComponentContext >& getORB() const { return m_xContext; }
385 ODataView* getView() const { return m_pView; }
386 weld::Window* getFrameWeld() const;
387 void setView( const VclPtr<ODataView>& i_rView );
388 void clearView();
389 // shows an error box if the SQLExceptionInfo is valid
390 void showError(const ::dbtools::SQLExceptionInfo& _rInfo);
392 // if there is a css::util::URL translation for the id
393 // ('handle') then if xListener is NULL the change will be forwarded
394 // to all listeners to the given css::util::URL
395 // if there is a toolbar slot with the given id it is updated (the new state is determined via GetState)
396 // if _bForceBroadcast is sal_True, the current feature state is broadcasted no matter if it is the same as the cached state
397 void InvalidateFeature(sal_uInt16 nId, const css::uno::Reference< css::frame::XStatusListener > & xListener = nullptr, bool _bForceBroadcast = false);
399 /** InvalidateAll invalidates all features currently known
401 void InvalidateAll();
402 // late construction
403 virtual bool Construct(vcl::Window* pParent);
405 /** get the layout manager
406 @param _xFrame
407 The frame to ask for the layout manager.
408 @return
409 The layout manager of the frame, can be <NULL/> if the frame isn't initialized.
411 static css::uno::Reference< css::frame::XLayoutManager > getLayoutManager(const css::uno::Reference< css::frame::XFrame >& _xFrame);
413 // IController
414 virtual void executeUnChecked(const css::util::URL& _rCommand, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
415 virtual void executeChecked(const css::util::URL& _rCommand, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
416 virtual void executeUnChecked(sal_uInt16 _nCommandId, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
417 virtual void executeChecked(sal_uInt16 _nCommandId, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
418 virtual bool isCommandEnabled(sal_uInt16 _nCommandId) const override;
419 virtual bool isCommandEnabled(const OUString& _rCompleteCommandURL) const override;
420 virtual bool isDataSourceReadOnly() const override;
421 virtual css::uno::Reference< css::frame::XController > getXController() override;
422 virtual bool interceptUserInput( const NotifyEvent& _rEvent ) override;
424 // misc
425 bool isCommandChecked(sal_uInt16 _nCommandId) const;
427 // css::lang::XEventListener
428 virtual void SAL_CALL disposing(const css::lang::EventObject& Source) override;
430 // css::util::XModifyListener
431 virtual void SAL_CALL modified(const css::lang::EventObject& aEvent) override;
433 // XInterface
434 virtual void SAL_CALL acquire( ) noexcept override;
435 virtual void SAL_CALL release( ) noexcept override;
437 // css::frame::XController2
438 virtual css::uno::Reference< css::awt::XWindow > SAL_CALL getComponentWindow() override;
439 virtual OUString SAL_CALL getViewControllerName() override;
440 virtual css::uno::Sequence< css::beans::PropertyValue > SAL_CALL getCreationArguments() override;
442 virtual css::uno::Reference< css::ui::XSidebarProvider > SAL_CALL getSidebar() override;
445 // css::frame::XController
446 virtual void SAL_CALL attachFrame(const css::uno::Reference< css::frame::XFrame > & xFrame) override;
447 virtual sal_Bool SAL_CALL attachModel(const css::uno::Reference< css::frame::XModel > & xModel) override;
448 virtual sal_Bool SAL_CALL suspend(sal_Bool bSuspend) override = 0;
449 virtual css::uno::Any SAL_CALL getViewData() override;
450 virtual void SAL_CALL restoreViewData(const css::uno::Any& Data) override;
451 virtual css::uno::Reference< css::frame::XModel > SAL_CALL getModel() override;
452 virtual css::uno::Reference< css::frame::XFrame > SAL_CALL getFrame() override;
454 // css::frame::XDispatch
455 virtual void SAL_CALL dispatch(const css::util::URL& aURL, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
456 virtual void SAL_CALL addStatusListener(const css::uno::Reference< css::frame::XStatusListener > & aListener, const css::util::URL& aURL) override;
457 virtual void SAL_CALL removeStatusListener(const css::uno::Reference< css::frame::XStatusListener > & aListener, const css::util::URL& aURL) override;
459 // css::frame::XDispatchProviderInterceptor
460 virtual css::uno::Reference< css::frame::XDispatchProvider > SAL_CALL getSlaveDispatchProvider() override;
461 virtual void SAL_CALL setSlaveDispatchProvider(const css::uno::Reference< css::frame::XDispatchProvider > & _xNewProvider) override;
462 virtual css::uno::Reference< css::frame::XDispatchProvider > SAL_CALL getMasterDispatchProvider() override;
463 virtual void SAL_CALL setMasterDispatchProvider(const css::uno::Reference< css::frame::XDispatchProvider > & _xNewProvider) override;
465 // css::frame::XDispatchProvider
466 virtual css::uno::Reference< css::frame::XDispatch > SAL_CALL queryDispatch(const css::util::URL& aURL, const OUString& aTargetFrameName, sal_Int32 nSearchFlags) override;
467 virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL queryDispatches(const css::uno::Sequence< css::frame::DispatchDescriptor >& aDescripts) override;
469 // css::lang::XComponent
470 virtual void SAL_CALL dispose() override; //LLA: need solar mutex {OGenericUnoController_COMPBASE::dispose(); }
471 virtual void SAL_CALL disposing() override;
472 virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > & aListener) override;
473 virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > & aListener) override;
475 // css::frame::XFrameActionListener
476 virtual void SAL_CALL frameAction(const css::frame::FrameActionEvent& aEvent) override;
477 // lang::XInitialization
478 virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
480 // XServiceInfo
481 virtual OUString SAL_CALL getImplementationName() override = 0;
482 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
483 virtual css::uno::Sequence< OUString> SAL_CALL getSupportedServiceNames() override = 0;
485 // XDispatchInformationProvider
486 virtual css::uno::Sequence< ::sal_Int16 > SAL_CALL getSupportedCommandGroups() override;
487 virtual css::uno::Sequence< css::frame::DispatchInformation > SAL_CALL getConfigurableDispatchInformation( ::sal_Int16 ) override;
489 // XTitle
490 virtual OUString SAL_CALL getTitle( ) override;
491 virtual void SAL_CALL setTitle( const OUString& sTitle ) override;
493 // XTitleChangeBroadcaster
494 virtual void SAL_CALL addTitleChangeListener( const css::uno::Reference< css::frame::XTitleChangeListener >& xListener ) override;
495 virtual void SAL_CALL removeTitleChangeListener( const css::uno::Reference< css::frame::XTitleChangeListener >& xListener ) override;
497 // XUserInputInterception
498 virtual void SAL_CALL addKeyHandler( const css::uno::Reference< css::awt::XKeyHandler >& xHandler ) override;
499 virtual void SAL_CALL removeKeyHandler( const css::uno::Reference< css::awt::XKeyHandler >& xHandler ) override;
500 virtual void SAL_CALL addMouseClickHandler( const css::uno::Reference< css::awt::XMouseClickHandler >& xHandler ) override;
501 virtual void SAL_CALL removeMouseClickHandler( const css::uno::Reference< css::awt::XMouseClickHandler >& xHandler ) override;
505 #endif // INCLUDED_DBACCESS_GENERICCONTROLLER_HXX
508 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */