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 "dbu_reghelper.hxx"
22 : #include "dbustrings.hrc"
23 : #include "UITools.hxx"
24 :
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>
41 :
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 <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 : ~DBContentLoader();
74 :
75 : // XServiceInfo
76 : OUString SAL_CALL getImplementationName() throw( );
77 : sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw( );
78 : Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw( );
79 :
80 : // static methods
81 9 : static OUString getImplementationName_Static() throw( )
82 : {
83 9 : 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);
93 : virtual void SAL_CALL cancel(void) throw();
94 : };
95 : DBG_NAME(DBContentLoader)
96 :
97 3 : DBContentLoader::DBContentLoader(const Reference< XComponentContext >& _rxContext)
98 3 : :m_xContext(_rxContext)
99 : {
100 : DBG_CTOR(DBContentLoader,NULL);
101 :
102 3 : }
103 : // -------------------------------------------------------------------------
104 :
105 6 : DBContentLoader::~DBContentLoader()
106 : {
107 :
108 : DBG_DTOR(DBContentLoader,NULL);
109 6 : }
110 : // -------------------------------------------------------------------------
111 : // -------------------------------------------------------------------------
112 4 : extern "C" void SAL_CALL createRegistryInfo_DBContentLoader()
113 : {
114 4 : static ::dbaui::OMultiInstanceAutoRegistration< DBContentLoader > aAutoRegistration;
115 4 : }
116 : // -------------------------------------------------------------------------
117 3 : Reference< XInterface > SAL_CALL DBContentLoader::Create( const Reference< XMultiServiceFactory > & rSMgr )
118 : {
119 3 : return *(new DBContentLoader(comphelper::getComponentContext(rSMgr)));
120 : }
121 : // -------------------------------------------------------------------------
122 : // XServiceInfo
123 1 : OUString SAL_CALL DBContentLoader::getImplementationName() throw( )
124 : {
125 1 : return getImplementationName_Static();
126 : }
127 : // -------------------------------------------------------------------------
128 :
129 : // XServiceInfo
130 0 : sal_Bool SAL_CALL DBContentLoader::supportsService(const OUString& ServiceName) throw( )
131 : {
132 0 : Sequence< OUString > aSNL = getSupportedServiceNames();
133 0 : const OUString * pBegin = aSNL.getConstArray();
134 0 : const OUString * pEnd = pBegin + aSNL.getLength();
135 0 : for( ; pBegin != pEnd; ++pBegin)
136 0 : if( *pBegin == ServiceName )
137 0 : return sal_True;
138 0 : return sal_False;
139 : }
140 : // -------------------------------------------------------------------------
141 : // XServiceInfo
142 0 : Sequence< OUString > SAL_CALL DBContentLoader::getSupportedServiceNames(void) throw( )
143 : {
144 0 : return getSupportedServiceNames_Static();
145 : }
146 : // -------------------------------------------------------------------------
147 : // ORegistryServiceManager_Static
148 4 : Sequence< OUString > DBContentLoader::getSupportedServiceNames_Static(void) throw( )
149 : {
150 4 : Sequence< OUString > aSNS( 2 );
151 4 : aSNS.getArray()[0] = OUString("com.sun.star.frame.FrameLoader");
152 4 : aSNS.getArray()[1] = OUString("com.sun.star.sdb.ContentLoader");
153 4 : return aSNS;
154 : }
155 : // -------------------------------------------------------------------------
156 0 : extern "C" void SAL_CALL writeDBLoaderInfo(void* pRegistryKey)
157 : {
158 0 : Reference< XRegistryKey> xKey(reinterpret_cast< XRegistryKey*>(pRegistryKey));
159 :
160 : // register content loader for dispatch
161 0 : OUString aImpl("/");
162 0 : aImpl += DBContentLoader::getImplementationName_Static();
163 :
164 0 : OUString aImpltwo = aImpl;
165 0 : aImpltwo += "/UNO/Loader";
166 0 : Reference< XRegistryKey> xNewKey = xKey->createKey( aImpltwo );
167 0 : aImpltwo = aImpl;
168 0 : aImpltwo += "/Loader";
169 0 : Reference< XRegistryKey > xLoaderKey = xKey->createKey( aImpltwo );
170 0 : xNewKey = xLoaderKey->createKey( OUString("Pattern") );
171 0 : xNewKey->setAsciiValue( OUString(".component:DB*") );
172 0 : }
173 :
174 : // -----------------------------------------------------------------------
175 1 : void SAL_CALL DBContentLoader::load(const Reference< XFrame > & rFrame, const OUString& rURL,
176 : const Sequence< PropertyValue >& rArgs,
177 : const Reference< XLoadEventListener > & rListener) throw(::com::sun::star::uno::RuntimeException)
178 : {
179 1 : m_xFrame = rFrame;
180 1 : m_xListener = rListener;
181 1 : m_aURL = rURL;
182 1 : m_aArgs = rArgs;
183 :
184 : struct ServiceNameToImplName
185 : {
186 : const sal_Char* pAsciiServiceName;
187 : const sal_Char* pAsciiImplementationName;
188 6 : ServiceNameToImplName( const sal_Char* _pService, const sal_Char* _pImpl )
189 : :pAsciiServiceName( _pService )
190 6 : ,pAsciiImplementationName( _pImpl )
191 : {
192 6 : }
193 : } aImplementations[] = {
194 : ServiceNameToImplName( URL_COMPONENT_FORMGRIDVIEW, "org.openoffice.comp.dbu.OFormGridView" ),
195 : ServiceNameToImplName( URL_COMPONENT_DATASOURCEBROWSER, "org.openoffice.comp.dbu.ODatasourceBrowser" ),
196 : ServiceNameToImplName( URL_COMPONENT_QUERYDESIGN, "org.openoffice.comp.dbu.OQueryDesign" ),
197 : ServiceNameToImplName( URL_COMPONENT_TABLEDESIGN, "org.openoffice.comp.dbu.OTableDesign" ),
198 : ServiceNameToImplName( URL_COMPONENT_RELATIONDESIGN, "org.openoffice.comp.dbu.ORelationDesign" ),
199 : ServiceNameToImplName( URL_COMPONENT_VIEWDESIGN, "org.openoffice.comp.dbu.OViewDesign" )
200 1 : };
201 :
202 1 : INetURLObject aParser( rURL );
203 2 : Reference< XController2 > xController;
204 :
205 2 : const OUString sComponentURL( aParser.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
206 2 : for ( size_t i=0; i < sizeof( aImplementations ) / sizeof( aImplementations[0] ); ++i )
207 : {
208 2 : if ( sComponentURL.equalsAscii( aImplementations[i].pAsciiServiceName ) )
209 : {
210 2 : xController.set( m_xContext->getServiceManager()->
211 1 : createInstanceWithContext( OUString::createFromAscii( aImplementations[i].pAsciiImplementationName ), m_xContext), UNO_QUERY_THROW );
212 1 : break;
213 : }
214 : }
215 :
216 : // if a data source browser is loaded without its tree pane, then we assume it to be a
217 : // table data view, effectively. In this case, we need to adjust the module identifier.
218 : // #i85879#
219 2 : ::comphelper::NamedValueCollection aLoadArgs( rArgs );
220 :
221 1 : if ( sComponentURL == URL_COMPONENT_DATASOURCEBROWSER )
222 : {
223 4 : sal_Bool bDisableBrowser = ( sal_False == aLoadArgs.getOrDefault( "ShowTreeViewButton", sal_True ) ) // compatibility name
224 3 : || ( sal_False == aLoadArgs.getOrDefault( (OUString)PROPERTY_ENABLE_BROWSER, sal_True ) );
225 :
226 1 : if ( bDisableBrowser )
227 : {
228 : try
229 : {
230 0 : Reference< XModule > xModule( xController, UNO_QUERY_THROW );
231 0 : xModule->setIdentifier( OUString( "com.sun.star.sdb.TableDataView" ) );
232 : }
233 0 : catch( const Exception& )
234 : {
235 : DBG_UNHANDLED_EXCEPTION();
236 : }
237 : }
238 : }
239 :
240 1 : if ( sComponentURL == URL_COMPONENT_REPORTDESIGN )
241 : {
242 0 : sal_Bool bPreview = aLoadArgs.getOrDefault( "Preview", sal_False );
243 0 : if ( bPreview )
244 : { // report designs cannot be previewed
245 0 : if ( rListener.is() )
246 0 : rListener->loadCancelled( this );
247 1 : return;
248 : }
249 0 : Reference< XModel > xReportModel( aLoadArgs.getOrDefault( "Model", Reference< XModel >() ) );
250 0 : if ( xReportModel.is() )
251 : {
252 0 : xController.set( ReportDesign::create( m_xContext ) );
253 0 : xController->attachModel( xReportModel );
254 0 : xReportModel->connectController( xController.get() );
255 0 : xReportModel->setCurrentController( xController.get() );
256 0 : }
257 : }
258 :
259 1 : sal_Bool bSuccess = xController.is();
260 2 : Reference< XModel > xDatabaseDocument;
261 1 : if ( bSuccess )
262 : {
263 1 : Reference< XDataSource > xDataSource ( aLoadArgs.getOrDefault( "DataSource", Reference< XDataSource >() ) );
264 2 : OUString sDataSourceName( aLoadArgs.getOrDefault( "DataSourceName", OUString() ) );
265 2 : Reference< XConnection > xConnection ( aLoadArgs.getOrDefault( "ActiveConnection", Reference< XConnection >() ) );
266 1 : if ( xDataSource.is() )
267 : {
268 0 : xDatabaseDocument.set( getDataSourceOrModel( xDataSource ), UNO_QUERY );
269 : }
270 1 : else if ( !sDataSourceName.isEmpty() )
271 : {
272 0 : ::dbtools::SQLExceptionInfo aError;
273 0 : xDataSource.set( getDataSourceByName( sDataSourceName, NULL, m_xContext, &aError ) );
274 0 : xDatabaseDocument.set( getDataSourceOrModel( xDataSource ), UNO_QUERY );
275 : }
276 1 : else if ( xConnection.is() )
277 : {
278 0 : Reference< XChild > xAsChild( xConnection, UNO_QUERY );
279 0 : if ( xAsChild.is() )
280 : {
281 : OSL_ENSURE( Reference< XDataSource >( xAsChild->getParent(), UNO_QUERY ).is(),
282 : "DBContentLoader::load: a connection whose parent is no data source?" );
283 0 : xDatabaseDocument.set( getDataSourceOrModel( xAsChild->getParent() ), UNO_QUERY );
284 0 : }
285 : }
286 :
287 : // init controller
288 2 : SolarMutexGuard aGuard;
289 : try
290 : {
291 1 : Reference<XInitialization > xIni(xController,UNO_QUERY);
292 2 : PropertyValue aFrame(OUString("Frame"),0,makeAny(rFrame),PropertyState_DIRECT_VALUE);
293 2 : Sequence< Any > aInitArgs(m_aArgs.getLength()+1);
294 :
295 1 : Any* pBegin = aInitArgs.getArray();
296 1 : Any* pEnd = pBegin + aInitArgs.getLength();
297 1 : *pBegin <<= aFrame;
298 1 : const PropertyValue* pIter = m_aArgs.getConstArray();
299 8 : for(++pBegin;pBegin != pEnd;++pBegin,++pIter)
300 : {
301 7 : *pBegin <<= *pIter;
302 : }
303 :
304 2 : xIni->initialize(aInitArgs);
305 : }
306 0 : catch(const Exception&)
307 : {
308 : // Does this need to be shown to the user?
309 0 : bSuccess = false;
310 : try
311 : {
312 0 : ::comphelper::disposeComponent( xController );
313 : }
314 0 : catch( const Exception& )
315 : {
316 : DBG_UNHANDLED_EXCEPTION();
317 : }
318 1 : }
319 : }
320 :
321 : // assign controller and frame
322 1 : if ( bSuccess )
323 : {
324 1 : if ( xController.is() && rFrame.is() )
325 : {
326 1 : rFrame->setComponent( xController->getComponentWindow(), xController.get() );
327 1 : xController->attachFrame(rFrame);
328 : }
329 :
330 1 : if ( rListener.is() )
331 1 : rListener->loadFinished( this );
332 : }
333 : else
334 0 : if ( rListener.is() )
335 1 : rListener->loadCancelled( this );
336 : }
337 :
338 : // -----------------------------------------------------------------------
339 0 : void DBContentLoader::cancel(void) throw()
340 : {
341 12 : }
342 :
343 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|