Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <oleembobj.hxx>
21 : #include <com/sun/star/embed/EmbedStates.hpp>
22 : #include <com/sun/star/embed/EmbedVerbs.hpp>
23 : #include <com/sun/star/embed/EntryInitModes.hpp>
24 : #include <com/sun/star/embed/XStorage.hpp>
25 : #include <com/sun/star/embed/ElementModes.hpp>
26 : #include <com/sun/star/embed/EmbedUpdateModes.hpp>
27 : #include <com/sun/star/embed/Aspects.hpp>
28 : #include <com/sun/star/embed/NeedsRunningStateException.hpp>
29 : #include <com/sun/star/embed/StateChangeInProgressException.hpp>
30 : #include <com/sun/star/embed/EmbedMisc.hpp>
31 : #include <com/sun/star/embed/XEmbedObjectCreator.hpp>
32 : #include <com/sun/star/io/TempFile.hpp>
33 : #include <com/sun/star/io/XSeekable.hpp>
34 : #include <com/sun/star/lang/DisposedException.hpp>
35 : #include <com/sun/star/beans/NamedValue.hpp>
36 : #include <com/sun/star/beans/XPropertySet.hpp>
37 : #include <com/sun/star/frame/XLoadable.hpp>
38 : #include <com/sun/star/document/XStorageBasedDocument.hpp>
39 : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
40 : #include <com/sun/star/container/XNameAccess.hpp>
41 : #include <com/sun/star/container/XNameContainer.hpp>
42 : #include <com/sun/star/system/SystemShellExecute.hpp>
43 : #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
44 :
45 : #include <cppuhelper/interfacecontainer.h>
46 : #include <comphelper/processfactory.hxx>
47 : #include <comphelper/mimeconfighelper.hxx>
48 : #include <comphelper/storagehelper.hxx>
49 :
50 :
51 : #include <targetstatecontrol.hxx>
52 :
53 : #include "ownview.hxx"
54 :
55 : #if defined WNT
56 : #include <olecomponent.hxx>
57 : #endif
58 :
59 : using namespace ::com::sun::star;
60 :
61 : #ifdef WNT
62 :
63 : void OleEmbeddedObject::SwitchComponentToRunningState_Impl()
64 : {
65 : if ( m_pOleComponent )
66 : {
67 : try
68 : {
69 : m_pOleComponent->RunObject();
70 : }
71 : catch( const embed::UnreachableStateException& )
72 : {
73 : GetRidOfComponent();
74 : throw;
75 : }
76 : catch( const embed::WrongStateException& )
77 : {
78 : GetRidOfComponent();
79 : throw;
80 : }
81 : }
82 : else
83 : {
84 : throw embed::UnreachableStateException();
85 : }
86 : }
87 :
88 :
89 : uno::Sequence< sal_Int32 > OleEmbeddedObject::GetReachableStatesList_Impl(
90 : const uno::Sequence< embed::VerbDescriptor >& aVerbList )
91 : {
92 : uno::Sequence< sal_Int32 > aStates(2);
93 : aStates[0] = embed::EmbedStates::LOADED;
94 : aStates[1] = embed::EmbedStates::RUNNING;
95 : for ( sal_Int32 nInd = 0; nInd < aVerbList.getLength(); nInd++ )
96 : if ( aVerbList[nInd].VerbID == embed::EmbedVerbs::MS_OLEVERB_OPEN )
97 : {
98 : aStates.realloc(3);
99 : aStates[2] = embed::EmbedStates::ACTIVE;
100 : }
101 :
102 : return aStates;
103 : }
104 :
105 :
106 : uno::Sequence< sal_Int32 > OleEmbeddedObject::GetIntermediateVerbsSequence_Impl( sal_Int32 nNewState )
107 : {
108 : SAL_WARN_IF( m_nObjectState == embed::EmbedStates::LOADED, "embeddedobj.ole", "Loaded object is switched to running state without verbs using!" );
109 :
110 : // actually there will be only one verb
111 : if ( m_nObjectState == embed::EmbedStates::RUNNING && nNewState == embed::EmbedStates::ACTIVE )
112 : {
113 : uno::Sequence< sal_Int32 > aVerbs( 1 );
114 : aVerbs[0] = embed::EmbedVerbs::MS_OLEVERB_OPEN;
115 : }
116 :
117 : return uno::Sequence< sal_Int32 >();
118 : }
119 : #endif
120 :
121 0 : void OleEmbeddedObject::MoveListeners()
122 : {
123 0 : if ( m_pInterfaceContainer )
124 : {
125 : // move state change listeners
126 : {
127 : ::cppu::OInterfaceContainerHelper* pStateChangeContainer =
128 0 : m_pInterfaceContainer->getContainer( cppu::UnoType<embed::XStateChangeListener>::get());
129 0 : if ( pStateChangeContainer != NULL )
130 : {
131 0 : uno::Reference< embed::XStateChangeBroadcaster > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
132 0 : if ( xWrappedObject.is() )
133 : {
134 0 : ::cppu::OInterfaceIteratorHelper pIterator( *pStateChangeContainer );
135 0 : while ( pIterator.hasMoreElements() )
136 : {
137 : try
138 : {
139 0 : xWrappedObject->addStateChangeListener( static_cast<embed::XStateChangeListener*>(pIterator.next()) );
140 : }
141 0 : catch( const uno::RuntimeException& )
142 : {
143 0 : pIterator.remove();
144 : }
145 0 : }
146 0 : }
147 : }
148 : }
149 :
150 : // move event listeners
151 : {
152 : ::cppu::OInterfaceContainerHelper* pEventContainer =
153 0 : m_pInterfaceContainer->getContainer( cppu::UnoType<document::XEventListener>::get());
154 0 : if ( pEventContainer != NULL )
155 : {
156 0 : uno::Reference< document::XEventBroadcaster > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
157 0 : if ( xWrappedObject.is() )
158 : {
159 0 : ::cppu::OInterfaceIteratorHelper pIterator( *pEventContainer );
160 0 : while ( pIterator.hasMoreElements() )
161 : {
162 : try
163 : {
164 0 : xWrappedObject->addEventListener( static_cast<document::XEventListener*>(pIterator.next()) );
165 : }
166 0 : catch( const uno::RuntimeException& )
167 : {
168 0 : pIterator.remove();
169 : }
170 0 : }
171 0 : }
172 : }
173 : }
174 :
175 : // move close listeners
176 : {
177 : ::cppu::OInterfaceContainerHelper* pCloseContainer =
178 0 : m_pInterfaceContainer->getContainer( cppu::UnoType<util::XCloseListener>::get());
179 0 : if ( pCloseContainer != NULL )
180 : {
181 0 : uno::Reference< util::XCloseBroadcaster > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
182 0 : if ( xWrappedObject.is() )
183 : {
184 0 : ::cppu::OInterfaceIteratorHelper pIterator( *pCloseContainer );
185 0 : while ( pIterator.hasMoreElements() )
186 : {
187 : try
188 : {
189 0 : xWrappedObject->addCloseListener( static_cast<util::XCloseListener*>(pIterator.next()) );
190 : }
191 0 : catch( const uno::RuntimeException& )
192 : {
193 0 : pIterator.remove();
194 : }
195 0 : }
196 0 : }
197 : }
198 : }
199 :
200 0 : delete m_pInterfaceContainer;
201 0 : m_pInterfaceContainer = NULL;
202 : }
203 0 : }
204 :
205 :
206 0 : uno::Reference< embed::XStorage > OleEmbeddedObject::CreateTemporarySubstorage( OUString& o_aStorageName )
207 : {
208 0 : uno::Reference< embed::XStorage > xResult;
209 :
210 0 : for ( sal_Int32 nInd = 0; nInd < 32000 && !xResult.is(); nInd++ )
211 : {
212 0 : OUString aName = OUString::number( nInd ) + "TMPSTOR" + m_aEntryName;
213 0 : if ( !m_xParentStorage->hasByName( aName ) )
214 : {
215 0 : xResult = m_xParentStorage->openStorageElement( aName, embed::ElementModes::READWRITE );
216 0 : o_aStorageName = aName;
217 : }
218 0 : }
219 :
220 0 : if ( !xResult.is() )
221 : {
222 0 : o_aStorageName = OUString();
223 0 : throw uno::RuntimeException();
224 : }
225 :
226 0 : return xResult;
227 : }
228 :
229 :
230 0 : OUString OleEmbeddedObject::MoveToTemporarySubstream()
231 : {
232 0 : OUString aResult;
233 0 : for ( sal_Int32 nInd = 0; nInd < 32000 && aResult.isEmpty(); nInd++ )
234 : {
235 0 : OUString aName = OUString::number( nInd ) + "TMPSTREAM" + m_aEntryName;
236 0 : if ( !m_xParentStorage->hasByName( aName ) )
237 : {
238 0 : m_xParentStorage->renameElement( m_aEntryName, aName );
239 0 : aResult = aName;
240 : }
241 0 : }
242 :
243 0 : if ( aResult.isEmpty() )
244 0 : throw uno::RuntimeException();
245 :
246 0 : return aResult;
247 : }
248 :
249 :
250 0 : bool OleEmbeddedObject::TryToConvertToOOo()
251 : {
252 0 : bool bResult = false;
253 :
254 0 : OUString aStorageName;
255 0 : OUString aTmpStreamName;
256 0 : sal_Int32 nStep = 0;
257 :
258 0 : if ( m_pOleComponent || m_bReadOnly )
259 0 : return false;
260 :
261 : try
262 : {
263 0 : changeState( embed::EmbedStates::LOADED );
264 :
265 : // the stream must be seekable
266 0 : uno::Reference< io::XSeekable > xSeekable( m_xObjectStream, uno::UNO_QUERY_THROW );
267 0 : xSeekable->seek( 0 );
268 0 : m_aFilterName = OwnView_Impl::GetFilterNameFromExtentionAndInStream( m_xFactory, OUString(), m_xObjectStream->getInputStream() );
269 :
270 : // use the solution only for OOXML format currently
271 0 : if ( !m_aFilterName.isEmpty()
272 0 : && ( m_aFilterName == "Calc MS Excel 2007 XML" || m_aFilterName == "Impress MS PowerPoint 2007 XML" || m_aFilterName == "MS Word 2007 XML" ) )
273 : {
274 : uno::Reference< container::XNameAccess > xFilterFactory(
275 0 : m_xFactory->createInstance("com.sun.star.document.FilterFactory"),
276 0 : uno::UNO_QUERY_THROW );
277 :
278 0 : OUString aDocServiceName;
279 0 : uno::Any aFilterAnyData = xFilterFactory->getByName( m_aFilterName );
280 0 : uno::Sequence< beans::PropertyValue > aFilterData;
281 0 : if ( aFilterAnyData >>= aFilterData )
282 : {
283 0 : for ( sal_Int32 nInd = 0; nInd < aFilterData.getLength(); nInd++ )
284 0 : if ( aFilterData[nInd].Name == "DocumentService" )
285 0 : aFilterData[nInd].Value >>= aDocServiceName;
286 : }
287 :
288 0 : if ( !aDocServiceName.isEmpty() )
289 : {
290 : // create the model
291 0 : uno::Sequence< uno::Any > aArguments(1);
292 0 : aArguments[0] <<= beans::NamedValue( OUString( "EmbeddedObject" ), uno::makeAny( true ));
293 :
294 0 : uno::Reference< util::XCloseable > xDocument( m_xFactory->createInstanceWithArguments( aDocServiceName, aArguments ), uno::UNO_QUERY_THROW );
295 0 : uno::Reference< frame::XLoadable > xLoadable( xDocument, uno::UNO_QUERY_THROW );
296 0 : uno::Reference< document::XStorageBasedDocument > xStorDoc( xDocument, uno::UNO_QUERY_THROW );
297 :
298 : // let the model behave as embedded one
299 0 : uno::Reference< frame::XModel > xModel( xDocument, uno::UNO_QUERY_THROW );
300 0 : uno::Sequence< beans::PropertyValue > aSeq( 1 );
301 0 : aSeq[0].Name = "SetEmbedded";
302 0 : aSeq[0].Value <<= sal_True;
303 0 : xModel->attachResource( OUString(), aSeq );
304 :
305 : // load the model from the stream
306 0 : uno::Sequence< beans::PropertyValue > aArgs( 5 );
307 0 : aArgs[0].Name = "HierarchicalDocumentName";
308 0 : aArgs[0].Value <<= m_aEntryName;
309 0 : aArgs[1].Name = "ReadOnly";
310 0 : aArgs[1].Value <<= sal_True;
311 0 : aArgs[2].Name = "FilterName";
312 0 : aArgs[2].Value <<= m_aFilterName;
313 0 : aArgs[3].Name = "URL";
314 0 : aArgs[3].Value <<= OUString( "private:stream" );
315 0 : aArgs[4].Name = "InputStream";
316 0 : aArgs[4].Value <<= m_xObjectStream->getInputStream();
317 :
318 0 : xSeekable->seek( 0 );
319 0 : xLoadable->load( aArgs );
320 :
321 : // the model is successfully loaded, create a new storage and store the model to the storage
322 0 : uno::Reference< embed::XStorage > xTmpStorage = CreateTemporarySubstorage( aStorageName );
323 0 : xStorDoc->storeToStorage( xTmpStorage, uno::Sequence< beans::PropertyValue >() );
324 0 : xDocument->close( sal_True );
325 0 : uno::Reference< beans::XPropertySet > xStorProps( xTmpStorage, uno::UNO_QUERY_THROW );
326 0 : OUString aMediaType;
327 0 : xStorProps->getPropertyValue("MediaType") >>= aMediaType;
328 0 : xTmpStorage->dispose();
329 :
330 : // look for the related embedded object factory
331 0 : ::comphelper::MimeConfigurationHelper aConfigHelper( comphelper::getComponentContext(m_xFactory) );
332 0 : OUString aEmbedFactory;
333 0 : if ( !aMediaType.isEmpty() )
334 0 : aEmbedFactory = aConfigHelper.GetFactoryNameByMediaType( aMediaType );
335 :
336 0 : if ( aEmbedFactory.isEmpty() )
337 0 : throw uno::RuntimeException();
338 :
339 0 : uno::Reference< uno::XInterface > xFact = m_xFactory->createInstance( aEmbedFactory );
340 :
341 0 : uno::Reference< embed::XEmbedObjectCreator > xEmbCreator( xFact, uno::UNO_QUERY_THROW );
342 :
343 : // now the object should be adjusted to become the wrapper
344 0 : nStep = 1;
345 0 : uno::Reference< lang::XComponent > xComp( m_xObjectStream, uno::UNO_QUERY_THROW );
346 0 : xComp->dispose();
347 0 : m_xObjectStream = uno::Reference< io::XStream >();
348 0 : m_nObjectState = -1;
349 :
350 0 : nStep = 2;
351 0 : aTmpStreamName = MoveToTemporarySubstream();
352 :
353 0 : nStep = 3;
354 0 : m_xParentStorage->renameElement( aStorageName, m_aEntryName );
355 :
356 0 : nStep = 4;
357 0 : m_xWrappedObject.set( xEmbCreator->createInstanceInitFromEntry( m_xParentStorage, m_aEntryName, uno::Sequence< beans::PropertyValue >(), uno::Sequence< beans::PropertyValue >() ), uno::UNO_QUERY_THROW );
358 :
359 0 : bResult = true; // the change is no more revertable
360 : try
361 : {
362 0 : m_xParentStorage->removeElement( aTmpStreamName );
363 : }
364 0 : catch( const uno::Exception& )
365 : {
366 : // the success of the removing is not so important
367 0 : }
368 0 : }
369 0 : }
370 : }
371 0 : catch( const uno::Exception& )
372 : {
373 : // repair the object if necessary
374 0 : switch( nStep )
375 : {
376 : case 4:
377 : case 3:
378 0 : if ( !aTmpStreamName.isEmpty() && aTmpStreamName != m_aEntryName )
379 : try
380 : {
381 0 : if ( m_xParentStorage->hasByName( m_aEntryName ) )
382 0 : m_xParentStorage->removeElement( m_aEntryName );
383 0 : m_xParentStorage->renameElement( aTmpStreamName, m_aEntryName );
384 : }
385 0 : catch ( const uno::Exception& )
386 : {
387 : try {
388 0 : close( sal_True );
389 0 : } catch( const uno::Exception& ) {}
390 :
391 0 : m_xParentStorage->dispose(); // ??? the storage has information loss, it should be closed without committing!
392 0 : throw uno::RuntimeException(); // the repairing is not possible
393 : }
394 : case 2:
395 : try
396 : {
397 0 : m_xObjectStream = m_xParentStorage->openStreamElement( m_aEntryName, m_bReadOnly ? embed::ElementModes::READ : embed::ElementModes::READWRITE );
398 0 : m_nObjectState = embed::EmbedStates::LOADED;
399 : }
400 0 : catch( const uno::Exception& )
401 : {
402 : try {
403 0 : close( sal_True );
404 0 : } catch( const uno::Exception& ) {}
405 :
406 0 : throw uno::RuntimeException(); // the repairing is not possible
407 : }
408 : // no break as designed!
409 :
410 : case 1:
411 : case 0:
412 0 : if ( !aStorageName.isEmpty() )
413 : try {
414 0 : m_xParentStorage->removeElement( aStorageName );
415 0 : } catch( const uno::Exception& ) { SAL_WARN( "embeddedobj.ole", "Can not remove temporary storage!" ); }
416 0 : break;
417 : }
418 : }
419 :
420 0 : if ( bResult )
421 : {
422 : // the conversion was done successfully, now the additional initializations should happen
423 :
424 0 : MoveListeners();
425 0 : m_xWrappedObject->setClientSite( m_xClientSite );
426 0 : if ( m_xParent.is() )
427 : {
428 0 : uno::Reference< container::XChild > xChild( m_xWrappedObject, uno::UNO_QUERY );
429 0 : if ( xChild.is() )
430 0 : xChild->setParent( m_xParent );
431 : }
432 :
433 : }
434 :
435 0 : return bResult;
436 : }
437 :
438 :
439 0 : void SAL_CALL OleEmbeddedObject::changeState( sal_Int32 nNewState )
440 : throw ( embed::UnreachableStateException,
441 : embed::WrongStateException,
442 : uno::Exception,
443 : uno::RuntimeException, std::exception )
444 : {
445 : // begin wrapping related part ====================
446 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
447 0 : if ( xWrappedObject.is() )
448 : {
449 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
450 0 : xWrappedObject->changeState( nNewState );
451 0 : return;
452 : }
453 : // end wrapping related part ====================
454 :
455 0 : ::osl::ResettableMutexGuard aGuard( m_aMutex );
456 :
457 0 : if ( m_bDisposed )
458 0 : throw lang::DisposedException(); // TODO
459 :
460 0 : if ( m_nObjectState == -1 )
461 : throw embed::WrongStateException( "The object has no persistence!",
462 0 : static_cast< ::cppu::OWeakObject* >(this) );
463 :
464 : // in case the object is already in requested state
465 0 : if ( m_nObjectState == nNewState )
466 0 : return;
467 :
468 : #ifdef WNT
469 : if ( m_pOleComponent )
470 : {
471 : if ( m_nTargetState != -1 )
472 : {
473 : // means that the object is currently trying to reach the target state
474 : throw embed::StateChangeInProgressException( OUString(),
475 : uno::Reference< uno::XInterface >(),
476 : m_nTargetState );
477 : }
478 :
479 : TargetStateControl_Impl aControl( m_nTargetState, nNewState );
480 :
481 : // TODO: additional verbs can be a problem, since nobody knows how the object
482 : // will behave after activation
483 :
484 : sal_Int32 nOldState = m_nObjectState;
485 : aGuard.clear();
486 : StateChangeNotification_Impl( sal_True, nOldState, nNewState );
487 : aGuard.reset();
488 :
489 : try
490 : {
491 : if ( nNewState == embed::EmbedStates::LOADED )
492 : {
493 : // This means just closing of the current object
494 : // If component can not be closed the object stays in loaded state
495 : // and it holds reference to "incomplete" component
496 : // If the object is switched to running state later
497 : // the component will become "complete"
498 :
499 : // the loaded state must be set before, because of notifications!
500 : m_nObjectState = nNewState;
501 :
502 : {
503 : VerbExecutionControllerGuard aVerbGuard( m_aVerbExecutionController );
504 : m_pOleComponent->CloseObject();
505 : }
506 :
507 : aGuard.clear();
508 : StateChangeNotification_Impl( sal_False, nOldState, m_nObjectState );
509 : aGuard.reset();
510 : }
511 : else if ( nNewState == embed::EmbedStates::RUNNING || nNewState == embed::EmbedStates::ACTIVE )
512 : {
513 : if ( m_nObjectState == embed::EmbedStates::LOADED )
514 : {
515 : // if the target object is in loaded state and a different state is specified
516 : // as a new one the object first must be switched to running state.
517 :
518 : // the component can exist already in nonrunning state
519 : // it can be created during loading to detect type of object
520 : CreateOleComponentAndLoad_Impl( m_pOleComponent );
521 :
522 : SwitchComponentToRunningState_Impl();
523 : m_nObjectState = embed::EmbedStates::RUNNING;
524 : aGuard.clear();
525 : StateChangeNotification_Impl( sal_False, nOldState, m_nObjectState );
526 : aGuard.reset();
527 :
528 : if ( m_pOleComponent && m_bHasSizeToSet )
529 : {
530 : aGuard.clear();
531 : try {
532 : m_pOleComponent->SetExtent( m_aSizeToSet, m_nAspectToSet );
533 : m_bHasSizeToSet = sal_False;
534 : }
535 : catch( const uno::Exception& ) {}
536 : aGuard.reset();
537 : }
538 :
539 : if ( m_nObjectState == nNewState )
540 : return;
541 : }
542 :
543 : // so now the object is either switched from Active to Running state or vise versa
544 : // the notification about object state change will be done asynchronously
545 : if ( m_nObjectState == embed::EmbedStates::RUNNING && nNewState == embed::EmbedStates::ACTIVE )
546 : {
547 : // execute OPEN verb, if object does not reach active state it is an object's problem
548 : aGuard.clear();
549 : m_pOleComponent->ExecuteVerb( embed::EmbedVerbs::MS_OLEVERB_OPEN );
550 : aGuard.reset();
551 :
552 : // some objects do not allow to set the size even in running state
553 : if ( m_pOleComponent && m_bHasSizeToSet )
554 : {
555 : aGuard.clear();
556 : try {
557 : m_pOleComponent->SetExtent( m_aSizeToSet, m_nAspectToSet );
558 : m_bHasSizeToSet = sal_False;
559 : }
560 : catch( uno::Exception& ) {}
561 : aGuard.reset();
562 : }
563 :
564 : m_nObjectState = nNewState;
565 : }
566 : else if ( m_nObjectState == embed::EmbedStates::ACTIVE && nNewState == embed::EmbedStates::RUNNING )
567 : {
568 : aGuard.clear();
569 : m_pOleComponent->CloseObject();
570 : m_pOleComponent->RunObject(); // Should not fail, the object already was active
571 : aGuard.reset();
572 : m_nObjectState = nNewState;
573 : }
574 : else
575 : {
576 : throw embed::UnreachableStateException();
577 : }
578 : }
579 : else
580 : throw embed::UnreachableStateException();
581 : }
582 : catch( uno::Exception& )
583 : {
584 : aGuard.clear();
585 : StateChangeNotification_Impl( sal_False, nOldState, m_nObjectState );
586 : throw;
587 : }
588 : }
589 : else
590 : #endif
591 : {
592 0 : throw embed::UnreachableStateException();
593 0 : }
594 : }
595 :
596 :
597 0 : uno::Sequence< sal_Int32 > SAL_CALL OleEmbeddedObject::getReachableStates()
598 : throw ( embed::WrongStateException,
599 : uno::RuntimeException, std::exception )
600 : {
601 : // begin wrapping related part ====================
602 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
603 0 : if ( xWrappedObject.is() )
604 : {
605 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
606 0 : return xWrappedObject->getReachableStates();
607 : }
608 : // end wrapping related part ====================
609 :
610 0 : ::osl::MutexGuard aGuard( m_aMutex );
611 0 : if ( m_bDisposed )
612 0 : throw lang::DisposedException(); // TODO
613 :
614 0 : if ( m_nObjectState == -1 )
615 : throw embed::WrongStateException( "The object has no persistence!",
616 0 : static_cast< ::cppu::OWeakObject* >(this) );
617 :
618 : #ifdef WNT
619 : if ( m_pOleComponent )
620 : {
621 : if ( m_nObjectState == embed::EmbedStates::LOADED )
622 : {
623 : // the list of supported verbs can be retrieved only when object is in running state
624 : throw embed::NeedsRunningStateException(); // TODO:
625 : }
626 :
627 : // the list of states can only be guessed based on standard verbs,
628 : // since there is no way to detect what additional verbs do
629 : return GetReachableStatesList_Impl( m_pOleComponent->GetVerbList() );
630 : }
631 : else
632 : #endif
633 : {
634 0 : return uno::Sequence< sal_Int32 >();
635 0 : }
636 : }
637 :
638 :
639 0 : sal_Int32 SAL_CALL OleEmbeddedObject::getCurrentState()
640 : throw ( embed::WrongStateException,
641 : uno::RuntimeException, std::exception )
642 : {
643 : // begin wrapping related part ====================
644 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
645 0 : if ( xWrappedObject.is() )
646 : {
647 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
648 0 : return xWrappedObject->getCurrentState();
649 : }
650 : // end wrapping related part ====================
651 :
652 0 : ::osl::MutexGuard aGuard( m_aMutex );
653 0 : if ( m_bDisposed )
654 0 : throw lang::DisposedException(); // TODO
655 :
656 0 : if ( m_nObjectState == -1 )
657 : throw embed::WrongStateException( "The object has no persistence!",
658 0 : static_cast< ::cppu::OWeakObject* >(this) );
659 :
660 : // TODO: Shouldn't we ask object? ( I guess no )
661 0 : return m_nObjectState;
662 : }
663 :
664 : namespace
665 : {
666 0 : bool lcl_CopyStream(uno::Reference<io::XInputStream> xIn, uno::Reference<io::XOutputStream> xOut)
667 : {
668 0 : const sal_Int32 nChunkSize = 4096;
669 0 : uno::Sequence< sal_Int8 > aData(nChunkSize);
670 0 : sal_Int32 nTotalRead = 0;
671 : sal_Int32 nRead;
672 0 : do
673 : {
674 0 : nRead = xIn->readBytes(aData, nChunkSize);
675 0 : nTotalRead += nRead;
676 0 : xOut->writeBytes(aData);
677 : } while (nRead == nChunkSize);
678 0 : return nTotalRead != 0;
679 : }
680 :
681 : //Dump the objects content to a tempfile, just the "CONTENTS" stream if
682 : //there is one for non-compound documents, otherwise the whole content.
683 : //On success a file is returned which must be removed by the caller
684 0 : OUString lcl_ExtractObject(::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xFactory,
685 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream > xObjectStream)
686 : {
687 0 : OUString sUrl;
688 :
689 : // the solution is only active for Unix systems
690 : #ifndef WNT
691 : uno::Reference <beans::XPropertySet> xNativeTempFile(
692 : io::TempFile::create(comphelper::getComponentContext(xFactory)),
693 0 : uno::UNO_QUERY_THROW);
694 0 : uno::Reference < io::XStream > xStream(xNativeTempFile, uno::UNO_QUERY_THROW);
695 :
696 0 : uno::Sequence< uno::Any > aArgs( 2 );
697 0 : aArgs[0] <<= xObjectStream;
698 0 : aArgs[1] <<= true; // do not create copy
699 : uno::Reference< container::XNameContainer > xNameContainer(
700 0 : xFactory->createInstanceWithArguments(
701 : OUString("com.sun.star.embed.OLESimpleStorage"),
702 0 : aArgs ), uno::UNO_QUERY_THROW );
703 :
704 0 : uno::Reference< io::XStream > xCONTENTS;
705 : try
706 : {
707 0 : xNameContainer->getByName("CONTENTS") >>= xCONTENTS;
708 : }
709 0 : catch (container::NoSuchElementException const&)
710 : {
711 : // ignore
712 : }
713 :
714 0 : bool bCopied = xCONTENTS.is() && lcl_CopyStream(xCONTENTS->getInputStream(), xStream->getOutputStream());
715 :
716 0 : uno::Reference< io::XSeekable > xSeekableStor(xObjectStream, uno::UNO_QUERY);
717 0 : if (xSeekableStor.is())
718 0 : xSeekableStor->seek(0);
719 :
720 0 : if (!bCopied)
721 0 : bCopied = lcl_CopyStream(xObjectStream->getInputStream(), xStream->getOutputStream());
722 :
723 0 : if (bCopied)
724 : {
725 0 : xNativeTempFile->setPropertyValue("RemoveFile",
726 0 : uno::makeAny(sal_False));
727 0 : uno::Any aUrl = xNativeTempFile->getPropertyValue("Uri");
728 0 : aUrl >>= sUrl;
729 :
730 0 : xNativeTempFile = uno::Reference<beans::XPropertySet>();
731 :
732 : uno::Reference < ucb::XSimpleFileAccess3 > xSimpleFileAccess(
733 0 : ucb::SimpleFileAccess::create( comphelper::getComponentContext(xFactory) ) );
734 :
735 0 : xSimpleFileAccess->setReadOnly(sUrl, sal_True);
736 : }
737 : else
738 : {
739 0 : xNativeTempFile->setPropertyValue("RemoveFile",
740 0 : uno::makeAny(sal_True));
741 : }
742 : #else
743 : (void) xFactory;
744 : (void) xObjectStream;
745 : #endif
746 0 : return sUrl;
747 : }
748 : }
749 :
750 :
751 0 : void SAL_CALL OleEmbeddedObject::doVerb( sal_Int32 nVerbID )
752 : throw ( lang::IllegalArgumentException,
753 : embed::WrongStateException,
754 : embed::UnreachableStateException,
755 : uno::Exception,
756 : uno::RuntimeException, std::exception )
757 : {
758 : // begin wrapping related part ====================
759 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
760 0 : if ( xWrappedObject.is() )
761 : {
762 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
763 0 : xWrappedObject->doVerb( nVerbID );
764 0 : return;
765 : }
766 : // end wrapping related part ====================
767 :
768 0 : ::osl::ResettableMutexGuard aGuard( m_aMutex );
769 0 : if ( m_bDisposed )
770 0 : throw lang::DisposedException(); // TODO
771 :
772 0 : if ( m_nObjectState == -1 )
773 : throw embed::WrongStateException( "The object has no persistence!",
774 0 : static_cast< ::cppu::OWeakObject* >(this) );
775 :
776 : #ifdef WNT
777 : if ( m_pOleComponent )
778 : {
779 : sal_Int32 nOldState = m_nObjectState;
780 :
781 : // TODO/LATER detect target state here and do a notification
782 : // StateChangeNotification_Impl( sal_True, nOldState, nNewState );
783 : if ( m_nObjectState == embed::EmbedStates::LOADED )
784 : {
785 : // if the target object is in loaded state
786 : // it must be switched to running state to execute verb
787 : aGuard.clear();
788 : changeState( embed::EmbedStates::RUNNING );
789 : aGuard.reset();
790 : }
791 :
792 : try {
793 : if ( !m_pOleComponent )
794 : throw uno::RuntimeException();
795 :
796 : // ==== the STAMPIT related solution =============================
797 : m_aVerbExecutionController.StartControlExecution();
798 :
799 :
800 : m_pOleComponent->ExecuteVerb( nVerbID );
801 :
802 : // ==== the STAMPIT related solution =============================
803 : sal_Bool bModifiedOnExecution = m_aVerbExecutionController.EndControlExecution_WasModified();
804 :
805 : // this workaround is implemented for STAMPIT object
806 : // if object was modified during verb execution it is saved here
807 : if ( bModifiedOnExecution && m_pOleComponent->IsDirty() )
808 : SaveObject_Impl();
809 :
810 : }
811 : catch( uno::Exception& )
812 : {
813 : // ==== the STAMPIT related solution =============================
814 : m_aVerbExecutionController.EndControlExecution_WasModified();
815 :
816 :
817 : aGuard.clear();
818 : StateChangeNotification_Impl( sal_False, nOldState, m_nObjectState );
819 : throw;
820 : }
821 :
822 : }
823 : else
824 : #endif
825 : {
826 0 : if ( nVerbID == -9 )
827 : {
828 : // the workaround verb to show the object in case no server is available
829 :
830 : // if it is possible, the object will be converted to OOo format
831 0 : if ( !m_bTriedConversion )
832 : {
833 0 : m_bTriedConversion = true;
834 0 : if ( TryToConvertToOOo() )
835 : {
836 0 : changeState( embed::EmbedStates::UI_ACTIVE );
837 0 : return;
838 : }
839 : }
840 :
841 0 : if ( !m_pOwnView && m_xObjectStream.is() && m_aFilterName != "Text" )
842 : {
843 : try {
844 0 : uno::Reference< io::XSeekable > xSeekable( m_xObjectStream, uno::UNO_QUERY );
845 0 : if ( xSeekable.is() )
846 0 : xSeekable->seek( 0 );
847 :
848 0 : m_pOwnView = new OwnView_Impl( m_xFactory, m_xObjectStream->getInputStream() );
849 0 : m_pOwnView->acquire();
850 : }
851 0 : catch( uno::RuntimeException& )
852 : {
853 0 : throw;
854 : }
855 0 : catch (uno::Exception const& e)
856 : {
857 : SAL_WARN("embeddedobj.ole", "OleEmbeddedObject::doVerb: "
858 : "-9 fallback path: exception caught: " << e.Message);
859 : }
860 : }
861 :
862 0 : if ( m_aFilterName != "Text" && (!m_pOwnView || !m_pOwnView->Open()) )
863 : {
864 : //Make a RO copy and see if the OS can find something to at
865 : //least display the content for us
866 0 : if (m_aTempDumpURL.isEmpty())
867 0 : m_aTempDumpURL = lcl_ExtractObject(m_xFactory, m_xObjectStream);
868 :
869 0 : if (!m_aTempDumpURL.isEmpty())
870 : {
871 : uno::Reference< ::com::sun::star::system::XSystemShellExecute > xSystemShellExecute(
872 0 : ::com::sun::star::system::SystemShellExecute::create(comphelper::getComponentContext(m_xFactory)) );
873 0 : xSystemShellExecute->execute(m_aTempDumpURL, OUString(), ::com::sun::star::system::SystemShellExecuteFlags::URIS_ONLY);
874 : }
875 : else
876 0 : throw embed::UnreachableStateException();
877 : }
878 : }
879 : else
880 : {
881 :
882 0 : throw embed::UnreachableStateException();
883 : }
884 0 : }
885 : }
886 :
887 :
888 0 : uno::Sequence< embed::VerbDescriptor > SAL_CALL OleEmbeddedObject::getSupportedVerbs()
889 : throw ( embed::WrongStateException,
890 : uno::RuntimeException, std::exception )
891 : {
892 : // begin wrapping related part ====================
893 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
894 0 : if ( xWrappedObject.is() )
895 : {
896 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
897 0 : return xWrappedObject->getSupportedVerbs();
898 : }
899 : // end wrapping related part ====================
900 :
901 0 : ::osl::MutexGuard aGuard( m_aMutex );
902 0 : if ( m_bDisposed )
903 0 : throw lang::DisposedException(); // TODO
904 :
905 0 : if ( m_nObjectState == -1 )
906 : throw embed::WrongStateException( "The object has no persistence!",
907 0 : static_cast< ::cppu::OWeakObject* >(this) );
908 : #ifdef WNT
909 : if ( m_pOleComponent )
910 : {
911 : // registry could be used in this case
912 : // if ( m_nObjectState == embed::EmbedStates::LOADED )
913 : // {
914 : // // the list of supported verbs can be retrieved only when object is in running state
915 : // throw embed::NeedsRunningStateException(); // TODO:
916 : // }
917 :
918 : return m_pOleComponent->GetVerbList();
919 : }
920 : else
921 : #endif
922 : {
923 0 : return uno::Sequence< embed::VerbDescriptor >();
924 0 : }
925 : }
926 :
927 :
928 0 : void SAL_CALL OleEmbeddedObject::setClientSite(
929 : const uno::Reference< embed::XEmbeddedClient >& xClient )
930 : throw ( embed::WrongStateException,
931 : uno::RuntimeException, std::exception )
932 : {
933 : // begin wrapping related part ====================
934 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
935 0 : if ( xWrappedObject.is() )
936 : {
937 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
938 0 : xWrappedObject->setClientSite( xClient );
939 0 : return;
940 : }
941 : // end wrapping related part ====================
942 :
943 0 : ::osl::MutexGuard aGuard( m_aMutex );
944 0 : if ( m_bDisposed )
945 0 : throw lang::DisposedException(); // TODO
946 :
947 0 : if ( m_xClientSite != xClient)
948 : {
949 0 : if ( m_nObjectState != embed::EmbedStates::LOADED && m_nObjectState != embed::EmbedStates::RUNNING )
950 : throw embed::WrongStateException(
951 : "The client site can not be set currently!",
952 0 : static_cast< ::cppu::OWeakObject* >(this) );
953 :
954 0 : m_xClientSite = xClient;
955 0 : }
956 : }
957 :
958 :
959 0 : uno::Reference< embed::XEmbeddedClient > SAL_CALL OleEmbeddedObject::getClientSite()
960 : throw ( embed::WrongStateException,
961 : uno::RuntimeException, std::exception )
962 : {
963 : // begin wrapping related part ====================
964 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
965 0 : if ( xWrappedObject.is() )
966 : {
967 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
968 0 : return xWrappedObject->getClientSite();
969 : }
970 : // end wrapping related part ====================
971 :
972 0 : ::osl::MutexGuard aGuard( m_aMutex );
973 0 : if ( m_bDisposed )
974 0 : throw lang::DisposedException(); // TODO
975 :
976 0 : if ( m_nObjectState == -1 )
977 : throw embed::WrongStateException( "The object has no persistence!",
978 0 : static_cast< ::cppu::OWeakObject* >(this) );
979 :
980 0 : return m_xClientSite;
981 : }
982 :
983 :
984 0 : void SAL_CALL OleEmbeddedObject::update()
985 : throw ( embed::WrongStateException,
986 : uno::Exception,
987 : uno::RuntimeException, std::exception )
988 : {
989 : // begin wrapping related part ====================
990 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
991 0 : if ( xWrappedObject.is() )
992 : {
993 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
994 0 : xWrappedObject->update();
995 0 : return;
996 : }
997 : // end wrapping related part ====================
998 :
999 0 : ::osl::MutexGuard aGuard( m_aMutex );
1000 0 : if ( m_bDisposed )
1001 0 : throw lang::DisposedException(); // TODO
1002 :
1003 0 : if ( m_nObjectState == -1 )
1004 : throw embed::WrongStateException( "The object has no persistence!",
1005 0 : static_cast< ::cppu::OWeakObject* >(this) );
1006 :
1007 0 : if ( m_nUpdateMode == embed::EmbedUpdateModes::EXPLICIT_UPDATE )
1008 : {
1009 : // TODO: update view representation
1010 : }
1011 : else
1012 : {
1013 : // the object must be up to date
1014 : SAL_WARN_IF( m_nUpdateMode != embed::EmbedUpdateModes::ALWAYS_UPDATE, "embeddedobj.ole", "Unknown update mode!" );
1015 0 : }
1016 : }
1017 :
1018 :
1019 0 : void SAL_CALL OleEmbeddedObject::setUpdateMode( sal_Int32 nMode )
1020 : throw ( embed::WrongStateException,
1021 : uno::RuntimeException, std::exception )
1022 : {
1023 : // begin wrapping related part ====================
1024 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
1025 0 : if ( xWrappedObject.is() )
1026 : {
1027 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1028 0 : xWrappedObject->setUpdateMode( nMode );
1029 0 : return;
1030 : }
1031 : // end wrapping related part ====================
1032 :
1033 0 : ::osl::MutexGuard aGuard( m_aMutex );
1034 0 : if ( m_bDisposed )
1035 0 : throw lang::DisposedException(); // TODO
1036 :
1037 0 : if ( m_nObjectState == -1 )
1038 : throw embed::WrongStateException( "The object has no persistence!",
1039 0 : static_cast< ::cppu::OWeakObject* >(this) );
1040 :
1041 : OSL_ENSURE( nMode == embed::EmbedUpdateModes::ALWAYS_UPDATE
1042 : || nMode == embed::EmbedUpdateModes::EXPLICIT_UPDATE,
1043 : "Unknown update mode!\n" );
1044 0 : m_nUpdateMode = nMode;
1045 : }
1046 :
1047 :
1048 0 : sal_Int64 SAL_CALL OleEmbeddedObject::getStatus( sal_Int64
1049 : nAspect
1050 : )
1051 : throw ( embed::WrongStateException,
1052 : uno::RuntimeException, std::exception )
1053 : {
1054 : // begin wrapping related part ====================
1055 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
1056 0 : if ( xWrappedObject.is() )
1057 : {
1058 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1059 0 : return xWrappedObject->getStatus( nAspect );
1060 : }
1061 : // end wrapping related part ====================
1062 :
1063 0 : ::osl::MutexGuard aGuard( m_aMutex );
1064 0 : if ( m_bDisposed )
1065 0 : throw lang::DisposedException(); // TODO
1066 :
1067 0 : if ( m_nObjectState == -1 )
1068 : throw embed::WrongStateException( "The object must be in running state!",
1069 0 : static_cast< ::cppu::OWeakObject* >(this) );
1070 :
1071 0 : sal_Int64 nResult = 0;
1072 :
1073 : #ifdef WNT
1074 : if ( m_bGotStatus && m_nStatusAspect == nAspect )
1075 : nResult = m_nStatus;
1076 : else if ( m_pOleComponent )
1077 : {
1078 :
1079 : m_nStatus = m_pOleComponent->GetMiscStatus( nAspect );
1080 : m_nStatusAspect = nAspect;
1081 : m_bGotStatus = sal_True;
1082 : nResult = m_nStatus;
1083 : }
1084 : #endif
1085 :
1086 : // this implementation needs size to be provided after object loading/creating to work in optimal way
1087 0 : return ( nResult | embed::EmbedMisc::EMBED_NEEDSSIZEONLOAD );
1088 : }
1089 :
1090 :
1091 0 : void SAL_CALL OleEmbeddedObject::setContainerName( const OUString& sName )
1092 : throw ( uno::RuntimeException, std::exception )
1093 : {
1094 : // begin wrapping related part ====================
1095 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
1096 0 : if ( xWrappedObject.is() )
1097 : {
1098 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1099 0 : xWrappedObject->setContainerName( sName );
1100 0 : return;
1101 : }
1102 : // end wrapping related part ====================
1103 :
1104 0 : ::osl::MutexGuard aGuard( m_aMutex );
1105 0 : if ( m_bDisposed )
1106 0 : throw lang::DisposedException(); // TODO
1107 :
1108 0 : m_aContainerName = sName;
1109 : }
1110 :
1111 :
1112 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|