1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "recovery/dbdocrecovery.hxx"
22 #include "sdbcoretools.hxx"
23 #include "storagetextstream.hxx"
24 #include "subcomponentrecovery.hxx"
25 #include "subcomponents.hxx"
26 #include "dbastrings.hrc"
28 #include <com/sun/star/sdb/application/XDatabaseDocumentUI.hpp>
29 #include <com/sun/star/embed/ElementModes.hpp>
30 #include <com/sun/star/document/XStorageBasedDocument.hpp>
31 #include <com/sun/star/io/XTextOutputStream.hpp>
32 #include <com/sun/star/io/TextInputStream.hpp>
33 #include <com/sun/star/io/XActiveDataSource.hpp>
34 #include <com/sun/star/io/XActiveDataSink.hpp>
35 #include <com/sun/star/util/XModifiable.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <comphelper/namedvaluecollection.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include <tools/diagnose_ex.h>
44 //........................................................................
47 //........................................................................
49 using ::com::sun::star::uno::Reference
;
50 using ::com::sun::star::uno::XInterface
;
51 using ::com::sun::star::uno::UNO_QUERY
;
52 using ::com::sun::star::uno::UNO_QUERY_THROW
;
53 using ::com::sun::star::uno::UNO_SET_THROW
;
54 using ::com::sun::star::uno::Exception
;
55 using ::com::sun::star::uno::RuntimeException
;
56 using ::com::sun::star::uno::Any
;
57 using ::com::sun::star::uno::makeAny
;
58 using ::com::sun::star::uno::Sequence
;
59 using ::com::sun::star::uno::Type
;
60 using ::com::sun::star::uno::XComponentContext
;
61 using ::com::sun::star::embed::XStorage
;
62 using ::com::sun::star::frame::XController
;
63 using ::com::sun::star::sdb::application::XDatabaseDocumentUI
;
64 using ::com::sun::star::lang::XComponent
;
65 using ::com::sun::star::document::XStorageBasedDocument
;
66 using ::com::sun::star::beans::PropertyValue
;
67 using ::com::sun::star::io::XStream
;
68 using ::com::sun::star::io::XTextOutputStream
;
69 using ::com::sun::star::io::XActiveDataSource
;
70 using ::com::sun::star::io::TextInputStream
;
71 using ::com::sun::star::io::XTextInputStream2
;
72 using ::com::sun::star::io::XActiveDataSink
;
73 using ::com::sun::star::frame::XModel
;
74 using ::com::sun::star::util::XModifiable
;
75 using ::com::sun::star::beans::XPropertySet
;
77 namespace ElementModes
= ::com::sun::star::embed::ElementModes
;
79 //====================================================================
81 //====================================================================
84 // .........................................................................
85 static void lcl_getPersistentRepresentation( const MapStringToCompDesc::value_type
& i_rComponentDesc
, OUStringBuffer
& o_rBuffer
)
87 o_rBuffer
.append( i_rComponentDesc
.first
);
88 o_rBuffer
.append( sal_Unicode( '=' ) );
89 o_rBuffer
.append( i_rComponentDesc
.second
.sName
);
90 o_rBuffer
.append( sal_Unicode( ',' ) );
91 o_rBuffer
.append( sal_Unicode( i_rComponentDesc
.second
.bForEditing
? '1' : '0' ) );
94 // .........................................................................
95 static bool lcl_extractCompDesc( const OUString
& i_rIniLine
, OUString
& o_rStorName
, SubComponentDescriptor
& o_rCompDesc
)
97 const sal_Int32 nEqualSignPos
= i_rIniLine
.indexOf( sal_Unicode( '=' ) );
98 if ( nEqualSignPos
< 1 )
100 OSL_FAIL( "lcl_extractCompDesc: invalid map file entry - unexpected pos of '='" );
103 o_rStorName
= i_rIniLine
.copy( 0, nEqualSignPos
);
105 const sal_Int32 nCommaPos
= i_rIniLine
.lastIndexOf( sal_Unicode( ',' ) );
106 if ( nCommaPos
!= i_rIniLine
.getLength() - 2 )
108 OSL_FAIL( "lcl_extractCompDesc: invalid map file entry - unexpected pos of ','" );
111 o_rCompDesc
.sName
= i_rIniLine
.copy( nEqualSignPos
+ 1, nCommaPos
- nEqualSignPos
- 1 );
112 o_rCompDesc
.bForEditing
= ( i_rIniLine
.getStr()[ nCommaPos
+ 1 ] == '1' );
116 static const OUString
& lcl_getRecoveryDataSubStorageName()
118 static const OUString
s_sRecDataStorName( "recovery" );
119 return s_sRecDataStorName
;
122 static const OUString
& lcl_getObjectMapStreamName()
124 static const OUString
s_sObjectMapStreamName( "storage-component-map.ini" );
125 return s_sObjectMapStreamName
;
128 static const OUString
& lcl_getMapStreamEncodingName()
130 static const OUString
s_sMapStreamEncodingName( "UTF-8" );
131 return s_sMapStreamEncodingName
;
134 static void lcl_writeObjectMap_throw( const Reference
<XComponentContext
> & i_rContext
, const Reference
< XStorage
>& i_rStorage
,
135 const MapStringToCompDesc
& i_mapStorageToCompDesc
)
137 if ( i_mapStorageToCompDesc
.empty() )
141 StorageTextOutputStream
aTextOutput( i_rContext
, i_rStorage
, lcl_getObjectMapStreamName() );
143 aTextOutput
.writeLine( "[storages]" );
145 for ( MapStringToCompDesc::const_iterator stor
= i_mapStorageToCompDesc
.begin();
146 stor
!= i_mapStorageToCompDesc
.end();
150 OUStringBuffer aLine
;
151 lcl_getPersistentRepresentation( *stor
, aLine
);
153 aTextOutput
.writeLine( aLine
.makeStringAndClear() );
156 aTextOutput
.writeLine();
159 // .........................................................................
160 static bool lcl_isSectionStart( const OUString
& i_rIniLine
, OUString
& o_rSectionName
)
162 const sal_Int32 nLen
= i_rIniLine
.getLength();
163 if ( ( nLen
> 0 ) && ( i_rIniLine
.getStr()[0] == '[' ) && ( i_rIniLine
.getStr()[ nLen
- 1 ] == ']' ) )
165 o_rSectionName
= i_rIniLine
.copy( 1, nLen
-2 );
171 // .........................................................................
172 static void lcl_stripTrailingLineFeed( OUString
& io_rLine
)
174 const sal_Int32 nLen
= io_rLine
.getLength();
175 if ( ( nLen
> 0 ) && ( io_rLine
.getStr()[ nLen
- 1 ] == '\n' ) )
176 io_rLine
= io_rLine
.copy( 0, nLen
- 1 );
179 // .........................................................................
180 static void lcl_readObjectMap_throw( const Reference
<XComponentContext
> & i_rxContext
, const Reference
< XStorage
>& i_rStorage
,
181 MapStringToCompDesc
& o_mapStorageToObjectName
)
183 ENSURE_OR_THROW( i_rStorage
.is(), "invalid storage" );
184 if ( !i_rStorage
->hasByName( lcl_getObjectMapStreamName() ) )
185 { // nothing to do, though suspicious
186 OSL_FAIL( "lcl_readObjectMap_throw: if there's no map file, then there's expected to be no storage, too!" );
190 Reference
< XStream
> xIniStream( i_rStorage
->openStreamElement(
191 lcl_getObjectMapStreamName(), ElementModes::READ
), UNO_SET_THROW
);
193 Reference
< XTextInputStream2
> xTextInput
= TextInputStream::create( i_rxContext
);
194 xTextInput
->setEncoding( lcl_getMapStreamEncodingName() );
195 xTextInput
->setInputStream( xIniStream
->getInputStream() );
197 OUString sCurrentSection
;
198 bool bCurrentSectionIsKnownToBeUnsupported
= true;
199 while ( !xTextInput
->isEOF() )
201 OUString sLine
= xTextInput
->readLine();
202 lcl_stripTrailingLineFeed( sLine
);
204 if ( sLine
.isEmpty() )
207 if ( lcl_isSectionStart( sLine
, sCurrentSection
) )
209 bCurrentSectionIsKnownToBeUnsupported
= false;
213 if ( bCurrentSectionIsKnownToBeUnsupported
)
216 // the only section we support so far is "storages"
217 if ( sCurrentSection
!= "storages" )
219 bCurrentSectionIsKnownToBeUnsupported
= true;
223 OUString sStorageName
;
224 SubComponentDescriptor aCompDesc
;
225 if ( !lcl_extractCompDesc( sLine
, sStorageName
, aCompDesc
) )
227 o_mapStorageToObjectName
[ sStorageName
] = aCompDesc
;
231 // .........................................................................
232 static void lcl_markModified( const Reference
< XComponent
>& i_rSubComponent
)
234 const Reference
< XModifiable
> xModify( i_rSubComponent
, UNO_QUERY
);
237 OSL_FAIL( "lcl_markModified: unhandled case!" );
241 xModify
->setModified( sal_True
);
245 //====================================================================
246 //= DatabaseDocumentRecovery_Data
247 //====================================================================
248 struct DBACCESS_DLLPRIVATE DatabaseDocumentRecovery_Data
250 const Reference
<XComponentContext
> aContext
;
252 DatabaseDocumentRecovery_Data( const Reference
<XComponentContext
> & i_rContext
)
253 :aContext( i_rContext
)
258 //====================================================================
259 //= DatabaseDocumentRecovery
260 //====================================================================
261 //--------------------------------------------------------------------
262 DatabaseDocumentRecovery::DatabaseDocumentRecovery( const Reference
<XComponentContext
> & i_rContext
)
263 :m_pData( new DatabaseDocumentRecovery_Data( i_rContext
) )
267 //--------------------------------------------------------------------
268 DatabaseDocumentRecovery::~DatabaseDocumentRecovery()
272 //--------------------------------------------------------------------
273 void DatabaseDocumentRecovery::saveModifiedSubComponents( const Reference
< XStorage
>& i_rTargetStorage
,
274 const ::std::vector
< Reference
< XController
> >& i_rControllers
)
276 ENSURE_OR_THROW( i_rTargetStorage
.is(), "invalid document storage" );
278 // create a sub storage for recovery data
279 if ( i_rTargetStorage
->hasByName( lcl_getRecoveryDataSubStorageName() ) )
280 i_rTargetStorage
->removeElement( lcl_getRecoveryDataSubStorageName() );
281 Reference
< XStorage
> xRecoveryStorage
= i_rTargetStorage
->openStorageElement( lcl_getRecoveryDataSubStorageName(), ElementModes::READWRITE
);
283 // store recovery data for open sub components of the given controller(s)
284 if ( !i_rControllers
.empty() )
286 ENSURE_OR_THROW( i_rControllers
.size() == 1, "can't handle more than one controller" );
287 // At the moment, there can be only one view to a database document. If we ever allow for more than this,
288 // then we need a concept for sub documents opened from different controllers (i.e. two document views,
289 // and the user opens the very same form in both views). And depending on this, we need a concept for
290 // how those are saved to the recovery file.
292 MapCompTypeToCompDescs aMapCompDescs
;
294 for ( ::std::vector
< Reference
< XController
> >::const_iterator ctrl
= i_rControllers
.begin();
295 ctrl
!= i_rControllers
.end();
299 Reference
< XDatabaseDocumentUI
> xDatabaseUI( *ctrl
, UNO_QUERY_THROW
);
300 Sequence
< Reference
< XComponent
> > aComponents( xDatabaseUI
->getSubComponents() );
302 const Reference
< XComponent
>* component
= aComponents
.getConstArray();
303 const Reference
< XComponent
>* componentEnd
= aComponents
.getConstArray() + aComponents
.getLength();
304 for ( ; component
!= componentEnd
; ++component
)
306 SubComponentRecovery
aComponentRecovery( m_pData
->aContext
, xDatabaseUI
, *component
);
307 aComponentRecovery
.saveToRecoveryStorage( xRecoveryStorage
, aMapCompDescs
);
311 for ( MapCompTypeToCompDescs::const_iterator map
= aMapCompDescs
.begin();
312 map
!= aMapCompDescs
.end();
316 Reference
< XStorage
> xComponentsStor( xRecoveryStorage
->openStorageElement(
317 SubComponentRecovery::getComponentsStorageName( map
->first
), ElementModes::WRITE
| ElementModes::NOCREATE
) );
318 lcl_writeObjectMap_throw( m_pData
->aContext
, xComponentsStor
, map
->second
);
319 tools::stor::commitStorageIfWriteable( xComponentsStor
);
323 // commit the recovery storage
324 tools::stor::commitStorageIfWriteable( xRecoveryStorage
);
327 //--------------------------------------------------------------------
328 void DatabaseDocumentRecovery::recoverSubDocuments( const Reference
< XStorage
>& i_rDocumentStorage
,
329 const Reference
< XController
>& i_rTargetController
)
331 ENSURE_OR_THROW( i_rDocumentStorage
.is(), "illegal document storage" );
332 Reference
< XDatabaseDocumentUI
> xDocumentUI( i_rTargetController
, UNO_QUERY_THROW
);
334 if ( !i_rDocumentStorage
->hasByName( lcl_getRecoveryDataSubStorageName() ) )
338 // the "recovery" sub storage
339 Reference
< XStorage
> xRecoveryStorage
= i_rDocumentStorage
->openStorageElement( lcl_getRecoveryDataSubStorageName(), ElementModes::READ
);
341 // read the map from sub storages to object names
342 MapCompTypeToCompDescs aMapCompDescs
;
343 SubComponentType aKnownTypes
[] = { TABLE
, QUERY
, FORM
, REPORT
, RELATION_DESIGN
};
344 for ( size_t i
= 0; i
< sizeof( aKnownTypes
) / sizeof( aKnownTypes
[0] ); ++i
)
346 if ( !xRecoveryStorage
->hasByName( SubComponentRecovery::getComponentsStorageName( aKnownTypes
[i
] ) ) )
349 Reference
< XStorage
> xComponentsStor( xRecoveryStorage
->openStorageElement(
350 SubComponentRecovery::getComponentsStorageName( aKnownTypes
[i
] ), ElementModes::READ
) );
351 lcl_readObjectMap_throw( m_pData
->aContext
, xComponentsStor
, aMapCompDescs
[ aKnownTypes
[i
] ] );
352 xComponentsStor
->dispose();
355 // recover all sub components as indicated by the map
356 for ( MapCompTypeToCompDescs::const_iterator map
= aMapCompDescs
.begin();
357 map
!= aMapCompDescs
.end();
361 const SubComponentType eComponentType
= map
->first
;
363 // the storage for all components of the current type
364 Reference
< XStorage
> xComponentsStor( xRecoveryStorage
->openStorageElement(
365 SubComponentRecovery::getComponentsStorageName( eComponentType
), ElementModes::READ
), UNO_QUERY_THROW
);
367 // loop thru all components of this type
368 for ( MapStringToCompDesc::const_iterator stor
= map
->second
.begin();
369 stor
!= map
->second
.end();
373 const OUString
sComponentName( stor
->second
.sName
);
374 if ( !xComponentsStor
->hasByName( stor
->first
) )
376 SAL_WARN( "dbaccess",
377 "DatabaseDocumentRecovery::recoverSubDocuments: inconsistent recovery storage: storage '" <<
379 "' not found in '" <<
380 SubComponentRecovery::getComponentsStorageName( eComponentType
) <<
381 "', but required per map file!" );
385 // the controller needs to have a connection to be able to open sub components
386 if ( !xDocumentUI
->isConnected() )
387 xDocumentUI
->connect();
389 // recover the single component
390 Reference
< XStorage
> xCompStor( xComponentsStor
->openStorageElement( stor
->first
, ElementModes::READ
) );
391 SubComponentRecovery
aComponentRecovery( m_pData
->aContext
, xDocumentUI
, eComponentType
);
392 Reference
< XComponent
> xSubComponent( aComponentRecovery
.recoverFromStorage( xCompStor
, sComponentName
, stor
->second
.bForEditing
) );
394 // at the moment, we only store, during session save, sub components which are modified. So, set this
395 // recovered sub component to "modified", too.
396 lcl_markModified( xSubComponent
);
399 xComponentsStor
->dispose();
402 xRecoveryStorage
->dispose();
404 // now that we successfully recovered, removed the "recovery" sub storage
407 i_rDocumentStorage
->removeElement( lcl_getRecoveryDataSubStorageName() );
409 catch( const Exception
& )
411 DBG_UNHANDLED_EXCEPTION();
415 //........................................................................
416 } // namespace dbaccess
417 //........................................................................
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */