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 : #include "dbu_reghelper.hxx"
21 : #include "dbustrings.hrc"
22 : #include "UITools.hxx"
23 :
24 : #include <com/sun/star/container/XChild.hpp>
25 : #include <com/sun/star/container/XNameAccess.hpp>
26 : #include <com/sun/star/container/XSet.hpp>
27 : #include <com/sun/star/document/XEventListener.hpp>
28 : #include <com/sun/star/frame/XController2.hpp>
29 : #include <com/sun/star/frame/XFrame.hpp>
30 : #include <com/sun/star/frame/XFrameLoader.hpp>
31 : #include <com/sun/star/frame/XLoadEventListener.hpp>
32 : #include <com/sun/star/lang/XInitialization.hpp>
33 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 : #include <com/sun/star/lang/XServiceInfo.hpp>
35 : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
36 : #include <com/sun/star/registry/XRegistryKey.hpp>
37 : #include <com/sun/star/sdb/ReportDesign.hpp>
38 : #include <com/sun/star/sdbc/XConnection.hpp>
39 : #include <com/sun/star/frame/XModule.hpp>
40 :
41 : #include <com/sun/star/sdbc/XDataSource.hpp>
42 : #include <comphelper/namedvaluecollection.hxx>
43 : #include <comphelper/processfactory.hxx>
44 : #include <cppuhelper/implbase2.hxx>
45 : #include <cppuhelper/supportsservice.hxx>
46 : #include <toolkit/awt/vclxwindow.hxx>
47 : #include <toolkit/helper/vclunohelper.hxx>
48 : #include <tools/diagnose_ex.h>
49 : #include <tools/urlobj.hxx>
50 : #include <vcl/svapp.hxx>
51 :
52 : using namespace ::com::sun::star;
53 : using namespace ::com::sun::star::uno;
54 : using namespace ::com::sun::star::frame;
55 : using namespace ::com::sun::star::beans;
56 : using namespace ::com::sun::star::sdb;
57 : using namespace ::com::sun::star::sdbc;
58 : using namespace ::com::sun::star::container;
59 : using namespace ::com::sun::star::lang;
60 : using namespace ::com::sun::star::registry;
61 : using namespace dbaui;
62 :
63 : class DBContentLoader : public ::cppu::WeakImplHelper2< XFrameLoader, XServiceInfo>
64 : {
65 : private:
66 : OUString m_aURL;
67 : Sequence< PropertyValue> m_aArgs;
68 : Reference< XLoadEventListener > m_xListener;
69 : Reference< XFrame > m_xFrame;
70 : Reference< XComponentContext > m_xContext;
71 : public:
72 : DBContentLoader(const Reference< XComponentContext >&);
73 : virtual ~DBContentLoader();
74 :
75 : // XServiceInfo
76 : OUString SAL_CALL getImplementationName() throw(std::exception ) SAL_OVERRIDE;
77 : sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw(std::exception ) SAL_OVERRIDE;
78 : Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw(std::exception ) SAL_OVERRIDE;
79 :
80 : // static methods
81 0 : static OUString getImplementationName_Static() throw( )
82 : {
83 0 : return OUString("org.openoffice.comp.dbu.DBContentLoader");
84 : }
85 : static Sequence< OUString> getSupportedServiceNames_Static(void) throw( );
86 : static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
87 : SAL_CALL Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&);
88 :
89 : // XLoader
90 : virtual void SAL_CALL load( const Reference< XFrame > & _rFrame, const OUString& _rURL,
91 : const Sequence< PropertyValue >& _rArgs,
92 : const Reference< XLoadEventListener > & _rListener) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
93 : virtual void SAL_CALL cancel(void) throw(std::exception) SAL_OVERRIDE;
94 : };
95 :
96 :
97 0 : DBContentLoader::DBContentLoader(const Reference< XComponentContext >& _rxContext)
98 0 : :m_xContext(_rxContext)
99 : {
100 :
101 0 : }
102 :
103 0 : DBContentLoader::~DBContentLoader()
104 : {
105 :
106 0 : }
107 :
108 0 : extern "C" void SAL_CALL createRegistryInfo_DBContentLoader()
109 : {
110 0 : static ::dbaui::OMultiInstanceAutoRegistration< DBContentLoader > aAutoRegistration;
111 0 : }
112 :
113 0 : Reference< XInterface > SAL_CALL DBContentLoader::Create( const Reference< XMultiServiceFactory > & rSMgr )
114 : {
115 0 : return *(new DBContentLoader(comphelper::getComponentContext(rSMgr)));
116 : }
117 :
118 : // XServiceInfo
119 0 : OUString SAL_CALL DBContentLoader::getImplementationName() throw(std::exception )
120 : {
121 0 : return getImplementationName_Static();
122 : }
123 :
124 : // XServiceInfo
125 0 : sal_Bool SAL_CALL DBContentLoader::supportsService(const OUString& ServiceName) throw(std::exception )
126 : {
127 0 : return cppu::supportsService(this, ServiceName);
128 : }
129 :
130 : // XServiceInfo
131 0 : Sequence< OUString > SAL_CALL DBContentLoader::getSupportedServiceNames(void) throw(std::exception )
132 : {
133 0 : return getSupportedServiceNames_Static();
134 : }
135 :
136 : // ORegistryServiceManager_Static
137 0 : Sequence< OUString > DBContentLoader::getSupportedServiceNames_Static(void) throw( )
138 : {
139 0 : Sequence< OUString > aSNS( 2 );
140 0 : aSNS[0] = "com.sun.star.frame.FrameLoader";
141 0 : aSNS[1] = "com.sun.star.sdb.ContentLoader";
142 0 : return aSNS;
143 : }
144 :
145 0 : void SAL_CALL DBContentLoader::load(const Reference< XFrame > & rFrame, const OUString& rURL,
146 : const Sequence< PropertyValue >& rArgs,
147 : const Reference< XLoadEventListener > & rListener) throw(::com::sun::star::uno::RuntimeException, std::exception)
148 : {
149 0 : m_xFrame = rFrame;
150 0 : m_xListener = rListener;
151 0 : m_aURL = rURL;
152 0 : m_aArgs = rArgs;
153 :
154 : struct ServiceNameToImplName
155 : {
156 : const sal_Char* pAsciiServiceName;
157 : const sal_Char* pAsciiImplementationName;
158 0 : ServiceNameToImplName( const sal_Char* _pService, const sal_Char* _pImpl )
159 : :pAsciiServiceName( _pService )
160 0 : ,pAsciiImplementationName( _pImpl )
161 : {
162 0 : }
163 : } aImplementations[] = {
164 : ServiceNameToImplName( URL_COMPONENT_FORMGRIDVIEW, "org.openoffice.comp.dbu.OFormGridView" ),
165 : ServiceNameToImplName( URL_COMPONENT_DATASOURCEBROWSER, "org.openoffice.comp.dbu.ODatasourceBrowser" ),
166 : ServiceNameToImplName( URL_COMPONENT_QUERYDESIGN, "org.openoffice.comp.dbu.OQueryDesign" ),
167 : ServiceNameToImplName( URL_COMPONENT_TABLEDESIGN, "org.openoffice.comp.dbu.OTableDesign" ),
168 : ServiceNameToImplName( URL_COMPONENT_RELATIONDESIGN, "org.openoffice.comp.dbu.ORelationDesign" ),
169 : ServiceNameToImplName( URL_COMPONENT_VIEWDESIGN, "org.openoffice.comp.dbu.OViewDesign" )
170 0 : };
171 :
172 0 : INetURLObject aParser( rURL );
173 0 : Reference< XController2 > xController;
174 :
175 0 : const OUString sComponentURL( aParser.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
176 0 : for ( size_t i=0; i < sizeof( aImplementations ) / sizeof( aImplementations[0] ); ++i )
177 : {
178 0 : if ( sComponentURL.equalsAscii( aImplementations[i].pAsciiServiceName ) )
179 : {
180 0 : xController.set( m_xContext->getServiceManager()->
181 0 : createInstanceWithContext( OUString::createFromAscii( aImplementations[i].pAsciiImplementationName ), m_xContext), UNO_QUERY_THROW );
182 0 : break;
183 : }
184 : }
185 :
186 : // if a data source browser is loaded without its tree pane, then we assume it to be a
187 : // table data view, effectively. In this case, we need to adjust the module identifier.
188 : // #i85879#
189 0 : ::comphelper::NamedValueCollection aLoadArgs( rArgs );
190 :
191 0 : if ( sComponentURL == URL_COMPONENT_DATASOURCEBROWSER )
192 : {
193 0 : sal_Bool bDisableBrowser = ( sal_False == aLoadArgs.getOrDefault( "ShowTreeViewButton", sal_True ) ) // compatibility name
194 0 : || ( sal_False == aLoadArgs.getOrDefault( (OUString)PROPERTY_ENABLE_BROWSER, sal_True ) );
195 :
196 0 : if ( bDisableBrowser )
197 : {
198 : try
199 : {
200 0 : Reference< XModule > xModule( xController, UNO_QUERY_THROW );
201 0 : xModule->setIdentifier( OUString( "com.sun.star.sdb.TableDataView" ) );
202 : }
203 0 : catch( const Exception& )
204 : {
205 : DBG_UNHANDLED_EXCEPTION();
206 : }
207 : }
208 : }
209 :
210 0 : if ( sComponentURL == URL_COMPONENT_REPORTDESIGN )
211 : {
212 0 : sal_Bool bPreview = aLoadArgs.getOrDefault( "Preview", sal_False );
213 0 : if ( bPreview )
214 : { // report designs cannot be previewed
215 0 : if ( rListener.is() )
216 0 : rListener->loadCancelled( this );
217 0 : return;
218 : }
219 0 : Reference< XModel > xReportModel( aLoadArgs.getOrDefault( "Model", Reference< XModel >() ) );
220 0 : if ( xReportModel.is() )
221 : {
222 0 : xController.set( ReportDesign::create( m_xContext ) );
223 0 : xController->attachModel( xReportModel );
224 0 : xReportModel->connectController( xController.get() );
225 0 : xReportModel->setCurrentController( xController.get() );
226 0 : }
227 : }
228 :
229 0 : sal_Bool bSuccess = xController.is();
230 0 : Reference< XModel > xDatabaseDocument;
231 0 : if ( bSuccess )
232 : {
233 0 : Reference< XDataSource > xDataSource ( aLoadArgs.getOrDefault( "DataSource", Reference< XDataSource >() ) );
234 0 : OUString sDataSourceName( aLoadArgs.getOrDefault( "DataSourceName", OUString() ) );
235 0 : Reference< XConnection > xConnection ( aLoadArgs.getOrDefault( "ActiveConnection", Reference< XConnection >() ) );
236 0 : if ( xDataSource.is() )
237 : {
238 0 : xDatabaseDocument.set( getDataSourceOrModel( xDataSource ), UNO_QUERY );
239 : }
240 0 : else if ( !sDataSourceName.isEmpty() )
241 : {
242 0 : ::dbtools::SQLExceptionInfo aError;
243 0 : xDataSource.set( getDataSourceByName( sDataSourceName, NULL, m_xContext, &aError ) );
244 0 : xDatabaseDocument.set( getDataSourceOrModel( xDataSource ), UNO_QUERY );
245 : }
246 0 : else if ( xConnection.is() )
247 : {
248 0 : Reference< XChild > xAsChild( xConnection, UNO_QUERY );
249 0 : if ( xAsChild.is() )
250 : {
251 : OSL_ENSURE( Reference< XDataSource >( xAsChild->getParent(), UNO_QUERY ).is(),
252 : "DBContentLoader::load: a connection whose parent is no data source?" );
253 0 : xDatabaseDocument.set( getDataSourceOrModel( xAsChild->getParent() ), UNO_QUERY );
254 0 : }
255 : }
256 :
257 : // init controller
258 0 : SolarMutexGuard aGuard;
259 : try
260 : {
261 0 : Reference<XInitialization > xIni(xController,UNO_QUERY);
262 0 : PropertyValue aFrame(OUString("Frame"),0,makeAny(rFrame),PropertyState_DIRECT_VALUE);
263 0 : Sequence< Any > aInitArgs(m_aArgs.getLength()+1);
264 :
265 0 : Any* pBegin = aInitArgs.getArray();
266 0 : Any* pEnd = pBegin + aInitArgs.getLength();
267 0 : *pBegin <<= aFrame;
268 0 : const PropertyValue* pIter = m_aArgs.getConstArray();
269 0 : for(++pBegin;pBegin != pEnd;++pBegin,++pIter)
270 : {
271 0 : *pBegin <<= *pIter;
272 : }
273 :
274 0 : xIni->initialize(aInitArgs);
275 : }
276 0 : catch(const Exception&)
277 : {
278 : // Does this need to be shown to the user?
279 0 : bSuccess = false;
280 : try
281 : {
282 0 : ::comphelper::disposeComponent( xController );
283 : }
284 0 : catch( const Exception& )
285 : {
286 : DBG_UNHANDLED_EXCEPTION();
287 : }
288 0 : }
289 : }
290 :
291 : // assign controller and frame
292 0 : if ( bSuccess )
293 : {
294 0 : if ( xController.is() && rFrame.is() )
295 : {
296 0 : rFrame->setComponent( xController->getComponentWindow(), xController.get() );
297 0 : xController->attachFrame(rFrame);
298 : }
299 :
300 0 : if ( rListener.is() )
301 0 : rListener->loadFinished( this );
302 : }
303 : else
304 0 : if ( rListener.is() )
305 0 : rListener->loadCancelled( this );
306 : }
307 :
308 0 : void DBContentLoader::cancel(void) throw(std::exception)
309 : {
310 0 : }
311 :
312 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|