fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / dbaccess / source / core / recovery / dbdocrecovery.cxx
blobe49def107a854046e77571d317f354c72b85da9f
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 "recovery/dbdocrecovery.hxx"
21 #include "sdbcoretools.hxx"
22 #include "storagetextstream.hxx"
23 #include "subcomponentrecovery.hxx"
24 #include "subcomponents.hxx"
25 #include "dbastrings.hrc"
27 #include <com/sun/star/sdb/application/XDatabaseDocumentUI.hpp>
28 #include <com/sun/star/embed/ElementModes.hpp>
29 #include <com/sun/star/document/XStorageBasedDocument.hpp>
30 #include <com/sun/star/io/XTextOutputStream.hpp>
31 #include <com/sun/star/io/TextInputStream.hpp>
32 #include <com/sun/star/io/XActiveDataSource.hpp>
33 #include <com/sun/star/io/XActiveDataSink.hpp>
34 #include <com/sun/star/util/XModifiable.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <comphelper/namedvaluecollection.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <tools/diagnose_ex.h>
41 #include <algorithm>
43 namespace dbaccess
46 using css::uno::Reference;
47 using css::uno::XInterface;
48 using css::uno::UNO_QUERY;
49 using css::uno::UNO_QUERY_THROW;
50 using css::uno::UNO_SET_THROW;
51 using css::uno::Exception;
52 using css::uno::RuntimeException;
53 using css::uno::Any;
54 using css::uno::makeAny;
55 using css::uno::Sequence;
56 using css::uno::Type;
57 using css::uno::XComponentContext;
58 using css::embed::XStorage;
59 using css::frame::XController;
60 using css::sdb::application::XDatabaseDocumentUI;
61 using css::lang::XComponent;
62 using css::document::XStorageBasedDocument;
63 using css::beans::PropertyValue;
64 using css::io::XStream;
65 using css::io::XTextOutputStream;
66 using css::io::XActiveDataSource;
67 using css::io::TextInputStream;
68 using css::io::XTextInputStream2;
69 using css::io::XActiveDataSink;
70 using css::frame::XModel;
71 using css::util::XModifiable;
72 using css::beans::XPropertySet;
74 namespace ElementModes = css::embed::ElementModes;
76 // helpers
77 namespace
79 static void lcl_getPersistentRepresentation( const MapStringToCompDesc::value_type& i_rComponentDesc, OUStringBuffer& o_rBuffer )
81 o_rBuffer.append( i_rComponentDesc.first );
82 o_rBuffer.append( '=' );
83 o_rBuffer.append( i_rComponentDesc.second.sName );
84 o_rBuffer.append( ',' );
85 o_rBuffer.append( sal_Unicode( i_rComponentDesc.second.bForEditing ? '1' : '0' ) );
88 static bool lcl_extractCompDesc( const OUString& i_rIniLine, OUString& o_rStorName, SubComponentDescriptor& o_rCompDesc )
90 const sal_Int32 nEqualSignPos = i_rIniLine.indexOf( '=' );
91 if ( nEqualSignPos < 1 )
93 OSL_FAIL( "lcl_extractCompDesc: invalid map file entry - unexpected pos of '='" );
94 return false;
96 o_rStorName = i_rIniLine.copy( 0, nEqualSignPos );
98 const sal_Int32 nCommaPos = i_rIniLine.lastIndexOf( ',' );
99 if ( nCommaPos != i_rIniLine.getLength() - 2 )
101 OSL_FAIL( "lcl_extractCompDesc: invalid map file entry - unexpected pos of ','" );
102 return false;
104 o_rCompDesc.sName = i_rIniLine.copy( nEqualSignPos + 1, nCommaPos - nEqualSignPos - 1 );
105 o_rCompDesc.bForEditing = ( i_rIniLine[ nCommaPos + 1 ] == '1' );
106 return true;
109 static const char sRecoveryDataSubStorageName[] = "recovery";
111 static const char sObjectMapStreamName[] = "storage-component-map.ini";
113 static void lcl_writeObjectMap_throw( const Reference<XComponentContext> & i_rContext, const Reference< XStorage >& i_rStorage,
114 const MapStringToCompDesc& i_mapStorageToCompDesc )
116 if ( i_mapStorageToCompDesc.empty() )
117 // nothing to do
118 return;
120 StorageTextOutputStream aTextOutput( i_rContext, i_rStorage, sObjectMapStreamName );
122 aTextOutput.writeLine( "[storages]" );
124 for ( MapStringToCompDesc::const_iterator stor = i_mapStorageToCompDesc.begin();
125 stor != i_mapStorageToCompDesc.end();
126 ++stor
129 OUStringBuffer aLine;
130 lcl_getPersistentRepresentation( *stor, aLine );
132 aTextOutput.writeLine( aLine.makeStringAndClear() );
135 aTextOutput.writeLine();
138 static bool lcl_isSectionStart( const OUString& i_rIniLine, OUString& o_rSectionName )
140 const sal_Int32 nLen = i_rIniLine.getLength();
141 if ( i_rIniLine.startsWith("[") && i_rIniLine.endsWith("]") )
143 o_rSectionName = i_rIniLine.copy( 1, nLen -2 );
144 return true;
146 return false;
149 static void lcl_stripTrailingLineFeed( OUString& io_rLine )
151 const sal_Int32 nLen = io_rLine.getLength();
152 if ( io_rLine.endsWith("\n") )
153 io_rLine = io_rLine.copy( 0, nLen - 1 );
156 static void lcl_readObjectMap_throw( const Reference<XComponentContext> & i_rxContext, const Reference< XStorage >& i_rStorage,
157 MapStringToCompDesc& o_mapStorageToObjectName )
159 ENSURE_OR_THROW( i_rStorage.is(), "invalid storage" );
160 if ( !i_rStorage->hasByName( sObjectMapStreamName ) )
161 { // nothing to do, though suspicious
162 OSL_FAIL( "lcl_readObjectMap_throw: if there's no map file, then there's expected to be no storage, too!" );
163 return;
166 Reference< XStream > xIniStream( i_rStorage->openStreamElement(
167 sObjectMapStreamName, ElementModes::READ ), UNO_SET_THROW );
169 Reference< XTextInputStream2 > xTextInput = TextInputStream::create( i_rxContext );
170 xTextInput->setEncoding( "UTF-8" );
171 xTextInput->setInputStream( xIniStream->getInputStream() );
173 OUString sCurrentSection;
174 bool bCurrentSectionIsKnownToBeUnsupported = true;
175 while ( !xTextInput->isEOF() )
177 OUString sLine = xTextInput->readLine();
178 lcl_stripTrailingLineFeed( sLine );
180 if ( sLine.isEmpty() )
181 continue;
183 if ( lcl_isSectionStart( sLine, sCurrentSection ) )
185 bCurrentSectionIsKnownToBeUnsupported = false;
186 continue;
189 if ( bCurrentSectionIsKnownToBeUnsupported )
190 continue;
192 // the only section we support so far is "storages"
193 if ( sCurrentSection != "storages" )
195 bCurrentSectionIsKnownToBeUnsupported = true;
196 continue;
199 OUString sStorageName;
200 SubComponentDescriptor aCompDesc;
201 if ( !lcl_extractCompDesc( sLine, sStorageName, aCompDesc ) )
202 continue;
203 o_mapStorageToObjectName[ sStorageName ] = aCompDesc;
207 static void lcl_markModified( const Reference< XComponent >& i_rSubComponent )
209 const Reference< XModifiable > xModify( i_rSubComponent, UNO_QUERY );
210 if ( !xModify.is() )
212 OSL_FAIL( "lcl_markModified: unhandled case!" );
213 return;
216 xModify->setModified( sal_True );
220 // DatabaseDocumentRecovery_Data
221 struct DBACCESS_DLLPRIVATE DatabaseDocumentRecovery_Data
223 const Reference<XComponentContext> aContext;
225 DatabaseDocumentRecovery_Data( const Reference<XComponentContext> & i_rContext )
226 :aContext( i_rContext )
231 // DatabaseDocumentRecovery
232 DatabaseDocumentRecovery::DatabaseDocumentRecovery( const Reference<XComponentContext> & i_rContext )
233 :m_pData( new DatabaseDocumentRecovery_Data( i_rContext ) )
237 DatabaseDocumentRecovery::~DatabaseDocumentRecovery()
241 void DatabaseDocumentRecovery::saveModifiedSubComponents( const Reference< XStorage >& i_rTargetStorage,
242 const ::std::vector< Reference< XController > >& i_rControllers )
244 ENSURE_OR_THROW( i_rTargetStorage.is(), "invalid document storage" );
246 // create a sub storage for recovery data
247 if ( i_rTargetStorage->hasByName( sRecoveryDataSubStorageName ) )
248 i_rTargetStorage->removeElement( sRecoveryDataSubStorageName );
249 Reference< XStorage > xRecoveryStorage = i_rTargetStorage->openStorageElement( sRecoveryDataSubStorageName, ElementModes::READWRITE );
251 // store recovery data for open sub components of the given controller(s)
252 if ( !i_rControllers.empty() )
254 ENSURE_OR_THROW( i_rControllers.size() == 1, "can't handle more than one controller" );
255 // At the moment, there can be only one view to a database document. If we ever allow for more than this,
256 // then we need a concept for sub documents opened from different controllers (i.e. two document views,
257 // and the user opens the very same form in both views). And depending on this, we need a concept for
258 // how those are saved to the recovery file.
260 MapCompTypeToCompDescs aMapCompDescs;
262 for ( ::std::vector< Reference< XController > >::const_iterator ctrl = i_rControllers.begin();
263 ctrl != i_rControllers.end();
264 ++ctrl
267 Reference< XDatabaseDocumentUI > xDatabaseUI( *ctrl, UNO_QUERY_THROW );
268 Sequence< Reference< XComponent > > aComponents( xDatabaseUI->getSubComponents() );
270 const Reference< XComponent >* component = aComponents.getConstArray();
271 const Reference< XComponent >* componentEnd = aComponents.getConstArray() + aComponents.getLength();
272 for ( ; component != componentEnd; ++component )
274 SubComponentRecovery aComponentRecovery( m_pData->aContext, xDatabaseUI, *component );
275 aComponentRecovery.saveToRecoveryStorage( xRecoveryStorage, aMapCompDescs );
279 for ( MapCompTypeToCompDescs::const_iterator map = aMapCompDescs.begin();
280 map != aMapCompDescs.end();
281 ++map
284 Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
285 SubComponentRecovery::getComponentsStorageName( map->first ), ElementModes::WRITE | ElementModes::NOCREATE ) );
286 lcl_writeObjectMap_throw( m_pData->aContext, xComponentsStor, map->second );
287 tools::stor::commitStorageIfWriteable( xComponentsStor );
291 // commit the recovery storage
292 tools::stor::commitStorageIfWriteable( xRecoveryStorage );
295 void DatabaseDocumentRecovery::recoverSubDocuments( const Reference< XStorage >& i_rDocumentStorage,
296 const Reference< XController >& i_rTargetController )
298 ENSURE_OR_THROW( i_rDocumentStorage.is(), "illegal document storage" );
299 Reference< XDatabaseDocumentUI > xDocumentUI( i_rTargetController, UNO_QUERY_THROW );
301 if ( !i_rDocumentStorage->hasByName( sRecoveryDataSubStorageName ) )
302 // that's allowed
303 return;
305 // the "recovery" sub storage
306 Reference< XStorage > xRecoveryStorage = i_rDocumentStorage->openStorageElement( sRecoveryDataSubStorageName, ElementModes::READ );
308 // read the map from sub storages to object names
309 MapCompTypeToCompDescs aMapCompDescs;
310 SubComponentType aKnownTypes[] = { TABLE, QUERY, FORM, REPORT, RELATION_DESIGN };
311 for ( size_t i = 0; i < sizeof( aKnownTypes ) / sizeof( aKnownTypes[0] ); ++i )
313 if ( !xRecoveryStorage->hasByName( SubComponentRecovery::getComponentsStorageName( aKnownTypes[i] ) ) )
314 continue;
316 Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
317 SubComponentRecovery::getComponentsStorageName( aKnownTypes[i] ), ElementModes::READ ) );
318 lcl_readObjectMap_throw( m_pData->aContext, xComponentsStor, aMapCompDescs[ aKnownTypes[i] ] );
319 xComponentsStor->dispose();
322 // recover all sub components as indicated by the map
323 for ( MapCompTypeToCompDescs::const_iterator map = aMapCompDescs.begin();
324 map != aMapCompDescs.end();
325 ++map
328 const SubComponentType eComponentType = map->first;
330 // the storage for all components of the current type
331 Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
332 SubComponentRecovery::getComponentsStorageName( eComponentType ), ElementModes::READ ), UNO_QUERY_THROW );
334 // loop through all components of this type
335 for ( MapStringToCompDesc::const_iterator stor = map->second.begin();
336 stor != map->second.end();
337 ++stor
340 const OUString sComponentName( stor->second.sName );
341 if ( !xComponentsStor->hasByName( stor->first ) )
343 SAL_WARN( "dbaccess",
344 "DatabaseDocumentRecovery::recoverSubDocuments: inconsistent recovery storage: storage '" <<
345 stor->first <<
346 "' not found in '" <<
347 SubComponentRecovery::getComponentsStorageName( eComponentType ) <<
348 "', but required per map file!" );
349 continue;
352 // the controller needs to have a connection to be able to open sub components
353 if ( !xDocumentUI->isConnected() )
354 xDocumentUI->connect();
356 // recover the single component
357 Reference< XStorage > xCompStor( xComponentsStor->openStorageElement( stor->first, ElementModes::READ ) );
358 SubComponentRecovery aComponentRecovery( m_pData->aContext, xDocumentUI, eComponentType );
359 Reference< XComponent > xSubComponent( aComponentRecovery.recoverFromStorage( xCompStor, sComponentName, stor->second.bForEditing ) );
361 // at the moment, we only store, during session save, sub components which are modified. So, set this
362 // recovered sub component to "modified", too.
363 lcl_markModified( xSubComponent );
366 xComponentsStor->dispose();
369 xRecoveryStorage->dispose();
371 // now that we successfully recovered, removed the "recovery" sub storage
374 i_rDocumentStorage->removeElement( sRecoveryDataSubStorageName );
376 catch( const Exception& )
378 DBG_UNHANDLED_EXCEPTION();
382 } // namespace dbaccess
384 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */