LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / include / dbaccess / genericcontroller.hxx
blob7ce10174e3d122a9a26112d5f84a525aedd93c37
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 <vector>
30 #include <optional>
32 #include <com/sun/star/awt/XUserInputInterception.hpp>
33 #include <com/sun/star/frame/CommandGroup.hpp>
34 #include <com/sun/star/frame/DispatchInformation.hpp>
35 #include <com/sun/star/frame/XController2.hpp>
36 #include <com/sun/star/frame/XDispatch.hpp>
37 #include <com/sun/star/frame/XDispatchInformationProvider.hpp>
38 #include <com/sun/star/frame/XDispatchProviderInterceptor.hpp>
39 #include <com/sun/star/frame/XLayoutManager.hpp>
40 #include <com/sun/star/frame/XTitle.hpp>
41 #include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
42 #include <com/sun/star/lang/EventObject.hpp>
43 #include <com/sun/star/lang/XInitialization.hpp>
44 #include <com/sun/star/lang/XServiceInfo.hpp>
45 #include <com/sun/star/uno/Any.hxx>
46 #include <com/sun/star/uno/Reference.hxx>
47 #include <com/sun/star/uno/Sequence.hxx>
48 #include <com/sun/star/util/URL.hpp>
49 #include <com/sun/star/util/XModifyListener.hpp>
50 #include <comphelper/sharedmutex.hxx>
51 #include <comphelper/namedvaluecollection.hxx>
52 #include <connectivity/dbexception.hxx>
53 #include <cppuhelper/compbase.hxx>
54 #include <cppuhelper/interfacecontainer.h>
55 #include <dbaccess/AsynchronousLink.hxx>
56 #include <dbaccess/controllerframe.hxx>
57 #include <dbaccess/dbaccessdllapi.h>
58 #include <dbaccess/IController.hxx>
59 #include <osl/mutex.hxx>
60 #include <rtl/ustring.hxx>
61 #include <sal/types.h>
62 #include <tools/link.hxx>
63 #include <vcl/vclptr.hxx>
65 namespace com::sun::star {
66 namespace awt { class XKeyHandler; }
67 namespace awt { class XMouseClickHandler; }
68 namespace awt { class XWindow; }
69 namespace beans { struct PropertyValue; }
70 namespace frame { class XController; }
71 namespace frame { class XDispatchProvider; }
72 namespace frame { class XFrame; }
73 namespace frame { class XFrameActionListener; }
74 namespace frame { class XModel; }
75 namespace frame { class XStatusListener; }
76 namespace frame { class XTitleChangeListener; }
77 namespace frame { struct DispatchDescriptor; }
78 namespace frame { struct FrameActionEvent; }
79 namespace lang { class XEventListener; }
80 namespace sdb { class XDatabaseContext; }
81 namespace sdbc { class XConnection; }
82 namespace sdbc { class XDataSource; }
83 namespace ui { class XSidebarProvider; }
84 namespace uno { class XComponentContext; }
85 namespace util { class XURLTransformer; }
88 namespace vcl { class Window; }
89 namespace weld { class Window; }
90 class NotifyEvent;
92 namespace dbaui
94 class ODataView;
96 template< typename T >
97 inline bool SAL_CALL operator >>= (const css::uno::Any& _any, std::optional< T >& _value)
99 _value.reset(); // de-init the optional value
101 T directValue = T();
102 if ( _any >>= directValue )
103 _value = directValue;
105 return !!_value;
109 // = FeatureState
111 /** describes the state of a feature
113 In opposite to the FeatureStateEvent in css.frame, this one allows for multiple states to be specified at once.
114 With this, you can for instance specify that a toolbox item is checked, and has a certain title, at the same
115 time.
117 struct FeatureState
119 bool bEnabled;
121 std::optional<bool> bChecked;
122 std::optional<bool> bInvisible;
123 css::uno::Any aValue;
124 std::optional<OUString> sTitle;
126 FeatureState() : bEnabled(false) { }
130 // = helper
133 struct ControllerFeature : public css::frame::DispatchInformation
135 sal_uInt16 nFeatureId;
139 typedef ::std::map < OUString
140 , ControllerFeature
141 , ::std::less< OUString >
142 > SupportedFeatures;
145 class CompareFeatureById
147 const sal_Int32 m_nId;
148 public:
149 CompareFeatureById(sal_Int32 _nId) : m_nId(_nId)
152 bool operator()( const SupportedFeatures::value_type& _aType ) const
154 return m_nId == _aType.second.nFeatureId;
159 struct FeatureListener
161 css::uno::Reference< css::frame::XStatusListener >
162 xListener;
163 sal_Int32 nId;
164 bool bForceBroadcast;
168 class FindFeatureListener
170 const css::uno::Reference< css::frame::XStatusListener >& m_xListener;
171 public:
172 FindFeatureListener(const css::uno::Reference< css::frame::XStatusListener >& _xListener)
173 : m_xListener(_xListener)
176 bool operator()( const FeatureListener& lhs ) const
178 return lhs.xListener == m_xListener;
183 typedef ::comphelper::SharedMutexBase OGenericUnoController_MBASE;
185 typedef ::cppu::WeakComponentImplHelper< css::frame::XDispatch
186 , css::frame::XDispatchProviderInterceptor
187 , css::util::XModifyListener
188 , css::frame::XFrameActionListener
189 , css::lang::XInitialization
190 , css::lang::XServiceInfo
191 , css::frame::XDispatchInformationProvider
192 , css::frame::XController2
193 , css::frame::XTitle
194 , css::frame::XTitleChangeBroadcaster
195 , css::awt::XUserInputInterception
196 > OGenericUnoController_Base;
198 struct OGenericUnoController_Data;
200 class DBACCESS_DLLPUBLIC OGenericUnoController
201 :public OGenericUnoController_MBASE
202 ,public OGenericUnoController_Base
203 ,public IController
205 private:
206 SupportedFeatures m_aSupportedFeatures;
207 ::comphelper::NamedValueCollection
208 m_aInitParameters;
210 ::std::unique_ptr< OGenericUnoController_Data >
211 m_pData;
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(const css::util::URL& rURL, const css::uno::Reference< css::frame::XStatusListener >& rRef) : aURL(rURL), xListener(rRef) { }
229 typedef std::map<sal_uInt16, FeatureState> StateCache;
230 typedef std::vector<DispatchTarget> Dispatch;
232 ::std::deque< FeatureListener >
233 m_aFeaturesToInvalidate;
235 ::osl::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 _pAsciiCommandURL
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 char* _pAsciiCommandURL,
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 // link methods
378 DECL_DLLPRIVATE_LINK(OnAsyncInvalidateAll, void*, void);
379 DECL_DLLPRIVATE_LINK(OnAsyncCloseTask, void*, void);
381 public:
382 const css::uno::Reference< css::uno::XComponentContext >& getORB() const { return m_xContext; }
383 ODataView* getView() const { return m_pView; }
384 weld::Window* getFrameWeld() const;
385 void setView( const VclPtr<ODataView>& i_rView );
386 void clearView();
387 // shows an error box if the SQLExceptionInfo is valid
388 void showError(const ::dbtools::SQLExceptionInfo& _rInfo);
390 // if there is a css::util::URL translation for the id
391 // ('handle') then if xListener is NULL the change will be forwarded
392 // to all listeners to the given css::util::URL
393 // if there is a toolbar slot with the given id it is updated (the new state is determined via GetState)
394 // if _bForceBroadcast is sal_True, the current feature state is broadcasted no matter if it is the same as the cached state
395 void InvalidateFeature(sal_uInt16 nId, const css::uno::Reference< css::frame::XStatusListener > & xListener = nullptr, bool _bForceBroadcast = false);
397 /** InvalidateAll invalidates all features currently known
399 void InvalidateAll();
400 // late construction
401 virtual bool Construct(vcl::Window* pParent);
403 /** get the layout manager
404 @param _xFrame
405 The frame to ask for the layout manager.
406 @return
407 The layout manager of the frame, can be <NULL/> if the frame isn't initialized.
409 static css::uno::Reference< css::frame::XLayoutManager > getLayoutManager(const css::uno::Reference< css::frame::XFrame >& _xFrame);
411 // IController
412 virtual void executeUnChecked(const css::util::URL& _rCommand, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
413 virtual void executeChecked(const css::util::URL& _rCommand, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
414 virtual void executeUnChecked(sal_uInt16 _nCommandId, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
415 virtual void executeChecked(sal_uInt16 _nCommandId, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
416 virtual bool isCommandEnabled(sal_uInt16 _nCommandId) const override;
417 virtual bool isCommandEnabled(const OUString& _rCompleteCommandURL) const override;
418 virtual bool isDataSourceReadOnly() const override;
419 virtual css::uno::Reference< css::frame::XController > getXController() override;
420 virtual bool interceptUserInput( const NotifyEvent& _rEvent ) override;
422 // misc
423 bool isCommandChecked(sal_uInt16 _nCommandId) const;
425 // css::lang::XEventListener
426 virtual void SAL_CALL disposing(const css::lang::EventObject& Source) override;
428 // css::util::XModifyListener
429 virtual void SAL_CALL modified(const css::lang::EventObject& aEvent) override;
431 // XInterface
432 virtual void SAL_CALL acquire( ) noexcept override;
433 virtual void SAL_CALL release( ) noexcept override;
435 // css::frame::XController2
436 virtual css::uno::Reference< css::awt::XWindow > SAL_CALL getComponentWindow() override;
437 virtual OUString SAL_CALL getViewControllerName() override;
438 virtual css::uno::Sequence< css::beans::PropertyValue > SAL_CALL getCreationArguments() override;
440 virtual css::uno::Reference< css::ui::XSidebarProvider > SAL_CALL getSidebar() override;
443 // css::frame::XController
444 virtual void SAL_CALL attachFrame(const css::uno::Reference< css::frame::XFrame > & xFrame) override;
445 virtual sal_Bool SAL_CALL attachModel(const css::uno::Reference< css::frame::XModel > & xModel) override;
446 virtual sal_Bool SAL_CALL suspend(sal_Bool bSuspend) override = 0;
447 virtual css::uno::Any SAL_CALL getViewData() override;
448 virtual void SAL_CALL restoreViewData(const css::uno::Any& Data) override;
449 virtual css::uno::Reference< css::frame::XModel > SAL_CALL getModel() override;
450 virtual css::uno::Reference< css::frame::XFrame > SAL_CALL getFrame() override;
452 // css::frame::XDispatch
453 virtual void SAL_CALL dispatch(const css::util::URL& aURL, const css::uno::Sequence< css::beans::PropertyValue>& aArgs) override;
454 virtual void SAL_CALL addStatusListener(const css::uno::Reference< css::frame::XStatusListener > & aListener, const css::util::URL& aURL) override;
455 virtual void SAL_CALL removeStatusListener(const css::uno::Reference< css::frame::XStatusListener > & aListener, const css::util::URL& aURL) override;
457 // css::frame::XDispatchProviderInterceptor
458 virtual css::uno::Reference< css::frame::XDispatchProvider > SAL_CALL getSlaveDispatchProvider() override;
459 virtual void SAL_CALL setSlaveDispatchProvider(const css::uno::Reference< css::frame::XDispatchProvider > & _xNewProvider) override;
460 virtual css::uno::Reference< css::frame::XDispatchProvider > SAL_CALL getMasterDispatchProvider() override;
461 virtual void SAL_CALL setMasterDispatchProvider(const css::uno::Reference< css::frame::XDispatchProvider > & _xNewProvider) override;
463 // css::frame::XDispatchProvider
464 virtual css::uno::Reference< css::frame::XDispatch > SAL_CALL queryDispatch(const css::util::URL& aURL, const OUString& aTargetFrameName, sal_Int32 nSearchFlags) override;
465 virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL queryDispatches(const css::uno::Sequence< css::frame::DispatchDescriptor >& aDescripts) override;
467 // css::lang::XComponent
468 virtual void SAL_CALL dispose() override; //LLA: need solar mutex {OGenericUnoController_COMPBASE::dispose(); }
469 virtual void SAL_CALL disposing() override;
470 virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > & aListener) override;
471 virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > & aListener) override;
473 // css::frame::XFrameActionListener
474 virtual void SAL_CALL frameAction(const css::frame::FrameActionEvent& aEvent) override;
475 // lang::XInitialization
476 virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
478 // XServiceInfo
479 virtual OUString SAL_CALL getImplementationName() override = 0;
480 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
481 virtual css::uno::Sequence< OUString> SAL_CALL getSupportedServiceNames() override = 0;
483 // XDispatchInformationProvider
484 virtual css::uno::Sequence< ::sal_Int16 > SAL_CALL getSupportedCommandGroups() override;
485 virtual css::uno::Sequence< css::frame::DispatchInformation > SAL_CALL getConfigurableDispatchInformation( ::sal_Int16 ) override;
487 // XTitle
488 virtual OUString SAL_CALL getTitle( ) override;
489 virtual void SAL_CALL setTitle( const OUString& sTitle ) override;
491 // XTitleChangeBroadcaster
492 virtual void SAL_CALL addTitleChangeListener( const css::uno::Reference< css::frame::XTitleChangeListener >& xListener ) override;
493 virtual void SAL_CALL removeTitleChangeListener( const css::uno::Reference< css::frame::XTitleChangeListener >& xListener ) override;
495 // XUserInputInterception
496 virtual void SAL_CALL addKeyHandler( const css::uno::Reference< css::awt::XKeyHandler >& xHandler ) override;
497 virtual void SAL_CALL removeKeyHandler( const css::uno::Reference< css::awt::XKeyHandler >& xHandler ) override;
498 virtual void SAL_CALL addMouseClickHandler( const css::uno::Reference< css::awt::XMouseClickHandler >& xHandler ) override;
499 virtual void SAL_CALL removeMouseClickHandler( const css::uno::Reference< css::awt::XMouseClickHandler >& xHandler ) override;
503 #endif // INCLUDED_DBACCESS_GENERICCONTROLLER_HXX
506 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */