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 "flt_reghelper.hxx"
22 : #include "xmlstrings.hrc"
23 :
24 : #include <com/sun/star/beans/NamedValue.hpp>
25 : #include <com/sun/star/beans/XPropertySet.hpp>
26 : #include <com/sun/star/document/XEventListener.hpp>
27 : #include <com/sun/star/document/XExtendedFilterDetection.hpp>
28 : #include <com/sun/star/document/XFilter.hpp>
29 : #include <com/sun/star/embed/ElementModes.hpp>
30 : #include <com/sun/star/embed/XStorage.hpp>
31 : #include <com/sun/star/frame/XController.hpp>
32 : #include <com/sun/star/frame/XFrame.hpp>
33 : #include <com/sun/star/frame/XFrameLoader.hpp>
34 : #include <com/sun/star/frame/XFramesSupplier.hpp>
35 : #include <com/sun/star/frame/XLoadEventListener.hpp>
36 : #include <com/sun/star/frame/XModel2.hpp>
37 : #include <com/sun/star/io/XInputStream.hpp>
38 : #include <com/sun/star/lang/XInitialization.hpp>
39 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 : #include <com/sun/star/lang/XServiceInfo.hpp>
41 : #include <com/sun/star/registry/XRegistryKey.hpp>
42 : #include <com/sun/star/sdb/DatabaseContext.hpp>
43 : #include <com/sun/star/sdb/XDocumentDataSource.hpp>
44 : #include <com/sun/star/task/XJobExecutor.hpp>
45 : #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
46 : #include <com/sun/star/task/InteractionHandler.hpp>
47 : #include <com/sun/star/util/URLTransformer.hpp>
48 : #include <com/sun/star/util/XURLTransformer.hpp>
49 : #include <com/sun/star/view/XSelectionSupplier.hpp>
50 : #include <com/sun/star/sdb/application/DatabaseObjectContainer.hpp>
51 : #include <com/sun/star/sdb/application/NamedDatabaseObject.hpp>
52 : #include <com/sun/star/frame/XLoadable.hpp>
53 :
54 : #include <comphelper/componentcontext.hxx>
55 : #include <comphelper/documentconstants.hxx>
56 : #include <comphelper/namedvaluecollection.hxx>
57 : #include <comphelper/processfactory.hxx>
58 : #include <comphelper/sequenceashashmap.hxx>
59 : #include <comphelper/stl_types.hxx>
60 : #include <comphelper/storagehelper.hxx>
61 : #include <comphelper/types.hxx>
62 : #include <cppuhelper/implbase2.hxx>
63 : #include <osl/file.hxx>
64 : #include <sfx2/docfile.hxx>
65 : #include <unotools/moduleoptions.hxx>
66 : #include <toolkit/awt/vclxwindow.hxx>
67 : #include <toolkit/helper/vclunohelper.hxx>
68 : #include <tools/diagnose_ex.h>
69 : #include <ucbhelper/commandenvironment.hxx>
70 : #include <ucbhelper/content.hxx>
71 : #include <vcl/msgbox.hxx>
72 : #include <vcl/svapp.hxx>
73 :
74 : using namespace ::ucbhelper;
75 : using namespace ::com::sun::star::task;
76 : using namespace ::com::sun::star::uno;
77 : using namespace ::com::sun::star::ucb;
78 : using namespace ::com::sun::star::io;
79 : using namespace ::com::sun::star::util;
80 : using namespace ::com::sun::star::frame;
81 : using namespace ::com::sun::star::beans;
82 : using namespace ::com::sun::star::container;
83 : using namespace ::com::sun::star::lang;
84 : using namespace ::com::sun::star::document;
85 : using namespace ::com::sun::star::registry;
86 : using namespace ::com::sun::star::sdb;
87 : using namespace ::com::sun::star::embed;
88 : using namespace ::com::sun::star::ui::dialogs;
89 : using ::com::sun::star::awt::XWindow;
90 : using ::com::sun::star::sdb::application::NamedDatabaseObject;
91 :
92 : // -------------------------------------------------------------------------
93 : namespace dbaxml
94 : {
95 :
96 0 : class DBTypeDetection : public ::cppu::WeakImplHelper2< XExtendedFilterDetection, XServiceInfo>
97 : {
98 : ::comphelper::ComponentContext m_aContext;
99 :
100 : public:
101 : DBTypeDetection(const Reference< XMultiServiceFactory >&);
102 :
103 : // XServiceInfo
104 : ::rtl::OUString SAL_CALL getImplementationName() throw( );
105 : sal_Bool SAL_CALL supportsService(const ::rtl::OUString& ServiceName) throw( );
106 : Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(void) throw( );
107 :
108 : // static methods
109 0 : static ::rtl::OUString getImplementationName_Static() throw( )
110 : {
111 0 : return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.dbflt.DBTypeDetection"));
112 : }
113 : static Sequence< ::rtl::OUString> getSupportedServiceNames_Static(void) throw( );
114 : static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
115 : SAL_CALL Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&);
116 :
117 : virtual ::rtl::OUString SAL_CALL detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& Descriptor ) throw (::com::sun::star::uno::RuntimeException);
118 : };
119 : // -------------------------------------------------------------------------
120 0 : DBTypeDetection::DBTypeDetection(const Reference< XMultiServiceFactory >& _rxFactory)
121 0 : :m_aContext( _rxFactory )
122 : {
123 0 : }
124 : // -------------------------------------------------------------------------
125 0 : ::rtl::OUString SAL_CALL DBTypeDetection::detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& Descriptor ) throw (::com::sun::star::uno::RuntimeException)
126 : {
127 : try
128 : {
129 0 : ::comphelper::NamedValueCollection aMedia( Descriptor );
130 0 : sal_Bool bStreamFromDescr = sal_False;
131 0 : ::rtl::OUString sURL = aMedia.getOrDefault( "URL", ::rtl::OUString() );
132 :
133 0 : Reference< XInputStream > xInStream( aMedia.getOrDefault( "InputStream", Reference< XInputStream >() ) );
134 0 : Reference< XPropertySet > xStorageProperties;
135 0 : if ( xInStream.is() )
136 : {
137 0 : bStreamFromDescr = sal_True;
138 : xStorageProperties.set( ::comphelper::OStorageHelper::GetStorageFromInputStream(
139 0 : xInStream, m_aContext.getUNOContext() ), UNO_QUERY );
140 : }
141 : else
142 : {
143 0 : ::rtl::OUString sSalvagedURL( aMedia.getOrDefault( "SalvagedFile", ::rtl::OUString() ) );
144 :
145 0 : ::rtl::OUString sFileLocation( sSalvagedURL.isEmpty() ? sURL : sSalvagedURL );
146 0 : if ( !sFileLocation.isEmpty() )
147 : {
148 : xStorageProperties.set( ::comphelper::OStorageHelper::GetStorageFromURL(
149 0 : sFileLocation, ElementModes::READ, m_aContext.getUNOContext() ), UNO_QUERY );
150 0 : }
151 : }
152 :
153 0 : if ( xStorageProperties.is() )
154 : {
155 0 : ::rtl::OUString sMediaType;
156 0 : xStorageProperties->getPropertyValue( INFO_MEDIATYPE ) >>= sMediaType;
157 0 : if ( sMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII || sMediaType == MIMETYPE_VND_SUN_XML_BASE_ASCII )
158 : {
159 0 : if ( bStreamFromDescr && sURL.compareTo( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "private:stream" ) ), 14 ) != COMPARE_EQUAL )
160 : {
161 : // After fixing of the i88522 issue ( use the new file locking for database files ) the stream from the type detection can be used further
162 : // for now the file should be reopened to have read/write access
163 0 : aMedia.remove( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "InputStream" ) ) );
164 0 : aMedia.remove( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Stream" ) ) );
165 0 : aMedia >>= Descriptor;
166 : try
167 : {
168 0 : ::comphelper::disposeComponent(xStorageProperties);
169 0 : if ( xInStream.is() )
170 0 : xInStream->closeInput();
171 : }
172 0 : catch( Exception& )
173 : {
174 : DBG_UNHANDLED_EXCEPTION();
175 : }
176 : }
177 :
178 0 : return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("StarBase"));
179 : }
180 0 : ::comphelper::disposeComponent(xStorageProperties);
181 0 : }
182 0 : } catch(Exception&){}
183 0 : return ::rtl::OUString();
184 : }
185 : // -------------------------------------------------------------------------
186 0 : Reference< XInterface > SAL_CALL DBTypeDetection::Create( const Reference< XMultiServiceFactory > & rSMgr )
187 : {
188 0 : return *(new DBTypeDetection(rSMgr));
189 : }
190 : // -------------------------------------------------------------------------
191 : // XServiceInfo
192 0 : ::rtl::OUString SAL_CALL DBTypeDetection::getImplementationName() throw( )
193 : {
194 0 : return getImplementationName_Static();
195 : }
196 : // -------------------------------------------------------------------------
197 :
198 : // XServiceInfo
199 0 : sal_Bool SAL_CALL DBTypeDetection::supportsService(const ::rtl::OUString& ServiceName) throw( )
200 : {
201 0 : Sequence< ::rtl::OUString > aSNL = getSupportedServiceNames();
202 0 : const ::rtl::OUString * pBegin = aSNL.getConstArray();
203 0 : const ::rtl::OUString * pEnd = pBegin + aSNL.getLength();
204 0 : for( ; pBegin != pEnd; ++pBegin)
205 0 : if( *pBegin == ServiceName )
206 0 : return sal_True;
207 0 : return sal_False;
208 : }
209 : // -------------------------------------------------------------------------
210 : // XServiceInfo
211 0 : Sequence< ::rtl::OUString > SAL_CALL DBTypeDetection::getSupportedServiceNames(void) throw( )
212 : {
213 0 : return getSupportedServiceNames_Static();
214 : }
215 : // -------------------------------------------------------------------------
216 : // ORegistryServiceManager_Static
217 0 : Sequence< ::rtl::OUString > DBTypeDetection::getSupportedServiceNames_Static(void) throw( )
218 : {
219 0 : Sequence< ::rtl::OUString > aSNS( 1 );
220 0 : aSNS.getArray()[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.document.ExtendedTypeDetection"));
221 0 : return aSNS;
222 : }
223 : // -------------------------------------------------------------------------
224 0 : extern "C" void SAL_CALL createRegistryInfo_DBTypeDetection()
225 : {
226 0 : static ::dbaxml::OMultiInstanceAutoRegistration< ::dbaxml::DBTypeDetection > aAutoRegistration;
227 0 : }
228 : // -----------------------------------------------------------------------------
229 :
230 : class DBContentLoader : public ::cppu::WeakImplHelper2< XFrameLoader, XServiceInfo>
231 : {
232 : private:
233 : ::comphelper::ComponentContext m_aContext;
234 : Reference< XFrameLoader > m_xMySelf;
235 : ::rtl::OUString m_sCurrentURL;
236 : sal_uLong m_nStartWizard;
237 :
238 : DECL_LINK( OnStartTableWizard, void* );
239 : public:
240 : DBContentLoader(const Reference< XMultiServiceFactory >&);
241 : ~DBContentLoader();
242 :
243 : // XServiceInfo
244 : ::rtl::OUString SAL_CALL getImplementationName() throw( );
245 : sal_Bool SAL_CALL supportsService(const ::rtl::OUString& ServiceName) throw( );
246 : Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(void) throw( );
247 :
248 : // static methods
249 0 : static ::rtl::OUString getImplementationName_Static() throw( )
250 : {
251 0 : return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.dbflt.DBContentLoader2"));
252 : }
253 : static Sequence< ::rtl::OUString> getSupportedServiceNames_Static(void) throw( );
254 : static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
255 : SAL_CALL Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&);
256 :
257 : // XLoader
258 : virtual void SAL_CALL load( const Reference< XFrame > & _rFrame, const ::rtl::OUString& _rURL,
259 : const Sequence< PropertyValue >& _rArgs,
260 : const Reference< XLoadEventListener > & _rListener) throw(::com::sun::star::uno::RuntimeException);
261 : virtual void SAL_CALL cancel(void) throw();
262 :
263 : private:
264 : sal_Bool impl_executeNewDatabaseWizard( Reference< XModel >& _rxModel, sal_Bool& _bShouldStartTableWizard );
265 : };
266 : DBG_NAME(DBContentLoader)
267 :
268 0 : DBContentLoader::DBContentLoader(const Reference< XMultiServiceFactory >& _rxFactory)
269 : :m_aContext( _rxFactory )
270 0 : ,m_nStartWizard(0)
271 : {
272 : DBG_CTOR(DBContentLoader,NULL);
273 :
274 0 : }
275 : // -------------------------------------------------------------------------
276 :
277 0 : DBContentLoader::~DBContentLoader()
278 : {
279 :
280 : DBG_DTOR(DBContentLoader,NULL);
281 0 : }
282 : // -------------------------------------------------------------------------
283 :
284 : // -------------------------------------------------------------------------
285 0 : Reference< XInterface > SAL_CALL DBContentLoader::Create( const Reference< XMultiServiceFactory > & rSMgr )
286 : {
287 0 : return *(new DBContentLoader(rSMgr));
288 : }
289 : // -------------------------------------------------------------------------
290 : // XServiceInfo
291 0 : ::rtl::OUString SAL_CALL DBContentLoader::getImplementationName() throw( )
292 : {
293 0 : return getImplementationName_Static();
294 : }
295 : // -------------------------------------------------------------------------
296 :
297 : // XServiceInfo
298 0 : sal_Bool SAL_CALL DBContentLoader::supportsService(const ::rtl::OUString& ServiceName) throw( )
299 : {
300 0 : Sequence< ::rtl::OUString > aSNL = getSupportedServiceNames();
301 0 : const ::rtl::OUString * pBegin = aSNL.getConstArray();
302 0 : const ::rtl::OUString * pEnd = pBegin + aSNL.getLength();
303 0 : for( ; pBegin != pEnd; ++pBegin)
304 0 : if( *pBegin == ServiceName )
305 0 : return sal_True;
306 0 : return sal_False;
307 : }
308 : // -------------------------------------------------------------------------
309 : // XServiceInfo
310 0 : Sequence< ::rtl::OUString > SAL_CALL DBContentLoader::getSupportedServiceNames(void) throw( )
311 : {
312 0 : return getSupportedServiceNames_Static();
313 : }
314 : // -------------------------------------------------------------------------
315 : // ORegistryServiceManager_Static
316 0 : Sequence< ::rtl::OUString > DBContentLoader::getSupportedServiceNames_Static(void) throw( )
317 : {
318 0 : Sequence< ::rtl::OUString > aSNS( 1 );
319 0 : aSNS.getArray()[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.FrameLoader"));
320 0 : return aSNS;
321 : }
322 :
323 : // -----------------------------------------------------------------------
324 : namespace
325 : {
326 : // ...................................................................
327 0 : sal_Bool lcl_urlAllowsInteraction( const ::comphelper::ComponentContext& _rContext, const ::rtl::OUString& _rURL )
328 : {
329 0 : bool bDoesAllow = sal_False;
330 : try
331 : {
332 0 : Reference< XURLTransformer > xTransformer( URLTransformer::create(_rContext.getUNOContext()) );
333 0 : URL aURL;
334 0 : aURL.Complete = _rURL;
335 0 : xTransformer->parseStrict( aURL );
336 0 : bDoesAllow = aURL.Arguments == "Interactive";
337 : }
338 0 : catch( const Exception& )
339 : {
340 : OSL_FAIL( "lcl_urlAllowsInteraction: caught an exception while analyzing the URL!" );
341 : }
342 0 : return bDoesAllow;
343 : }
344 :
345 : // ...................................................................
346 0 : Reference< XWindow > lcl_getTopMostWindow( const ::comphelper::ComponentContext& _rContext )
347 : {
348 0 : Reference< XWindow > xWindow;
349 : // get the top most window
350 0 : Reference < XFramesSupplier > xDesktop;
351 0 : if ( _rContext.createComponent( "com.sun.star.frame.Desktop", xDesktop ) )
352 : {
353 0 : Reference < XFrame > xActiveFrame = xDesktop->getActiveFrame();
354 0 : if ( xActiveFrame.is() )
355 : {
356 0 : xWindow = xActiveFrame->getContainerWindow();
357 0 : Reference<XFrame> xFrame = xActiveFrame;
358 0 : while ( xFrame.is() && !xFrame->isTop() )
359 0 : xFrame.set(xFrame->getCreator(),UNO_QUERY);
360 :
361 0 : if ( xFrame.is() )
362 0 : xWindow = xFrame->getContainerWindow();
363 0 : }
364 : }
365 0 : return xWindow;
366 : }
367 : }
368 :
369 : // -----------------------------------------------------------------------
370 0 : sal_Bool DBContentLoader::impl_executeNewDatabaseWizard( Reference< XModel >& _rxModel, sal_Bool& _bShouldStartTableWizard )
371 : {
372 0 : Sequence< Any > aWizardArgs(2);
373 0 : aWizardArgs[0] <<= PropertyValue(
374 : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ParentWindow")),
375 : 0,
376 : makeAny( lcl_getTopMostWindow( m_aContext ) ),
377 0 : PropertyState_DIRECT_VALUE);
378 :
379 0 : aWizardArgs[1] <<= PropertyValue(
380 : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("InitialSelection")),
381 : 0,
382 : makeAny( _rxModel ),
383 0 : PropertyState_DIRECT_VALUE);
384 :
385 : // create the dialog
386 0 : Reference< XExecutableDialog > xAdminDialog;
387 0 : OSL_VERIFY( m_aContext.createComponentWithArguments( "com.sun.star.sdb.DatabaseWizardDialog", aWizardArgs, xAdminDialog ) );
388 :
389 : // execute it
390 0 : if ( !xAdminDialog.is() || ( RET_OK != xAdminDialog->execute() ) )
391 0 : return sal_False;
392 :
393 0 : Reference<XPropertySet> xProp(xAdminDialog,UNO_QUERY);
394 0 : sal_Bool bSuccess = sal_False;
395 0 : xProp->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OpenDatabase"))) >>= bSuccess;
396 0 : xProp->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("StartTableWizard"))) >>= _bShouldStartTableWizard;
397 0 : return bSuccess;
398 : }
399 :
400 : // -----------------------------------------------------------------------
401 0 : void SAL_CALL DBContentLoader::load(const Reference< XFrame > & rFrame, const ::rtl::OUString& _rURL,
402 : const Sequence< PropertyValue >& rArgs,
403 : const Reference< XLoadEventListener > & rListener) throw(::com::sun::star::uno::RuntimeException)
404 : {
405 : // first check if preview is true, if so return with out creating a controller. Preview is not supported
406 0 : ::comphelper::NamedValueCollection aMediaDesc( rArgs );
407 0 : sal_Bool bPreview = aMediaDesc.getOrDefault( "Preview", sal_False );
408 0 : if ( bPreview )
409 : {
410 0 : if (rListener.is())
411 0 : rListener->loadCancelled(this);
412 : return;
413 : }
414 :
415 0 : Reference< XModel > xModel = aMediaDesc.getOrDefault( "Model", Reference< XModel >() );
416 0 : ::rtl::OUString sSalvagedURL = aMediaDesc.getOrDefault( "SalvagedFile", _rURL );
417 :
418 0 : sal_Bool bCreateNew = sal_False; // does the URL denote the private:factory URL?
419 0 : sal_Bool bStartTableWizard = sal_False; // start the table wizard after everything was loaded successfully?
420 :
421 0 : sal_Bool bSuccess = sal_True;
422 :
423 : // If there's no interaction handler in the media descriptor, put one.
424 : // By definition, loading via loadComponentFromURL (and thus via the content loader here)
425 : // is allowed to raise UI. To not burden every place inside the document with creating
426 : // a default handler, we simply ensure there is one.
427 : // If a handler is present in the media descriptor, even if it is NULL, we will
428 : // not touch it.
429 0 : if ( !aMediaDesc.has( "InteractionHandler" ) )
430 : {
431 0 : Reference< XInteractionHandler2 > xHandler( InteractionHandler::createWithParent(m_aContext.getUNOContext(), 0) );
432 0 : aMediaDesc.put( "InteractionHandler", xHandler );
433 : }
434 :
435 : // it's allowed to pass an existing document
436 0 : Reference< XOfficeDatabaseDocument > xExistentDBDoc;
437 0 : xModel.set( aMediaDesc.getOrDefault( "Model", xExistentDBDoc ), UNO_QUERY );
438 0 : aMediaDesc.remove( "Model" );
439 :
440 : // also, it's allowed to specify the type of view which should be created
441 0 : ::rtl::OUString sViewName = aMediaDesc.getOrDefault( "ViewName", ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Default" ) ) );
442 0 : aMediaDesc.remove( "ViewName" );
443 :
444 0 : sal_Int32 nInitialSelection = -1;
445 0 : if ( !xModel.is() )
446 : {
447 0 : Reference< XDatabaseContext > xDatabaseContext( DatabaseContext::create(m_aContext.getUNOContext()) );
448 :
449 0 : ::rtl::OUString sFactoryName = SvtModuleOptions().GetFactoryEmptyDocumentURL(SvtModuleOptions::E_DATABASE);
450 0 : bCreateNew = sFactoryName.match(_rURL);
451 :
452 0 : Reference< XDocumentDataSource > xDocumentDataSource;
453 0 : bool bNewAndInteractive = false;
454 0 : if ( bCreateNew )
455 : {
456 0 : bNewAndInteractive = lcl_urlAllowsInteraction( m_aContext, _rURL );
457 0 : xDocumentDataSource.set( xDatabaseContext->createInstance(), UNO_QUERY_THROW );
458 : }
459 : else
460 : {
461 0 : ::comphelper::NamedValueCollection aCreationArgs;
462 0 : aCreationArgs.put( (::rtl::OUString)INFO_POOLURL, sSalvagedURL );
463 0 : xDocumentDataSource.set( xDatabaseContext->createInstanceWithArguments( aCreationArgs.getWrappedNamedValues() ), UNO_QUERY_THROW );
464 : }
465 :
466 0 : xModel.set( xDocumentDataSource->getDatabaseDocument(), UNO_QUERY );
467 :
468 0 : if ( bCreateNew && xModel.is() )
469 : {
470 0 : if ( bNewAndInteractive )
471 : {
472 0 : bSuccess = impl_executeNewDatabaseWizard( xModel, bStartTableWizard );
473 : }
474 : else
475 : {
476 : try
477 : {
478 0 : Reference< XLoadable > xLoad( xModel, UNO_QUERY_THROW );
479 0 : xLoad->initNew();
480 0 : bSuccess = true;
481 : }
482 0 : catch( const Exception& )
483 : {
484 0 : bSuccess = false;
485 : }
486 : }
487 :
488 : // initially select the "Tables" category (will be done below)
489 0 : nInitialSelection = ::com::sun::star::sdb::application::DatabaseObjectContainer::TABLES;
490 0 : }
491 : }
492 :
493 0 : if ( !xModel.is() )
494 : {
495 0 : if ( rListener.is() )
496 0 : rListener->loadCancelled(this);
497 : return;
498 : }
499 :
500 0 : if ( !bCreateNew )
501 : {
502 : // We need to XLoadable::load the document if it does not yet have an URL.
503 : // If it already *does* have an URL, then it was either passed in the arguments, or a previous incarnation
504 : // of that model existed before (which can happen if a model is closed, but an associated DataSource is kept
505 : // alive 'til loading the document again).
506 0 : bool bNeedLoad = ( xModel->getURL().isEmpty() );
507 : try
508 : {
509 0 : aMediaDesc.put( "FileName", _rURL );
510 0 : Sequence< PropertyValue > aResource( aMediaDesc.getPropertyValues() );
511 :
512 0 : if ( bNeedLoad )
513 : {
514 0 : Reference< XLoadable > xLoad( xModel, UNO_QUERY_THROW );
515 0 : xLoad->load( aResource );
516 : }
517 :
518 : // always attach the resource, even if the document has not been freshly loaded
519 0 : xModel->attachResource( _rURL, aResource );
520 : }
521 0 : catch(const Exception&)
522 : {
523 : DBG_UNHANDLED_EXCEPTION();
524 0 : bSuccess = sal_False;
525 : }
526 : }
527 :
528 0 : if ( bSuccess )
529 : {
530 : try
531 : {
532 0 : Reference< XModel2 > xModel2( xModel, UNO_QUERY_THROW );
533 0 : Reference< XController2 > xController( xModel2->createViewController( sViewName, Sequence< PropertyValue >(), rFrame ), UNO_QUERY_THROW );
534 :
535 0 : xController->attachModel( xModel );
536 0 : xModel->connectController( xController.get() );
537 0 : rFrame->setComponent( xController->getComponentWindow(), xController.get() );
538 0 : xController->attachFrame( rFrame );
539 0 : xModel->setCurrentController( xController.get() );
540 :
541 0 : bSuccess = sal_True;
542 : }
543 0 : catch( const Exception& )
544 : {
545 : DBG_UNHANDLED_EXCEPTION();
546 0 : bSuccess = sal_False;
547 : }
548 : }
549 :
550 0 : if (bSuccess)
551 : {
552 0 : if ( rListener.is() )
553 0 : rListener->loadFinished(this);
554 :
555 0 : if ( nInitialSelection != -1 )
556 : {
557 0 : Reference< css::view::XSelectionSupplier > xDocView( xModel->getCurrentController(), UNO_QUERY );
558 0 : if ( xDocView.is() )
559 : {
560 0 : NamedDatabaseObject aSelection;
561 0 : aSelection.Type = nInitialSelection;
562 0 : xDocView->select( makeAny( aSelection ) );
563 0 : }
564 : }
565 :
566 0 : if ( bStartTableWizard )
567 : {
568 : // reset the data of the previous async drop (if any)
569 0 : if ( m_nStartWizard )
570 0 : Application::RemoveUserEvent(m_nStartWizard);
571 0 : m_sCurrentURL = xModel->getURL();
572 0 : m_xMySelf = this;
573 0 : m_nStartWizard = Application::PostUserEvent(LINK(this, DBContentLoader, OnStartTableWizard));
574 : }
575 : }
576 : else
577 : {
578 0 : if ( rListener.is() )
579 0 : rListener->loadCancelled( this );
580 : }
581 :
582 0 : if ( !bSuccess )
583 0 : ::comphelper::disposeComponent(xModel);
584 : }
585 :
586 : // -----------------------------------------------------------------------
587 0 : void DBContentLoader::cancel(void) throw()
588 : {
589 0 : }
590 : // -----------------------------------------------------------------------------
591 0 : IMPL_LINK( DBContentLoader, OnStartTableWizard, void*, /*NOTINTERESTEDIN*/ )
592 : {
593 0 : m_nStartWizard = 0;
594 : try
595 : {
596 0 : Sequence< Any > aWizArgs(1);
597 0 : PropertyValue aValue;
598 0 : aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DatabaseLocation"));
599 0 : aValue.Value <<= m_sCurrentURL;
600 0 : aWizArgs[0] <<= aValue;
601 :
602 0 : SolarMutexGuard aGuard;
603 0 : Reference< XJobExecutor > xTableWizard;
604 0 : if ( m_aContext.createComponentWithArguments( "com.sun.star.wizards.table.CallTableWizard", aWizArgs, xTableWizard ) )
605 0 : xTableWizard->trigger(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("start")));
606 : }
607 0 : catch(const Exception&)
608 : {
609 : OSL_FAIL("caught an exception while starting the table wizard!");
610 : }
611 0 : m_xMySelf = NULL;
612 0 : return 0L;
613 : }
614 : }
615 : // -------------------------------------------------------------------------
616 0 : extern "C" void SAL_CALL createRegistryInfo_DBContentLoader2()
617 : {
618 0 : static ::dbaxml::OMultiInstanceAutoRegistration< ::dbaxml::DBContentLoader > aAutoRegistration;
619 0 : }
620 : // -------------------------------------------------------------------------
621 0 : extern "C" void SAL_CALL writeDBLoaderInfo2(void* pRegistryKey)
622 : {
623 0 : Reference< XRegistryKey> xKey(reinterpret_cast< XRegistryKey*>(pRegistryKey));
624 :
625 : // register content loader for dispatch
626 0 : ::rtl::OUString aImpl(RTL_CONSTASCII_USTRINGPARAM("/"));
627 0 : aImpl += ::dbaxml::DBContentLoader::getImplementationName_Static();
628 :
629 0 : ::rtl::OUString aImpltwo = aImpl;
630 0 : aImpltwo += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/UNO/Loader"));
631 0 : Reference< XRegistryKey> xNewKey = xKey->createKey( aImpltwo );
632 0 : aImpltwo = aImpl;
633 0 : aImpltwo += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/Loader"));
634 0 : Reference< XRegistryKey > xLoaderKey = xKey->createKey( aImpltwo );
635 0 : xNewKey = xLoaderKey->createKey( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Pattern")) );
636 0 : xNewKey->setAsciiValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("private:factory/sdatabase")) );
637 0 : }
638 : // -----------------------------------------------------------------------------
639 :
640 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|