1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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 <osl/mutex.hxx>
62 #include <rtl/ustring.hxx>
63 #include <sal/types.h>
64 #include <tools/link.hxx>
65 #include <vcl/vclptr.hxx>
66 #include <sfx2/userinputinterception.hxx>
68 namespace com::sun::star
{
69 namespace awt
{ class XKeyHandler
; }
70 namespace awt
{ class XMouseClickHandler
; }
71 namespace awt
{ class XWindow
; }
72 namespace beans
{ struct PropertyValue
; }
73 namespace frame
{ class XController
; }
74 namespace frame
{ class XDispatchProvider
; }
75 namespace frame
{ class XFrame
; }
76 namespace frame
{ class XFrameActionListener
; }
77 namespace frame
{ class XModel
; }
78 namespace frame
{ class XStatusListener
; }
79 namespace frame
{ class XTitleChangeListener
; }
80 namespace frame
{ struct DispatchDescriptor
; }
81 namespace frame
{ struct FrameActionEvent
; }
82 namespace lang
{ class XEventListener
; }
83 namespace sdb
{ class XDatabaseContext
; }
84 namespace sdbc
{ class XConnection
; }
85 namespace sdbc
{ class XDataSource
; }
86 namespace ui
{ class XSidebarProvider
; }
87 namespace uno
{ class XComponentContext
; }
88 namespace util
{ class XURLTransformer
; }
91 namespace vcl
{ class Window
; }
92 namespace weld
{ class Window
; }
99 template< typename T
>
100 inline bool SAL_CALL
operator >>= (const css::uno::Any
& _any
, std::optional
< T
>& _value
)
102 _value
.reset(); // de-init the optional value
105 if ( _any
>>= directValue
)
106 _value
= directValue
;
114 /** describes the state of a feature
116 In opposite to the FeatureStateEvent in css.frame, this one allows for multiple states to be specified at once.
117 With this, you can for instance specify that a toolbox item is checked, and has a certain title, at the same
124 std::optional
<bool> bChecked
;
125 std::optional
<bool> bInvisible
;
126 css::uno::Any aValue
;
127 std::optional
<OUString
> sTitle
;
129 FeatureState() : bEnabled(false) { }
136 struct ControllerFeature
: public css::frame::DispatchInformation
138 sal_uInt16 nFeatureId
;
142 typedef ::std::map
< OUString
144 , ::std::less
< OUString
>
148 class CompareFeatureById
150 const sal_Int32 m_nId
;
152 CompareFeatureById(sal_Int32 _nId
) : m_nId(_nId
)
155 bool operator()( const SupportedFeatures::value_type
& _aType
) const
157 return m_nId
== _aType
.second
.nFeatureId
;
162 struct FeatureListener
164 css::uno::Reference
< css::frame::XStatusListener
>
167 bool bForceBroadcast
;
171 class FindFeatureListener
173 const css::uno::Reference
< css::frame::XStatusListener
>& m_xListener
;
175 FindFeatureListener(const css::uno::Reference
< css::frame::XStatusListener
>& _xListener
)
176 : m_xListener(_xListener
)
179 bool operator()( const FeatureListener
& lhs
) const
181 return lhs
.xListener
== m_xListener
;
186 typedef ::comphelper::SharedMutexBase OGenericUnoController_MBASE
;
188 typedef ::cppu::WeakComponentImplHelper
< css::frame::XDispatch
189 , css::frame::XDispatchProviderInterceptor
190 , css::util::XModifyListener
191 , css::frame::XFrameActionListener
192 , css::lang::XInitialization
193 , css::lang::XServiceInfo
194 , css::frame::XDispatchInformationProvider
195 , css::frame::XController2
197 , css::frame::XTitleChangeBroadcaster
198 , css::awt::XUserInputInterception
199 > OGenericUnoController_Base
;
201 struct OGenericUnoController_Data
;
203 class DBACCESS_DLLPUBLIC OGenericUnoController
204 :public OGenericUnoController_MBASE
205 ,public OGenericUnoController_Base
209 SupportedFeatures m_aSupportedFeatures
;
210 ::comphelper::NamedValueCollection
212 ::sfx2::UserInputInterception m_aUserInputInterception
;
213 VclPtr
<ODataView
> m_pView
; // our (VCL) "main window"
216 bool m_bDescribingSupportedFeatures
;
222 struct DispatchTarget
225 css::uno::Reference
< css::frame::XStatusListener
> xListener
;
227 DispatchTarget(css::util::URL _aURL
, css::uno::Reference
< css::frame::XStatusListener
> xRef
) : aURL(std::move(_aURL
)), xListener(std::move(xRef
)) { }
230 typedef std::map
<sal_uInt16
, FeatureState
> StateCache
;
231 typedef std::vector
<DispatchTarget
> Dispatch
;
233 ::std::deque
< FeatureListener
>
234 m_aFeaturesToInvalidate
;
236 std::mutex m_aFeatureMutex
; // locked when features are append to or remove from deque
237 StateCache m_aStateCache
; // save the current status of feature state
238 Dispatch m_arrStatusListener
; // all our listeners where we dispatch status changes
239 OAsynchronousLink m_aAsyncInvalidateAll
;
240 OAsynchronousLink m_aAsyncCloseTask
; // called when a task should be closed
242 css::uno::Reference
< css::util::XURLTransformer
> m_xUrlTransformer
; // needed sometimes
243 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
244 ControllerFrame m_aCurrentFrame
;
245 css::uno::Reference
< css::frame::XDispatchProvider
> m_xSlaveDispatcher
; // for intercepting dispatches
246 css::uno::Reference
< css::frame::XDispatchProvider
> m_xMasterDispatcher
; // ditto
247 css::uno::Reference
< css::sdb::XDatabaseContext
> m_xDatabaseContext
;
248 css::uno::Reference
< css::frame::XTitle
> m_xTitleHelper
;
253 bool m_bCurrentlyModified
: 1;
254 bool m_bExternalTitle
: 1;
258 using OGenericUnoController_MBASE::getMutex
;
259 ::cppu::OBroadcastHelper
& getBroadcastHelper() { return OGenericUnoController_Base::rBHelper
; }
263 OGenericUnoController( const css::uno::Reference
< css::uno::XComponentContext
>& _rM
);
264 OGenericUnoController() = delete;
265 const ::comphelper::NamedValueCollection
&
266 getInitParams() const { return m_aInitParameters
; }
268 // closes the task when possible
271 // if getMenu returns a non empty string than this will be dispatched at the frame
272 virtual void loadMenu(const css::uno::Reference
< css::frame::XFrame
>& _xFrame
);
274 /** called when our menu has been loaded into our frame, can be used to load sub toolbars
276 @param _xLayoutManager
279 virtual void onLoadedMenu(const css::uno::Reference
< css::frame::XLayoutManager
>& _xLayoutManager
);
281 // all the features which should be handled by this class
282 virtual void describeSupportedFeatures();
284 // state of a feature. 'feature' may be the handle of a css::util::URL somebody requested a dispatch interface for OR a toolbar slot.
285 virtual FeatureState
GetState(sal_uInt16 nId
) const;
287 virtual void Execute(sal_uInt16 nId
, const css::uno::Sequence
< css::beans::PropertyValue
>& aArgs
);
289 /** describes a feature supported by the controller
291 Must not be called outside <member>describeSupportedFeatures</member>.
294 the URL of the feature command
296 the id of the feature. Later references to this feature usually happen by id, not by
298 @param _nCommandGroup
299 the command group of the feature. This is important for configuring the controller UI
300 by the user, see also <type scope="css::frame">CommandGroup</type>.
302 void implDescribeSupportedFeature(
303 const OUString
& _rCommandURL
,
304 sal_uInt16 _nFeatureId
,
305 sal_Int16 _nCommandGroup
= css::frame::CommandGroup::INTERNAL
308 /** returns <TRUE/> if the feature is supported, otherwise <FALSE/>
310 The ID of the feature.
312 bool isFeatureSupported( sal_Int32 _nId
);
314 // gets the URL which the given id is assigned to
315 css::util::URL
getURLForId(sal_Int32 _nId
) const;
317 /** determines whether the given feature ID denotes a user-defined feature
319 @see IController::registerCommandURL
321 static bool isUserDefinedFeature( const sal_uInt16 nFeatureId
);
323 /** determines whether the given feature URL denotes a user-defined feature
325 @see IController::registerCommandURL
327 bool isUserDefinedFeature( const OUString
& _rFeatureURL
) const;
329 // connect to a datasource
330 css::uno::Reference
< css::sdbc::XConnection
> connect(
331 const css::uno::Reference
< css::sdbc::XDataSource
>& _xDataSource
334 // connect to a datasource
335 css::uno::Reference
< css::sdbc::XConnection
> connect(
336 const OUString
& _rsDataSourceName
,
337 const OUString
& _rContextInformation
,
338 ::dbtools::SQLExceptionInfo
* _pErrorInfo
341 void startConnectionListening(const css::uno::Reference
< css::sdbc::XConnection
>& _rxConnection
);
342 void stopConnectionListening(const css::uno::Reference
< css::sdbc::XConnection
>& _rxConnection
);
344 /** return the container window of the top most frame
346 The top most container window, nmay be <NULL/>.
348 css::uno::Reference
< css::awt::XWindow
> getTopMostContainerWindow() const;
350 // XInitialize will be called inside initialize
351 virtual void impl_initialize();
353 virtual OUString
getPrivateTitle() const { return OUString(); }
355 css::uno::Reference
< css::frame::XTitle
> impl_getTitleHelper_throw(bool bCreateIfNecessary
= true);
356 virtual css::uno::Reference
< css::frame::XModel
> getPrivateModel() const
358 return css::uno::Reference
< css::frame::XModel
>();
361 virtual void startFrameListening( const css::uno::Reference
< css::frame::XFrame
>& _rxFrame
);
362 virtual void stopFrameListening( const css::uno::Reference
< css::frame::XFrame
>& _rxFrame
);
364 void releaseNumberForComponent();
366 virtual ~OGenericUnoController() override
;
369 void fillSupportedFeatures();
371 void InvalidateAll_Impl();
372 void InvalidateFeature_Impl();
374 void ImplInvalidateFeature( sal_Int32 _nId
, const css::uno::Reference
< css::frame::XStatusListener
>& _xListener
, bool _bForceBroadcast
);
376 void ImplBroadcastFeatureState(const OUString
& _rFeature
, const css::uno::Reference
< css::frame::XStatusListener
> & xListener
, bool _bIgnoreCache
);
378 void executeUserDefinedFeatures( const css::util::URL
& _rFeatureURL
, const css::uno::Sequence
< css::beans::PropertyValue
>& _rArgs
);
381 DECL_DLLPRIVATE_LINK(OnAsyncInvalidateAll
, void*, void);
382 DECL_DLLPRIVATE_LINK(OnAsyncCloseTask
, void*, void);
385 const css::uno::Reference
< css::uno::XComponentContext
>& getORB() const { return m_xContext
; }
386 ODataView
* getView() const { return m_pView
; }
387 weld::Window
* getFrameWeld() const;
388 void setView( const VclPtr
<ODataView
>& i_rView
);
390 // shows an error box if the SQLExceptionInfo is valid
391 void showError(const ::dbtools::SQLExceptionInfo
& _rInfo
);
393 // if there is a css::util::URL translation for the id
394 // ('handle') then if xListener is NULL the change will be forwarded
395 // to all listeners to the given css::util::URL
396 // if there is a toolbar slot with the given id it is updated (the new state is determined via GetState)
397 // if _bForceBroadcast is sal_True, the current feature state is broadcasted no matter if it is the same as the cached state
398 void InvalidateFeature(sal_uInt16 nId
, const css::uno::Reference
< css::frame::XStatusListener
> & xListener
= nullptr, bool _bForceBroadcast
= false);
400 /** InvalidateAll invalidates all features currently known
402 void InvalidateAll();
404 virtual bool Construct(vcl::Window
* pParent
);
406 /** get the layout manager
408 The frame to ask for the layout manager.
410 The layout manager of the frame, can be <NULL/> if the frame isn't initialized.
412 static css::uno::Reference
< css::frame::XLayoutManager
> getLayoutManager(const css::uno::Reference
< css::frame::XFrame
>& _xFrame
);
415 virtual void executeUnChecked(const css::util::URL
& _rCommand
, const css::uno::Sequence
< css::beans::PropertyValue
>& aArgs
) override
;
416 virtual void executeChecked(const css::util::URL
& _rCommand
, const css::uno::Sequence
< css::beans::PropertyValue
>& aArgs
) override
;
417 virtual void executeUnChecked(sal_uInt16 _nCommandId
, const css::uno::Sequence
< css::beans::PropertyValue
>& aArgs
) override
;
418 virtual void executeChecked(sal_uInt16 _nCommandId
, const css::uno::Sequence
< css::beans::PropertyValue
>& aArgs
) override
;
419 virtual bool isCommandEnabled(sal_uInt16 _nCommandId
) const override
;
420 virtual bool isCommandEnabled(const OUString
& _rCompleteCommandURL
) const override
;
421 virtual bool isDataSourceReadOnly() const override
;
422 virtual css::uno::Reference
< css::frame::XController
> getXController() override
;
423 virtual bool interceptUserInput( const NotifyEvent
& _rEvent
) override
;
426 bool isCommandChecked(sal_uInt16 _nCommandId
) const;
428 // css::lang::XEventListener
429 virtual void SAL_CALL
disposing(const css::lang::EventObject
& Source
) override
;
431 // css::util::XModifyListener
432 virtual void SAL_CALL
modified(const css::lang::EventObject
& aEvent
) override
;
435 virtual void SAL_CALL
acquire( ) noexcept override
;
436 virtual void SAL_CALL
release( ) noexcept override
;
438 // css::frame::XController2
439 virtual css::uno::Reference
< css::awt::XWindow
> SAL_CALL
getComponentWindow() override
;
440 virtual OUString SAL_CALL
getViewControllerName() override
;
441 virtual css::uno::Sequence
< css::beans::PropertyValue
> SAL_CALL
getCreationArguments() override
;
443 virtual css::uno::Reference
< css::ui::XSidebarProvider
> SAL_CALL
getSidebar() override
;
446 // css::frame::XController
447 virtual void SAL_CALL
attachFrame(const css::uno::Reference
< css::frame::XFrame
> & xFrame
) override
;
448 virtual sal_Bool SAL_CALL
attachModel(const css::uno::Reference
< css::frame::XModel
> & xModel
) override
;
449 virtual sal_Bool SAL_CALL
suspend(sal_Bool bSuspend
) override
= 0;
450 virtual css::uno::Any SAL_CALL
getViewData() override
;
451 virtual void SAL_CALL
restoreViewData(const css::uno::Any
& Data
) override
;
452 virtual css::uno::Reference
< css::frame::XModel
> SAL_CALL
getModel() override
;
453 virtual css::uno::Reference
< css::frame::XFrame
> SAL_CALL
getFrame() override
;
455 // css::frame::XDispatch
456 virtual void SAL_CALL
dispatch(const css::util::URL
& aURL
, const css::uno::Sequence
< css::beans::PropertyValue
>& aArgs
) override
;
457 virtual void SAL_CALL
addStatusListener(const css::uno::Reference
< css::frame::XStatusListener
> & aListener
, const css::util::URL
& aURL
) override
;
458 virtual void SAL_CALL
removeStatusListener(const css::uno::Reference
< css::frame::XStatusListener
> & aListener
, const css::util::URL
& aURL
) override
;
460 // css::frame::XDispatchProviderInterceptor
461 virtual css::uno::Reference
< css::frame::XDispatchProvider
> SAL_CALL
getSlaveDispatchProvider() override
;
462 virtual void SAL_CALL
setSlaveDispatchProvider(const css::uno::Reference
< css::frame::XDispatchProvider
> & _xNewProvider
) override
;
463 virtual css::uno::Reference
< css::frame::XDispatchProvider
> SAL_CALL
getMasterDispatchProvider() override
;
464 virtual void SAL_CALL
setMasterDispatchProvider(const css::uno::Reference
< css::frame::XDispatchProvider
> & _xNewProvider
) override
;
466 // css::frame::XDispatchProvider
467 virtual css::uno::Reference
< css::frame::XDispatch
> SAL_CALL
queryDispatch(const css::util::URL
& aURL
, const OUString
& aTargetFrameName
, sal_Int32 nSearchFlags
) override
;
468 virtual css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > SAL_CALL
queryDispatches(const css::uno::Sequence
< css::frame::DispatchDescriptor
>& aDescripts
) override
;
470 // css::lang::XComponent
471 virtual void SAL_CALL
dispose() override
; //LLA: need solar mutex {OGenericUnoController_COMPBASE::dispose(); }
472 virtual void SAL_CALL
disposing() override
;
473 virtual void SAL_CALL
addEventListener(const css::uno::Reference
< css::lang::XEventListener
> & aListener
) override
;
474 virtual void SAL_CALL
removeEventListener(const css::uno::Reference
< css::lang::XEventListener
> & aListener
) override
;
476 // css::frame::XFrameActionListener
477 virtual void SAL_CALL
frameAction(const css::frame::FrameActionEvent
& aEvent
) override
;
478 // lang::XInitialization
479 virtual void SAL_CALL
initialize( const css::uno::Sequence
< css::uno::Any
>& aArguments
) override
;
482 virtual OUString SAL_CALL
getImplementationName() override
= 0;
483 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
484 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
= 0;
486 // XDispatchInformationProvider
487 virtual css::uno::Sequence
< ::sal_Int16
> SAL_CALL
getSupportedCommandGroups() override
;
488 virtual css::uno::Sequence
< css::frame::DispatchInformation
> SAL_CALL
getConfigurableDispatchInformation( ::sal_Int16
) override
;
491 virtual OUString SAL_CALL
getTitle( ) override
;
492 virtual void SAL_CALL
setTitle( const OUString
& sTitle
) override
;
494 // XTitleChangeBroadcaster
495 virtual void SAL_CALL
addTitleChangeListener( const css::uno::Reference
< css::frame::XTitleChangeListener
>& xListener
) override
;
496 virtual void SAL_CALL
removeTitleChangeListener( const css::uno::Reference
< css::frame::XTitleChangeListener
>& xListener
) override
;
498 // XUserInputInterception
499 virtual void SAL_CALL
addKeyHandler( const css::uno::Reference
< css::awt::XKeyHandler
>& xHandler
) override
;
500 virtual void SAL_CALL
removeKeyHandler( const css::uno::Reference
< css::awt::XKeyHandler
>& xHandler
) override
;
501 virtual void SAL_CALL
addMouseClickHandler( const css::uno::Reference
< css::awt::XMouseClickHandler
>& xHandler
) override
;
502 virtual void SAL_CALL
removeMouseClickHandler( const css::uno::Reference
< css::awt::XMouseClickHandler
>& xHandler
) override
;
506 #endif // INCLUDED_DBACCESS_GENERICCONTROLLER_HXX
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */