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 .
22 #include <AppElementType.hxx>
23 #include <callbacks.hxx>
24 #include <commontypes.hxx>
25 #include <dsntypes.hxx>
26 #include <dbaccess/genericcontroller.hxx>
27 #include <linkeddocuments.hxx>
28 #include <TableCopyHelper.hxx>
30 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
31 #include <com/sun/star/container/XContainerListener.hpp>
32 #include <com/sun/star/sdb/application/XDatabaseDocumentUI.hpp>
33 #include <com/sun/star/ui/XContextMenuInterception.hpp>
35 #include <comphelper/namedvaluecollection.hxx>
36 #include <comphelper/uno3.hxx>
37 #include <cppuhelper/implbase5.hxx>
38 #include <comphelper/interfacecontainer2.hxx>
39 #include <vcl/transfer.hxx>
40 #include <svx/dataaccessdescriptor.hxx>
44 class TransferableHelper
;
45 class TransferableClipboardListener
;
47 namespace com::sun::star
{
59 class OComponentTransferable
;
71 class SubComponentManager
;
72 class OApplicationController
;
73 class OApplicationView
;
74 class OLinkedDocumentsAccess
;
75 class SelectionNotifier
;
77 typedef ::cppu::ImplHelper5
< css::container::XContainerListener
78 , css::beans::XPropertyChangeListener
79 , css::sdb::application::XDatabaseDocumentUI
80 , css::ui::XContextMenuInterception
81 , css::view::XSelectionSupplier
82 > OApplicationController_Base
;
85 class OApplicationController
86 :public OGenericUnoController
87 ,public OApplicationController_Base
88 ,public IControlActionListener
89 ,public IContextMenuProvider
92 typedef std::vector
< css::uno::Reference
< css::container::XContainer
> > TContainerVector
;
96 OTableCopyHelper::DropDescriptor m_aAsyncDrop
;
98 SharedConnection m_xDataSourceConnection
;
99 css::uno::Reference
< css::sdbc::XDatabaseMetaData
>
102 TransferableDataHelper m_aSystemClipboard
; // content of the clipboard
103 css::uno::Reference
< css::beans::XPropertySet
>
105 css::uno::Reference
< css::frame::XModel
>
107 ::comphelper::OInterfaceContainerHelper2
108 m_aContextMenuInterceptors
;
110 TContainerVector m_aCurrentContainers
; // the containers where we are listener on
111 ::rtl::Reference
< SubComponentManager
>
112 m_pSubComponentManager
;
113 ::dbaccess::ODsnTypeCollection
115 OTableCopyHelper m_aTableCopyHelper
;
116 rtl::Reference
<TransferableClipboardListener
>
117 m_pClipboardNotifier
; // notifier for changes in the clipboard
118 ImplSVEvent
* m_nAsyncDrop
;
119 OAsynchronousLink m_aSelectContainerEvent
;
120 PreviewMode m_ePreviewMode
; // the mode of the preview
121 ElementType m_eCurrentType
;
122 bool m_bNeedToReconnect
; // true when the settings of the data source were modified and the connection is no longer up to date
123 bool m_bSuspended
; // is true when the controller was already suspended
125 std::unique_ptr
< SelectionNotifier
>
126 m_pSelectionNotifier
;
127 typedef std::map
< ElementType
, std::vector
< OUString
> > SelectionByElementType
;
128 SelectionByElementType m_aPendingSelection
;
132 OApplicationView
* getContainer() const;
134 /** returns the database name
138 OUString
getDatabaseName() const;
140 /** returns the stripped database name.
142 The stripped database name either the registered name or if it is a file url the last segment.
144 OUString
getStrippedDatabaseName() const;
146 /** return the element type for given container
147 @param _xContainer The container where the element type has to be found
148 @return the element type corresponding to the given container
150 static ElementType
getElementType(const css::uno::Reference
< css::container::XContainer
>& _xContainer
);
152 /** opens a new sub frame with a table/query/form/report/view, passing additional arguments
154 css::uno::Reference
< css::lang::XComponent
> openElementWithArguments(
155 const OUString
& _sName
,
157 ElementOpenMode _eOpenMode
,
158 sal_uInt16 _nInstigatorCommand
,
159 const ::comphelper::NamedValueCollection
& _rAdditionalArguments
162 /** opens a new frame for creation or auto pilot
164 Defines the type to open
165 @param i_rAdditionalArguments
166 Additional arguments to pass when creating the component
168 css::uno::Reference
< css::lang::XComponent
>
171 const ::comphelper::NamedValueCollection
& i_rAdditionalArguments
,
172 css::uno::Reference
< css::lang::XComponent
>& o_rDocumentDefinition
175 /** creates a new database object, using an auto pilot
177 Defines the type of the object to create
179 Our mutex must not be locked.
182 void newElementWithPilot( ElementType _eType
);
184 /** converts the query to a view
186 The name of the query.
188 void convertToView(const OUString
& _sName
);
190 /** checks if the connection for the selected data source is read only. If the connection doesn't exist, <TRUE/> will be returned.
192 <TRUE/> if read only or doesn't exist, otherwise <FALSE/>
194 bool isConnectionReadOnly() const;
196 /// fills the list with the selected entries.
197 void getSelectionElementNames( std::vector
< OUString
>& _rNames
) const;
199 /// deletes the entries selected.
200 void deleteEntries();
202 /// renames the selected entry in the detail page
205 /** deletes queries, forms, or reports
207 the type of the objects
209 The names of the elements to delete
211 determines whether the user must confirm the deletion
213 void deleteObjects( ElementType _eType
,
214 const std::vector
< OUString
>& _rList
,
221 void deleteTables(const std::vector
< OUString
>& _rList
);
223 /// copies the current object into clipboard
224 rtl::Reference
<TransferableHelper
> copyObject();
226 /// fills rExchange with current object if it's a Table or Query
227 bool copySQLObject(ODataClipboard
& rExchange
);
229 /// fills rExchange with current object if it's a Form or Report
230 bool copyDocObject(svx::OComponentTransferable
& rExchange
);
232 /// returns the nameaccess
233 css::uno::Reference
< css::container::XNameAccess
> getElements(ElementType _eType
);
235 /** returns the document access for the specific type
238 @return std::unique_ptr<OLinkedDocumentsAccess>
240 std::unique_ptr
<OLinkedDocumentsAccess
> getDocumentsAccess(ElementType _eType
);
242 /// returns the query definitions of the active data source.
243 css::uno::Reference
< css::container::XNameContainer
> getQueryDefinitions() const;
245 /** pastes a special format from the system clipboard to the currently selected object types
247 The format to be copied.
249 void pasteFormat(SotClipboardFormatId _nFormatId
);
251 /** pastes a query, form or report into the data source
253 The type of the object to paste.
256 @param _sParentFolder
257 The name of the parent folder if it exists.
259 if <TRUE/> the name of the content must be inserted without any change, otherwise not.
261 <TRUE/> if the paste operations was successful, otherwise <FALSE/>.
263 bool paste( ElementType _eType
, const svx::ODataAccessDescriptor
& _rPasteData
, const OUString
& _sParentFolder
= OUString(), bool _bMove
= false);
265 /// returns the system clipboard.
266 const TransferableDataHelper
& getViewClipboard() const { return m_aSystemClipboard
; }
268 /// returns <TRUE/> if the clipboard supports a table format, otherwise <FALSE/>.
269 bool isTableFormat() const;
271 /** fills the vector with all supported formats
273 The type for which we need the formats
275 The vector to be filled up.
277 static void getSupportedFormats(ElementType _eType
,std::vector
<SotClipboardFormatId
>& _rFormatIds
);
279 /** adds a listener to the current name access.
281 The collection where we want to listen on.
283 void addContainerListener(const css::uno::Reference
< css::container::XNameAccess
>& _xCollection
);
285 /** opens a uno dialog with the currently selected data source as initialize argument
287 The service name of the dialog to be executed.
289 void openDialog(const OUString
& _sServiceName
);
291 /** when the settings of the data source changed,
292 it opens a dialog which ask to close all depending documents, then recreate the connection.
293 The SolarMutex has to be locked before calling this.
295 void askToReconnect();
297 /** remember a newly opened sub document for later access
299 void onDocumentOpened(
300 const OUString
& _rName
,
301 const sal_Int32 _nType
,
302 const ElementOpenMode _eMode
,
303 const css::uno::Reference
< css::lang::XComponent
>& _xDocument
,
304 const css::uno::Reference
< css::lang::XComponent
>& _xDefinition
307 /** Inserts a new object into the hierarchy given be the type.
309 Where to insert the new item.
310 @param _sParentFolder
311 The name of the parent folder if it exists.
313 The content to insert.
315 if <TRUE/> the name of the content must be inserted without any change, otherwise not.
317 <TRUE/> if the insert operations was successful, otherwise <FALSE/>.
319 bool insertHierarchyElement( ElementType _eType
320 ,const OUString
& _sParentFolder
321 ,bool _bCollection
= true
322 ,const css::uno::Reference
< css::ucb::XContent
>& _xContent
= css::uno::Reference
< css::ucb::XContent
>()
323 ,bool _bMove
= false);
324 /** checks if delete command or rename command is allowed
328 If <TRUE> then the delete command should be checked.
330 <TRUE> if the command is allowed
332 bool isRenameDeleteAllowed(ElementType _eType
, bool _bDelete
) const;
333 /** all selected entries will be opened, or edited, or converted to a view
335 The slot which should be executed.
337 Defines the mode of opening. @see ElementOpenMode
339 void doAction(sal_uInt16 _nId
, ElementOpenMode _eOpenMode
);
341 /** returns the currently selected table or query name.
343 * \return the name of the currently table or query. If the tables or query container is selected otherwise an empty string will be returned.
345 OUString
getCurrentlySelectedName(sal_Int32
& _rnCommandType
) const;
347 /** shows the preview for the given entry
349 void showPreviewFor( const ElementType _eType
,const OUString
& _sName
);
351 /** called we were attached to a frame
353 In particular, this is called *after* the controller has been announced to the model
354 (XModel::connectController)
356 void onAttachedFrame();
358 /// determines whether the given table name denotes a view which can be altered
359 bool impl_isAlterableView_nothrow( const OUString
& _rTableOrViewName
) const;
361 /** verifies the object type denotes a valid DatabaseObject, and the object name denotes an existing
362 object of this type. Throws if not.
364 void impl_validateObjectTypeAndName_throw( const sal_Int32 _nObjectType
, const ::std::optional
< OUString
>& i_rObjectName
);
367 // initializing members
369 // state of a feature. 'feature' may be the handle of a css::util::URL somebody requested a dispatch interface for OR a toolbar slot.
370 virtual FeatureState
GetState(sal_uInt16 nId
) const override
;
372 virtual void Execute(sal_uInt16 nId
, const css::uno::Sequence
< css::beans::PropertyValue
>& aArgs
) override
;
374 // OGenericUnoController
375 virtual void onLoadedMenu( const css::uno::Reference
< css::frame::XLayoutManager
>& _xLayoutManager
) override
;
377 virtual css::uno::Reference
< css::frame::XModel
> getPrivateModel() const override
382 virtual ~OApplicationController() override
;
385 explicit OApplicationController(const css::uno::Reference
< css::uno::XComponentContext
>& _rxORB
);
387 DECLARE_XINTERFACE( )
388 DECLARE_XTYPEPROVIDER( )
391 virtual OUString SAL_CALL
getImplementationName() override
;
392 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
394 // css::frame::XController
395 virtual void SAL_CALL
attachFrame(const css::uno::Reference
< css::frame::XFrame
> & xFrame
) override
;
396 virtual sal_Bool SAL_CALL
suspend(sal_Bool bSuspend
) override
;
397 virtual sal_Bool SAL_CALL
attachModel(const css::uno::Reference
< css::frame::XModel
> & xModel
) override
;
398 virtual css::uno::Reference
< css::frame::XModel
> SAL_CALL
getModel() override
;
400 // css::container::XContainerListener
401 virtual void SAL_CALL
elementInserted(const css::container::ContainerEvent
& Event
) override
;
402 virtual void SAL_CALL
elementRemoved(const css::container::ContainerEvent
& Event
) override
;
403 virtual void SAL_CALL
elementReplaced(const css::container::ContainerEvent
& Event
) override
;
405 // XPropertyChangeListener
406 virtual void SAL_CALL
propertyChange( const css::beans::PropertyChangeEvent
& evt
) override
;
408 // XDatabaseDocumentUI
409 virtual css::uno::Reference
< css::sdbc::XDataSource
> SAL_CALL
getDataSource() override
;
410 virtual css::uno::Reference
< css::awt::XWindow
> SAL_CALL
getApplicationMainWindow() override
;
411 virtual css::uno::Reference
< css::sdbc::XConnection
> SAL_CALL
getActiveConnection() override
;
412 virtual css::uno::Sequence
< css::uno::Reference
< css::lang::XComponent
> > SAL_CALL
getSubComponents() override
;
413 virtual sal_Bool SAL_CALL
isConnected( ) override
;
414 // DO NOT CALL with getMutex() held!!
415 virtual void SAL_CALL
connect( ) override
;
416 virtual css::beans::Pair
< ::sal_Int32
, OUString
> SAL_CALL
identifySubComponent( const css::uno::Reference
< css::lang::XComponent
>& SubComponent
) override
;
417 virtual sal_Bool SAL_CALL
closeSubComponents( ) override
;
418 virtual css::uno::Reference
< css::lang::XComponent
> SAL_CALL
loadComponent( ::sal_Int32 ObjectType
, const OUString
& ObjectName
, sal_Bool ForEditing
) override
;
419 virtual css::uno::Reference
< css::lang::XComponent
> SAL_CALL
loadComponentWithArguments( ::sal_Int32 ObjectType
, const OUString
& ObjectName
, sal_Bool ForEditing
, const css::uno::Sequence
< css::beans::PropertyValue
>& Arguments
) override
;
420 virtual css::uno::Reference
< css::lang::XComponent
> SAL_CALL
createComponent( ::sal_Int32 ObjectType
, css::uno::Reference
< css::lang::XComponent
>& o_DocumentDefinition
) override
;
421 virtual css::uno::Reference
< css::lang::XComponent
> SAL_CALL
createComponentWithArguments( ::sal_Int32 ObjectType
, const css::uno::Sequence
< css::beans::PropertyValue
>& Arguments
, css::uno::Reference
< css::lang::XComponent
>& o_DocumentDefinition
) override
;
423 // XContextMenuInterception
424 virtual void SAL_CALL
registerContextMenuInterceptor( const css::uno::Reference
< css::ui::XContextMenuInterceptor
>& Interceptor
) override
;
425 virtual void SAL_CALL
releaseContextMenuInterceptor( const css::uno::Reference
< css::ui::XContextMenuInterceptor
>& Interceptor
) override
;
427 // XSelectionSupplier
428 virtual sal_Bool SAL_CALL
select( const css::uno::Any
& xSelection
) override
;
429 virtual css::uno::Any SAL_CALL
getSelection( ) override
;
430 virtual void SAL_CALL
addSelectionChangeListener( const css::uno::Reference
< css::view::XSelectionChangeListener
>& xListener
) override
;
431 virtual void SAL_CALL
removeSelectionChangeListener( const css::uno::Reference
< css::view::XSelectionChangeListener
>& xListener
) override
;
433 /** retrieves the current connection, creates it if necessary
435 If an error occurs, then this is either stored in the location pointed to by <arg>_pErrorInfo</arg>,
436 or, if <code>_pErrorInfo</code> is <NULL/>, then the error is displayed to the user.
438 DO NOT CALL with getMutex() held!!
440 const SharedConnection
& ensureConnection( ::dbtools::SQLExceptionInfo
* _pErrorInfo
= nullptr );
442 /** retrieves the current connection
444 const SharedConnection
& getConnection() const { return m_xDataSourceConnection
; }
446 /// determines whether we're currently connected to the database
447 bool isConnected() const { return m_xDataSourceConnection
.is(); }
449 /** refreshes the tables
451 void refreshTables();
453 /** called when an entry in a tree list box has been double-clicked
457 <TRUE/> if the double click event has been handled by the called, and should not
458 be processed further.
460 bool onEntryDoubleClick(const weld::TreeView
& rTree
);
462 /** called when a container (category) in the application view has been selected
466 <TRUE/> if the container could be changed otherwise <FALSE/>
468 bool onContainerSelect(ElementType _eType
);
470 /** called when an entry in a tree view has been selected
474 void onSelectionChanged();
476 /** called when a "Copy" command is executed in a tree view
480 /** called when a "Paste" command is executed in a tree view
484 /** called when a "Delete" command is executed in a tree view
486 void onDeleteEntry();
488 /// called when the preview mode was changed
489 void previewChanged( sal_Int32 _nMode
);
491 /// called when an object container of any kind was found during enumerating tree view elements
492 void containerFound( const css::uno::Reference
< css::container::XContainer
>& _xContainer
);
495 virtual bool isDataSourceReadOnly() const override
;
497 // IControlActionListener overridables
498 virtual bool requestQuickHelp(const void* pUserData
, OUString
& rText
) const override
;
499 virtual bool requestDrag(const weld::TreeIter
& rEntry
) override
;
500 virtual sal_Int8
queryDrop( const AcceptDropEvent
& _rEvt
, const DataFlavorExVector
& _rFlavors
) override
;
501 virtual sal_Int8
executeDrop( const ExecuteDropEvent
& _rEvt
) override
;
503 // IContextMenuProvider
504 virtual OUString
getContextMenuResourceName() const override
;
505 virtual IController
& getCommandController() override
;
506 virtual ::comphelper::OInterfaceContainerHelper2
*
507 getContextMenuInterceptors() override
;
508 virtual css::uno::Any
getCurrentSelection(weld::TreeView
& rControl
) const override
;
509 virtual vcl::Window
* getMenuParent() const override
;
510 virtual void adjustMenuPosition(const weld::TreeView
& rControl
, ::Point
& rPos
) const override
;
512 void OnInvalidateClipboard();
513 DECL_LINK( OnClipboardChanged
, TransferableDataHelper
*, void );
514 DECL_LINK( OnAsyncDrop
, void*, void );
515 DECL_LINK( OnCreateWithPilot
, void*, void );
516 DECL_LINK( OnSelectContainer
, void*, void );
517 void OnFirstControllerConnected();
520 using OGenericUnoController::connect
;
522 /** disconnects from our XConnection, and cleans up this connection
527 virtual bool Construct(vcl::Window
* pParent
) override
;
528 virtual void describeSupportedFeatures() override
;
532 virtual void SAL_CALL
disposing(const css::lang::EventObject
& Source
) override
;
535 virtual void SAL_CALL
disposing() override
;
540 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */