Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / embeddedobj / source / general / dummyobject.cxx
blob4d2af144a479ec64785caf4a0f1eef40bb28d29f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/embed/EmbedStates.hpp>
21 #include <com/sun/star/embed/UnreachableStateException.hpp>
22 #include <com/sun/star/embed/WrongStateException.hpp>
23 #include <com/sun/star/embed/XEmbeddedClient.hpp>
24 #include <com/sun/star/embed/Aspects.hpp>
25 #include <com/sun/star/embed/EmbedMapUnits.hpp>
26 #include <com/sun/star/embed/EntryInitModes.hpp>
27 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
28 #include <com/sun/star/io/IOException.hpp>
29 #include <com/sun/star/lang/DisposedException.hpp>
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <com/sun/star/lang/NoSupportException.hpp>
33 #include <comphelper/multicontainer2.hxx>
34 #include <cppuhelper/supportsservice.hxx>
35 #include <osl/diagnose.h>
36 #include <dummyobject.hxx>
39 using namespace ::com::sun::star;
42 void ODummyEmbeddedObject::CheckInit_WrongState()
44 if ( m_bDisposed )
45 throw lang::DisposedException();
47 if ( m_nObjectState == -1 )
48 throw embed::WrongStateException( "The object has no persistence!",
49 static_cast< ::cppu::OWeakObject* >(this) );
52 void ODummyEmbeddedObject::CheckInit_Runtime()
54 if ( m_bDisposed )
55 throw lang::DisposedException();
57 if ( m_nObjectState == -1 )
58 throw uno::RuntimeException( "The object has no persistence!",
59 static_cast< ::cppu::OWeakObject* >(this) );
61 void ODummyEmbeddedObject::PostEvent_Impl( const OUString& aEventName )
63 if ( !m_pInterfaceContainer )
64 return;
66 comphelper::OInterfaceContainerHelper2* pIC = m_pInterfaceContainer->getContainer(
67 cppu::UnoType<document::XEventListener>::get());
68 if( !pIC )
69 return;
71 document::EventObject aEvent;
72 aEvent.EventName = aEventName;
73 aEvent.Source.set( static_cast< ::cppu::OWeakObject* >( this ) );
74 // For now all the events are sent as object events
75 // aEvent.Source = ( xSource.is() ? xSource
76 // : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >( this ) ) );
77 comphelper::OInterfaceIteratorHelper2 aIt( *pIC );
78 while( aIt.hasMoreElements() )
80 try
82 static_cast<document::XEventListener *>(aIt.next())->notifyEvent( aEvent );
84 catch( const uno::RuntimeException& )
86 aIt.remove();
89 // the listener could dispose the object.
90 if ( m_bDisposed )
91 return;
96 ODummyEmbeddedObject::~ODummyEmbeddedObject()
101 void SAL_CALL ODummyEmbeddedObject::changeState( sal_Int32 nNewState )
103 ::osl::MutexGuard aGuard( m_aMutex );
104 CheckInit_WrongState();
106 if ( nNewState == embed::EmbedStates::LOADED )
107 return;
109 throw embed::UnreachableStateException();
113 uno::Sequence< sal_Int32 > SAL_CALL ODummyEmbeddedObject::getReachableStates()
115 ::osl::MutexGuard aGuard( m_aMutex );
116 CheckInit_WrongState();
118 return { embed::EmbedStates::LOADED };
122 sal_Int32 SAL_CALL ODummyEmbeddedObject::getCurrentState()
124 ::osl::MutexGuard aGuard( m_aMutex );
125 CheckInit_WrongState();
127 return m_nObjectState;
131 void SAL_CALL ODummyEmbeddedObject::doVerb( sal_Int32 )
133 ::osl::MutexGuard aGuard( m_aMutex );
134 CheckInit_WrongState();
136 // no supported verbs
140 uno::Sequence< embed::VerbDescriptor > SAL_CALL ODummyEmbeddedObject::getSupportedVerbs()
142 ::osl::MutexGuard aGuard( m_aMutex );
143 CheckInit_WrongState();
145 return uno::Sequence< embed::VerbDescriptor >();
149 void SAL_CALL ODummyEmbeddedObject::setClientSite(
150 const uno::Reference< embed::XEmbeddedClient >& xClient )
152 ::osl::MutexGuard aGuard( m_aMutex );
153 CheckInit_WrongState();
155 m_xClientSite = xClient;
159 uno::Reference< embed::XEmbeddedClient > SAL_CALL ODummyEmbeddedObject::getClientSite()
161 ::osl::MutexGuard aGuard( m_aMutex );
162 CheckInit_WrongState();
164 return m_xClientSite;
168 void SAL_CALL ODummyEmbeddedObject::update()
170 ::osl::MutexGuard aGuard( m_aMutex );
171 CheckInit_WrongState();
175 void SAL_CALL ODummyEmbeddedObject::setUpdateMode( sal_Int32 )
177 ::osl::MutexGuard aGuard( m_aMutex );
178 CheckInit_WrongState();
182 sal_Int64 SAL_CALL ODummyEmbeddedObject::getStatus( sal_Int64 )
184 ::osl::MutexGuard aGuard( m_aMutex );
185 CheckInit_WrongState();
187 return 0;
191 void SAL_CALL ODummyEmbeddedObject::setContainerName( const OUString& )
193 ::osl::MutexGuard aGuard( m_aMutex );
194 CheckInit_Runtime();
198 void SAL_CALL ODummyEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect, const awt::Size& aSize )
200 ::osl::MutexGuard aGuard( m_aMutex );
201 CheckInit_WrongState();
203 OSL_ENSURE( nAspect != embed::Aspects::MSOLE_ICON, "For iconified objects no graphical replacement is required!" );
204 if ( nAspect == embed::Aspects::MSOLE_ICON )
205 // no representation can be retrieved
206 throw embed::WrongStateException( "Illegal call!",
207 static_cast< ::cppu::OWeakObject* >(this) );
209 m_nCachedAspect = nAspect;
210 m_aCachedSize = aSize;
211 m_bHasCachedSize = true;
215 awt::Size SAL_CALL ODummyEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect )
217 ::osl::MutexGuard aGuard( m_aMutex );
218 CheckInit_WrongState();
220 OSL_ENSURE( nAspect != embed::Aspects::MSOLE_ICON, "For iconified objects no graphical replacement is required!" );
221 if ( nAspect == embed::Aspects::MSOLE_ICON )
222 // no representation can be retrieved
223 throw embed::WrongStateException( "Illegal call!",
224 static_cast< ::cppu::OWeakObject* >(this) );
226 if ( !m_bHasCachedSize || m_nCachedAspect != nAspect )
227 throw embed::NoVisualAreaSizeException(
228 "No size available!",
229 static_cast< ::cppu::OWeakObject* >(this) );
231 return m_aCachedSize;
235 sal_Int32 SAL_CALL ODummyEmbeddedObject::getMapUnit( sal_Int64 nAspect )
237 ::osl::MutexGuard aGuard( m_aMutex );
238 CheckInit_Runtime();
240 OSL_ENSURE( nAspect != embed::Aspects::MSOLE_ICON, "For iconified objects no graphical replacement is required!" );
241 if ( nAspect == embed::Aspects::MSOLE_ICON )
242 // no representation can be retrieved
243 throw embed::WrongStateException( "Illegal call!",
244 static_cast< ::cppu::OWeakObject* >(this) );
246 return embed::EmbedMapUnits::ONE_100TH_MM;
250 embed::VisualRepresentation SAL_CALL ODummyEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 )
252 ::osl::MutexGuard aGuard( m_aMutex );
253 CheckInit_WrongState();
255 // no representation can be retrieved
256 throw embed::WrongStateException( "Illegal call!",
257 static_cast< ::cppu::OWeakObject* >(this) );
261 void SAL_CALL ODummyEmbeddedObject::setPersistentEntry(
262 const uno::Reference< embed::XStorage >& xStorage,
263 const OUString& sEntName,
264 sal_Int32 nEntryConnectionMode,
265 const uno::Sequence< beans::PropertyValue >& /* lArguments */,
266 const uno::Sequence< beans::PropertyValue >& /* lObjArgs */ )
268 ::osl::MutexGuard aGuard( m_aMutex );
269 if ( m_bDisposed )
270 throw lang::DisposedException(); // TODO
272 if ( !xStorage.is() )
273 throw lang::IllegalArgumentException( "No parent storage is provided!",
274 static_cast< ::cppu::OWeakObject* >(this),
275 1 );
277 if ( sEntName.isEmpty() )
278 throw lang::IllegalArgumentException( "Empty element name is provided!",
279 static_cast< ::cppu::OWeakObject* >(this),
280 2 );
282 if ( ( m_nObjectState != -1 || nEntryConnectionMode == embed::EntryInitModes::NO_INIT )
283 && ( m_nObjectState == -1 || nEntryConnectionMode != embed::EntryInitModes::NO_INIT ) )
285 throw embed::WrongStateException(
286 "Can't change persistent representation of activated object!",
287 static_cast< ::cppu::OWeakObject* >(this) );
290 if ( m_bWaitSaveCompleted )
292 if ( nEntryConnectionMode != embed::EntryInitModes::NO_INIT )
293 throw embed::WrongStateException(
294 "The object waits for saveCompleted() call!",
295 static_cast< ::cppu::OWeakObject* >(this) );
297 saveCompleted( m_xParentStorage != xStorage || m_aEntryName != sEntName );
301 if ( nEntryConnectionMode != embed::EntryInitModes::DEFAULT_INIT
302 && nEntryConnectionMode != embed::EntryInitModes::NO_INIT )
303 throw lang::IllegalArgumentException( "Wrong connection mode is provided!",
304 static_cast< ::cppu::OWeakObject* >(this),
305 3 );
307 if ( !xStorage->hasByName( sEntName ) )
308 throw lang::IllegalArgumentException( "Wrong entry is provided!",
309 static_cast< ::cppu::OWeakObject* >(this),
310 2 );
312 m_xParentStorage = xStorage;
313 m_aEntryName = sEntName;
314 m_nObjectState = embed::EmbedStates::LOADED;
318 void SAL_CALL ODummyEmbeddedObject::storeToEntry( const uno::Reference< embed::XStorage >& xStorage,
319 const OUString& sEntName,
320 const uno::Sequence< beans::PropertyValue >& /* lArguments */,
321 const uno::Sequence< beans::PropertyValue >& /* lObjArgs */ )
323 ::osl::MutexGuard aGuard( m_aMutex );
324 CheckInit_WrongState();
326 if ( m_bWaitSaveCompleted )
327 throw embed::WrongStateException(
328 "The object waits for saveCompleted() call!",
329 static_cast< ::cppu::OWeakObject* >(this) );
331 m_xParentStorage->copyElementTo( m_aEntryName, xStorage, sEntName );
335 void SAL_CALL ODummyEmbeddedObject::storeAsEntry( const uno::Reference< embed::XStorage >& xStorage,
336 const OUString& sEntName,
337 const uno::Sequence< beans::PropertyValue >& /* lArguments */,
338 const uno::Sequence< beans::PropertyValue >& /* lObjArgs */ )
340 ::osl::MutexGuard aGuard( m_aMutex );
341 CheckInit_WrongState();
343 if ( m_bWaitSaveCompleted )
344 throw embed::WrongStateException(
345 "The object waits for saveCompleted() call!",
346 static_cast< ::cppu::OWeakObject* >(this) );
348 PostEvent_Impl( "OnSaveAs" );
350 m_xParentStorage->copyElementTo( m_aEntryName, xStorage, sEntName );
352 m_bWaitSaveCompleted = true;
353 m_xNewParentStorage = xStorage;
354 m_aNewEntryName = sEntName;
358 void SAL_CALL ODummyEmbeddedObject::saveCompleted( sal_Bool bUseNew )
360 ::osl::MutexGuard aGuard( m_aMutex );
361 CheckInit_WrongState();
363 // it is allowed to call saveCompleted( false ) for nonstored objects
364 if ( !m_bWaitSaveCompleted && !bUseNew )
365 return;
367 OSL_ENSURE( m_bWaitSaveCompleted, "Unexpected saveCompleted() call!" );
368 if ( !m_bWaitSaveCompleted )
369 throw io::IOException(); // TODO: illegal call
371 OSL_ENSURE( m_xNewParentStorage.is() , "Internal object information is broken!" );
372 if ( !m_xNewParentStorage.is() )
373 throw uno::RuntimeException(); // TODO: broken internal information
375 if ( bUseNew )
377 m_xParentStorage = m_xNewParentStorage;
378 m_aEntryName = m_aNewEntryName;
380 PostEvent_Impl( "OnSaveAsDone" );
383 m_xNewParentStorage.clear();
384 m_aNewEntryName.clear();
385 m_bWaitSaveCompleted = false;
389 sal_Bool SAL_CALL ODummyEmbeddedObject::hasEntry()
391 ::osl::MutexGuard aGuard( m_aMutex );
392 CheckInit_WrongState();
394 if ( m_bWaitSaveCompleted )
395 throw embed::WrongStateException(
396 "The object waits for saveCompleted() call!",
397 static_cast< ::cppu::OWeakObject* >(this) );
399 if ( !m_aEntryName.isEmpty() )
400 return true;
402 return false;
406 OUString SAL_CALL ODummyEmbeddedObject::getEntryName()
408 ::osl::MutexGuard aGuard( m_aMutex );
409 CheckInit_WrongState();
411 if ( m_bWaitSaveCompleted )
412 throw embed::WrongStateException(
413 "The object waits for saveCompleted() call!",
414 static_cast< ::cppu::OWeakObject* >(this) );
416 return m_aEntryName;
420 void SAL_CALL ODummyEmbeddedObject::storeOwn()
422 ::osl::MutexGuard aGuard( m_aMutex );
423 CheckInit_WrongState();
425 if ( m_bWaitSaveCompleted )
426 throw embed::WrongStateException(
427 "The object waits for saveCompleted() call!",
428 static_cast< ::cppu::OWeakObject* >(this) );
430 // the object can not be activated or changed
434 sal_Bool SAL_CALL ODummyEmbeddedObject::isReadonly()
436 ::osl::MutexGuard aGuard( m_aMutex );
437 CheckInit_WrongState();
439 if ( m_bWaitSaveCompleted )
440 throw embed::WrongStateException(
441 "The object waits for saveCompleted() call!",
442 static_cast< ::cppu::OWeakObject* >(this) );
444 // this object can not be changed
445 return true;
449 void SAL_CALL ODummyEmbeddedObject::reload(
450 const uno::Sequence< beans::PropertyValue >& /* lArguments */,
451 const uno::Sequence< beans::PropertyValue >& /* lObjArgs */ )
453 ::osl::MutexGuard aGuard( m_aMutex );
454 CheckInit_WrongState();
456 if ( m_bWaitSaveCompleted )
457 throw embed::WrongStateException(
458 "The object waits for saveCompleted() call!",
459 static_cast< ::cppu::OWeakObject* >(this) );
461 // nothing to reload
465 uno::Sequence< sal_Int8 > SAL_CALL ODummyEmbeddedObject::getClassID()
467 ::osl::MutexGuard aGuard( m_aMutex );
468 CheckInit_Runtime();
470 // currently the class ID is empty
471 // TODO/LATER: should a special class ID be used in this case?
472 return uno::Sequence< sal_Int8 >();
476 OUString SAL_CALL ODummyEmbeddedObject::getClassName()
478 ::osl::MutexGuard aGuard( m_aMutex );
479 if ( m_bDisposed )
480 throw lang::DisposedException(); // TODO
482 return OUString();
486 void SAL_CALL ODummyEmbeddedObject::setClassInfo(
487 const uno::Sequence< sal_Int8 >& /*aClassID*/, const OUString& /*aClassName*/ )
489 throw lang::NoSupportException();
493 uno::Reference< util::XCloseable > SAL_CALL ODummyEmbeddedObject::getComponent()
495 ::osl::MutexGuard aGuard( m_aMutex );
496 CheckInit_Runtime();
498 return uno::Reference< util::XCloseable >();
502 void SAL_CALL ODummyEmbeddedObject::addStateChangeListener( const uno::Reference< embed::XStateChangeListener >& xListener )
504 ::osl::MutexGuard aGuard( m_aMutex );
505 if ( m_bDisposed )
506 return;
508 if ( !m_pInterfaceContainer )
509 m_pInterfaceContainer.reset(new comphelper::OMultiTypeInterfaceContainerHelper2( m_aMutex ));
511 m_pInterfaceContainer->addInterface( cppu::UnoType<embed::XStateChangeListener>::get(),
512 xListener );
516 void SAL_CALL ODummyEmbeddedObject::removeStateChangeListener(
517 const uno::Reference< embed::XStateChangeListener >& xListener )
519 ::osl::MutexGuard aGuard( m_aMutex );
520 if ( m_pInterfaceContainer )
521 m_pInterfaceContainer->removeInterface( cppu::UnoType<embed::XStateChangeListener>::get(),
522 xListener );
526 void SAL_CALL ODummyEmbeddedObject::close( sal_Bool bDeliverOwnership )
528 ::osl::MutexGuard aGuard( m_aMutex );
529 if ( m_bDisposed )
530 throw lang::DisposedException(); // TODO
532 uno::Reference< uno::XInterface > xSelfHold( static_cast< ::cppu::OWeakObject* >( this ) );
533 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >( this ) );
535 if ( m_pInterfaceContainer )
537 comphelper::OInterfaceContainerHelper2* pContainer =
538 m_pInterfaceContainer->getContainer( cppu::UnoType<util::XCloseListener>::get());
539 if ( pContainer != nullptr )
541 comphelper::OInterfaceIteratorHelper2 pIterator(*pContainer);
542 while (pIterator.hasMoreElements())
546 static_cast<util::XCloseListener*>(pIterator.next())->queryClosing( aSource, bDeliverOwnership );
548 catch( const uno::RuntimeException& )
550 pIterator.remove();
555 pContainer = m_pInterfaceContainer->getContainer(
556 cppu::UnoType<util::XCloseListener>::get());
557 if ( pContainer != nullptr )
559 comphelper::OInterfaceIteratorHelper2 pCloseIterator(*pContainer);
560 while (pCloseIterator.hasMoreElements())
564 static_cast<util::XCloseListener*>(pCloseIterator.next())->notifyClosing( aSource );
566 catch( const uno::RuntimeException& )
568 pCloseIterator.remove();
573 m_pInterfaceContainer->disposeAndClear( aSource );
576 m_bDisposed = true; // the object is disposed now for outside
580 void SAL_CALL ODummyEmbeddedObject::addCloseListener( const uno::Reference< util::XCloseListener >& xListener )
582 ::osl::MutexGuard aGuard( m_aMutex );
583 if ( m_bDisposed )
584 return;
586 if ( !m_pInterfaceContainer )
587 m_pInterfaceContainer.reset(new comphelper::OMultiTypeInterfaceContainerHelper2( m_aMutex ));
589 m_pInterfaceContainer->addInterface( cppu::UnoType<util::XCloseListener>::get(), xListener );
593 void SAL_CALL ODummyEmbeddedObject::removeCloseListener( const uno::Reference< util::XCloseListener >& xListener )
595 ::osl::MutexGuard aGuard( m_aMutex );
596 if ( m_pInterfaceContainer )
597 m_pInterfaceContainer->removeInterface( cppu::UnoType<util::XCloseListener>::get(),
598 xListener );
602 void SAL_CALL ODummyEmbeddedObject::addEventListener( const uno::Reference< document::XEventListener >& xListener )
604 ::osl::MutexGuard aGuard( m_aMutex );
605 if ( m_bDisposed )
606 return;
608 if ( !m_pInterfaceContainer )
609 m_pInterfaceContainer.reset(new comphelper::OMultiTypeInterfaceContainerHelper2( m_aMutex ));
611 m_pInterfaceContainer->addInterface( cppu::UnoType<document::XEventListener>::get(), xListener );
615 void SAL_CALL ODummyEmbeddedObject::removeEventListener( const uno::Reference< document::XEventListener >& xListener )
617 ::osl::MutexGuard aGuard( m_aMutex );
618 if ( m_pInterfaceContainer )
619 m_pInterfaceContainer->removeInterface( cppu::UnoType<document::XEventListener>::get(),
620 xListener );
623 OUString SAL_CALL ODummyEmbeddedObject::getImplementationName()
625 return "com.sun.star.comp.embed.ODummyEmbeddedObject";
628 sal_Bool SAL_CALL ODummyEmbeddedObject::supportsService(const OUString& ServiceName)
630 return cppu::supportsService(this, ServiceName);
633 uno::Sequence<OUString> SAL_CALL ODummyEmbeddedObject::getSupportedServiceNames()
635 return { "com.sun.star.comp.embed.ODummyEmbeddedObject" };
638 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */