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