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
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
|
/* -*- 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 <com/sun/star/container/XChild.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/embed/EmbeddedObjectCreator.hpp>
#include <com/sun/star/embed/XLinkCreator.hpp>
#include <com/sun/star/embed/XEmbedPersist.hpp>
#include <com/sun/star/embed/XLinkageSupport.hpp>
#include <com/sun/star/embed/XTransactedObject.hpp>
#include <com/sun/star/embed/XOptimizedStorage.hpp>
#include <com/sun/star/embed/EntryInitModes.hpp>
#include <com/sun/star/util/XCloseable.hpp>
#include <com/sun/star/util/XModifiable.hpp>
#include <com/sun/star/embed/EmbedStates.hpp>
#include <com/sun/star/datatransfer/XTransferable.hpp>
#include <com/sun/star/beans/XPropertySetInfo.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/embed/Aspects.hpp>
#include <com/sun/star/embed/EmbedMisc.hpp>
#include <comphelper/seqstream.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/storagehelper.hxx>
#include <comphelper/embeddedobjectcontainer.hxx>
#include <comphelper/sequence.hxx>
#include <cppuhelper/weakref.hxx>
#include <boost/unordered_map.hpp>
#include <algorithm>
#include <rtl/logfile.hxx>
using namespace ::com::sun::star;
namespace comphelper
{
struct hashObjectName_Impl
{
size_t operator()(const OUString Str) const
{
return (size_t)Str.hashCode();
}
};
struct eqObjectName_Impl
{
sal_Bool operator()(const OUString Str1, const OUString Str2) const
{
return ( Str1 == Str2 );
}
};
typedef boost::unordered_map
<
OUString,
::com::sun::star::uno::Reference < com::sun::star::embed::XEmbeddedObject >,
hashObjectName_Impl,
eqObjectName_Impl
>
EmbeddedObjectContainerNameMap;
struct EmbedImpl
{
// TODO/LATER: remove objects from temp. Container storage when object is disposed
EmbeddedObjectContainerNameMap maObjectContainer;
uno::Reference < embed::XStorage > mxStorage;
EmbeddedObjectContainer* mpTempObjectContainer;
uno::Reference < embed::XStorage > mxImageStorage;
uno::WeakReference < uno::XInterface > m_xModel;
//EmbeddedObjectContainerNameMap maTempObjectContainer;
//uno::Reference < embed::XStorage > mxTempStorage;
bool bOwnsStorage;
const uno::Reference < embed::XStorage >& GetReplacements();
};
const uno::Reference < embed::XStorage >& EmbedImpl::GetReplacements()
{
if ( !mxImageStorage.is() )
{
try
{
mxImageStorage = mxStorage->openStorageElement(
OUString("ObjectReplacements"), embed::ElementModes::READWRITE );
}
catch (const uno::Exception&)
{
mxImageStorage = mxStorage->openStorageElement(
OUString("ObjectReplacements"), embed::ElementModes::READ );
}
}
if ( !mxImageStorage.is() )
throw io::IOException();
return mxImageStorage;
}
EmbeddedObjectContainer::EmbeddedObjectContainer()
{
pImpl = new EmbedImpl;
pImpl->mxStorage = ::comphelper::OStorageHelper::GetTemporaryStorage();
pImpl->bOwnsStorage = true;
pImpl->mpTempObjectContainer = 0;
}
EmbeddedObjectContainer::EmbeddedObjectContainer( const uno::Reference < embed::XStorage >& rStor )
{
pImpl = new EmbedImpl;
pImpl->mxStorage = rStor;
pImpl->bOwnsStorage = false;
pImpl->mpTempObjectContainer = 0;
}
EmbeddedObjectContainer::EmbeddedObjectContainer( const uno::Reference < embed::XStorage >& rStor, const uno::Reference < uno::XInterface >& xModel )
{
pImpl = new EmbedImpl;
pImpl->mxStorage = rStor;
pImpl->bOwnsStorage = false;
pImpl->mpTempObjectContainer = 0;
pImpl->m_xModel = xModel;
}
void EmbeddedObjectContainer::SwitchPersistence( const uno::Reference < embed::XStorage >& rStor )
{
ReleaseImageSubStorage();
if ( pImpl->bOwnsStorage )
pImpl->mxStorage->dispose();
pImpl->mxStorage = rStor;
pImpl->bOwnsStorage = false;
}
sal_Bool EmbeddedObjectContainer::CommitImageSubStorage()
{
if ( pImpl->mxImageStorage.is() )
{
try
{
sal_Bool bReadOnlyMode = sal_True;
uno::Reference < beans::XPropertySet > xSet(pImpl->mxImageStorage,uno::UNO_QUERY);
if ( xSet.is() )
{
// get the open mode from the parent storage
sal_Int32 nMode = 0;
uno::Any aAny = xSet->getPropertyValue( OUString("OpenMode") );
if ( aAny >>= nMode )
bReadOnlyMode = !(nMode & embed::ElementModes::WRITE );
} // if ( xSet.is() )
if ( !bReadOnlyMode )
{
uno::Reference< embed::XTransactedObject > xTransact( pImpl->mxImageStorage, uno::UNO_QUERY_THROW );
xTransact->commit();
}
}
catch (const uno::Exception&)
{
return sal_False;
}
}
return sal_True;
}
void EmbeddedObjectContainer::ReleaseImageSubStorage()
{
CommitImageSubStorage();
if ( pImpl->mxImageStorage.is() )
{
try
{
pImpl->mxImageStorage->dispose();
pImpl->mxImageStorage = uno::Reference< embed::XStorage >();
}
catch (const uno::Exception&)
{
OSL_FAIL( "Problems releasing image substorage!\n" );
}
}
}
EmbeddedObjectContainer::~EmbeddedObjectContainer()
{
ReleaseImageSubStorage();
if ( pImpl->bOwnsStorage )
pImpl->mxStorage->dispose();
delete pImpl->mpTempObjectContainer;
delete pImpl;
}
void EmbeddedObjectContainer::CloseEmbeddedObjects()
{
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
while ( aIt != pImpl->maObjectContainer.end() )
{
uno::Reference < util::XCloseable > xClose( (*aIt).second, uno::UNO_QUERY );
if ( xClose.is() )
{
try
{
xClose->close( sal_True );
}
catch (const uno::Exception&)
{
}
}
++aIt;
}
}
OUString EmbeddedObjectContainer::CreateUniqueObjectName()
{
OUString aPersistName("Object ");
OUString aStr;
sal_Int32 i=1;
do
{
aStr = aPersistName;
aStr += OUString::valueOf( i++ );
}
while( HasEmbeddedObject( aStr ) );
// TODO/LATER: should we consider deleted objects?
return aStr;
}
uno::Sequence < OUString > EmbeddedObjectContainer::GetObjectNames()
{
uno::Sequence < OUString > aSeq( pImpl->maObjectContainer.size() );
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
sal_Int32 nIdx=0;
while ( aIt != pImpl->maObjectContainer.end() )
aSeq[nIdx++] = (*aIt++).first;
return aSeq;
}
sal_Bool EmbeddedObjectContainer::HasEmbeddedObjects()
{
return pImpl->maObjectContainer.size() != 0;
}
sal_Bool EmbeddedObjectContainer::HasEmbeddedObject( const OUString& rName )
{
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName );
if ( aIt == pImpl->maObjectContainer.end() )
{
uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY );
return xAccess->hasByName(rName);
}
else
return sal_True;
}
sal_Bool EmbeddedObjectContainer::HasEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj )
{
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
while ( aIt != pImpl->maObjectContainer.end() )
{
if ( (*aIt).second == xObj )
return sal_True;
else
++aIt;
}
return sal_False;
}
sal_Bool EmbeddedObjectContainer::HasInstantiatedEmbeddedObject( const OUString& rName )
{
// allows to detect whether the object was already instantiated
// currently the filter instantiate it on loading, so this method allows
// to avoid objects pointing to the same persistence
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName );
return ( aIt != pImpl->maObjectContainer.end() );
}
OUString EmbeddedObjectContainer::GetEmbeddedObjectName( const ::com::sun::star::uno::Reference < ::com::sun::star::embed::XEmbeddedObject >& xObj )
{
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
while ( aIt != pImpl->maObjectContainer.end() )
{
if ( (*aIt).second == xObj )
return (*aIt).first;
else
++aIt;
}
OSL_FAIL( "Unknown object!" );
return OUString();
}
uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::GetEmbeddedObject( const OUString& rName )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::GetEmbeddedObject" );
OSL_ENSURE( !rName.isEmpty(), "Empty object name!");
uno::Reference < embed::XEmbeddedObject > xObj;
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName );
#if OSL_DEBUG_LEVEL > 1
uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY );
uno::Sequence< OUString> aSeq = xAccess->getElementNames();
const OUString* pIter = aSeq.getConstArray();
const OUString* pEnd = pIter + aSeq.getLength();
for(;pIter != pEnd;++pIter)
{
(void)*pIter;
}
OSL_ENSURE( aIt != pImpl->maObjectContainer.end() || xAccess->hasByName(rName), "Could not return object!" );
#endif
// check if object was already created
if ( aIt != pImpl->maObjectContainer.end() )
xObj = (*aIt).second;
else
xObj = Get_Impl( rName, uno::Reference < embed::XEmbeddedObject >() );
return xObj;
}
uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::Get_Impl( const OUString& rName, const uno::Reference < embed::XEmbeddedObject >& xCopy )
{
uno::Reference < embed::XEmbeddedObject > xObj;
try
{
// create the object from the storage
uno::Reference < beans::XPropertySet > xSet( pImpl->mxStorage, uno::UNO_QUERY );
sal_Bool bReadOnlyMode = sal_True;
if ( xSet.is() )
{
// get the open mode from the parent storage
sal_Int32 nMode = 0;
uno::Any aAny = xSet->getPropertyValue( OUString("OpenMode") );
if ( aAny >>= nMode )
bReadOnlyMode = !(nMode & embed::ElementModes::WRITE );
}
// object was not added until now - should happen only by calling this method from "inside"
//TODO/LATER: it would be good to detect an error when an object should be created already, but isn't (not an "inside" call)
uno::Reference < embed::XEmbeddedObjectCreator > xFactory = embed::EmbeddedObjectCreator::create( ::comphelper::getProcessComponentContext() );
uno::Sequence< beans::PropertyValue > aObjDescr( xCopy.is() ? 2 : 1 );
aObjDescr[0].Name = "Parent";
aObjDescr[0].Value <<= pImpl->m_xModel.get();
if ( xCopy.is() )
{
aObjDescr[1].Name = "CloneFrom";
aObjDescr[1].Value <<= xCopy;
}
uno::Sequence< beans::PropertyValue > aMediaDescr( 1 );
aMediaDescr[0].Name = "ReadOnly";
aMediaDescr[0].Value <<= bReadOnlyMode;
xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitFromEntry(
pImpl->mxStorage, rName,
aMediaDescr, aObjDescr ), uno::UNO_QUERY );
// insert object into my list
AddEmbeddedObject( xObj, rName );
}
catch (uno::Exception const& e)
{
SAL_WARN("comphelper", "EmbeddedObjectContainer::Get_Impl: "
"exception caught: " << e.Message);
}
return xObj;
}
uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::CreateEmbeddedObject( const uno::Sequence < sal_Int8 >& rClassId,
const uno::Sequence < beans::PropertyValue >& rArgs, OUString& rNewName )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::CreateEmbeddedObject" );
if ( rNewName.isEmpty() )
rNewName = CreateUniqueObjectName();
OSL_ENSURE( !HasEmbeddedObject(rNewName), "Object to create already exists!");
// create object from classid by inserting it into storage
uno::Reference < embed::XEmbeddedObject > xObj;
try
{
uno::Reference < embed::XEmbeddedObjectCreator > xFactory = embed::EmbeddedObjectCreator::create( ::comphelper::getProcessComponentContext() );
uno::Sequence< beans::PropertyValue > aObjDescr( rArgs.getLength() + 1 );
aObjDescr[0].Name = "Parent";
aObjDescr[0].Value <<= pImpl->m_xModel.get();
::std::copy( rArgs.getConstArray(), rArgs.getConstArray() + rArgs.getLength(), aObjDescr.getArray() + 1 );
xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitNew(
rClassId, OUString(), pImpl->mxStorage, rNewName,
aObjDescr ), uno::UNO_QUERY );
AddEmbeddedObject( xObj, rNewName );
OSL_ENSURE( !xObj.is() || xObj->getCurrentState() != embed::EmbedStates::LOADED,
"A freshly create object should be running always!\n" );
}
catch (uno::Exception const& e)
{
SAL_WARN("comphelper", "EmbeddedObjectContainer::CreateEmbeddedObject: "
"exception caught: " << e.Message);
}
return xObj;
}
uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::CreateEmbeddedObject( const uno::Sequence < sal_Int8 >& rClassId, OUString& rNewName )
{
return CreateEmbeddedObject( rClassId, uno::Sequence < beans::PropertyValue >(), rNewName );
}
void EmbeddedObjectContainer::AddEmbeddedObject( const ::com::sun::star::uno::Reference < ::com::sun::star::embed::XEmbeddedObject >& xObj, const OUString& rName )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::AddEmbeddedObject" );
#if OSL_DEBUG_LEVEL > 1
OSL_ENSURE( !rName.isEmpty(), "Added object doesn't have a name!");
uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY );
uno::Reference < embed::XEmbedPersist > xEmb( xObj, uno::UNO_QUERY );
uno::Reference < embed::XLinkageSupport > xLink( xEmb, uno::UNO_QUERY );
// if the object has a persistance and the object is not a link than it must have persistence entry in the storage
OSL_ENSURE( !( xEmb.is() && ( !xLink.is() || !xLink->isLink() ) ) || xAccess->hasByName(rName),
"Added element not in storage!" );
#endif
// remember object - it needs to be in storage already
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName );
OSL_ENSURE( aIt == pImpl->maObjectContainer.end(), "Element already inserted!" );
pImpl->maObjectContainer[ rName ] = xObj;
uno::Reference < container::XChild > xChild( xObj, uno::UNO_QUERY );
if ( xChild.is() && xChild->getParent() != pImpl->m_xModel.get() )
xChild->setParent( pImpl->m_xModel.get() );
// look for object in temorary container
if ( pImpl->mpTempObjectContainer )
{
aIt = pImpl->mpTempObjectContainer->pImpl->maObjectContainer.begin();
while ( aIt != pImpl->mpTempObjectContainer->pImpl->maObjectContainer.end() )
{
if ( (*aIt).second == xObj )
{
// copy replacement image from temporary container (if there is any)
OUString aTempName = (*aIt).first;
OUString aMediaType;
uno::Reference < io::XInputStream > xStream = pImpl->mpTempObjectContainer->GetGraphicStream( xObj, &aMediaType );
if ( xStream.is() )
{
InsertGraphicStream( xStream, rName, aMediaType );
xStream = 0;
pImpl->mpTempObjectContainer->RemoveGraphicStream( aTempName );
}
// remove object from storage of temporary container
uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
if ( xPersist.is() )
{
try
{
pImpl->mpTempObjectContainer->pImpl->mxStorage->removeElement( aTempName );
}
catch (const uno::Exception&)
{
}
}
// temp. container needs to forget the object
pImpl->mpTempObjectContainer->pImpl->maObjectContainer.erase( aIt );
break;
}
else
++aIt;
}
}
}
sal_Bool EmbeddedObjectContainer::StoreEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj, OUString& rName, sal_Bool bCopy )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::StoreEmbeddedObject" );
uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
if ( rName.isEmpty() )
rName = CreateUniqueObjectName();
#if OSL_DEBUG_LEVEL > 1
uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY );
OSL_ENSURE( !xPersist.is() || !xAccess->hasByName(rName), "Inserting element already present in storage!" );
OSL_ENSURE( xPersist.is() || xObj->getCurrentState() == embed::EmbedStates::RUNNING, "Non persistent object inserted!");
#endif
// insert objects' storage into the container storage (if object has one)
try
{
if ( xPersist.is() )
{
uno::Sequence < beans::PropertyValue > aSeq;
if ( bCopy )
xPersist->storeToEntry( pImpl->mxStorage, rName, aSeq, aSeq );
else
{
//TODO/LATER: possible optimisation, don't store immediately
//xPersist->setPersistentEntry( pImpl->mxStorage, rName, embed::EntryInitModes::ENTRY_NO_INIT, aSeq, aSeq );
xPersist->storeAsEntry( pImpl->mxStorage, rName, aSeq, aSeq );
xPersist->saveCompleted( sal_True );
}
}
}
catch (uno::Exception const& e)
{
SAL_WARN("comphelper", "EmbeddedObjectContainer::StoreEmbeddedObject: "
"exception caught: " << e.Message);
// TODO/LATER: better error recovery should keep storage intact
return sal_False;
}
return sal_True;
}
sal_Bool EmbeddedObjectContainer::InsertEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj, OUString& rName )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedObject( Object )" );
// store it into the container storage
if ( StoreEmbeddedObject( xObj, rName, sal_False ) )
{
// remember object
AddEmbeddedObject( xObj, rName );
return sal_True;
}
else
return sal_False;
}
uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::InsertEmbeddedObject( const uno::Reference < io::XInputStream >& xStm, OUString& rNewName )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedObject( InputStream )" );
if ( rNewName.isEmpty() )
rNewName = CreateUniqueObjectName();
// store it into the container storage
sal_Bool bIsStorage = sal_False;
try
{
// first try storage persistence
uno::Reference < embed::XStorage > xStore = ::comphelper::OStorageHelper::GetStorageFromInputStream( xStm );
// storage was created from stream successfully
bIsStorage = sal_True;
uno::Reference < embed::XStorage > xNewStore = pImpl->mxStorage->openStorageElement( rNewName, embed::ElementModes::READWRITE );
xStore->copyToStorage( xNewStore );
}
catch (const uno::Exception&)
{
if ( bIsStorage )
// it is storage persistence, but opening of new substorage or copying to it failed
return uno::Reference < embed::XEmbeddedObject >();
// stream didn't contain a storage, now try stream persistence
try
{
uno::Reference < io::XStream > xNewStream = pImpl->mxStorage->openStreamElement( rNewName, embed::ElementModes::READWRITE );
::comphelper::OStorageHelper::CopyInputToOutput( xStm, xNewStream->getOutputStream() );
// No mediatype is provided so the default for OLE objects value is used
// it is correct so for now, but what if somebody introduces a new stream based embedded object?
// Probably introducing of such an object must be restricted ( a storage must be used! ).
uno::Reference< beans::XPropertySet > xProps( xNewStream, uno::UNO_QUERY_THROW );
xProps->setPropertyValue( OUString( "MediaType" ),
uno::makeAny( OUString( "application/vnd.sun.star.oleobject" ) ) );
}
catch (uno::Exception const& e)
{
// complete disaster!
SAL_WARN("comphelper",
"EmbeddedObjectContainer::InsertEmbeddedObject: "
"exception caught: " << e.Message);
return uno::Reference < embed::XEmbeddedObject >();
}
}
// stream was copied into the container storage in either way, now try to open something form it
uno::Reference < embed::XEmbeddedObject > xRet = GetEmbeddedObject( rNewName );
try
{
if ( !xRet.is() )
// no object could be created, so withdraw insertion
pImpl->mxStorage->removeElement( rNewName );
}
catch (const uno::Exception&)
{
}
return xRet;
}
uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::InsertEmbeddedObject( const ::com::sun::star::uno::Sequence < ::com::sun::star::beans::PropertyValue >& aMedium, OUString& rNewName )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedObject( MediaDescriptor )" );
if ( rNewName.isEmpty() )
rNewName = CreateUniqueObjectName();
uno::Reference < embed::XEmbeddedObject > xObj;
try
{
uno::Reference < embed::XEmbeddedObjectCreator > xFactory = embed::EmbeddedObjectCreator::create( ::comphelper::getProcessComponentContext() );
uno::Sequence< beans::PropertyValue > aObjDescr( 1 );
aObjDescr[0].Name = "Parent";
aObjDescr[0].Value <<= pImpl->m_xModel.get();
xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitFromMediaDescriptor(
pImpl->mxStorage, rNewName, aMedium, aObjDescr ), uno::UNO_QUERY );
uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
OSL_ENSURE( !xObj.is() || xObj->getCurrentState() != embed::EmbedStates::LOADED,
"A freshly create object should be running always!\n" );
// possible optimization: store later!
if ( xPersist.is())
xPersist->storeOwn();
AddEmbeddedObject( xObj, rNewName );
}
catch (const uno::Exception&)
{
}
return xObj;
}
uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::InsertEmbeddedLink( const ::com::sun::star::uno::Sequence < ::com::sun::star::beans::PropertyValue >& aMedium, OUString& rNewName )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedLink" );
if ( rNewName.isEmpty() )
rNewName = CreateUniqueObjectName();
uno::Reference < embed::XEmbeddedObject > xObj;
try
{
uno::Reference < embed::XEmbeddedObjectCreator > xFactory = embed::EmbeddedObjectCreator::create(::comphelper::getProcessComponentContext());
uno::Sequence< beans::PropertyValue > aObjDescr( 1 );
aObjDescr[0].Name = "Parent";
aObjDescr[0].Value <<= pImpl->m_xModel.get();
xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceLink(
pImpl->mxStorage, rNewName, aMedium, aObjDescr ), uno::UNO_QUERY );
uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
OSL_ENSURE( !xObj.is() || xObj->getCurrentState() != embed::EmbedStates::LOADED,
"A freshly create object should be running always!\n" );
// possible optimization: store later!
if ( xPersist.is())
xPersist->storeOwn();
AddEmbeddedObject( xObj, rNewName );
}
catch (uno::Exception const& e)
{
SAL_WARN("comphelper", "EmbeddedObjectContainer::InsertEmbeddedLink: "
"exception caught: " << e.Message);
}
return xObj;
}
sal_Bool EmbeddedObjectContainer::TryToCopyGraphReplacement( EmbeddedObjectContainer& rSrc,
const OUString& aOrigName,
const OUString& aTargetName )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::TryToCopyGraphReplacement" );
sal_Bool bResult = sal_False;
if ( ( &rSrc != this || !aOrigName.equals( aTargetName ) ) && !aOrigName.isEmpty() && !aTargetName.isEmpty() )
{
OUString aMediaType;
uno::Reference < io::XInputStream > xGrStream = rSrc.GetGraphicStream( aOrigName, &aMediaType );
if ( xGrStream.is() )
bResult = InsertGraphicStream( xGrStream, aTargetName, aMediaType );
}
return bResult;
}
uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::CopyAndGetEmbeddedObject( EmbeddedObjectContainer& rSrc, const uno::Reference < embed::XEmbeddedObject >& xObj, OUString& rName )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::CopyAndGetEmbeddedObject" );
uno::Reference< embed::XEmbeddedObject > xResult;
// TODO/LATER: For now only objects that implement XEmbedPersist have a replacement image, it might change in future
// do an incompatible change so that object name is provided in all the move and copy methods
OUString aOrigName;
try
{
uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY_THROW );
aOrigName = xPersist->getEntryName();
}
catch (const uno::Exception&)
{
}
if ( rName.isEmpty() )
rName = CreateUniqueObjectName();
// objects without persistance are not really stored by the method
if ( xObj.is() && StoreEmbeddedObject( xObj, rName, sal_True ) )
{
xResult = Get_Impl( rName, xObj);
if ( !xResult.is() )
{
// this is a case when object has no real persistence
// in such cases a new object should be explicitly created and initialized with the data of the old one
try
{
uno::Reference< embed::XLinkageSupport > xOrigLinkage( xObj, uno::UNO_QUERY );
if ( xOrigLinkage.is() && xOrigLinkage->isLink() )
{
// this is a OOo link, it has no persistence
OUString aURL = xOrigLinkage->getLinkURL();
if ( aURL.isEmpty() )
throw uno::RuntimeException();
// create new linked object from the URL the link is based on
uno::Reference < embed::XEmbeddedObjectCreator > xCreator =
embed::EmbeddedObjectCreator::create( ::comphelper::getProcessComponentContext() );
uno::Sequence< beans::PropertyValue > aMediaDescr( 1 );
aMediaDescr[0].Name = "URL";
aMediaDescr[0].Value <<= aURL;
uno::Sequence< beans::PropertyValue > aObjDescr( 1 );
aObjDescr[0].Name = "Parent";
aObjDescr[0].Value <<= pImpl->m_xModel.get();
xResult = uno::Reference < embed::XEmbeddedObject >(
xCreator->createInstanceLink(
pImpl->mxStorage,
rName,
aMediaDescr,
aObjDescr ),
uno::UNO_QUERY_THROW );
}
else
{
// the component is required for copying of this object
if ( xObj->getCurrentState() == embed::EmbedStates::LOADED )
xObj->changeState( embed::EmbedStates::RUNNING );
// this must be an object based on properties, otherwise we can not copy it currently
uno::Reference< beans::XPropertySet > xOrigProps( xObj->getComponent(), uno::UNO_QUERY_THROW );
// use object class ID to create a new one and tranfer all the properties
uno::Reference < embed::XEmbeddedObjectCreator > xCreator =
embed::EmbeddedObjectCreator::create( ::comphelper::getProcessComponentContext() );
uno::Sequence< beans::PropertyValue > aObjDescr( 1 );
aObjDescr[0].Name = "Parent";
aObjDescr[0].Value <<= pImpl->m_xModel.get();
xResult = uno::Reference < embed::XEmbeddedObject >(
xCreator->createInstanceInitNew(
xObj->getClassID(),
xObj->getClassName(),
pImpl->mxStorage,
rName,
aObjDescr ),
uno::UNO_QUERY_THROW );
if ( xResult->getCurrentState() == embed::EmbedStates::LOADED )
xResult->changeState( embed::EmbedStates::RUNNING );
uno::Reference< beans::XPropertySet > xTargetProps( xResult->getComponent(), uno::UNO_QUERY_THROW );
// copy all the properties from xOrigProps to xTargetProps
uno::Reference< beans::XPropertySetInfo > xOrigInfo = xOrigProps->getPropertySetInfo();
if ( !xOrigInfo.is() )
throw uno::RuntimeException();
uno::Sequence< beans::Property > aPropertiesList = xOrigInfo->getProperties();
for ( sal_Int32 nInd = 0; nInd < aPropertiesList.getLength(); nInd++ )
{
try
{
xTargetProps->setPropertyValue(
aPropertiesList[nInd].Name,
xOrigProps->getPropertyValue( aPropertiesList[nInd].Name ) );
}
catch (const beans::PropertyVetoException&)
{
// impossibility to copy readonly property is not treated as an error for now
// but the assertion is helpful to detect such scenarios and review them
OSL_FAIL( "Could not copy readonly property!\n" );
}
}
}
if ( xResult.is() )
AddEmbeddedObject( xResult, rName );
}
catch (const uno::Exception&)
{
if ( xResult.is() )
{
try
{
xResult->close( sal_True );
}
catch (const uno::Exception&)
{
}
xResult = uno::Reference< embed::XEmbeddedObject >();
}
}
}
}
OSL_ENSURE( xResult.is(), "Can not copy embedded object that has no persistance!\n" );
if ( xResult.is() )
{
// the object is successfully copied, try to copy graphical replacement
if ( !aOrigName.isEmpty() )
TryToCopyGraphReplacement( rSrc, aOrigName, rName );
// the object might need the size to be set
try
{
if ( xResult->getStatus( embed::Aspects::MSOLE_CONTENT ) & embed::EmbedMisc::EMBED_NEEDSSIZEONLOAD )
xResult->setVisualAreaSize( embed::Aspects::MSOLE_CONTENT,
xObj->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT ) );
}
catch (const uno::Exception&)
{
}
}
return xResult;
}
sal_Bool EmbeddedObjectContainer::MoveEmbeddedObject( EmbeddedObjectContainer& rSrc, const uno::Reference < embed::XEmbeddedObject >& xObj, OUString& rName )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::MoveEmbeddedObject( Object )" );
// get the object name before(!) it is assigned to a new storage
uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
OUString aName;
if ( xPersist.is() )
aName = xPersist->getEntryName();
// now move the object to the new container; the returned name is the new persist name in this container
sal_Bool bRet;
try
{
bRet = InsertEmbeddedObject( xObj, rName );
if ( bRet )
TryToCopyGraphReplacement( rSrc, aName, rName );
}
catch (const uno::Exception&)
{
OSL_FAIL( "Failed to insert embedded object into storage!" );
bRet = sal_False;
}
if ( bRet )
{
// now remove the object from the former container
bRet = sal_False;
EmbeddedObjectContainerNameMap::iterator aIt = rSrc.pImpl->maObjectContainer.begin();
while ( aIt != rSrc.pImpl->maObjectContainer.end() )
{
if ( (*aIt).second == xObj )
{
rSrc.pImpl->maObjectContainer.erase( aIt );
bRet = sal_True;
break;
}
++aIt;
}
OSL_ENSURE( bRet, "Object not found for removal!" );
if ( xPersist.is() )
{
// now it's time to remove the storage from the container storage
try
{
if ( xPersist.is() )
rSrc.pImpl->mxStorage->removeElement( aName );
}
catch (const uno::Exception&)
{
OSL_FAIL( "Failed to remove object from storage!" );
bRet = sal_False;
}
}
// rSrc.RemoveGraphicStream( aName );
}
return bRet;
}
sal_Bool EmbeddedObjectContainer::RemoveEmbeddedObject( const OUString& rName, sal_Bool bClose )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::RemoveEmbeddedObject( Name )" );
uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( rName );
if ( xObj.is() )
return RemoveEmbeddedObject( xObj, bClose );
else
return sal_False;
}
sal_Bool EmbeddedObjectContainer::MoveEmbeddedObject( const OUString& rName, EmbeddedObjectContainer& rCnt )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::MoveEmbeddedObject( Name )" );
// find object entry
EmbeddedObjectContainerNameMap::iterator aIt2 = rCnt.pImpl->maObjectContainer.find( rName );
OSL_ENSURE( aIt2 == rCnt.pImpl->maObjectContainer.end(), "Object does already exist in target container!" );
if ( aIt2 != rCnt.pImpl->maObjectContainer.end() )
return sal_False;
uno::Reference < embed::XEmbeddedObject > xObj;
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName );
if ( aIt != pImpl->maObjectContainer.end() )
{
xObj = (*aIt).second;
try
{
if ( xObj.is() )
{
// move object
OUString aName( rName );
rCnt.InsertEmbeddedObject( xObj, aName );
pImpl->maObjectContainer.erase( aIt );
uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
if ( xPersist.is() )
pImpl->mxStorage->removeElement( rName );
}
else
{
// copy storages; object *must* have persistence!
uno::Reference < embed::XStorage > xOld = pImpl->mxStorage->openStorageElement( rName, embed::ElementModes::READ );
uno::Reference < embed::XStorage > xNew = rCnt.pImpl->mxStorage->openStorageElement( rName, embed::ElementModes::READWRITE );
xOld->copyToStorage( xNew );
}
rCnt.TryToCopyGraphReplacement( *this, rName, rName );
// RemoveGraphicStream( rName );
return sal_True;
}
catch (const uno::Exception&)
{
OSL_FAIL("Could not move object!");
return sal_False;
}
}
else
OSL_FAIL("Unknown object!");
return sal_False;
}
sal_Bool EmbeddedObjectContainer::RemoveEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj, sal_Bool bClose )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::RemoveEmbeddedObject( Object )" );
uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
OUString aName;
if ( xPersist.is() )
aName = xPersist->getEntryName();
#if OSL_DEBUG_LEVEL > 1
uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY );
uno::Reference < embed::XLinkageSupport > xLink( xPersist, uno::UNO_QUERY );
sal_Bool bIsNotEmbedded = !xPersist.is() || ( xLink.is() && xLink->isLink() );
// if the object has a persistance and the object is not a link than it must have persistence entry in the storage
OSL_ENSURE( bIsNotEmbedded || xAccess->hasByName(aName), "Removing element not present in storage!" );
#endif
// try to close it if permitted
if ( bClose )
{
uno::Reference < ::util::XCloseable > xClose( xObj, uno::UNO_QUERY );
try
{
xClose->close( sal_True );
}
catch (const util::CloseVetoException&)
{
bClose = sal_False;
}
}
if ( !bClose )
{
// somebody still needs the object, so we must assign a temporary persistence
try
{
if ( xPersist.is() )
{
/*
//TODO/LATER: needs storage handling! Why not letting the object do it?!
if ( !pImpl->mxTempStorage.is() )
pImpl->mxTempStorage = ::comphelper::OStorageHelper::GetTemporaryStorage();
uno::Sequence < beans::PropertyValue > aSeq;
OUString aTmpPersistName = "Object ";
aTmpPersistName += OUString::valueOf( (sal_Int32) pImpl->maTempObjectContainer.size() );
xPersist->storeAsEntry( pImpl->mxTempStorage, aTmpPersistName, aSeq, aSeq );
xPersist->saveCompleted( sal_True );
pImpl->maTempObjectContainer[ aTmpPersistName ] = uno::Reference < embed::XEmbeddedObject >();
*/
if ( !pImpl->mpTempObjectContainer )
{
pImpl->mpTempObjectContainer = new EmbeddedObjectContainer();
try
{
// TODO/LATER: in future probably the temporary container will have two storages ( of two formats )
// the media type will be provided with object insertion
OUString aOrigStorMediaType;
uno::Reference< beans::XPropertySet > xStorProps( pImpl->mxStorage, uno::UNO_QUERY_THROW );
static const OUString s_sMediaType("MediaType");
xStorProps->getPropertyValue( s_sMediaType ) >>= aOrigStorMediaType;
OSL_ENSURE( !aOrigStorMediaType.isEmpty(), "No valuable media type in the storage!\n" );
uno::Reference< beans::XPropertySet > xTargetStorProps(
pImpl->mpTempObjectContainer->pImpl->mxStorage,
uno::UNO_QUERY_THROW );
xTargetStorProps->setPropertyValue( s_sMediaType,uno::makeAny( aOrigStorMediaType ) );
}
catch (const uno::Exception&)
{
OSL_FAIL( "Can not set the new media type to a storage!\n" );
}
}
OUString aTempName, aMediaType;
pImpl->mpTempObjectContainer->InsertEmbeddedObject( xObj, aTempName );
uno::Reference < io::XInputStream > xStream = GetGraphicStream( xObj, &aMediaType );
if ( xStream.is() )
pImpl->mpTempObjectContainer->InsertGraphicStream( xStream, aTempName, aMediaType );
// object is stored, so at least it can be set to loaded state
xObj->changeState( embed::EmbedStates::LOADED );
}
else
// objects without persistence need to stay in running state if they shall not be closed
xObj->changeState( embed::EmbedStates::RUNNING );
}
catch (const uno::Exception&)
{
return sal_False;
}
}
sal_Bool bFound = sal_False;
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
while ( aIt != pImpl->maObjectContainer.end() )
{
if ( (*aIt).second == xObj )
{
pImpl->maObjectContainer.erase( aIt );
bFound = sal_True;
uno::Reference < container::XChild > xChild( xObj, uno::UNO_QUERY );
if ( xChild.is() )
xChild->setParent( uno::Reference < uno::XInterface >() );
break;
}
++aIt;
}
OSL_ENSURE( bFound, "Object not found for removal!" );
(void)bFound;
if ( xPersist.is() )
{
// remove replacement image (if there is one)
RemoveGraphicStream( aName );
// now it's time to remove the storage from the container storage
try
{
#if OSL_DEBUG_LEVEL > 1
// if the object has a persistance and the object is not a link than it must have persistence entry in storage
OSL_ENSURE( bIsNotEmbedded || pImpl->mxStorage->hasByName( aName ), "The object has no persistence entry in the storage!" );
#endif
if ( xPersist.is() && pImpl->mxStorage->hasByName( aName ) )
pImpl->mxStorage->removeElement( aName );
}
catch (const uno::Exception&)
{
OSL_FAIL( "Failed to remove object from storage!" );
return sal_False;
}
}
return sal_True;
}
sal_Bool EmbeddedObjectContainer::CloseEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::CloseEmbeddedObject" );
// disconnect the object from the container and close it if possible
sal_Bool bFound = sal_False;
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
while ( aIt != pImpl->maObjectContainer.end() )
{
if ( (*aIt).second == xObj )
{
pImpl->maObjectContainer.erase( aIt );
bFound = sal_True;
break;
}
++aIt;
}
if ( bFound )
{
uno::Reference < ::util::XCloseable > xClose( xObj, uno::UNO_QUERY );
try
{
xClose->close( sal_True );
}
catch (const uno::Exception&)
{
// it is no problem if the object is already closed
// TODO/LATER: what if the object can not be closed?
}
}
return bFound;
}
uno::Reference < io::XInputStream > EmbeddedObjectContainer::GetGraphicStream( const OUString& aName, OUString* pMediaType )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::GetGraphicStream( Name )" );
uno::Reference < io::XInputStream > xStream;
OSL_ENSURE( !aName.isEmpty(), "Retrieving graphic for unknown object!" );
if ( !aName.isEmpty() )
{
try
{
uno::Reference < embed::XStorage > xReplacements = pImpl->GetReplacements();
uno::Reference < io::XStream > xGraphicStream = xReplacements->openStreamElement( aName, embed::ElementModes::READ );
xStream = xGraphicStream->getInputStream();
if ( pMediaType )
{
uno::Reference < beans::XPropertySet > xSet( xStream, uno::UNO_QUERY );
if ( xSet.is() )
{
uno::Any aAny = xSet->getPropertyValue( OUString("MediaType") );
aAny >>= *pMediaType;
}
}
}
catch (const uno::Exception&)
{
}
}
return xStream;
}
uno::Reference < io::XInputStream > EmbeddedObjectContainer::GetGraphicStream( const ::com::sun::star::uno::Reference < ::com::sun::star::embed::XEmbeddedObject >& xObj, OUString* pMediaType )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::GetGraphicStream( Object )" );
// get the object name
OUString aName;
EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
while ( aIt != pImpl->maObjectContainer.end() )
{
if ( (*aIt).second == xObj )
{
aName = (*aIt).first;
break;
}
++aIt;
}
// try to load it from the container storage
return GetGraphicStream( aName, pMediaType );
}
sal_Bool EmbeddedObjectContainer::InsertGraphicStream( const com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& rStream, const OUString& rObjectName, const OUString& rMediaType )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertGraphicStream" );
try
{
uno::Reference < embed::XStorage > xReplacements = pImpl->GetReplacements();
// store it into the subfolder
uno::Reference < io::XOutputStream > xOutStream;
uno::Reference < io::XStream > xGraphicStream = xReplacements->openStreamElement( rObjectName,
embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
xOutStream = xGraphicStream->getOutputStream();
::comphelper::OStorageHelper::CopyInputToOutput( rStream, xOutStream );
xOutStream->flush();
uno::Reference< beans::XPropertySet > xPropSet( xGraphicStream, uno::UNO_QUERY );
if ( !xPropSet.is() )
throw uno::RuntimeException();
xPropSet->setPropertyValue( OUString("UseCommonStoragePasswordEncryption"),
uno::makeAny( (sal_Bool)sal_True ) );
uno::Any aAny;
aAny <<= rMediaType;
xPropSet->setPropertyValue( OUString("MediaType"), aAny );
xPropSet->setPropertyValue( OUString("Compressed"),
uno::makeAny( (sal_Bool)sal_True ) );
}
catch (const uno::Exception&)
{
return sal_False;
}
return sal_True;
}
sal_Bool EmbeddedObjectContainer::InsertGraphicStreamDirectly( const com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& rStream, const OUString& rObjectName, const OUString& rMediaType )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertGraphicStreamDirectly" );
try
{
uno::Reference < embed::XStorage > xReplacement = pImpl->GetReplacements();
uno::Reference < embed::XOptimizedStorage > xOptRepl( xReplacement, uno::UNO_QUERY_THROW );
// store it into the subfolder
uno::Sequence< beans::PropertyValue > aProps( 3 );
aProps[0].Name = "MediaType";
aProps[0].Value <<= rMediaType;
aProps[1].Name = "UseCommonStoragePasswordEncryption";
aProps[1].Value <<= (sal_Bool)sal_True;
aProps[2].Name = "Compressed";
aProps[2].Value <<= (sal_Bool)sal_True;
if ( xReplacement->hasByName( rObjectName ) )
xReplacement->removeElement( rObjectName );
xOptRepl->insertStreamElementDirect( rObjectName, rStream, aProps );
}
catch (const uno::Exception&)
{
return sal_False;
}
return sal_True;
}
sal_Bool EmbeddedObjectContainer::RemoveGraphicStream( const OUString& rObjectName )
{
RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::RemoveGraphicStream" );
try
{
uno::Reference < embed::XStorage > xReplacements = pImpl->GetReplacements();
xReplacements->removeElement( rObjectName );
}
catch (const uno::Exception&)
{
return sal_False;
}
return sal_True;
}
namespace {
void InsertStreamIntoPicturesStorage_Impl( const uno::Reference< embed::XStorage >& xDocStor,
const uno::Reference< io::XInputStream >& xInStream,
const OUString& aStreamName )
{
OSL_ENSURE( !aStreamName.isEmpty() && xInStream.is() && xDocStor.is(), "Misuse of the method!\n" );
try
{
uno::Reference< embed::XStorage > xPictures = xDocStor->openStorageElement(
OUString( "Pictures" ),
embed::ElementModes::READWRITE );
uno::Reference< io::XStream > xObjReplStr = xPictures->openStreamElement(
aStreamName,
embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
uno::Reference< io::XOutputStream > xOutStream(
xObjReplStr->getInputStream(), uno::UNO_QUERY_THROW );
::comphelper::OStorageHelper::CopyInputToOutput( xInStream, xOutStream );
xOutStream->closeOutput();
uno::Reference< embed::XTransactedObject > xTransact( xPictures, uno::UNO_QUERY );
if ( xTransact.is() )
xTransact->commit();
}
catch (const uno::Exception&)
{
OSL_FAIL( "The pictures storage is not available!\n" );
}
}
}
// -----------------------------------------------------------------------------
sal_Bool EmbeddedObjectContainer::StoreAsChildren(sal_Bool _bOasisFormat,sal_Bool _bCreateEmbedded,const uno::Reference < embed::XStorage >& _xStorage)
{
sal_Bool bResult = sal_False;
try
{
comphelper::EmbeddedObjectContainer aCnt( _xStorage );
const uno::Sequence < OUString > aNames = GetObjectNames();
const OUString* pIter = aNames.getConstArray();
const OUString* pEnd = pIter + aNames.getLength();
for(;pIter != pEnd;++pIter)
{
uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( *pIter );
OSL_ENSURE( xObj.is(), "An empty entry in the embedded objects list!\n" );
if ( xObj.is() )
{
sal_Bool bSwitchBackToLoaded = sal_False;
uno::Reference< embed::XLinkageSupport > xLink( xObj, uno::UNO_QUERY );
uno::Reference < io::XInputStream > xStream;
OUString aMediaType;
sal_Int32 nCurState = xObj->getCurrentState();
if ( nCurState == embed::EmbedStates::LOADED || nCurState == embed::EmbedStates::RUNNING )
{
// means that the object is not active
// copy replacement image from old to new container
xStream = GetGraphicStream( xObj, &aMediaType );
}
if ( !xStream.is() )
{
// the image must be regenerated
// TODO/LATER: another aspect could be used
if ( xObj->getCurrentState() == embed::EmbedStates::LOADED )
bSwitchBackToLoaded = sal_True;
xStream = GetGraphicReplacementStream(
embed::Aspects::MSOLE_CONTENT,
xObj,
&aMediaType );
}
if ( _bOasisFormat || (xLink.is() && xLink->isLink()) )
{
if ( xStream.is() )
{
if ( _bOasisFormat )
{
// if it is an embedded object or the optimized inserting fails the normal inserting should be done
if ( _bCreateEmbedded
|| !aCnt.InsertGraphicStreamDirectly( xStream, *pIter, aMediaType ) )
aCnt.InsertGraphicStream( xStream, *pIter, aMediaType );
}
else
{
// it is a linked object exported into SO7 format
InsertStreamIntoPicturesStorage_Impl( _xStorage, xStream, *pIter );
}
}
}
uno::Reference< embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
if ( xPersist.is() )
{
uno::Sequence< beans::PropertyValue > aArgs( _bOasisFormat ? 2 : 3 );
aArgs[0].Name = "StoreVisualReplacement";
aArgs[0].Value <<= (sal_Bool)( !_bOasisFormat );
// if it is an embedded object or the optimized inserting fails the normal inserting should be done
aArgs[1].Name = "CanTryOptimization";
aArgs[1].Value <<= !_bCreateEmbedded;
if ( !_bOasisFormat )
{
// if object has no cached replacement it will use this one
aArgs[2].Name = "VisualReplacement";
aArgs[2].Value <<= xStream;
}
try
{
xPersist->storeAsEntry( _xStorage, xPersist->getEntryName(), uno::Sequence< beans::PropertyValue >(), aArgs );
}
catch (const embed::WrongStateException&)
{
SAL_WARN("comphelper", "failed to store '" << *pIter << "'");
}
}
if ( bSwitchBackToLoaded )
// switch back to loaded state; that way we have a minimum cache confusion
xObj->changeState( embed::EmbedStates::LOADED );
}
}
bResult = aCnt.CommitImageSubStorage();
}
catch (const uno::Exception& e)
{
// TODO/LATER: error handling
bResult = sal_False;
SAL_WARN("comphelper", "failed. Message: " << e.Message);
}
// the old SO6 format does not store graphical replacements
if ( !_bOasisFormat && bResult )
{
try
{
// the substorage still can not be locked by the embedded object conteiner
OUString aObjReplElement( "ObjectReplacements" );
if ( _xStorage->hasByName( aObjReplElement ) && _xStorage->isStorageElement( aObjReplElement ) )
_xStorage->removeElement( aObjReplElement );
}
catch (const uno::Exception&)
{
// TODO/LATER: error handling;
bResult = sal_False;
}
}
return bResult;
}
// -----------------------------------------------------------------------------
sal_Bool EmbeddedObjectContainer::StoreChildren(sal_Bool _bOasisFormat,sal_Bool _bObjectsOnly)
{
sal_Bool bResult = sal_True;
const uno::Sequence < OUString > aNames = GetObjectNames();
const OUString* pIter = aNames.getConstArray();
const OUString* pEnd = pIter + aNames.getLength();
for(;pIter != pEnd;++pIter)
{
uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( *pIter );
OSL_ENSURE( xObj.is(), "An empty entry in the embedded objects list!\n" );
if ( xObj.is() )
{
sal_Int32 nCurState = xObj->getCurrentState();
if ( _bOasisFormat && nCurState != embed::EmbedStates::LOADED && nCurState != embed::EmbedStates::RUNNING )
{
// means that the object is active
// the image must be regenerated
OUString aMediaType;
// TODO/LATER: another aspect could be used
uno::Reference < io::XInputStream > xStream =
GetGraphicReplacementStream(
embed::Aspects::MSOLE_CONTENT,
xObj,
&aMediaType );
if ( xStream.is() )
{
if ( !InsertGraphicStreamDirectly( xStream, *pIter, aMediaType ) )
InsertGraphicStream( xStream, *pIter, aMediaType );
}
}
// TODO/LATER: currently the object by default does not cache replacement image
// that means that if somebody loads SO7 document and store its objects using
// this method the images might be lost.
// Currently this method is only used on storing to alien formats, that means
// that SO7 documents storing does not use it, and all other filters are
// based on OASIS format. But if it changes the method must be fixed. The fix
// must be done only on demand since it can affect performance.
uno::Reference< embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
if ( xPersist.is() )
{
try
{
//TODO/LATER: only storing if changed!
//xPersist->storeOwn(); //commented, i120168
// begin:all charts will be persited as xml format on disk when saving, which is time consuming.
// '_bObjectsOnly' mean we are storing to alien formats.
// 'isStorageElement' mean current object is NOT an MS OLE format. (may also include in future), i120168
if (_bObjectsOnly && (nCurState == embed::EmbedStates::LOADED || nCurState == embed::EmbedStates::RUNNING)
&& (pImpl->mxStorage->isStorageElement( *pIter ) ))
{
uno::Reference< util::XModifiable > xModifiable( xObj->getComponent(), uno::UNO_QUERY );
if ( xModifiable.is() && xModifiable->isModified())
{
xPersist->storeOwn();
}
else
{
//do nothing.embeded model is not modified, no need to persist.
}
}
else //the embeded object is in active status, always store back it.
{
xPersist->storeOwn();
}
//end i120168
}
catch (const uno::Exception&)
{
// TODO/LATER: error handling
bResult = sal_False;
break;
}
}
if ( !_bOasisFormat && !_bObjectsOnly )
{
// copy replacement images for linked objects
try
{
uno::Reference< embed::XLinkageSupport > xLink( xObj, uno::UNO_QUERY );
if ( xLink.is() && xLink->isLink() )
{
OUString aMediaType;
uno::Reference < io::XInputStream > xInStream = GetGraphicStream( xObj, &aMediaType );
if ( xInStream.is() )
InsertStreamIntoPicturesStorage_Impl( pImpl->mxStorage, xInStream, *pIter );
}
}
catch (const uno::Exception&)
{
}
}
}
}
if ( bResult && _bOasisFormat )
bResult = CommitImageSubStorage();
if ( bResult && !_bObjectsOnly )
{
try
{
ReleaseImageSubStorage();
OUString aObjReplElement( "ObjectReplacements" );
if ( !_bOasisFormat && pImpl->mxStorage->hasByName( aObjReplElement ) && pImpl->mxStorage->isStorageElement( aObjReplElement ) )
pImpl->mxStorage->removeElement( aObjReplElement );
}
catch (const uno::Exception&)
{
// TODO/LATER: error handling
bResult = sal_False;
}
}
return bResult;
}
// -----------------------------------------------------------------------------
uno::Reference< io::XInputStream > EmbeddedObjectContainer::GetGraphicReplacementStream(
sal_Int64 nViewAspect,
const uno::Reference< embed::XEmbeddedObject >& xObj,
OUString* pMediaType )
{
uno::Reference< io::XInputStream > xInStream;
if ( xObj.is() )
{
try
{
// retrieving of the visual representation can switch object to running state
embed::VisualRepresentation aRep = xObj->getPreferredVisualRepresentation( nViewAspect );
if ( pMediaType )
*pMediaType = aRep.Flavor.MimeType;
uno::Sequence < sal_Int8 > aSeq;
aRep.Data >>= aSeq;
xInStream = new ::comphelper::SequenceInputStream( aSeq );
}
catch (const uno::Exception&)
{
}
}
return xInStream;
}
// -----------------------------------------------------------------------------
sal_Bool EmbeddedObjectContainer::SetPersistentEntries(const uno::Reference< embed::XStorage >& _xStorage,bool _bClearModifedFlag)
{
sal_Bool bError = sal_False;
const uno::Sequence < OUString > aNames = GetObjectNames();
const OUString* pIter = aNames.getConstArray();
const OUString* pEnd = pIter + aNames.getLength();
for(;pIter != pEnd;++pIter)
{
uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( *pIter );
OSL_ENSURE( xObj.is(), "An empty entry in the embedded objects list!\n" );
if ( xObj.is() )
{
uno::Reference< embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
if ( xPersist.is() )
{
try
{
xPersist->setPersistentEntry( _xStorage,
*pIter,
embed::EntryInitModes::NO_INIT,
uno::Sequence< beans::PropertyValue >(),
uno::Sequence< beans::PropertyValue >() );
}
catch (const uno::Exception&)
{
// TODO/LATER: error handling
bError = sal_True;
break;
}
}
if ( _bClearModifedFlag )
{
// if this method is used as part of SaveCompleted the object must stay unmodified after execution
try
{
uno::Reference< util::XModifiable > xModif( xObj->getComponent(), uno::UNO_QUERY_THROW );
if ( xModif->isModified() )
xModif->setModified( sal_False );
}
catch (const uno::Exception&)
{
}
}
}
}
return bError;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|