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
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762 | /* -*- 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 <osl/mutex.hxx>
#include <tools/diagnose_ex.h>
#include <tools/urlobj.hxx>
#include <rtl/ustring.hxx>
#include <sal/log.hxx>
#include <vcl/svapp.hxx>
#include <vcl/wrkwin.hxx>
#include <unotools/pathoptions.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertysequence.hxx>
#include <comphelper/sequenceashashmap.hxx>
#include <comphelper/storagehelper.hxx>
#include <comphelper/string.hxx>
#include <cppuhelper/implbase.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <com/sun/star/beans/IllegalTypeException.hpp>
#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <com/sun/star/beans/PropertyExistException.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/XPropertySetInfo.hpp>
#include <com/sun/star/beans/XPropertyContainer.hpp>
#include <com/sun/star/beans/StringPair.hpp>
#include <com/sun/star/util/theMacroExpander.hpp>
#include <com/sun/star/util/theOfficeInstallationDirectories.hpp>
#include <com/sun/star/configuration/theDefaultProvider.hpp>
#include <com/sun/star/document/XTypeDetection.hpp>
#include <com/sun/star/document/DocumentProperties.hpp>
#include <com/sun/star/io/TempFile.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/NameClashException.hpp>
#include <com/sun/star/ucb/XCommandEnvironment.hpp>
#include <com/sun/star/ucb/XContentAccess.hpp>
#include <com/sun/star/frame/ModuleManager.hpp>
#include <com/sun/star/uno/Exception.hpp>
#include <com/sun/star/task/InteractionHandler.hpp>
#include <com/sun/star/ucb/XProgressHandler.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/frame/XDocumentTemplates.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/lang/Locale.hpp>
#include <com/sun/star/lang/XLocalizable.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/ucb/XContent.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/uno/RuntimeException.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/util/thePathSettings.hpp>
#include <svtools/templatefoldercache.hxx>
#include <unotools/configmgr.hxx>
#include <unotools/ucbhelper.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <ucbhelper/content.hxx>
#include <sfx2/sfxresid.hxx>
#include <sfxurlrelocator.hxx>
#include "doctemplateslocal.hxx"
#include <sfx2/docfac.hxx>
#include <sfx2/strings.hrc>
#include <doctempl.hrc>
#include <memory>
#include <vector>
#define SERVICENAME_TYPEDETECTION "com.sun.star.document.TypeDetection"
#define TEMPLATE_ROOT_URL "vnd.sun.star.hier:/templates"
#define TITLE "Title"
#define IS_FOLDER "IsFolder"
#define IS_DOCUMENT "IsDocument"
#define TARGET_URL "TargetURL"
#define TEMPLATE_VERSION "TemplateComponentVersion"
#define TEMPLATE_VERSION_VALUE "2"
#define TYPE_FOLDER "application/vnd.sun.star.hier-folder"
#define TYPE_LINK "application/vnd.sun.star.hier-link"
#define TYPE_FSYS_FOLDER "application/vnd.sun.staroffice.fsys-folder"
#define TYPE_FSYS_FILE "application/vnd.sun.staroffice.fsys-file"
#define PROPERTY_DIRLIST "DirectoryList"
#define PROPERTY_NEEDSUPDATE "NeedsUpdate"
#define PROPERTY_TYPE "TypeDescription"
#define TARGET_DIR_URL "TargetDirURL"
#define COMMAND_DELETE "delete"
#define STANDARD_FOLDER "standard"
#define C_DELIM ';'
using namespace ::com::sun::star;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::document;
using namespace ::com::sun::star::io;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::ucb;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::util;
using namespace ::ucbhelper;
using namespace ::comphelper;
using ::std::vector;
namespace {
class WaitWindow_Impl : public WorkWindow
{
tools::Rectangle maRect;
OUString maText;
static constexpr DrawTextFlags gnTextStyle = DrawTextFlags::Center | DrawTextFlags::VCenter | DrawTextFlags::WordBreak | DrawTextFlags::MultiLine;
public:
WaitWindow_Impl();
virtual ~WaitWindow_Impl() override;
virtual void dispose() override;
virtual void Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect) override;
};
#define X_OFFSET 15
#define Y_OFFSET 15
struct NamePair_Impl
{
OUString maShortName;
OUString maLongName;
};
class DocTemplates_EntryData_Impl;
class GroupData_Impl;
typedef vector< std::unique_ptr<GroupData_Impl> > GroupList_Impl;
class TplTaskEnvironment : public ::cppu::WeakImplHelper< ucb::XCommandEnvironment >
{
uno::Reference< task::XInteractionHandler > m_xInteractionHandler;
public:
explicit TplTaskEnvironment( const uno::Reference< task::XInteractionHandler>& rxInteractionHandler )
: m_xInteractionHandler( rxInteractionHandler )
{}
virtual uno::Reference<task::XInteractionHandler> SAL_CALL getInteractionHandler() override
{ return m_xInteractionHandler; }
virtual uno::Reference<ucb::XProgressHandler> SAL_CALL getProgressHandler() override
{ return uno::Reference<ucb::XProgressHandler>(); }
};
class SfxDocTplService_Impl
{
uno::Reference< XComponentContext > mxContext;
uno::Reference< XCommandEnvironment > maCmdEnv;
uno::Reference< XDocumentProperties> m_xDocProps;
uno::Reference< XTypeDetection > mxType;
::osl::Mutex maMutex;
Sequence< OUString > maTemplateDirs;
Sequence< OUString > maInternalTemplateDirs;
OUString maRootURL;
std::vector< std::unique_ptr<NamePair_Impl> > maNames;
lang::Locale maLocale;
Content maRootContent;
bool mbIsInitialized : 1;
bool mbLocaleSet : 1;
SfxURLRelocator_Impl maRelocator;
void init_Impl();
void getDefaultLocale();
void getDirList();
void readFolderList();
bool needsUpdate();
OUString getLongName( const OUString& rShortName );
bool setTitleForURL( const OUString& rURL, const OUString& aTitle );
void getTitleFromURL( const OUString& rURL, OUString& aTitle, OUString& aType, bool& bDocHasTitle );
bool addEntry( Content& rParentFolder,
const OUString& rTitle,
const OUString& rTargetURL,
const OUString& rType );
bool createFolder( const OUString& rNewFolderURL,
bool bCreateParent,
bool bFsysFolder,
Content &rNewFolder );
static bool CreateNewUniqueFolderWithPrefix( const OUString& aPath,
const OUString& aPrefix,
OUString& aNewFolderName,
OUString& aNewFolderURL,
Content& aNewFolder );
static OUString CreateNewUniqueFileWithPrefix( const OUString& aPath,
const OUString& aPrefix,
const OUString& aExt );
std::vector< beans::StringPair > ReadUINamesForTemplateDir_Impl( const OUString& aUserPath );
bool UpdateUINamesForTemplateDir_Impl( const OUString& aUserPath,
const OUString& aGroupName,
const OUString& aNewFolderName );
bool ReplaceUINamesForTemplateDir_Impl( const OUString& aUserPath,
const OUString& aFsysGroupName,
const OUString& aOldGroupName,
const OUString& aNewGroupName );
void RemoveUINamesForTemplateDir_Impl( const OUString& aUserPath,
const OUString& aGroupName );
bool WriteUINamesForTemplateDir_Impl( const OUString& aUserPath,
const std::vector< beans::StringPair >& aUINames );
OUString CreateNewGroupFsys( const OUString& rGroupName, Content& aGroup );
static bool removeContent( Content& rContent );
bool removeContent( const OUString& rContentURL );
bool setProperty( Content& rContent,
const OUString& rPropName,
const Any& rPropValue );
bool getProperty( Content& rContent,
const OUString& rPropName,
Any& rPropValue );
void createFromContent( GroupList_Impl& rList,
Content &rContent,
bool bHierarchy,
bool bWriteableContent );
void addHierGroup( GroupList_Impl& rList,
const OUString& rTitle,
const OUString& rOwnURL );
void addFsysGroup( GroupList_Impl& rList,
const OUString& rTitle,
const OUString& rUITitle,
const OUString& rOwnURL,
bool bWriteableGroup );
void removeFromHierarchy( DocTemplates_EntryData_Impl const *pData );
void addToHierarchy( GroupData_Impl const *pGroup,
DocTemplates_EntryData_Impl const *pData );
void removeFromHierarchy( GroupData_Impl const *pGroup );
void addGroupToHierarchy( GroupData_Impl *pGroup );
void updateData( DocTemplates_EntryData_Impl const *pData );
//See: #i66157# and rhbz#1065807
//return which template dir the rURL is a subpath of
OUString findParentTemplateDir(const OUString& rURL) const;
//See: #i66157# and rhbz#1065807
//return true if rURL is a path (or subpath of) a dir which is not a user path
//which implies neither it or its contents can be removed
bool isInternalTemplateDir(const OUString& rURL) const;
public:
explicit SfxDocTplService_Impl( const uno::Reference< XComponentContext > & xContext );
~SfxDocTplService_Impl();
bool init() { if ( !mbIsInitialized ) init_Impl(); return mbIsInitialized; }
const Content& getContent() const { return maRootContent; }
void setLocale( const lang::Locale & rLocale );
lang::Locale getLocale();
bool storeTemplate( const OUString& rGroupName,
const OUString& rTemplateName,
const uno::Reference< frame::XStorable >& rStorable );
bool addTemplate( const OUString& rGroupName,
const OUString& rTemplateName,
const OUString& rSourceURL );
bool removeTemplate( const OUString& rGroupName,
const OUString& rTemplateName );
bool renameTemplate( const OUString& rGroupName,
const OUString& rOldName,
const OUString& rNewName );
bool addGroup( const OUString& rGroupName );
bool removeGroup( const OUString& rGroupName );
bool renameGroup( const OUString& rOldName,
const OUString& rNewName );
void update();
void doUpdate();
};
class DocTemplates_EntryData_Impl
{
OUString maTitle;
OUString maType;
OUString maTargetURL;
OUString maHierarchyURL;
bool mbInHierarchy : 1;
bool mbInUse : 1;
bool mbUpdateType : 1;
bool mbUpdateLink : 1;
public:
explicit DocTemplates_EntryData_Impl( const OUString& rTitle );
void setInUse() { mbInUse = true; }
void setHierarchy( bool bInHierarchy ) { mbInHierarchy = bInHierarchy; }
void setUpdateLink( bool bUpdateLink ) { mbUpdateLink = bUpdateLink; }
void setUpdateType( bool bUpdateType ) { mbUpdateType = bUpdateType; }
bool getInUse() const { return mbInUse; }
bool getInHierarchy() const { return mbInHierarchy; }
bool getUpdateLink() const { return mbUpdateLink; }
bool getUpdateType() const { return mbUpdateType; }
const OUString& getHierarchyURL() const { return maHierarchyURL; }
const OUString& getTargetURL() const { return maTargetURL; }
const OUString& getTitle() const { return maTitle; }
const OUString& getType() const { return maType; }
void setHierarchyURL( const OUString& rURL ) { maHierarchyURL = rURL; }
void setTargetURL( const OUString& rURL ) { maTargetURL = rURL; }
void setType( const OUString& rType ) { maType = rType; }
};
class GroupData_Impl
{
std::vector< std::unique_ptr<DocTemplates_EntryData_Impl> > maEntries;
OUString maTitle;
OUString maHierarchyURL;
OUString maTargetURL;
bool mbInUse : 1;
bool mbInHierarchy : 1;
public:
explicit GroupData_Impl( const OUString& rTitle );
void setInUse() { mbInUse = true; }
void setHierarchy( bool bInHierarchy ) { mbInHierarchy = bInHierarchy; }
void setHierarchyURL( const OUString& rURL ) { maHierarchyURL = rURL; }
void setTargetURL( const OUString& rURL ) { maTargetURL = rURL; }
bool getInUse() const { return mbInUse; }
bool getInHierarchy() const { return mbInHierarchy; }
const OUString& getHierarchyURL() const { return maHierarchyURL; }
const OUString& getTargetURL() const { return maTargetURL; }
const OUString& getTitle() const { return maTitle; }
DocTemplates_EntryData_Impl* addEntry( const OUString& rTitle,
const OUString& rTargetURL,
const OUString& rType,
const OUString& rHierURL );
size_t count() { return maEntries.size(); }
DocTemplates_EntryData_Impl* getEntry( size_t nPos ) { return maEntries[ nPos ].get(); }
};
// private SfxDocTplService_Impl
void SfxDocTplService_Impl::init_Impl()
{
uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
uno::Reference < task::XInteractionHandler > xInteractionHandler(
task::InteractionHandler::createWithParent(xContext, nullptr), uno::UNO_QUERY_THROW );
maCmdEnv = new TplTaskEnvironment( xInteractionHandler );
::osl::ClearableMutexGuard aGuard( maMutex );
bool bIsInitialized = false;
bool bNeedsUpdate = false;
if ( !mbLocaleSet )
getDefaultLocale();
// convert locale to string
// set maRootContent to the root of the templates hierarchy. Create the
// entry if necessary
maRootURL = ( TEMPLATE_ROOT_URL "/" ) + LanguageTag::convertToBcp47(maLocale);
const OUString aTemplVersPropName( TEMPLATE_VERSION );
const OUString aTemplVers( TEMPLATE_VERSION_VALUE );
if ( Content::create( maRootURL, maCmdEnv, comphelper::getProcessComponentContext(), maRootContent ) )
{
uno::Any aValue;
OUString aPropValue;
if ( getProperty( maRootContent, aTemplVersPropName, aValue )
&& ( aValue >>= aPropValue )
&& aPropValue == aTemplVers )
{
bIsInitialized = true;
}
else
removeContent( maRootContent );
}
if ( !bIsInitialized )
{
if ( createFolder( maRootURL, true, false, maRootContent )
&& setProperty( maRootContent, aTemplVersPropName, uno::makeAny( aTemplVers ) ) )
bIsInitialized = true;
bNeedsUpdate = true;
}
if ( bIsInitialized )
{
try {
m_xDocProps.set(document::DocumentProperties::create(
::comphelper::getProcessComponentContext()));
} catch (uno::RuntimeException const&) {
TOOLS_WARN_EXCEPTION("sfx.doc", "SfxDocTplService_Impl::init_Impl: cannot create DocumentProperties service:");
}
mxType.set( mxContext->getServiceManager()->createInstanceWithContext(SERVICENAME_TYPEDETECTION, mxContext), UNO_QUERY );
getDirList();
readFolderList();
if ( bNeedsUpdate )
{
aGuard.clear();
SolarMutexClearableGuard aSolarGuard;
VclPtrInstance< WaitWindow_Impl > pWin;
aSolarGuard.clear();
{
osl::MutexGuard anotherGuard(maMutex);
update();
}
SolarMutexGuard aSecondSolarGuard;
pWin.disposeAndClear();
}
else if ( needsUpdate() )
// the UI should be shown only on the first update
update();
}
else
{
SAL_WARN( "sfx.doc", "init_Impl(): Could not create root" );
}
mbIsInitialized = bIsInitialized;
}
void SfxDocTplService_Impl::getDefaultLocale()
{
if ( !mbLocaleSet )<--- outer condition: !mbLocaleSet
{
::osl::MutexGuard aGuard( maMutex );
if ( !mbLocaleSet )<--- identical inner condition: !mbLocaleSet
{
maLocale = LanguageTag::convertToLocale( utl::ConfigManager::getUILocale(), false);
mbLocaleSet = true;
}
}
}
const char* TEMPLATE_SHORT_NAMES_ARY[] =
{
"standard",
"styles",
"officorr",
"offimisc",
"personal",
"forms",
"finance",
"educate",
"layout",
"presnt",
"misc",
"labels"
};
void SfxDocTplService_Impl::readFolderList()
{
SolarMutexGuard aGuard;
static_assert( SAL_N_ELEMENTS(TEMPLATE_SHORT_NAMES_ARY) == SAL_N_ELEMENTS(TEMPLATE_LONG_NAMES_ARY), "mismatch array lengths" );
const size_t nCount = std::min(SAL_N_ELEMENTS(TEMPLATE_SHORT_NAMES_ARY), SAL_N_ELEMENTS(TEMPLATE_LONG_NAMES_ARY));
for (size_t i = 0; i < nCount; ++i)
{
std::unique_ptr<NamePair_Impl> pPair( new NamePair_Impl );
pPair->maShortName = OUString::createFromAscii(TEMPLATE_SHORT_NAMES_ARY[i]);
pPair->maLongName = SfxResId(TEMPLATE_LONG_NAMES_ARY[i]);
maNames.push_back( std::move(pPair) );
}
}
OUString SfxDocTplService_Impl::getLongName( const OUString& rShortName )
{
OUString aRet;
for (auto const & pPair : maNames)
{
if ( pPair->maShortName == rShortName )
{<--- Consider using std::find_if algorithm instead of a raw loop.
aRet = pPair->maLongName;
break;
}
}
if ( aRet.isEmpty() )
aRet = rShortName;
return aRet;
}
void SfxDocTplService_Impl::getDirList()
{
Any aValue;
// Get the template dir list
// TODO/LATER: let use service, register listener
INetURLObject aURL;
OUString aDirs = SvtPathOptions().GetTemplatePath();
sal_Int32 nCount = comphelper::string::getTokenCount(aDirs, C_DELIM);
maTemplateDirs = Sequence< OUString >( nCount );
uno::Reference< util::XMacroExpander > xExpander = util::theMacroExpander::get(mxContext);
const OUString aPrefix(
"vnd.sun.star.expand:" );
sal_Int32 nIdx{ 0 };
for (auto& rTemplateDir : maTemplateDirs)
{
aURL.SetSmartProtocol( INetProtocol::File );
aURL.SetURL( aDirs.getToken( 0, C_DELIM, nIdx ) );
rTemplateDir = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
if ( xExpander.is() )
{
const sal_Int32 nIndex{ rTemplateDir.indexOf( aPrefix ) };
if (nIndex<0)
continue;
rTemplateDir = rTemplateDir.replaceAt(nIndex, aPrefix.getLength(), OUString());
rTemplateDir = xExpander->expandMacros( rTemplateDir );
}
}
aValue <<= maTemplateDirs;
css::uno::Reference< css::util::XPathSettings > xPathSettings =
css::util::thePathSettings::get(mxContext);
// load internal paths
Any aAny = xPathSettings->getPropertyValue( "Template_internal" );<--- Variable 'aAny' is assigned a value that is never used.
aAny >>= maInternalTemplateDirs;<--- Variable 'aAny' is assigned a value that is never used.
for (auto& rInternalTemplateDir : maInternalTemplateDirs)
{
//expand vnd.sun.star.expand: and remove "..." from them
//to normalize into the expected url patterns
maRelocator.makeRelocatableURL(rInternalTemplateDir);
maRelocator.makeAbsoluteURL(rInternalTemplateDir);
}
// Store the template dir list
setProperty( maRootContent, PROPERTY_DIRLIST, aValue );
}
bool SfxDocTplService_Impl::needsUpdate()
{
bool bNeedsUpdate = true;<--- Assignment 'bNeedsUpdate=true', assigned value is 1<--- Assignment 'bNeedsUpdate=true', assigned value is 1
Any aValue;
// Get the template dir list
bool bHasProperty = getProperty( maRootContent, PROPERTY_NEEDSUPDATE, aValue );
if ( bHasProperty )
aValue >>= bNeedsUpdate;
// the old template component also checks this state, but it is initialized from this component
// so if this component was already updated the old component does not need such an update
::svt::TemplateFolderCache aTempCache;
if ( !bNeedsUpdate )<--- Condition '!bNeedsUpdate' is always false
bNeedsUpdate = aTempCache.needsUpdate();
if ( bNeedsUpdate )<--- Condition 'bNeedsUpdate' is always true
aTempCache.storeState();
return bNeedsUpdate;
}
bool SfxDocTplService_Impl::setTitleForURL( const OUString& rURL, const OUString& aTitle )
{
if (m_xDocProps.is())
{
try
{
m_xDocProps->loadFromMedium(rURL, Sequence<PropertyValue>());
m_xDocProps->setTitle(aTitle);
uno::Reference< embed::XStorage > xStorage = ::comphelper::OStorageHelper::GetStorageFromURL(
rURL, embed::ElementModes::READWRITE);
uno::Sequence<beans::PropertyValue> medium( comphelper::InitPropertySequence({
{ "DocumentBaseURL", Any(rURL) },
{ "URL", Any(rURL) }
}));
m_xDocProps->storeToStorage(xStorage, medium);
return true;
}
catch ( Exception& )
{
}
}
return false;
}
void SfxDocTplService_Impl::getTitleFromURL( const OUString& rURL, OUString& aTitle, OUString& aType, bool& bDocHasTitle )
{
bDocHasTitle = false;
if (m_xDocProps.is())
{
try
{
m_xDocProps->loadFromMedium(rURL, Sequence<PropertyValue>());
aTitle = m_xDocProps->getTitle();
}
catch ( Exception& )
{
}
}
if ( aType.isEmpty() && mxType.is() )
{
const OUString aDocType {mxType->queryTypeByURL( rURL )};
if ( !aDocType.isEmpty() )
try
{
uno::Reference< container::XNameAccess > xTypeDetection( mxType, uno::UNO_QUERY_THROW );
SequenceAsHashMap aTypeProps( xTypeDetection->getByName( aDocType ) );
aType = aTypeProps.getUnpackedValueOrDefault(
"MediaType",
OUString() );
}
catch( uno::Exception& )
{}
}
if ( aTitle.isEmpty() )
{
INetURLObject aURL( rURL );
aURL.CutExtension();
aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true,
INetURLObject::DecodeMechanism::WithCharset );
}
else
bDocHasTitle = true;
}
bool SfxDocTplService_Impl::addEntry( Content& rParentFolder,
const OUString& rTitle,
const OUString& rTargetURL,
const OUString& rType )
{
bool bAddedEntry = false;
INetURLObject aLinkObj( rParentFolder.getURL() );
aLinkObj.insertName( rTitle, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aLinkURL {aLinkObj.GetMainURL( INetURLObject::DecodeMechanism::NONE )};
Content aLink;
if ( ! Content::create( aLinkURL, maCmdEnv, comphelper::getProcessComponentContext(), aLink ) )
{
Sequence< OUString > aNames(3);
aNames[0] = TITLE;
aNames[1] = IS_FOLDER;
aNames[2] = TARGET_URL;
Sequence< Any > aValues(3);
aValues[0] <<= rTitle;
aValues[1] <<= false;
aValues[2] <<= rTargetURL;
try
{
rParentFolder.insertNewContent( TYPE_LINK, aNames, aValues, aLink );
setProperty( aLink, PROPERTY_TYPE, makeAny( rType ) );
bAddedEntry = true;
}
catch( Exception& )
{}
}
return bAddedEntry;
}
bool SfxDocTplService_Impl::createFolder( const OUString& rNewFolderURL,
bool bCreateParent,
bool bFsysFolder,
Content &rNewFolder )
{
Content aParent;
bool bCreatedFolder = false;
INetURLObject aParentURL( rNewFolderURL );
const OUString aFolderName {aParentURL.getName( INetURLObject::LAST_SEGMENT, true,
INetURLObject::DecodeMechanism::WithCharset )};
// compute the parent folder url from the new folder url
// and remove the final slash, because Content::create doesn't
// like it
aParentURL.removeSegment();
if ( aParentURL.getSegmentCount() >= 1 )
aParentURL.removeFinalSlash();
// if the parent exists, we can continue with the creation of the
// new folder, we have to create the parent otherwise ( as long as
// bCreateParent is set to true )
if ( Content::create( aParentURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), maCmdEnv, comphelper::getProcessComponentContext(), aParent ) )
{
try
{
Sequence< OUString > aNames(2);
aNames[0] = TITLE;
aNames[1] = IS_FOLDER;
Sequence< Any > aValues(2);
aValues[0] <<= aFolderName;
aValues[1] <<= true;
OUString aType;
if ( bFsysFolder )
aType = TYPE_FSYS_FOLDER;
else
aType = TYPE_FOLDER;
aParent.insertNewContent( aType, aNames, aValues, rNewFolder );
bCreatedFolder = true;
}
catch( Exception const & )
{
TOOLS_WARN_EXCEPTION( "sfx.doc", "createFolder(): Could not create new folder" );
}
}
else if ( bCreateParent )
{
// if the parent doesn't exists and bCreateParent is set to true,
// we try to create the parent and if this was successful, we
// try to create the new folder again ( but this time, we set
// bCreateParent to false to avoid endless recursions )
if ( ( aParentURL.getSegmentCount() >= 1 ) &&
createFolder( aParentURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), bCreateParent, bFsysFolder, aParent ) )
{
bCreatedFolder = createFolder( rNewFolderURL, false, bFsysFolder, rNewFolder );
}
}
return bCreatedFolder;
}
bool SfxDocTplService_Impl::CreateNewUniqueFolderWithPrefix( const OUString& aPath,
const OUString& aPrefix,
OUString& aNewFolderName,
OUString& aNewFolderURL,
Content& aNewFolder )
{
bool bCreated = false;
INetURLObject aDirPath( aPath );
Content aParent;
uno::Reference< XCommandEnvironment > aQuietEnv;
if ( Content::create( aDirPath.GetMainURL( INetURLObject::DecodeMechanism::NONE ), aQuietEnv, comphelper::getProcessComponentContext(), aParent ) )
{
for ( sal_Int32 nInd = 0; nInd < 32000; nInd++ )
{
OUString aTryName = aPrefix;
if ( nInd )
aTryName += OUString::number( nInd );
try
{
Sequence< OUString > aNames(2);
aNames[0] = TITLE;
aNames[1] = IS_FOLDER;
Sequence< Any > aValues(2);
aValues[0] <<= aTryName;
aValues[1] <<= true;
bCreated = aParent.insertNewContent( TYPE_FSYS_FOLDER, aNames, aValues, aNewFolder );
}
catch( ucb::NameClashException& )
{
// if there is already an element, retry
}
catch( Exception& )
{
INetURLObject aObjPath( aDirPath );
aObjPath.insertName( aTryName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
// if there is already an element, retry
// if there was another error, do not try any more
if ( !::utl::UCBContentHelper::Exists( aObjPath.GetMainURL( INetURLObject::DecodeMechanism::NONE ) ) )
break;
}
if ( bCreated )
{
aNewFolderName = aTryName;
aNewFolderURL = aNewFolder.get()->getIdentifier()->getContentIdentifier();
break;
}
}
}
return bCreated;
}
OUString SfxDocTplService_Impl::CreateNewUniqueFileWithPrefix( const OUString& aPath,
const OUString& aPrefix,
const OUString& aExt )
{
OUString aNewFileURL;
INetURLObject aDirPath( aPath );
Content aParent;
uno::Reference< XCommandEnvironment > aQuietEnv;
if ( Content::create( aDirPath.GetMainURL( INetURLObject::DecodeMechanism::NONE ), aQuietEnv, comphelper::getProcessComponentContext(), aParent ) )
{
for ( sal_Int32 nInd = 0; nInd < 32000; nInd++ )
{
Content aNewFile;
bool bCreated = false;
OUString aTryName = aPrefix;
if ( nInd )
aTryName += OUString::number( nInd );
if ( aExt.toChar() != '.' )
aTryName += ".";
aTryName += aExt;
try
{
Sequence< OUString > aNames(2);
aNames[0] = TITLE;
aNames[1] = IS_DOCUMENT;
Sequence< Any > aValues(2);
aValues[0] <<= aTryName;
aValues[1] <<= true;
bCreated = aParent.insertNewContent( TYPE_FSYS_FILE, aNames, aValues, aNewFile );
}
catch( ucb::NameClashException& )
{
// if there is already an element, retry
}
catch( Exception& )
{
INetURLObject aObjPath( aPath );
aObjPath.insertName( aTryName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
// if there is already an element, retry
// if there was another error, do not try any more
if ( !::utl::UCBContentHelper::Exists( aObjPath.GetMainURL( INetURLObject::DecodeMechanism::NONE ) ) )
break;
}
if ( bCreated )
{
aNewFileURL = aNewFile.get()->getIdentifier()->getContentIdentifier();
break;
}
}
}
return aNewFileURL;
}
bool SfxDocTplService_Impl::removeContent( Content& rContent )
{
bool bRemoved = false;
try
{
Any aArg = makeAny( true );
rContent.executeCommand( COMMAND_DELETE, aArg );
bRemoved = true;
}
catch ( RuntimeException& ) {}
catch ( Exception& ) {}
return bRemoved;
}
bool SfxDocTplService_Impl::removeContent( const OUString& rContentURL )
{
Content aContent;
if ( Content::create( rContentURL, maCmdEnv, comphelper::getProcessComponentContext(), aContent ) )
return removeContent( aContent );
return false;
}
bool SfxDocTplService_Impl::setProperty( Content& rContent,
const OUString& rPropName,
const Any& rPropValue )
{
bool bPropertySet = false;
// Store the property
try
{
Any aPropValue( rPropValue );
uno::Reference< XPropertySetInfo > aPropInfo = rContent.getProperties();
// check, whether or not the property exists, create it, when not
if ( !aPropInfo.is() || !aPropInfo->hasPropertyByName( rPropName ) )
{
uno::Reference< XPropertyContainer > xProperties( rContent.get(), UNO_QUERY );
if ( xProperties.is() )
{
try
{
xProperties->addProperty( rPropName, PropertyAttribute::MAYBEVOID, rPropValue );
}
catch( PropertyExistException& ) {}
catch( IllegalTypeException& ) {
SAL_WARN( "sfx.doc", "IllegalTypeException" );
}
catch( IllegalArgumentException& ) {
SAL_WARN( "sfx.doc", "IllegalArgumentException" );
}
}
}
// To ensure a reloctable office installation, the path to the
// office installation directory must never be stored directly.
if ( SfxURLRelocator_Impl::propertyCanContainOfficeDir( rPropName ) )
{
OUString aValue;
if ( rPropValue >>= aValue )
{
maRelocator.makeRelocatableURL( aValue );
aPropValue <<= aValue;
}
else
{
Sequence< OUString > aValues;
if ( rPropValue >>= aValues )
{
for ( auto& rValue : aValues )
{
maRelocator.makeRelocatableURL( rValue );
}
aPropValue <<= aValues;
}
else
{
OSL_FAIL( "Unsupported property value type" );
}
}
}
// now set the property
rContent.setPropertyValue( rPropName, aPropValue );
bPropertySet = true;
}
catch ( RuntimeException& ) {}
catch ( Exception& ) {}
return bPropertySet;
}
bool SfxDocTplService_Impl::getProperty(Content& rContent, const OUString& rPropName, Any& 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
rPropValue = rContent.getPropertyValue( rPropName );
// To ensure a reloctable office installation, the path to the
// office installation directory must never be stored directly.
if ( SfxURLRelocator_Impl::propertyCanContainOfficeDir( rPropName ) )
{
OUString aValue;
if ( rPropValue >>= aValue )
{
maRelocator.makeAbsoluteURL( aValue );
rPropValue <<= aValue;
}
else
{
Sequence< OUString > aValues;
if ( rPropValue >>= aValues )
{
for ( auto& rValue : aValues )
{
maRelocator.makeAbsoluteURL( rValue );
}
rPropValue <<= aValues;
}
else
{
OSL_FAIL( "Unsupported property value type" );
}
}
}
bGotProperty = true;
}
catch ( RuntimeException& ) {}
catch ( Exception& ) {}
return bGotProperty;
}
SfxDocTplService_Impl::SfxDocTplService_Impl( const uno::Reference< XComponentContext > & xContext )
: mxContext(xContext), mbIsInitialized(false), mbLocaleSet(false), maRelocator(xContext)
{
}
SfxDocTplService_Impl::~SfxDocTplService_Impl()
{
::osl::MutexGuard aGuard( maMutex );
maNames.clear();
}
lang::Locale SfxDocTplService_Impl::getLocale()
{
::osl::MutexGuard aGuard( maMutex );
if ( !mbLocaleSet )
getDefaultLocale();
return maLocale;
}
void SfxDocTplService_Impl::setLocale( const lang::Locale &rLocale )
{
::osl::MutexGuard aGuard( maMutex );
if ( mbLocaleSet && (
( maLocale.Language != rLocale.Language ) ||
( maLocale.Country != rLocale.Country ) ||
( maLocale.Variant != rLocale.Variant ) ) )
mbIsInitialized = false;
maLocale = rLocale;
mbLocaleSet = true;
}
void SfxDocTplService_Impl::update()
{
::osl::MutexGuard aGuard( maMutex );
doUpdate();
}
void SfxDocTplService_Impl::doUpdate()
{
::osl::MutexGuard aGuard( maMutex );
const OUString aPropName( PROPERTY_NEEDSUPDATE );
Any aValue;
aValue <<= true;
setProperty( maRootContent, aPropName, aValue );
GroupList_Impl aGroupList;
// get the entries from the hierarchy
createFromContent( aGroupList, maRootContent, true, false );
// get the entries from the template directories
sal_Int32 nCountDir = maTemplateDirs.getLength();
OUString* pDirs = maTemplateDirs.getArray();
Content aDirContent;
// the last directory in the list must be writable
bool bWriteableDirectory = true;
// the target folder might not exist, for this reason no interaction handler should be used
uno::Reference< XCommandEnvironment > aQuietEnv;
while ( nCountDir )
{
nCountDir--;
if ( Content::create( pDirs[ nCountDir ], aQuietEnv, comphelper::getProcessComponentContext(), aDirContent ) )
{
createFromContent( aGroupList, aDirContent, false, bWriteableDirectory );
}
bWriteableDirectory = false;
}
// now check the list
for(std::unique_ptr<GroupData_Impl>& pGroup : aGroupList)
{
if ( pGroup->getInUse() )
{
if ( pGroup->getInHierarchy() )
{
Content aGroup;
if ( Content::create( pGroup->getHierarchyURL(), maCmdEnv, comphelper::getProcessComponentContext(), aGroup ) )
setProperty( aGroup,
TARGET_DIR_URL,
makeAny( pGroup->getTargetURL() ) );
size_t nCount = pGroup->count();
for ( size_t i=0; i<nCount; i++ )
{
DocTemplates_EntryData_Impl *pData = pGroup->getEntry( i );
if ( ! pData->getInUse() )
{
if ( pData->getInHierarchy() )
removeFromHierarchy( pData ); // delete entry in hierarchy
else
addToHierarchy( pGroup.get(), pData ); // add entry to hierarchy
}
else if ( pData->getUpdateType() ||
pData->getUpdateLink() )
{
updateData( pData );
}
}
}
else
{
addGroupToHierarchy( pGroup.get() ); // add group to hierarchy
}
}
else
removeFromHierarchy( pGroup.get() ); // delete group from hierarchy
}
aGroupList.clear();
aValue <<= false;
setProperty( maRootContent, aPropName, aValue );
}
std::vector< beans::StringPair > SfxDocTplService_Impl::ReadUINamesForTemplateDir_Impl( const OUString& aUserPath )
{
INetURLObject aLocObj( aUserPath );
aLocObj.insertName( "groupuinames.xml", false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
Content aLocContent;
// TODO/LATER: Use hashmap in future
std::vector< beans::StringPair > aUINames;
if ( Content::create( aLocObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), uno::Reference < ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext(), aLocContent ) )
{
try
{
uno::Reference< io::XInputStream > xLocStream = aLocContent.openStream();
if ( xLocStream.is() )
aUINames = DocTemplLocaleHelper::ReadGroupLocalizationSequence( xLocStream, mxContext );
}
catch( uno::Exception& )
{}
}
return aUINames;
}
bool SfxDocTplService_Impl::UpdateUINamesForTemplateDir_Impl( const OUString& aUserPath,
const OUString& aGroupName,
const OUString& aNewFolderName )
{
std::vector< beans::StringPair > aUINames = ReadUINamesForTemplateDir_Impl( aUserPath );
sal_Int32 nLen = aUINames.size();
// it is possible that the name is used already, but it should be checked before
for ( sal_Int32 nInd = 0; nInd < nLen; nInd++ )
if ( aUINames[nInd].First == aNewFolderName )
return false;
aUINames.resize( ++nLen );
aUINames[nLen-1].First = aNewFolderName;
aUINames[nLen-1].Second = aGroupName;
return WriteUINamesForTemplateDir_Impl( aUserPath, aUINames );
}
bool SfxDocTplService_Impl::ReplaceUINamesForTemplateDir_Impl( const OUString& aUserPath,
const OUString& aDefaultFsysGroupName,
const OUString& aOldGroupName,
const OUString& aNewGroupName )
{
std::vector< beans::StringPair > aUINames = ReadUINamesForTemplateDir_Impl( aUserPath );
sal_Int32 nLen = aUINames.size();
bool bChanged = false;
for ( sal_Int32 nInd = 0; nInd < nLen; nInd++ )
if ( aUINames[nInd].Second == aOldGroupName )
{
aUINames[nInd].Second = aNewGroupName;
bChanged = true;
}
if ( !bChanged )
{
aUINames.resize( ++nLen );
aUINames[nLen-1].First = aDefaultFsysGroupName;
aUINames[nLen-1].Second = aNewGroupName;
}
return WriteUINamesForTemplateDir_Impl( aUserPath, aUINames );
}
void SfxDocTplService_Impl::RemoveUINamesForTemplateDir_Impl( const OUString& aUserPath,
const OUString& aGroupName )
{
std::vector< beans::StringPair > aUINames = ReadUINamesForTemplateDir_Impl( aUserPath );
sal_Int32 nLen = aUINames.size();
std::vector< beans::StringPair > aNewUINames( nLen );
sal_Int32 nNewLen = 0;
bool bChanged = false;
for ( sal_Int32 nInd = 0; nInd < nLen; nInd++ )
if ( aUINames[nInd].Second == aGroupName )
bChanged = true;
else
{
nNewLen++;
aNewUINames[nNewLen-1].First = aUINames[nInd].First;
aNewUINames[nNewLen-1].Second = aUINames[nInd].Second;
}
aNewUINames.resize( nNewLen );
if (bChanged)
WriteUINamesForTemplateDir_Impl( aUserPath, aNewUINames );
}
bool SfxDocTplService_Impl::WriteUINamesForTemplateDir_Impl( const OUString& aUserPath,
const std::vector< beans::StringPair >& aUINames )
{
bool bResult = false;
try {
uno::Reference< beans::XPropertySet > xTempFile(
io::TempFile::create(mxContext),
uno::UNO_QUERY_THROW );
OUString aTempURL;
uno::Any aUrl = xTempFile->getPropertyValue("Uri");
aUrl >>= aTempURL;
uno::Reference< io::XStream > xStream( xTempFile, uno::UNO_QUERY_THROW );
uno::Reference< io::XOutputStream > xOutStream = xStream->getOutputStream();
if ( !xOutStream.is() )
throw uno::RuntimeException();
DocTemplLocaleHelper::WriteGroupLocalizationSequence( xOutStream, aUINames, mxContext);
try {
// the SAX writer might close the stream
xOutStream->closeOutput();
} catch( uno::Exception& )
{}
Content aTargetContent( aUserPath, maCmdEnv, comphelper::getProcessComponentContext() );
Content aSourceContent( aTempURL, maCmdEnv, comphelper::getProcessComponentContext() );
aTargetContent.transferContent( aSourceContent,
InsertOperation::Copy,
"groupuinames.xml",
ucb::NameClash::OVERWRITE,
"text/xml" );
bResult = true;
}
catch ( uno::Exception& )
{
}
return bResult;
}
OUString SfxDocTplService_Impl::CreateNewGroupFsys( const OUString& rGroupName, Content& aGroup )
{
OUString aResultURL;
if ( maTemplateDirs.hasElements() )
{
OUString aTargetPath = maTemplateDirs[ maTemplateDirs.getLength() - 1 ];
// create a new folder with the given name
Content aNewFolder;
OUString aNewFolderName;
// the Fsys name instead of GroupName should be used, the groupuinames must be added also
if ( !CreateNewUniqueFolderWithPrefix( aTargetPath,
rGroupName,
aNewFolderName,
aResultURL,
aNewFolder )
&& !CreateNewUniqueFolderWithPrefix( aTargetPath,
"UserGroup",
aNewFolderName,
aResultURL,
aNewFolder ) )
return OUString();
if ( !UpdateUINamesForTemplateDir_Impl( aTargetPath, rGroupName, aNewFolderName ) )
{
// we could not create the groupuinames for the folder, so we delete the group in
// the folder and return
removeContent( aNewFolder );
return OUString();
}
// Now set the target url for this group and we are done
Any aValue = makeAny( aResultURL );
if ( ! setProperty( aGroup, TARGET_DIR_URL, aValue ) )
{
removeContent( aNewFolder );
return OUString();
}
}
return aResultURL;
}
bool SfxDocTplService_Impl::addGroup( const OUString& rGroupName )
{
::osl::MutexGuard aGuard( maMutex );
// Check, whether or not there is a group with this name
Content aNewGroup;
OUString aNewGroupURL;
INetURLObject aNewGroupObj( maRootURL );
aNewGroupObj.insertName( rGroupName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
aNewGroupURL = aNewGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
if ( Content::create( aNewGroupURL, maCmdEnv, comphelper::getProcessComponentContext(), aNewGroup ) ||
! createFolder( aNewGroupURL, false, false, aNewGroup ) )
{
// if there already was a group with this name or the new group
// could not be created, we return here
return false;
}
// Get the user template path entry ( new group will always
// be added in the user template path )
sal_Int32 nIndex;
OUString aUserPath;
nIndex = maTemplateDirs.getLength();
if ( nIndex )
nIndex--;
else
return false; // We don't know where to add the group
aUserPath = maTemplateDirs[ nIndex ];
// create a new folder with the given name
Content aNewFolder;
OUString aNewFolderName;
OUString aNewFolderURL;
// the Fsys name instead of GroupName should be used, the groupuinames must be added also
if ( !CreateNewUniqueFolderWithPrefix( aUserPath,
rGroupName,
aNewFolderName,
aNewFolderURL,
aNewFolder )
&& !CreateNewUniqueFolderWithPrefix( aUserPath,
"UserGroup",
aNewFolderName,
aNewFolderURL,
aNewFolder ) )
{
// we could not create the folder, so we delete the group in the
// hierarchy and return
removeContent( aNewGroup );
return false;
}
if ( !UpdateUINamesForTemplateDir_Impl( aUserPath, rGroupName, aNewFolderName ) )
{
// we could not create the groupuinames for the folder, so we delete the group in the
// hierarchy, the folder and return
removeContent( aNewGroup );
removeContent( aNewFolder );
return false;
}
// Now set the target url for this group and we are done
Any aValue = makeAny( aNewFolderURL );
if ( ! setProperty( aNewGroup, TARGET_DIR_URL, aValue ) )
{
removeContent( aNewGroup );
removeContent( aNewFolder );
return false;
}
return true;
}
bool SfxDocTplService_Impl::removeGroup( const OUString& rGroupName )
{
// remove all the elements that have the prefix aTargetURL
// if the group does not have other elements remove it
::osl::MutexGuard aGuard( maMutex );
bool bResult = false;
// create the group url
INetURLObject aGroupObj( maRootURL );
aGroupObj.insertName( rGroupName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
// Get the target url
Content aGroup;
const OUString aGroupURL = aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
if ( Content::create( aGroupURL, maCmdEnv, comphelper::getProcessComponentContext(), aGroup ) )
{
const OUString aPropName( TARGET_DIR_URL );
Any aValue;
OUString aGroupTargetURL;
if ( getProperty( aGroup, aPropName, aValue ) )
aValue >>= aGroupTargetURL;
if ( aGroupTargetURL.isEmpty() )
return false; // nothing is allowed to be removed
if ( !maTemplateDirs.hasElements() )
return false;
// check that the fs location is in writable folder and this is not a "My templates" folder
INetURLObject aGroupParentFolder( aGroupTargetURL );
if (!aGroupParentFolder.removeSegment())
return false;
OUString aGeneralTempPath = findParentTemplateDir(
aGroupParentFolder.GetMainURL(INetURLObject::DecodeMechanism::NONE));
if (aGeneralTempPath.isEmpty())
return false;
// now get the content of the Group
uno::Reference< XResultSet > xResultSet;
Sequence< OUString > aProps { TARGET_URL };
try
{
xResultSet = aGroup.createCursor( aProps, INCLUDE_DOCUMENTS_ONLY );
if ( xResultSet.is() )
{
bool bHasNonRemovable = false;
bool bHasShared = false;
uno::Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY_THROW );
uno::Reference< XRow > xRow( xResultSet, UNO_QUERY_THROW );
while ( xResultSet->next() )
{
OUString aTemplTargetURL( xRow->getString( 1 ) );
OUString aHierURL = xContentAccess->queryContentIdentifierString();
if ( ::utl::UCBContentHelper::IsSubPath( aGroupTargetURL, aTemplTargetURL ) )
{
// this is a user template, and it can be removed
if ( removeContent( aTemplTargetURL ) )
removeContent( aHierURL );
else
bHasNonRemovable = true;
}
else
bHasShared = true;
}
if ( !bHasNonRemovable && !bHasShared )
{
if ( removeContent( aGroupTargetURL )
|| !::utl::UCBContentHelper::Exists( aGroupTargetURL ) )
{
removeContent( aGroupURL );
RemoveUINamesForTemplateDir_Impl( aGeneralTempPath, rGroupName );
bResult = true; // the operation is successful only if the whole group is removed
}
}
else if ( !bHasNonRemovable )
{
if ( removeContent( aGroupTargetURL )
|| !::utl::UCBContentHelper::Exists( aGroupTargetURL ) )
{
RemoveUINamesForTemplateDir_Impl( aGeneralTempPath, rGroupName );
setProperty( aGroup, aPropName, uno::makeAny( OUString() ) );
}
}
}
}
catch ( Exception& ) {}
}
return bResult;
}
bool SfxDocTplService_Impl::renameGroup( const OUString& rOldName,
const OUString& rNewName )
{
::osl::MutexGuard aGuard( maMutex );
// create the group url
Content aGroup;
INetURLObject aGroupObj( maRootURL );
aGroupObj.insertName( rNewName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
OUString aGroupURL = aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
// Check, if there is a group with the new name, return false
// if there is one.
if ( Content::create( aGroupURL, maCmdEnv, comphelper::getProcessComponentContext(), aGroup ) )
return false;
aGroupObj.removeSegment();
aGroupObj.insertName( rOldName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
aGroupURL = aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
// When there is no group with the old name, we can't rename it
if ( ! Content::create( aGroupURL, maCmdEnv, comphelper::getProcessComponentContext(), aGroup ) )
return false;
OUString aGroupTargetURL;
// there is no need to check whether target dir url is in target path, since if the target path is changed
// the target dir url should be already generated new
Any aValue;
if ( getProperty( aGroup, TARGET_DIR_URL, aValue ) )
aValue >>= aGroupTargetURL;
if ( aGroupTargetURL.isEmpty() )
return false;
if ( !maTemplateDirs.hasElements() )
return false;
// check that the fs location is in writable folder and this is not a "My templates" folder
INetURLObject aGroupParentFolder( aGroupTargetURL );
if (!aGroupParentFolder.removeSegment() ||
isInternalTemplateDir(aGroupParentFolder.GetMainURL(INetURLObject::DecodeMechanism::NONE)))
{
return false;
}
// check that the group can be renamed ( all the contents must be in target location )
bool bCanBeRenamed = false;
try
{
uno::Reference< XResultSet > xResultSet;
Sequence< OUString > aProps { TARGET_URL };
xResultSet = aGroup.createCursor( aProps, INCLUDE_DOCUMENTS_ONLY );
if ( xResultSet.is() )
{
uno::Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY_THROW );
uno::Reference< XRow > xRow( xResultSet, UNO_QUERY_THROW );
while ( xResultSet->next() )
{
if ( !::utl::UCBContentHelper::IsSubPath( aGroupTargetURL, xRow->getString( 1 ) ) )
throw uno::Exception("not sub path", nullptr);
}
bCanBeRenamed = true;
}
}
catch ( Exception& ) {}
if ( bCanBeRenamed )
{
INetURLObject aGroupTargetObj( aGroupTargetURL );
const OUString aFsysName = aGroupTargetObj.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset );
if ( aGroupTargetObj.removeSegment()
&& ReplaceUINamesForTemplateDir_Impl( aGroupTargetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ),
aFsysName,
rOldName,
rNewName ) )
{
// rename the group in the hierarchy
Any aTitleValue;
aTitleValue <<= rNewName;
return setProperty( aGroup, TITLE, aTitleValue );
}
}
return false;
}
bool SfxDocTplService_Impl::storeTemplate( const OUString& rGroupName,
const OUString& rTemplateName,
const uno::Reference< frame::XStorable >& rStorable )
{
::osl::MutexGuard aGuard( maMutex );
// Check, whether or not there is a group with this name
// Return false, if there is no group with the given name
Content aGroup, aTemplateToRemove;
INetURLObject aGroupObj( maRootURL );
bool bRemoveOldTemplateContent = false;
aGroupObj.insertName( rGroupName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aGroupURL {aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE )};
if ( ! Content::create( aGroupURL, maCmdEnv, comphelper::getProcessComponentContext(), aGroup ) )
return false;
OUString aGroupTargetURL;
Any aValue;
if ( getProperty( aGroup, TARGET_DIR_URL, aValue ) )
aValue >>= aGroupTargetURL;
// Check, if there's a template with the given name in this group
// the target template should be overwritten if it is imported by user
// in case the template is installed by office installation of by an add-in
// it can not be replaced
aGroupObj.insertName( rTemplateName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aTemplateURL {aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE )};
OUString aTemplateToRemoveTargetURL;
if ( Content::create( aTemplateURL, maCmdEnv, comphelper::getProcessComponentContext(), aTemplateToRemove ) )
{
bRemoveOldTemplateContent = true;
if ( getProperty( aTemplateToRemove, TARGET_URL, aValue ) )
aValue >>= aTemplateToRemoveTargetURL;
if ( aGroupTargetURL.isEmpty() || !maTemplateDirs.hasElements()
|| (!aTemplateToRemoveTargetURL.isEmpty() && isInternalTemplateDir(aTemplateToRemoveTargetURL)) )
return false; // it is not allowed to remove the template
}
try
{
uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
// get document service name
uno::Reference< frame::XModuleManager2 > xModuleManager( frame::ModuleManager::create(xContext) );
const OUString sDocServiceName {xModuleManager->identify( uno::Reference< uno::XInterface >( rStorable, uno::UNO_QUERY ) )};
if ( sDocServiceName.isEmpty() )
throw uno::RuntimeException();
// get the actual filter name
uno::Reference< lang::XMultiServiceFactory > xConfigProvider =
configuration::theDefaultProvider::get( xContext );
uno::Sequence<uno::Any> aArgs(comphelper::InitAnyPropertySequence(
{
{"nodepath", uno::Any(OUString( "/org.openoffice.Setup/Office/Factories/" ))}
}));
uno::Reference< container::XNameAccess > xSOFConfig(
xConfigProvider->createInstanceWithArguments(
"com.sun.star.configuration.ConfigurationAccess",
aArgs ),
uno::UNO_QUERY_THROW );
uno::Reference< container::XNameAccess > xApplConfig;
xSOFConfig->getByName( sDocServiceName ) >>= xApplConfig;
if ( !xApplConfig.is() )
throw uno::RuntimeException();
OUString aFilterName;
xApplConfig->getByName("ooSetupFactoryActualTemplateFilter") >>= aFilterName;
if ( aFilterName.isEmpty() )
throw uno::RuntimeException();
// find the related type name
uno::Reference< container::XNameAccess > xFilterFactory(
mxContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.FilterFactory", mxContext),
uno::UNO_QUERY_THROW );
uno::Sequence< beans::PropertyValue > aFilterData;
xFilterFactory->getByName( aFilterName ) >>= aFilterData;
OUString aTypeName;
for ( const auto& rProp : std::as_const(aFilterData) )
if ( rProp.Name == "Type" )
rProp.Value >>= aTypeName;
if ( aTypeName.isEmpty() )
throw uno::RuntimeException();
// find the mediatype and extension
uno::Reference< container::XNameAccess > xTypeDetection =
mxType.is() ?
uno::Reference< container::XNameAccess >( mxType, uno::UNO_QUERY_THROW ) :
uno::Reference< container::XNameAccess >(
mxContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", mxContext),
uno::UNO_QUERY_THROW );
SequenceAsHashMap aTypeProps( xTypeDetection->getByName( aTypeName ) );
uno::Sequence< OUString > aAllExt =
aTypeProps.getUnpackedValueOrDefault("Extensions", Sequence< OUString >() );
if ( !aAllExt.hasElements() )
throw uno::RuntimeException();
const OUString aMediaType {aTypeProps.getUnpackedValueOrDefault("MediaType", OUString() )};
const OUString aExt {aAllExt[0]};
if ( aMediaType.isEmpty() || aExt.isEmpty() )
throw uno::RuntimeException();
// construct destination url
if ( aGroupTargetURL.isEmpty() )
{
aGroupTargetURL = CreateNewGroupFsys( rGroupName, aGroup );
if ( aGroupTargetURL.isEmpty() )
throw uno::RuntimeException();
}
OUString aNewTemplateTargetURL = CreateNewUniqueFileWithPrefix( aGroupTargetURL, rTemplateName, aExt );
if ( aNewTemplateTargetURL.isEmpty() )
{
aNewTemplateTargetURL = CreateNewUniqueFileWithPrefix( aGroupTargetURL, "UserTemplate", aExt );
if ( aNewTemplateTargetURL.isEmpty() )
throw uno::RuntimeException();
}
// store template
uno::Sequence< PropertyValue > aStoreArgs( 2 );
aStoreArgs[0].Name = "FilterName";
aStoreArgs[0].Value <<= aFilterName;
aStoreArgs[1].Name = "DocumentTitle";
aStoreArgs[1].Value <<= rTemplateName;
if( !::utl::UCBContentHelper::EqualURLs( aNewTemplateTargetURL, rStorable->getLocation() ))
rStorable->storeToURL( aNewTemplateTargetURL, aStoreArgs );
else
rStorable->store();
// the storing was successful, now the old template with the same name can be removed if it existed
if ( !aTemplateToRemoveTargetURL.isEmpty() )
{
removeContent( aTemplateToRemoveTargetURL );
/*
* pb: #i79496#
* if the old template was the standard template
* it is necessary to change the standard template with the new file name
*/
const OUString sStdTmplFile = SfxObjectFactory::GetStandardTemplate( sDocServiceName );
if ( INetURLObject( sStdTmplFile ) == INetURLObject( aTemplateToRemoveTargetURL ) )
{
SfxObjectFactory::SetStandardTemplate( sDocServiceName, aNewTemplateTargetURL );
}
}
if ( bRemoveOldTemplateContent )
removeContent( aTemplateToRemove );
// add the template to hierarchy
return addEntry( aGroup, rTemplateName, aNewTemplateTargetURL, aMediaType );
}
catch( Exception& )
{
// the template was not stored
return false;
}
}
bool SfxDocTplService_Impl::addTemplate( const OUString& rGroupName,
const OUString& rTemplateName,
const OUString& rSourceURL )
{
::osl::MutexGuard aGuard( maMutex );
// Check, whether or not there is a group with this name
// Return false, if there is no group with the given name
Content aGroup, aTemplate, aTargetGroup;
INetURLObject aGroupObj( maRootURL );
aGroupObj.insertName( rGroupName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aGroupURL = aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
if ( ! Content::create( aGroupURL, maCmdEnv, comphelper::getProcessComponentContext(), aGroup ) )
return false;
// Check, if there's a template with the given name in this group
// Return false, if there already is a template
aGroupObj.insertName( rTemplateName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aTemplateURL {aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE )};
if ( Content::create( aTemplateURL, maCmdEnv, comphelper::getProcessComponentContext(), aTemplate ) )
return false;
// get the target url of the group
OUString aTargetURL;
Any aValue;
if ( getProperty( aGroup, TARGET_DIR_URL, aValue ) )
aValue >>= aTargetURL;
if ( aTargetURL.isEmpty() )
{
aTargetURL = CreateNewGroupFsys( rGroupName, aGroup );
if ( aTargetURL.isEmpty() )
return false;
}
// Get the content type
OUString aTitle, aType;
bool bDocHasTitle = false;
getTitleFromURL( rSourceURL, aTitle, aType, bDocHasTitle );
INetURLObject aSourceObj( rSourceURL );
if ( rTemplateName == aTitle )
{
// addTemplate will sometimes be called just to add an entry in the
// hierarchy; the target URL and the source URL will be the same in
// this scenario
// TODO/LATER: get rid of this old hack
INetURLObject aTargetObj( aTargetURL );
aTargetObj.insertName( rTemplateName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
aTargetObj.setExtension( aSourceObj.getExtension() );
const OUString aTargetURL2 = aTargetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
if ( aTargetURL2 == rSourceURL )
return addEntry( aGroup, rTemplateName, aTargetURL2, aType );
}
// copy the template into the new group (targeturl)
INetURLObject aTmpURL( aSourceObj );
aTmpURL.CutExtension();
const OUString aPattern {aTmpURL.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset )};
const OUString aNewTemplateTargetURL {CreateNewUniqueFileWithPrefix( aTargetURL, aPattern, aSourceObj.getExtension() )};
INetURLObject aNewTemplateTargetObj( aNewTemplateTargetURL );
const OUString aNewTemplateTargetName {aNewTemplateTargetObj.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset )};
if ( aNewTemplateTargetURL.isEmpty() || aNewTemplateTargetName.isEmpty() )
return false;
// get access to source file
Content aSourceContent;
uno::Reference < ucb::XCommandEnvironment > xEnv;
INetURLObject aSourceURL( rSourceURL );
if( ! Content::create( aSourceURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), xEnv, comphelper::getProcessComponentContext(), aSourceContent ) )
return false;
if( ! Content::create( aTargetURL, xEnv, comphelper::getProcessComponentContext(), aTargetGroup ) )
return false;
// transfer source file
try
{
aTargetGroup.transferContent( aSourceContent,
InsertOperation::Copy,
aNewTemplateTargetName,
NameClash::OVERWRITE,
aType );
// allow to edit the added template
Content aResultContent;
if ( Content::create( aNewTemplateTargetURL, xEnv, comphelper::getProcessComponentContext(), aResultContent ) )
{
const OUString aPropertyName( "IsReadOnly" );
uno::Any aProperty;
bool bReadOnly = false;<--- Assignment 'bReadOnly=false', assigned value is 0
if ( getProperty( aResultContent, aPropertyName, aProperty ) && ( aProperty >>= bReadOnly ) && bReadOnly )<--- Condition 'bReadOnly' is always false
setProperty( aResultContent, aPropertyName, uno::makeAny( false ) );
}
}
catch ( ContentCreationException& )
{ return false; }
catch ( Exception& )
{ return false; }
// either the document has title and it is the same as requested, or we have to set it
bool bCorrectTitle = ( bDocHasTitle && aTitle == rTemplateName );
if ( !bCorrectTitle )
{
if ( !bDocHasTitle )
{
INetURLObject aNewTmpObj( aNewTemplateTargetObj );
aNewTmpObj.CutExtension();
bCorrectTitle = ( aNewTmpObj.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset ) == rTemplateName );
}
if ( !bCorrectTitle )
bCorrectTitle = setTitleForURL( aNewTemplateTargetURL, rTemplateName );
}
if ( bCorrectTitle )
{
// create a new entry in the hierarchy
return addEntry( aGroup, rTemplateName, aNewTemplateTargetURL, aType );
}
// TODO/LATER: The user could be notified here that the renaming has failed
// create a new entry in the hierarchy
addEntry( aGroup, aTitle, aNewTemplateTargetURL, aType );
return false;
}
bool SfxDocTplService_Impl::isInternalTemplateDir(const OUString& rURL) const
{
return std::any_of(maInternalTemplateDirs.begin(), maInternalTemplateDirs.end(),
[&rURL](const OUString& rDir) { return ::utl::UCBContentHelper::IsSubPath(rDir, rURL); });
}
OUString SfxDocTplService_Impl::findParentTemplateDir(const OUString& rURL) const
{
const OUString* pDirs = std::find_if(maTemplateDirs.begin(), maTemplateDirs.end(),
[&rURL](const OUString& rDir) { return ::utl::UCBContentHelper::IsSubPath(rDir, rURL); });
if (pDirs != maTemplateDirs.end())
return *pDirs;
return OUString();
}
bool SfxDocTplService_Impl::removeTemplate( const OUString& rGroupName,
const OUString& rTemplateName )
{
::osl::MutexGuard aGuard( maMutex );
// Check, whether or not there is a group with this name
// Return false, if there is no group with the given name
Content aGroup, aTemplate;
INetURLObject aGroupObj( maRootURL );
aGroupObj.insertName( rGroupName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aGroupURL {aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE )};
if ( ! Content::create( aGroupURL, maCmdEnv, comphelper::getProcessComponentContext(), aGroup ) )
return false;
// Check, if there's a template with the given name in this group
// Return false, if there is no template
aGroupObj.insertName( rTemplateName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aTemplateURL {aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE )};
if ( !Content::create( aTemplateURL, maCmdEnv, comphelper::getProcessComponentContext(), aTemplate ) )
return false;
// get the target URL from the template
OUString aTargetURL;
Any aValue;
if ( getProperty( aTemplate, TARGET_URL, aValue ) )
aValue >>= aTargetURL;
// delete the target template
if ( !aTargetURL.isEmpty() )
{
if (isInternalTemplateDir(aTargetURL))
return false;
removeContent( aTargetURL );
}
// delete the template entry
return removeContent( aTemplate );
}
bool SfxDocTplService_Impl::renameTemplate( const OUString& rGroupName,
const OUString& rOldName,
const OUString& rNewName )
{
::osl::MutexGuard aGuard( maMutex );
// Check, whether or not there is a group with this name
// Return false, if there is no group with the given name
Content aGroup, aTemplate;
INetURLObject aGroupObj( maRootURL );
aGroupObj.insertName( rGroupName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aGroupURL {aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE )};
if ( ! Content::create( aGroupURL, maCmdEnv, comphelper::getProcessComponentContext(), aGroup ) )
return false;
// Check, if there's a template with the new name in this group
// Return false, if there is one
aGroupObj.insertName( rNewName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
OUString aTemplateURL {aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE )};
if ( Content::create( aTemplateURL, maCmdEnv, comphelper::getProcessComponentContext(), aTemplate ) )
return false;
// Check, if there's a template with the old name in this group
// Return false, if there is no template
aGroupObj.removeSegment();
aGroupObj.insertName( rOldName, false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
aTemplateURL = aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
if ( !Content::create( aTemplateURL, maCmdEnv, comphelper::getProcessComponentContext(), aTemplate ) )
return false;
OUString aTemplateTargetURL;
Any aTargetValue;
if ( getProperty( aTemplate, TARGET_URL, aTargetValue ) )
aTargetValue >>= aTemplateTargetURL;
if ( !setTitleForURL( aTemplateTargetURL, rNewName ) )
return false;
// rename the template entry in the cache
Any aTitleValue;
aTitleValue <<= rNewName;
return setProperty( aTemplate, TITLE, aTitleValue );
}
class SfxDocTplService: public ::cppu::WeakImplHelper< css::lang::XLocalizable, css::frame::XDocumentTemplates, css::lang::XServiceInfo >
{
std::unique_ptr<SfxDocTplService_Impl> pImp;
public:
explicit SfxDocTplService( const css::uno::Reference < uno::XComponentContext >& xContext );
virtual OUString SAL_CALL getImplementationName() override
{
return "com.sun.star.comp.sfx2.DocumentTemplates";
}
virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
{
return cppu::supportsService(this, ServiceName);
}
virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
{
css::uno::Sequence< OUString > aSeq { "com.sun.star.frame.DocumentTemplates" };
return aSeq;
}
// --- XLocalizable ---
void SAL_CALL setLocale( const css::lang::Locale & eLocale ) override;
css::lang::Locale SAL_CALL getLocale() override;
// --- XDocumentTemplates ---
css::uno::Reference< css::ucb::XContent > SAL_CALL getContent() override;
sal_Bool SAL_CALL storeTemplate( const OUString& GroupName,
const OUString& TemplateName,
const css::uno::Reference< css::frame::XStorable >& Storable ) override;
sal_Bool SAL_CALL addTemplate( const OUString& GroupName,
const OUString& TemplateName,
const OUString& SourceURL ) override;
sal_Bool SAL_CALL removeTemplate( const OUString& GroupName,
const OUString& TemplateName ) override;
sal_Bool SAL_CALL renameTemplate( const OUString& GroupName,
const OUString& OldTemplateName,
const OUString& NewTemplateName ) override;
sal_Bool SAL_CALL addGroup( const OUString& GroupName ) override;
sal_Bool SAL_CALL removeGroup( const OUString& GroupName ) override;
sal_Bool SAL_CALL renameGroup( const OUString& OldGroupName,
const OUString& NewGroupName ) override;
void SAL_CALL update() override;
};
SfxDocTplService::SfxDocTplService( const uno::Reference< XComponentContext >& xContext )
{
pImp.reset( new SfxDocTplService_Impl(xContext) );
}
//--- XLocalizable ---
lang::Locale SAL_CALL SfxDocTplService::getLocale()
{
return pImp->getLocale();
}
void SAL_CALL SfxDocTplService::setLocale( const lang::Locale & rLocale )
{
pImp->setLocale( rLocale );
}
//--- XDocumentTemplates ---
uno::Reference< ucb::XContent > SAL_CALL SfxDocTplService::getContent()
{
if ( pImp->init() )
return pImp->getContent().get();
return nullptr;
}
sal_Bool SAL_CALL SfxDocTplService::storeTemplate( const OUString& GroupName,
const OUString& TemplateName,
const uno::Reference< frame::XStorable >& Storable )
{
return pImp->init() && pImp->storeTemplate( GroupName, TemplateName, Storable );
}
sal_Bool SAL_CALL SfxDocTplService::addTemplate( const OUString& rGroupName,
const OUString& rTemplateName,
const OUString& rSourceURL )
{
return pImp->init() && pImp->addTemplate( rGroupName, rTemplateName, rSourceURL );
}
sal_Bool SAL_CALL SfxDocTplService::removeTemplate( const OUString& rGroupName,
const OUString& rTemplateName )
{
return pImp->init() && pImp->removeTemplate( rGroupName, rTemplateName );
}
sal_Bool SAL_CALL SfxDocTplService::renameTemplate( const OUString& rGroupName,
const OUString& rOldName,
const OUString& rNewName )
{
if ( rOldName == rNewName )
return true;
return pImp->init() && pImp->renameTemplate( rGroupName, rOldName, rNewName );
}
sal_Bool SAL_CALL SfxDocTplService::addGroup( const OUString& rGroupName )
{
return pImp->init() && pImp->addGroup( rGroupName );
}
sal_Bool SAL_CALL SfxDocTplService::removeGroup( const OUString& rGroupName )
{
return pImp->init() && pImp->removeGroup( rGroupName );
}
sal_Bool SAL_CALL SfxDocTplService::renameGroup( const OUString& rOldName,
const OUString& rNewName )
{
if ( rOldName == rNewName )
return true;
return pImp->init() && pImp->renameGroup( rOldName, rNewName );
}
void SAL_CALL SfxDocTplService::update()
{
if ( pImp->init() )
pImp->update();
}
WaitWindow_Impl::WaitWindow_Impl() : WorkWindow(nullptr, WB_BORDER | WB_3DLOOK)
{
tools::Rectangle aRect(0, 0, 300, 30000);
maText = SfxResId(RID_CNT_STR_WAITING);
maRect = GetTextRect(aRect, maText, gnTextStyle);
aRect = maRect;
aRect.AdjustRight(2 * X_OFFSET );
aRect.AdjustBottom(2 * Y_OFFSET );
maRect.SetPos(Point(X_OFFSET, Y_OFFSET));
SetOutputSizePixel(aRect.GetSize());
Show();
PaintImmediately();
Flush();
}
WaitWindow_Impl::~WaitWindow_Impl()
{
disposeOnce();
}
void WaitWindow_Impl::dispose()
{
Hide();
WorkWindow::dispose();
}
void WaitWindow_Impl::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
{
rRenderContext.DrawText(maRect, maText, gnTextStyle);
}
void SfxDocTplService_Impl::addHierGroup( GroupList_Impl& rList,
const OUString& rTitle,
const OUString& rOwnURL )
{
// now get the content of the Group
Content aContent;
uno::Reference<XResultSet> xResultSet;
Sequence<OUString> aProps(3);
aProps[0] = TITLE;
aProps[1] = TARGET_URL;
aProps[2] = PROPERTY_TYPE;
try
{
aContent = Content(rOwnURL, maCmdEnv, comphelper::getProcessComponentContext());
xResultSet = aContent.createCursor( aProps, INCLUDE_DOCUMENTS_ONLY );
}
catch (ContentCreationException&)
{
SAL_WARN( "sfx.doc", "addHierGroup: ContentCreationException" );
}
catch (Exception&) {}
if ( !xResultSet.is() )
return;
GroupData_Impl *pGroup = new GroupData_Impl( rTitle );
pGroup->setHierarchy( true );
pGroup->setHierarchyURL( rOwnURL );
rList.push_back( std::unique_ptr<GroupData_Impl>(pGroup) );
uno::Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
uno::Reference< XRow > xRow( xResultSet, UNO_QUERY );
try
{
while ( xResultSet->next() )
{
bool bUpdateType = false;
DocTemplates_EntryData_Impl *pData;
const OUString aTitle( xRow->getString( 1 ) );
const OUString aTargetDir( xRow->getString( 2 ) );
OUString aType( xRow->getString( 3 ) );
const OUString aHierURL {xContentAccess->queryContentIdentifierString()};
if ( aType.isEmpty() )
{
OUString aTmpTitle;
bool bDocHasTitle = false;
getTitleFromURL( aTargetDir, aTmpTitle, aType, bDocHasTitle );
if ( !aType.isEmpty() )
bUpdateType = true;
}
pData = pGroup->addEntry( aTitle, aTargetDir, aType, aHierURL );
pData->setUpdateType( bUpdateType );
}
}
catch ( Exception& ) {}
}
void SfxDocTplService_Impl::addFsysGroup( GroupList_Impl& rList,
const OUString& rTitle,
const OUString& rUITitle,
const OUString& rOwnURL,
bool bWriteableGroup )
{
OUString aTitle;
if ( rUITitle.isEmpty() )
{
// reserved FS names that should not be used
if ( rTitle == "wizard" )
return;
else if ( rTitle == "internal" )
return;
aTitle = getLongName( rTitle );
}
else
aTitle = rUITitle;
if ( aTitle.isEmpty() )
return;
GroupData_Impl* pGroup = nullptr;
for (const std::unique_ptr<GroupData_Impl>& i : rList)
{
if ( i->getTitle() == aTitle )
{<--- Consider using std::find_if algorithm instead of a raw loop.
pGroup = i.get();
break;
}
}
if ( !pGroup )
{
pGroup = new GroupData_Impl( aTitle );
rList.push_back( std::unique_ptr<GroupData_Impl>(pGroup) );
}
if ( bWriteableGroup )
pGroup->setTargetURL( rOwnURL );
pGroup->setInUse();
// now get the content of the Group
Content aContent;
uno::Reference< XResultSet > xResultSet;
Sequence< OUString > aProps { TITLE };
try
{
// this method is only used during checking of the available template-folders
// that should happen quietly
uno::Reference< XCommandEnvironment > aQuietEnv;
aContent = Content( rOwnURL, aQuietEnv, comphelper::getProcessComponentContext() );
xResultSet = aContent.createCursor( aProps, INCLUDE_DOCUMENTS_ONLY );
}
catch ( Exception& ) {}
if ( !xResultSet.is() )
return;
uno::Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
uno::Reference< XRow > xRow( xResultSet, UNO_QUERY );
try
{
while ( xResultSet->next() )
{
OUString aChildTitle( xRow->getString( 1 ) );
const OUString aTargetURL {xContentAccess->queryContentIdentifierString()};
OUString aType;
if ( aChildTitle == "sfx.tlx" || aChildTitle == "groupuinames.xml" )
continue;
bool bDocHasTitle = false;
getTitleFromURL( aTargetURL, aChildTitle, aType, bDocHasTitle );
pGroup->addEntry( aChildTitle, aTargetURL, aType, OUString() );
}
}
catch ( Exception& ) {}
}
void SfxDocTplService_Impl::createFromContent( GroupList_Impl& rList,
Content &rContent,
bool bHierarchy,
bool bWriteableContent )
{
const OUString aTargetURL {rContent.get()->getIdentifier()->getContentIdentifier()};
// when scanning the file system, we have to add the 'standard' group, too
if ( ! bHierarchy )
{
const OUString aUIStdTitle {getLongName( STANDARD_FOLDER )};
addFsysGroup( rList, OUString(), aUIStdTitle, aTargetURL, bWriteableContent );
}
// search for predefined UI names
INetURLObject aLayerObj( aTargetURL );
// TODO/LATER: Use hashmap in future
std::vector< beans::StringPair > aUINames;
if ( !bHierarchy )
aUINames = ReadUINamesForTemplateDir_Impl( aLayerObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
uno::Reference< XResultSet > xResultSet;
Sequence< OUString > aProps { TITLE };
try
{
xResultSet = rContent.createCursor( aProps, INCLUDE_FOLDERS_ONLY );
}
catch ( Exception& ) {}
if ( !xResultSet.is() )
return;
uno::Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
uno::Reference< XRow > xRow( xResultSet, UNO_QUERY );
try
{
while ( xResultSet->next() )
{
// TODO/LATER: clarify the encoding of the Title
const OUString aTitle( xRow->getString( 1 ) );
const OUString aTargetSubfolderURL( xContentAccess->queryContentIdentifierString() );
if ( bHierarchy )
addHierGroup( rList, aTitle, aTargetSubfolderURL );
else
{
OUString aUITitle;
for (const beans::StringPair & rUIName : aUINames)
if ( rUIName.First == aTitle )
{<--- Consider using std::find_if algorithm instead of a raw loop.
aUITitle = rUIName.Second;
break;
}
addFsysGroup( rList, aTitle, aUITitle, aTargetSubfolderURL, bWriteableContent );
}
}
}
catch ( Exception& ) {}
}
void SfxDocTplService_Impl::removeFromHierarchy( DocTemplates_EntryData_Impl const *pData )
{
Content aTemplate;
if ( Content::create( pData->getHierarchyURL(), maCmdEnv, comphelper::getProcessComponentContext(), aTemplate ) )
{
removeContent( aTemplate );
}
}
void SfxDocTplService_Impl::addToHierarchy( GroupData_Impl const *pGroup,
DocTemplates_EntryData_Impl const *pData )
{
Content aGroup, aTemplate;
if ( ! Content::create( pGroup->getHierarchyURL(), maCmdEnv, comphelper::getProcessComponentContext(), aGroup ) )
return;
// Check, if there's a template with the given name in this group
// Return if there is already a template
INetURLObject aGroupObj( pGroup->getHierarchyURL() );
aGroupObj.insertName( pData->getTitle(), false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aTemplateURL {aGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE )};
if ( Content::create( aTemplateURL, maCmdEnv, comphelper::getProcessComponentContext(), aTemplate ) )
return;
addEntry( aGroup, pData->getTitle(),
pData->getTargetURL(),
pData->getType() );
}
void SfxDocTplService_Impl::updateData( DocTemplates_EntryData_Impl const *pData )
{
Content aTemplate;
if ( ! Content::create( pData->getHierarchyURL(), maCmdEnv, comphelper::getProcessComponentContext(), aTemplate ) )
return;
if ( pData->getUpdateType() )
{
setProperty( aTemplate, PROPERTY_TYPE, makeAny( pData->getType() ) );
}
if ( pData->getUpdateLink() )
{
setProperty( aTemplate, TARGET_URL, makeAny( pData->getTargetURL() ) );
}
}
void SfxDocTplService_Impl::addGroupToHierarchy( GroupData_Impl *pGroup )
{
Content aGroup;
INetURLObject aNewGroupObj( maRootURL );
aNewGroupObj.insertName( pGroup->getTitle(), false,
INetURLObject::LAST_SEGMENT,
INetURLObject::EncodeMechanism::All );
const OUString aNewGroupURL {aNewGroupObj.GetMainURL( INetURLObject::DecodeMechanism::NONE )};
if ( createFolder( aNewGroupURL, false, false, aGroup ) )
{
setProperty( aGroup, TARGET_DIR_URL, makeAny( pGroup->getTargetURL() ) );
pGroup->setHierarchyURL( aNewGroupURL );
size_t nCount = pGroup->count();
for ( size_t i = 0; i < nCount; i++ )
{
DocTemplates_EntryData_Impl *pData = pGroup->getEntry( i );
addToHierarchy( pGroup, pData ); // add entry to hierarchy
}
}
}
void SfxDocTplService_Impl::removeFromHierarchy( GroupData_Impl const *pGroup )
{
Content aGroup;
if ( Content::create( pGroup->getHierarchyURL(), maCmdEnv, comphelper::getProcessComponentContext(), aGroup ) )
{
removeContent( aGroup );
}
}
GroupData_Impl::GroupData_Impl( const OUString& rTitle )
: maTitle(rTitle), mbInUse(false), mbInHierarchy(false)
{
}
DocTemplates_EntryData_Impl* GroupData_Impl::addEntry( const OUString& rTitle,
const OUString& rTargetURL,
const OUString& rType,
const OUString& rHierURL )
{
DocTemplates_EntryData_Impl* pData = nullptr;
bool EntryFound = false;
for (auto const & p : maEntries)
{
pData = p.get();
if ( pData->getTitle() == rTitle )
{
EntryFound = true;
break;
}
}
if ( !EntryFound )
{
pData = new DocTemplates_EntryData_Impl( rTitle );
pData->setTargetURL( rTargetURL );
pData->setType( rType );
if ( !rHierURL.isEmpty() )
{
pData->setHierarchyURL( rHierURL );
pData->setHierarchy( true );
}
maEntries.emplace_back( pData );
}
else
{
if ( !rHierURL.isEmpty() )
{
pData->setHierarchyURL( rHierURL );
pData->setHierarchy( true );
}
if ( pData->getInHierarchy() )
pData->setInUse();
if ( rTargetURL != pData->getTargetURL() )
{
pData->setTargetURL( rTargetURL );
pData->setUpdateLink( true );
}
}
return pData;
}
DocTemplates_EntryData_Impl::DocTemplates_EntryData_Impl( const OUString& rTitle )
: maTitle(rTitle), mbInHierarchy(false), mbInUse(false), mbUpdateType(false), mbUpdateLink(false)
{
}
}
// static
bool SfxURLRelocator_Impl::propertyCanContainOfficeDir(
const OUString & rPropName )
{
// Note: TargetURL is handled by UCB itself (because it is a property
// with a predefined semantic). Additional Core properties introduced
// be a client app must be handled by the client app itself, because
// the UCB does not know the semantics of those properties.
return ( rPropName == TARGET_DIR_URL || rPropName == PROPERTY_DIRLIST );
}
SfxURLRelocator_Impl::SfxURLRelocator_Impl( const uno::Reference< XComponentContext > & xContext )
: mxContext( xContext )
{
}
SfxURLRelocator_Impl::~SfxURLRelocator_Impl()
{
}
void SfxURLRelocator_Impl::initOfficeInstDirs()
{
if ( !mxOfficeInstDirs.is() )
{
osl::MutexGuard aGuard( maMutex );
if ( !mxOfficeInstDirs.is() )
{
OSL_ENSURE( mxContext.is(), "No service manager!" );
mxOfficeInstDirs = theOfficeInstallationDirectories::get(mxContext);
}
}
}
void SfxURLRelocator_Impl::implExpandURL( OUString& io_url )
{
const INetURLObject aParser( io_url );
if ( aParser.GetProtocol() != INetProtocol::VndSunStarExpand )
return;
io_url = aParser.GetURLPath( INetURLObject::DecodeMechanism::WithCharset );
try
{
if ( !mxMacroExpander.is() )
{
mxMacroExpander.set( theMacroExpander::get(mxContext), UNO_SET_THROW );
}
io_url = mxMacroExpander->expandMacros( io_url );
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("sfx.doc");
}
}
void SfxURLRelocator_Impl::makeRelocatableURL( OUString & rURL )
{
if ( !rURL.isEmpty() )
{
initOfficeInstDirs();
implExpandURL( rURL );
rURL = mxOfficeInstDirs->makeRelocatableURL( rURL );
}
}
void SfxURLRelocator_Impl::makeAbsoluteURL( OUString & rURL )
{
if ( !rURL.isEmpty() )
{
initOfficeInstDirs();
implExpandURL( rURL );
rURL = mxOfficeInstDirs->makeAbsoluteURL( rURL );
}
}
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_comp_sfx2_DocumentTemplates_get_implementation(<--- The function 'com_sun_star_comp_sfx2_DocumentTemplates_get_implementation' is never used.
css::uno::XComponentContext *context,
css::uno::Sequence<css::uno::Any> const &)
{
return cppu::acquire(new SfxDocTplService(context));
}
OUString DocTemplLocaleHelper::GetStandardGroupString()
{
return SfxResId(TEMPLATE_LONG_NAMES_ARY[0]);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|