Debian Codegraph @

embeddedobj/source/commonembedding/persistence.cxx

raw
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 #include <commonembobj.hxx>
21 #include <com/sun/star/embed/Aspects.hpp>
22 #include <com/sun/star/document/XStorageBasedDocument.hpp>
23 #include <com/sun/star/embed/EmbedStates.hpp>
24 #include <com/sun/star/embed/EntryInitModes.hpp>
25 #include <com/sun/star/embed/StorageWrappedTargetException.hpp>
26 #include <com/sun/star/embed/WrongStateException.hpp>
27 #include <com/sun/star/embed/XStorage.hpp>
28 #include <com/sun/star/embed/XOptimizedStorage.hpp>
29 #include <com/sun/star/embed/ElementModes.hpp>
30 #include <com/sun/star/embed/EmbedUpdateModes.hpp>
31 #include <com/sun/star/embed/StorageFactory.hpp>
32 #include <com/sun/star/io/IOException.hpp>
33 #include <com/sun/star/io/TempFile.hpp>
34 #include <com/sun/star/frame/XModel.hpp>
35 #include <com/sun/star/frame/XStorable.hpp>
36 #include <com/sun/star/frame/XLoadable.hpp>
37 #include <com/sun/star/frame/XModule.hpp>
38 #include <com/sun/star/lang/NoSupportException.hpp>
39 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
40 #include <com/sun/star/lang/DisposedException.hpp>
41 #include <com/sun/star/util/XModifiable.hpp>
42
43 #include <com/sun/star/container/XNameAccess.hpp>
44 #include <com/sun/star/container/XChild.hpp>
45 #include <com/sun/star/util/XCloseable.hpp>
46 #include <com/sun/star/beans/XPropertySet.hpp>
47 #include <com/sun/star/beans/IllegalTypeException.hpp>
48 #include <com/sun/star/chart2/XChartDocument.hpp>
49
50 #include <comphelper/fileformat.h>
51 #include <comphelper/storagehelper.hxx>
52 #include <comphelper/mimeconfighelper.hxx>
53 #include <comphelper/namedvaluecollection.hxx>
54 #include <comphelper/propertyvalue.hxx>
55 #include <comphelper/configuration.hxx>
56 #include <tools/urlobj.hxx>
57 #include <unotools/mediadescriptor.hxx>
58 #include <unotools/securityoptions.hxx>
59
60 #include <comphelper/diagnose_ex.hxx>
61 #include <sal/log.hxx>
62 #include "persistence.hxx"
63
64 using namespace ::com::sun::star;
65
66
67 uno::Sequence< beans::PropertyValue > GetValuableArgs_Impl( const uno::Sequence< beans::PropertyValue >& aMedDescr,
68 bool bCanUseDocumentBaseURL )
69 {
70 uno::Sequence< beans::PropertyValue > aResult;
71 sal_Int32 nResLen = 0;
72
73 for ( beans::PropertyValue const & prop : aMedDescr )
74 {
75 if ( prop.Name == "ComponentData" || prop.Name == "DocumentTitle"
76 || prop.Name == "InteractionHandler" || prop.Name == "JumpMark"
77 // || prop.Name == "Password" // makes no sense for embedded objects
78 || prop.Name == "Preview" || prop.Name == "ReadOnly"
79 || prop.Name == "StartPresentation" || prop.Name == "RepairPackage"
80 || prop.Name == "StatusIndicator" || prop.Name == "ViewData"
81 || prop.Name == "ViewId" || prop.Name == "MacroExecutionMode"
82 || prop.Name == "UpdateDocMode" || prop.Name == "Referer"
83 || (prop.Name == "DocumentBaseURL" && bCanUseDocumentBaseURL) )
84 {
85 aResult.realloc( ++nResLen );
86 aResult.getArray()[nResLen-1] = prop;
87 }
88 }
89
90 return aResult;
91 }
92
93
94 static uno::Sequence< beans::PropertyValue > addAsTemplate( const uno::Sequence< beans::PropertyValue >& aOrig )
95 {
96 bool bAsTemplateSet = false;
97 sal_Int32 nLength = aOrig.getLength();
98 uno::Sequence< beans::PropertyValue > aResult( aOrig );
99
100 for ( sal_Int32 nInd = 0; nInd < nLength; nInd++ )
101 {
102 if ( aResult[nInd].Name == "AsTemplate" )
103 {
104 aResult.getArray()[nInd].Value <<= true;
105 bAsTemplateSet = true;
106 }
107 }
108
109 if ( !bAsTemplateSet )
110 {
111 aResult.realloc( nLength + 1 );
112 auto pResult = aResult.getArray();
113 pResult[nLength].Name = "AsTemplate";
114 pResult[nLength].Value <<= true;
115 }
116
117 return aResult;
118 }
119
120
121 static uno::Reference< io::XInputStream > createTempInpStreamFromStor(
122 const uno::Reference< embed::XStorage >& xStorage,
123 const uno::Reference< uno::XComponentContext >& xContext )
124 {
125 SAL_WARN_IF( !xStorage.is(), "embeddedobj.common", "The storage can not be empty!" );
126
127 uno::Reference< io::XInputStream > xResult;
128
129 uno::Reference < io::XStream > xTempStream( io::TempFile::create(xContext), uno::UNO_QUERY_THROW );
130
131 uno::Reference < lang::XSingleServiceFactory > xStorageFactory( embed::StorageFactory::create(xContext) );
132
133 uno::Sequence< uno::Any > aArgs{ uno::Any(xTempStream),
134 uno::Any(embed::ElementModes::READWRITE) };
135 uno::Reference< embed::XStorage > xTempStorage( xStorageFactory->createInstanceWithArguments( aArgs ),
136 uno::UNO_QUERY_THROW );
137
138 try
139 {
140 xStorage->copyToStorage( xTempStorage );
141 } catch( const uno::Exception& )
142 {
143 css::uno::Any anyEx = cppu::getCaughtException();
144 throw embed::StorageWrappedTargetException(
145 u"Can't copy storage!"_ustr,
146 uno::Reference< uno::XInterface >(),
147 anyEx );
148 }
149
150 try {
151 if ( xTempStorage.is() )
152 xTempStorage->dispose();
153 }
154 catch ( const uno::Exception& )
155 {
156 }
157
158 try {
159 uno::Reference< io::XOutputStream > xTempOut = xTempStream->getOutputStream();
160 if ( xTempOut.is() )
161 xTempOut->closeOutput();
162 }
163 catch ( const uno::Exception& )
164 {
165 }
166
167 xResult = xTempStream->getInputStream();
168
169 return xResult;
170
171 }
172
173
174 static void TransferMediaType( const uno::Reference< embed::XStorage >& i_rSource, const uno::Reference< embed::XStorage >& i_rTarget )
175 {
176 try
177 {
178 const uno::Reference< beans::XPropertySet > xSourceProps( i_rSource, uno::UNO_QUERY_THROW );
179 const uno::Reference< beans::XPropertySet > xTargetProps( i_rTarget, uno::UNO_QUERY_THROW );
180 static constexpr OUString sMediaTypePropName( u"MediaType"_ustr );
181 xTargetProps->setPropertyValue( sMediaTypePropName, xSourceProps->getPropertyValue( sMediaTypePropName ) );
182 }
183 catch( const uno::Exception& )
184 {
185 DBG_UNHANDLED_EXCEPTION("embeddedobj.common");
186 }
187 }
188
189
190 static uno::Reference< util::XCloseable > CreateDocument( const uno::Reference< uno::XComponentContext >& _rxContext,
191 const OUString& _rDocumentServiceName, bool _bEmbeddedScriptSupport, const bool i_bDocumentRecoverySupport )
192 {
193 static constexpr OUStringLiteral sEmbeddedObject = u"EmbeddedObject";
194 static constexpr OUStringLiteral sEmbeddedScriptSupport = u"EmbeddedScriptSupport";
195 static constexpr OUStringLiteral sDocumentRecoverySupport = u"DocumentRecoverySupport";
196 ::comphelper::NamedValueCollection aArguments;
197 aArguments.put( sEmbeddedObject, true );
198 aArguments.put( sEmbeddedScriptSupport, _bEmbeddedScriptSupport );
199 aArguments.put( sDocumentRecoverySupport, i_bDocumentRecoverySupport );
200
201 uno::Reference< uno::XInterface > xDocument;
202 try
203 {
204 xDocument = _rxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
205 _rDocumentServiceName, aArguments.getWrappedPropertyValues(), _rxContext );
206 }
207 catch( const uno::Exception& )
208 {
209 // if an embedded object implementation does not support XInitialization,
210 // the default factory from cppuhelper will throw an
211 // IllegalArgumentException when we try to create the instance with arguments.
212 // Okay, so we fall back to creating the instance without any arguments.
213 OSL_FAIL("Consider implementing interface XInitialization to avoid duplicate construction");
214 xDocument = _rxContext->getServiceManager()->createInstanceWithContext( _rDocumentServiceName, _rxContext );
215 }
216
217 SAL_WARN_IF(!xDocument.is(), "embeddedobj.common", "Service " << _rDocumentServiceName << " is not available?");
218 return uno::Reference< util::XCloseable >( xDocument, uno::UNO_QUERY );
219 }
220
221
222 static void SetDocToEmbedded( const uno::Reference< frame::XModel >& rDocument, const OUString& aModuleName )
223 {
224 if (!rDocument.is())
225 return;
226
227 uno::Sequence< beans::PropertyValue > aSeq{ comphelper::makePropertyValue(u"SetEmbedded"_ustr, true) };
228 rDocument->attachResource( OUString(), aSeq );
229
230 if ( !aModuleName.isEmpty() )
231 {
232 try
233 {
234 uno::Reference< frame::XModule > xModule( rDocument, uno::UNO_QUERY_THROW );
235 xModule->setIdentifier( aModuleName );
236 }
237 catch( const uno::Exception& )
238 {}
239 }
240 }
241
242
243 void OCommonEmbeddedObject::SwitchOwnPersistence( const uno::Reference< embed::XStorage >& xNewParentStorage,
244 const uno::Reference< embed::XStorage >& xNewObjectStorage,
245 const OUString& aNewName )
246 {
247 if ( xNewParentStorage == m_xParentStorage && aNewName == m_aEntryName )
248 {
249 SAL_WARN_IF( xNewObjectStorage != m_xObjectStorage, "embeddedobj.common", "The storage must be the same!" );
250 return;
251 }
252
253 auto xOldObjectStorage = m_xObjectStorage;
254 m_xObjectStorage = xNewObjectStorage;
255 m_xParentStorage = xNewParentStorage;
256 m_aEntryName = aNewName;
257
258 // the linked document should not be switched
259 if ( !m_bIsLinkURL )
260 {
261 uno::Reference< document::XStorageBasedDocument > xDoc( m_xDocHolder->GetComponent(), uno::UNO_QUERY );
262 if ( xDoc.is() )
263 SwitchDocToStorage_Impl( xDoc, m_xObjectStorage );
264 }
265
266 try {
267 if ( xOldObjectStorage.is() )
268 xOldObjectStorage->dispose();
269 }
270 catch ( const uno::Exception& )
271 {
272 }
273 }
274
275
276 void OCommonEmbeddedObject::SwitchOwnPersistence( const uno::Reference< embed::XStorage >& xNewParentStorage,
277 const OUString& aNewName )
278 {
279 if ( xNewParentStorage == m_xParentStorage && aNewName == m_aEntryName )
280 return;
281
282 sal_Int32 nStorageMode = m_bReadOnly ? embed::ElementModes::READ : embed::ElementModes::READWRITE;
283
284 uno::Reference< embed::XStorage > xNewOwnStorage = xNewParentStorage->openStorageElement( aNewName, nStorageMode );
285 SAL_WARN_IF( !xNewOwnStorage.is(), "embeddedobj.common", "The method can not return empty reference!" );
286
287 SwitchOwnPersistence( xNewParentStorage, xNewOwnStorage, aNewName );
288 }
289
290
291 void OCommonEmbeddedObject::EmbedAndReparentDoc_Impl( const uno::Reference< util::XCloseable >& i_rxDocument ) const
292 {
293 SetDocToEmbedded( uno::Reference< frame::XModel >( i_rxDocument, uno::UNO_QUERY ), m_aModuleName );
294
295 try
296 {
297 uno::Reference < container::XChild > xChild( i_rxDocument, uno::UNO_QUERY );
298 if ( xChild.is() )
299 xChild->setParent( m_xParent );
300 }
301 catch( const lang::NoSupportException & )
302 {
303 SAL_WARN( "embeddedobj.common", "OCommonEmbeddedObject::EmbedAndReparentDoc: cannot set parent at document!" );
304 }
305 }
306
307
308 uno::Reference< util::XCloseable > OCommonEmbeddedObject::InitNewDocument_Impl()
309 {
310 uno::Reference< util::XCloseable > xDocument( CreateDocument( m_xContext, GetDocumentServiceName(),
311 m_bEmbeddedScriptSupport, m_bDocumentRecoverySupport ) );
312
313 uno::Reference< frame::XModel > xModel( xDocument, uno::UNO_QUERY );
314 uno::Reference< frame::XLoadable > xLoadable( xModel, uno::UNO_QUERY_THROW );
315
316 try
317 {
318 // set the document mode to embedded as the first action on document!!!
319 EmbedAndReparentDoc_Impl( xDocument );
320
321 // if we have a storage to recover the document from, do not use initNew, but instead load from that storage
322 bool bInitNew = true;
323 if ( m_xRecoveryStorage.is() )
324 {
325 uno::Reference< document::XStorageBasedDocument > xDoc( xLoadable, uno::UNO_QUERY );
326 SAL_WARN_IF( !xDoc.is(), "embeddedobj.common", "OCommonEmbeddedObject::InitNewDocument_Impl: cannot recover from a storage when the document is not storage based!" );
327 if ( xDoc.is() )
328 {
329 ::comphelper::NamedValueCollection aLoadArgs;
330 FillDefaultLoadArgs_Impl( m_xRecoveryStorage, aLoadArgs );
331
332 xDoc->loadFromStorage( m_xRecoveryStorage, aLoadArgs.getPropertyValues() );
333 SwitchDocToStorage_Impl( xDoc, m_xObjectStorage );
334 bInitNew = false;
335 }
336 }
337
338 if ( bInitNew )
339 {
340 // init document as a new
341 xLoadable->initNew();
342 }
343 xModel->attachResource( xModel->getURL(), m_aDocMediaDescriptor );
344 }
345 catch( const uno::Exception& )
346 {
347 if ( xDocument.is() )
348 {
349 try
350 {
351 xDocument->close( true );
352 }
353 catch( const uno::Exception& )
354 {
355 }
356 }
357
358 throw; // TODO
359 }
360
361 return xDocument;
362 }
363
364 bool OCommonEmbeddedObject::getAllowLinkUpdate() const
365 {
366 // assume we can update if we can't determine a parent
367 bool bAllowLinkUpdate(true);
368
369 try
370 {
371 uno::Reference<container::XChild> xParent(m_xParent, uno::UNO_QUERY);
372 while (xParent)
373 {
374 uno::Reference<container::XChild> xGrandParent(xParent->getParent(), uno::UNO_QUERY);
375 if (!xGrandParent)
376 break;
377 xParent = std::move(xGrandParent);
378 }
379
380 uno::Reference<beans::XPropertySet> xPropSet(xParent, uno::UNO_QUERY);
381 if (xPropSet.is())
382 {
383 uno::Any aAny = xPropSet->getPropertyValue(u"AllowLinkUpdate"_ustr);
384 aAny >>= bAllowLinkUpdate;
385 }
386 }
387 catch (const uno::Exception&)
388 {
389 }
390
391 SAL_WARN_IF(!bAllowLinkUpdate, "embeddedobj.common", "getAllowLinkUpdate is false");
392
393 return bAllowLinkUpdate;
394 }
395
396 uno::Reference< util::XCloseable > OCommonEmbeddedObject::LoadLink_Impl()
397 {
398 if (!getAllowLinkUpdate())
399 return nullptr;
400
401 sal_Int32 nLen = m_bLinkHasPassword ? 3 : 2;
402 uno::Sequence< beans::PropertyValue > aArgs( m_aDocMediaDescriptor.getLength() + nLen );
403 auto pArgs = aArgs.getArray();
404
405 OUString sURL;
406 if (m_aLinkTempFile.is())
407 sURL = m_aLinkTempFile->getUri();
408 else
409 sURL = m_aLinkURL;
410 if (INetURLObject(sURL).IsExoticProtocol())
411 {
412 SAL_WARN("embeddedobj.common", "Ignore exotic protocol: " << pArgs[0].Value);
413 return nullptr;
414 }
415
416 pArgs[0].Name = "URL";
417 pArgs[0].Value <<= sURL;
418
419 pArgs[1].Name = "FilterName";
420 pArgs[1].Value <<= m_aLinkFilterName;
421
422 if ( m_bLinkHasPassword )
423 {
424 pArgs[2].Name = "Password";
425 pArgs[2].Value <<= m_aLinkPassword;
426 }
427
428 for ( sal_Int32 nInd = 0; nInd < m_aDocMediaDescriptor.getLength(); nInd++ )
429 {
430 // return early if this document is not trusted to open links
431 if (m_aDocMediaDescriptor[nInd].Name == utl::MediaDescriptor::PROP_REFERRER)
432 {
433 OUString referer;
434 m_aDocMediaDescriptor[nInd].Value >>= referer;
435 if (SvtSecurityOptions::isUntrustedReferer(referer))
436 return nullptr;
437 }
438 pArgs[nInd+nLen].Name = m_aDocMediaDescriptor[nInd].Name;
439 pArgs[nInd+nLen].Value = m_aDocMediaDescriptor[nInd].Value;
440 }
441
442 uno::Reference< util::XCloseable > xDocument( CreateDocument( m_xContext, GetDocumentServiceName(),
443 m_bEmbeddedScriptSupport, m_bDocumentRecoverySupport ) );
444 uno::Reference< frame::XLoadable > xLoadable( xDocument, uno::UNO_QUERY_THROW );
445
446 try
447 {
448 handleLinkedOLE(CopyBackToOLELink::CopyLinkToTemp);
449
450 // the document is not really an embedded one, it is a link
451 EmbedAndReparentDoc_Impl( xDocument );
452
453 // load the document
454 xLoadable->load( aArgs );
455
456 if ( !m_bLinkHasPassword )
457 {
458 // check if there is a password to cache
459 uno::Reference< frame::XModel > xModel( xLoadable, uno::UNO_QUERY_THROW );
460 const uno::Sequence< beans::PropertyValue > aProps = xModel->getArgs();
461 for ( beans::PropertyValue const & prop : aProps )
462 if ( prop.Name == "Password" && ( prop.Value >>= m_aLinkPassword ) )
463 {
464 m_bLinkHasPassword = true;
465 break;
466 }
467 }
468 }
469 catch( const uno::Exception& )
470 {
471 if ( xDocument.is() )
472 {
473 try
474 {
475 xDocument->close( true );
476 }
477 catch( const uno::Exception& )
478 {
479 }
480 }
481
482 throw; // TODO
483 }
484
485 return xDocument;
486
487 }
488
489 OUString OCommonEmbeddedObject::GetFilterName( sal_Int32 nVersion ) const
490 {
491 OUString aFilterName = GetPresetFilterName();
492 if ( aFilterName.isEmpty() )
493 {
494 OUString sDocumentServiceName = GetDocumentServiceName();
495 if (comphelper::IsFuzzing() && nVersion == SOFFICE_FILEFORMAT_CURRENT &&
496 sDocumentServiceName == "com.sun.star.chart2.ChartDocument")
497 {
498 return u"chart8"_ustr;
499 }
500 try {
501 ::comphelper::MimeConfigurationHelper aHelper( m_xContext );
502 aFilterName = aHelper.GetDefaultFilterFromServiceName(sDocumentServiceName, nVersion);
503
504 // If no filter is found, fall back to the FileFormatVersion=6200 filter, Base only has that.
505 if (aFilterName.isEmpty() && nVersion == SOFFICE_FILEFORMAT_CURRENT)
506 aFilterName = aHelper.GetDefaultFilterFromServiceName(GetDocumentServiceName(), SOFFICE_FILEFORMAT_60);
507 } catch( const uno::Exception& )
508 {}
509 }
510
511 return aFilterName;
512 }
513
514
515 void OCommonEmbeddedObject::FillDefaultLoadArgs_Impl( const uno::Reference< embed::XStorage >& i_rxStorage,
516 ::comphelper::NamedValueCollection& o_rLoadArgs ) const
517 {
518 o_rLoadArgs.put( u"DocumentBaseURL"_ustr, GetBaseURL_Impl() );
519 o_rLoadArgs.put( u"HierarchicalDocumentName"_ustr, m_aEntryName );
520 o_rLoadArgs.put( u"ReadOnly"_ustr, m_bReadOnly );
521
522 OUString aFilterName = GetFilterName( ::comphelper::OStorageHelper::GetXStorageFormat( i_rxStorage ) );
523 SAL_WARN_IF( aFilterName.isEmpty(), "embeddedobj.common", "OCommonEmbeddedObject::FillDefaultLoadArgs_Impl: Wrong document service name!" );
524 if ( aFilterName.isEmpty() )
525 throw io::IOException(); // TODO: error message/code
526
527 o_rLoadArgs.put( u"FilterName"_ustr, aFilterName );
528 }
529
530
531 uno::Reference< util::XCloseable > OCommonEmbeddedObject::LoadDocumentFromStorage_Impl()
532 {
533 ENSURE_OR_THROW( m_xObjectStorage.is(), "no object storage" );
534
535 const uno::Reference< embed::XStorage > xSourceStorage( m_xRecoveryStorage.is() ? m_xRecoveryStorage : m_xObjectStorage );
536
537 uno::Reference< util::XCloseable > xDocument( CreateDocument( m_xContext, GetDocumentServiceName(),
538 m_bEmbeddedScriptSupport, m_bDocumentRecoverySupport ) );
539
540 //#i103460# ODF: take the size given from the parent frame as default
541 uno::Reference< chart2::XChartDocument > xChart( xDocument, uno::UNO_QUERY );
542 if( xChart.is() )
543 {
544 uno::Reference< embed::XVisualObject > xChartVisualObject( xChart, uno::UNO_QUERY );
545 if( xChartVisualObject.is() )
546 xChartVisualObject->setVisualAreaSize( embed::Aspects::MSOLE_CONTENT, m_aDefaultSizeForChart_In_100TH_MM );
547 }
548
549 uno::Reference< frame::XLoadable > xLoadable( xDocument, uno::UNO_QUERY );
550 uno::Reference< document::XStorageBasedDocument > xDoc( xDocument, uno::UNO_QUERY );
551 if ( !xDoc.is() && !xLoadable.is() )
552 throw uno::RuntimeException();
553
554 ::comphelper::NamedValueCollection aLoadArgs;
555 FillDefaultLoadArgs_Impl( xSourceStorage, aLoadArgs );
556
557 uno::Reference< io::XInputStream > xTempInpStream;
558 if ( !xDoc.is() )
559 {
560 xTempInpStream = createTempInpStreamFromStor( xSourceStorage, m_xContext );
561 if ( !xTempInpStream.is() )
562 throw uno::RuntimeException();
563
564 OUString aTempFileURL;
565 try
566 {
567 // no need to let the file stay after the stream is removed since the embedded document
568 // can not be stored directly
569 uno::Reference< beans::XPropertySet > xTempStreamProps( xTempInpStream, uno::UNO_QUERY_THROW );
570 xTempStreamProps->getPropertyValue(u"Uri"_ustr) >>= aTempFileURL;
571 }
572 catch( const uno::Exception& )
573 {
574 }
575
576 SAL_WARN_IF( aTempFileURL.isEmpty(), "embeddedobj.common", "Couldn't retrieve temporary file URL!" );
577
578 aLoadArgs.put( u"URL"_ustr, aTempFileURL );
579 aLoadArgs.put( u"InputStream"_ustr, xTempInpStream );
580 }
581
582
583 aLoadArgs.merge( m_aDocMediaDescriptor, true );
584
585 try
586 {
587 // set the document mode to embedded as the first step!!!
588 EmbedAndReparentDoc_Impl( xDocument );
589
590 if (m_bReadOnly)
591 {
592 aLoadArgs.put(u"ReadOnly"_ustr, true);
593 }
594
595 if ( xDoc.is() )
596 {
597 xDoc->loadFromStorage( xSourceStorage, aLoadArgs.getPropertyValues() );
598 if ( xSourceStorage != m_xObjectStorage )
599 SwitchDocToStorage_Impl( xDoc, m_xObjectStorage );
600 }
601 else
602 xLoadable->load( aLoadArgs.getPropertyValues() );
603 }
604 catch( const uno::Exception& )
605 {
606 if ( xDocument.is() )
607 {
608 try
609 {
610 xDocument->close( true );
611 }
612 catch( const uno::Exception& )
613 {
614 DBG_UNHANDLED_EXCEPTION("embeddedobj.common");
615 }
616 }
617
618 throw; // TODO
619 }
620
621 return xDocument;
622 }
623
624
625 uno::Reference< io::XInputStream > OCommonEmbeddedObject::StoreDocumentToTempStream_Impl(
626 sal_Int32 nStorageFormat,
627 const OUString& aBaseURL,
628 const OUString& aHierarchName )
629 {
630 uno::Reference < io::XOutputStream > xTempOut(
631 io::TempFile::create(m_xContext),
632 uno::UNO_QUERY_THROW );
633 uno::Reference< io::XInputStream > aResult( xTempOut, uno::UNO_QUERY_THROW );
634
635 uno::Reference< frame::XStorable > xStorable;
636 {
637 osl::MutexGuard aGuard( m_aMutex );
638 if ( m_xDocHolder.is() )
639 xStorable.set( m_xDocHolder->GetComponent(), uno::UNO_QUERY );
640 }
641
642 if( !xStorable.is() )
643 throw uno::RuntimeException(u"No storage is provided for storing!"_ustr); // TODO:
644
645 OUString aFilterName = GetFilterName( nStorageFormat );
646
647 SAL_WARN_IF( aFilterName.isEmpty(), "embeddedobj.common", "Wrong document service name!" );
648 if ( aFilterName.isEmpty() )
649 throw io::IOException(u"No filter name provided / Wrong document service name"_ustr); // TODO:
650
651 uno::Sequence< beans::PropertyValue > aArgs{
652 comphelper::makePropertyValue(u"FilterName"_ustr, aFilterName),
653 comphelper::makePropertyValue(u"OutputStream"_ustr, xTempOut),
654 comphelper::makePropertyValue(u"DocumentBaseURL"_ustr, aBaseURL),
655 comphelper::makePropertyValue(u"HierarchicalDocumentName"_ustr, aHierarchName)
656 };
657
658 xStorable->storeToURL( u"private:stream"_ustr, aArgs );
659 try
660 {
661 xTempOut->closeOutput();
662 }
663 catch( const uno::Exception& )
664 {
665 SAL_WARN( "embeddedobj.common", "Looks like stream was closed already" );
666 }
667
668 return aResult;
669 }
670
671
672 void OCommonEmbeddedObject::SaveObject_Impl()
673 {
674 if ( !m_xClientSite.is() )
675 return;
676
677 try
678 {
679 // check whether the component is modified,
680 // if not there is no need for storing
681 uno::Reference< util::XModifiable > xModifiable( m_xDocHolder->GetComponent(), uno::UNO_QUERY );
682 if ( xModifiable.is() && !xModifiable->isModified() )
683 return;
684 }
685 catch( const uno::Exception& )
686 {}
687
688 try {
689 m_xClientSite->saveObject();
690 }
691 catch( const uno::Exception& )
692 {
693 SAL_WARN( "embeddedobj.common", "The object was not stored!" );
694 }
695 }
696
697
698 OUString OCommonEmbeddedObject::GetBaseURL_Impl() const
699 {
700 OUString aBaseURL;
701
702 if ( m_xClientSite.is() )
703 {
704 try
705 {
706 uno::Reference< frame::XModel > xParentModel( m_xClientSite->getComponent(), uno::UNO_QUERY_THROW );
707 const uno::Sequence< beans::PropertyValue > aModelProps = xParentModel->getArgs();
708 for ( beans::PropertyValue const & prop : aModelProps )
709 if ( prop.Name == "DocumentBaseURL" )
710 {
711 prop.Value >>= aBaseURL;
712 break;
713 }
714 }
715 catch( const uno::Exception& )
716 {}
717 }
718
719 if ( aBaseURL.isEmpty() )
720 {
721 for ( beans::PropertyValue const & prop : m_aDocMediaDescriptor )
722 if ( prop.Name == "DocumentBaseURL" )
723 {
724 prop.Value >>= aBaseURL;
725 break;
726 }
727 }
728
729 if ( aBaseURL.isEmpty() )
730 aBaseURL = m_aDefaultParentBaseURL;
731
732 return aBaseURL;
733 }
734
735
736 OUString OCommonEmbeddedObject::GetBaseURLFrom_Impl(
737 const uno::Sequence< beans::PropertyValue >& lArguments,
738 const uno::Sequence< beans::PropertyValue >& lObjArgs )
739 {
740 OUString aBaseURL;
741
742 for ( beans::PropertyValue const & prop : lArguments )
743 if ( prop.Name == "DocumentBaseURL" )
744 {
745 prop.Value >>= aBaseURL;
746 break;
747 }
748
749 if ( aBaseURL.isEmpty() )
750 {
751 for ( beans::PropertyValue const & prop : lObjArgs )
752 if ( prop.Name == "DefaultParentBaseURL" )
753 {
754 prop.Value >>= aBaseURL;
755 break;
756 }
757 }
758
759 return aBaseURL;
760 }
761
762
763 void OCommonEmbeddedObject::SwitchDocToStorage_Impl( const uno::Reference< document::XStorageBasedDocument >& xDoc, const uno::Reference< embed::XStorage >& xStorage )
764 {
765 xDoc->switchToStorage( xStorage );
766
767 uno::Reference< util::XModifiable > xModif( xDoc, uno::UNO_QUERY );
768 if ( xModif.is() )
769 xModif->setModified( false );
770
771 if ( m_xRecoveryStorage.is() )
772 m_xRecoveryStorage.clear();
773 }
774
775 namespace {
776
777 beans::PropertyValue getStringPropertyValue(const uno::Sequence<beans::PropertyValue>& rProps,
778 const OUString& rName)
779 {
780 OUString aStr;
781
782 for (beans::PropertyValue const & prop : rProps)
783 {
784 if (prop.Name == rName)
785 {
786 prop.Value >>= aStr;
787 break;
788 }
789 }
790
791 return comphelper::makePropertyValue(rName, aStr);
792 }
793
794 }
795
796 void OCommonEmbeddedObject::StoreDocToStorage_Impl(
797 const uno::Reference<embed::XStorage>& xStorage,
798 const uno::Sequence<beans::PropertyValue>& rMediaArgs,
799 const uno::Sequence<beans::PropertyValue>& rObjArgs,
800 sal_Int32 nStorageFormat,
801 const OUString& aHierarchName,
802 bool bAttachToTheStorage )
803 {
804 SAL_WARN_IF( !xStorage.is(), "embeddedobj.common", "No storage is provided for storing!" );
805
806 if ( !xStorage.is() )
807 throw uno::RuntimeException(); // TODO:
808
809 uno::Reference< document::XStorageBasedDocument > xDoc;
810 {
811 osl::MutexGuard aGuard( m_aMutex );
812 if ( m_xDocHolder.is() )
813 xDoc.set( m_xDocHolder->GetComponent(), uno::UNO_QUERY );
814 }
815
816 OUString aBaseURL = GetBaseURLFrom_Impl(rMediaArgs, rObjArgs);
817
818 if ( xDoc.is() )
819 {
820 OUString aFilterName = GetFilterName( nStorageFormat );
821
822 // No filter found? Try the older format, e.g. Base has only that.
823 if (aFilterName.isEmpty() && nStorageFormat == SOFFICE_FILEFORMAT_CURRENT)
824 aFilterName = GetFilterName( SOFFICE_FILEFORMAT_60 );
825
826 SAL_WARN_IF( aFilterName.isEmpty(), "embeddedobj.common", "Wrong document service name!" );
827 if ( aFilterName.isEmpty() )
828 throw io::IOException(); // TODO:
829
830 uno::Sequence<beans::PropertyValue> aArgs{
831 comphelper::makePropertyValue(u"FilterName"_ustr, aFilterName),
832 comphelper::makePropertyValue(u"HierarchicalDocumentName"_ustr, aHierarchName),
833 comphelper::makePropertyValue(u"DocumentBaseURL"_ustr, aBaseURL),
834 getStringPropertyValue(rObjArgs, u"SourceShellID"_ustr),
835 getStringPropertyValue(rObjArgs, u"DestinationShellID"_ustr),
836 };
837
838 xDoc->storeToStorage( xStorage, aArgs );
839 if ( bAttachToTheStorage )
840 SwitchDocToStorage_Impl( xDoc, xStorage );
841 }
842 else
843 {
844 // store document to temporary stream based on temporary file
845 uno::Reference < io::XInputStream > xTempIn = StoreDocumentToTempStream_Impl( nStorageFormat, aBaseURL, aHierarchName );
846
847 SAL_WARN_IF( !xTempIn.is(), "embeddedobj.common", "The stream reference can not be empty!" );
848
849 // open storage based on document temporary file for reading
850 uno::Reference < lang::XSingleServiceFactory > xStorageFactory = embed::StorageFactory::create(m_xContext);
851
852 uno::Sequence< uno::Any > aArgs{ uno::Any(xTempIn) };
853 uno::Reference< embed::XStorage > xTempStorage( xStorageFactory->createInstanceWithArguments( aArgs ),
854 uno::UNO_QUERY_THROW );
855
856 // object storage must be committed automatically
857 xTempStorage->copyToStorage( xStorage );
858 }
859 }
860
861
862 uno::Reference< util::XCloseable > OCommonEmbeddedObject::CreateDocFromMediaDescr_Impl(
863 const uno::Sequence< beans::PropertyValue >& aMedDescr )
864 {
865 uno::Reference< util::XCloseable > xDocument( CreateDocument( m_xContext, GetDocumentServiceName(),
866 m_bEmbeddedScriptSupport, m_bDocumentRecoverySupport ) );
867
868 uno::Reference< frame::XLoadable > xLoadable( xDocument, uno::UNO_QUERY_THROW );
869
870 try
871 {
872 // set the document mode to embedded as the first action on the document!!!
873 EmbedAndReparentDoc_Impl( xDocument );
874
875 xLoadable->load( addAsTemplate( aMedDescr ) );
876 }
877 catch( const uno::Exception& )
878 {
879 if ( xDocument.is() )
880 {
881 try
882 {
883 xDocument->close( true );
884 }
885 catch( const uno::Exception& )
886 {
887 }
888 }
889
890 throw; // TODO
891 }
892
893 return xDocument;
894 }
895
896
897 uno::Reference< util::XCloseable > OCommonEmbeddedObject::CreateTempDocFromLink_Impl()
898 {
899 uno::Reference< util::XCloseable > xResult;
900
901 SAL_WARN_IF( !m_bIsLinkURL, "embeddedobj.common", "The object is not a linked one!" );
902
903 uno::Sequence< beans::PropertyValue > aTempMediaDescr;
904
905 sal_Int32 nStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
906 try {
907 nStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( m_xParentStorage );
908 }
909 catch ( const beans::IllegalTypeException& )
910 {
911 // the container just has an unknown type, use current file format
912 }
913 catch ( const uno::Exception& )
914 {
915 SAL_WARN( "embeddedobj.common", "Can not retrieve storage media type!" );
916 }
917
918 if ( m_xDocHolder->GetComponent().is() )
919 {
920 aTempMediaDescr.realloc( 4 );
921
922 // TODO/LATER: may be private:stream should be used as target URL
923 OUString aTempFileURL;
924 uno::Reference< io::XInputStream > xTempStream = StoreDocumentToTempStream_Impl( SOFFICE_FILEFORMAT_CURRENT,
925 OUString(),
926 OUString() );
927 try
928 {
929 // no need to let the file stay after the stream is removed since the embedded document
930 // can not be stored directly
931 uno::Reference< beans::XPropertySet > xTempStreamProps( xTempStream, uno::UNO_QUERY_THROW );
932 xTempStreamProps->getPropertyValue(u"Uri"_ustr) >>= aTempFileURL;
933 }
934 catch( const uno::Exception& )
935 {
936 }
937
938 SAL_WARN_IF( aTempFileURL.isEmpty(), "embeddedobj.common", "Couldn't retrieve temporary file URL!" );
939
940 aTempMediaDescr
941 = { comphelper::makePropertyValue(u"URL"_ustr, aTempFileURL),
942 comphelper::makePropertyValue(u"InputStream"_ustr, xTempStream),
943 comphelper::makePropertyValue(u"FilterName"_ustr, GetFilterName( nStorageFormat )),
944 comphelper::makePropertyValue(u"AsTemplate"_ustr, true) };
945 }
946 else
947 {
948 aTempMediaDescr = { comphelper::makePropertyValue(
949 u"URL"_ustr,
950 // tdf#141529 use URL of the linked TempFile if it exists
951 m_aLinkTempFile.is() ? m_aLinkTempFile->getUri() : m_aLinkURL),
952 comphelper::makePropertyValue(u"FilterName"_ustr, m_aLinkFilterName) };
953 }
954
955 xResult = CreateDocFromMediaDescr_Impl( aTempMediaDescr );
956
957 return xResult;
958 }
959
960
961 void SAL_CALL OCommonEmbeddedObject::setPersistentEntry(
962 const uno::Reference< embed::XStorage >& xStorage,
963 const OUString& sEntName,
964 sal_Int32 nEntryConnectionMode,
965 const uno::Sequence< beans::PropertyValue >& lArguments,
966 const uno::Sequence< beans::PropertyValue >& lObjArgs )
967 {
968 // the type of the object must be already set
969 // a kind of typedetection should be done in the factory
970
971 ::osl::MutexGuard aGuard( m_aMutex );
972 if ( m_bDisposed )
973 throw lang::DisposedException(); // TODO
974
975 if ( !xStorage.is() )
976 throw lang::IllegalArgumentException( u"No parent storage is provided!"_ustr,
977 static_cast< ::cppu::OWeakObject* >(this),
978 1 );
979
980 if ( sEntName.isEmpty() )
981 throw lang::IllegalArgumentException( u"Empty element name is provided!"_ustr,
982 static_cast< ::cppu::OWeakObject* >(this),
983 2 );
984
985 // May be LOADED should be forbidden here ???
986 if ( ( m_nObjectState != -1 || nEntryConnectionMode == embed::EntryInitModes::NO_INIT )
987 && ( m_nObjectState == -1 || nEntryConnectionMode != embed::EntryInitModes::NO_INIT ) )
988 {
989 // if the object is not loaded
990 // it can not get persistent representation without initialization
991
992 // if the object is loaded
993 // it can switch persistent representation only without initialization
994
995 throw embed::WrongStateException(
996 u"Can't change persistent representation of activated object!"_ustr,
997 static_cast< ::cppu::OWeakObject* >(this) );
998 }
999
1000 if ( m_bWaitSaveCompleted )
1001 {
1002 if ( nEntryConnectionMode != embed::EntryInitModes::NO_INIT )
1003 throw embed::WrongStateException(
1004 u"The object waits for saveCompleted() call!"_ustr,
1005 static_cast< ::cppu::OWeakObject* >(this) );
1006 // saveCompleted is expected, handle it accordingly
1007 if ( m_xNewParentStorage == xStorage && m_aNewEntryName == sEntName )
1008 {
1009 saveCompleted( true );
1010 return;
1011 }
1012
1013 // if a completely different entry is provided, switch first back to the old persistence in saveCompleted
1014 // and then switch to the target persistence
1015 bool bSwitchFurther = ( m_xParentStorage != xStorage || m_aEntryName != sEntName );
1016 saveCompleted( false );
1017 if ( !bSwitchFurther )
1018 return;
1019 }
1020
1021 // for now support of this interface is required to allow breaking of links and converting them to normal embedded
1022 // objects, so the persist name must be handled correctly ( althowgh no real persist entry is used )
1023 // OSL_ENSURE( !m_bIsLinkURL, "This method implementation must not be used for links!" );
1024 if ( m_bIsLinkURL )
1025 {
1026 m_aEntryName = sEntName;
1027 return;
1028 }
1029
1030 uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY_THROW );
1031
1032 // detect entry existence
1033 bool bElExists = xNameAccess->hasByName( sEntName );
1034
1035 m_aDocMediaDescriptor = GetValuableArgs_Impl( lArguments,
1036 nEntryConnectionMode != embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT );
1037
1038 m_bReadOnly = false;
1039 for ( beans::PropertyValue const & prop : lArguments )
1040 if ( prop.Name == "ReadOnly" )
1041 prop.Value >>= m_bReadOnly;
1042
1043 // TODO: use lObjArgs for StoreVisualReplacement
1044 for ( beans::PropertyValue const & prop : lObjArgs )
1045 if ( prop.Name == "OutplaceDispatchInterceptor" )
1046 {
1047 uno::Reference< frame::XDispatchProviderInterceptor > xDispatchInterceptor;
1048 if ( prop.Value >>= xDispatchInterceptor )
1049 m_xDocHolder->SetOutplaceDispatchInterceptor( xDispatchInterceptor );
1050 }
1051 else if ( prop.Name == "DefaultParentBaseURL" )
1052 {
1053 prop.Value >>= m_aDefaultParentBaseURL;
1054 }
1055 else if ( prop.Name == "Parent" )
1056 {
1057 prop.Value >>= m_xParent;
1058 }
1059 else if ( prop.Name == "IndividualMiscStatus" )
1060 {
1061 sal_Int64 nMiscStatus=0;
1062 prop.Value >>= nMiscStatus;
1063 m_nMiscStatus |= nMiscStatus;
1064 }
1065 else if ( prop.Name == "CloneFrom" )
1066 {
1067 uno::Reference < embed::XEmbeddedObject > xObj;
1068 prop.Value >>= xObj;
1069 if ( xObj.is() )
1070 {
1071 m_bHasClonedSize = true;
1072 m_aClonedSize = xObj->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
1073 m_nClonedMapUnit = xObj->getMapUnit( embed::Aspects::MSOLE_CONTENT );
1074 }
1075 }
1076 else if ( prop.Name == "OutplaceFrameProperties" )
1077 {
1078 uno::Sequence< uno::Any > aOutFrameProps;
1079 uno::Sequence< beans::NamedValue > aOutFramePropsTyped;
1080 if ( prop.Value >>= aOutFrameProps )
1081 {
1082 m_xDocHolder->SetOutplaceFrameProperties( aOutFrameProps );
1083 }
1084 else if ( prop.Value >>= aOutFramePropsTyped )
1085 {
1086 aOutFrameProps.realloc( aOutFramePropsTyped.getLength() );
1087 uno::Any* pProp = aOutFrameProps.getArray();
1088 for ( const beans::NamedValue* pTypedProp = aOutFramePropsTyped.getConstArray();
1089 pTypedProp != aOutFramePropsTyped.getConstArray() + aOutFramePropsTyped.getLength();
1090 ++pTypedProp, ++pProp
1091 )
1092 {
1093 *pProp <<= *pTypedProp;
1094 }
1095 m_xDocHolder->SetOutplaceFrameProperties( aOutFrameProps );
1096 }
1097 else
1098 SAL_WARN( "embeddedobj.common", "OCommonEmbeddedObject::setPersistentEntry: illegal type for argument 'OutplaceFrameProperties'!" );
1099 }
1100 else if ( prop.Name == "ModuleName" )
1101 {
1102 prop.Value >>= m_aModuleName;
1103 }
1104 else if ( prop.Name == "EmbeddedScriptSupport" )
1105 {
1106 OSL_VERIFY( prop.Value >>= m_bEmbeddedScriptSupport );
1107 }
1108 else if ( prop.Name == "DocumentRecoverySupport" )
1109 {
1110 OSL_VERIFY( prop.Value >>= m_bDocumentRecoverySupport );
1111 }
1112 else if ( prop.Name == "RecoveryStorage" )
1113 {
1114 OSL_VERIFY( prop.Value >>= m_xRecoveryStorage );
1115 }
1116
1117
1118 sal_Int32 nStorageMode = m_bReadOnly ? embed::ElementModes::READ : embed::ElementModes::READWRITE;
1119
1120 SwitchOwnPersistence( xStorage, sEntName );
1121
1122 if ( nEntryConnectionMode == embed::EntryInitModes::DEFAULT_INIT )
1123 {
1124 if ( bElExists )
1125 {
1126 // the initialization from existing storage allows to leave object in loaded state
1127 m_nObjectState = embed::EmbedStates::LOADED;
1128 }
1129 else
1130 {
1131 m_xDocHolder->SetComponent( InitNewDocument_Impl(), m_bReadOnly );
1132 if ( !m_xDocHolder->GetComponent().is() )
1133 throw io::IOException(); // TODO: can not create document
1134
1135 m_nObjectState = embed::EmbedStates::RUNNING;
1136 }
1137 }
1138 else
1139 {
1140 if ( ( nStorageMode & embed::ElementModes::READWRITE ) != embed::ElementModes::READWRITE )
1141 throw io::IOException();
1142
1143 if ( nEntryConnectionMode == embed::EntryInitModes::NO_INIT )
1144 {
1145 // the document just already changed its storage to store to
1146 // the links to OOo documents for now ignore this call
1147 // TODO: OOo links will have persistence so it will be switched here
1148 }
1149 else if ( nEntryConnectionMode == embed::EntryInitModes::TRUNCATE_INIT )
1150 {
1151 if ( m_xRecoveryStorage.is() )
1152 TransferMediaType( m_xRecoveryStorage, m_xObjectStorage );
1153
1154 // TODO:
1155 m_xDocHolder->SetComponent( InitNewDocument_Impl(), m_bReadOnly );
1156
1157 if ( !m_xDocHolder->GetComponent().is() )
1158 throw io::IOException(); // TODO: can not create document
1159
1160 m_nObjectState = embed::EmbedStates::RUNNING;
1161 }
1162 else if ( nEntryConnectionMode == embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT )
1163 {
1164 m_xDocHolder->SetComponent( CreateDocFromMediaDescr_Impl( lArguments ), m_bReadOnly );
1165 m_nObjectState = embed::EmbedStates::RUNNING;
1166 }
1167 //else if ( nEntryConnectionMode == embed::EntryInitModes::TRANSFERABLE_INIT )
1168 //{
1169 //TODO:
1170 //}
1171 else
1172 throw lang::IllegalArgumentException( u"Wrong connection mode is provided!"_ustr,
1173 static_cast< ::cppu::OWeakObject* >(this),
1174 3 );
1175 }
1176 }
1177
1178
1179 void SAL_CALL OCommonEmbeddedObject::storeToEntry( const uno::Reference< embed::XStorage >& xStorage,
1180 const OUString& sEntName,
1181 const uno::Sequence< beans::PropertyValue >& lArguments,
1182 const uno::Sequence< beans::PropertyValue >& lObjArgs )
1183 {
1184 ::osl::ResettableMutexGuard aGuard( m_aMutex );
1185 if ( m_bDisposed )
1186 throw lang::DisposedException(); // TODO
1187
1188 if ( m_nObjectState == -1 )
1189 {
1190 // the object is still not loaded
1191 throw embed::WrongStateException( u"Can't store object without persistence!"_ustr,
1192 static_cast< ::cppu::OWeakObject* >(this) );
1193 }
1194
1195 if ( m_bWaitSaveCompleted )
1196 throw embed::WrongStateException(
1197 u"The object waits for saveCompleted() call!"_ustr,
1198 static_cast< ::cppu::OWeakObject* >(this) );
1199
1200 // for now support of this interface is required to allow breaking of links and converting them to normal embedded
1201 // objects, so the persist name must be handled correctly ( althowgh no real persist entry is used )
1202 // OSL_ENSURE( !m_bIsLinkURL, "This method implementation must not be used for links!" );
1203 if ( m_bIsLinkURL )
1204 return;
1205
1206 OSL_ENSURE( m_xParentStorage.is() && m_xObjectStorage.is(), "The object has no valid persistence!" );
1207
1208 sal_Int32 nTargetStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1209 sal_Int32 nOriginalStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1210 try {
1211 nTargetStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( xStorage );
1212 }
1213 catch ( const beans::IllegalTypeException& )
1214 {
1215 // the container just has an unknown type, use current file format
1216 }
1217 catch ( const uno::Exception& )
1218 {
1219 SAL_WARN( "embeddedobj.common", "Can not retrieve target storage media type!" );
1220 }
1221 if (nTargetStorageFormat == SOFFICE_FILEFORMAT_60)
1222 {
1223 SAL_INFO("embeddedobj.common", "fdo#78159: Storing OOoXML as ODF");
1224 nTargetStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1225 // setting MediaType is done later anyway, no need to do it here
1226 }
1227
1228 try
1229 {
1230 nOriginalStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( m_xParentStorage );
1231 }
1232 catch ( const beans::IllegalTypeException& )
1233 {
1234 // the container just has an unknown type, use current file format
1235 }
1236 catch ( const uno::Exception& )
1237 {
1238 SAL_WARN( "embeddedobj.common", "Can not retrieve own storage media type!" );
1239 }
1240
1241 bool bTryOptimization = false;
1242 for ( beans::PropertyValue const & prop : lObjArgs )
1243 {
1244 // StoreVisualReplacement and VisualReplacement args have no sense here
1245 if ( prop.Name == "CanTryOptimization" )
1246 prop.Value >>= bTryOptimization;
1247 }
1248
1249 bool bSwitchBackToLoaded = false;
1250
1251 // Storing to different format can be done only in running state.
1252 if ( m_nObjectState == embed::EmbedStates::LOADED )
1253 {
1254 // TODO/LATER: copying is not legal for documents with relative links.
1255 if ( nTargetStorageFormat == nOriginalStorageFormat )
1256 {
1257 bool bOptimizationWorks = false;
1258 if ( bTryOptimization )
1259 {
1260 try
1261 {
1262 // try to use optimized copying
1263 uno::Reference< embed::XOptimizedStorage > xSource( m_xParentStorage, uno::UNO_QUERY_THROW );
1264 uno::Reference< embed::XOptimizedStorage > xTarget( xStorage, uno::UNO_QUERY_THROW );
1265 xSource->copyElementDirectlyTo( m_aEntryName, xTarget, sEntName );
1266 bOptimizationWorks = true;
1267 }
1268 catch( const uno::Exception& )
1269 {
1270 }
1271 }
1272
1273 if ( !bOptimizationWorks )
1274 m_xParentStorage->copyElementTo( m_aEntryName, xStorage, sEntName );
1275 }
1276 else
1277 {
1278 changeState( embed::EmbedStates::RUNNING );
1279 bSwitchBackToLoaded = true;
1280 }
1281 }
1282
1283 if ( m_nObjectState == embed::EmbedStates::LOADED )
1284 return;
1285
1286 uno::Reference< embed::XStorage > xSubStorage =
1287 xStorage->openStorageElement( sEntName, embed::ElementModes::READWRITE );
1288
1289 if ( !xSubStorage.is() )
1290 throw uno::RuntimeException(); //TODO
1291
1292 aGuard.clear();
1293 // TODO/LATER: support hierarchical name for embedded objects in embedded objects
1294 StoreDocToStorage_Impl(
1295 xSubStorage, lArguments, lObjArgs, nTargetStorageFormat, sEntName, false );
1296 aGuard.reset();
1297
1298 if ( bSwitchBackToLoaded )
1299 changeState( embed::EmbedStates::LOADED );
1300
1301 // TODO: should the listener notification be done?
1302 }
1303
1304
1305 void SAL_CALL OCommonEmbeddedObject::storeAsEntry( const uno::Reference< embed::XStorage >& xStorage,
1306 const OUString& sEntName,
1307 const uno::Sequence< beans::PropertyValue >& lArguments,
1308 const uno::Sequence< beans::PropertyValue >& lObjArgs )
1309 {
1310 ::osl::ResettableMutexGuard aGuard( m_aMutex );
1311 if ( m_bDisposed )
1312 throw lang::DisposedException(); // TODO
1313
1314 bool AutoSaveEvent = false;
1315 utl::MediaDescriptor lArgs(lObjArgs);
1316 lArgs[utl::MediaDescriptor::PROP_AUTOSAVEEVENT] >>= AutoSaveEvent;
1317
1318 if ( m_nObjectState == -1 )
1319 {
1320 // the object is still not loaded
1321 throw embed::WrongStateException( u"Can't store object without persistence!"_ustr,
1322 static_cast< ::cppu::OWeakObject* >(this) );
1323 }
1324
1325 if ( m_bWaitSaveCompleted )
1326 throw embed::WrongStateException(
1327 u"The object waits for saveCompleted() call!"_ustr,
1328 static_cast< ::cppu::OWeakObject* >(this) );
1329
1330 // for now support of this interface is required to allow breaking of links and converting them to normal embedded
1331 // objects, so the persist name must be handled correctly ( althowgh no real persist entry is used )
1332 // OSL_ENSURE( !m_bIsLinkURL, "This method implementation must not be used for links!" );
1333 if ( m_bIsLinkURL )
1334 {
1335 m_aNewEntryName = sEntName;
1336
1337 if ( !AutoSaveEvent )
1338 handleLinkedOLE(CopyBackToOLELink::CopyTempToLink);
1339
1340 return;
1341 }
1342
1343 OSL_ENSURE( m_xParentStorage.is() && m_xObjectStorage.is(), "The object has no valid persistence!" );
1344
1345 sal_Int32 nTargetStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1346 sal_Int32 nOriginalStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1347 try {
1348 nTargetStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( xStorage );
1349 }
1350 catch ( const beans::IllegalTypeException& )
1351 {
1352 // the container just has an unknown type, use current file format
1353 }
1354 catch ( const uno::Exception& )
1355 {
1356 SAL_WARN( "embeddedobj.common", "Can not retrieve target storage media type!" );
1357 }
1358 if (nTargetStorageFormat == SOFFICE_FILEFORMAT_60)
1359 {
1360 SAL_INFO("embeddedobj.common", "fdo#78159: Storing OOoXML as ODF");
1361 nTargetStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1362 // setting MediaType is done later anyway, no need to do it here
1363 }
1364
1365 try
1366 {
1367 nOriginalStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( m_xParentStorage );
1368 }
1369 catch ( const beans::IllegalTypeException& )
1370 {
1371 // the container just has an unknown type, use current file format
1372 }
1373 catch ( const uno::Exception& )
1374 {
1375 SAL_WARN( "embeddedobj.common", "Can not retrieve own storage media type!" );
1376 }
1377
1378 PostEvent_Impl( u"OnSaveAs"_ustr );
1379
1380 bool bTryOptimization = false;
1381 for ( beans::PropertyValue const & prop : lObjArgs )
1382 {
1383 // StoreVisualReplacement and VisualReplacement args have no sense here
1384 if ( prop.Name == "CanTryOptimization" )
1385 prop.Value >>= bTryOptimization;
1386 }
1387
1388 bool bSwitchBackToLoaded = false;
1389
1390 // Storing to different format can be done only in running state.
1391 if ( m_nObjectState == embed::EmbedStates::LOADED )
1392 {
1393 // TODO/LATER: copying is not legal for documents with relative links.
1394 if ( nTargetStorageFormat == nOriginalStorageFormat )
1395 {
1396 bool bOptimizationWorks = false;
1397 if ( bTryOptimization )
1398 {
1399 try
1400 {
1401 // try to use optimized copying
1402 uno::Reference< embed::XOptimizedStorage > xSource( m_xParentStorage, uno::UNO_QUERY_THROW );
1403 uno::Reference< embed::XOptimizedStorage > xTarget( xStorage, uno::UNO_QUERY_THROW );
1404 xSource->copyElementDirectlyTo( m_aEntryName, xTarget, sEntName );
1405 bOptimizationWorks = true;
1406 }
1407 catch( const uno::Exception& )
1408 {
1409 }
1410 }
1411
1412 if ( !bOptimizationWorks )
1413 m_xParentStorage->copyElementTo( m_aEntryName, xStorage, sEntName );
1414 }
1415 else
1416 {
1417 changeState( embed::EmbedStates::RUNNING );
1418 bSwitchBackToLoaded = true;
1419 }
1420 }
1421
1422 uno::Reference< embed::XStorage > xSubStorage =
1423 xStorage->openStorageElement( sEntName, embed::ElementModes::READWRITE );
1424
1425 if ( !xSubStorage.is() )
1426 throw uno::RuntimeException(); //TODO
1427
1428 if ( m_nObjectState != embed::EmbedStates::LOADED )
1429 {
1430 aGuard.clear();
1431 // TODO/LATER: support hierarchical name for embedded objects in embedded objects
1432 StoreDocToStorage_Impl(
1433 xSubStorage, lArguments, lObjArgs, nTargetStorageFormat, sEntName, false );
1434 aGuard.reset();
1435
1436 if ( bSwitchBackToLoaded )
1437 changeState( embed::EmbedStates::LOADED );
1438 }
1439
1440 m_bWaitSaveCompleted = true;
1441 m_xNewObjectStorage = std::move(xSubStorage);
1442 m_xNewParentStorage = xStorage;
1443 m_aNewEntryName = sEntName;
1444 m_aNewDocMediaDescriptor = GetValuableArgs_Impl( lArguments, true );
1445
1446 // TODO: register listeners for storages above, in case they are disposed
1447 // an exception will be thrown on saveCompleted( true )
1448
1449 // TODO: should the listener notification be done here or in saveCompleted?
1450 }
1451
1452
1453 void SAL_CALL OCommonEmbeddedObject::saveCompleted( sal_Bool bUseNew )
1454 {
1455 ::osl::MutexGuard aGuard( m_aMutex );
1456 if ( m_bDisposed )
1457 throw lang::DisposedException(); // TODO
1458
1459 if ( m_nObjectState == -1 )
1460 {
1461 // the object is still not loaded
1462 throw embed::WrongStateException( u"Can't store object without persistence!"_ustr,
1463 static_cast< ::cppu::OWeakObject* >(this) );
1464 }
1465
1466 // for now support of this interface is required to allow breaking of links and converting them to normal embedded
1467 // objects, so the persist name must be handled correctly ( althowgh no real persist entry is used )
1468 // OSL_ENSURE( !m_bIsLinkURL, "This method implementation must not be used for links!" );
1469 if ( m_bIsLinkURL )
1470 {
1471 if ( bUseNew )
1472 m_aEntryName = m_aNewEntryName;
1473 m_aNewEntryName.clear();
1474 return;
1475 }
1476
1477 // it is allowed to call saveCompleted( false ) for nonstored objects
1478 if ( !m_bWaitSaveCompleted && !bUseNew )
1479 return;
1480
1481 SAL_WARN_IF( !m_bWaitSaveCompleted, "embeddedobj.common", "Unexpected saveCompleted() call!" );
1482 if ( !m_bWaitSaveCompleted )
1483 throw io::IOException(); // TODO: illegal call
1484
1485 OSL_ENSURE( m_xNewObjectStorage.is() && m_xNewParentStorage.is() , "Internal object information is broken!" );
1486 if ( !m_xNewObjectStorage.is() || !m_xNewParentStorage.is() )
1487 throw uno::RuntimeException(); // TODO: broken internal information
1488
1489 if ( bUseNew )
1490 {
1491 SwitchOwnPersistence( m_xNewParentStorage, m_xNewObjectStorage, m_aNewEntryName );
1492 m_aDocMediaDescriptor = m_aNewDocMediaDescriptor;
1493
1494 uno::Reference< util::XModifiable > xModif( m_xDocHolder->GetComponent(), uno::UNO_QUERY );
1495 if ( xModif.is() )
1496 xModif->setModified( false );
1497
1498 PostEvent_Impl( u"OnSaveAsDone"_ustr);
1499 }
1500 else
1501 {
1502 try {
1503 m_xNewObjectStorage->dispose();
1504 }
1505 catch ( const uno::Exception& )
1506 {
1507 }
1508 }
1509
1510 m_xNewObjectStorage.clear();
1511 m_xNewParentStorage.clear();
1512 m_aNewEntryName.clear();
1513 m_aNewDocMediaDescriptor.realloc( 0 );
1514 m_bWaitSaveCompleted = false;
1515
1516 if ( bUseNew )
1517 {
1518 // TODO: notify listeners
1519
1520 if ( m_nUpdateMode == embed::EmbedUpdateModes::ALWAYS_UPDATE )
1521 {
1522 // TODO: update visual representation
1523 }
1524 }
1525 }
1526
1527
1528 sal_Bool SAL_CALL OCommonEmbeddedObject::hasEntry()
1529 {
1530 ::osl::MutexGuard aGuard( m_aMutex );
1531 if ( m_bDisposed )
1532 throw lang::DisposedException(); // TODO
1533
1534 if ( m_bWaitSaveCompleted )
1535 throw embed::WrongStateException(
1536 u"The object waits for saveCompleted() call!"_ustr,
1537 static_cast< ::cppu::OWeakObject* >(this) );
1538
1539 if ( m_xObjectStorage.is() )
1540 return true;
1541
1542 return false;
1543 }
1544
1545
1546 OUString SAL_CALL OCommonEmbeddedObject::getEntryName()
1547 {
1548 ::osl::MutexGuard aGuard( m_aMutex );
1549 if ( m_bDisposed )
1550 throw lang::DisposedException(); // TODO
1551
1552 if ( m_nObjectState == -1 )
1553 {
1554 // the object is still not loaded
1555 throw embed::WrongStateException( u"The object persistence is not initialized!"_ustr,
1556 static_cast< ::cppu::OWeakObject* >(this) );
1557 }
1558
1559 if ( m_bWaitSaveCompleted )
1560 throw embed::WrongStateException(
1561 u"The object waits for saveCompleted() call!"_ustr,
1562 static_cast< ::cppu::OWeakObject* >(this) );
1563
1564 return m_aEntryName;
1565 }
1566
1567
1568 void SAL_CALL OCommonEmbeddedObject::storeOwn()
1569 {
1570 // during switching from Activated to Running and from Running to Loaded states the object will
1571 // ask container to store the object, the container has to make decision
1572 // to do so or not
1573
1574 ::osl::ResettableMutexGuard aGuard( m_aMutex );
1575 if ( m_bDisposed )
1576 throw lang::DisposedException(); // TODO
1577
1578 if ( m_nObjectState == -1 )
1579 {
1580 // the object is still not loaded
1581 throw embed::WrongStateException( u"Can't store object without persistence!"_ustr,
1582 static_cast< ::cppu::OWeakObject* >(this) );
1583 }
1584
1585 if ( m_bWaitSaveCompleted )
1586 throw embed::WrongStateException(
1587 u"The object waits for saveCompleted() call!"_ustr,
1588 static_cast< ::cppu::OWeakObject* >(this) );
1589
1590 if ( m_bReadOnly )
1591 throw io::IOException(); // TODO: access denied
1592
1593 // nothing to do, if the object is in loaded state
1594 if ( m_nObjectState == embed::EmbedStates::LOADED )
1595 return;
1596
1597 PostEvent_Impl( u"OnSave"_ustr );
1598
1599 SAL_WARN_IF( !m_xDocHolder->GetComponent().is(), "embeddedobj.common", "If an object is activated or in running state it must have a document!" );
1600 if ( !m_xDocHolder->GetComponent().is() )
1601 throw uno::RuntimeException();
1602
1603 if ( m_bIsLinkURL )
1604 {
1605 // TODO: just store the document to its location
1606 uno::Reference< frame::XStorable > xStorable( m_xDocHolder->GetComponent(), uno::UNO_QUERY_THROW );
1607
1608 // free the main mutex for the storing time
1609 aGuard.clear();
1610
1611 xStorable->store();
1612
1613 aGuard.reset();
1614 }
1615 else
1616 {
1617 OSL_ENSURE( m_xParentStorage.is() && m_xObjectStorage.is(), "The object has no valid persistence!" );
1618
1619 if ( !m_xObjectStorage.is() )
1620 throw io::IOException(); //TODO: access denied
1621
1622 sal_Int32 nStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1623 try {
1624 nStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( m_xParentStorage );
1625 }
1626 catch ( const beans::IllegalTypeException& )
1627 {
1628 // the container just has an unknown type, use current file format
1629 }
1630 catch ( const uno::Exception& )
1631 {
1632 SAL_WARN( "embeddedobj.common", "Can not retrieve storage media type!" );
1633 }
1634 if (nStorageFormat == SOFFICE_FILEFORMAT_60)
1635 {
1636 SAL_INFO("embeddedobj.common", "fdo#78159: Storing OOoXML as ODF");
1637 nStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1638 // setting MediaType is done later anyway, no need to do it here
1639 }
1640
1641 aGuard.clear();
1642 uno::Sequence<beans::PropertyValue> aEmpty;
1643 uno::Sequence<beans::PropertyValue> aMediaArgs{ comphelper::makePropertyValue(
1644 u"DocumentBaseURL"_ustr, GetBaseURL_Impl()) };
1645 StoreDocToStorage_Impl( m_xObjectStorage, aMediaArgs, aEmpty, nStorageFormat, m_aEntryName, true );
1646 aGuard.reset();
1647 }
1648
1649 uno::Reference< util::XModifiable > xModif( m_xDocHolder->GetComponent(), uno::UNO_QUERY );
1650 if ( xModif.is() )
1651 xModif->setModified( false );
1652
1653 PostEvent_Impl( u"OnSaveDone"_ustr );
1654 }
1655
1656
1657 sal_Bool SAL_CALL OCommonEmbeddedObject::isReadonly()
1658 {
1659 ::osl::MutexGuard aGuard( m_aMutex );
1660 if ( m_bDisposed )
1661 throw lang::DisposedException(); // TODO
1662
1663 if ( m_nObjectState == -1 )
1664 {
1665 // the object is still not loaded
1666 throw embed::WrongStateException( u"The object persistence is not initialized!"_ustr,
1667 static_cast< ::cppu::OWeakObject* >(this) );
1668 }
1669
1670 if ( m_bWaitSaveCompleted )
1671 throw embed::WrongStateException(
1672 u"The object waits for saveCompleted() call!"_ustr,
1673 static_cast< ::cppu::OWeakObject* >(this) );
1674
1675 return m_bReadOnly;
1676 }
1677
1678
1679 void SAL_CALL OCommonEmbeddedObject::reload(
1680 const uno::Sequence< beans::PropertyValue >& lArguments,
1681 const uno::Sequence< beans::PropertyValue >& lObjArgs )
1682 {
1683 // TODO: use lObjArgs
1684 // for now this method is used only to switch readonly state
1685
1686 ::osl::MutexGuard aGuard( m_aMutex );
1687 if ( m_bDisposed )
1688 throw lang::DisposedException(); // TODO
1689
1690 if ( m_nObjectState == -1 )
1691 {
1692 // the object is still not loaded
1693 throw embed::WrongStateException( u"The object persistence is not initialized!"_ustr,
1694 static_cast< ::cppu::OWeakObject* >(this) );
1695 }
1696
1697 if ( m_nObjectState != embed::EmbedStates::LOADED )
1698 {
1699 // the object is still not loaded
1700 throw embed::WrongStateException(
1701 u"The object must be in loaded state to be reloaded!"_ustr,
1702 static_cast< ::cppu::OWeakObject* >(this) );
1703 }
1704
1705 if ( m_bWaitSaveCompleted )
1706 throw embed::WrongStateException(
1707 u"The object waits for saveCompleted() call!"_ustr,
1708 static_cast< ::cppu::OWeakObject* >(this) );
1709
1710 if ( m_bIsLinkURL )
1711 {
1712 // reload of the link
1713 OUString aOldLinkFilter = m_aLinkFilterName;
1714
1715 OUString aNewLinkFilter;
1716 for ( beans::PropertyValue const & prop : lArguments )
1717 {
1718 if ( prop.Name == "URL" )
1719 {
1720 // the new URL
1721 prop.Value >>= m_aLinkURL;
1722 m_aLinkFilterName.clear();
1723 }
1724 else if ( prop.Name == "FilterName" )
1725 {
1726 prop.Value >>= aNewLinkFilter;
1727 m_aLinkFilterName.clear();
1728 }
1729 }
1730
1731 ::comphelper::MimeConfigurationHelper aHelper( m_xContext );
1732 if ( m_aLinkFilterName.isEmpty() )
1733 {
1734 if ( !aNewLinkFilter.isEmpty() )
1735 m_aLinkFilterName = aNewLinkFilter;
1736 else
1737 {
1738 uno::Sequence< beans::PropertyValue > aArgs{ comphelper::makePropertyValue(
1739 u"URL"_ustr, m_aLinkURL) };
1740 m_aLinkFilterName = aHelper.UpdateMediaDescriptorWithFilterName( aArgs, false );
1741 }
1742 }
1743
1744 if ( aOldLinkFilter != m_aLinkFilterName )
1745 {
1746 uno::Sequence< beans::NamedValue > aObject = aHelper.GetObjectPropsByFilter( m_aLinkFilterName );
1747
1748 // TODO/LATER: probably the document holder could be cleaned explicitly as in the destructor
1749 m_xDocHolder.clear();
1750
1751 LinkInit_Impl( aObject, lArguments, lObjArgs );
1752 }
1753 }
1754
1755 m_aDocMediaDescriptor = GetValuableArgs_Impl( lArguments, true );
1756
1757 // TODO: use lObjArgs for StoreVisualReplacement
1758 for ( beans::PropertyValue const & prop : lObjArgs )
1759 if ( prop.Name == "OutplaceDispatchInterceptor" )
1760 {
1761 uno::Reference< frame::XDispatchProviderInterceptor > xDispatchInterceptor;
1762 if ( prop.Value >>= xDispatchInterceptor )
1763 m_xDocHolder->SetOutplaceDispatchInterceptor( xDispatchInterceptor );
1764
1765 break;
1766 }
1767
1768 // TODO:
1769 // when document allows reloading through API the object can be reloaded not only in loaded state
1770
1771 bool bOldReadOnlyValue = m_bReadOnly;
1772
1773 m_bReadOnly = false;
1774 for ( beans::PropertyValue const & prop : lArguments )
1775 if ( prop.Name == "ReadOnly" )
1776 prop.Value >>= m_bReadOnly;
1777
1778 if ( bOldReadOnlyValue == m_bReadOnly || m_bIsLinkURL )
1779 return;
1780
1781 // close own storage
1782 try {
1783 if ( m_xObjectStorage.is() )
1784 m_xObjectStorage->dispose();
1785 }
1786 catch ( const uno::Exception& )
1787 {
1788 }
1789
1790 sal_Int32 nStorageMode = m_bReadOnly ? embed::ElementModes::READ : embed::ElementModes::READWRITE;
1791 m_xObjectStorage = m_xParentStorage->openStorageElement( m_aEntryName, nStorageMode );
1792 }
1793
1794 sal_Bool SAL_CALL OCommonEmbeddedObject::isStored()
1795 {
1796 if (!m_xObjectStorage.is())
1797 return false;
1798
1799 return m_xObjectStorage->getElementNames().hasElements();
1800 }
1801
1802
1803 void SAL_CALL OCommonEmbeddedObject::breakLink( const uno::Reference< embed::XStorage >& xStorage,
1804 const OUString& sEntName )
1805 {
1806 ::osl::ResettableMutexGuard aGuard( m_aMutex );
1807 if ( m_bDisposed )
1808 throw lang::DisposedException(); // TODO
1809
1810 if (!m_bIsLinkURL || m_nObjectState == -1)
1811 {
1812 // it must be a linked initialized object
1813 throw embed::WrongStateException(
1814 u"The object is not a valid linked object!"_ustr,
1815 static_cast< ::cppu::OWeakObject* >(this) );
1816 }
1817 // the current implementation of OOo links does not implement this method since it does not implement
1818 // all the set of interfaces required for OOo embedded object ( XEmbedPersist is not supported ).
1819
1820 if ( !xStorage.is() )
1821 throw lang::IllegalArgumentException( u"No parent storage is provided!"_ustr,
1822 static_cast< ::cppu::OWeakObject* >(this),
1823 1 );
1824
1825 if ( sEntName.isEmpty() )
1826 throw lang::IllegalArgumentException( u"Empty element name is provided!"_ustr,
1827 static_cast< ::cppu::OWeakObject* >(this),
1828 2 );
1829
1830 if ( m_bWaitSaveCompleted )
1831 throw embed::WrongStateException(
1832 u"The object waits for saveCompleted() call!"_ustr,
1833 static_cast< ::cppu::OWeakObject* >(this) );
1834
1835 uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY_THROW );
1836
1837 m_bReadOnly = false;
1838
1839 if ( m_xParentStorage != xStorage || m_aEntryName != sEntName )
1840 SwitchOwnPersistence( xStorage, sEntName );
1841
1842 // for linked object it means that it becomes embedded object
1843 // the document must switch it's persistence also
1844
1845 // TODO/LATER: handle the case when temp doc can not be created
1846 // the document is a new embedded object so it must be marked as modified
1847 uno::Reference< util::XCloseable > xDocument = CreateTempDocFromLink_Impl();
1848 try
1849 {
1850 if(m_xDocHolder.is() && m_xDocHolder->GetComponent().is())
1851 {
1852 // tdf#141528 m_xDocHolder->GetComponent() may be not set, so add it
1853 // to the try path to not get thrown out of the local context to the next
1854 // higher try...catch on the stack. To make breakLink work it is
1855 // *necessary* to execute the code below that resets the linked state,
1856 // esp. the *.clear stuff and resetting m_bIsLink.
1857 uno::Reference< util::XModifiable > xModif( m_xDocHolder->GetComponent(), uno::UNO_QUERY_THROW );
1858
1859 // all other locations in this file check for xModif.is(), so do it here, too
1860 if ( xModif.is() )
1861 xModif->setModified( true );
1862 }
1863 }
1864 catch( const uno::Exception& )
1865 {}
1866
1867 m_xDocHolder->SetComponent( xDocument, m_bReadOnly );
1868 SAL_WARN_IF( !m_xDocHolder->GetComponent().is(), "embeddedobj.common", "If document can't be created, an exception must be thrown!" );
1869
1870 if ( m_nObjectState == embed::EmbedStates::LOADED )
1871 {
1872 // the state is changed and can not be switched to loaded state back without saving
1873 m_nObjectState = embed::EmbedStates::RUNNING;
1874 StateChangeNotification_Impl( false, embed::EmbedStates::LOADED, m_nObjectState, aGuard );
1875 }
1876 else if ( m_nObjectState == embed::EmbedStates::ACTIVE )
1877 m_xDocHolder->Show();
1878
1879 // tdf#141529 reset all stuff involved in linked state, including
1880 // the OLE content copied to the temp file
1881 m_bIsLinkURL = false;
1882 m_aLinkTempFile.clear();
1883 m_aLinkFilterName.clear();
1884 m_aLinkURL.clear();
1885 }
1886
1887
1888 sal_Bool SAL_CALL OCommonEmbeddedObject::isLink()
1889 {
1890 ::osl::MutexGuard aGuard( m_aMutex );
1891 if ( m_bDisposed )
1892 throw lang::DisposedException(); // TODO
1893
1894 return m_bIsLinkURL;
1895 }
1896
1897
1898 OUString SAL_CALL OCommonEmbeddedObject::getLinkURL()
1899 {
1900 ::osl::MutexGuard aGuard( m_aMutex );
1901 if ( m_bDisposed )
1902 throw lang::DisposedException(); // TODO
1903
1904 if ( !m_bIsLinkURL )
1905 throw embed::WrongStateException(
1906 u"The object is not a link object!"_ustr,
1907 static_cast< ::cppu::OWeakObject* >(this) );
1908
1909 return m_aLinkURL;
1910 }
1911
1912 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */