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 1 : void OleEmbeddedObject::MoveListeners()
122 : {
123 1 : if ( m_pInterfaceContainer )
124 : {
125 : // move state change listeners
126 : {
127 : ::cppu::OInterfaceContainerHelper* pStateChangeContainer =
128 1 : m_pInterfaceContainer->getContainer( cppu::UnoType<embed::XStateChangeListener>::get());
129 1 : if ( pStateChangeContainer != NULL )
130 : {
131 1 : uno::Reference< embed::XStateChangeBroadcaster > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
132 1 : if ( xWrappedObject.is() )
133 : {
134 1 : ::cppu::OInterfaceIteratorHelper pIterator( *pStateChangeContainer );
135 4 : while ( pIterator.hasMoreElements() )
136 : {
137 : try
138 : {
139 2 : xWrappedObject->addStateChangeListener( static_cast<embed::XStateChangeListener*>(pIterator.next()) );
140 : }
141 0 : catch( const uno::RuntimeException& )
142 : {
143 0 : pIterator.remove();
144 : }
145 1 : }
146 1 : }
147 : }
148 : }
149 :
150 : // move event listeners
151 : {
152 : ::cppu::OInterfaceContainerHelper* pEventContainer =
153 1 : m_pInterfaceContainer->getContainer( cppu::UnoType<document::XEventListener>::get());
154 1 : if ( pEventContainer != NULL )
155 : {
156 1 : uno::Reference< document::XEventBroadcaster > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
157 1 : if ( xWrappedObject.is() )
158 : {
159 1 : ::cppu::OInterfaceIteratorHelper pIterator( *pEventContainer );
160 4 : while ( pIterator.hasMoreElements() )
161 : {
162 : try
163 : {
164 2 : xWrappedObject->addEventListener( static_cast<document::XEventListener*>(pIterator.next()) );
165 : }
166 0 : catch( const uno::RuntimeException& )
167 : {
168 0 : pIterator.remove();
169 : }
170 1 : }
171 1 : }
172 : }
173 : }
174 :
175 : // move close listeners
176 : {
177 : ::cppu::OInterfaceContainerHelper* pCloseContainer =
178 1 : m_pInterfaceContainer->getContainer( cppu::UnoType<util::XCloseListener>::get());
179 1 : if ( pCloseContainer != NULL )
180 : {
181 1 : uno::Reference< util::XCloseBroadcaster > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
182 1 : 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 1 : }
197 : }
198 : }
199 :
200 1 : delete m_pInterfaceContainer;
201 1 : m_pInterfaceContainer = NULL;
202 : }
203 1 : }
204 :
205 :
206 1 : uno::Reference< embed::XStorage > OleEmbeddedObject::CreateTemporarySubstorage( OUString& o_aStorageName )
207 : {
208 1 : uno::Reference< embed::XStorage > xResult;
209 :
210 2 : for ( sal_Int32 nInd = 0; nInd < 32000 && !xResult.is(); nInd++ )
211 : {
212 1 : OUString aName = OUString::number( nInd ) + "TMPSTOR" + m_aEntryName;
213 1 : if ( !m_xParentStorage->hasByName( aName ) )
214 : {
215 1 : xResult = m_xParentStorage->openStorageElement( aName, embed::ElementModes::READWRITE );
216 1 : o_aStorageName = aName;
217 : }
218 1 : }
219 :
220 1 : if ( !xResult.is() )
221 : {
222 0 : o_aStorageName.clear();
223 0 : throw uno::RuntimeException();
224 : }
225 :
226 1 : return xResult;
227 : }
228 :
229 :
230 1 : OUString OleEmbeddedObject::MoveToTemporarySubstream()
231 : {
232 1 : OUString aResult;
233 2 : for ( sal_Int32 nInd = 0; nInd < 32000 && aResult.isEmpty(); nInd++ )
234 : {
235 1 : OUString aName = OUString::number( nInd ) + "TMPSTREAM" + m_aEntryName;
236 1 : if ( !m_xParentStorage->hasByName( aName ) )
237 : {
238 1 : m_xParentStorage->renameElement( m_aEntryName, aName );
239 1 : aResult = aName;
240 : }
241 1 : }
242 :
243 1 : if ( aResult.isEmpty() )
244 0 : throw uno::RuntimeException();
245 :
246 1 : return aResult;
247 : }
248 :
249 :
250 1 : bool OleEmbeddedObject::TryToConvertToOOo()
251 : {
252 1 : bool bResult = false;
253 :
254 1 : OUString aStorageName;
255 2 : OUString aTmpStreamName;
256 1 : sal_Int32 nStep = 0;
257 :
258 1 : if ( m_pOleComponent || m_bReadOnly )
259 0 : return false;
260 :
261 : try
262 : {
263 1 : changeState( embed::EmbedStates::LOADED );
264 :
265 : // the stream must be seekable
266 1 : uno::Reference< io::XSeekable > xSeekable( m_xObjectStream, uno::UNO_QUERY_THROW );
267 1 : xSeekable->seek( 0 );
268 1 : m_aFilterName = OwnView_Impl::GetFilterNameFromExtentionAndInStream( m_xFactory, OUString(), m_xObjectStream->getInputStream() );
269 :
270 : // use the solution only for OOXML format currently
271 2 : if ( !m_aFilterName.isEmpty()
272 1 : && ( 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 1 : m_xFactory->createInstance("com.sun.star.document.FilterFactory"),
276 1 : uno::UNO_QUERY_THROW );
277 :
278 2 : OUString aDocServiceName;
279 2 : uno::Any aFilterAnyData = xFilterFactory->getByName( m_aFilterName );
280 2 : uno::Sequence< beans::PropertyValue > aFilterData;
281 1 : if ( aFilterAnyData >>= aFilterData )
282 : {
283 14 : for ( sal_Int32 nInd = 0; nInd < aFilterData.getLength(); nInd++ )
284 13 : if ( aFilterData[nInd].Name == "DocumentService" )
285 1 : aFilterData[nInd].Value >>= aDocServiceName;
286 : }
287 :
288 1 : if ( !aDocServiceName.isEmpty() )
289 : {
290 : // create the model
291 1 : uno::Sequence< uno::Any > aArguments(1);
292 1 : aArguments[0] <<= beans::NamedValue( OUString( "EmbeddedObject" ), uno::makeAny( true ));
293 :
294 2 : uno::Reference< util::XCloseable > xDocument( m_xFactory->createInstanceWithArguments( aDocServiceName, aArguments ), uno::UNO_QUERY_THROW );
295 2 : uno::Reference< frame::XLoadable > xLoadable( xDocument, uno::UNO_QUERY_THROW );
296 2 : uno::Reference< document::XStorageBasedDocument > xStorDoc( xDocument, uno::UNO_QUERY_THROW );
297 :
298 : // let the model behave as embedded one
299 2 : uno::Reference< frame::XModel > xModel( xDocument, uno::UNO_QUERY_THROW );
300 2 : uno::Sequence< beans::PropertyValue > aSeq( 1 );
301 1 : aSeq[0].Name = "SetEmbedded";
302 1 : aSeq[0].Value <<= sal_True;
303 1 : xModel->attachResource( OUString(), aSeq );
304 :
305 : // load the model from the stream
306 2 : uno::Sequence< beans::PropertyValue > aArgs( 5 );
307 1 : aArgs[0].Name = "HierarchicalDocumentName";
308 1 : aArgs[0].Value <<= m_aEntryName;
309 1 : aArgs[1].Name = "ReadOnly";
310 1 : aArgs[1].Value <<= sal_True;
311 1 : aArgs[2].Name = "FilterName";
312 1 : aArgs[2].Value <<= m_aFilterName;
313 1 : aArgs[3].Name = "URL";
314 1 : aArgs[3].Value <<= OUString( "private:stream" );
315 1 : aArgs[4].Name = "InputStream";
316 1 : aArgs[4].Value <<= m_xObjectStream->getInputStream();
317 :
318 1 : xSeekable->seek( 0 );
319 1 : xLoadable->load( aArgs );
320 :
321 : // the model is successfully loaded, create a new storage and store the model to the storage
322 2 : uno::Reference< embed::XStorage > xTmpStorage = CreateTemporarySubstorage( aStorageName );
323 1 : xStorDoc->storeToStorage( xTmpStorage, uno::Sequence< beans::PropertyValue >() );
324 1 : xDocument->close( sal_True );
325 2 : uno::Reference< beans::XPropertySet > xStorProps( xTmpStorage, uno::UNO_QUERY_THROW );
326 2 : OUString aMediaType;
327 1 : xStorProps->getPropertyValue("MediaType") >>= aMediaType;
328 1 : xTmpStorage->dispose();
329 :
330 : // look for the related embedded object factory
331 2 : ::comphelper::MimeConfigurationHelper aConfigHelper( comphelper::getComponentContext(m_xFactory) );
332 2 : OUString aEmbedFactory;
333 1 : if ( !aMediaType.isEmpty() )
334 1 : aEmbedFactory = aConfigHelper.GetFactoryNameByMediaType( aMediaType );
335 :
336 1 : if ( aEmbedFactory.isEmpty() )
337 0 : throw uno::RuntimeException();
338 :
339 2 : uno::Reference< uno::XInterface > xFact = m_xFactory->createInstance( aEmbedFactory );
340 :
341 2 : uno::Reference< embed::XEmbedObjectCreator > xEmbCreator( xFact, uno::UNO_QUERY_THROW );
342 :
343 : // now the object should be adjusted to become the wrapper
344 1 : nStep = 1;
345 2 : uno::Reference< lang::XComponent > xComp( m_xObjectStream, uno::UNO_QUERY_THROW );
346 1 : xComp->dispose();
347 1 : m_xObjectStream = uno::Reference< io::XStream >();
348 1 : m_nObjectState = -1;
349 :
350 1 : nStep = 2;
351 1 : aTmpStreamName = MoveToTemporarySubstream();
352 :
353 1 : nStep = 3;
354 1 : m_xParentStorage->renameElement( aStorageName, m_aEntryName );
355 :
356 1 : nStep = 4;
357 1 : m_xWrappedObject.set( xEmbCreator->createInstanceInitFromEntry( m_xParentStorage, m_aEntryName, uno::Sequence< beans::PropertyValue >(), uno::Sequence< beans::PropertyValue >() ), uno::UNO_QUERY_THROW );
358 :
359 1 : bResult = true; // the change is no more revertable
360 : try
361 : {
362 1 : m_xParentStorage->removeElement( aTmpStreamName );
363 : }
364 0 : catch( const uno::Exception& )
365 : {
366 : // the success of the removing is not so important
367 1 : }
368 1 : }
369 1 : }
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 1 : if ( bResult )
421 : {
422 : // the conversion was done successfully, now the additional initializations should happen
423 :
424 1 : MoveListeners();
425 1 : m_xWrappedObject->setClientSite( m_xClientSite );
426 1 : if ( m_xParent.is() )
427 : {
428 1 : uno::Reference< container::XChild > xChild( m_xWrappedObject, uno::UNO_QUERY );
429 1 : if ( xChild.is() )
430 1 : xChild->setParent( m_xParent );
431 : }
432 :
433 : }
434 :
435 2 : return bResult;
436 : }
437 :
438 :
439 5 : 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 5 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
447 5 : if ( xWrappedObject.is() )
448 : {
449 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
450 2 : xWrappedObject->changeState( nNewState );
451 1 : return;
452 : }
453 : // end wrapping related part ====================
454 :
455 6 : ::osl::ResettableMutexGuard aGuard( m_aMutex );
456 :
457 3 : if ( m_bDisposed )
458 0 : throw lang::DisposedException(); // TODO
459 :
460 3 : 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 3 : if ( m_nObjectState == nNewState )
466 2 : 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 : #else
591 : // if it is possible, the object will be converted to OOo format
592 1 : if ( !m_bTriedConversion )
593 : {
594 1 : m_bTriedConversion = true;
595 1 : if ( TryToConvertToOOo() )
596 : {
597 1 : changeState( nNewState );
598 1 : return;
599 : }
600 : }
601 : #endif
602 : {
603 0 : throw embed::UnreachableStateException();
604 5 : }
605 : }
606 :
607 :
608 0 : uno::Sequence< sal_Int32 > SAL_CALL OleEmbeddedObject::getReachableStates()
609 : throw ( embed::WrongStateException,
610 : uno::RuntimeException, std::exception )
611 : {
612 : // begin wrapping related part ====================
613 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
614 0 : if ( xWrappedObject.is() )
615 : {
616 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
617 0 : return xWrappedObject->getReachableStates();
618 : }
619 : // end wrapping related part ====================
620 :
621 0 : ::osl::MutexGuard aGuard( m_aMutex );
622 0 : if ( m_bDisposed )
623 0 : throw lang::DisposedException(); // TODO
624 :
625 0 : if ( m_nObjectState == -1 )
626 : throw embed::WrongStateException( "The object has no persistence!",
627 0 : static_cast< ::cppu::OWeakObject* >(this) );
628 :
629 : #ifdef WNT
630 : if ( m_pOleComponent )
631 : {
632 : if ( m_nObjectState == embed::EmbedStates::LOADED )
633 : {
634 : // the list of supported verbs can be retrieved only when object is in running state
635 : throw embed::NeedsRunningStateException(); // TODO:
636 : }
637 :
638 : // the list of states can only be guessed based on standard verbs,
639 : // since there is no way to detect what additional verbs do
640 : return GetReachableStatesList_Impl( m_pOleComponent->GetVerbList() );
641 : }
642 : else
643 : #endif
644 : {
645 0 : return uno::Sequence< sal_Int32 >();
646 0 : }
647 : }
648 :
649 :
650 20 : sal_Int32 SAL_CALL OleEmbeddedObject::getCurrentState()
651 : throw ( embed::WrongStateException,
652 : uno::RuntimeException, std::exception )
653 : {
654 : // begin wrapping related part ====================
655 20 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
656 20 : if ( xWrappedObject.is() )
657 : {
658 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
659 4 : return xWrappedObject->getCurrentState();
660 : }
661 : // end wrapping related part ====================
662 :
663 32 : ::osl::MutexGuard aGuard( m_aMutex );
664 16 : if ( m_bDisposed )
665 0 : throw lang::DisposedException(); // TODO
666 :
667 16 : if ( m_nObjectState == -1 )
668 : throw embed::WrongStateException( "The object has no persistence!",
669 0 : static_cast< ::cppu::OWeakObject* >(this) );
670 :
671 : // TODO: Shouldn't we ask object? ( I guess no )
672 36 : return m_nObjectState;
673 : }
674 :
675 : namespace
676 : {
677 0 : bool lcl_CopyStream(uno::Reference<io::XInputStream> xIn, uno::Reference<io::XOutputStream> xOut)
678 : {
679 0 : const sal_Int32 nChunkSize = 4096;
680 0 : uno::Sequence< sal_Int8 > aData(nChunkSize);
681 0 : sal_Int32 nTotalRead = 0;
682 : sal_Int32 nRead;
683 0 : do
684 : {
685 0 : nRead = xIn->readBytes(aData, nChunkSize);
686 0 : nTotalRead += nRead;
687 0 : xOut->writeBytes(aData);
688 : } while (nRead == nChunkSize);
689 0 : return nTotalRead != 0;
690 : }
691 :
692 : //Dump the objects content to a tempfile, just the "CONTENTS" stream if
693 : //there is one for non-compound documents, otherwise the whole content.
694 : //On success a file is returned which must be removed by the caller
695 0 : OUString lcl_ExtractObject(::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xFactory,
696 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream > xObjectStream)
697 : {
698 0 : OUString sUrl;
699 :
700 : // the solution is only active for Unix systems
701 : #ifndef WNT
702 : uno::Reference <beans::XPropertySet> xNativeTempFile(
703 : io::TempFile::create(comphelper::getComponentContext(xFactory)),
704 0 : uno::UNO_QUERY_THROW);
705 0 : uno::Reference < io::XStream > xStream(xNativeTempFile, uno::UNO_QUERY_THROW);
706 :
707 0 : uno::Sequence< uno::Any > aArgs( 2 );
708 0 : aArgs[0] <<= xObjectStream;
709 0 : aArgs[1] <<= true; // do not create copy
710 : uno::Reference< container::XNameContainer > xNameContainer(
711 0 : xFactory->createInstanceWithArguments(
712 : OUString("com.sun.star.embed.OLESimpleStorage"),
713 0 : aArgs ), uno::UNO_QUERY_THROW );
714 :
715 0 : uno::Reference< io::XStream > xCONTENTS;
716 : try
717 : {
718 0 : xNameContainer->getByName("CONTENTS") >>= xCONTENTS;
719 : }
720 0 : catch (container::NoSuchElementException const&)
721 : {
722 : // ignore
723 : }
724 :
725 0 : bool bCopied = xCONTENTS.is() && lcl_CopyStream(xCONTENTS->getInputStream(), xStream->getOutputStream());
726 :
727 0 : uno::Reference< io::XSeekable > xSeekableStor(xObjectStream, uno::UNO_QUERY);
728 0 : if (xSeekableStor.is())
729 0 : xSeekableStor->seek(0);
730 :
731 0 : if (!bCopied)
732 0 : bCopied = lcl_CopyStream(xObjectStream->getInputStream(), xStream->getOutputStream());
733 :
734 0 : if (bCopied)
735 : {
736 0 : xNativeTempFile->setPropertyValue("RemoveFile",
737 0 : uno::makeAny(sal_False));
738 0 : uno::Any aUrl = xNativeTempFile->getPropertyValue("Uri");
739 0 : aUrl >>= sUrl;
740 :
741 0 : xNativeTempFile = uno::Reference<beans::XPropertySet>();
742 :
743 : uno::Reference < ucb::XSimpleFileAccess3 > xSimpleFileAccess(
744 0 : ucb::SimpleFileAccess::create( comphelper::getComponentContext(xFactory) ) );
745 :
746 0 : xSimpleFileAccess->setReadOnly(sUrl, sal_True);
747 : }
748 : else
749 : {
750 0 : xNativeTempFile->setPropertyValue("RemoveFile",
751 0 : uno::makeAny(sal_True));
752 : }
753 : #else
754 : (void) xFactory;
755 : (void) xObjectStream;
756 : #endif
757 0 : return sUrl;
758 : }
759 : }
760 :
761 :
762 0 : void SAL_CALL OleEmbeddedObject::doVerb( sal_Int32 nVerbID )
763 : throw ( lang::IllegalArgumentException,
764 : embed::WrongStateException,
765 : embed::UnreachableStateException,
766 : uno::Exception,
767 : uno::RuntimeException, std::exception )
768 : {
769 : // begin wrapping related part ====================
770 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
771 0 : if ( xWrappedObject.is() )
772 : {
773 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
774 0 : xWrappedObject->doVerb( nVerbID );
775 0 : return;
776 : }
777 : // end wrapping related part ====================
778 :
779 0 : ::osl::ResettableMutexGuard aGuard( m_aMutex );
780 0 : if ( m_bDisposed )
781 0 : throw lang::DisposedException(); // TODO
782 :
783 0 : if ( m_nObjectState == -1 )
784 : throw embed::WrongStateException( "The object has no persistence!",
785 0 : static_cast< ::cppu::OWeakObject* >(this) );
786 :
787 : #ifdef WNT
788 : if ( m_pOleComponent )
789 : {
790 : sal_Int32 nOldState = m_nObjectState;
791 :
792 : // TODO/LATER detect target state here and do a notification
793 : // StateChangeNotification_Impl( sal_True, nOldState, nNewState );
794 : if ( m_nObjectState == embed::EmbedStates::LOADED )
795 : {
796 : // if the target object is in loaded state
797 : // it must be switched to running state to execute verb
798 : aGuard.clear();
799 : changeState( embed::EmbedStates::RUNNING );
800 : aGuard.reset();
801 : }
802 :
803 : try {
804 : if ( !m_pOleComponent )
805 : throw uno::RuntimeException();
806 :
807 : // ==== the STAMPIT related solution =============================
808 : m_aVerbExecutionController.StartControlExecution();
809 :
810 :
811 : m_pOleComponent->ExecuteVerb( nVerbID );
812 :
813 : // ==== the STAMPIT related solution =============================
814 : sal_Bool bModifiedOnExecution = m_aVerbExecutionController.EndControlExecution_WasModified();
815 :
816 : // this workaround is implemented for STAMPIT object
817 : // if object was modified during verb execution it is saved here
818 : if ( bModifiedOnExecution && m_pOleComponent->IsDirty() )
819 : SaveObject_Impl();
820 :
821 : }
822 : catch( uno::Exception& )
823 : {
824 : // ==== the STAMPIT related solution =============================
825 : m_aVerbExecutionController.EndControlExecution_WasModified();
826 :
827 :
828 : aGuard.clear();
829 : StateChangeNotification_Impl( sal_False, nOldState, m_nObjectState );
830 : throw;
831 : }
832 :
833 : }
834 : else
835 : #endif
836 : {
837 0 : if ( nVerbID == -9 )
838 : {
839 : // the workaround verb to show the object in case no server is available
840 :
841 : // if it is possible, the object will be converted to OOo format
842 0 : if ( !m_bTriedConversion )
843 : {
844 0 : m_bTriedConversion = true;
845 0 : if ( TryToConvertToOOo() )
846 : {
847 0 : changeState( embed::EmbedStates::UI_ACTIVE );
848 0 : return;
849 : }
850 : }
851 :
852 0 : if ( !m_pOwnView && m_xObjectStream.is() && m_aFilterName != "Text" )
853 : {
854 : try {
855 0 : uno::Reference< io::XSeekable > xSeekable( m_xObjectStream, uno::UNO_QUERY );
856 0 : if ( xSeekable.is() )
857 0 : xSeekable->seek( 0 );
858 :
859 0 : m_pOwnView = new OwnView_Impl( m_xFactory, m_xObjectStream->getInputStream() );
860 0 : m_pOwnView->acquire();
861 : }
862 0 : catch( uno::RuntimeException& )
863 : {
864 0 : throw;
865 : }
866 0 : catch (uno::Exception const& e)
867 : {
868 : SAL_WARN("embeddedobj.ole", "OleEmbeddedObject::doVerb: "
869 : "-9 fallback path: exception caught: " << e.Message);
870 : }
871 : }
872 :
873 0 : if ( m_aFilterName != "Text" && (!m_pOwnView || !m_pOwnView->Open()) )
874 : {
875 : //Make a RO copy and see if the OS can find something to at
876 : //least display the content for us
877 0 : if (m_aTempDumpURL.isEmpty())
878 0 : m_aTempDumpURL = lcl_ExtractObject(m_xFactory, m_xObjectStream);
879 :
880 0 : if (!m_aTempDumpURL.isEmpty())
881 : {
882 : uno::Reference< ::com::sun::star::system::XSystemShellExecute > xSystemShellExecute(
883 0 : ::com::sun::star::system::SystemShellExecute::create(comphelper::getComponentContext(m_xFactory)) );
884 0 : xSystemShellExecute->execute(m_aTempDumpURL, OUString(), ::com::sun::star::system::SystemShellExecuteFlags::URIS_ONLY);
885 : }
886 : else
887 0 : throw embed::UnreachableStateException();
888 : }
889 : }
890 : else
891 : {
892 :
893 0 : throw embed::UnreachableStateException();
894 : }
895 0 : }
896 : }
897 :
898 :
899 0 : uno::Sequence< embed::VerbDescriptor > SAL_CALL OleEmbeddedObject::getSupportedVerbs()
900 : throw ( embed::WrongStateException,
901 : uno::RuntimeException, std::exception )
902 : {
903 : // begin wrapping related part ====================
904 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
905 0 : if ( xWrappedObject.is() )
906 : {
907 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
908 0 : return xWrappedObject->getSupportedVerbs();
909 : }
910 : // end wrapping related part ====================
911 :
912 0 : ::osl::MutexGuard aGuard( m_aMutex );
913 0 : if ( m_bDisposed )
914 0 : throw lang::DisposedException(); // TODO
915 :
916 0 : if ( m_nObjectState == -1 )
917 : throw embed::WrongStateException( "The object has no persistence!",
918 0 : static_cast< ::cppu::OWeakObject* >(this) );
919 : #ifdef WNT
920 : if ( m_pOleComponent )
921 : {
922 : // registry could be used in this case
923 : // if ( m_nObjectState == embed::EmbedStates::LOADED )
924 : // {
925 : // // the list of supported verbs can be retrieved only when object is in running state
926 : // throw embed::NeedsRunningStateException(); // TODO:
927 : // }
928 :
929 : return m_pOleComponent->GetVerbList();
930 : }
931 : else
932 : #endif
933 : {
934 0 : return uno::Sequence< embed::VerbDescriptor >();
935 0 : }
936 : }
937 :
938 :
939 3 : void SAL_CALL OleEmbeddedObject::setClientSite(
940 : const uno::Reference< embed::XEmbeddedClient >& xClient )
941 : throw ( embed::WrongStateException,
942 : uno::RuntimeException, std::exception )
943 : {
944 : // begin wrapping related part ====================
945 3 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
946 3 : if ( xWrappedObject.is() )
947 : {
948 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
949 2 : xWrappedObject->setClientSite( xClient );
950 3 : return;
951 : }
952 : // end wrapping related part ====================
953 :
954 2 : ::osl::MutexGuard aGuard( m_aMutex );
955 1 : if ( m_bDisposed )
956 0 : throw lang::DisposedException(); // TODO
957 :
958 1 : if ( m_xClientSite != xClient)
959 : {
960 0 : if ( m_nObjectState != embed::EmbedStates::LOADED && m_nObjectState != embed::EmbedStates::RUNNING )
961 : throw embed::WrongStateException(
962 : "The client site can not be set currently!",
963 0 : static_cast< ::cppu::OWeakObject* >(this) );
964 :
965 0 : m_xClientSite = xClient;
966 2 : }
967 : }
968 :
969 :
970 1 : uno::Reference< embed::XEmbeddedClient > SAL_CALL OleEmbeddedObject::getClientSite()
971 : throw ( embed::WrongStateException,
972 : uno::RuntimeException, std::exception )
973 : {
974 : // begin wrapping related part ====================
975 1 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
976 1 : if ( xWrappedObject.is() )
977 : {
978 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
979 1 : return xWrappedObject->getClientSite();
980 : }
981 : // end wrapping related part ====================
982 :
983 0 : ::osl::MutexGuard aGuard( m_aMutex );
984 0 : if ( m_bDisposed )
985 0 : throw lang::DisposedException(); // TODO
986 :
987 0 : if ( m_nObjectState == -1 )
988 : throw embed::WrongStateException( "The object has no persistence!",
989 0 : static_cast< ::cppu::OWeakObject* >(this) );
990 :
991 1 : return m_xClientSite;
992 : }
993 :
994 :
995 0 : void SAL_CALL OleEmbeddedObject::update()
996 : throw ( embed::WrongStateException,
997 : uno::Exception,
998 : uno::RuntimeException, std::exception )
999 : {
1000 : // begin wrapping related part ====================
1001 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
1002 0 : if ( xWrappedObject.is() )
1003 : {
1004 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1005 0 : xWrappedObject->update();
1006 0 : return;
1007 : }
1008 : // end wrapping related part ====================
1009 :
1010 0 : ::osl::MutexGuard aGuard( m_aMutex );
1011 0 : if ( m_bDisposed )
1012 0 : throw lang::DisposedException(); // TODO
1013 :
1014 0 : if ( m_nObjectState == -1 )
1015 : throw embed::WrongStateException( "The object has no persistence!",
1016 0 : static_cast< ::cppu::OWeakObject* >(this) );
1017 :
1018 0 : if ( m_nUpdateMode == embed::EmbedUpdateModes::EXPLICIT_UPDATE )
1019 : {
1020 : // TODO: update view representation
1021 : }
1022 : else
1023 : {
1024 : // the object must be up to date
1025 : SAL_WARN_IF( m_nUpdateMode != embed::EmbedUpdateModes::ALWAYS_UPDATE, "embeddedobj.ole", "Unknown update mode!" );
1026 0 : }
1027 : }
1028 :
1029 :
1030 0 : void SAL_CALL OleEmbeddedObject::setUpdateMode( sal_Int32 nMode )
1031 : throw ( embed::WrongStateException,
1032 : uno::RuntimeException, std::exception )
1033 : {
1034 : // begin wrapping related part ====================
1035 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
1036 0 : if ( xWrappedObject.is() )
1037 : {
1038 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1039 0 : xWrappedObject->setUpdateMode( nMode );
1040 0 : return;
1041 : }
1042 : // end wrapping related part ====================
1043 :
1044 0 : ::osl::MutexGuard aGuard( m_aMutex );
1045 0 : if ( m_bDisposed )
1046 0 : throw lang::DisposedException(); // TODO
1047 :
1048 0 : if ( m_nObjectState == -1 )
1049 : throw embed::WrongStateException( "The object has no persistence!",
1050 0 : static_cast< ::cppu::OWeakObject* >(this) );
1051 :
1052 : OSL_ENSURE( nMode == embed::EmbedUpdateModes::ALWAYS_UPDATE
1053 : || nMode == embed::EmbedUpdateModes::EXPLICIT_UPDATE,
1054 : "Unknown update mode!\n" );
1055 0 : m_nUpdateMode = nMode;
1056 : }
1057 :
1058 :
1059 0 : sal_Int64 SAL_CALL OleEmbeddedObject::getStatus( sal_Int64
1060 : nAspect
1061 : )
1062 : throw ( embed::WrongStateException,
1063 : uno::RuntimeException, std::exception )
1064 : {
1065 : // begin wrapping related part ====================
1066 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
1067 0 : if ( xWrappedObject.is() )
1068 : {
1069 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1070 0 : return xWrappedObject->getStatus( nAspect );
1071 : }
1072 : // end wrapping related part ====================
1073 :
1074 0 : ::osl::MutexGuard aGuard( m_aMutex );
1075 0 : if ( m_bDisposed )
1076 0 : throw lang::DisposedException(); // TODO
1077 :
1078 0 : if ( m_nObjectState == -1 )
1079 : throw embed::WrongStateException( "The object must be in running state!",
1080 0 : static_cast< ::cppu::OWeakObject* >(this) );
1081 :
1082 0 : sal_Int64 nResult = 0;
1083 :
1084 : #ifdef WNT
1085 : if ( m_bGotStatus && m_nStatusAspect == nAspect )
1086 : nResult = m_nStatus;
1087 : else if ( m_pOleComponent )
1088 : {
1089 :
1090 : m_nStatus = m_pOleComponent->GetMiscStatus( nAspect );
1091 : m_nStatusAspect = nAspect;
1092 : m_bGotStatus = sal_True;
1093 : nResult = m_nStatus;
1094 : }
1095 : #endif
1096 :
1097 : // this implementation needs size to be provided after object loading/creating to work in optimal way
1098 0 : return ( nResult | embed::EmbedMisc::EMBED_NEEDSSIZEONLOAD );
1099 : }
1100 :
1101 :
1102 0 : void SAL_CALL OleEmbeddedObject::setContainerName( const OUString& sName )
1103 : throw ( uno::RuntimeException, std::exception )
1104 : {
1105 : // begin wrapping related part ====================
1106 0 : uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
1107 0 : if ( xWrappedObject.is() )
1108 : {
1109 : // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1110 0 : xWrappedObject->setContainerName( sName );
1111 0 : return;
1112 : }
1113 : // end wrapping related part ====================
1114 :
1115 0 : ::osl::MutexGuard aGuard( m_aMutex );
1116 0 : if ( m_bDisposed )
1117 0 : throw lang::DisposedException(); // TODO
1118 :
1119 0 : m_aContainerName = sName;
1120 : }
1121 :
1122 :
1123 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|