build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / dbaccess / source / core / recovery / dbdocrecovery.cxx
bloba19fb26b008c647989fae1776b85af2c47c5bcb0
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/io/TextInputStream.hpp>
30 #include <com/sun/star/util/XModifiable.hpp>
32 #include <comphelper/namedvaluecollection.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <tools/diagnose_ex.h>
36 #include <algorithm>
38 namespace dbaccess
41 using css::uno::Reference;
42 using css::uno::UNO_QUERY;
43 using css::uno::UNO_QUERY_THROW;
44 using css::uno::UNO_SET_THROW;
45 using css::uno::Exception;
46 using css::uno::Sequence;
47 using css::uno::XComponentContext;
48 using css::embed::XStorage;
49 using css::frame::XController;
50 using css::sdb::application::XDatabaseDocumentUI;
51 using css::lang::XComponent;
52 using css::io::XStream;
53 using css::io::TextInputStream;
54 using css::io::XTextInputStream2;
55 using css::util::XModifiable;
57 namespace ElementModes = css::embed::ElementModes;
59 // helpers
60 namespace
62 void lcl_getPersistentRepresentation( const MapStringToCompDesc::value_type& i_rComponentDesc, OUStringBuffer& o_rBuffer )
64 o_rBuffer.append( i_rComponentDesc.first );
65 o_rBuffer.append( '=' );
66 o_rBuffer.append( i_rComponentDesc.second.sName );
67 o_rBuffer.append( ',' );
68 o_rBuffer.append( sal_Unicode( i_rComponentDesc.second.bForEditing ? '1' : '0' ) );
71 bool lcl_extractCompDesc( const OUString& i_rIniLine, OUString& o_rStorName, SubComponentDescriptor& o_rCompDesc )
73 const sal_Int32 nEqualSignPos = i_rIniLine.indexOf( '=' );
74 if ( nEqualSignPos < 1 )
76 OSL_FAIL( "lcl_extractCompDesc: invalid map file entry - unexpected pos of '='" );
77 return false;
79 o_rStorName = i_rIniLine.copy( 0, nEqualSignPos );
81 const sal_Int32 nCommaPos = i_rIniLine.lastIndexOf( ',' );
82 if ( nCommaPos != i_rIniLine.getLength() - 2 )
84 OSL_FAIL( "lcl_extractCompDesc: invalid map file entry - unexpected pos of ','" );
85 return false;
87 o_rCompDesc.sName = i_rIniLine.copy( nEqualSignPos + 1, nCommaPos - nEqualSignPos - 1 );
88 o_rCompDesc.bForEditing = ( i_rIniLine[ nCommaPos + 1 ] == '1' );
89 return true;
92 static const char sRecoveryDataSubStorageName[] = "recovery";
94 static const char sObjectMapStreamName[] = "storage-component-map.ini";
96 void lcl_writeObjectMap_throw( const Reference<XComponentContext> & i_rContext, const Reference< XStorage >& i_rStorage,
97 const MapStringToCompDesc& i_mapStorageToCompDesc )
99 if ( i_mapStorageToCompDesc.empty() )
100 // nothing to do
101 return;
103 StorageTextOutputStream aTextOutput( i_rContext, i_rStorage, sObjectMapStreamName );
105 aTextOutput.writeLine( "[storages]" );
107 for ( MapStringToCompDesc::const_iterator stor = i_mapStorageToCompDesc.begin();
108 stor != i_mapStorageToCompDesc.end();
109 ++stor
112 OUStringBuffer aLine;
113 lcl_getPersistentRepresentation( *stor, aLine );
115 aTextOutput.writeLine( aLine.makeStringAndClear() );
118 aTextOutput.writeLine();
121 bool lcl_isSectionStart( const OUString& i_rIniLine, OUString& o_rSectionName )
123 const sal_Int32 nLen = i_rIniLine.getLength();
124 if ( i_rIniLine.startsWith("[") && i_rIniLine.endsWith("]") )
126 o_rSectionName = i_rIniLine.copy( 1, nLen -2 );
127 return true;
129 return false;
132 void lcl_stripTrailingLineFeed( OUString& io_rLine )
134 const sal_Int32 nLen = io_rLine.getLength();
135 if ( io_rLine.endsWith("\n") )
136 io_rLine = io_rLine.copy( 0, nLen - 1 );
139 void lcl_readObjectMap_throw( const Reference<XComponentContext> & i_rxContext, const Reference< XStorage >& i_rStorage,
140 MapStringToCompDesc& o_mapStorageToObjectName )
142 ENSURE_OR_THROW( i_rStorage.is(), "invalid storage" );
143 if ( !i_rStorage->hasByName( sObjectMapStreamName ) )
144 { // nothing to do, though suspicious
145 OSL_FAIL( "lcl_readObjectMap_throw: if there's no map file, then there's expected to be no storage, too!" );
146 return;
149 Reference< XStream > xIniStream( i_rStorage->openStreamElement(
150 sObjectMapStreamName, ElementModes::READ ), UNO_SET_THROW );
152 Reference< XTextInputStream2 > xTextInput = TextInputStream::create( i_rxContext );
153 xTextInput->setEncoding( "UTF-8" );
154 xTextInput->setInputStream( xIniStream->getInputStream() );
156 OUString sCurrentSection;
157 bool bCurrentSectionIsKnownToBeUnsupported = true;
158 while ( !xTextInput->isEOF() )
160 OUString sLine = xTextInput->readLine();
161 lcl_stripTrailingLineFeed( sLine );
163 if ( sLine.isEmpty() )
164 continue;
166 if ( lcl_isSectionStart( sLine, sCurrentSection ) )
168 bCurrentSectionIsKnownToBeUnsupported = false;
169 continue;
172 if ( bCurrentSectionIsKnownToBeUnsupported )
173 continue;
175 // the only section we support so far is "storages"
176 if ( sCurrentSection != "storages" )
178 bCurrentSectionIsKnownToBeUnsupported = true;
179 continue;
182 OUString sStorageName;
183 SubComponentDescriptor aCompDesc;
184 if ( !lcl_extractCompDesc( sLine, sStorageName, aCompDesc ) )
185 continue;
186 o_mapStorageToObjectName[ sStorageName ] = aCompDesc;
190 void lcl_markModified( const Reference< XComponent >& i_rSubComponent )
192 const Reference< XModifiable > xModify( i_rSubComponent, UNO_QUERY );
193 if ( !xModify.is() )
195 OSL_FAIL( "lcl_markModified: unhandled case!" );
196 return;
199 xModify->setModified( true );
203 // DatabaseDocumentRecovery_Data
204 struct DatabaseDocumentRecovery_Data
206 const Reference<XComponentContext> aContext;
208 explicit DatabaseDocumentRecovery_Data( const Reference<XComponentContext> & i_rContext )
209 :aContext( i_rContext )
214 // DatabaseDocumentRecovery
215 DatabaseDocumentRecovery::DatabaseDocumentRecovery( const Reference<XComponentContext> & i_rContext )
216 :m_pData( new DatabaseDocumentRecovery_Data( i_rContext ) )
220 DatabaseDocumentRecovery::~DatabaseDocumentRecovery()
224 void DatabaseDocumentRecovery::saveModifiedSubComponents( const Reference< XStorage >& i_rTargetStorage,
225 const ::std::vector< Reference< XController > >& i_rControllers )
227 ENSURE_OR_THROW( i_rTargetStorage.is(), "invalid document storage" );
229 // create a sub storage for recovery data
230 if ( i_rTargetStorage->hasByName( sRecoveryDataSubStorageName ) )
231 i_rTargetStorage->removeElement( sRecoveryDataSubStorageName );
232 Reference< XStorage > xRecoveryStorage = i_rTargetStorage->openStorageElement( sRecoveryDataSubStorageName, ElementModes::READWRITE );
234 // store recovery data for open sub components of the given controller(s)
235 if ( !i_rControllers.empty() )
237 ENSURE_OR_THROW( i_rControllers.size() == 1, "can't handle more than one controller" );
238 // At the moment, there can be only one view to a database document. If we ever allow for more than this,
239 // then we need a concept for sub documents opened from different controllers (i.e. two document views,
240 // and the user opens the very same form in both views). And depending on this, we need a concept for
241 // how those are saved to the recovery file.
243 MapCompTypeToCompDescs aMapCompDescs;
245 for ( ::std::vector< Reference< XController > >::const_iterator ctrl = i_rControllers.begin();
246 ctrl != i_rControllers.end();
247 ++ctrl
250 Reference< XDatabaseDocumentUI > xDatabaseUI( *ctrl, UNO_QUERY_THROW );
251 Sequence< Reference< XComponent > > aComponents( xDatabaseUI->getSubComponents() );
253 const Reference< XComponent >* component = aComponents.getConstArray();
254 const Reference< XComponent >* componentEnd = aComponents.getConstArray() + aComponents.getLength();
255 for ( ; component != componentEnd; ++component )
257 SubComponentRecovery aComponentRecovery( m_pData->aContext, xDatabaseUI, *component );
258 aComponentRecovery.saveToRecoveryStorage( xRecoveryStorage, aMapCompDescs );
262 for ( MapCompTypeToCompDescs::const_iterator map = aMapCompDescs.begin();
263 map != aMapCompDescs.end();
264 ++map
267 Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
268 SubComponentRecovery::getComponentsStorageName( map->first ), ElementModes::WRITE | ElementModes::NOCREATE ) );
269 lcl_writeObjectMap_throw( m_pData->aContext, xComponentsStor, map->second );
270 tools::stor::commitStorageIfWriteable( xComponentsStor );
274 // commit the recovery storage
275 tools::stor::commitStorageIfWriteable( xRecoveryStorage );
278 void DatabaseDocumentRecovery::recoverSubDocuments( const Reference< XStorage >& i_rDocumentStorage,
279 const Reference< XController >& i_rTargetController )
281 ENSURE_OR_THROW( i_rDocumentStorage.is(), "illegal document storage" );
282 Reference< XDatabaseDocumentUI > xDocumentUI( i_rTargetController, UNO_QUERY_THROW );
284 if ( !i_rDocumentStorage->hasByName( sRecoveryDataSubStorageName ) )
285 // that's allowed
286 return;
288 // the "recovery" sub storage
289 Reference< XStorage > xRecoveryStorage = i_rDocumentStorage->openStorageElement( sRecoveryDataSubStorageName, ElementModes::READ );
291 // read the map from sub storages to object names
292 MapCompTypeToCompDescs aMapCompDescs;
293 const SubComponentType aKnownTypes[] = { TABLE, QUERY, FORM, REPORT, RELATION_DESIGN };
294 for (SubComponentType aKnownType : aKnownTypes)
296 if ( !xRecoveryStorage->hasByName( SubComponentRecovery::getComponentsStorageName( aKnownType ) ) )
297 continue;
299 Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
300 SubComponentRecovery::getComponentsStorageName( aKnownType ), ElementModes::READ ) );
301 lcl_readObjectMap_throw( m_pData->aContext, xComponentsStor, aMapCompDescs[ aKnownType ] );
302 xComponentsStor->dispose();
305 // recover all sub components as indicated by the map
306 for ( MapCompTypeToCompDescs::const_iterator map = aMapCompDescs.begin();
307 map != aMapCompDescs.end();
308 ++map
311 const SubComponentType eComponentType = map->first;
313 // the storage for all components of the current type
314 Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
315 SubComponentRecovery::getComponentsStorageName( eComponentType ), ElementModes::READ ), UNO_QUERY_THROW );
317 // loop through all components of this type
318 for ( MapStringToCompDesc::const_iterator stor = map->second.begin();
319 stor != map->second.end();
320 ++stor
323 const OUString sComponentName( stor->second.sName );
324 if ( !xComponentsStor->hasByName( stor->first ) )
326 SAL_WARN( "dbaccess",
327 "DatabaseDocumentRecovery::recoverSubDocuments: inconsistent recovery storage: storage '" <<
328 stor->first <<
329 "' not found in '" <<
330 SubComponentRecovery::getComponentsStorageName( eComponentType ) <<
331 "', but required per map file!" );
332 continue;
335 // the controller needs to have a connection to be able to open sub components
336 if ( !xDocumentUI->isConnected() )
337 xDocumentUI->connect();
339 // recover the single component
340 Reference< XStorage > xCompStor( xComponentsStor->openStorageElement( stor->first, ElementModes::READ ) );
341 SubComponentRecovery aComponentRecovery( m_pData->aContext, xDocumentUI, eComponentType );
342 Reference< XComponent > xSubComponent( aComponentRecovery.recoverFromStorage( xCompStor, sComponentName, stor->second.bForEditing ) );
344 // at the moment, we only store, during session save, sub components which are modified. So, set this
345 // recovered sub component to "modified", too.
346 lcl_markModified( xSubComponent );
349 xComponentsStor->dispose();
352 xRecoveryStorage->dispose();
354 // now that we successfully recovered, removed the "recovery" sub storage
357 i_rDocumentStorage->removeElement( sRecoveryDataSubStorageName );
359 catch( const Exception& )
361 DBG_UNHANDLED_EXCEPTION();
365 } // namespace dbaccess
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */