Debian Codegraph @

embeddedobj/source/commonembedding/miscobj.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/EmbedStates.hpp>
22 #include <com/sun/star/embed/EmbedVerbs.hpp>
23 #include <com/sun/star/embed/XStorage.hpp>
24 #include <com/sun/star/embed/EmbedUpdateModes.hpp>
25 #include <com/sun/star/embed/XInplaceClient.hpp>
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <com/sun/star/lang/NoSupportException.hpp>
28 #include <com/sun/star/beans/NamedValue.hpp>
29
30 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
31 #include <com/sun/star/io/TempFile.hpp>
32 #include <comphelper/multicontainer2.hxx>
33
34 #include <cppuhelper/queryinterface.hxx>
35 #include <comphelper/mimeconfighelper.hxx>
36
37 #include <utility>
38 #include <vcl/weld.hxx>
39 #include <vcl/stdtext.hxx>
40 #include <strings.hrc>
41 #include <osl/file.hxx>
42 #include <comphelper/DirectoryHelper.hxx>
43
44 #include <vcl/svapp.hxx>
45 #include <comphelper/diagnose_ex.hxx>
46 #include <cppuhelper/supportsservice.hxx>
47 #include <comphelper/sequenceashashmap.hxx>
48
49 #include "persistence.hxx"
50
51 using namespace ::com::sun::star;
52
53
54 OCommonEmbeddedObject::OCommonEmbeddedObject( uno::Reference< uno::XComponentContext > xContext,
55 const uno::Sequence< beans::NamedValue >& aObjProps )
56 : m_bReadOnly( false )
57 , m_bDisposed( false )
58 , m_bClosed( false )
59 , m_nObjectState( -1 )
60 , m_nTargetState( -1 )
61 , m_nUpdateMode ( embed::EmbedUpdateModes::ALWAYS_UPDATE )
62 , m_xContext(std::move( xContext ))
63 , m_nMiscStatus( 0 )
64 , m_bEmbeddedScriptSupport( true )
65 , m_bDocumentRecoverySupport( true )
66 , m_bWaitSaveCompleted( false )
67 , m_bIsLinkURL( false )
68 , m_bLinkTempFileChanged( false )
69 , m_pLinkFile( )
70 , m_bOleUpdate( false )
71 , m_bInHndFunc( false )
72 , m_bLinkHasPassword( false )
73 , m_aLinkTempFile( )
74 , m_bHasClonedSize( false )
75 , m_nClonedMapUnit( 0 )
76 {
77 CommonInit_Impl( aObjProps );
78 }
79
80
81 OCommonEmbeddedObject::OCommonEmbeddedObject(
82 uno::Reference< uno::XComponentContext > xContext,
83 const uno::Sequence< beans::NamedValue >& aObjProps,
84 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
85 const uno::Sequence< beans::PropertyValue >& aObjectDescr )
86 : m_bReadOnly( false )
87 , m_bDisposed( false )
88 , m_bClosed( false )
89 , m_nObjectState( embed::EmbedStates::LOADED )
90 , m_nTargetState( -1 )
91 , m_nUpdateMode ( embed::EmbedUpdateModes::ALWAYS_UPDATE )
92 , m_xContext(std::move( xContext ))
93 , m_nMiscStatus( 0 )
94 , m_bEmbeddedScriptSupport( true )
95 , m_bDocumentRecoverySupport( true )
96 , m_bWaitSaveCompleted( false )
97 , m_bIsLinkURL( true )
98 , m_bLinkTempFileChanged( false )
99 , m_pLinkFile( )
100 , m_bOleUpdate( false )
101 , m_bInHndFunc( false )
102 , m_bLinkHasPassword( false )
103 , m_aLinkTempFile( )
104 , m_bHasClonedSize( false )
105 , m_nClonedMapUnit( 0 )
106 {
107 // linked object has no own persistence so it is in loaded state starting from creation
108 LinkInit_Impl( aObjProps, aMediaDescr, aObjectDescr );
109 }
110
111
112 void OCommonEmbeddedObject::CommonInit_Impl( const uno::Sequence< beans::NamedValue >& aObjectProps )
113 {
114 OSL_ENSURE( m_xContext.is(), "No ServiceFactory is provided!" );
115 if ( !m_xContext.is() )
116 throw uno::RuntimeException();
117
118 m_xDocHolder = new embeddedobj::DocumentHolder( m_xContext, this );
119
120 // parse configuration entries
121 // TODO/LATER: in future UI names can be also provided here
122 for ( beans::NamedValue const & prop : aObjectProps )
123 {
124 if ( prop.Name == "ClassID" )
125 prop.Value >>= m_aClassID;
126 else if ( prop.Name == "ObjectDocumentServiceName" )
127 prop.Value >>= m_aDocServiceName;
128 else if ( prop.Name == "ObjectDocumentFilterName" )
129 prop.Value >>= m_aPresetFilterName;
130 else if ( prop.Name == "ObjectMiscStatus" )
131 prop.Value >>= m_nMiscStatus;
132 else if ( prop.Name == "ObjectVerbs" )
133 prop.Value >>= m_aObjectVerbs;
134 }
135
136 if ( m_aClassID.getLength() != 16 /*|| !m_aDocServiceName.getLength()*/ )
137 throw uno::RuntimeException(); // something goes really wrong
138
139 // verbs table
140 for (auto const& verb : m_aObjectVerbs)
141 {
142 if ( verb.VerbID == embed::EmbedVerbs::MS_OLEVERB_PRIMARY )
143 {
144 m_aVerbTable.insert( { verb.VerbID, embed::EmbedStates::UI_ACTIVE } );
145 }
146 else if ( verb.VerbID == embed::EmbedVerbs::MS_OLEVERB_SHOW )
147 {
148 m_aVerbTable.insert( { verb.VerbID, embed::EmbedStates::UI_ACTIVE } );
149 }
150 else if ( verb.VerbID == embed::EmbedVerbs::MS_OLEVERB_OPEN )
151 {
152 m_aVerbTable.insert( { verb.VerbID, embed::EmbedStates::ACTIVE } );
153 }
154 else if ( verb.VerbID == embed::EmbedVerbs::MS_OLEVERB_IPACTIVATE )
155 {
156 m_aVerbTable.insert( { verb.VerbID, embed::EmbedStates::INPLACE_ACTIVE } );
157 }
158 else if ( verb.VerbID == embed::EmbedVerbs::MS_OLEVERB_UIACTIVATE )
159 {
160 m_aVerbTable.insert( { verb.VerbID, embed::EmbedStates::UI_ACTIVE } );
161 }
162 else if ( verb.VerbID == embed::EmbedVerbs::MS_OLEVERB_HIDE )
163 {
164 m_aVerbTable.insert( { verb.VerbID, embed::EmbedStates::RUNNING } );
165 }
166 }
167 }
168
169
170 void OCommonEmbeddedObject::LinkInit_Impl(
171 const uno::Sequence< beans::NamedValue >& aObjectProps,
172 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
173 const uno::Sequence< beans::PropertyValue >& aObjectDescr )
174 {
175 // setPersistance has no effect on own links, so the complete initialization must be done here
176
177 for ( beans::PropertyValue const & prop : aMediaDescr )
178 if ( prop.Name == "URL" )
179 prop.Value >>= m_aLinkURL;
180 else if ( prop.Name == "FilterName" )
181 prop.Value >>= m_aLinkFilterName;
182
183 OSL_ENSURE( m_aLinkURL.getLength() && m_aLinkFilterName.getLength(), "Filter and URL must be provided!" );
184
185 m_bReadOnly = true;
186 if ( m_aLinkFilterName.getLength() )
187 {
188 ::comphelper::MimeConfigurationHelper aHelper( m_xContext );
189 OUString aExportFilterName = aHelper.GetExportFilterFromImportFilter( m_aLinkFilterName );
190 m_bReadOnly = aExportFilterName != m_aLinkFilterName;
191 }
192
193 if(m_bIsLinkURL && !m_bReadOnly)
194 {
195 // tdf#141529 we have a linked OLE object. To prevent the original OLE
196 // data to be changed each time the OLE gets changed (at deactivate), copy it to
197 // a temporary file. That file will be changed on activated OLE changes then.
198 // The moment the original gets changed itself will now be associated with the
199 // file/document embedding the OLE being changed (see other additions to the
200 // task-ID above)
201 //
202 // open OLE original data as read input file
203 if ( comphelper::DirectoryHelper::fileExists( m_aLinkURL ) )
204 {
205 // create temporary file
206 m_aLinkTempFile = io::TempFile::create( m_xContext );
207
208 m_pLinkFile.reset( new FileChangedChecker( m_aLinkURL ) );
209 handleLinkedOLE( CopyBackToOLELink::CopyLinkToTempInit );
210 }
211 }
212
213 if(m_aLinkTempFile.is())
214 {
215 uno::Sequence< beans::PropertyValue > aAlternativeMediaDescr(aMediaDescr.getLength());
216 auto aAlternativeMediaDescrRange = asNonConstRange(aAlternativeMediaDescr);
217
218 for ( sal_Int32 a(0); a < aMediaDescr.getLength(); a++ )
219 {
220 const beans::PropertyValue& rSource(aMediaDescr[a]);
221 beans::PropertyValue& rDestination(aAlternativeMediaDescrRange[a]);
222
223 rDestination.Name = rSource.Name;
224 if(rSource.Name == "URL")
225 rDestination.Value <<= m_aLinkTempFile->getUri();
226 else
227 rDestination.Value = rSource.Value;
228 }
229
230 m_aDocMediaDescriptor = GetValuableArgs_Impl( aAlternativeMediaDescr, false );
231 }
232 else
233 {
234 m_aDocMediaDescriptor = GetValuableArgs_Impl( aMediaDescr, false );
235 }
236
237 uno::Reference< frame::XDispatchProviderInterceptor > xDispatchInterceptor;
238 for ( beans::PropertyValue const & prop : aObjectDescr )
239 if ( prop.Name == "OutplaceDispatchInterceptor" )
240 {
241 prop.Value >>= xDispatchInterceptor;
242 break;
243 }
244 else if ( prop.Name == "Parent" )
245 {
246 prop.Value >>= m_xParent;
247 }
248
249 CommonInit_Impl( aObjectProps );
250
251 if ( xDispatchInterceptor.is() )
252 m_xDocHolder->SetOutplaceDispatchInterceptor( xDispatchInterceptor );
253 }
254
255
256 OCommonEmbeddedObject::~OCommonEmbeddedObject()
257 {
258 if ( !(m_pInterfaceContainer || m_xDocHolder.is()) )
259 return;
260
261 osl_atomic_increment(&m_refCount);
262 if ( m_pInterfaceContainer )
263 {
264 try {
265 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >( this ) );
266 m_pInterfaceContainer->disposeAndClear( aSource );
267 } catch( const uno::Exception& ) {}
268 m_pInterfaceContainer.reset();
269 }
270
271 try {
272 if ( m_xDocHolder.is() )
273 {
274 m_xDocHolder->CloseFrame();
275 try {
276 m_xDocHolder->CloseDocument( true, true );
277 } catch ( const uno::Exception& ) {}
278 m_xDocHolder->FreeOffice();
279
280 m_xDocHolder.clear();
281 }
282 } catch( const uno::Exception& ) {}
283 }
284
285
286 void OCommonEmbeddedObject::requestPositioning( const awt::Rectangle& aRect )
287 {
288 // the method is called in case object is inplace active and the object window was resized
289
290 OSL_ENSURE( m_xClientSite.is(), "The client site must be set for inplace active object!" );
291 if ( !m_xClientSite.is() )
292 return;
293
294 uno::Reference< embed::XInplaceClient > xInplaceClient( m_xClientSite, uno::UNO_QUERY );
295
296 OSL_ENSURE( xInplaceClient.is(), "The client site must support XInplaceClient to allow inplace activation!" );
297 if ( xInplaceClient.is() )
298 {
299 try {
300 xInplaceClient->changedPlacement( aRect );
301 }
302 catch( const uno::Exception& )
303 {
304 TOOLS_WARN_EXCEPTION( "embeddedobj", "Exception on request to resize!" );
305 }
306 }
307 }
308
309
310 void OCommonEmbeddedObject::PostEvent_Impl( const OUString& aEventName )
311 {
312 if ( !m_pInterfaceContainer )
313 return;
314
315 comphelper::OInterfaceContainerHelper2* pIC = m_pInterfaceContainer->getContainer(
316 cppu::UnoType<document::XEventListener>::get());
317 if( !pIC )
318 return;
319
320 document::EventObject aEvent;
321 aEvent.EventName = aEventName;
322 aEvent.Source.set( static_cast< ::cppu::OWeakObject* >( this ) );
323 // For now all the events are sent as object events
324 // aEvent.Source = ( xSource.is() ? xSource
325 // : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >( this ) ) );
326 comphelper::OInterfaceIteratorHelper2 aIt( *pIC );
327 while( aIt.hasMoreElements() )
328 {
329 try
330 {
331 static_cast<document::XEventListener *>(aIt.next())->notifyEvent( aEvent );
332 }
333 catch( const uno::RuntimeException& )
334 {
335 aIt.remove();
336 }
337
338 // the listener could dispose the object.
339 if ( m_bDisposed )
340 return;
341 }
342 }
343
344
345 int OCommonEmbeddedObject::ShowMsgDialog(TranslateId Msg, const OUString& sFileName)
346 {
347 std::locale aResLocale = Translate::Create( "emo" );
348 OUString aMsg = Translate::get( Msg, aResLocale );
349 OUString aBtn = Translate::get( BTN_OVERWRITE_TEXT, aResLocale );
350 OUString aTemp = sFileName;
351
352 osl::FileBase::getSystemPathFromFileURL( sFileName, aTemp );
353
354 aMsg = aMsg.replaceFirst( "%{filename}", aTemp );
355 weld::Window* pParent = Application::GetFrameWeld(m_xClientWindow);
356
357 std::unique_ptr<weld::MessageDialog> xQueryBox (Application::CreateMessageDialog( pParent,
358 VclMessageType::Warning, VclButtonsType::NONE, aMsg ) );
359 xQueryBox->add_button( aBtn, RET_YES );
360 xQueryBox->add_button( GetStandardText( StandardButtonType::Cancel ), RET_CANCEL );
361 xQueryBox->set_default_response( RET_CANCEL );
362
363 return xQueryBox->run();
364 }
365
366
367 void OCommonEmbeddedObject::handleLinkedOLE( CopyBackToOLELink eState )
368 {
369 // do not refresh and autosave at the same time
370 // when refresh all, then get both Link and Ole Update, in this case ignore OLE-refresh
371 if ( m_bInHndFunc || m_bOleUpdate || !m_aLinkTempFile.is() )
372 return;
373
374 m_bInHndFunc = true;
375
376 bool bLnkFileChg = m_pLinkFile->hasFileChanged( false );
377 bool bTmpFileChg = m_bLinkTempFileChanged;
378
379
380 if ( eState != CopyBackToOLELink::CopyLinkToTempInit && !bLnkFileChg && !bTmpFileChg )
381 {
382 // no changes
383 eState = CopyBackToOLELink::NoCopy;
384 }
385 else if ( ( eState == CopyBackToOLELink::CopyTempToLink ) && bLnkFileChg && !bTmpFileChg )
386 {
387 // Save pressed, but the Link-file is changed, but not the temp-file
388 // in this case update the object with new link data
389 eState = CopyBackToOLELink::CopyLinkToTempRefresh;
390 }
391 else if ( ( eState == CopyBackToOLELink::CopyTempToLink ) && bLnkFileChg && bTmpFileChg )
392 {
393 // Save pressed, but the Link-file is changed, question to user for overwrite
394 if ( ShowMsgDialog(STR_OVERWRITE_LINK, m_aLinkURL) == RET_CANCEL )
395 eState = CopyBackToOLELink::NoCopy;
396 }
397 else if ( ( eState == CopyBackToOLELink::CopyLinkToTemp ) && bTmpFileChg )
398 {
399 // Refresh pressed, but the Temp-file is changed, question to user for overwrite
400 // it is not important it has bLnkFileChg, always overwrite the temp-file
401 if ( ShowMsgDialog( STR_OVERWRITE_TEMP, m_aLinkURL ) == RET_CANCEL )
402 eState = CopyBackToOLELink::NoCopy;
403 }
404
405 auto writeFile = [ this ]( const OUString& SrcName, const OUString& DesName )
406 {
407 uno::Reference < ucb::XSimpleFileAccess2 > xWriteAccess( ucb::SimpleFileAccess::create( m_xContext ) );
408 uno::Reference < ucb::XSimpleFileAccess > xReadAccess( ucb::SimpleFileAccess::create( m_xContext ) );
409
410 try
411 {
412 uno::Reference < io::XInputStream > xInStream( xReadAccess->openFileRead (SrcName ) );
413
414 // This is *needed* since OTempFileService calls OTempFileService::readBytes which
415 // ensures the SvStream mpStream gets/is opened, *but* also sets the mnCachedPos from
416 // OTempFileService which still points to the end-of-file (from write-cc'ing).
417 uno::Reference < io::XSeekable > xSeek( xInStream, uno::UNO_QUERY_THROW );
418 xSeek->seek( 0 );
419
420 xWriteAccess->writeFile( DesName, xInStream );
421 m_bLinkTempFileChanged = false;
422 // store the new timestamp
423 m_pLinkFile->hasFileChanged();
424 }
425 catch ( const uno::Exception& ex )
426 {
427 OUString aMsg;
428 osl::FileBase::getSystemPathFromFileURL( SrcName, aMsg );
429 aMsg = ex.Message + "\n\n" + aMsg;
430 weld::Window* pParent = Application::GetFrameWeld(m_xClientWindow);
431 std::unique_ptr<weld::MessageDialog> xQueryBox( Application::CreateMessageDialog( pParent,
432 VclMessageType::Error, VclButtonsType::Ok, aMsg ) );
433
434 xQueryBox->run();
435 }
436 };
437
438 switch ( eState )
439 {
440 case CopyBackToOLELink::NoCopy:
441 break;
442 case CopyBackToOLELink::CopyLinkToTemp: // copy Link-File to Temp-File (Refresh)
443 case CopyBackToOLELink::CopyLinkToTempInit: //create temp file
444 writeFile( m_aLinkURL, m_aLinkTempFile->getUri() );
445 break;
446 case CopyBackToOLELink::CopyTempToLink: // copy Temp-File to Link-File (Save)
447 // tdf#141529 if we have a changed copy of the original OLE data we now
448 // need to write it back 'over' the original OLE data
449 writeFile( m_aLinkTempFile->getUri(), m_aLinkURL );
450 break;
451 case CopyBackToOLELink::CopyLinkToTempRefresh: // need a Refresh not save
452 // do nothing
453 break;
454 default:
455 break;
456 }
457
458 m_bInHndFunc = false;
459 }
460
461
462 uno::Any SAL_CALL OCommonEmbeddedObject::queryInterface( const uno::Type& rType )
463 {
464 uno::Any aReturn;
465
466 if ( rType == cppu::UnoType<embed::XEmbeddedObject>::get() )
467 {
468 void * p = static_cast< embed::XEmbeddedObject * >( this );
469 return uno::Any( &p, rType );
470 }
471 else if (rType == cppu::UnoType<embed::XEmbedPersist2>::get())
472 {
473 void* p = static_cast<embed::XEmbedPersist2*>(this);
474 return uno::Any(&p, rType);
475 }
476 else if (rType == cppu::UnoType<lang::XServiceInfo>::get())
477 {
478 void* p = static_cast<lang::XServiceInfo*>(this);
479 return uno::Any(&p, rType);
480 }
481 else if (rType == cppu::UnoType<lang::XInitialization>::get())
482 {
483 void* p = static_cast<lang::XInitialization*>(this);
484 return uno::Any(&p, rType);
485 }
486 else if (rType == cppu::UnoType<lang::XTypeProvider>::get())
487 {
488 void* p = static_cast<lang::XTypeProvider*>(this);
489 return uno::Any(&p, rType);
490 }
491 else
492 aReturn = ::cppu::queryInterface(
493 rType,
494 static_cast< embed::XInplaceObject* >( this ),
495 static_cast< embed::XVisualObject* >( this ),
496 static_cast< embed::XCommonEmbedPersist* >( static_cast< embed::XEmbedPersist* >( this ) ),
497 static_cast< embed::XEmbedPersist* >( this ),
498 static_cast< embed::XLinkageSupport* >( this ),
499 static_cast< embed::XStateChangeBroadcaster* >( this ),
500 static_cast< embed::XClassifiedObject* >( this ),
501 static_cast< embed::XComponentSupplier* >( this ),
502 static_cast< util::XCloseable* >( this ),
503 static_cast< container::XChild* >( this ),
504 static_cast< chart2::XDefaultSizeTransmitter* >( this ),
505 static_cast< document::XEventBroadcaster* >( this ) );
506
507 if ( aReturn.hasValue() )
508 return aReturn;
509 else
510 return ::cppu::OWeakObject::queryInterface( rType ) ;
511
512 }
513
514
515 void SAL_CALL OCommonEmbeddedObject::acquire()
516 noexcept
517 {
518 ::cppu::OWeakObject::acquire() ;
519 }
520
521
522 void SAL_CALL OCommonEmbeddedObject::release()
523 noexcept
524 {
525 ::cppu::OWeakObject::release() ;
526 }
527
528
529 uno::Sequence< sal_Int8 > SAL_CALL OCommonEmbeddedObject::getClassID()
530 {
531 if ( m_bDisposed )
532 throw lang::DisposedException();
533
534 return m_aClassID;
535 }
536
537 OUString SAL_CALL OCommonEmbeddedObject::getClassName()
538 {
539 if ( m_bDisposed )
540 throw lang::DisposedException();
541
542 return m_aClassName;
543 }
544
545 void SAL_CALL OCommonEmbeddedObject::setClassInfo(
546 const uno::Sequence< sal_Int8 >& /*aClassID*/, const OUString& /*aClassName*/ )
547 {
548 // the object class info can not be changed explicitly
549 throw lang::NoSupportException(); //TODO:
550 }
551
552
553 uno::Reference< util::XCloseable > SAL_CALL OCommonEmbeddedObject::getComponent()
554 {
555 SolarMutexGuard aGuard;
556 if ( m_bDisposed )
557 throw lang::DisposedException(); // TODO
558
559 // add an exception
560 if ( m_nObjectState == -1 )
561 {
562 // the object is still not loaded
563 throw uno::RuntimeException( u"Can't store object without persistence!"_ustr,
564 static_cast< ::cppu::OWeakObject* >(this) );
565 }
566
567 return m_xDocHolder->GetComponent();
568 }
569
570
571 void SAL_CALL OCommonEmbeddedObject::addStateChangeListener( const uno::Reference< embed::XStateChangeListener >& xListener )
572 {
573 SolarMutexGuard aGuard;
574 if ( m_bDisposed )
575 throw lang::DisposedException(); // TODO
576
577 if ( !m_pInterfaceContainer )
578 m_pInterfaceContainer.reset(new comphelper::OMultiTypeInterfaceContainerHelper2( m_aMutex ));
579
580 m_pInterfaceContainer->addInterface( cppu::UnoType<embed::XStateChangeListener>::get(),
581 xListener );
582 }
583
584
585 void SAL_CALL OCommonEmbeddedObject::removeStateChangeListener(
586 const uno::Reference< embed::XStateChangeListener >& xListener )
587 {
588 SolarMutexGuard aGuard;
589 if ( m_pInterfaceContainer )
590 m_pInterfaceContainer->removeInterface( cppu::UnoType<embed::XStateChangeListener>::get(),
591 xListener );
592 }
593
594
595 void SAL_CALL OCommonEmbeddedObject::close( sal_Bool bDeliverOwnership )
596 {
597 SolarMutexGuard aGuard;
598 if ( m_bClosed )
599 throw lang::DisposedException(); // TODO
600
601 uno::Reference< uno::XInterface > xSelfHold( static_cast< ::cppu::OWeakObject* >( this ) );
602 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >( this ) );
603
604 if ( m_pInterfaceContainer )
605 {
606 comphelper::OInterfaceContainerHelper2* pContainer =
607 m_pInterfaceContainer->getContainer( cppu::UnoType<util::XCloseListener>::get());
608 if ( pContainer != nullptr )
609 {
610 comphelper::OInterfaceIteratorHelper2 pIterator(*pContainer);
611 while (pIterator.hasMoreElements())
612 {
613 try
614 {
615 static_cast<util::XCloseListener*>(pIterator.next())->queryClosing( aSource, bDeliverOwnership );
616 }
617 catch( const uno::RuntimeException& )
618 {
619 pIterator.remove();
620 }
621 }
622 }
623
624 pContainer = m_pInterfaceContainer->getContainer(
625 cppu::UnoType<util::XCloseListener>::get());
626 if ( pContainer != nullptr )
627 {
628 comphelper::OInterfaceIteratorHelper2 pCloseIterator(*pContainer);
629 while (pCloseIterator.hasMoreElements())
630 {
631 try
632 {
633 static_cast<util::XCloseListener*>(pCloseIterator.next())->notifyClosing( aSource );
634 }
635 catch( const uno::RuntimeException& )
636 {
637 pCloseIterator.remove();
638 }
639 }
640 }
641
642 m_pInterfaceContainer->disposeAndClear( aSource );
643 m_pInterfaceContainer.reset();
644 }
645
646 m_bDisposed = true; // the object is disposed now for outside
647
648 // it is possible that the document can not be closed, in this case if the argument is false
649 // the exception will be thrown otherwise in addition to exception the object must register itself
650 // as termination listener and listen for document events
651
652 if ( m_xDocHolder.is() )
653 {
654 m_xDocHolder->CloseFrame();
655
656 try {
657 m_xDocHolder->CloseDocument( bDeliverOwnership, bDeliverOwnership );
658 }
659 catch( const uno::Exception& )
660 {
661 if ( bDeliverOwnership )
662 {
663 m_xDocHolder.clear();
664 m_bClosed = true;
665 }
666
667 throw;
668 }
669
670 m_xDocHolder->FreeOffice();
671
672 m_xDocHolder.clear();
673 }
674
675 // TODO: for now the storage will be disposed by the object, but after the document
676 // will use the storage, the storage will be disposed by the document and recreated by the object
677 if ( m_xObjectStorage.is() )
678 {
679 try {
680 m_xObjectStorage->dispose();
681 } catch ( const uno::Exception& ) {}
682
683 m_xObjectStorage.clear();
684 m_xRecoveryStorage.clear();
685 }
686
687 m_bClosed = true; // the closing succeeded
688 }
689
690
691 void SAL_CALL OCommonEmbeddedObject::addCloseListener( const uno::Reference< util::XCloseListener >& xListener )
692 {
693 SolarMutexGuard aGuard;
694 if ( m_bDisposed )
695 throw lang::DisposedException(); // TODO
696
697 if ( !m_pInterfaceContainer )
698 m_pInterfaceContainer.reset(new comphelper::OMultiTypeInterfaceContainerHelper2(m_aMutex));
699
700 m_pInterfaceContainer->addInterface( cppu::UnoType<util::XCloseListener>::get(), xListener );
701 }
702
703
704 void SAL_CALL OCommonEmbeddedObject::removeCloseListener( const uno::Reference< util::XCloseListener >& xListener )
705 {
706 SolarMutexGuard aGuard;
707 if ( m_pInterfaceContainer )
708 m_pInterfaceContainer->removeInterface( cppu::UnoType<util::XCloseListener>::get(),
709 xListener );
710 }
711
712
713 void SAL_CALL OCommonEmbeddedObject::addEventListener( const uno::Reference< document::XEventListener >& xListener )
714 {
715 SolarMutexGuard aGuard;
716 if ( m_bDisposed )
717 throw lang::DisposedException(); // TODO
718
719 if ( !m_pInterfaceContainer )
720 m_pInterfaceContainer.reset(new comphelper::OMultiTypeInterfaceContainerHelper2(m_aMutex));
721
722 m_pInterfaceContainer->addInterface( cppu::UnoType<document::XEventListener>::get(), xListener );
723 }
724
725
726 void SAL_CALL OCommonEmbeddedObject::removeEventListener( const uno::Reference< document::XEventListener >& xListener )
727 {
728 SolarMutexGuard aGuard;
729 if ( m_pInterfaceContainer )
730 m_pInterfaceContainer->removeInterface( cppu::UnoType<document::XEventListener>::get(),
731 xListener );
732 }
733
734 OUString SAL_CALL OCommonEmbeddedObject::getImplementationName()
735 {
736 return u"com.sun.star.comp.embed.OCommonEmbeddedObject"_ustr;
737 }
738
739 sal_Bool SAL_CALL OCommonEmbeddedObject::supportsService(const OUString& ServiceName)
740 {
741 return cppu::supportsService(this, ServiceName);
742 }
743
744 uno::Sequence<OUString> SAL_CALL OCommonEmbeddedObject::getSupportedServiceNames()
745 {
746 return { u"com.sun.star.comp.embed.OCommonEmbeddedObject"_ustr };
747 }
748
749 uno::Sequence<uno::Type> SAL_CALL OCommonEmbeddedObject::getTypes()
750 {
751 static const uno::Sequence<uno::Type> aTypes{
752 cppu::UnoType<embed::XEmbeddedObject>::get(),
753 cppu::UnoType<embed::XEmbedPersist2>::get(),
754 cppu::UnoType<embed::XLinkageSupport>::get(),
755 cppu::UnoType<embed::XInplaceObject>::get(),
756 cppu::UnoType<container::XChild>::get(),
757 cppu::UnoType<chart2::XDefaultSizeTransmitter>::get(),
758 cppu::UnoType<lang::XServiceInfo>::get(),
759 cppu::UnoType<lang::XInitialization>::get(),
760 cppu::UnoType<lang::XTypeProvider>::get(),
761 };
762 return aTypes;
763 }
764
765 uno::Sequence<sal_Int8> SAL_CALL OCommonEmbeddedObject::getImplementationId()
766 {
767 return uno::Sequence<sal_Int8>();
768 }
769
770 void SAL_CALL OCommonEmbeddedObject::initialize(const uno::Sequence<uno::Any>& rArguments)
771 {
772 if (!rArguments.hasElements())
773 {
774 return;
775 }
776
777 comphelper::SequenceAsHashMap aMap(rArguments[0]);
778 auto it = aMap.find(u"ReadOnly"_ustr);
779 if (it != aMap.end())
780 {
781 it->second >>= m_bReadOnly;
782 }
783 }
784
785 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */