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 .
20 #include "dbu_reghelper.hxx"
21 #include "dbustrings.hrc"
22 #include "uiservices.hxx"
23 #include "UITools.hxx"
25 #include <com/sun/star/container/XChild.hpp>
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <com/sun/star/container/XSet.hpp>
28 #include <com/sun/star/document/XEventListener.hpp>
29 #include <com/sun/star/frame/XController2.hpp>
30 #include <com/sun/star/frame/XFrame.hpp>
31 #include <com/sun/star/frame/XFrameLoader.hpp>
32 #include <com/sun/star/frame/XLoadEventListener.hpp>
33 #include <com/sun/star/lang/XInitialization.hpp>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
37 #include <com/sun/star/registry/XRegistryKey.hpp>
38 #include <com/sun/star/sdb/ReportDesign.hpp>
39 #include <com/sun/star/sdbc/XConnection.hpp>
40 #include <com/sun/star/frame/XModule.hpp>
42 #include <com/sun/star/sdbc/XDataSource.hpp>
43 #include <comphelper/namedvaluecollection.hxx>
44 #include <comphelper/processfactory.hxx>
45 #include <cppuhelper/implbase2.hxx>
46 #include <cppuhelper/supportsservice.hxx>
47 #include <toolkit/awt/vclxwindow.hxx>
48 #include <toolkit/helper/vclunohelper.hxx>
49 #include <tools/diagnose_ex.h>
50 #include <tools/urlobj.hxx>
51 #include <vcl/svapp.hxx>
53 using namespace ::com::sun::star
;
54 using namespace ::com::sun::star::uno
;
55 using namespace ::com::sun::star::frame
;
56 using namespace ::com::sun::star::beans
;
57 using namespace ::com::sun::star::sdb
;
58 using namespace ::com::sun::star::sdbc
;
59 using namespace ::com::sun::star::container
;
60 using namespace ::com::sun::star::lang
;
61 using namespace ::com::sun::star::registry
;
62 using namespace dbaui
;
64 class DBContentLoader
: public ::cppu::WeakImplHelper2
< XFrameLoader
, XServiceInfo
>
68 Sequence
< PropertyValue
> m_aArgs
;
69 Reference
< XLoadEventListener
> m_xListener
;
70 Reference
< XFrame
> m_xFrame
;
71 Reference
< XComponentContext
> m_xContext
;
73 DBContentLoader(const Reference
< XComponentContext
>&);
74 virtual ~DBContentLoader();
77 OUString SAL_CALL
getImplementationName() throw(std::exception
) SAL_OVERRIDE
;
78 sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) throw(std::exception
) SAL_OVERRIDE
;
79 Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw(std::exception
) SAL_OVERRIDE
;
82 static OUString
getImplementationName_Static() throw( )
84 return OUString("org.openoffice.comp.dbu.DBContentLoader");
86 static Sequence
< OUString
> getSupportedServiceNames_Static() throw( );
87 static ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>
88 SAL_CALL
Create(const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>&);
91 virtual void SAL_CALL
load( const Reference
< XFrame
> & _rFrame
, const OUString
& _rURL
,
92 const Sequence
< PropertyValue
>& _rArgs
,
93 const Reference
< XLoadEventListener
> & _rListener
) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
94 virtual void SAL_CALL
cancel() throw(std::exception
) SAL_OVERRIDE
;
98 DBContentLoader::DBContentLoader(const Reference
< XComponentContext
>& _rxContext
)
99 :m_xContext(_rxContext
)
104 DBContentLoader::~DBContentLoader()
109 extern "C" void SAL_CALL
createRegistryInfo_DBContentLoader()
111 static ::dbaui::OMultiInstanceAutoRegistration
< DBContentLoader
> aAutoRegistration
;
114 Reference
< XInterface
> SAL_CALL
DBContentLoader::Create( const Reference
< XMultiServiceFactory
> & rSMgr
)
116 return *(new DBContentLoader(comphelper::getComponentContext(rSMgr
)));
120 OUString SAL_CALL
DBContentLoader::getImplementationName() throw(std::exception
)
122 return getImplementationName_Static();
126 sal_Bool SAL_CALL
DBContentLoader::supportsService(const OUString
& ServiceName
) throw(std::exception
)
128 return cppu::supportsService(this, ServiceName
);
132 Sequence
< OUString
> SAL_CALL
DBContentLoader::getSupportedServiceNames() throw(std::exception
)
134 return getSupportedServiceNames_Static();
137 // ORegistryServiceManager_Static
138 Sequence
< OUString
> DBContentLoader::getSupportedServiceNames_Static() throw( )
140 Sequence
< OUString
> aSNS( 2 );
141 aSNS
[0] = "com.sun.star.frame.FrameLoader";
142 aSNS
[1] = "com.sun.star.sdb.ContentLoader";
146 void SAL_CALL
DBContentLoader::load(const Reference
< XFrame
> & rFrame
, const OUString
& rURL
,
147 const Sequence
< PropertyValue
>& rArgs
,
148 const Reference
< XLoadEventListener
> & rListener
) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
151 m_xListener
= rListener
;
155 struct ServiceNameToImplName
157 const sal_Char
* pAsciiServiceName
;
158 const sal_Char
* pAsciiImplementationName
;
159 ServiceNameToImplName( const sal_Char
* _pService
, const sal_Char
* _pImpl
)
160 :pAsciiServiceName( _pService
)
161 ,pAsciiImplementationName( _pImpl
)
164 } aImplementations
[] = {
165 ServiceNameToImplName( URL_COMPONENT_FORMGRIDVIEW
, "org.openoffice.comp.dbu.OFormGridView" ),
166 ServiceNameToImplName( URL_COMPONENT_DATASOURCEBROWSER
, "org.openoffice.comp.dbu.ODatasourceBrowser" ),
167 ServiceNameToImplName( URL_COMPONENT_QUERYDESIGN
, "org.openoffice.comp.dbu.OQueryDesign" ),
168 ServiceNameToImplName( URL_COMPONENT_TABLEDESIGN
, "org.openoffice.comp.dbu.OTableDesign" ),
169 ServiceNameToImplName( URL_COMPONENT_RELATIONDESIGN
, "org.openoffice.comp.dbu.ORelationDesign" ),
170 ServiceNameToImplName( URL_COMPONENT_VIEWDESIGN
, "org.openoffice.comp.dbu.OViewDesign" )
173 INetURLObject
aParser( rURL
);
174 Reference
< XController2
> xController
;
176 const OUString
sComponentURL( aParser
.GetMainURL( INetURLObject::DECODE_TO_IURI
) );
177 for ( size_t i
=0; i
< sizeof( aImplementations
) / sizeof( aImplementations
[0] ); ++i
)
179 if ( sComponentURL
.equalsAscii( aImplementations
[i
].pAsciiServiceName
) )
181 xController
.set( m_xContext
->getServiceManager()->
182 createInstanceWithContext( OUString::createFromAscii( aImplementations
[i
].pAsciiImplementationName
), m_xContext
), UNO_QUERY_THROW
);
187 // if a data source browser is loaded without its tree pane, then we assume it to be a
188 // table data view, effectively. In this case, we need to adjust the module identifier.
190 ::comphelper::NamedValueCollection
aLoadArgs( rArgs
);
192 if ( sComponentURL
== URL_COMPONENT_DATASOURCEBROWSER
)
194 bool bDisableBrowser
= !aLoadArgs
.getOrDefault( "ShowTreeViewButton", sal_True
) // compatibility name
195 || !aLoadArgs
.getOrDefault( OUString(PROPERTY_ENABLE_BROWSER
), sal_True
);
197 if ( bDisableBrowser
)
201 Reference
< XModule
> xModule( xController
, UNO_QUERY_THROW
);
202 xModule
->setIdentifier( OUString( "com.sun.star.sdb.TableDataView" ) );
204 catch( const Exception
& )
206 DBG_UNHANDLED_EXCEPTION();
211 if ( sComponentURL
== URL_COMPONENT_REPORTDESIGN
)
213 bool bPreview
= aLoadArgs
.getOrDefault( "Preview", sal_False
);
215 { // report designs cannot be previewed
216 if ( rListener
.is() )
217 rListener
->loadCancelled( this );
220 Reference
< XModel
> xReportModel( aLoadArgs
.getOrDefault( "Model", Reference
< XModel
>() ) );
221 if ( xReportModel
.is() )
223 xController
.set( ReportDesign::create( m_xContext
) );
224 xController
->attachModel( xReportModel
);
225 xReportModel
->connectController( xController
.get() );
226 xReportModel
->setCurrentController( xController
.get() );
230 bool bSuccess
= xController
.is();
231 Reference
< XModel
> xDatabaseDocument
;
234 Reference
< XDataSource
> xDataSource ( aLoadArgs
.getOrDefault( "DataSource", Reference
< XDataSource
>() ) );
235 OUString
sDataSourceName( aLoadArgs
.getOrDefault( "DataSourceName", OUString() ) );
236 Reference
< XConnection
> xConnection ( aLoadArgs
.getOrDefault( "ActiveConnection", Reference
< XConnection
>() ) );
237 if ( xDataSource
.is() )
239 xDatabaseDocument
.set( getDataSourceOrModel( xDataSource
), UNO_QUERY
);
241 else if ( !sDataSourceName
.isEmpty() )
243 ::dbtools::SQLExceptionInfo aError
;
244 xDataSource
.set( getDataSourceByName( sDataSourceName
, NULL
, m_xContext
, &aError
) );
245 xDatabaseDocument
.set( getDataSourceOrModel( xDataSource
), UNO_QUERY
);
247 else if ( xConnection
.is() )
249 Reference
< XChild
> xAsChild( xConnection
, UNO_QUERY
);
252 OSL_ENSURE( Reference
< XDataSource
>( xAsChild
->getParent(), UNO_QUERY
).is(),
253 "DBContentLoader::load: a connection whose parent is no data source?" );
254 xDatabaseDocument
.set( getDataSourceOrModel( xAsChild
->getParent() ), UNO_QUERY
);
259 SolarMutexGuard aGuard
;
262 Reference
<XInitialization
> xIni(xController
,UNO_QUERY
);
263 PropertyValue
aFrame(OUString("Frame"),0,makeAny(rFrame
),PropertyState_DIRECT_VALUE
);
264 Sequence
< Any
> aInitArgs(m_aArgs
.getLength()+1);
266 Any
* pBegin
= aInitArgs
.getArray();
267 Any
* pEnd
= pBegin
+ aInitArgs
.getLength();
269 const PropertyValue
* pIter
= m_aArgs
.getConstArray();
270 for(++pBegin
;pBegin
!= pEnd
;++pBegin
,++pIter
)
275 xIni
->initialize(aInitArgs
);
277 catch(const Exception
&)
279 // Does this need to be shown to the user?
283 ::comphelper::disposeComponent( xController
);
285 catch( const Exception
& )
287 DBG_UNHANDLED_EXCEPTION();
292 // assign controller and frame
295 if ( xController
.is() && rFrame
.is() )
297 rFrame
->setComponent( xController
->getComponentWindow(), xController
.get() );
298 xController
->attachFrame(rFrame
);
301 if ( rListener
.is() )
302 rListener
->loadFinished( this );
305 if ( rListener
.is() )
306 rListener
->loadCancelled( this );
309 void DBContentLoader::cancel() throw(std::exception
)
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */