Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "databaseobjectview.hxx"
22 : #include "dbustrings.hrc"
23 : #include "asyncmodaldialog.hxx"
24 :
25 : #include <com/sun/star/lang/XSingleServiceFactory.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>
34 :
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>
42 :
43 : // .........................................................................
44 : namespace dbaui
45 : {
46 : // .........................................................................
47 :
48 : using namespace ::com::sun::star::uno;
49 : using namespace ::com::sun::star::sdbc;
50 : using namespace ::com::sun::star::sdb;
51 : using namespace ::com::sun::star::sdb::application;
52 : using namespace ::com::sun::star::ui::dialogs;
53 : using namespace ::com::sun::star::frame;
54 : using namespace ::com::sun::star::lang;
55 : using namespace ::com::sun::star::beans;
56 : using namespace ::com::sun::star::awt;
57 :
58 : //======================================================================
59 : //= DatabaseObjectView
60 : //======================================================================
61 0 : DatabaseObjectView::DatabaseObjectView( const Reference< XMultiServiceFactory >& _rxORB,
62 : const Reference< XDatabaseDocumentUI >& _rxApplication,
63 : const Reference< XFrame >& _rxParentFrame,
64 : const ::rtl::OUString& _rComponentURL )
65 : :m_xORB ( _rxORB )
66 : ,m_xParentFrame ( _rxParentFrame )
67 : ,m_xFrameLoader ( )
68 : ,m_xApplication ( _rxApplication )
69 0 : ,m_sComponentURL ( _rComponentURL )
70 : {
71 : OSL_ENSURE( m_xORB.is(), "DatabaseObjectView::DatabaseObjectView: invalid service factory!" );
72 : OSL_ENSURE( m_xApplication.is(), "DatabaseObjectView::DatabaseObjectView: invalid connection!" );
73 0 : }
74 :
75 : //----------------------------------------------------------------------
76 0 : Reference< XConnection > DatabaseObjectView::getConnection() const
77 : {
78 0 : Reference< XConnection > xConnection;
79 0 : if ( m_xApplication.is() )
80 0 : xConnection = m_xApplication->getActiveConnection();
81 0 : return xConnection;
82 : }
83 :
84 : //----------------------------------------------------------------------
85 0 : Reference< XComponent > DatabaseObjectView::createNew( const Reference< XDataSource >& _xDataSource, const ::comphelper::NamedValueCollection& i_rDispatchArgs )
86 : {
87 0 : return doCreateView( makeAny( _xDataSource ), ::rtl::OUString(), i_rDispatchArgs );
88 : }
89 :
90 : //----------------------------------------------------------------------
91 0 : Reference< XComponent > DatabaseObjectView::openExisting( const Any& _rDataSource, const ::rtl::OUString& _rName,
92 : const ::comphelper::NamedValueCollection& i_rDispatchArgs )
93 : {
94 0 : return doCreateView( _rDataSource, _rName, i_rDispatchArgs );
95 : }
96 :
97 : //----------------------------------------------------------------------
98 0 : Reference< XComponent > DatabaseObjectView::doCreateView( const Any& _rDataSource, const ::rtl::OUString& _rObjectName,
99 : const ::comphelper::NamedValueCollection& i_rCreationArgs )
100 : {
101 0 : ::comphelper::NamedValueCollection aDispatchArgs;
102 :
103 0 : aDispatchArgs.merge( i_rCreationArgs, false ); // false => do not overwrite
104 0 : fillDispatchArgs( aDispatchArgs, _rDataSource, _rObjectName );
105 0 : aDispatchArgs.merge( i_rCreationArgs, true ); // true => do overwrite
106 :
107 0 : return doDispatch( aDispatchArgs );
108 : }
109 :
110 : //----------------------------------------------------------------------
111 0 : Reference< XComponent > DatabaseObjectView::doDispatch( const ::comphelper::NamedValueCollection& i_rDispatchArgs )
112 : {
113 0 : Reference< XComponent > xReturn;
114 0 : if ( m_xORB.is() )
115 : {
116 : try
117 : {
118 : // if we have no externally provided frame, create one
119 0 : if ( !m_xFrameLoader.is() )
120 : {
121 0 : Reference< XSingleServiceFactory > xFact(m_xORB->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.TaskCreator"))), UNO_QUERY_THROW);
122 0 : Sequence< Any > lArgs(2);
123 0 : NamedValue aProp;
124 0 : sal_Int32 nArg = 0;
125 :
126 0 : aProp.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ParentFrame"));
127 0 : aProp.Value <<= m_xParentFrame;
128 0 : lArgs[nArg++] <<= aProp;
129 :
130 0 : aProp.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TopWindow"));
131 0 : aProp.Value <<= sal_True;
132 0 : lArgs[nArg++] <<= aProp;
133 :
134 0 : m_xFrameLoader.set(xFact->createInstanceWithArguments(lArgs), UNO_QUERY_THROW);
135 :
136 : // everything we load can be considered a "top level document", so set the respective bit at the window.
137 : // This, amongst other things, triggers that the component in this task participates in the
138 : // "ThisComponent"-game for the global application Basic.
139 0 : const Reference< XFrame > xFrame( m_xFrameLoader, UNO_QUERY_THROW );
140 0 : const Reference< XWindow > xFrameWindow( xFrame->getContainerWindow(), UNO_SET_THROW );
141 0 : Window* pContainerWindow = VCLUnoHelper::GetWindow( xFrameWindow );
142 0 : ENSURE_OR_THROW( pContainerWindow, "no implementation access to the frame's container window!" );
143 0 : pContainerWindow->SetExtendedStyle( pContainerWindow->GetExtendedStyle() | WB_EXT_DOCUMENT );
144 : }
145 :
146 0 : Reference< XComponentLoader > xFrameLoader( m_xFrameLoader, UNO_QUERY_THROW );
147 0 : xReturn = xFrameLoader->loadComponentFromURL(
148 : m_sComponentURL,
149 : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_self")),
150 : 0,
151 : i_rDispatchArgs.getPropertyValues()
152 0 : );
153 : }
154 0 : catch( const Exception& )
155 : {
156 : DBG_UNHANDLED_EXCEPTION();
157 : }
158 : }
159 0 : return xReturn;
160 : }
161 :
162 : //----------------------------------------------------------------------
163 0 : void DatabaseObjectView::fillDispatchArgs(
164 : ::comphelper::NamedValueCollection& i_rDispatchArgs,
165 : const Any& _aDataSource,
166 : const ::rtl::OUString& /* _rName */
167 : )
168 : {
169 0 : ::rtl::OUString sDataSource;
170 0 : Reference<XDataSource> xDataSource;
171 0 : if ( _aDataSource >>= sDataSource )
172 : {
173 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_DATASOURCENAME, sDataSource );
174 : }
175 0 : else if ( _aDataSource >>= xDataSource )
176 : {
177 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_DATASOURCE, xDataSource );
178 : }
179 :
180 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_ACTIVE_CONNECTION, getConnection() );
181 0 : }
182 :
183 : //======================================================================
184 : //= QueryDesigner
185 : //======================================================================
186 : //----------------------------------------------------------------------
187 0 : QueryDesigner::QueryDesigner( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication,
188 : const Reference< XFrame >& _rxParentFrame, bool _bCreateView )
189 : :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, _bCreateView ? URL_COMPONENT_VIEWDESIGN : URL_COMPONENT_QUERYDESIGN )
190 0 : ,m_nCommandType( _bCreateView ? CommandType::TABLE : CommandType::QUERY )
191 : {
192 0 : }
193 :
194 : //----------------------------------------------------------------------
195 0 : void QueryDesigner::fillDispatchArgs( ::comphelper::NamedValueCollection& i_rDispatchArgs, const Any& _aDataSource,
196 : const ::rtl::OUString& _rObjectName )
197 : {
198 0 : DatabaseObjectView::fillDispatchArgs( i_rDispatchArgs, _aDataSource, _rObjectName );
199 :
200 0 : const bool bIncludeQueryName = !_rObjectName.isEmpty();
201 0 : const bool bGraphicalDesign = i_rDispatchArgs.getOrDefault( (::rtl::OUString)PROPERTY_GRAPHICAL_DESIGN, sal_True );
202 0 : const bool bEditViewAsSQLCommand = ( m_nCommandType == CommandType::TABLE ) && !bGraphicalDesign;
203 :
204 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_COMMAND_TYPE, m_nCommandType );
205 :
206 0 : if ( bIncludeQueryName )
207 : {
208 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_COMMAND, _rObjectName );
209 : }
210 :
211 0 : if ( bEditViewAsSQLCommand )
212 : {
213 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_ESCAPE_PROCESSING, sal_False );
214 : }
215 0 : }
216 :
217 : //======================================================================
218 : //= TableDesigner
219 : //======================================================================
220 : //----------------------------------------------------------------------
221 0 : TableDesigner::TableDesigner( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame )
222 0 : :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast< ::rtl::OUString >( URL_COMPONENT_TABLEDESIGN ) )
223 : {
224 0 : }
225 :
226 : //----------------------------------------------------------------------
227 0 : void TableDesigner::fillDispatchArgs( ::comphelper::NamedValueCollection& i_rDispatchArgs, const Any& _aDataSource,
228 : const ::rtl::OUString& _rObjectName )
229 : {
230 0 : DatabaseObjectView::fillDispatchArgs( i_rDispatchArgs, _aDataSource, _rObjectName );
231 :
232 0 : if ( !_rObjectName.isEmpty() )
233 : {
234 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_CURRENTTABLE, _rObjectName );
235 : }
236 0 : }
237 :
238 : //----------------------------------------------------------------------
239 0 : Reference< XComponent > TableDesigner::doCreateView( const Any& _rDataSource, const ::rtl::OUString& _rObjectName,
240 : const ::comphelper::NamedValueCollection& i_rCreationArgs )
241 : {
242 0 : bool bIsNewDesign = _rObjectName.isEmpty();
243 :
244 : // let's see whether the connection can provide a dedicated table desginer
245 0 : Reference< XInterface > xDesigner;
246 0 : if ( !bIsNewDesign )
247 0 : xDesigner = impl_getConnectionProvidedDesigner_nothrow( _rObjectName );
248 :
249 0 : if ( !xDesigner.is() )
250 0 : return DatabaseObjectView::doCreateView( _rDataSource, _rObjectName, i_rCreationArgs );
251 :
252 : // try whether the designer is a dialog
253 0 : Reference< XExecutableDialog > xDialog( xDesigner, UNO_QUERY_THROW );
254 0 : if ( xDialog.is() )
255 : {
256 0 : try { AsyncDialogExecutor::executeModalDialogAsync( xDialog ); }
257 0 : catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
258 0 : return NULL;
259 : }
260 :
261 0 : Reference< XComponent > xDesignerComponent( xDesigner, UNO_QUERY );
262 : OSL_ENSURE( xDesignerComponent.is(), "TableDesigner::doCreateView: a designer which is no dialog and no component?" );
263 0 : return xDesignerComponent;
264 : }
265 :
266 : //----------------------------------------------------------------------
267 0 : Reference< XInterface > TableDesigner::impl_getConnectionProvidedDesigner_nothrow( const ::rtl::OUString& _rTableName )
268 : {
269 0 : Reference< XInterface > xDesigner;
270 : try
271 : {
272 0 : Reference< XTableUIProvider > xTableUIProv( getConnection(), UNO_QUERY );
273 0 : if ( xTableUIProv.is() )
274 0 : xDesigner = xTableUIProv->getTableEditor( getApplicationUI(), _rTableName );
275 : }
276 0 : catch( const Exception& )
277 : {
278 : DBG_UNHANDLED_EXCEPTION();
279 : }
280 0 : return xDesigner;
281 : }
282 :
283 : //======================================================================
284 : //= ResultSetBrowser
285 : //======================================================================
286 : //----------------------------------------------------------------------
287 0 : ResultSetBrowser::ResultSetBrowser( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame,
288 : sal_Bool _bTable )
289 : :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast < ::rtl::OUString >( URL_COMPONENT_DATASOURCEBROWSER ) )
290 0 : ,m_bTable(_bTable)
291 : {
292 0 : }
293 :
294 : //----------------------------------------------------------------------
295 0 : void ResultSetBrowser::fillDispatchArgs( ::comphelper::NamedValueCollection& i_rDispatchArgs, const Any& _aDataSource,
296 : const ::rtl::OUString& _rQualifiedName)
297 : {
298 0 : DatabaseObjectView::fillDispatchArgs( i_rDispatchArgs, _aDataSource, _rQualifiedName );
299 : OSL_ENSURE( !_rQualifiedName.isEmpty(),"A Table name must be set");
300 0 : ::rtl::OUString sCatalog;
301 0 : ::rtl::OUString sSchema;
302 0 : ::rtl::OUString sTable;
303 0 : if ( m_bTable )
304 0 : ::dbtools::qualifiedNameComponents( getConnection()->getMetaData(), _rQualifiedName, sCatalog, sSchema, sTable, ::dbtools::eInDataManipulation );
305 :
306 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_COMMAND_TYPE, (m_bTable ? CommandType::TABLE : CommandType::QUERY) );
307 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_COMMAND, _rQualifiedName );
308 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_ENABLE_BROWSER, sal_False );
309 :
310 0 : if ( m_bTable )
311 : {
312 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_UPDATE_CATALOGNAME, sCatalog );
313 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_UPDATE_SCHEMANAME, sSchema );
314 0 : i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_UPDATE_TABLENAME, sTable );
315 0 : }
316 0 : }
317 :
318 : //======================================================================
319 : //= RelationDesigner
320 : //======================================================================
321 : //----------------------------------------------------------------------
322 0 : RelationDesigner::RelationDesigner( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame )
323 0 : :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast< ::rtl::OUString >( URL_COMPONENT_RELATIONDESIGN ) )
324 : {
325 0 : }
326 : // .........................................................................
327 : } // namespace dbaui
328 : // .........................................................................
329 :
330 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|