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/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>
37 :
38 : #include <comphelper/namedvaluecollection.hxx>
39 : #include <rtl/ustrbuf.hxx>
40 : #include <tools/diagnose_ex.h>
41 :
42 : #include <algorithm>
43 :
44 : //........................................................................
45 : namespace dbaccess
46 : {
47 : //........................................................................
48 :
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;
76 :
77 : namespace ElementModes = ::com::sun::star::embed::ElementModes;
78 :
79 : //====================================================================
80 : //= helpers
81 : //====================================================================
82 : namespace
83 : {
84 : // .........................................................................
85 0 : static void lcl_getPersistentRepresentation( const MapStringToCompDesc::value_type& i_rComponentDesc, OUStringBuffer& o_rBuffer )
86 : {
87 0 : o_rBuffer.append( i_rComponentDesc.first );
88 0 : o_rBuffer.append( sal_Unicode( '=' ) );
89 0 : o_rBuffer.append( i_rComponentDesc.second.sName );
90 0 : o_rBuffer.append( sal_Unicode( ',' ) );
91 0 : o_rBuffer.append( sal_Unicode( i_rComponentDesc.second.bForEditing ? '1' : '0' ) );
92 0 : }
93 :
94 : // .........................................................................
95 0 : static bool lcl_extractCompDesc( const OUString& i_rIniLine, OUString& o_rStorName, SubComponentDescriptor& o_rCompDesc )
96 : {
97 0 : const sal_Int32 nEqualSignPos = i_rIniLine.indexOf( sal_Unicode( '=' ) );
98 0 : if ( nEqualSignPos < 1 )
99 : {
100 : OSL_FAIL( "lcl_extractCompDesc: invalid map file entry - unexpected pos of '='" );
101 0 : return false;
102 : }
103 0 : o_rStorName = i_rIniLine.copy( 0, nEqualSignPos );
104 :
105 0 : const sal_Int32 nCommaPos = i_rIniLine.lastIndexOf( sal_Unicode( ',' ) );
106 0 : if ( nCommaPos != i_rIniLine.getLength() - 2 )
107 : {
108 : OSL_FAIL( "lcl_extractCompDesc: invalid map file entry - unexpected pos of ','" );
109 0 : return false;
110 : }
111 0 : o_rCompDesc.sName = i_rIniLine.copy( nEqualSignPos + 1, nCommaPos - nEqualSignPos - 1 );
112 0 : o_rCompDesc.bForEditing = ( i_rIniLine.getStr()[ nCommaPos + 1 ] == '1' );
113 0 : return true;
114 : }
115 :
116 0 : static const OUString& lcl_getRecoveryDataSubStorageName()
117 : {
118 0 : static const OUString s_sRecDataStorName( "recovery" );
119 0 : return s_sRecDataStorName;
120 : }
121 :
122 0 : static const OUString& lcl_getObjectMapStreamName()
123 : {
124 0 : static const OUString s_sObjectMapStreamName( "storage-component-map.ini" );
125 0 : return s_sObjectMapStreamName;
126 : }
127 :
128 0 : static const OUString& lcl_getMapStreamEncodingName()
129 : {
130 0 : static const OUString s_sMapStreamEncodingName( "UTF-8" );
131 0 : return s_sMapStreamEncodingName;
132 : }
133 :
134 0 : static void lcl_writeObjectMap_throw( const Reference<XComponentContext> & i_rContext, const Reference< XStorage >& i_rStorage,
135 : const MapStringToCompDesc& i_mapStorageToCompDesc )
136 : {
137 0 : if ( i_mapStorageToCompDesc.empty() )
138 : // nothing to do
139 0 : return;
140 :
141 0 : StorageTextOutputStream aTextOutput( i_rContext, i_rStorage, lcl_getObjectMapStreamName() );
142 :
143 0 : aTextOutput.writeLine( "[storages]" );
144 :
145 0 : for ( MapStringToCompDesc::const_iterator stor = i_mapStorageToCompDesc.begin();
146 0 : stor != i_mapStorageToCompDesc.end();
147 : ++stor
148 : )
149 : {
150 0 : OUStringBuffer aLine;
151 0 : lcl_getPersistentRepresentation( *stor, aLine );
152 :
153 0 : aTextOutput.writeLine( aLine.makeStringAndClear() );
154 0 : }
155 :
156 0 : aTextOutput.writeLine();
157 : }
158 :
159 : // .........................................................................
160 0 : static bool lcl_isSectionStart( const OUString& i_rIniLine, OUString& o_rSectionName )
161 : {
162 0 : const sal_Int32 nLen = i_rIniLine.getLength();
163 0 : if ( ( nLen > 0 ) && ( i_rIniLine.getStr()[0] == '[' ) && ( i_rIniLine.getStr()[ nLen - 1 ] == ']' ) )
164 : {
165 0 : o_rSectionName = i_rIniLine.copy( 1, nLen -2 );
166 0 : return true;
167 : }
168 0 : return false;
169 : }
170 :
171 : // .........................................................................
172 0 : static void lcl_stripTrailingLineFeed( OUString& io_rLine )
173 : {
174 0 : const sal_Int32 nLen = io_rLine.getLength();
175 0 : if ( ( nLen > 0 ) && ( io_rLine.getStr()[ nLen - 1 ] == '\n' ) )
176 0 : io_rLine = io_rLine.copy( 0, nLen - 1 );
177 0 : }
178 :
179 : // .........................................................................
180 0 : static void lcl_readObjectMap_throw( const Reference<XComponentContext> & i_rxContext, const Reference< XStorage >& i_rStorage,
181 : MapStringToCompDesc& o_mapStorageToObjectName )
182 : {
183 0 : ENSURE_OR_THROW( i_rStorage.is(), "invalid storage" );
184 0 : 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!" );
187 0 : return;
188 : }
189 :
190 0 : Reference< XStream > xIniStream( i_rStorage->openStreamElement(
191 0 : lcl_getObjectMapStreamName(), ElementModes::READ ), UNO_SET_THROW );
192 :
193 0 : Reference< XTextInputStream2 > xTextInput = TextInputStream::create( i_rxContext );
194 0 : xTextInput->setEncoding( lcl_getMapStreamEncodingName() );
195 0 : xTextInput->setInputStream( xIniStream->getInputStream() );
196 :
197 0 : OUString sCurrentSection;
198 0 : bool bCurrentSectionIsKnownToBeUnsupported = true;
199 0 : while ( !xTextInput->isEOF() )
200 : {
201 0 : OUString sLine = xTextInput->readLine();
202 0 : lcl_stripTrailingLineFeed( sLine );
203 :
204 0 : if ( sLine.isEmpty() )
205 0 : continue;
206 :
207 0 : if ( lcl_isSectionStart( sLine, sCurrentSection ) )
208 : {
209 0 : bCurrentSectionIsKnownToBeUnsupported = false;
210 0 : continue;
211 : }
212 :
213 0 : if ( bCurrentSectionIsKnownToBeUnsupported )
214 0 : continue;
215 :
216 : // the only section we support so far is "storages"
217 0 : if ( sCurrentSection != "storages" )
218 : {
219 0 : bCurrentSectionIsKnownToBeUnsupported = true;
220 0 : continue;
221 : }
222 :
223 0 : OUString sStorageName;
224 0 : SubComponentDescriptor aCompDesc;
225 0 : if ( !lcl_extractCompDesc( sLine, sStorageName, aCompDesc ) )
226 0 : continue;
227 0 : o_mapStorageToObjectName[ sStorageName ] = aCompDesc;
228 0 : }
229 : }
230 :
231 : // .........................................................................
232 0 : static void lcl_markModified( const Reference< XComponent >& i_rSubComponent )
233 : {
234 0 : const Reference< XModifiable > xModify( i_rSubComponent, UNO_QUERY );
235 0 : if ( !xModify.is() )
236 : {
237 : OSL_FAIL( "lcl_markModified: unhandled case!" );
238 0 : return;
239 : }
240 :
241 0 : xModify->setModified( sal_True );
242 : }
243 : }
244 :
245 : //====================================================================
246 : //= DatabaseDocumentRecovery_Data
247 : //====================================================================
248 0 : struct DBACCESS_DLLPRIVATE DatabaseDocumentRecovery_Data
249 : {
250 : const Reference<XComponentContext> aContext;
251 :
252 0 : DatabaseDocumentRecovery_Data( const Reference<XComponentContext> & i_rContext )
253 0 : :aContext( i_rContext )
254 : {
255 0 : }
256 : };
257 :
258 : //====================================================================
259 : //= DatabaseDocumentRecovery
260 : //====================================================================
261 : //--------------------------------------------------------------------
262 0 : DatabaseDocumentRecovery::DatabaseDocumentRecovery( const Reference<XComponentContext> & i_rContext )
263 0 : :m_pData( new DatabaseDocumentRecovery_Data( i_rContext ) )
264 : {
265 0 : }
266 :
267 : //--------------------------------------------------------------------
268 0 : DatabaseDocumentRecovery::~DatabaseDocumentRecovery()
269 : {
270 0 : }
271 :
272 : //--------------------------------------------------------------------
273 0 : void DatabaseDocumentRecovery::saveModifiedSubComponents( const Reference< XStorage >& i_rTargetStorage,
274 : const ::std::vector< Reference< XController > >& i_rControllers )
275 : {
276 0 : ENSURE_OR_THROW( i_rTargetStorage.is(), "invalid document storage" );
277 :
278 : // create a sub storage for recovery data
279 0 : if ( i_rTargetStorage->hasByName( lcl_getRecoveryDataSubStorageName() ) )
280 0 : i_rTargetStorage->removeElement( lcl_getRecoveryDataSubStorageName() );
281 0 : Reference< XStorage > xRecoveryStorage = i_rTargetStorage->openStorageElement( lcl_getRecoveryDataSubStorageName(), ElementModes::READWRITE );
282 :
283 : // store recovery data for open sub components of the given controller(s)
284 0 : if ( !i_rControllers.empty() )
285 : {
286 0 : 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.
291 :
292 0 : MapCompTypeToCompDescs aMapCompDescs;
293 :
294 0 : for ( ::std::vector< Reference< XController > >::const_iterator ctrl = i_rControllers.begin();
295 0 : ctrl != i_rControllers.end();
296 : ++ctrl
297 : )
298 : {
299 0 : Reference< XDatabaseDocumentUI > xDatabaseUI( *ctrl, UNO_QUERY_THROW );
300 0 : Sequence< Reference< XComponent > > aComponents( xDatabaseUI->getSubComponents() );
301 :
302 0 : const Reference< XComponent >* component = aComponents.getConstArray();
303 0 : const Reference< XComponent >* componentEnd = aComponents.getConstArray() + aComponents.getLength();
304 0 : for ( ; component != componentEnd; ++component )
305 : {
306 0 : SubComponentRecovery aComponentRecovery( m_pData->aContext, xDatabaseUI, *component );
307 0 : aComponentRecovery.saveToRecoveryStorage( xRecoveryStorage, aMapCompDescs );
308 0 : }
309 0 : }
310 :
311 0 : for ( MapCompTypeToCompDescs::const_iterator map = aMapCompDescs.begin();
312 0 : map != aMapCompDescs.end();
313 : ++map
314 : )
315 : {
316 0 : Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
317 0 : SubComponentRecovery::getComponentsStorageName( map->first ), ElementModes::WRITE | ElementModes::NOCREATE ) );
318 0 : lcl_writeObjectMap_throw( m_pData->aContext, xComponentsStor, map->second );
319 0 : tools::stor::commitStorageIfWriteable( xComponentsStor );
320 0 : }
321 : }
322 :
323 : // commit the recovery storage
324 0 : tools::stor::commitStorageIfWriteable( xRecoveryStorage );
325 0 : }
326 :
327 : //--------------------------------------------------------------------
328 0 : void DatabaseDocumentRecovery::recoverSubDocuments( const Reference< XStorage >& i_rDocumentStorage,
329 : const Reference< XController >& i_rTargetController )
330 : {
331 0 : ENSURE_OR_THROW( i_rDocumentStorage.is(), "illegal document storage" );
332 0 : Reference< XDatabaseDocumentUI > xDocumentUI( i_rTargetController, UNO_QUERY_THROW );
333 :
334 0 : if ( !i_rDocumentStorage->hasByName( lcl_getRecoveryDataSubStorageName() ) )
335 : // that's allowed
336 0 : return;
337 :
338 : // the "recovery" sub storage
339 0 : Reference< XStorage > xRecoveryStorage = i_rDocumentStorage->openStorageElement( lcl_getRecoveryDataSubStorageName(), ElementModes::READ );
340 :
341 : // read the map from sub storages to object names
342 0 : MapCompTypeToCompDescs aMapCompDescs;
343 0 : SubComponentType aKnownTypes[] = { TABLE, QUERY, FORM, REPORT, RELATION_DESIGN };
344 0 : for ( size_t i = 0; i < sizeof( aKnownTypes ) / sizeof( aKnownTypes[0] ); ++i )
345 : {
346 0 : if ( !xRecoveryStorage->hasByName( SubComponentRecovery::getComponentsStorageName( aKnownTypes[i] ) ) )
347 0 : continue;
348 :
349 0 : Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
350 0 : SubComponentRecovery::getComponentsStorageName( aKnownTypes[i] ), ElementModes::READ ) );
351 0 : lcl_readObjectMap_throw( m_pData->aContext, xComponentsStor, aMapCompDescs[ aKnownTypes[i] ] );
352 0 : xComponentsStor->dispose();
353 0 : }
354 :
355 : // recover all sub components as indicated by the map
356 0 : for ( MapCompTypeToCompDescs::const_iterator map = aMapCompDescs.begin();
357 0 : map != aMapCompDescs.end();
358 : ++map
359 : )
360 : {
361 0 : const SubComponentType eComponentType = map->first;
362 :
363 : // the storage for all components of the current type
364 0 : Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement(
365 0 : SubComponentRecovery::getComponentsStorageName( eComponentType ), ElementModes::READ ), UNO_QUERY_THROW );
366 :
367 : // loop thru all components of this type
368 0 : for ( MapStringToCompDesc::const_iterator stor = map->second.begin();
369 0 : stor != map->second.end();
370 : ++stor
371 : )
372 : {
373 0 : const OUString sComponentName( stor->second.sName );
374 0 : if ( !xComponentsStor->hasByName( stor->first ) )
375 : {
376 : SAL_WARN( "dbaccess",
377 : "DatabaseDocumentRecovery::recoverSubDocuments: inconsistent recovery storage: storage '" <<
378 : stor->first <<
379 : "' not found in '" <<
380 : SubComponentRecovery::getComponentsStorageName( eComponentType ) <<
381 : "', but required per map file!" );
382 0 : continue;
383 : }
384 :
385 : // the controller needs to have a connection to be able to open sub components
386 0 : if ( !xDocumentUI->isConnected() )
387 0 : xDocumentUI->connect();
388 :
389 : // recover the single component
390 0 : Reference< XStorage > xCompStor( xComponentsStor->openStorageElement( stor->first, ElementModes::READ ) );
391 0 : SubComponentRecovery aComponentRecovery( m_pData->aContext, xDocumentUI, eComponentType );
392 0 : Reference< XComponent > xSubComponent( aComponentRecovery.recoverFromStorage( xCompStor, sComponentName, stor->second.bForEditing ) );
393 :
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 0 : lcl_markModified( xSubComponent );
397 0 : }
398 :
399 0 : xComponentsStor->dispose();
400 0 : }
401 :
402 0 : xRecoveryStorage->dispose();
403 :
404 : // now that we successfully recovered, removed the "recovery" sub storage
405 : try
406 : {
407 0 : i_rDocumentStorage->removeElement( lcl_getRecoveryDataSubStorageName() );
408 : }
409 0 : catch( const Exception& )
410 : {
411 : DBG_UNHANDLED_EXCEPTION();
412 0 : }
413 : }
414 :
415 : //........................................................................
416 : } // namespace dbaccess
417 : //........................................................................
418 :
419 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|