Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
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"
27 :
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/XTextInputStream.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>
37 :
38 : #include <comphelper/componentcontext.hxx>
39 : #include <comphelper/namedvaluecollection.hxx>
40 : #include <rtl/ustrbuf.hxx>
41 : #include <tools/diagnose_ex.h>
42 :
43 : #include <algorithm>
44 :
45 : //........................................................................
46 : namespace dbaccess
47 : {
48 : //........................................................................
49 :
50 : /** === begin UNO using === **/
51 : using ::com::sun::star::uno::Reference;
52 : using ::com::sun::star::uno::XInterface;
53 : using ::com::sun::star::uno::UNO_QUERY;
54 : using ::com::sun::star::uno::UNO_QUERY_THROW;
55 : using ::com::sun::star::uno::UNO_SET_THROW;
56 : using ::com::sun::star::uno::Exception;
57 : using ::com::sun::star::uno::RuntimeException;
58 : using ::com::sun::star::uno::Any;
59 : using ::com::sun::star::uno::makeAny;
60 : using ::com::sun::star::uno::Sequence;
61 : using ::com::sun::star::uno::Type;
62 : using ::com::sun::star::embed::XStorage;
63 : using ::com::sun::star::frame::XController;
64 : using ::com::sun::star::sdb::application::XDatabaseDocumentUI;
65 : using ::com::sun::star::lang::XComponent;
66 : using ::com::sun::star::document::XStorageBasedDocument;
67 : using ::com::sun::star::beans::PropertyValue;
68 : using ::com::sun::star::io::XStream;
69 : using ::com::sun::star::io::XTextOutputStream;
70 : using ::com::sun::star::io::XActiveDataSource;
71 : using ::com::sun::star::io::XTextInputStream;
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;
76 : using ::com::sun::star::lang::XMultiServiceFactory;
77 : /** === end UNO using === **/
78 :
79 : namespace ElementModes = ::com::sun::star::embed::ElementModes;
80 :
81 : //====================================================================
82 : //= helpers
83 : //====================================================================
84 : namespace
85 : {
86 : // .........................................................................
87 0 : static void lcl_getPersistentRepresentation( const MapStringToCompDesc::value_type& i_rComponentDesc, ::rtl::OUStringBuffer& o_rBuffer )
88 : {
89 0 : o_rBuffer.append( i_rComponentDesc.first );
90 0 : o_rBuffer.append( sal_Unicode( '=' ) );
91 0 : o_rBuffer.append( i_rComponentDesc.second.sName );
92 0 : o_rBuffer.append( sal_Unicode( ',' ) );
93 0 : o_rBuffer.append( sal_Unicode( i_rComponentDesc.second.bForEditing ? '1' : '0' ) );
94 0 : }
95 :
96 : // .........................................................................
97 0 : static bool lcl_extractCompDesc( const ::rtl::OUString& i_rIniLine, ::rtl::OUString& o_rStorName, SubComponentDescriptor& o_rCompDesc )
98 : {
99 0 : const sal_Int32 nEqualSignPos = i_rIniLine.indexOf( sal_Unicode( '=' ) );
100 0 : if ( nEqualSignPos < 1 )
101 : {
102 : OSL_FAIL( "lcl_extractCompDesc: invalid map file entry - unexpected pos of '='" );
103 0 : return false;
104 : }
105 0 : o_rStorName = i_rIniLine.copy( 0, nEqualSignPos );
106 :
107 0 : const sal_Int32 nCommaPos = i_rIniLine.lastIndexOf( sal_Unicode( ',' ) );
108 0 : if ( nCommaPos != i_rIniLine.getLength() - 2 )
109 : {
110 : OSL_FAIL( "lcl_extractCompDesc: invalid map file entry - unexpected pos of ','" );
111 0 : return false;
112 : }
113 0 : o_rCompDesc.sName = i_rIniLine.copy( nEqualSignPos + 1, nCommaPos - nEqualSignPos - 1 );
114 0 : o_rCompDesc.bForEditing = ( i_rIniLine.getStr()[ nCommaPos + 1 ] == '1' );
115 0 : return true;
116 : }
117 :
118 : // .........................................................................
119 0 : static const ::rtl::OUString& lcl_getRecoveryDataSubStorageName()
120 : {
121 0 : static const ::rtl::OUString s_sRecDataStorName( RTL_CONSTASCII_USTRINGPARAM( "recovery" ) );
122 0 : return s_sRecDataStorName;
123 : }
124 : // .........................................................................
125 0 : static const ::rtl::OUString& lcl_getObjectMapStreamName()
126 : {
127 0 : static const ::rtl::OUString s_sObjectMapStreamName( RTL_CONSTASCII_USTRINGPARAM( "storage-component-map.ini" ) );
128 0 : return s_sObjectMapStreamName;
129 : }
130 :
131 : // .........................................................................
132 0 : static const ::rtl::OUString& lcl_getMapStreamEncodingName()
133 : {
134 0 : static const ::rtl::OUString s_sMapStreamEncodingName( RTL_CONSTASCII_USTRINGPARAM( "UTF-8" ) );
135 0 : return s_sMapStreamEncodingName;
136 : }
137 :
138 : // .........................................................................
139 0 : static void lcl_writeObjectMap_throw( const ::comphelper::ComponentContext& i_rContext, const Reference< XStorage >& i_rStorage,
140 : const MapStringToCompDesc& i_mapStorageToCompDesc )
141 : {
142 0 : if ( i_mapStorageToCompDesc.empty() )
143 : // nothing to do
144 0 : return;
145 :
146 0 : StorageTextOutputStream aTextOutput( i_rContext, i_rStorage, lcl_getObjectMapStreamName() );
147 :
148 0 : aTextOutput.writeLine( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "[storages]" ) ) );
149 :
150 0 : for ( MapStringToCompDesc::const_iterator stor = i_mapStorageToCompDesc.begin();
151 0 : stor != i_mapStorageToCompDesc.end();
152 : ++stor
153 : )
154 : {
155 0 : ::rtl::OUStringBuffer aLine;
156 0 : lcl_getPersistentRepresentation( *stor, aLine );
157 :
158 0 : aTextOutput.writeLine( aLine.makeStringAndClear() );
159 0 : }
160 :
161 0 : aTextOutput.writeLine();
162 : }
163 :
164 : // .........................................................................
165 0 : static bool lcl_isSectionStart( const ::rtl::OUString& i_rIniLine, ::rtl::OUString& o_rSectionName )
166 : {
167 0 : const sal_Int32 nLen = i_rIniLine.getLength();
168 0 : if ( ( nLen > 0 ) && ( i_rIniLine.getStr()[0] == '[' ) && ( i_rIniLine.getStr()[ nLen - 1 ] == ']' ) )
169 : {
170 0 : o_rSectionName = i_rIniLine.copy( 1, nLen -2 );
171 0 : return true;
172 : }
173 0 : return false;
174 : }
175 :
176 : // .........................................................................
177 0 : static void lcl_stripTrailingLineFeed( ::rtl::OUString& io_rLine )
178 : {
179 0 : const sal_Int32 nLen = io_rLine.getLength();
180 0 : if ( ( nLen > 0 ) && ( io_rLine.getStr()[ nLen - 1 ] == '\n' ) )
181 0 : io_rLine = io_rLine.copy( 0, nLen - 1 );
182 0 : }
183 :
184 : // .........................................................................
185 0 : static void lcl_readObjectMap_throw( const ::comphelper::ComponentContext& i_rContext, const Reference< XStorage >& i_rStorage,
186 : MapStringToCompDesc& o_mapStorageToObjectName )
187 : {
188 0 : ENSURE_OR_THROW( i_rStorage.is(), "invalid storage" );
189 0 : if ( !i_rStorage->hasByName( lcl_getObjectMapStreamName() ) )
190 : { // nothing to do, though suspicious
191 : OSL_FAIL( "lcl_readObjectMap_throw: if there's no map file, then there's expected to be no storage, too!" );
192 0 : return;
193 : }
194 :
195 0 : Reference< XStream > xIniStream( i_rStorage->openStreamElement(
196 0 : lcl_getObjectMapStreamName(), ElementModes::READ ), UNO_SET_THROW );
197 :
198 0 : Reference< XTextInputStream > xTextInput( i_rContext.createComponent( "com.sun.star.io.TextInputStream" ), UNO_QUERY_THROW );
199 0 : xTextInput->setEncoding( lcl_getMapStreamEncodingName() );
200 :
201 0 : Reference< XActiveDataSink > xDataSink( xTextInput, UNO_QUERY_THROW );
202 0 : xDataSink->setInputStream( xIniStream->getInputStream() );
203 :
204 0 : ::rtl::OUString sCurrentSection;
205 0 : bool bCurrentSectionIsKnownToBeUnsupported = true;
206 0 : while ( !xTextInput->isEOF() )
207 : {
208 0 : ::rtl::OUString sLine = xTextInput->readLine();
209 0 : lcl_stripTrailingLineFeed( sLine );
210 :
211 0 : if ( sLine.isEmpty() )
212 0 : continue;
213 :
214 0 : if ( lcl_isSectionStart( sLine, sCurrentSection ) )
215 : {
216 0 : bCurrentSectionIsKnownToBeUnsupported = false;
217 0 : continue;
218 : }
219 :
220 0 : if ( bCurrentSectionIsKnownToBeUnsupported )
221 0 : continue;
222 :
223 : // the only section we support so far is "storages"
224 0 : if ( sCurrentSection != "storages" )
225 : {
226 0 : bCurrentSectionIsKnownToBeUnsupported = true;
227 0 : continue;
228 : }
229 :
230 0 : ::rtl::OUString sStorageName;
231 0 : SubComponentDescriptor aCompDesc;
232 0 : if ( !lcl_extractCompDesc( sLine, sStorageName, aCompDesc ) )
233 0 : continue;
234 0 : o_mapStorageToObjectName[ sStorageName ] = aCompDesc;
235 0 : }
236 : }
237 :
238 : // .........................................................................
239 0 : static void lcl_markModified( const Reference< XComponent >& i_rSubComponent )
240 : {
241 0 : const Reference< XModifiable > xModify( i_rSubComponent, UNO_QUERY );
242 0 : if ( !xModify.is() )
243 : {
244 : OSL_FAIL( "lcl_markModified: unhandled case!" );
245 0 : return;
246 : }
247 :
248 0 : xModify->setModified( sal_True );
249 : }
250 : }
251 :
252 : //====================================================================
253 : //= DatabaseDocumentRecovery_Data
254 : //====================================================================
255 0 : struct DBACCESS_DLLPRIVATE DatabaseDocumentRecovery_Data
256 : {
257 : const ::comphelper::ComponentContext aContext;
258 :
259 0 : DatabaseDocumentRecovery_Data( const ::comphelper::ComponentContext& i_rContext )
260 0 : :aContext( i_rContext )
261 : {
262 0 : }
263 : };
264 :
265 : //====================================================================
266 : //= DatabaseDocumentRecovery
267 : //====================================================================
268 : //--------------------------------------------------------------------
269 0 : DatabaseDocumentRecovery::DatabaseDocumentRecovery( const ::comphelper::ComponentContext& i_rContext )
270 0 : :m_pData( new DatabaseDocumentRecovery_Data( i_rContext ) )
271 : {
272 0 : }
273 :
274 : //--------------------------------------------------------------------
275 0 : DatabaseDocumentRecovery::~DatabaseDocumentRecovery()
276 : {
277 0 : }
278 :
279 : //--------------------------------------------------------------------
280 0 : void DatabaseDocumentRecovery::saveModifiedSubComponents( const Reference< XStorage >& i_rTargetStorage,
281 : const ::std::vector< Reference< XController > >& i_rControllers )
282 : {
283 0 : ENSURE_OR_THROW( i_rTargetStorage.is(), "invalid document storage" );
284 :
285 : // create a sub storage for recovery data
286 0 : if ( i_rTargetStorage->hasByName( lcl_getRecoveryDataSubStorageName() ) )
287 0 : i_rTargetStorage->removeElement( lcl_getRecoveryDataSubStorageName() );
288 0 : Reference< XStorage > xRecoveryStorage = i_rTargetStorage->openStorageElement( lcl_getRecoveryDataSubStorageName(), ElementModes::READWRITE );
289 :
290 : // store recovery data for open sub components of the given controller(s)
291 0 : if ( !i_rControllers.empty() )
292 : {
293 0 : ENSURE_OR_THROW( i_rControllers.size() == 1, "can't handle more than one controller" );
294 : // At the moment, there can be only one view to a database document. If we ever allow for more than this,
295 : // then we need a concept for sub documents opened from different controllers (i.e. two document views,
296 : // and the user opens the very same form in both views). And depending on this, we need a concept for
297 : // how those are saved to the recovery file.
298 :
299 0 : MapCompTypeToCompDescs aMapCompDescs;
300 :
301 0 : for ( ::std::vector< Reference< XController > >::const_iterator ctrl = i_rControllers.begin();
302 0 : ctrl != i_rControllers.end();
303 : ++ctrl
304 : )
305 : {
306 0 : Reference< XDatabaseDocumentUI > xDatabaseUI( *ctrl, UNO_QUERY_THROW );
307 0 : Sequence< Reference< XComponent > > aComponents( xDatabaseUI->getSubComponents() );
308 :
309 0 : const Reference< XComponent >* component = aComponents.getConstArray();
310 0 : const Reference< XComponent >* componentEnd = aComponents.getConstArray() + aComponents.getLength();
311 0 : for ( ; component != componentEnd; ++component )
312 : {
313 0 : SubComponentRecovery aComponentRecovery( m_pData->aContext, xDatabaseUI, *component );
314 0 : aComponentRecovery.saveToRecoveryStorage( xRecoveryStorage, aMapCompDescs );
315 0 : }
316 0 : }
317 :
318 0 : for ( MapCompTypeToCompDescs::const_iterator map = aMapCompDescs.begin();
319 0 : map != aMapCompDescs.end();
320 : ++map
321 : )
322 : {
323 0 : Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
324 0 : SubComponentRecovery::getComponentsStorageName( map->first ), ElementModes::WRITE | ElementModes::NOCREATE ) );
325 0 : lcl_writeObjectMap_throw( m_pData->aContext, xComponentsStor, map->second );
326 0 : tools::stor::commitStorageIfWriteable( xComponentsStor );
327 0 : }
328 : }
329 :
330 : // commit the recovery storage
331 0 : tools::stor::commitStorageIfWriteable( xRecoveryStorage );
332 0 : }
333 :
334 : //--------------------------------------------------------------------
335 0 : void DatabaseDocumentRecovery::recoverSubDocuments( const Reference< XStorage >& i_rDocumentStorage,
336 : const Reference< XController >& i_rTargetController )
337 : {
338 0 : ENSURE_OR_THROW( i_rDocumentStorage.is(), "illegal document storage" );
339 0 : Reference< XDatabaseDocumentUI > xDocumentUI( i_rTargetController, UNO_QUERY_THROW );
340 :
341 0 : if ( !i_rDocumentStorage->hasByName( lcl_getRecoveryDataSubStorageName() ) )
342 : // that's allowed
343 0 : return;
344 :
345 : // the "recovery" sub storage
346 0 : Reference< XStorage > xRecoveryStorage = i_rDocumentStorage->openStorageElement( lcl_getRecoveryDataSubStorageName(), ElementModes::READ );
347 :
348 : // read the map from sub storages to object names
349 0 : MapCompTypeToCompDescs aMapCompDescs;
350 0 : SubComponentType aKnownTypes[] = { TABLE, QUERY, FORM, REPORT, RELATION_DESIGN };
351 0 : for ( size_t i = 0; i < sizeof( aKnownTypes ) / sizeof( aKnownTypes[0] ); ++i )
352 : {
353 0 : if ( !xRecoveryStorage->hasByName( SubComponentRecovery::getComponentsStorageName( aKnownTypes[i] ) ) )
354 0 : continue;
355 :
356 0 : Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
357 0 : SubComponentRecovery::getComponentsStorageName( aKnownTypes[i] ), ElementModes::READ ) );
358 0 : lcl_readObjectMap_throw( m_pData->aContext, xComponentsStor, aMapCompDescs[ aKnownTypes[i] ] );
359 0 : xComponentsStor->dispose();
360 0 : }
361 :
362 : // recover all sub components as indicated by the map
363 0 : for ( MapCompTypeToCompDescs::const_iterator map = aMapCompDescs.begin();
364 0 : map != aMapCompDescs.end();
365 : ++map
366 : )
367 : {
368 0 : const SubComponentType eComponentType = map->first;
369 :
370 : // the storage for all components of the current type
371 0 : Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
372 0 : SubComponentRecovery::getComponentsStorageName( eComponentType ), ElementModes::READ ), UNO_QUERY_THROW );
373 :
374 : // loop thru all components of this type
375 0 : for ( MapStringToCompDesc::const_iterator stor = map->second.begin();
376 0 : stor != map->second.end();
377 : ++stor
378 : )
379 : {
380 0 : const ::rtl::OUString sComponentName( stor->second.sName );
381 0 : if ( !xComponentsStor->hasByName( stor->first ) )
382 : {
383 : #if OSL_DEBUG_LEVEL > 0
384 : ::rtl::OStringBuffer message;
385 : message.append( "DatabaseDocumentRecovery::recoverSubDocuments: inconsistent recovery storage: storage '" );
386 : message.append( ::rtl::OUStringToOString( stor->first, RTL_TEXTENCODING_ASCII_US ) );
387 : message.append( "' not found in '" );
388 : message.append( ::rtl::OUStringToOString( SubComponentRecovery::getComponentsStorageName( eComponentType ), RTL_TEXTENCODING_ASCII_US ) );
389 : message.append( "', but required per map file!" );
390 : OSL_FAIL( message.getStr() );
391 : #endif
392 0 : continue;
393 : }
394 :
395 : // the controller needs to have a connection to be able to open sub components
396 0 : if ( !xDocumentUI->isConnected() )
397 0 : xDocumentUI->connect();
398 :
399 : // recover the single component
400 0 : Reference< XStorage > xCompStor( xComponentsStor->openStorageElement( stor->first, ElementModes::READ ) );
401 0 : SubComponentRecovery aComponentRecovery( m_pData->aContext, xDocumentUI, eComponentType );
402 0 : Reference< XComponent > xSubComponent( aComponentRecovery.recoverFromStorage( xCompStor, sComponentName, stor->second.bForEditing ) );
403 :
404 : // at the moment, we only store, during session save, sub components which are modified. So, set this
405 : // recovered sub component to "modified", too.
406 0 : lcl_markModified( xSubComponent );
407 0 : }
408 :
409 0 : xComponentsStor->dispose();
410 0 : }
411 :
412 0 : xRecoveryStorage->dispose();
413 :
414 : // now that we successfully recovered, removed the "recovery" sub storage
415 : try
416 : {
417 0 : i_rDocumentStorage->removeElement( lcl_getRecoveryDataSubStorageName() );
418 : }
419 0 : catch( const Exception& )
420 : {
421 : DBG_UNHANDLED_EXCEPTION();
422 0 : }
423 : }
424 :
425 : //........................................................................
426 : } // namespace dbaccess
427 : //........................................................................
428 :
429 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|