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