build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / dbaccess / source / core / recovery / subcomponentrecovery.cxx
blob220964ab0d40faef32d986740270a1cf2815d301
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "subcomponentrecovery.hxx"
22 #include "sdbcoretools.hxx"
23 #include "storagexmlstream.hxx"
24 #include "subcomponentloader.hxx"
25 #include "settingsimport.hxx"
27 #include <com/sun/star/embed/ElementModes.hpp>
28 #include <com/sun/star/frame/ModuleManager.hpp>
29 #include <com/sun/star/document/XStorageBasedDocument.hpp>
30 #include <com/sun/star/ucb/XCommandProcessor.hpp>
31 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
32 #include <com/sun/star/sdb/XFormDocumentsSupplier.hpp>
33 #include <com/sun/star/sdb/XReportDocumentsSupplier.hpp>
34 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
36 #include <comphelper/namedvaluecollection.hxx>
37 #include <cppuhelper/implbase.hxx>
38 #include <connectivity/dbtools.hxx>
39 #include <tools/diagnose_ex.h>
40 #include <xmloff/XMLSettingsExportContext.hxx>
41 #include <xmloff/SettingsExportHelper.hxx>
43 #include <stack>
45 namespace dbaccess
48 using css::uno::Reference;
49 using css::uno::XInterface;
50 using css::uno::UNO_QUERY;
51 using css::uno::UNO_QUERY_THROW;
52 using css::uno::UNO_SET_THROW;
53 using css::uno::Exception;
54 using css::uno::RuntimeException;
55 using css::uno::Any;
56 using css::uno::Sequence;
57 using css::uno::XComponentContext;
58 using css::embed::XStorage;
59 using css::sdb::application::XDatabaseDocumentUI;
60 using css::beans::Pair;
61 using css::frame::ModuleManager;
62 using css::frame::XModuleManager2;
63 using css::lang::XComponent;
64 using css::frame::XModel;
65 using css::frame::XController;
66 using css::beans::XPropertySet;
67 using css::beans::PropertyValue;
68 using css::document::XStorageBasedDocument;
69 using css::ucb::XCommandProcessor;
70 using css::container::XHierarchicalNameAccess;
71 using css::sdb::XFormDocumentsSupplier;
72 using css::sdb::XReportDocumentsSupplier;
73 using css::xml::sax::SAXException;
74 using css::xml::sax::XLocator;
75 using css::xml::sax::XDocumentHandler;
76 using css::xml::sax::XAttributeList;
78 namespace ElementModes = css::embed::ElementModes;
79 namespace DatabaseObject = css::sdb::application::DatabaseObject;
81 // helper
82 namespace
84 OUString lcl_getComponentStorageBaseName( const SubComponentType i_eType )
86 switch ( i_eType )
88 case FORM:
89 return OUString("form");
90 case REPORT:
91 return OUString("report");
92 case TABLE:
93 return OUString("table");
94 case QUERY:
95 return OUString("query");
96 default:
97 break;
100 OSL_FAIL( "lcl_getComponentStorageBaseName: unimplemented case!" );
101 return OUString();
104 SubComponentType lcl_databaseObjectToSubComponentType( const sal_Int32 i_nObjectType )
106 switch ( i_nObjectType )
108 case DatabaseObject::TABLE: return TABLE;
109 case DatabaseObject::QUERY: return QUERY;
110 case DatabaseObject::FORM: return FORM;
111 case DatabaseObject::REPORT:return REPORT;
112 default:
113 break;
115 return UNKNOWN;
118 bool lcl_determineReadOnly( const Reference< XComponent >& i_rComponent )
120 Reference< XModel > xDocument( i_rComponent, UNO_QUERY );
121 if ( !xDocument.is() )
123 Reference< XController > xController( i_rComponent, UNO_QUERY_THROW );
124 xDocument = xController->getModel();
127 if ( !xDocument.is() )
128 return false;
130 ::comphelper::NamedValueCollection aDocArgs( xDocument->getArgs() );
131 return aDocArgs.getOrDefault( "ReadOnly", false );
134 Reference< XCommandProcessor > lcl_getSubComponentDef_nothrow( const Reference< XDatabaseDocumentUI >& i_rAppUI,
135 const SubComponentType i_eType, const OUString& i_rName )
137 Reference< XController > xController( i_rAppUI, UNO_QUERY_THROW );
138 ENSURE_OR_RETURN( ( i_eType == FORM ) || ( i_eType == REPORT ), "lcl_getSubComponentDef_nothrow: illegal controller", nullptr );
140 Reference< XCommandProcessor > xCommandProcessor;
143 Reference< XHierarchicalNameAccess > xDefinitionContainer;
144 if ( i_eType == FORM )
146 Reference< XFormDocumentsSupplier > xSuppForms( xController->getModel(), UNO_QUERY_THROW );
147 xDefinitionContainer.set( xSuppForms->getFormDocuments(), UNO_QUERY_THROW );
149 else
151 Reference< XReportDocumentsSupplier > xSuppReports( xController->getModel(), UNO_QUERY_THROW );
152 xDefinitionContainer.set( xSuppReports->getReportDocuments(), UNO_QUERY_THROW );
154 xCommandProcessor.set( xDefinitionContainer->getByHierarchicalName( i_rName ), UNO_QUERY_THROW );
156 catch( const Exception& )
158 DBG_UNHANDLED_EXCEPTION();
160 return xCommandProcessor;
163 static const char sSettingsStreamName[] = "settings.xml";
164 static const char sCurrentQueryDesignName[] = "ooo:current-query-design";
167 // SettingsExportContext
168 class SettingsExportContext : public ::xmloff::XMLSettingsExportContext
170 public:
171 SettingsExportContext( const Reference<XComponentContext>& i_rContext, const StorageXMLOutputStream& i_rDelegator )
172 :m_rContext( i_rContext )
173 ,m_rDelegator( i_rDelegator )
174 ,m_aNamespace( ::xmloff::token::GetXMLToken( ::xmloff::token::XML_NP_CONFIG ) )
178 virtual ~SettingsExportContext()
182 public:
183 virtual void AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, const OUString& i_rValue ) override;
184 virtual void AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, enum ::xmloff::token::XMLTokenEnum i_eValue ) override;
185 virtual void StartElement( enum ::xmloff::token::XMLTokenEnum i_eName ) override;
186 virtual void EndElement ( const bool i_bIgnoreWhitespace ) override;
187 virtual void Characters( const OUString& i_rCharacters ) override;
189 virtual css::uno::Reference< css::uno::XComponentContext >
190 GetComponentContext() const override;
192 private:
193 OUString impl_prefix( const ::xmloff::token::XMLTokenEnum i_eToken )
195 return m_aNamespace + ":" + ::xmloff::token::GetXMLToken( i_eToken );
198 private:
199 const Reference<XComponentContext>& m_rContext;
200 const StorageXMLOutputStream& m_rDelegator;
201 const OUString m_aNamespace;
204 void SettingsExportContext::AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, const OUString& i_rValue )
206 m_rDelegator.addAttribute( impl_prefix( i_eName ), i_rValue );
209 void SettingsExportContext::AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, enum ::xmloff::token::XMLTokenEnum i_eValue )
211 m_rDelegator.addAttribute( impl_prefix( i_eName ), ::xmloff::token::GetXMLToken( i_eValue ) );
214 void SettingsExportContext::StartElement( enum ::xmloff::token::XMLTokenEnum i_eName )
216 m_rDelegator.ignorableWhitespace( " " );
218 m_rDelegator.startElement( impl_prefix( i_eName ) );
221 void SettingsExportContext::EndElement( const bool i_bIgnoreWhitespace )
223 if ( i_bIgnoreWhitespace )
224 m_rDelegator.ignorableWhitespace( " " );
225 m_rDelegator.endElement();
228 void SettingsExportContext::Characters( const OUString& i_rCharacters )
230 m_rDelegator.characters( i_rCharacters );
233 Reference< css::uno::XComponentContext > SettingsExportContext::GetComponentContext() const
235 return m_rContext;
238 // SettingsDocumentHandler
239 typedef ::cppu::WeakImplHelper< XDocumentHandler
240 > SettingsDocumentHandler_Base;
241 class SettingsDocumentHandler : public SettingsDocumentHandler_Base
243 public:
244 SettingsDocumentHandler()
248 protected:
249 virtual ~SettingsDocumentHandler() override
253 public:
254 // XDocumentHandler
255 virtual void SAL_CALL startDocument( ) throw (SAXException, RuntimeException, std::exception) override;
256 virtual void SAL_CALL endDocument( ) throw (SAXException, RuntimeException, std::exception) override;
257 virtual void SAL_CALL startElement( const OUString& aName, const Reference< XAttributeList >& xAttribs ) throw (SAXException, RuntimeException, std::exception) override;
258 virtual void SAL_CALL endElement( const OUString& aName ) throw (SAXException, RuntimeException, std::exception) override;
259 virtual void SAL_CALL characters( const OUString& aChars ) throw (SAXException, RuntimeException, std::exception) override;
260 virtual void SAL_CALL ignorableWhitespace( const OUString& aWhitespaces ) throw (SAXException, RuntimeException, std::exception) override;
261 virtual void SAL_CALL processingInstruction( const OUString& aTarget, const OUString& aData ) throw (SAXException, RuntimeException, std::exception) override;
262 virtual void SAL_CALL setDocumentLocator( const Reference< XLocator >& xLocator ) throw (SAXException, RuntimeException, std::exception) override;
264 const ::comphelper::NamedValueCollection& getSettings() const { return m_aSettings; }
266 private:
267 ::std::stack< ::rtl::Reference< SettingsImport > > m_aStates;
268 ::comphelper::NamedValueCollection m_aSettings;
271 void SAL_CALL SettingsDocumentHandler::startDocument( ) throw (SAXException, RuntimeException, std::exception)
275 void SAL_CALL SettingsDocumentHandler::endDocument( ) throw (SAXException, RuntimeException, std::exception)
279 void SAL_CALL SettingsDocumentHandler::startElement( const OUString& i_Name, const Reference< XAttributeList >& i_Attribs ) throw (SAXException, RuntimeException, std::exception)
281 ::rtl::Reference< SettingsImport > pNewState;
283 if ( m_aStates.empty() )
285 if ( i_Name == "office:settings" )
287 pNewState = new OfficeSettingsImport( m_aSettings );
289 else
291 OSL_FAIL( "SettingsDocumentHandler::startElement: invalid settings file!" );
292 // Yes, that's not correct. Somebody could, in theory, give us a document which starts with "foo:settings",
293 // where "foo" is mapped to the proper namespace URL.
294 // However, there's no need to bother with this. The "recovery" sub storage we're recovering from is
295 // not part of ODF, so we can impose any format restrictions on it ...
298 else
300 ::rtl::Reference< SettingsImport > pCurrentState( m_aStates.top() );
301 pNewState = pCurrentState->nextState( i_Name );
304 ENSURE_OR_THROW( pNewState.is(), "no new state - aborting import" );
305 pNewState->startElement( i_Attribs );
307 m_aStates.push( pNewState );
310 void SAL_CALL SettingsDocumentHandler::endElement( const OUString& i_Name ) throw (SAXException, RuntimeException, std::exception)
312 ENSURE_OR_THROW( !m_aStates.empty(), "no active element" );
313 (void)i_Name;
315 ::rtl::Reference< SettingsImport > pCurrentState( m_aStates.top() );
316 pCurrentState->endElement();
317 m_aStates.pop();
320 void SAL_CALL SettingsDocumentHandler::characters( const OUString& i_Chars ) throw (SAXException, RuntimeException, std::exception)
322 ENSURE_OR_THROW( !m_aStates.empty(), "no active element" );
324 ::rtl::Reference< SettingsImport > pCurrentState( m_aStates.top() );
325 pCurrentState->characters( i_Chars );
328 void SAL_CALL SettingsDocumentHandler::ignorableWhitespace( const OUString& aWhitespaces ) throw (SAXException, RuntimeException, std::exception)
330 // ignore them - that's why they're called "ignorable"
331 (void)aWhitespaces;
334 void SAL_CALL SettingsDocumentHandler::processingInstruction( const OUString& i_Target, const OUString& i_Data ) throw (SAXException, RuntimeException, std::exception)
336 OSL_FAIL( "SettingsDocumentHandler::processingInstruction: unexpected ..." );
337 (void)i_Target;
338 (void)i_Data;
341 void SAL_CALL SettingsDocumentHandler::setDocumentLocator( const Reference< XLocator >& i_Locator ) throw (SAXException, RuntimeException, std::exception)
343 (void)i_Locator;
346 // SubComponentRecovery
347 const OUString SubComponentRecovery::getComponentsStorageName( const SubComponentType i_eType )
349 switch ( i_eType )
351 case FORM:
352 return OUString("forms");
353 case REPORT:
354 return OUString("reports");
355 case TABLE:
356 return OUString("tables");
357 case QUERY:
358 return OUString("queries");
359 case RELATION_DESIGN:
360 return OUString("relations");
361 default:
362 break;
365 OSL_FAIL( "SubComponentRecovery::getComponentsStorageName: unimplemented case!" );
366 return OUString();
369 void SubComponentRecovery::saveToRecoveryStorage( const Reference< XStorage >& i_rRecoveryStorage,
370 MapCompTypeToCompDescs& io_mapCompDescs )
372 if ( m_eType == UNKNOWN )
373 // quite fatal, but has already been reported (as assertion) before
374 return;
376 // open the sub storage for the given kind of components
377 const OUString& rStorageName( getComponentsStorageName( m_eType ) );
378 const Reference< XStorage > xComponentsStorage( i_rRecoveryStorage->openStorageElement(
379 rStorageName, ElementModes::READWRITE ), UNO_QUERY_THROW );
381 // find a free sub storage name, and create Yet Another Sub Storage
382 const OUString& rBaseName( lcl_getComponentStorageBaseName( m_eType ) );
383 const OUString sStorName = ::dbtools::createUniqueName( xComponentsStorage.get(), rBaseName );
384 const Reference< XStorage > xObjectStor( xComponentsStorage->openStorageElement(
385 sStorName, ElementModes::READWRITE ), UNO_QUERY_THROW );
387 switch ( m_eType )
389 case FORM:
390 case REPORT:
391 impl_saveSubDocument_throw( xObjectStor );
392 break;
394 case QUERY:
395 impl_saveQueryDesign_throw( xObjectStor );
396 break;
398 default:
399 // TODO
400 OSL_FAIL( "SubComponentRecoverys::saveToRecoveryStorage: unimplemented case!" );
401 break;
404 // commit the storage(s)
405 tools::stor::commitStorageIfWriteable( xObjectStor );
406 tools::stor::commitStorageIfWriteable( xComponentsStorage );
408 // remember the relationship from the component name to the storage name
409 MapStringToCompDesc& rMapCompDescs = io_mapCompDescs[ m_eType ];
410 OSL_ENSURE( rMapCompDescs.find( sStorName ) == rMapCompDescs.end(),
411 "SubComponentRecoverys::saveToRecoveryStorage: object name already used!" );
412 rMapCompDescs[ sStorName ] = m_aCompDesc;
415 void SubComponentRecovery::impl_identifyComponent_throw()
417 // ask the controller
418 Pair< sal_Int32, OUString > aComponentIdentity = m_xDocumentUI->identifySubComponent( m_xComponent );
419 m_eType = lcl_databaseObjectToSubComponentType( aComponentIdentity.First );
420 m_aCompDesc.sName = aComponentIdentity.Second;
422 // what the controller didn't give us is the information whether this is in edit mode or not ...
423 Reference< XModuleManager2 > xModuleManager( ModuleManager::create(m_rContext) );
424 const OUString sModuleIdentifier = xModuleManager->identify( m_xComponent );
426 switch ( m_eType )
428 case TABLE:
429 m_aCompDesc.bForEditing = sModuleIdentifier == "com.sun.star.sdb.TableDesign";
430 break;
432 case QUERY:
433 m_aCompDesc.bForEditing = sModuleIdentifier == "com.sun.star.sdb.QueryDesign";
434 break;
436 case REPORT:
437 if ( sModuleIdentifier == "com.sun.star.report.ReportDefinition" )
439 // it's an SRB report designer
440 m_aCompDesc.bForEditing = true;
441 break;
443 SAL_FALLTHROUGH;
445 case FORM:
446 m_aCompDesc.bForEditing = !lcl_determineReadOnly( m_xComponent );
447 break;
449 default:
450 if ( sModuleIdentifier == "com.sun.star.sdb.RelationDesign" )
452 m_eType = RELATION_DESIGN;
453 m_aCompDesc.bForEditing = true;
455 else
457 OSL_FAIL( "SubComponentRecovery::impl_identifyComponent_throw: couldn't classify the given sub component!" );
459 break;
462 SAL_WARN_IF( m_eType == UNKNOWN, "dbaccess",
463 "SubComponentRecovery::impl_identifyComponent_throw: couldn't classify the component!" );
466 void SubComponentRecovery::impl_saveQueryDesign_throw( const Reference< XStorage >& i_rObjectStorage )
468 ENSURE_OR_THROW( m_eType == QUERY, "illegal sub component type" );
469 ENSURE_OR_THROW( i_rObjectStorage.is(), "illegal storage" );
471 // retrieve the current query design (which might differ from what we can retrieve as ActiveCommand property, since
472 // the latter is updated only upon successful save of the design)
473 Reference< XPropertySet > xDesignerProps( m_xComponent, UNO_QUERY_THROW );
474 Sequence< PropertyValue > aCurrentQueryDesign;
475 OSL_VERIFY( xDesignerProps->getPropertyValue( "CurrentQueryDesign" ) >>= aCurrentQueryDesign );
477 // write the query design
478 StorageXMLOutputStream aDesignOutput( m_rContext, i_rObjectStorage, sSettingsStreamName );
479 SettingsExportContext aSettingsExportContext( m_rContext, aDesignOutput );
481 const OUString sWhitespace( " " );
483 aDesignOutput.startElement( "office:settings" );
484 aDesignOutput.ignorableWhitespace( sWhitespace );
486 XMLSettingsExportHelper aSettingsExporter( aSettingsExportContext );
487 aSettingsExporter.exportAllSettings( aCurrentQueryDesign, sCurrentQueryDesignName );
489 aDesignOutput.ignorableWhitespace( sWhitespace );
490 aDesignOutput.endElement();
491 aDesignOutput.close();
494 void SubComponentRecovery::impl_saveSubDocument_throw( const Reference< XStorage >& i_rObjectStorage )
496 ENSURE_OR_THROW( ( m_eType == FORM ) || ( m_eType == REPORT ), "illegal sub component type" );
497 ENSURE_OR_THROW( i_rObjectStorage.is(), "illegal storage" );
499 // store the document into the storage
500 Reference< XStorageBasedDocument > xStorageDocument( m_xComponent, UNO_QUERY_THROW );
501 xStorageDocument->storeToStorage( i_rObjectStorage, Sequence< PropertyValue >() );
504 Reference< XComponent > SubComponentRecovery::impl_recoverSubDocument_throw( const Reference< XStorage >& i_rRecoveryStorage,
505 const OUString& i_rComponentName, const bool i_bForEditing )
507 Reference< XComponent > xSubComponent;
508 Reference< XCommandProcessor > xDocDefinition;
510 ::comphelper::NamedValueCollection aLoadArgs;
511 aLoadArgs.put( "RecoveryStorage", i_rRecoveryStorage );
513 // load/create the sub component hidden. We'll show it when the main app window is shown.
514 aLoadArgs.put( "Hidden", true );
516 if ( !i_rComponentName.isEmpty() )
518 xDocDefinition = lcl_getSubComponentDef_nothrow( m_xDocumentUI, m_eType, i_rComponentName );
519 xSubComponent.set( m_xDocumentUI->loadComponentWithArguments(
520 m_eType,
521 i_rComponentName,
522 i_bForEditing,
523 aLoadArgs.getPropertyValues()
525 UNO_SET_THROW
528 else
530 Reference< XComponent > xDocDefComponent;
531 xSubComponent.set( m_xDocumentUI->createComponentWithArguments(
532 m_eType,
533 aLoadArgs.getPropertyValues(),
534 xDocDefComponent
536 UNO_SET_THROW
539 xDocDefinition.set( xDocDefComponent, UNO_QUERY );
540 OSL_ENSURE( xDocDefinition.is(), "DatabaseDocumentRecovery::recoverSubDocuments: loaded a form/report, but don't have a document definition?!" );
543 if ( xDocDefinition.is() )
545 Reference< XController > xController( m_xDocumentUI, UNO_QUERY_THROW );
546 Reference< XInterface > xLoader( *new SubComponentLoader( xController, xDocDefinition ) );
547 (void)xLoader;
550 return xSubComponent;
553 Reference< XComponent > SubComponentRecovery::impl_recoverQueryDesign_throw( const Reference< XStorage >& i_rRecoveryStorage,
554 const OUString& i_rComponentName, const bool i_bForEditing )
556 Reference< XComponent > xSubComponent;
558 // first read the settings query design settings from the storage
559 StorageXMLInputStream aDesignInput( m_rContext, i_rRecoveryStorage, sSettingsStreamName );
561 ::rtl::Reference< SettingsDocumentHandler > pDocHandler( new SettingsDocumentHandler );
562 aDesignInput.import( pDocHandler.get() );
564 const ::comphelper::NamedValueCollection& rSettings( pDocHandler->getSettings() );
565 const Any& aCurrentQueryDesign = rSettings.get( sCurrentQueryDesignName );
566 #if OSL_DEBUG_LEVEL > 0
567 Sequence< PropertyValue > aQueryDesignLayout;
568 OSL_VERIFY( aCurrentQueryDesign >>= aQueryDesignLayout );
569 #endif
571 // then load the query designer
572 ::comphelper::NamedValueCollection aLoadArgs;
573 aLoadArgs.put( "CurrentQueryDesign", aCurrentQueryDesign );
574 aLoadArgs.put( "Hidden", true );
576 if ( !i_rComponentName.isEmpty() )
578 xSubComponent.set( m_xDocumentUI->loadComponentWithArguments(
579 m_eType,
580 i_rComponentName,
581 i_bForEditing,
582 aLoadArgs.getPropertyValues()
584 UNO_SET_THROW
587 else
589 Reference< XComponent > xDummy;
590 xSubComponent.set( m_xDocumentUI->createComponentWithArguments(
591 m_eType,
592 aLoadArgs.getPropertyValues(),
593 xDummy
595 UNO_SET_THROW
599 Reference< XController > xController( m_xDocumentUI, UNO_QUERY_THROW );
600 Reference< XInterface > xLoader( *new SubComponentLoader( xController, xSubComponent ) );
601 (void)xLoader;
603 return xSubComponent;
606 Reference< XComponent > SubComponentRecovery::recoverFromStorage( const Reference< XStorage >& i_rRecoveryStorage,
607 const OUString& i_rComponentName, const bool i_bForEditing )
609 Reference< XComponent > xSubComponent;
610 switch ( m_eType )
612 case FORM:
613 case REPORT:
614 xSubComponent = impl_recoverSubDocument_throw( i_rRecoveryStorage, i_rComponentName, i_bForEditing );
615 break;
616 case QUERY:
617 xSubComponent = impl_recoverQueryDesign_throw( i_rRecoveryStorage, i_rComponentName, i_bForEditing );
618 break;
619 default:
620 OSL_FAIL( "SubComponentRecovery::recoverFromStorage: unimplemented case!" );
621 break;
623 return xSubComponent;
626 } // namespace dbaccess
628 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */