Coverage Report

Created: 2026-03-31 11:00

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