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
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
|
/* -*- 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 <limits.h>
#include <mutex>
#include <string_view>
#include <com/sun/star/uno/Any.h>
#include <sal/log.hxx>
#include <unotools/pathoptions.hxx>
#include <tools/urlobj.hxx>
#include <tools/debug.hxx>
#include <comphelper/diagnose_ex.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertyvalue.hxx>
#include <ucbhelper/content.hxx>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/XPropertySetInfo.hpp>
#include <com/sun/star/document/XTypeDetection.hpp>
#include <com/sun/star/document/DocumentProperties.hpp>
#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/frame/DocumentTemplates.hpp>
#include <com/sun/star/frame/XDocumentTemplates.hpp>
#include <com/sun/star/io/IOException.hpp>
#include <com/sun/star/io/XPersist.hpp>
#include <com/sun/star/lang/XLocalizable.hpp>
#include <com/sun/star/sdbc/XResultSet.hpp>
#include <com/sun/star/sdbc/XRow.hpp>
#include <com/sun/star/ucb/ContentCreationException.hpp>
#include <com/sun/star/ucb/NameClash.hpp>
#include <com/sun/star/ucb/TransferInfo.hpp>
#include <com/sun/star/ucb/XContent.hpp>
#include <com/sun/star/ucb/XContentAccess.hpp>
#include <com/sun/star/ucb/AnyCompareFactory.hpp>
#include <com/sun/star/ucb/NumberedSortingInfo.hpp>
#include "doctemplateslocal.hxx"
#include <sfxurlrelocator.hxx>
#include <sfx2/doctempl.hxx>
#include <sfx2/objsh.hxx>
#include <sfx2/sfxresid.hxx>
#include <sfx2/strings.hrc>
#include <strings.hxx>
#include <svtools/templatefoldercache.hxx>
#include <memory>
#include <utility>
#include <vector>
using namespace ::com::sun::star;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::frame;
using namespace ::com::sun::star::io;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::ucb;
using namespace ::com::sun::star::document;
using namespace ::rtl;
using namespace ::ucbhelper;
constexpr OUString TITLE = u"Title"_ustr;
constexpr OUString TARGET_URL = u"TargetURL"_ustr;
constexpr OUStringLiteral COMMAND_TRANSFER = u"transfer";
namespace {
class RegionData_Impl;
}
namespace DocTempl {
namespace {
class DocTempl_EntryData_Impl
{
RegionData_Impl* mpParent;
// the following member must be SfxObjectShellLock since it controls that SfxObjectShell lifetime by design
// and users of this class expect it to be so.
SfxObjectShellLock mxObjShell;
OUString maTitle;
OUString maOwnURL;
OUString maTargetURL;
public:
DocTempl_EntryData_Impl( RegionData_Impl* pParent,
const OUString& rTitle );
const OUString& GetTitle() const { return maTitle; }
const OUString& GetTargetURL();
const OUString& GetHierarchyURL();
void SetTitle( const OUString& rTitle ) { maTitle = rTitle; }
void SetTargetURL( const OUString& rURL ) { maTargetURL = rURL; }
void SetHierarchyURL( const OUString& rURL) { maOwnURL = rURL; }
int Compare( std::u16string_view rTitle ) const;
};
}
}
using namespace ::DocTempl;
namespace {
class RegionData_Impl
{
const SfxDocTemplate_Impl* mpParent;
std::vector<std::unique_ptr<DocTempl_EntryData_Impl>> maEntries;
OUString maTitle;
OUString maOwnURL;
private:
size_t GetEntryPos( std::u16string_view rTitle,
bool& rFound ) const;
public:
RegionData_Impl( const SfxDocTemplate_Impl* pParent,
OUString aTitle );
void SetHierarchyURL( const OUString& rURL) { maOwnURL = rURL; }
DocTempl_EntryData_Impl* GetEntry( size_t nIndex ) const;
DocTempl_EntryData_Impl* GetEntry( std::u16string_view rName ) const;
const OUString& GetTitle() const { return maTitle; }
const OUString& GetHierarchyURL();
size_t GetCount() const;
void SetTitle( const OUString& rTitle ) { maTitle = rTitle; }
void AddEntry( const OUString& rTitle,
const OUString& rTargetURL,
const size_t *pPos );
void DeleteEntry( size_t nIndex );
int Compare( RegionData_Impl const * pCompareWith ) const;
};
}
class SfxDocTemplate_Impl : public SvRefBase
{
uno::Reference< XPersist > mxInfo;
uno::Reference< XDocumentTemplates > mxTemplates;
std::mutex maMutex;
OUString maRootURL;
OUString maStandardGroup;
std::vector<std::unique_ptr<RegionData_Impl>> maRegions;
bool mbConstructed;
uno::Reference< XAnyCompareFactory > m_rCompareFactory;
// the following member is intended to prevent clearing of the global data when it is in use
// TODO/LATER: it still does not make the implementation complete thread-safe
sal_Int32 mnLockCounter;
private:
void Clear();
public:
SfxDocTemplate_Impl();
virtual ~SfxDocTemplate_Impl() override;
void IncrementLock();
void DecrementLock();
bool Construct( );
void CreateFromHierarchy( std::unique_lock<std::mutex>& rGuard, Content &rTemplRoot );
void ReInitFromComponent();
void AddRegion( std::unique_lock<std::mutex>& rGuard,
const OUString& rTitle,
Content& rContent );
void Rescan();
void DeleteRegion( size_t nIndex );
size_t GetRegionCount() const
{ return maRegions.size(); }
RegionData_Impl* GetRegion( std::u16string_view rName ) const;
RegionData_Impl* GetRegion( size_t nIndex ) const;
bool GetTitleFromURL( const OUString& rURL, OUString& aTitle );
bool InsertRegion( std::unique_ptr<RegionData_Impl> pData, size_t nPos );
const OUString& GetRootURL() const { return maRootURL; }
const uno::Reference< XDocumentTemplates >& getDocTemplates() const { return mxTemplates; }
};
namespace {
class DocTemplLocker_Impl
{
SfxDocTemplate_Impl& m_aDocTempl;
public:
explicit DocTemplLocker_Impl( SfxDocTemplate_Impl& aDocTempl )
: m_aDocTempl( aDocTempl )
{
m_aDocTempl.IncrementLock();
}
~DocTemplLocker_Impl()
{
m_aDocTempl.DecrementLock();
}
};
}
static SfxDocTemplate_Impl *gpTemplateData = nullptr;
static bool getTextProperty_Impl( Content& rContent,
const OUString& rPropName,
OUString& rPropValue );
OUString SfxDocumentTemplates::GetFullRegionName
(
sal_uInt16 nIdx // Region Index
) const
/* [Description]
Returns the logical name of a region and its path
[Return value] Reference to the Region name
*/
{
// First: find the RegionData for the index
DocTemplLocker_Impl aLocker( *pImp );
if ( pImp->Construct() )
{
RegionData_Impl *pData1 = pImp->GetRegion( nIdx );
if ( pData1 )
return pData1->GetTitle();
// --**-- here was some code which appended the path to the
// group if there was more than one with the same name.
// this should not happen anymore
}
return OUString();
}
OUString SfxDocumentTemplates::GetRegionName
(
sal_uInt16 nIdx // Region Index
) const
/* [Description]
Returns the logical name of a region
[Return value]
const String& Reference to the Region name
*/
{
DocTemplLocker_Impl aLocker( *pImp );
if ( pImp->Construct() )
{
RegionData_Impl *pData = pImp->GetRegion( nIdx );
if ( pData )
return pData->GetTitle();
}
return OUString();
}
sal_uInt16 SfxDocumentTemplates::GetRegionCount() const
/* [Description]
Returns the number of Regions
[Return value]
sal_uInt16 Number of Regions
*/
{
DocTemplLocker_Impl aLocker( *pImp );
if ( !pImp->Construct() )
return 0;
return pImp->GetRegionCount();
}
sal_uInt16 SfxDocumentTemplates::GetCount
(
sal_uInt16 nRegion /* Region index whose number is
to be determined */
) const
/* [Description]
Number of entries in Region
[Return value] Number of entries
*/
{
DocTemplLocker_Impl aLocker( *pImp );
if ( !pImp->Construct() )
return 0;
RegionData_Impl *pData = pImp->GetRegion( nRegion );
if ( !pData )
return 0;
return pData->GetCount();
}
OUString SfxDocumentTemplates::GetName
(
sal_uInt16 nRegion, // Region Index, in which the entry lies
sal_uInt16 nIdx // Index of the entry
) const
/* [Description]
Returns the logical name of an entry in Region
[Return value]
const String& Entry Name
*/
{
DocTemplLocker_Impl aLocker( *pImp );
if ( pImp->Construct() )
{
RegionData_Impl *pRegion = pImp->GetRegion( nRegion );
if ( pRegion )
{
DocTempl_EntryData_Impl *pEntry = pRegion->GetEntry( nIdx );
if ( pEntry )
return pEntry->GetTitle();
}
}
return OUString();
}
OUString SfxDocumentTemplates::GetPath
(
sal_uInt16 nRegion, // Region Index, in which the entry lies
sal_uInt16 nIdx // Index of the entry
) const
/* [Description]
Returns the file name with full path to the file assigned to an entry
[Return value]
String File name with full path
*/
{
DocTemplLocker_Impl aLocker( *pImp );
if ( !pImp->Construct() )
return OUString();
RegionData_Impl *pRegion = pImp->GetRegion( nRegion );
if ( pRegion )
{
DocTempl_EntryData_Impl *pEntry = pRegion->GetEntry( nIdx );
if ( pEntry )
return pEntry->GetTargetURL();
}
return OUString();
}
OUString SfxDocumentTemplates::GetTemplateTargetURLFromComponent( std::u16string_view aGroupName,
std::u16string_view aTitle )
{
DocTemplLocker_Impl aLocker( *pImp );
INetURLObject aTemplateObj( pImp->GetRootURL() );
aTemplateObj.insertName( aGroupName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
aTemplateObj.insertName( aTitle, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
Content aTemplate;
uno::Reference< XCommandEnvironment > aCmdEnv;
if ( Content::create( aTemplateObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), aCmdEnv, comphelper::getProcessComponentContext(), aTemplate ) )
{
OUString aResult;
getTextProperty_Impl( aTemplate, TARGET_URL, aResult );
return SvtPathOptions().SubstituteVariable( aResult );
}
return OUString();
}
/** Convert a template name to its localised pair if it exists.
@param rString
Name to be translated.
@return
The localised pair of rString or rString if the former does not exist.
*/
OUString SfxDocumentTemplates::ConvertResourceString(const OUString& rString)
{
static constexpr OUString aTemplateNames[] =
{
STR_TEMPLATE_NAME1_DEF,
STR_TEMPLATE_NAME2_DEF,
STR_TEMPLATE_NAME3_DEF,
STR_TEMPLATE_NAME4_DEF,
STR_TEMPLATE_NAME5_DEF,
STR_TEMPLATE_NAME6_DEF,
STR_TEMPLATE_NAME7_DEF,
STR_TEMPLATE_NAME8_DEF,
STR_TEMPLATE_NAME9_DEF,
STR_TEMPLATE_NAME10_DEF,
STR_TEMPLATE_NAME11_DEF,
STR_TEMPLATE_NAME12_DEF,
STR_TEMPLATE_NAME13_DEF,
STR_TEMPLATE_NAME14_DEF,
STR_TEMPLATE_NAME15_DEF,
STR_TEMPLATE_NAME16_DEF,
STR_TEMPLATE_NAME17_DEF,
STR_TEMPLATE_NAME18_DEF,
STR_TEMPLATE_NAME19_DEF,
STR_TEMPLATE_NAME20_DEF,
STR_TEMPLATE_NAME21_DEF,
STR_TEMPLATE_NAME22_DEF,
STR_TEMPLATE_NAME23_DEF,
STR_TEMPLATE_NAME24_DEF,
STR_TEMPLATE_NAME25_DEF,
STR_TEMPLATE_NAME26_DEF,
STR_TEMPLATE_NAME27_DEF,
STR_TEMPLATE_NAME28_DEF,
STR_TEMPLATE_NAME29_DEF,
STR_TEMPLATE_NAME30_DEF,
STR_TEMPLATE_NAME31_DEF,
STR_TEMPLATE_NAME32_DEF,
STR_TEMPLATE_NAME33_DEF,
STR_TEMPLATE_NAME34_DEF
};
TranslateId STR_TEMPLATE_NAME[] =
{
STR_TEMPLATE_NAME1,
STR_TEMPLATE_NAME2,
STR_TEMPLATE_NAME3,
STR_TEMPLATE_NAME4,
STR_TEMPLATE_NAME5,
STR_TEMPLATE_NAME6,
STR_TEMPLATE_NAME7,
STR_TEMPLATE_NAME8,
STR_TEMPLATE_NAME9,
STR_TEMPLATE_NAME10,
STR_TEMPLATE_NAME11,
STR_TEMPLATE_NAME12,
STR_TEMPLATE_NAME13,
STR_TEMPLATE_NAME14,
STR_TEMPLATE_NAME15,
STR_TEMPLATE_NAME16,
STR_TEMPLATE_NAME17,
STR_TEMPLATE_NAME18,
STR_TEMPLATE_NAME19,
STR_TEMPLATE_NAME20,
STR_TEMPLATE_NAME21,
STR_TEMPLATE_NAME22,
STR_TEMPLATE_NAME23,
STR_TEMPLATE_NAME24,
STR_TEMPLATE_NAME25,
STR_TEMPLATE_NAME26,
STR_TEMPLATE_NAME27,
STR_TEMPLATE_NAME28,
STR_TEMPLATE_NAME29,
STR_TEMPLATE_NAME30,
STR_TEMPLATE_NAME31,
STR_TEMPLATE_NAME32,
STR_TEMPLATE_NAME33,
STR_TEMPLATE_NAME34
};
static_assert(SAL_N_ELEMENTS(aTemplateNames) == SAL_N_ELEMENTS(STR_TEMPLATE_NAME));
for (size_t i = 0; i < SAL_N_ELEMENTS(STR_TEMPLATE_NAME); ++i)
{
if (rString == aTemplateNames[i])
return SfxResId(STR_TEMPLATE_NAME[i]);
}
return rString;
}
bool SfxDocumentTemplates::CopyOrMove
(
sal_uInt16 nTargetRegion, // Target Region Index
sal_uInt16 nTargetIdx, // Target position Index
sal_uInt16 nSourceRegion, // Source Region Index
sal_uInt16 nSourceIdx, /* Index to be copied / to moved template */
bool bMove // Copy / Move
)
/* [Description]
Copy or move a document template
[Return value]
sal_Bool sal_True, Action could be performed
sal_False, Action could not be performed
[Cross-references]
<SfxDocumentTemplates::Move(sal_uInt16,sal_uInt16,sal_uInt16,sal_uInt16)>
<SfxDocumentTemplates::Copy(sal_uInt16,sal_uInt16,sal_uInt16,sal_uInt16)>
*/
{
/* to perform a copy or move, we need to send a transfer command to
the destination folder with the URL of the source as parameter.
( If the destination content doesn't support the transfer command,
we could try a copy ( and delete ) instead. )
We need two transfers ( one for the real template and one for its
representation in the hierarchy )
...
*/
DocTemplLocker_Impl aLocker( *pImp );
if ( !pImp->Construct() )
return false;
// Don't copy or move any folders
if( nSourceIdx == USHRT_MAX )
return false ;
if ( nSourceRegion == nTargetRegion )
{
SAL_WARN( "sfx.doc", "Don't know, what to do!" );
return false;
}
RegionData_Impl *pSourceRgn = pImp->GetRegion( nSourceRegion );
if ( !pSourceRgn )
return false;
DocTempl_EntryData_Impl *pSource = pSourceRgn->GetEntry( nSourceIdx );
if ( !pSource )
return false;
RegionData_Impl *pTargetRgn = pImp->GetRegion( nTargetRegion );
if ( !pTargetRgn )
return false;
const OUString aTitle = pSource->GetTitle();
uno::Reference< XDocumentTemplates > xTemplates = pImp->getDocTemplates();
if ( xTemplates->addTemplate( pTargetRgn->GetTitle(),
aTitle,
pSource->GetTargetURL() ) )
{
const OUString aNewTargetURL = GetTemplateTargetURLFromComponent( pTargetRgn->GetTitle(), aTitle );
if ( aNewTargetURL.isEmpty() )
return false;
if ( bMove )
{
// --**-- delete the original file
bool bDeleted = xTemplates->removeTemplate( pSourceRgn->GetTitle(),
pSource->GetTitle() );
if ( bDeleted )
pSourceRgn->DeleteEntry( nSourceIdx );
else
{
if ( xTemplates->removeTemplate( pTargetRgn->GetTitle(), aTitle ) )
return false; // will trigger retry with copy instead of move
// if it is not possible to remove just created template ( must be possible! )
// it is better to report success here, since at least the copy has succeeded
// TODO/LATER: solve it more gracefully in future
}
}
// todo: fix SfxDocumentTemplates to handle size_t instead of sal_uInt16
size_t temp_nTargetIdx = nTargetIdx;
pTargetRgn->AddEntry( aTitle, aNewTargetURL, &temp_nTargetIdx );
return true;
}
// --**-- if the current file is opened,
// it must be re-opened afterwards.
return false;
}
bool SfxDocumentTemplates::Move
(
sal_uInt16 nTargetRegion, // Target Region Index
sal_uInt16 nTargetIdx, // Target position Index
sal_uInt16 nSourceRegion, // Source Region Index
sal_uInt16 nSourceIdx /* Index to be copied / to moved template */
)
/* [Description]
Moving a template
[Return value]
sal_Bool sal_True, Action could be performed
sal_False, Action could not be performed
[Cross-references]
<SfxDocumentTemplates::CopyOrMove(sal_uInt16,sal_uInt16,sal_uInt16,sal_uInt16,sal_Bool)>
*/
{
DocTemplLocker_Impl aLocker( *pImp );
return CopyOrMove( nTargetRegion, nTargetIdx,
nSourceRegion, nSourceIdx, true );
}
bool SfxDocumentTemplates::Copy
(
sal_uInt16 nTargetRegion, // Target Region Index
sal_uInt16 nTargetIdx, // Target position Index
sal_uInt16 nSourceRegion, // Source Region Index
sal_uInt16 nSourceIdx /* Index to be copied / to moved template */
)
/* [Description]
Copying a template
[Return value]
sal_Bool sal_True, Action could be performed
sal_False, Action could not be performed
[Cross-references]
<SfxDocumentTemplates::CopyOrMove(sal_uInt16,sal_uInt16,sal_uInt16,sal_uInt16,sal_Bool)>
*/
{
DocTemplLocker_Impl aLocker( *pImp );
return CopyOrMove( nTargetRegion, nTargetIdx,
nSourceRegion, nSourceIdx, false );
}
bool SfxDocumentTemplates::CopyTo
(
sal_uInt16 nRegion, // Region of the template to be exported
sal_uInt16 nIdx, // Index of the template to be exported
std::u16string_view rName /* File name under which the template is to
be created */
) const
/* [Description]
Exporting a template into the file system
[Return value]
sal_Bool sal_True, Action could be performed
sal_False, Action could not be performed
[Cross-references]
<SfxDocumentTemplates::CopyFrom(sal_uInt16,sal_uInt16,String&)>
*/
{
DocTemplLocker_Impl aLocker( *pImp );
if ( ! pImp->Construct() )
return false;
RegionData_Impl *pSourceRgn = pImp->GetRegion( nRegion );
if ( !pSourceRgn )
return false;
DocTempl_EntryData_Impl *pSource = pSourceRgn->GetEntry( nIdx );
if ( !pSource )
return false;
INetURLObject aTargetURL( rName );
const OUString aTitle( aTargetURL.getName( INetURLObject::LAST_SEGMENT, true,
INetURLObject::DecodeMechanism::WithCharset ) );
aTargetURL.removeSegment();
const OUString aParentURL = aTargetURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
uno::Reference< XCommandEnvironment > aCmdEnv;
Content aTarget;
try
{
aTarget = Content( aParentURL, aCmdEnv, comphelper::getProcessComponentContext() );
TransferInfo aTransferInfo;
aTransferInfo.MoveData = false;
aTransferInfo.SourceURL = pSource->GetTargetURL();
aTransferInfo.NewTitle = aTitle;
aTransferInfo.NameClash = NameClash::RENAME;
Any aArg( aTransferInfo );
aTarget.executeCommand( COMMAND_TRANSFER, aArg );
}
catch ( ContentCreationException& )
{ return false; }
catch ( Exception& )
{ return false; }
return true;
}
bool SfxDocumentTemplates::CopyFrom
(
sal_uInt16 nRegion, /* Region in which the template is to be
imported */
sal_uInt16 nIdx, // Index of the new template in this Region
OUString& rName /* File name of the template to be imported
as an out parameter of the (automatically
generated from the file name) logical name
of the template */
)
/* [Description]
Import a template from the file system
[Return value] Success (sal_True) or serfpTargetDirectory->GetContent());
sal_Bool sal_True, Action could be performed
sal_False, Action could not be performed
[Cross-references]
<SfxDocumentTemplates::CopyTo(sal_uInt16,sal_uInt16,const String&)>
*/
{
DocTemplLocker_Impl aLocker( *pImp );
if ( ! pImp->Construct() )
return false;
RegionData_Impl *pTargetRgn = pImp->GetRegion( nRegion );
if ( !pTargetRgn )
return false;
uno::Reference< XDocumentTemplates > xTemplates = pImp->getDocTemplates();
if ( !xTemplates.is() )
return false;
OUString aTitle;
bool bTemplateAdded = false;
if( pImp->GetTitleFromURL( rName, aTitle ) )
{
bTemplateAdded = xTemplates->addTemplate( pTargetRgn->GetTitle(), aTitle, rName );
}
else
{
uno::Reference< XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() );
Sequence< PropertyValue > aArgs{ comphelper::makePropertyValue("Hidden", true) };
INetURLObject aTemplURL( rName );
uno::Reference< XDocumentPropertiesSupplier > xDocPropsSupplier;
uno::Reference< XStorable > xStorable;
try
{
xStorable.set(
xDesktop->loadComponentFromURL( aTemplURL.GetMainURL(INetURLObject::DecodeMechanism::NONE),
"_blank",
0,
aArgs ),
UNO_QUERY );
xDocPropsSupplier.set( xStorable, UNO_QUERY );
}
catch( Exception& )
{
}
if( xStorable.is() )
{
// get Title from XDocumentPropertiesSupplier
if( xDocPropsSupplier.is() )
{
uno::Reference< XDocumentProperties > xDocProps
= xDocPropsSupplier->getDocumentProperties();
if (xDocProps.is() ) {
aTitle = xDocProps->getTitle();
}
}
if( aTitle.isEmpty() )
{
INetURLObject aURL( aTemplURL );
aURL.CutExtension();
aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true,
INetURLObject::DecodeMechanism::WithCharset );
}
// write a template using XStorable interface
bTemplateAdded = xTemplates->storeTemplate( pTargetRgn->GetTitle(), aTitle, xStorable );
}
}
if( bTemplateAdded )
{
INetURLObject aTemplObj( pTargetRgn->GetHierarchyURL() );
aTemplObj.insertName( aTitle, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aTemplURL = aTemplObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
uno::Reference< XCommandEnvironment > aCmdEnv;
Content aTemplCont;
if( Content::create( aTemplURL, aCmdEnv, comphelper::getProcessComponentContext(), aTemplCont ) )
{
OUString aTemplName;
if( getTextProperty_Impl( aTemplCont, TARGET_URL, aTemplName ) )
{
if ( nIdx == USHRT_MAX )
nIdx = 0;
else
++nIdx;
// todo: fix SfxDocumentTemplates to handle size_t instead of sal_uInt16
size_t temp_nIdx = nIdx;
pTargetRgn->AddEntry( aTitle, aTemplName, &temp_nIdx );
rName = aTitle;
return true;
}
else
{
SAL_WARN( "sfx.doc", "CopyFrom(): The content should contain target URL!" );
}
}
else
{
SAL_WARN( "sfx.doc", "CopyFrom(): The content just was created!" );
}
}
return false;
}
bool SfxDocumentTemplates::Delete
(
sal_uInt16 nRegion, // Region Index
sal_uInt16 nIdx /* Index of the entry or USHRT_MAX,
if a directory is meant. */
)
/* [Description]
Deleting an entry or a directory
[Return value]
sal_Bool sal_True, Action could be performed
sal_False, Action could not be performed
[Cross-references]
<SfxDocumentTemplates::InsertDir(const String&,sal_uInt16)>
<SfxDocumentTemplates::KillDir(SfxTemplateDir&)>
*/
{
DocTemplLocker_Impl aLocker( *pImp );
/* delete the template or folder in the hierarchy and in the
template folder by sending a delete command to the content.
Then remove the data from the lists
*/
if ( ! pImp->Construct() )
return false;
RegionData_Impl *pRegion = pImp->GetRegion( nRegion );
if ( !pRegion )
return false;
bool bRet;
uno::Reference< XDocumentTemplates > xTemplates = pImp->getDocTemplates();
if ( nIdx == USHRT_MAX )
{
bRet = xTemplates->removeGroup( pRegion->GetTitle() );
if ( bRet )
pImp->DeleteRegion( nRegion );
}
else
{
DocTempl_EntryData_Impl *pEntry = pRegion->GetEntry( nIdx );
if ( !pEntry )
return false;
bRet = xTemplates->removeTemplate( pRegion->GetTitle(),
pEntry->GetTitle() );
if( bRet )
pRegion->DeleteEntry( nIdx );
}
return bRet;
}
bool SfxDocumentTemplates::InsertDir
(
const OUString& rText, // the logical name of the new Region
sal_uInt16 nRegion // Region Index
)
/* [Description]
Insert an index
[Return value]
sal_Bool sal_True, Action could be performed
sal_False, Action could not be performed
[Cross-references]
<SfxDocumentTemplates::KillDir(SfxTemplateDir&)>
*/
{
DocTemplLocker_Impl aLocker( *pImp );
if ( ! pImp->Construct() )
return false;
RegionData_Impl *pRegion = pImp->GetRegion( rText );
if ( pRegion )
return false;
uno::Reference< XDocumentTemplates > xTemplates = pImp->getDocTemplates();
if ( xTemplates->addGroup( rText ) )
{
return pImp->InsertRegion( std::make_unique<RegionData_Impl>( pImp.get(), rText ), nRegion );
}
return false;
}
bool SfxDocumentTemplates::InsertTemplate(sal_uInt16 nSourceRegion, sal_uInt16 nIdx, const OUString &rName, const OUString &rPath)
{
DocTemplLocker_Impl aLocker( *pImp );
if ( ! pImp->Construct() )
return false;
RegionData_Impl *pRegion = pImp->GetRegion( nSourceRegion );
if ( !pRegion )
return false;
size_t pos = nIdx;
pRegion->AddEntry( rName, rPath, &pos );
return true;
}
bool SfxDocumentTemplates::SetName( const OUString& rName, sal_uInt16 nRegion, sal_uInt16 nIdx )
{
DocTemplLocker_Impl aLocker( *pImp );
if ( ! pImp->Construct() )
return false;
RegionData_Impl *pRegion = pImp->GetRegion( nRegion );
if ( !pRegion )
return false;
uno::Reference< XDocumentTemplates > xTemplates = pImp->getDocTemplates();
if ( nIdx == USHRT_MAX )
{
if ( pRegion->GetTitle() == rName )
return true;
// we have to rename a region
if ( xTemplates->renameGroup( pRegion->GetTitle(), rName ) )
{
pRegion->SetTitle( rName );
pRegion->SetHierarchyURL( "" );
return true;
}
}
else
{
DocTempl_EntryData_Impl *pEntry = pRegion->GetEntry( nIdx );
if ( !pEntry )
return false;
if ( pEntry->GetTitle() == rName )
return true;
if ( xTemplates->renameTemplate( pRegion->GetTitle(),
pEntry->GetTitle(),
rName ) )
{
pEntry->SetTitle( rName );
pEntry->SetTargetURL( "" );
pEntry->SetHierarchyURL( "" );
return true;
}
}
return false;
}
bool SfxDocumentTemplates::GetFull
(
std::u16string_view rRegion, // Region Name
std::u16string_view rName, // Template Name
OUString &rPath // Out: Path + File name
)
/* [Description]
Returns Path + File name of the template specified by rRegion and rName.
[Return value]
sal_Bool sal_True, Action could be performed
sal_False, Action could not be performed
[Cross-references]
<SfxDocumentTemplates::GetLogicNames(const String&,String&,String&)>
*/
{
DocTemplLocker_Impl aLocker( *pImp );
// We don't search for empty names!
if ( rName.empty() )
return false;
if ( ! pImp->Construct() )
return false;
DocTempl_EntryData_Impl* pEntry = nullptr;
const sal_uInt16 nCount = GetRegionCount();
for ( sal_uInt16 i = 0; i < nCount; ++i )
{
RegionData_Impl *pRegion = pImp->GetRegion( i );
if( pRegion &&
( rRegion.empty() || ( rRegion == pRegion->GetTitle() ) ) )
{
pEntry = pRegion->GetEntry( rName );
if ( pEntry )
{
rPath = pEntry->GetTargetURL();
break;
}
}
}
return ( pEntry != nullptr );
}
bool SfxDocumentTemplates::GetLogicNames
(
std::u16string_view rPath, // Full Path to the template
OUString &rRegion, // Out: Region name
OUString &rName // Out: Template name
) const
/* [Description]
Returns and logical path name to the template specified by rPath
[Return value]
sal_Bool sal_True, Action could be performed
sal_False, Action could not be performed
[Cross-references]
<SfxDocumentTemplates::GetFull(const String&,const String&,DirEntry&)>
*/
{
DocTemplLocker_Impl aLocker( *pImp );
if ( ! pImp->Construct() )
return false;
INetURLObject aFullPath;
aFullPath.SetSmartProtocol( INetProtocol::File );
aFullPath.SetURL( rPath );
const OUString aPath( aFullPath.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
const sal_uInt16 nCount = GetRegionCount();
for ( sal_uInt16 i=0; i<nCount; ++i )
{
RegionData_Impl *pData = pImp->GetRegion( i );
if ( pData )
{
const sal_uInt16 nChildCount = pData->GetCount();
for ( sal_uInt16 j=0; j<nChildCount; ++j )
{
DocTempl_EntryData_Impl *pEntry = pData->GetEntry( j );
if ( pEntry && pEntry->GetTargetURL() == aPath )
{
rRegion = pData->GetTitle();
rName = pEntry->GetTitle();
return true;
}
}
}
}
return false;
}
SfxDocumentTemplates::SfxDocumentTemplates()
/* [Description]
Constructor
*/
{
if ( !gpTemplateData )
gpTemplateData = new SfxDocTemplate_Impl;
pImp = gpTemplateData;
}
SfxDocumentTemplates::~SfxDocumentTemplates()
/* [Description]
Destructor
Release of administrative data
*/
{
pImp = nullptr;
}
void SfxDocumentTemplates::Update( )
{
if ( ::svt::TemplateFolderCache( true ).needsUpdate() ) // update is really necessary
{
if ( pImp->Construct() )
pImp->Rescan();
}
}
void SfxDocumentTemplates::ReInitFromComponent()
{
pImp->ReInitFromComponent();
}
DocTempl_EntryData_Impl::DocTempl_EntryData_Impl( RegionData_Impl* pParent,
const OUString& rTitle )
{
mpParent = pParent;
maTitle = SfxDocumentTemplates::ConvertResourceString(rTitle);
}
int DocTempl_EntryData_Impl::Compare( std::u16string_view rTitle ) const
{
return maTitle.compareTo( rTitle );
}
const OUString& DocTempl_EntryData_Impl::GetHierarchyURL()
{
if ( maOwnURL.isEmpty() )
{
INetURLObject aTemplateObj( mpParent->GetHierarchyURL() );
aTemplateObj.insertName( GetTitle(), false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
maOwnURL = aTemplateObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
DBG_ASSERT( !maOwnURL.isEmpty(), "GetHierarchyURL(): Could not create URL!" );
}
return maOwnURL;
}
const OUString& DocTempl_EntryData_Impl::GetTargetURL()
{
if ( maTargetURL.isEmpty() )
{
uno::Reference< XCommandEnvironment > aCmdEnv;
Content aRegion;
if ( Content::create( GetHierarchyURL(), aCmdEnv, comphelper::getProcessComponentContext(), aRegion ) )
{
getTextProperty_Impl( aRegion, TARGET_URL, maTargetURL );
}
else
{
SAL_WARN( "sfx.doc", "GetTargetURL(): Could not create hierarchy content!" );
}
}
return maTargetURL;
}
RegionData_Impl::RegionData_Impl( const SfxDocTemplate_Impl* pParent,
OUString aTitle )
: mpParent(pParent), maTitle(std::move(aTitle))
{
}
size_t RegionData_Impl::GetEntryPos( std::u16string_view rTitle, bool& rFound ) const
{
const size_t nCount = maEntries.size();
for ( size_t i=0; i<nCount; ++i )
{
auto &pData = maEntries[ i ];
if ( pData->Compare( rTitle ) == 0 )
{
rFound = true;
return i;
}
}
rFound = false;
return nCount;
}
void RegionData_Impl::AddEntry( const OUString& rTitle,
const OUString& rTargetURL,
const size_t *pPos )
{
INetURLObject aLinkObj( GetHierarchyURL() );
aLinkObj.insertName( rTitle, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aLinkURL = aLinkObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
bool bFound = false;
size_t nPos = GetEntryPos( rTitle, bFound );
if ( bFound )
return;
if ( pPos )
nPos = *pPos;
auto pEntry = std::make_unique<DocTempl_EntryData_Impl>(
this, rTitle );
pEntry->SetTargetURL( rTargetURL );
pEntry->SetHierarchyURL( aLinkURL );
if ( nPos < maEntries.size() ) {
auto it = maEntries.begin();
std::advance( it, nPos );
maEntries.insert( it, std::move(pEntry) );
}
else
maEntries.push_back( std::move(pEntry) );
}
size_t RegionData_Impl::GetCount() const
{
return maEntries.size();
}
const OUString& RegionData_Impl::GetHierarchyURL()
{
if ( maOwnURL.isEmpty() )
{
INetURLObject aRegionObj( mpParent->GetRootURL() );
aRegionObj.insertName( GetTitle(), false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
maOwnURL = aRegionObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
DBG_ASSERT( !maOwnURL.isEmpty(), "GetHierarchyURL(): Could not create URL!" );
}
return maOwnURL;
}
DocTempl_EntryData_Impl* RegionData_Impl::GetEntry( std::u16string_view rName ) const
{
bool bFound = false;
tools::Long nPos = GetEntryPos( rName, bFound );
if ( bFound )
return maEntries[ nPos ].get();
return nullptr;
}
DocTempl_EntryData_Impl* RegionData_Impl::GetEntry( size_t nIndex ) const
{
if ( nIndex < maEntries.size() )
return maEntries[ nIndex ].get();
return nullptr;
}
void RegionData_Impl::DeleteEntry( size_t nIndex )
{
if ( nIndex < maEntries.size() )
{
auto it = maEntries.begin();
std::advance( it, nIndex );
maEntries.erase( it );
}
}
int RegionData_Impl::Compare( RegionData_Impl const * pCompare ) const
{
return maTitle.compareTo( pCompare->maTitle );
}
SfxDocTemplate_Impl::SfxDocTemplate_Impl()
: mbConstructed( false )
, mnLockCounter( 0 )
{
}
SfxDocTemplate_Impl::~SfxDocTemplate_Impl()
{
gpTemplateData = nullptr;
}
void SfxDocTemplate_Impl::IncrementLock()
{
std::unique_lock aGuard( maMutex );
mnLockCounter++;
}
void SfxDocTemplate_Impl::DecrementLock()
{
std::unique_lock aGuard( maMutex );
if ( mnLockCounter )
mnLockCounter--;
}
RegionData_Impl* SfxDocTemplate_Impl::GetRegion( size_t nIndex ) const
{
if ( nIndex < maRegions.size() )
return maRegions[ nIndex ].get();
return nullptr;
}
RegionData_Impl* SfxDocTemplate_Impl::GetRegion( std::u16string_view rName )
const
{
for (auto& pData : maRegions)
{
if( pData->GetTitle() == rName )
return pData.get();
}
return nullptr;
}
void SfxDocTemplate_Impl::DeleteRegion( size_t nIndex )
{
if ( nIndex < maRegions.size() )
{
auto it = maRegions.begin();
std::advance( it, nIndex );
maRegions.erase( it );
}
}
/* AddRegion adds a Region to the RegionList
*/
void SfxDocTemplate_Impl::AddRegion( std::unique_lock<std::mutex>& /*rGuard*/,
const OUString& rTitle,
Content& rContent )
{
auto pRegion = std::make_unique<RegionData_Impl>( this, rTitle );
auto pRegionTmp = pRegion.get();
if ( ! InsertRegion( std::move(pRegion), size_t(-1) ) )
{
return;
}
// now get the content of the region
uno::Reference< XResultSet > xResultSet;
try
{
xResultSet = rContent.createSortedCursor( { TITLE, TARGET_URL }, { { 1, true } }, m_rCompareFactory, INCLUDE_DOCUMENTS_ONLY );
}
catch ( Exception& ) {}
if ( !xResultSet.is() )
return;
uno::Reference< XRow > xRow( xResultSet, UNO_QUERY );
try
{
while ( xResultSet->next() )
{
pRegionTmp->AddEntry( xRow->getString( 1 ), xRow->getString( 2 ), nullptr );
}
}
catch ( Exception& ) {}
}
void SfxDocTemplate_Impl::CreateFromHierarchy( std::unique_lock<std::mutex>& rGuard, Content &rTemplRoot )
{
uno::Reference< XResultSet > xResultSet;
Sequence< OUString > aProps { TITLE };
try
{
xResultSet = rTemplRoot.createSortedCursor(
aProps,
{ // Sequence
{ // NumberedSortingInfo
/* ColumnIndex */ 1, /* Ascending */ true
}
},
m_rCompareFactory,
INCLUDE_FOLDERS_ONLY
);
}
catch ( Exception& ) {}
if ( !xResultSet.is() )
return;
uno::Reference< XCommandEnvironment > aCmdEnv;
uno::Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
uno::Reference< XRow > xRow( xResultSet, UNO_QUERY );
try
{
while ( xResultSet->next() )
{
const OUString aId = xContentAccess->queryContentIdentifierString();
Content aContent( aId, aCmdEnv, comphelper::getProcessComponentContext() );
AddRegion( rGuard, xRow->getString( 1 ), aContent );
}
}
catch ( Exception& ) {}
}
bool SfxDocTemplate_Impl::Construct( )
{
std::unique_lock aGuard( maMutex );
if ( mbConstructed )
return true;
uno::Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
uno::Reference< XPersist > xInfo( document::DocumentProperties::create(xContext), UNO_QUERY );
mxInfo = xInfo;
mxTemplates = frame::DocumentTemplates::create(xContext);
uno::Reference< XLocalizable > xLocalizable( mxTemplates, UNO_QUERY );
m_rCompareFactory = AnyCompareFactory::createWithLocale(xContext, xLocalizable->getLocale());
uno::Reference < XContent > aRootContent = mxTemplates->getContent();
uno::Reference < XCommandEnvironment > aCmdEnv;
if ( ! aRootContent.is() )
return false;
mbConstructed = true;
maRootURL = aRootContent->getIdentifier()->getContentIdentifier();
maStandardGroup = DocTemplLocaleHelper::GetStandardGroupString();
Content aTemplRoot( aRootContent, aCmdEnv, xContext );
CreateFromHierarchy( aGuard, aTemplRoot );
return true;
}
void SfxDocTemplate_Impl::ReInitFromComponent()
{
uno::Reference< XDocumentTemplates > xTemplates = getDocTemplates();
if ( xTemplates.is() )
{
uno::Reference < XContent > aRootContent = xTemplates->getContent();
uno::Reference < XCommandEnvironment > aCmdEnv;
Content aTemplRoot( aRootContent, aCmdEnv, comphelper::getProcessComponentContext() );
Clear();
std::unique_lock aGuard(maMutex);
CreateFromHierarchy( aGuard, aTemplRoot );
}
}
bool SfxDocTemplate_Impl::InsertRegion( std::unique_ptr<RegionData_Impl> pNew, size_t nPos )
{
// return false (not inserted) if the entry already exists
for (auto const& pRegion : maRegions)
if ( pRegion->Compare( pNew.get() ) == 0 )
return false;
size_t newPos = nPos;
if ( pNew->GetTitle() == maStandardGroup )
newPos = 0;
if ( newPos < maRegions.size() )
{
auto it = maRegions.begin();
std::advance( it, newPos );
maRegions.emplace( it, std::move(pNew) );
}
else
maRegions.emplace_back( std::move(pNew) );
return true;
}
void SfxDocTemplate_Impl::Rescan()
{
Clear();
try
{
uno::Reference< XDocumentTemplates > xTemplates = getDocTemplates();
DBG_ASSERT( xTemplates.is(), "SfxDocTemplate_Impl::Rescan:invalid template instance!" );
if ( xTemplates.is() )
{
xTemplates->update();
uno::Reference < XContent > aRootContent = xTemplates->getContent();
uno::Reference < XCommandEnvironment > aCmdEnv;
Content aTemplRoot( aRootContent, aCmdEnv, comphelper::getProcessComponentContext() );
std::unique_lock aGuard(maMutex);
CreateFromHierarchy( aGuard, aTemplRoot );
}
}
catch( const Exception& )
{
TOOLS_WARN_EXCEPTION( "sfx.doc", "SfxDocTemplate_Impl::Rescan: caught an exception while doing the update" );
}
}
bool SfxDocTemplate_Impl::GetTitleFromURL( const OUString& rURL,
OUString& aTitle )
{
if ( mxInfo.is() )
{
try
{
mxInfo->read( rURL );
}
catch ( Exception& )
{
// the document is not a StarOffice document
return false;
}
try
{
uno::Reference< XPropertySet > aPropSet( mxInfo, UNO_QUERY );
if ( aPropSet.is() )
{
Any aValue = aPropSet->getPropertyValue( TITLE );
aValue >>= aTitle;
}
}
catch ( IOException& ) {}
catch ( UnknownPropertyException& ) {}
catch ( Exception& ) {}
}
if ( aTitle.isEmpty() )
{
INetURLObject aURL( rURL );
aURL.CutExtension();
aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true,
INetURLObject::DecodeMechanism::WithCharset );
}
return true;
}
void SfxDocTemplate_Impl::Clear()
{
std::unique_lock aGuard( maMutex );
if ( mnLockCounter )
return;
maRegions.clear();
}
bool getTextProperty_Impl( Content& rContent,
const OUString& rPropName,
OUString& rPropValue )
{
bool bGotProperty = false;
// Get the property
try
{
uno::Reference< XPropertySetInfo > aPropInfo = rContent.getProperties();
// check, whether or not the property exists
if ( !aPropInfo.is() || !aPropInfo->hasPropertyByName( rPropName ) )
{
return false;
}
// now get the property
Any aAnyValue = rContent.getPropertyValue( rPropName );
aAnyValue >>= rPropValue;
if ( SfxURLRelocator_Impl::propertyCanContainOfficeDir( rPropName ) )
{
SfxURLRelocator_Impl aRelocImpl( ::comphelper::getProcessComponentContext() );
aRelocImpl.makeAbsoluteURL( rPropValue );
}
bGotProperty = true;
}
catch ( RuntimeException& ) {}
catch ( Exception& ) {}
return bGotProperty;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|