merge the formfield patch from ooo-build
[ooovba.git] / dbaccess / source / ui / browser / dbloader.cxx
blob4448d5b632d7c020b100750dfa38c799cd16e99f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dbloader.cxx,v $
10 * $Revision: 1.36.2.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #include "dbu_reghelper.hxx"
35 #include "dbustrings.hrc"
36 #include "UITools.hxx"
38 /** === begin UNO includes === **/
39 #include <com/sun/star/container/XChild.hpp>
40 #include <com/sun/star/container/XNameAccess.hpp>
41 #include <com/sun/star/container/XSet.hpp>
42 #include <com/sun/star/document/XEventListener.hpp>
43 #include <com/sun/star/frame/XController2.hpp>
44 #include <com/sun/star/frame/XFrame.hpp>
45 #include <com/sun/star/frame/XFrameLoader.hpp>
46 #include <com/sun/star/frame/XLoadEventListener.hpp>
47 #include <com/sun/star/lang/XInitialization.hpp>
48 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
49 #include <com/sun/star/lang/XServiceInfo.hpp>
50 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
51 #include <com/sun/star/registry/XRegistryKey.hpp>
52 #include <com/sun/star/sdbc/XConnection.hpp>
53 #include <com/sun/star/frame/XModule.hpp>
54 /** === end UNO includes === **/
56 #include <com/sun/star/sdbc/XDataSource.hpp>
57 #include <comphelper/namedvaluecollection.hxx>
58 #include <comphelper/componentcontext.hxx>
59 #include <cppuhelper/implbase2.hxx>
60 #include <toolkit/awt/vclxwindow.hxx>
61 #include <toolkit/helper/vclunohelper.hxx>
62 #include <tools/diagnose_ex.h>
63 #include <tools/urlobj.hxx>
64 #include <vcl/svapp.hxx>
66 using namespace ::com::sun::star;
67 using namespace ::com::sun::star::uno;
68 using namespace ::com::sun::star::frame;
69 using namespace ::com::sun::star::beans;
70 using namespace ::com::sun::star::sdbc;
71 using namespace ::com::sun::star::container;
72 using namespace ::com::sun::star::lang;
73 using namespace ::com::sun::star::registry;
74 using ::com::sun::star::sdbc::XDataSource;
75 using namespace dbaui;
77 class DBContentLoader : public ::cppu::WeakImplHelper2< XFrameLoader, XServiceInfo>
79 private:
80 ::rtl::OUString m_aURL;
81 Sequence< PropertyValue> m_aArgs;
82 Reference< XLoadEventListener > m_xListener;
83 Reference< XFrame > m_xFrame;
84 Reference< XMultiServiceFactory > m_xServiceFactory;
85 public:
86 DBContentLoader(const Reference< XMultiServiceFactory >&);
87 ~DBContentLoader();
89 // XServiceInfo
90 ::rtl::OUString SAL_CALL getImplementationName() throw( );
91 sal_Bool SAL_CALL supportsService(const ::rtl::OUString& ServiceName) throw( );
92 Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(void) throw( );
94 // static methods
95 static ::rtl::OUString getImplementationName_Static() throw( )
97 return ::rtl::OUString::createFromAscii("org.openoffice.comp.dbu.DBContentLoader");
99 static Sequence< ::rtl::OUString> getSupportedServiceNames_Static(void) throw( );
100 static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
101 SAL_CALL Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&);
103 // XLoader
104 virtual void SAL_CALL load( const Reference< XFrame > & _rFrame, const ::rtl::OUString& _rURL,
105 const Sequence< PropertyValue >& _rArgs,
106 const Reference< XLoadEventListener > & _rListener) throw(::com::sun::star::uno::RuntimeException);
107 virtual void SAL_CALL cancel(void) throw();
109 DBG_NAME(DBContentLoader)
111 DBContentLoader::DBContentLoader(const Reference< XMultiServiceFactory >& _rxFactory)
112 :m_xServiceFactory(_rxFactory)
114 DBG_CTOR(DBContentLoader,NULL);
117 // -------------------------------------------------------------------------
119 DBContentLoader::~DBContentLoader()
122 DBG_DTOR(DBContentLoader,NULL);
124 // -------------------------------------------------------------------------
125 // -------------------------------------------------------------------------
126 extern "C" void SAL_CALL createRegistryInfo_DBContentLoader()
128 static ::dbaui::OMultiInstanceAutoRegistration< DBContentLoader > aAutoRegistration;
130 // -------------------------------------------------------------------------
131 Reference< XInterface > SAL_CALL DBContentLoader::Create( const Reference< XMultiServiceFactory > & rSMgr )
133 return *(new DBContentLoader(rSMgr));
135 // -------------------------------------------------------------------------
136 // XServiceInfo
137 ::rtl::OUString SAL_CALL DBContentLoader::getImplementationName() throw( )
139 return getImplementationName_Static();
141 // -------------------------------------------------------------------------
143 // XServiceInfo
144 sal_Bool SAL_CALL DBContentLoader::supportsService(const ::rtl::OUString& ServiceName) throw( )
146 Sequence< ::rtl::OUString > aSNL = getSupportedServiceNames();
147 const ::rtl::OUString * pBegin = aSNL.getConstArray();
148 const ::rtl::OUString * pEnd = pBegin + aSNL.getLength();
149 for( ; pBegin != pEnd; ++pBegin)
150 if( *pBegin == ServiceName )
151 return sal_True;
152 return sal_False;
154 // -------------------------------------------------------------------------
155 // XServiceInfo
156 Sequence< ::rtl::OUString > SAL_CALL DBContentLoader::getSupportedServiceNames(void) throw( )
158 return getSupportedServiceNames_Static();
160 // -------------------------------------------------------------------------
161 // ORegistryServiceManager_Static
162 Sequence< ::rtl::OUString > DBContentLoader::getSupportedServiceNames_Static(void) throw( )
164 Sequence< ::rtl::OUString > aSNS( 2 );
165 aSNS.getArray()[0] = ::rtl::OUString::createFromAscii("com.sun.star.frame.FrameLoader");
166 aSNS.getArray()[1] = ::rtl::OUString::createFromAscii("com.sun.star.sdb.ContentLoader");
167 return aSNS;
169 // -------------------------------------------------------------------------
170 extern "C" void SAL_CALL writeDBLoaderInfo(void* pRegistryKey)
172 Reference< XRegistryKey> xKey(reinterpret_cast< XRegistryKey*>(pRegistryKey));
174 // register content loader for dispatch
175 ::rtl::OUString aImpl = ::rtl::OUString::createFromAscii("/");
176 aImpl += DBContentLoader::getImplementationName_Static();
178 ::rtl::OUString aImpltwo = aImpl;
179 aImpltwo += ::rtl::OUString::createFromAscii("/UNO/Loader");
180 Reference< XRegistryKey> xNewKey = xKey->createKey( aImpltwo );
181 aImpltwo = aImpl;
182 aImpltwo += ::rtl::OUString::createFromAscii("/Loader");
183 Reference< XRegistryKey > xLoaderKey = xKey->createKey( aImpltwo );
184 xNewKey = xLoaderKey->createKey( ::rtl::OUString::createFromAscii("Pattern") );
185 xNewKey->setAsciiValue( ::rtl::OUString::createFromAscii(".component:DB*") );
188 // -----------------------------------------------------------------------
189 void SAL_CALL DBContentLoader::load(const Reference< XFrame > & rFrame, const ::rtl::OUString& rURL,
190 const Sequence< PropertyValue >& rArgs,
191 const Reference< XLoadEventListener > & rListener) throw(::com::sun::star::uno::RuntimeException)
193 m_xFrame = rFrame;
194 m_xListener = rListener;
195 m_aURL = rURL;
196 m_aArgs = rArgs;
198 ::comphelper::ComponentContext aContext( m_xServiceFactory );
200 struct ServiceNameToImplName
202 const sal_Char* pAsciiServiceName;
203 const sal_Char* pAsciiImplementationName;
204 ServiceNameToImplName( const sal_Char* _pService, const sal_Char* _pImpl )
205 :pAsciiServiceName( _pService )
206 ,pAsciiImplementationName( _pImpl )
209 } aImplementations[] = {
210 ServiceNameToImplName( URL_COMPONENT_FORMGRIDVIEW, "org.openoffice.comp.dbu.OFormGridView" ),
211 ServiceNameToImplName( URL_COMPONENT_DATASOURCEBROWSER, "org.openoffice.comp.dbu.ODatasourceBrowser" ),
212 ServiceNameToImplName( URL_COMPONENT_QUERYDESIGN, "org.openoffice.comp.dbu.OQueryDesign" ),
213 ServiceNameToImplName( URL_COMPONENT_TABLEDESIGN, "org.openoffice.comp.dbu.OTableDesign" ),
214 ServiceNameToImplName( URL_COMPONENT_RELATIONDESIGN, "org.openoffice.comp.dbu.ORelationDesign" ),
215 ServiceNameToImplName( URL_COMPONENT_VIEWDESIGN, "org.openoffice.comp.dbu.OViewDesign" )
218 INetURLObject aParser( rURL );
219 Reference< XController2 > xController;
221 const ::rtl::OUString sComponentURL( aParser.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
222 for ( size_t i=0; i < sizeof( aImplementations ) / sizeof( aImplementations[0] ); ++i )
224 if ( sComponentURL.equalsAscii( aImplementations[i].pAsciiServiceName ) )
226 aContext.createComponent( aImplementations[i].pAsciiImplementationName, xController );
227 break;
231 // if a data source browser is loaded without its tree pane, then we assume it to be a
232 // table data view, effectively. In this case, we need to adjust the module identifier.
233 // 2008-02-05 / i85879 / frank.schoenheit@sun.com
234 ::comphelper::NamedValueCollection aLoadArgs( rArgs );
236 if ( sComponentURL == URL_COMPONENT_DATASOURCEBROWSER )
238 sal_Bool bDisableBrowser = ( sal_False == aLoadArgs.getOrDefault( "ShowTreeViewButton", sal_True ) ) // compatibility name
239 || ( sal_False == aLoadArgs.getOrDefault( (::rtl::OUString)PROPERTY_ENABLE_BROWSER, sal_True ) );
241 if ( bDisableBrowser )
245 Reference< XModule > xModule( xController, UNO_QUERY_THROW );
246 xModule->setIdentifier( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.TableDataView" ) ) );
248 catch( const Exception& )
250 DBG_UNHANDLED_EXCEPTION();
255 if ( sComponentURL == URL_COMPONENT_REPORTDESIGN )
257 sal_Bool bPreview = aLoadArgs.getOrDefault( "Preview", sal_False );
258 if ( bPreview )
259 { // report designs cannot be previewed
260 if ( rListener.is() )
261 rListener->loadCancelled( this );
262 return;
264 Reference< XModel > xReportModel( aLoadArgs.getOrDefault( "Model", Reference< XModel >() ) );
265 if ( xReportModel.is() )
267 xController.set( m_xServiceFactory->createInstance(
268 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.ReportDesign" ) ) ), UNO_QUERY );
269 if ( xController.is() )
271 xController->attachModel( xReportModel );
272 xReportModel->connectController( xController.get() );
273 xReportModel->setCurrentController( xController.get() );
278 sal_Bool bSuccess = xController.is();
279 Reference< XModel > xDatabaseDocument;
280 if ( bSuccess )
282 Reference< XDataSource > xDataSource ( aLoadArgs.getOrDefault( "DataSource", Reference< XDataSource >() ) );
283 ::rtl::OUString sDataSourceName( aLoadArgs.getOrDefault( "DataSourceName", ::rtl::OUString() ) );
284 Reference< XConnection > xConnection ( aLoadArgs.getOrDefault( "ActiveConnection", Reference< XConnection >() ) );
285 if ( xDataSource.is() )
287 xDatabaseDocument.set( getDataSourceOrModel( xDataSource ), UNO_QUERY );
289 else if ( sDataSourceName.getLength() )
291 ::dbtools::SQLExceptionInfo aError;
292 xDataSource.set( getDataSourceByName( sDataSourceName, NULL, m_xServiceFactory, &aError ) );
293 xDatabaseDocument.set( getDataSourceOrModel( xDataSource ), UNO_QUERY );
295 else if ( xConnection.is() )
297 Reference< XChild > xAsChild( xConnection, UNO_QUERY );
298 if ( xAsChild.is() )
300 OSL_ENSURE( Reference< XDataSource >( xAsChild->getParent(), UNO_QUERY ).is(),
301 "DBContentLoader::load: a connection whose parent is no data source?" );
302 xDatabaseDocument.set( getDataSourceOrModel( xAsChild->getParent() ), UNO_QUERY );
306 // init controller
307 ::vos::OGuard aGuard(Application::GetSolarMutex());
310 Reference<XInitialization > xIni(xController,UNO_QUERY);
311 PropertyValue aFrame(::rtl::OUString::createFromAscii("Frame"),0,makeAny(rFrame),PropertyState_DIRECT_VALUE);
312 Sequence< Any > aInitArgs(m_aArgs.getLength()+1);
314 Any* pBegin = aInitArgs.getArray();
315 Any* pEnd = pBegin + aInitArgs.getLength();
316 *pBegin <<= aFrame;
317 const PropertyValue* pIter = m_aArgs.getConstArray();
318 for(++pBegin;pBegin != pEnd;++pBegin,++pIter)
320 *pBegin <<= *pIter;
323 xIni->initialize(aInitArgs);
325 catch(const Exception&)
327 DBG_UNHANDLED_EXCEPTION();
331 // assign controller and frame
332 if ( bSuccess )
334 if ( xController.is() && rFrame.is() )
336 rFrame->setComponent( xController->getComponentWindow(), xController.get() );
337 xController->attachFrame(rFrame);
340 if ( rListener.is() )
341 rListener->loadFinished( this );
343 else
344 if ( rListener.is() )
345 rListener->loadCancelled( this );
348 // -----------------------------------------------------------------------
349 void DBContentLoader::cancel(void) throw()