Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / misc / databaseobjectview.cxx
blobe40734ff2eb4c6242424c2777485d0cdca83c0dd
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 #include "databaseobjectview.hxx"
21 #include "dbustrings.hrc"
22 #include "asyncmodaldialog.hxx"
24 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
25 #include <com/sun/star/frame/TaskCreator.hpp>
26 #include <com/sun/star/frame/XDispatchProvider.hpp>
27 #include <com/sun/star/frame/XFrame.hpp>
28 #include <com/sun/star/frame/XFrames.hpp>
29 #include <com/sun/star/frame/FrameSearchFlag.hpp>
30 #include <com/sun/star/sdb/CommandType.hpp>
31 #include <com/sun/star/sdb/application/XTableUIProvider.hpp>
32 #include <com/sun/star/beans/NamedValue.hpp>
33 #include <com/sun/star/awt/Rectangle.hpp>
35 #include <comphelper/extract.hxx>
36 #include <comphelper/sequence.hxx>
37 #include <connectivity/dbtools.hxx>
38 #include <osl/diagnose.h>
39 #include <toolkit/helper/vclunohelper.hxx>
40 #include <tools/diagnose_ex.h>
41 #include <vcl/window.hxx>
43 namespace dbaui
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::sdbc;
48 using namespace ::com::sun::star::sdb;
49 using namespace ::com::sun::star::sdb::application;
50 using namespace ::com::sun::star::ui::dialogs;
51 using namespace ::com::sun::star::frame;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::awt;
56 // DatabaseObjectView
57 DatabaseObjectView::DatabaseObjectView( const Reference< XComponentContext >& _rxORB,
58 const Reference< XDatabaseDocumentUI >& _rxApplication,
59 const Reference< XFrame >& _rxParentFrame,
60 const OUString& _rComponentURL )
61 :m_xORB ( _rxORB )
62 ,m_xParentFrame ( _rxParentFrame )
63 ,m_xFrameLoader ( )
64 ,m_xApplication ( _rxApplication )
65 ,m_sComponentURL ( _rComponentURL )
67 OSL_ENSURE( m_xORB.is(), "DatabaseObjectView::DatabaseObjectView: invalid service factory!" );
68 OSL_ENSURE( m_xApplication.is(), "DatabaseObjectView::DatabaseObjectView: invalid connection!" );
71 Reference< XConnection > DatabaseObjectView::getConnection() const
73 Reference< XConnection > xConnection;
74 if ( m_xApplication.is() )
75 xConnection = m_xApplication->getActiveConnection();
76 return xConnection;
79 Reference< XComponent > DatabaseObjectView::createNew( const Reference< XDataSource >& _xDataSource, const ::comphelper::NamedValueCollection& i_rDispatchArgs )
81 return doCreateView( makeAny( _xDataSource ), OUString(), i_rDispatchArgs );
84 Reference< XComponent > DatabaseObjectView::openExisting( const Any& _rDataSource, const OUString& _rName,
85 const ::comphelper::NamedValueCollection& i_rDispatchArgs )
87 return doCreateView( _rDataSource, _rName, i_rDispatchArgs );
90 Reference< XComponent > DatabaseObjectView::doCreateView( const Any& _rDataSource, const OUString& _rObjectName,
91 const ::comphelper::NamedValueCollection& i_rCreationArgs )
93 ::comphelper::NamedValueCollection aDispatchArgs;
95 aDispatchArgs.merge( i_rCreationArgs, false ); // false => do not overwrite
96 fillDispatchArgs( aDispatchArgs, _rDataSource, _rObjectName );
97 aDispatchArgs.merge( i_rCreationArgs, true ); // true => do overwrite
99 return doDispatch( aDispatchArgs );
102 Reference< XComponent > DatabaseObjectView::doDispatch( const ::comphelper::NamedValueCollection& i_rDispatchArgs )
104 Reference< XComponent > xReturn;
105 if ( m_xORB.is() )
109 // if we have no externally provided frame, create one
110 if ( !m_xFrameLoader.is() )
112 Reference< XSingleServiceFactory > xFact = TaskCreator::create(m_xORB);
113 Sequence< Any > lArgs(3);
114 NamedValue aProp;
115 sal_Int32 nArg = 0;
117 aProp.Name = "ParentFrame";
118 aProp.Value <<= m_xParentFrame;
119 lArgs[nArg++] <<= aProp;
121 aProp.Name = "TopWindow";
122 aProp.Value <<= sal_True;
123 lArgs[nArg++] <<= aProp;
125 aProp.Name = "SupportPersistentWindowState";
126 aProp.Value <<= sal_True;
127 lArgs[nArg++] <<= aProp;
129 m_xFrameLoader.set(xFact->createInstanceWithArguments(lArgs), UNO_QUERY_THROW);
131 // everything we load can be considered a "top level document", so set the respective bit at the window.
132 // This, amongst other things, triggers that the component in this task participates in the
133 // "ThisComponent"-game for the global application Basic.
134 const Reference< XFrame > xFrame( m_xFrameLoader, UNO_QUERY_THROW );
135 const Reference< XWindow > xFrameWindow( xFrame->getContainerWindow(), UNO_SET_THROW );
136 vcl::Window* pContainerWindow = VCLUnoHelper::GetWindow( xFrameWindow );
137 ENSURE_OR_THROW( pContainerWindow, "no implementation access to the frame's container window!" );
138 pContainerWindow->SetExtendedStyle( pContainerWindow->GetExtendedStyle() | WB_EXT_DOCUMENT );
141 Reference< XComponentLoader > xFrameLoader( m_xFrameLoader, UNO_QUERY_THROW );
142 xReturn = xFrameLoader->loadComponentFromURL(
143 m_sComponentURL,
144 OUString("_self"),
146 i_rDispatchArgs.getPropertyValues()
149 catch( const Exception& )
151 DBG_UNHANDLED_EXCEPTION();
154 return xReturn;
157 void DatabaseObjectView::fillDispatchArgs(
158 ::comphelper::NamedValueCollection& i_rDispatchArgs,
159 const Any& _aDataSource,
160 const OUString& /* _rName */
163 OUString sDataSource;
164 Reference<XDataSource> xDataSource;
165 if ( _aDataSource >>= sDataSource )
167 i_rDispatchArgs.put( OUString(PROPERTY_DATASOURCENAME), sDataSource );
169 else if ( _aDataSource >>= xDataSource )
171 i_rDispatchArgs.put( OUString(PROPERTY_DATASOURCE), xDataSource );
174 i_rDispatchArgs.put( OUString(PROPERTY_ACTIVE_CONNECTION), getConnection() );
177 // QueryDesigner
178 QueryDesigner::QueryDesigner( const Reference< XComponentContext >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication,
179 const Reference< XFrame >& _rxParentFrame, bool _bCreateView )
180 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, _bCreateView ? OUString(URL_COMPONENT_VIEWDESIGN) : OUString(URL_COMPONENT_QUERYDESIGN) )
181 ,m_nCommandType( _bCreateView ? CommandType::TABLE : CommandType::QUERY )
185 void QueryDesigner::fillDispatchArgs( ::comphelper::NamedValueCollection& i_rDispatchArgs, const Any& _aDataSource,
186 const OUString& _rObjectName )
188 DatabaseObjectView::fillDispatchArgs( i_rDispatchArgs, _aDataSource, _rObjectName );
190 const bool bIncludeQueryName = !_rObjectName.isEmpty();
191 const bool bGraphicalDesign = i_rDispatchArgs.getOrDefault( OUString(PROPERTY_GRAPHICAL_DESIGN), sal_True );
192 const bool bEditViewAsSQLCommand = ( m_nCommandType == CommandType::TABLE ) && !bGraphicalDesign;
194 i_rDispatchArgs.put( OUString(PROPERTY_COMMAND_TYPE), m_nCommandType );
196 if ( bIncludeQueryName )
198 i_rDispatchArgs.put( OUString(PROPERTY_COMMAND), _rObjectName );
201 if ( bEditViewAsSQLCommand )
203 i_rDispatchArgs.put( OUString(PROPERTY_ESCAPE_PROCESSING), sal_False );
207 // TableDesigner
208 TableDesigner::TableDesigner( const Reference< XComponentContext >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame )
209 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast< OUString >( URL_COMPONENT_TABLEDESIGN ) )
213 void TableDesigner::fillDispatchArgs( ::comphelper::NamedValueCollection& i_rDispatchArgs, const Any& _aDataSource,
214 const OUString& _rObjectName )
216 DatabaseObjectView::fillDispatchArgs( i_rDispatchArgs, _aDataSource, _rObjectName );
218 if ( !_rObjectName.isEmpty() )
220 i_rDispatchArgs.put( OUString(PROPERTY_CURRENTTABLE), _rObjectName );
224 Reference< XComponent > TableDesigner::doCreateView( const Any& _rDataSource, const OUString& _rObjectName,
225 const ::comphelper::NamedValueCollection& i_rCreationArgs )
227 bool bIsNewDesign = _rObjectName.isEmpty();
229 // let's see whether the connection can provide a dedicated table desginer
230 Reference< XInterface > xDesigner;
231 if ( !bIsNewDesign )
232 xDesigner = impl_getConnectionProvidedDesigner_nothrow( _rObjectName );
234 if ( !xDesigner.is() )
235 return DatabaseObjectView::doCreateView( _rDataSource, _rObjectName, i_rCreationArgs );
237 // try whether the designer is a dialog
238 Reference< XExecutableDialog > xDialog( xDesigner, UNO_QUERY_THROW );
239 if ( xDialog.is() )
241 try { AsyncDialogExecutor::executeModalDialogAsync( xDialog ); }
242 catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
243 return NULL;
246 Reference< XComponent > xDesignerComponent( xDesigner, UNO_QUERY );
247 OSL_ENSURE( xDesignerComponent.is(), "TableDesigner::doCreateView: a designer which is no dialog and no component?" );
248 return xDesignerComponent;
251 Reference< XInterface > TableDesigner::impl_getConnectionProvidedDesigner_nothrow( const OUString& _rTableName )
253 Reference< XInterface > xDesigner;
256 Reference< XTableUIProvider > xTableUIProv( getConnection(), UNO_QUERY );
257 if ( xTableUIProv.is() )
258 xDesigner = xTableUIProv->getTableEditor( getApplicationUI(), _rTableName );
260 catch( const Exception& )
262 DBG_UNHANDLED_EXCEPTION();
264 return xDesigner;
267 // ResultSetBrowser
268 ResultSetBrowser::ResultSetBrowser( const Reference< XComponentContext >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame,
269 bool _bTable )
270 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast < OUString >( URL_COMPONENT_DATASOURCEBROWSER ) )
271 ,m_bTable(_bTable)
275 void ResultSetBrowser::fillDispatchArgs( ::comphelper::NamedValueCollection& i_rDispatchArgs, const Any& _aDataSource,
276 const OUString& _rQualifiedName)
278 DatabaseObjectView::fillDispatchArgs( i_rDispatchArgs, _aDataSource, _rQualifiedName );
279 OSL_ENSURE( !_rQualifiedName.isEmpty(),"A Table name must be set");
280 OUString sCatalog;
281 OUString sSchema;
282 OUString sTable;
283 if ( m_bTable )
284 ::dbtools::qualifiedNameComponents( getConnection()->getMetaData(), _rQualifiedName, sCatalog, sSchema, sTable, ::dbtools::eInDataManipulation );
286 i_rDispatchArgs.put( OUString(PROPERTY_COMMAND_TYPE), (m_bTable ? CommandType::TABLE : CommandType::QUERY) );
287 i_rDispatchArgs.put( OUString(PROPERTY_COMMAND), _rQualifiedName );
288 i_rDispatchArgs.put( OUString(PROPERTY_ENABLE_BROWSER), sal_False );
290 if ( m_bTable )
292 i_rDispatchArgs.put( OUString(PROPERTY_UPDATE_CATALOGNAME), sCatalog );
293 i_rDispatchArgs.put( OUString(PROPERTY_UPDATE_SCHEMANAME), sSchema );
294 i_rDispatchArgs.put( OUString(PROPERTY_UPDATE_TABLENAME), sTable );
298 // RelationDesigner
299 RelationDesigner::RelationDesigner( const Reference< XComponentContext >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame )
300 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast< OUString >( URL_COMPONENT_RELATIONDESIGN ) )
303 } // namespace dbaui
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */