Fix typo
[LibreOffice.git] / include / dbaccess / genericcontroller.hxx
blob5e05581d2ae52126b5c2a6fdbfa5f3003e1d9193
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 XWindow; }
69 namespace beans { struct PropertyValue; }
70 namespace frame { class XDispatchProvider; }
71 namespace frame { class XFrame; }
72 namespace frame { class XFrameActionListener; }
73 namespace frame { class XModel; }
74 namespace frame { class XStatusListener; }
75 namespace sdb { class XDatabaseContext; }
76 namespace sdbc { class XConnection; }
77 namespace sdbc { class XDataSource; }
78 namespace uno { class XComponentContext; }
79 namespace util { class XURLTransformer; }
82 namespace vcl { class Window; }
83 namespace weld { class Window; }
84 namespace framework { class TitleHelper; }
86 namespace dbaui
88 class ODataView;
90 template< typename T >
91 inline bool SAL_CALL operator >>= (const css::uno::Any& _any, std::optional< T >& _value)
93 _value.reset(); // de-init the optional value
95 T directValue = T();
96 if ( _any >>= directValue )
97 _value = directValue;
99 return !!_value;
103 // = FeatureState
105 /** describes the state of a feature
107 In opposite to the FeatureStateEvent in css.frame, this one allows for multiple states to be specified at once.
108 With this, you can for instance specify that a toolbox item is checked, and has a certain title, at the same
109 time.
111 struct FeatureState
113 bool bEnabled;
115 std::optional<bool> bChecked;
116 std::optional<bool> bInvisible;
117 css::uno::Any aValue;
118 std::optional<OUString> sTitle;
120 FeatureState() : bEnabled(false) { }
124 // = helper
127 struct ControllerFeature : public css::frame::DispatchInformation
129 sal_uInt16 nFeatureId;
133 typedef ::std::map < OUString
134 , ControllerFeature
135 , ::std::less< OUString >
136 > SupportedFeatures;
139 class CompareFeatureById
141 const sal_Int32 m_nId;
142 public:
143 CompareFeatureById(sal_Int32 _nId) : m_nId(_nId)
146 bool operator()( const SupportedFeatures::value_type& _aType ) const
148 return m_nId == _aType.second.nFeatureId;
153 struct FeatureListener
155 css::uno::Reference< css::frame::XStatusListener >
156 xListener;
157 sal_Int32 nId;
158 bool bForceBroadcast;
162 class FindFeatureListener
164 const css::uno::Reference< css::frame::XStatusListener >& m_xListener;
165 public:
166 FindFeatureListener(const css::uno::Reference< css::frame::XStatusListener >& _xListener)
167 : m_xListener(_xListener)
170 bool operator()( const FeatureListener& lhs ) const
172 return lhs.xListener == m_xListener;
177 typedef ::comphelper::SharedMutexBase OGenericUnoController_MBASE;
179 typedef ::cppu::WeakComponentImplHelper< css::frame::XDispatch
180 , css::frame::XDispatchProviderInterceptor
181 , css::util::XModifyListener
182 , css::frame::XFrameActionListener
183 , css::lang::XInitialization
184 , css::lang::XServiceInfo
185 , css::frame::XDispatchInformationProvider
186 , css::frame::XController2
187 , css::frame::XTitle
188 , css::frame::XTitleChangeBroadcaster
189 , css::awt::XUserInputInterception
190 > OGenericUnoController_Base;
192 class UNLESS_MERGELIBS_MORE(DBACCESS_DLLPUBLIC) OGenericUnoController
193 :public OGenericUnoController_MBASE
194 ,public OGenericUnoController_Base
195 ,public IController
197 private:
198 SupportedFeatures m_aSupportedFeatures;
199 ::sfx2::UserInputInterception m_aUserInputInterception;
200 VclPtr<ODataView> m_pView; // our (VCL) "main window"
202 #ifdef DBG_UTIL
203 bool m_bDescribingSupportedFeatures;
204 #endif
206 protected:
208 // attributes
209 struct DispatchTarget
211 css::util::URL aURL;
212 css::uno::Reference< css::frame::XStatusListener > xListener;
214 DispatchTarget(css::util::URL _aURL, css::uno::Reference< css::frame::XStatusListener > xRef) : aURL(std::move(_aURL)), xListener(std::move(xRef)) { }
217 ::std::deque< FeatureListener >
218 m_aFeaturesToInvalidate;
220 std::mutex m_aFeatureMutex; // locked when features are append to or remove from deque
221 std::map<sal_uInt16, FeatureState> m_aStateCache; // save the current status of feature state
222 std::vector<DispatchTarget> m_arrStatusListener; // all our listeners where we dispatch status changes
223 OAsynchronousLink m_aAsyncInvalidateAll;
224 OAsynchronousLink m_aAsyncCloseTask; // called when a task should be closed
226 css::uno::Reference< css::util::XURLTransformer > m_xUrlTransformer; // needed sometimes
227 css::uno::Reference< css::uno::XComponentContext > m_xContext;
228 ControllerFrame m_aCurrentFrame;
229 css::uno::Reference< css::frame::XDispatchProvider > m_xSlaveDispatcher; // for intercepting dispatches
230 css::uno::Reference< css::frame::XDispatchProvider > m_xMasterDispatcher; // ditto
231 css::uno::Reference< css::sdb::XDatabaseContext > m_xDatabaseContext;
232 rtl::Reference<::framework::TitleHelper > m_xTitleHelper;
234 bool m_bPreview;
235 bool m_bReadOnly;
237 bool m_bCurrentlyModified : 1;
238 bool m_bExternalTitle : 1;
241 // attribute access
242 using OGenericUnoController_MBASE::getMutex;
243 ::cppu::OBroadcastHelper& getBroadcastHelper() { return OGenericUnoController_Base::rBHelper; }
246 // methods
247 OGenericUnoController( const css::uno::Reference< css::uno::XComponentContext >& _rM );
248 OGenericUnoController() = delete;
250 // closes the task when possible
251 void closeTask();
253 // if getMenu returns a non empty string than this will be dispatched at the frame
254 virtual void loadMenu(const css::uno::Reference< css::frame::XFrame >& _xFrame);
256 /** called when our menu has been loaded into our frame, can be used to load sub toolbars
258 @param _xLayoutManager
259 The layout manager.
261 virtual void onLoadedMenu(const css::uno::Reference< css::frame::XLayoutManager >& _xLayoutManager);
263 // all the features which should be handled by this class
264 virtual void describeSupportedFeatures();
266 // state of a feature. 'feature' may be the handle of a css::util::URL somebody requested a dispatch interface for OR a toolbar slot.
267 virtual FeatureState GetState(sal_uInt16 nId) const;
268 // execute a feature
269 virtual void Execute(sal_uInt16 nId , const css::uno::Sequence< css::beans::PropertyValue>& aArgs);
271 /** describes a feature supported by the controller
273 Must not be called outside <member>describeSupportedFeatures</member>.
275 @param _rCommandURL
276 the URL of the feature command
277 @param _nFeatureId
278 the id of the feature. Later references to this feature usually happen by id, not by
280 @param _nCommandGroup
281 the command group of the feature. This is important for configuring the controller UI
282 by the user, see also <type scope="css::frame">CommandGroup</type>.
284 void implDescribeSupportedFeature(
285 const OUString& _rCommandURL,
286 sal_uInt16 _nFeatureId,
287 sal_Int16 _nCommandGroup = css::frame::CommandGroup::INTERNAL
290 /** returns <TRUE/> if the feature is supported, otherwise <FALSE/>
291 @param _nId
292 The ID of the feature.
294 bool isFeatureSupported( sal_Int32 _nId );
296 // gets the URL which the given id is assigned to
297 css::util::URL getURLForId(sal_Int32 _nId) const;
299 /** determines whether the given feature ID denotes a user-defined feature
301 @see IController::registerCommandURL
303 static bool isUserDefinedFeature( const sal_uInt16 nFeatureId );
305 /** determines whether the given feature URL denotes a user-defined feature
307 @see IController::registerCommandURL
309 bool isUserDefinedFeature( const OUString& _rFeatureURL ) const;
311 // connect to a datasource
312 css::uno::Reference< css::sdbc::XConnection > connect(
313 const css::uno::Reference< css::sdbc::XDataSource>& _xDataSource
316 // connect to a datasource
317 css::uno::Reference< css::sdbc::XConnection > connect(
318 const OUString& _rsDataSourceName,
319 const OUString& _rContextInformation,
320 ::dbtools::SQLExceptionInfo* _pErrorInfo
323 void startConnectionListening(const css::uno::Reference< css::sdbc::XConnection >& _rxConnection);
324 void stopConnectionListening(const css::uno::Reference< css::sdbc::XConnection >& _rxConnection);
326 /** return the container window of the top most frame
327 @return
328 The top most container window, nmay be <NULL/>.
330 css::uno::Reference< css::awt::XWindow> getTopMostContainerWindow() const;
332 // XInitialize will be called inside initialize
333 virtual void impl_initialize(const ::comphelper::NamedValueCollection& rArguments);
335 virtual OUString getPrivateTitle() const { return OUString(); }
337 css::uno::Reference< css::frame::XTitle > impl_getTitleHelper_throw(bool bCreateIfNecessary = true);
338 virtual css::uno::Reference< css::frame::XModel > getPrivateModel() const
340 return css::uno::Reference< css::frame::XModel >();
343 virtual void startFrameListening( const css::uno::Reference< css::frame::XFrame >& _rxFrame );
344 virtual void stopFrameListening( const css::uno::Reference< css::frame::XFrame >& _rxFrame );
346 void releaseNumberForComponent();
348 virtual ~OGenericUnoController() override;
350 private:
351 void fillSupportedFeatures();
353 void InvalidateAll_Impl();
354 void InvalidateFeature_Impl();
356 void ImplInvalidateFeature( sal_Int32 _nId, const css::uno::Reference< css::frame::XStatusListener >& _xListener, bool _bForceBroadcast );
358 void ImplBroadcastFeatureState(const OUString& _rFeature, const css::uno::Reference< css::frame::XStatusListener > & xListener, bool _bIgnoreCache);
360 void executeUserDefinedFeatures( const css::util::URL& _rFeatureURL, const css::uno::Sequence< css::beans::PropertyValue>& _rArgs );
362 // link methods
363 DECL_DLLPRIVATE_LINK(OnAsyncInvalidateAll, void*, void);
364 DECL_DLLPRIVATE_LINK(OnAsyncCloseTask, void*, void);
366 public:
367 const css::uno::Reference< css::uno::XComponentContext >& getORB() const { return m_xContext; }
368 ODataView* getView() const { return m_pView; }
369 weld::Window* getFrameWeld() const;
370 void setView( const VclPtr<ODataView>& i_rView );
371 void clearView();
372 // shows an error box if the SQLExceptionInfo is valid
373 void showError(const ::dbtools::SQLExceptionInfo& _rInfo);
375 // if there is a css::util::URL translation for the id
376 // ('handle') then if xListener is NULL the change will be forwarded
377 // to all listeners to the given css::util::URL
378 // if there is a toolbar slot with the given id it is updated (the new state is determined via GetState)
379 // if _bForceBroadcast is sal_True, the current feature state is broadcasted no matter if it is the same as the cached state
380 void InvalidateFeature(sal_uInt16 nId, const css::uno::Reference< css::frame::XStatusListener > & xListener = nullptr, bool _bForceBroadcast = false);
382 /** InvalidateAll invalidates all features currently known
384 void InvalidateAll();
385 // late construction
386 virtual bool Construct(vcl::Window* pParent);
388 /** get the layout manager
389 @param _xFrame
390 The frame to ask for the layout manager.
391 @return
392 The layout manager of the frame, can be <NULL/> if the frame isn't initialized.
394 static css::uno::Reference< css::frame::XLayoutManager > getLayoutManager(const css::uno::Reference< css::frame::XFrame >& _xFrame);
396 // IController
397 virtual void executeUnChecked(const css::util::URL& _rCommand, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
398 virtual void executeChecked(const css::util::URL& _rCommand, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
399 virtual void executeUnChecked(sal_uInt16 _nCommandId, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
400 virtual void executeChecked(sal_uInt16 _nCommandId, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
401 virtual bool isCommandEnabled(sal_uInt16 _nCommandId) const override;
402 virtual bool isCommandEnabled(const OUString& _rCompleteCommandURL) const override;
403 virtual bool isDataSourceReadOnly() const override;
404 virtual css::uno::Reference< css::frame::XController > getXController() override;
405 virtual bool interceptUserInput( const NotifyEvent& _rEvent ) override;
407 // misc
408 bool isCommandChecked(sal_uInt16 _nCommandId) const;
410 // css::lang::XEventListener
411 virtual void SAL_CALL disposing(const css::lang::EventObject& Source) override;
413 // css::util::XModifyListener
414 virtual void SAL_CALL modified(const css::lang::EventObject& aEvent) override;
416 // XInterface
417 virtual void SAL_CALL acquire( ) noexcept override;
418 virtual void SAL_CALL release( ) noexcept override;
420 // css::frame::XController2
421 virtual css::uno::Reference< css::awt::XWindow > SAL_CALL getComponentWindow() override;
422 virtual OUString SAL_CALL getViewControllerName() override;
423 virtual css::uno::Sequence< css::beans::PropertyValue > SAL_CALL getCreationArguments() override;
425 virtual css::uno::Reference< css::ui::XSidebarProvider > SAL_CALL getSidebar() override;
428 // css::frame::XController
429 virtual void SAL_CALL attachFrame(const css::uno::Reference< css::frame::XFrame > & xFrame) override;
430 virtual sal_Bool SAL_CALL attachModel(const css::uno::Reference< css::frame::XModel > & xModel) override;
431 virtual sal_Bool SAL_CALL suspend(sal_Bool bSuspend) override = 0;
432 virtual css::uno::Any SAL_CALL getViewData() override;
433 virtual void SAL_CALL restoreViewData(const css::uno::Any& Data) override;
434 virtual css::uno::Reference< css::frame::XModel > SAL_CALL getModel() override;
435 virtual css::uno::Reference< css::frame::XFrame > SAL_CALL getFrame() override;
437 // css::frame::XDispatch
438 virtual void SAL_CALL dispatch(const css::util::URL& aURL, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
439 virtual void SAL_CALL addStatusListener(const css::uno::Reference< css::frame::XStatusListener > & aListener, const css::util::URL& aURL) override;
440 virtual void SAL_CALL removeStatusListener(const css::uno::Reference< css::frame::XStatusListener > & aListener, const css::util::URL& aURL) override;
442 // css::frame::XDispatchProviderInterceptor
443 virtual css::uno::Reference< css::frame::XDispatchProvider > SAL_CALL getSlaveDispatchProvider() override;
444 virtual void SAL_CALL setSlaveDispatchProvider(const css::uno::Reference< css::frame::XDispatchProvider > & _xNewProvider) override;
445 virtual css::uno::Reference< css::frame::XDispatchProvider > SAL_CALL getMasterDispatchProvider() override;
446 virtual void SAL_CALL setMasterDispatchProvider(const css::uno::Reference< css::frame::XDispatchProvider > & _xNewProvider) override;
448 // css::frame::XDispatchProvider
449 virtual css::uno::Reference< css::frame::XDispatch > SAL_CALL queryDispatch(const css::util::URL& aURL, const OUString& aTargetFrameName, sal_Int32 nSearchFlags) override;
450 virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL queryDispatches(const css::uno::Sequence< css::frame::DispatchDescriptor >& aDescripts) override;
452 // css::lang::XComponent
453 virtual void SAL_CALL dispose() override; //LLA: need solar mutex {OGenericUnoController_COMPBASE::dispose(); }
454 virtual void SAL_CALL disposing() override;
455 virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > & aListener) override;
456 virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > & aListener) override;
458 // css::frame::XFrameActionListener
459 virtual void SAL_CALL frameAction(const css::frame::FrameActionEvent& aEvent) override;
460 // lang::XInitialization
461 virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
463 // XServiceInfo
464 virtual OUString SAL_CALL getImplementationName() override = 0;
465 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
466 virtual css::uno::Sequence< OUString> SAL_CALL getSupportedServiceNames() override = 0;
468 // XDispatchInformationProvider
469 virtual css::uno::Sequence< ::sal_Int16 > SAL_CALL getSupportedCommandGroups() override;
470 virtual css::uno::Sequence< css::frame::DispatchInformation > SAL_CALL getConfigurableDispatchInformation( ::sal_Int16 ) override;
472 // XTitle
473 virtual OUString SAL_CALL getTitle( ) override;
474 virtual void SAL_CALL setTitle( const OUString& sTitle ) override;
476 // XTitleChangeBroadcaster
477 virtual void SAL_CALL addTitleChangeListener( const css::uno::Reference< css::frame::XTitleChangeListener >& xListener ) override;
478 virtual void SAL_CALL removeTitleChangeListener( const css::uno::Reference< css::frame::XTitleChangeListener >& xListener ) override;
480 // XUserInputInterception
481 virtual void SAL_CALL addKeyHandler( const css::uno::Reference< css::awt::XKeyHandler >& xHandler ) override;
482 virtual void SAL_CALL removeKeyHandler( const css::uno::Reference< css::awt::XKeyHandler >& xHandler ) override;
483 virtual void SAL_CALL addMouseClickHandler( const css::uno::Reference< css::awt::XMouseClickHandler >& xHandler ) override;
484 virtual void SAL_CALL removeMouseClickHandler( const css::uno::Reference< css::awt::XMouseClickHandler >& xHandler ) override;
488 #endif // INCLUDED_DBACCESS_GENERICCONTROLLER_HXX
491 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */