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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sc.hxx"
// INCLUDE ---------------------------------------------------------------
#include "externalrefmgr.hxx"
#include "document.hxx"
#include "token.hxx"
#include "tokenarray.hxx"
#include "address.hxx"
#include "tablink.hxx"
#include "docsh.hxx"
#include "scextopt.hxx"
#include "rangenam.hxx"
#include "cell.hxx"
#include "viewdata.hxx"
#include "tabvwsh.hxx"
#include "sc.hrc"
#include "sfx2/app.hxx"
#include "sfx2/docfilt.hxx"
#include "sfx2/docfile.hxx"
#include "sfx2/fcontnr.hxx"
#include "sfx2/sfxsids.hrc"
#include "sfx2/objsh.hxx"
#include "svl/broadcast.hxx"
#include "svl/smplhint.hxx"
#include "svl/itemset.hxx"
#include "svl/stritem.hxx"
#include "svl/urihelper.hxx"
#include "svl/zformat.hxx"
#include "sfx2/linkmgr.hxx"
#include "tools/urlobj.hxx"
#include "unotools/ucbhelper.hxx"
#include <memory>
#include <algorithm>
#include <boost/scoped_ptr.hpp>
using ::std::auto_ptr;
using ::com::sun::star::uno::Any;
using ::rtl::OUString;
using ::std::vector;
using ::std::find;
using ::std::find_if;
using ::std::distance;
using ::std::pair;
using ::std::list;
using ::std::unary_function;
using namespace formula;
#define SRCDOC_LIFE_SPAN 6000 // 1 minute (in 100th of a sec)
#define SRCDOC_SCAN_INTERVAL 1000*5 // every 5 seconds (in msec)
namespace {
class TabNameSearchPredicate : public unary_function<bool, ScExternalRefCache::TableName>
{
public:
explicit TabNameSearchPredicate(const String& rSearchName) :
maSearchName(ScGlobal::pCharClass->upper(rSearchName))
{
}
bool operator()(const ScExternalRefCache::TableName& rTabNameSet) const
{
// Ok, I'm doing case insensitive search here.
return rTabNameSet.maUpperName.Equals(maSearchName);
}
private:
String maSearchName;
};
class FindSrcFileByName : public unary_function<ScExternalRefManager::SrcFileData, bool>
{
public:
FindSrcFileByName(const String& rMatchName) :
mrMatchName(rMatchName)
{
}
bool operator()(const ScExternalRefManager::SrcFileData& rSrcData) const
{
return rSrcData.maFileName.Equals(mrMatchName);
}
private:
const String& mrMatchName;
};
class NotifyLinkListener : public unary_function<ScExternalRefManager::LinkListener*, void>
{
public:
NotifyLinkListener(sal_uInt16 nFileId, ScExternalRefManager::LinkUpdateType eType) :
mnFileId(nFileId), meType(eType) {}
NotifyLinkListener(const NotifyLinkListener& r) :
mnFileId(r.mnFileId), meType(r.meType) {}
void operator() (ScExternalRefManager::LinkListener* p) const
{
p->notify(mnFileId, meType);
}
private:
sal_uInt16 mnFileId;
ScExternalRefManager::LinkUpdateType meType;
};
struct UpdateFormulaCell : public unary_function<ScFormulaCell*, void>
{
void operator() (ScFormulaCell* pCell) const
{
// Check to make sure the cell really contains ocExternalRef.
// External names, external cell and range references all have a
// ocExternalRef token.
const ScTokenArray* pCode = pCell->GetCode();
if (!pCode->HasExternalRef())
return;
ScTokenArray* pArray = pCell->GetCode();
if (pArray)
// Clear the error code, or a cell with error won't get re-compiled.
pArray->SetCodeError(0);
pCell->SetCompile(true);
pCell->CompileTokenArray();
pCell->SetDirty();
}
};
class RemoveFormulaCell : public unary_function<pair<const sal_uInt16, ScExternalRefManager::RefCellSet>, void>
{
public:
explicit RemoveFormulaCell(ScFormulaCell* p) : mpCell(p) {}
void operator() (pair<const sal_uInt16, ScExternalRefManager::RefCellSet>& r) const
{
r.second.erase(mpCell);
}
private:
ScFormulaCell* mpCell;
};
class ConvertFormulaToStatic : public unary_function<ScFormulaCell*, void>
{
public:
explicit ConvertFormulaToStatic(ScDocument* pDoc) : mpDoc(pDoc) {}
void operator() (ScFormulaCell* pCell) const
{
ScAddress aPos = pCell->aPos;
// We don't check for empty cells because empty external cells are
// treated as having a value of 0.
if (pCell->IsValue())
{
// Turn this into value cell.
double fVal = pCell->GetValue();
mpDoc->PutCell(aPos, new ScValueCell(fVal));
}
else
{
// string cell otherwise.
String aVal;
pCell->GetString(aVal);
mpDoc->PutCell(aPos, new ScStringCell(aVal));
}
}
private:
ScDocument* mpDoc;
};
}
// ============================================================================
ScExternalRefCache::Table::Table()
: meReferenced( REFERENCED_MARKED )
// Prevent accidental data loss due to lack of knowledge.
{
}
ScExternalRefCache::Table::~Table()
{
}
void ScExternalRefCache::Table::setReferencedFlag( ScExternalRefCache::Table::ReferencedFlag eFlag )
{
meReferenced = eFlag;
}
void ScExternalRefCache::Table::setReferenced( bool bReferenced )
{
if (meReferenced != REFERENCED_PERMANENT)
meReferenced = (bReferenced ? REFERENCED_MARKED : UNREFERENCED);
}
ScExternalRefCache::Table::ReferencedFlag ScExternalRefCache::Table::getReferencedFlag() const
{
return meReferenced;
}
bool ScExternalRefCache::Table::isReferenced() const
{
return meReferenced != UNREFERENCED;
}
void ScExternalRefCache::Table::setCell(SCCOL nCol, SCROW nRow, TokenRef pToken, sal_uInt32 nFmtIndex, bool bSetCacheRange)
{
using ::std::pair;
RowsDataType::iterator itrRow = maRows.find(nRow);
if (itrRow == maRows.end())
{
// This row does not exist yet.
pair<RowsDataType::iterator, bool> res = maRows.insert(
RowsDataType::value_type(nRow, RowDataType()));
if (!res.second)
return;
itrRow = res.first;
}
// Insert this token into the specified column location. I don't need to
// check for existing data. Just overwrite it.
RowDataType& rRow = itrRow->second;
ScExternalRefCache::Cell aCell;
aCell.mxToken = pToken;
aCell.mnFmtIndex = nFmtIndex;
rRow.insert(RowDataType::value_type(nCol, aCell));
if (bSetCacheRange)
setCachedCell(nCol, nRow);
}
ScExternalRefCache::TokenRef ScExternalRefCache::Table::getCell(SCCOL nCol, SCROW nRow, sal_uInt32* pnFmtIndex) const
{
RowsDataType::const_iterator itrTable = maRows.find(nRow);
if (itrTable == maRows.end())
{
// this table doesn't have the specified row.
return getEmptyOrNullToken(nCol, nRow);
}
const RowDataType& rRowData = itrTable->second;
RowDataType::const_iterator itrRow = rRowData.find(nCol);
if (itrRow == rRowData.end())
{
// this row doesn't have the specified column.
return getEmptyOrNullToken(nCol, nRow);
}
const Cell& rCell = itrRow->second;
if (pnFmtIndex)
*pnFmtIndex = rCell.mnFmtIndex;
return rCell.mxToken;
}
bool ScExternalRefCache::Table::hasRow( SCROW nRow ) const
{
RowsDataType::const_iterator itrRow = maRows.find(nRow);
return itrRow != maRows.end();
}
void ScExternalRefCache::Table::getAllRows(vector<SCROW>& rRows, SCROW nLow, SCROW nHigh) const
{
vector<SCROW> aRows;
aRows.reserve(maRows.size());
RowsDataType::const_iterator itr = maRows.begin(), itrEnd = maRows.end();
for (; itr != itrEnd; ++itr)
if (nLow <= itr->first && itr->first <= nHigh)
aRows.push_back(itr->first);
// hash map is not ordered, so we need to explicitly sort it.
::std::sort(aRows.begin(), aRows.end());
rRows.swap(aRows);
}
::std::pair< SCROW, SCROW > ScExternalRefCache::Table::getRowRange() const
{
::std::pair< SCROW, SCROW > aRange( 0, 0 );
if( !maRows.empty() )
{
// iterate over entire container (hash map is not sorted by key)
RowsDataType::const_iterator itr = maRows.begin(), itrEnd = maRows.end();
aRange.first = itr->first;
aRange.second = itr->first + 1;
while( ++itr != itrEnd )
{
if( itr->first < aRange.first )
aRange.first = itr->first;
else if( itr->first >= aRange.second )
aRange.second = itr->first + 1;
}
}
return aRange;
}
void ScExternalRefCache::Table::getAllCols(SCROW nRow, vector<SCCOL>& rCols, SCCOL nLow, SCCOL nHigh) const
{
RowsDataType::const_iterator itrRow = maRows.find(nRow);
if (itrRow == maRows.end())
// this table doesn't have the specified row.
return;
const RowDataType& rRowData = itrRow->second;
vector<SCCOL> aCols;
aCols.reserve(rRowData.size());
RowDataType::const_iterator itrCol = rRowData.begin(), itrColEnd = rRowData.end();
for (; itrCol != itrColEnd; ++itrCol)
if (nLow <= itrCol->first && itrCol->first <= nHigh)
aCols.push_back(itrCol->first);
// hash map is not ordered, so we need to explicitly sort it.
::std::sort(aCols.begin(), aCols.end());
rCols.swap(aCols);
}
::std::pair< SCCOL, SCCOL > ScExternalRefCache::Table::getColRange( SCROW nRow ) const
{
::std::pair< SCCOL, SCCOL > aRange( 0, 0 );
RowsDataType::const_iterator itrRow = maRows.find( nRow );
if (itrRow == maRows.end())
// this table doesn't have the specified row.
return aRange;
const RowDataType& rRowData = itrRow->second;
if( !rRowData.empty() )
{
// iterate over entire container (hash map is not sorted by key)
RowDataType::const_iterator itr = rRowData.begin(), itrEnd = rRowData.end();
aRange.first = itr->first;
aRange.second = itr->first + 1;
while( ++itr != itrEnd )
{
if( itr->first < aRange.first )
aRange.first = itr->first;
else if( itr->first >= aRange.second )
aRange.second = itr->first + 1;
}
}
return aRange;
}
void ScExternalRefCache::Table::getAllNumberFormats(vector<sal_uInt32>& rNumFmts) const
{
RowsDataType::const_iterator itrRow = maRows.begin(), itrRowEnd = maRows.end();
for (; itrRow != itrRowEnd; ++itrRow)
{
const RowDataType& rRowData = itrRow->second;
RowDataType::const_iterator itrCol = rRowData.begin(), itrColEnd = rRowData.end();
for (; itrCol != itrColEnd; ++itrCol)
{
const Cell& rCell = itrCol->second;
rNumFmts.push_back(rCell.mnFmtIndex);
}
}
}
bool ScExternalRefCache::Table::isRangeCached(SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2) const
{
return maCachedRanges.In(ScRange(nCol1, nRow1, 0, nCol2, nRow2, 0));
}
void ScExternalRefCache::Table::setCachedCell(SCCOL nCol, SCROW nRow)
{
setCachedCellRange(nCol, nRow, nCol, nRow);
}
void ScExternalRefCache::Table::setCachedCellRange(SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2)
{
ScRange aRange(nCol1, nRow1, 0, nCol2, nRow2, 0);
if ( maCachedRanges.empty() )
maCachedRanges.Append(aRange);
else
maCachedRanges.Join(aRange);
String aStr;
maCachedRanges.Format(aStr, SCA_VALID);
}
void ScExternalRefCache::Table::setWholeTableCached()
{
setCachedCellRange(0, 0, MAXCOL, MAXROW);
}
bool ScExternalRefCache::Table::isInCachedRanges(SCCOL nCol, SCROW nRow) const
{
return maCachedRanges.In(ScRange(nCol, nRow, 0, nCol, nRow, 0));
}
ScExternalRefCache::TokenRef ScExternalRefCache::Table::getEmptyOrNullToken(
SCCOL nCol, SCROW nRow) const
{
if (isInCachedRanges(nCol, nRow))
{
TokenRef p(new ScEmptyCellToken(false, false));
return p;
}
return TokenRef();
}
// ----------------------------------------------------------------------------
ScExternalRefCache::TableName::TableName(const String& rUpper, const String& rReal) :
maUpperName(rUpper), maRealName(rReal)
{
}
// ----------------------------------------------------------------------------
ScExternalRefCache::CellFormat::CellFormat() :
mbIsSet(false), mnType(NUMBERFORMAT_ALL), mnIndex(0)
{
}
// ----------------------------------------------------------------------------
ScExternalRefCache::ScExternalRefCache()
{
}
ScExternalRefCache::~ScExternalRefCache()
{
}
const String* ScExternalRefCache::getRealTableName(sal_uInt16 nFileId, const String& rTabName) const
{
DocDataType::const_iterator itrDoc = maDocs.find(nFileId);
if (itrDoc == maDocs.end())
{
// specified document is not cached.
return NULL;
}
const DocItem& rDoc = itrDoc->second;
TableNameIndexMap::const_iterator itrTabId = rDoc.maTableNameIndex.find(
ScGlobal::pCharClass->upper(rTabName));
if (itrTabId == rDoc.maTableNameIndex.end())
{
// the specified table is not in cache.
return NULL;
}
return &rDoc.maTableNames[itrTabId->second].maRealName;
}
const String* ScExternalRefCache::getRealRangeName(sal_uInt16 nFileId, const String& rRangeName) const
{
DocDataType::const_iterator itrDoc = maDocs.find(nFileId);
if (itrDoc == maDocs.end())
{
// specified document is not cached.
return NULL;
}
const DocItem& rDoc = itrDoc->second;
NamePairMap::const_iterator itr = rDoc.maRealRangeNameMap.find(
ScGlobal::pCharClass->upper(rRangeName));
if (itr == rDoc.maRealRangeNameMap.end())
// range name not found.
return NULL;
return &itr->second;
}
ScExternalRefCache::TokenRef ScExternalRefCache::getCellData(
sal_uInt16 nFileId, const String& rTabName, SCCOL nCol, SCROW nRow, sal_uInt32* pnFmtIndex)
{
DocDataType::const_iterator itrDoc = maDocs.find(nFileId);
if (itrDoc == maDocs.end())
{
// specified document is not cached.
return TokenRef();
}
const DocItem& rDoc = itrDoc->second;
TableNameIndexMap::const_iterator itrTabId = rDoc.maTableNameIndex.find(
ScGlobal::pCharClass->upper(rTabName));
if (itrTabId == rDoc.maTableNameIndex.end())
{
// the specified table is not in cache.
return TokenRef();
}
const TableTypeRef& pTableData = rDoc.maTables[itrTabId->second];
if (!pTableData.get())
{
// the table data is not instantiated yet.
return TokenRef();
}
return pTableData->getCell(nCol, nRow, pnFmtIndex);
}
ScExternalRefCache::TokenArrayRef ScExternalRefCache::getCellRangeData(
sal_uInt16 nFileId, const String& rTabName, const ScRange& rRange)
{
DocDataType::iterator itrDoc = maDocs.find(nFileId);
if (itrDoc == maDocs.end())
// specified document is not cached.
return TokenArrayRef();
DocItem& rDoc = itrDoc->second;
TableNameIndexMap::iterator itrTabId = rDoc.maTableNameIndex.find(
ScGlobal::pCharClass->upper(rTabName));
if (itrTabId == rDoc.maTableNameIndex.end())
// the specified table is not in cache.
return TokenArrayRef();
const ScAddress& s = rRange.aStart;
const ScAddress& e = rRange.aEnd;
SCTAB nTab1 = s.Tab(), nTab2 = e.Tab();
SCCOL nCol1 = s.Col(), nCol2 = e.Col();
SCROW nRow1 = s.Row(), nRow2 = e.Row();
// Make sure I have all the tables cached.
size_t nTabFirstId = itrTabId->second;
size_t nTabLastId = nTabFirstId + nTab2 - nTab1;
if (nTabLastId >= rDoc.maTables.size())
// not all tables are cached.
return TokenArrayRef();
ScRange aCacheRange( nCol1, nRow1, static_cast<SCTAB>(nTabFirstId), nCol2, nRow2, static_cast<SCTAB>(nTabLastId));
RangeArrayMap::const_iterator itrRange = rDoc.maRangeArrays.find( aCacheRange);
if (itrRange != rDoc.maRangeArrays.end())
// Cache hit!
return itrRange->second;
::boost::scoped_ptr<ScRange> pNewRange;
TokenArrayRef pArray;
bool bFirstTab = true;
for (size_t nTab = nTabFirstId; nTab <= nTabLastId; ++nTab)
{
TableTypeRef pTab = rDoc.maTables[nTab];
if (!pTab.get())
return TokenArrayRef();
SCCOL nDataCol1 = nCol1, nDataCol2 = nCol2;
SCROW nDataRow1 = nRow1, nDataRow2 = nRow2;
if (!pTab->isRangeCached(nDataCol1, nDataRow1, nDataCol2, nDataRow2))
{
// specified range is not entirely within cached ranges.
return TokenArrayRef();
}
ScMatrixRef xMat = new ScMatrix(
static_cast<SCSIZE>(nDataCol2-nDataCol1+1),
static_cast<SCSIZE>(nDataRow2-nDataRow1+1), ScMatrix::SPARSE_EMPTY);
// Only fill non-empty cells, for better performance.
vector<SCROW> aRows;
pTab->getAllRows(aRows, nDataRow1, nDataRow2);
for (vector<SCROW>::const_iterator itr = aRows.begin(), itrEnd = aRows.end(); itr != itrEnd; ++itr)
{
SCROW nRow = *itr;
vector<SCCOL> aCols;
pTab->getAllCols(nRow, aCols, nDataCol1, nDataCol2);
for (vector<SCCOL>::const_iterator itrCol = aCols.begin(), itrColEnd = aCols.end(); itrCol != itrColEnd; ++itrCol)
{
SCCOL nCol = *itrCol;
TokenRef pToken = pTab->getCell(nCol, nRow);
if (!pToken)
// This should never happen!
return TokenArrayRef();
SCSIZE nC = nCol - nDataCol1, nR = nRow - nDataRow1;
switch (pToken->GetType())
{
case svDouble:
xMat->PutDouble(pToken->GetDouble(), nC, nR);
break;
case svString:
xMat->PutString(pToken->GetString(), nC, nR);
break;
default:
;
}
}
}
if (!bFirstTab)
pArray->AddOpCode(ocSep);
ScMatrixToken aToken(xMat);
if (!pArray)
pArray.reset(new ScTokenArray);
pArray->AddToken(aToken);
bFirstTab = false;
if (!pNewRange)
pNewRange.reset(new ScRange(nDataCol1, nDataRow1, 0, nDataCol2, nDataRow2, 0));
else
pNewRange->ExtendTo(ScRange(nDataCol1, nDataRow1, 0, nDataCol2, nDataRow2, 0));
}
if (pNewRange)
rDoc.maRangeArrays.insert( RangeArrayMap::value_type(*pNewRange, pArray));
return pArray;
}
ScExternalRefCache::TokenArrayRef ScExternalRefCache::getRangeNameTokens(sal_uInt16 nFileId, const String& rName)
{
DocItem* pDoc = getDocItem(nFileId);
if (!pDoc)
return TokenArrayRef();
RangeNameMap& rMap = pDoc->maRangeNames;
RangeNameMap::const_iterator itr = rMap.find(
ScGlobal::pCharClass->upper(rName));
if (itr == rMap.end())
return TokenArrayRef();
return itr->second;
}
void ScExternalRefCache::setRangeNameTokens(sal_uInt16 nFileId, const String& rName, TokenArrayRef pArray)
{
DocItem* pDoc = getDocItem(nFileId);
if (!pDoc)
return;
String aUpperName = ScGlobal::pCharClass->upper(rName);
RangeNameMap& rMap = pDoc->maRangeNames;
rMap.insert(RangeNameMap::value_type(aUpperName, pArray));
pDoc->maRealRangeNameMap.insert(NamePairMap::value_type(aUpperName, rName));
}
void ScExternalRefCache::setCellData(sal_uInt16 nFileId, const String& rTabName, SCCOL nCol, SCROW nRow,
TokenRef pToken, sal_uInt32 nFmtIndex)
{
if (!isDocInitialized(nFileId))
return;
using ::std::pair;
DocItem* pDocItem = getDocItem(nFileId);
if (!pDocItem)
return;
DocItem& rDoc = *pDocItem;
// See if the table by this name already exists.
TableNameIndexMap::iterator itrTabName = rDoc.maTableNameIndex.find(
ScGlobal::pCharClass->upper(rTabName));
if (itrTabName == rDoc.maTableNameIndex.end())
// Table not found. Maybe the table name or the file id is wrong ???
return;
TableTypeRef& pTableData = rDoc.maTables[itrTabName->second];
if (!pTableData.get())
pTableData.reset(new Table);
pTableData->setCell(nCol, nRow, pToken, nFmtIndex);
pTableData->setCachedCell(nCol, nRow);
}
void ScExternalRefCache::setCellRangeData(sal_uInt16 nFileId, const ScRange& rRange, const vector<SingleRangeData>& rData,
const TokenArrayRef& pArray)
{
using ::std::pair;
if (rData.empty() || !isDocInitialized(nFileId))
// nothing to cache
return;
// First, get the document item for the given file ID.
DocItem* pDocItem = getDocItem(nFileId);
if (!pDocItem)
return;
DocItem& rDoc = *pDocItem;
// Now, find the table position of the first table to cache.
const String& rFirstTabName = rData.front().maTableName;
TableNameIndexMap::iterator itrTabName = rDoc.maTableNameIndex.find(
ScGlobal::pCharClass->upper(rFirstTabName));
if (itrTabName == rDoc.maTableNameIndex.end())
{
// table index not found.
return;
}
size_t nTabFirstId = itrTabName->second;
SCROW nRow1 = rRange.aStart.Row(), nRow2 = rRange.aEnd.Row();
SCCOL nCol1 = rRange.aStart.Col(), nCol2 = rRange.aEnd.Col();
vector<SingleRangeData>::const_iterator itrDataBeg = rData.begin(), itrDataEnd = rData.end();
for (vector<SingleRangeData>::const_iterator itrData = itrDataBeg; itrData != itrDataEnd; ++itrData)
{
size_t i = nTabFirstId + ::std::distance(itrDataBeg, itrData);
TableTypeRef& pTabData = rDoc.maTables[i];
if (!pTabData.get())
pTabData.reset(new Table);
for (SCROW nRow = nRow1; nRow <= nRow2; ++nRow)
{
for (SCCOL nCol = nCol1; nCol <= nCol2; ++nCol)
{
SCSIZE nC = nCol - nCol1, nR = nRow - nRow1;
TokenRef pToken;
const ScMatrixRef& pMat = itrData->mpRangeData;
if (pMat->IsEmpty(nC, nR))
// Don't cache empty cells.
continue;
if (pMat->IsValue(nC, nR))
pToken.reset(new formula::FormulaDoubleToken(pMat->GetDouble(nC, nR)));
else if (pMat->IsString(nC, nR))
pToken.reset(new formula::FormulaStringToken(pMat->GetString(nC, nR)));
if (pToken)
// Don't mark this cell 'cached' here, for better performance.
pTabData->setCell(nCol, nRow, pToken, 0, false);
}
}
// Mark the whole range 'cached'.
pTabData->setCachedCellRange(nCol1, nRow1, nCol2, nRow2);
}
size_t nTabLastId = nTabFirstId + rRange.aEnd.Tab() - rRange.aStart.Tab();
ScRange aCacheRange( nCol1, nRow1, static_cast<SCTAB>(nTabFirstId), nCol2, nRow2, static_cast<SCTAB>(nTabLastId));
rDoc.maRangeArrays.insert( RangeArrayMap::value_type( aCacheRange, pArray));
}
bool ScExternalRefCache::isDocInitialized(sal_uInt16 nFileId)
{
DocItem* pDoc = getDocItem(nFileId);
if (!pDoc)
return false;
return pDoc->mbInitFromSource;
}
static bool lcl_getTableDataIndex(const ScExternalRefCache::TableNameIndexMap& rMap, const String& rName, size_t& rIndex)
{
ScExternalRefCache::TableNameIndexMap::const_iterator itr = rMap.find(rName);
if (itr == rMap.end())
return false;
rIndex = itr->second;
return true;
}
void ScExternalRefCache::initializeDoc(sal_uInt16 nFileId, const vector<String>& rTabNames)
{
DocItem* pDoc = getDocItem(nFileId);
if (!pDoc)
return;
size_t n = rTabNames.size();
// table name list - the list must include all table names in the source
// document and only to be populated when loading the source document, not
// when loading cached data from, say, Excel XCT/CRN records.
vector<TableName> aNewTabNames;
aNewTabNames.reserve(n);
for (vector<String>::const_iterator itr = rTabNames.begin(), itrEnd = rTabNames.end();
itr != itrEnd; ++itr)
{
TableName aNameItem(ScGlobal::pCharClass->upper(*itr), *itr);
aNewTabNames.push_back(aNameItem);
}
pDoc->maTableNames.swap(aNewTabNames);
// data tables - preserve any existing data that may have been set during
// file import.
vector<TableTypeRef> aNewTables(n);
for (size_t i = 0; i < n; ++i)
{
size_t nIndex;
if (lcl_getTableDataIndex(pDoc->maTableNameIndex, pDoc->maTableNames[i].maUpperName, nIndex))
{
aNewTables[i] = pDoc->maTables[nIndex];
}
}
pDoc->maTables.swap(aNewTables);
// name index map
TableNameIndexMap aNewNameIndex;
for (size_t i = 0; i < n; ++i)
aNewNameIndex.insert(TableNameIndexMap::value_type(pDoc->maTableNames[i].maUpperName, i));
pDoc->maTableNameIndex.swap(aNewNameIndex);
pDoc->mbInitFromSource = true;
}
String ScExternalRefCache::getTableName(sal_uInt16 nFileId, size_t nCacheId) const
{
if( DocItem* pDoc = getDocItem( nFileId ) )
if( nCacheId < pDoc->maTableNames.size() )
return pDoc->maTableNames[ nCacheId ].maRealName;
return EMPTY_STRING;
}
void ScExternalRefCache::getAllTableNames(sal_uInt16 nFileId, vector<OUString>& rTabNames) const
{
rTabNames.clear();
DocItem* pDoc = getDocItem(nFileId);
if (!pDoc)
return;
size_t n = pDoc->maTableNames.size();
rTabNames.reserve(n);
for (vector<TableName>::const_iterator itr = pDoc->maTableNames.begin(), itrEnd = pDoc->maTableNames.end();
itr != itrEnd; ++itr)
rTabNames.push_back(itr->maRealName);
}
SCsTAB ScExternalRefCache::getTabSpan( sal_uInt16 nFileId, const String& rStartTabName, const String& rEndTabName ) const
{
DocItem* pDoc = getDocItem(nFileId);
if (!pDoc)
return -1;
vector<TableName>::const_iterator itrBeg = pDoc->maTableNames.begin();
vector<TableName>::const_iterator itrEnd = pDoc->maTableNames.end();
vector<TableName>::const_iterator itrStartTab = ::std::find_if( itrBeg, itrEnd,
TabNameSearchPredicate( rStartTabName));
if (itrStartTab == itrEnd)
return -1;
vector<TableName>::const_iterator itrEndTab = ::std::find_if( itrBeg, itrEnd,
TabNameSearchPredicate( rEndTabName));
if (itrEndTab == itrEnd)
return 0;
size_t nStartDist = ::std::distance( itrBeg, itrStartTab);
size_t nEndDist = ::std::distance( itrBeg, itrEndTab);
return nStartDist <= nEndDist ? static_cast<SCsTAB>(nEndDist - nStartDist + 1) : -static_cast<SCsTAB>(nStartDist - nEndDist + 1);
}
void ScExternalRefCache::getAllNumberFormats(vector<sal_uInt32>& rNumFmts) const
{
using ::std::sort;
using ::std::unique;
vector<sal_uInt32> aNumFmts;
for (DocDataType::const_iterator itrDoc = maDocs.begin(), itrDocEnd = maDocs.end();
itrDoc != itrDocEnd; ++itrDoc)
{
const vector<TableTypeRef>& rTables = itrDoc->second.maTables;
for (vector<TableTypeRef>::const_iterator itrTab = rTables.begin(), itrTabEnd = rTables.end();
itrTab != itrTabEnd; ++itrTab)
{
TableTypeRef pTab = *itrTab;
if (!pTab)
continue;
pTab->getAllNumberFormats(aNumFmts);
}
}
// remove duplicates.
sort(aNumFmts.begin(), aNumFmts.end());
aNumFmts.erase(unique(aNumFmts.begin(), aNumFmts.end()), aNumFmts.end());
rNumFmts.swap(aNumFmts);
}
bool ScExternalRefCache::hasCacheTable(sal_uInt16 nFileId, const String& rTabName) const
{
DocItem* pDoc = getDocItem(nFileId);
if (!pDoc)
return false;
String aUpperName = ScGlobal::pCharClass->upper(rTabName);
vector<TableName>::const_iterator itrBeg = pDoc->maTableNames.begin(), itrEnd = pDoc->maTableNames.end();
vector<TableName>::const_iterator itr = ::std::find_if(
itrBeg, itrEnd, TabNameSearchPredicate(aUpperName));
return itr != itrEnd;
}
size_t ScExternalRefCache::getCacheTableCount(sal_uInt16 nFileId) const
{
DocItem* pDoc = getDocItem(nFileId);
return pDoc ? pDoc->maTables.size() : 0;
}
bool ScExternalRefCache::setCacheDocReferenced( sal_uInt16 nFileId )
{
DocItem* pDocItem = getDocItem(nFileId);
if (!pDocItem)
return areAllCacheTablesReferenced();
for (::std::vector<TableTypeRef>::iterator itrTab = pDocItem->maTables.begin();
itrTab != pDocItem->maTables.end(); ++itrTab)
{
if ((*itrTab).get())
(*itrTab)->setReferenced( true);
}
addCacheDocToReferenced( nFileId);
return areAllCacheTablesReferenced();
}
bool ScExternalRefCache::setCacheTableReferenced( sal_uInt16 nFileId, const String& rTabName, size_t nSheets, bool bPermanent )
{
DocItem* pDoc = getDocItem(nFileId);
if (pDoc)
{
size_t nIndex = 0;
String aTabNameUpper = ScGlobal::pCharClass->upper( rTabName);
if (lcl_getTableDataIndex( pDoc->maTableNameIndex, aTabNameUpper, nIndex))
{
size_t nStop = ::std::min( nIndex + nSheets, pDoc->maTables.size());
for (size_t i = nIndex; i < nStop; ++i)
{
TableTypeRef pTab = pDoc->maTables[i];
if (pTab.get())
{
Table::ReferencedFlag eNewFlag = (bPermanent ?
Table::REFERENCED_PERMANENT :
Table::REFERENCED_MARKED);
Table::ReferencedFlag eOldFlag = pTab->getReferencedFlag();
if (eOldFlag != Table::REFERENCED_PERMANENT && eNewFlag != eOldFlag)
{
pTab->setReferencedFlag( eNewFlag);
addCacheTableToReferenced( nFileId, i);
}
}
}
}
}
return areAllCacheTablesReferenced();
}
void ScExternalRefCache::setCacheTableReferencedPermanently( sal_uInt16 nFileId, const String& rTabName, size_t nSheets )
{
DocItem* pDoc = getDocItem(nFileId);
if (pDoc)
{
size_t nIndex = 0;
String aTabNameUpper = ScGlobal::pCharClass->upper( rTabName);
if (lcl_getTableDataIndex( pDoc->maTableNameIndex, aTabNameUpper, nIndex))
{
size_t nStop = ::std::min( nIndex + nSheets, pDoc->maTables.size());
for (size_t i = nIndex; i < nStop; ++i)
{
TableTypeRef pTab = pDoc->maTables[i];
if (pTab.get())
pTab->setReferencedFlag( Table::REFERENCED_PERMANENT);
}
}
}
}
void ScExternalRefCache::setAllCacheTableReferencedStati( bool bReferenced )
{
if (bReferenced)
{
maReferenced.reset(0);
for (DocDataType::iterator itrDoc = maDocs.begin(); itrDoc != maDocs.end(); ++itrDoc)
{
ScExternalRefCache::DocItem& rDocItem = (*itrDoc).second;
for (::std::vector<TableTypeRef>::iterator itrTab = rDocItem.maTables.begin();
itrTab != rDocItem.maTables.end(); ++itrTab)
{
if ((*itrTab).get())
(*itrTab)->setReferenced( true);
}
}
}
else
{
size_t nDocs = 0;
for (DocDataType::const_iterator itrDoc = maDocs.begin(); itrDoc != maDocs.end(); ++itrDoc)
{
if (nDocs <= (*itrDoc).first)
nDocs = (*itrDoc).first + 1;
}
maReferenced.reset( nDocs);
for (DocDataType::iterator itrDoc = maDocs.begin(); itrDoc != maDocs.end(); ++itrDoc)
{
ScExternalRefCache::DocItem& rDocItem = (*itrDoc).second;
sal_uInt16 nFileId = (*itrDoc).first;
size_t nTables = rDocItem.maTables.size();
ReferencedStatus::DocReferenced & rDocReferenced = maReferenced.maDocs[nFileId];
// All referenced => non-existing tables evaluate as completed.
rDocReferenced.maTables.resize( nTables, true);
for (size_t i=0; i < nTables; ++i)
{
TableTypeRef & xTab = rDocItem.maTables[i];
if (xTab.get())
{
if (xTab->getReferencedFlag() == Table::REFERENCED_PERMANENT)
addCacheTableToReferenced( nFileId, i);
else
{
xTab->setReferencedFlag( Table::UNREFERENCED);
rDocReferenced.maTables[i] = false;
rDocReferenced.mbAllTablesReferenced = false;
// An addCacheTableToReferenced() actually may have
// resulted in mbAllReferenced been set. Clear it.
maReferenced.mbAllReferenced = false;
}
}
}
}
}
}
void ScExternalRefCache::addCacheTableToReferenced( sal_uInt16 nFileId, size_t nIndex )
{
if (nFileId >= maReferenced.maDocs.size())
return;
::std::vector<bool> & rTables = maReferenced.maDocs[nFileId].maTables;
size_t nTables = rTables.size();
if (nIndex >= nTables)
return;
if (!rTables[nIndex])
{
rTables[nIndex] = true;
size_t i = 0;
while (i < nTables && rTables[i])
++i;
if (i == nTables)
{
maReferenced.maDocs[nFileId].mbAllTablesReferenced = true;
maReferenced.checkAllDocs();
}
}
}
void ScExternalRefCache::addCacheDocToReferenced( sal_uInt16 nFileId )
{
if (nFileId >= maReferenced.maDocs.size())
return;
if (!maReferenced.maDocs[nFileId].mbAllTablesReferenced)
{
::std::vector<bool> & rTables = maReferenced.maDocs[nFileId].maTables;
size_t nSize = rTables.size();
for (size_t i=0; i < nSize; ++i)
rTables[i] = true;
maReferenced.maDocs[nFileId].mbAllTablesReferenced = true;
maReferenced.checkAllDocs();
}
}
bool ScExternalRefCache::areAllCacheTablesReferenced() const
{
return maReferenced.mbAllReferenced;
}
ScExternalRefCache::ReferencedStatus::ReferencedStatus() :
mbAllReferenced(false)
{
reset(0);
}
void ScExternalRefCache::ReferencedStatus::reset( size_t nDocs )
{
if (nDocs)
{
mbAllReferenced = false;
DocReferencedVec aRefs( nDocs);
maDocs.swap( aRefs);
}
else
{
mbAllReferenced = true;
DocReferencedVec aRefs;
maDocs.swap( aRefs);
}
}
void ScExternalRefCache::ReferencedStatus::checkAllDocs()
{
for (DocReferencedVec::const_iterator itr = maDocs.begin(); itr != maDocs.end(); ++itr)
{
if (!(*itr).mbAllTablesReferenced)
return;
}
mbAllReferenced = true;
}
ScExternalRefCache::TableTypeRef ScExternalRefCache::getCacheTable(sal_uInt16 nFileId, size_t nTabIndex) const
{
DocItem* pDoc = getDocItem(nFileId);
if (!pDoc || nTabIndex >= pDoc->maTables.size())
return TableTypeRef();
return pDoc->maTables[nTabIndex];
}
ScExternalRefCache::TableTypeRef ScExternalRefCache::getCacheTable(sal_uInt16 nFileId, const String& rTabName, bool bCreateNew, size_t* pnIndex)
{
// In API, the index is transported as cached sheet ID of type sal_Int32 in
// sheet::SingleReference.Sheet or sheet::ComplexReference.Reference1.Sheet
// in a sheet::FormulaToken, choose a sensible value for N/A. Effectively
// being 0xffffffff
const size_t nNotAvailable = static_cast<size_t>( static_cast<sal_Int32>( -1));
DocItem* pDoc = getDocItem(nFileId);
if (!pDoc)
{
if (pnIndex) *pnIndex = nNotAvailable;
return TableTypeRef();
}
DocItem& rDoc = *pDoc;
size_t nIndex;
String aTabNameUpper = ScGlobal::pCharClass->upper(rTabName);
if (lcl_getTableDataIndex(rDoc.maTableNameIndex, aTabNameUpper, nIndex))
{
// specified table found.
if( pnIndex ) *pnIndex = nIndex;
if (bCreateNew && !rDoc.maTables[nIndex])
rDoc.maTables[nIndex].reset(new Table);
return rDoc.maTables[nIndex];
}
if (!bCreateNew)
{
if (pnIndex) *pnIndex = nNotAvailable;
return TableTypeRef();
}
// Specified table doesn't exist yet. Create one.
nIndex = rDoc.maTables.size();
if( pnIndex ) *pnIndex = nIndex;
TableTypeRef pTab(new Table);
rDoc.maTables.push_back(pTab);
rDoc.maTableNames.push_back(TableName(aTabNameUpper, rTabName));
rDoc.maTableNameIndex.insert(
TableNameIndexMap::value_type(aTabNameUpper, nIndex));
return pTab;
}
void ScExternalRefCache::clearCache(sal_uInt16 nFileId)
{
maDocs.erase(nFileId);
}
ScExternalRefCache::DocItem* ScExternalRefCache::getDocItem(sal_uInt16 nFileId) const
{
using ::std::pair;
DocDataType::iterator itrDoc = maDocs.find(nFileId);
if (itrDoc == maDocs.end())
{
// specified document is not cached.
pair<DocDataType::iterator, bool> res = maDocs.insert(
DocDataType::value_type(nFileId, DocItem()));
if (!res.second)
// insertion failed.
return NULL;
itrDoc = res.first;
}
return &itrDoc->second;
}
// ============================================================================
ScExternalRefLink::ScExternalRefLink(ScDocument* pDoc, sal_uInt16 nFileId, const String& rFilter) :
::sfx2::SvBaseLink(::sfx2::LINKUPDATE_ONCALL, FORMAT_FILE),
mnFileId(nFileId),
maFilterName(rFilter),
mpDoc(pDoc),
mbDoRefresh(true)
{
}
ScExternalRefLink::~ScExternalRefLink()
{
}
void ScExternalRefLink::Closed()
{
ScExternalRefManager* pMgr = mpDoc->GetExternalRefManager();
pMgr->breakLink(mnFileId);
}
void ScExternalRefLink::DataChanged(const String& /*rMimeType*/, const Any& /*rValue*/)
{
if (!mbDoRefresh)
return;
String aFile, aFilter;
mpDoc->GetLinkManager()->GetDisplayNames(this, NULL, &aFile, NULL, &aFilter);
ScExternalRefManager* pMgr = mpDoc->GetExternalRefManager();
const String* pCurFile = pMgr->getExternalFileName(mnFileId);
if (!pCurFile)
return;
if (pCurFile->Equals(aFile))
{
// Refresh the current source document.
pMgr->refreshNames(mnFileId);
}
else
{
// The source document has changed.
ScDocShell* pDocShell = ScDocShell::GetViewData()->GetDocShell();
ScDocShellModificator aMod(*pDocShell);
pMgr->switchSrcFile(mnFileId, aFile, aFilter);
maFilterName = aFilter;
aMod.SetDocumentModified();
}
}
void ScExternalRefLink::Edit(Window* pParent, const Link& /*rEndEditHdl*/)
{
SvBaseLink::Edit(pParent, LINK(this, ScExternalRefLink, ExternalRefEndEditHdl));
}
void ScExternalRefLink::SetDoReferesh(bool b)
{
mbDoRefresh = b;
}
IMPL_LINK( ScExternalRefLink, ExternalRefEndEditHdl, ::sfx2::SvBaseLink*, EMPTYARG )
{
return 0;
}
// ============================================================================
static FormulaToken* lcl_convertToToken(ScBaseCell* pCell)
{
if (!pCell || pCell->HasEmptyData())
{
bool bInherited = (pCell && pCell->GetCellType() == CELLTYPE_FORMULA);
return new ScEmptyCellToken( bInherited, false);
}
switch (pCell->GetCellType())
{
case CELLTYPE_EDIT:
{
String aStr;
static_cast<ScEditCell*>(pCell)->GetString(aStr);
return new formula::FormulaStringToken(aStr);
}
case CELLTYPE_STRING:
{
String aStr;
static_cast<ScStringCell*>(pCell)->GetString(aStr);
return new formula::FormulaStringToken(aStr);
}
case CELLTYPE_VALUE:
{
double fVal = static_cast<ScValueCell*>(pCell)->GetValue();
return new formula::FormulaDoubleToken(fVal);
}
case CELLTYPE_FORMULA:
{
ScFormulaCell* pFCell = static_cast<ScFormulaCell*>(pCell);
sal_uInt16 nError = pFCell->GetErrCode();
if (nError)
return new FormulaErrorToken( nError);
else if (pFCell->IsValue())
{
double fVal = pFCell->GetValue();
return new formula::FormulaDoubleToken(fVal);
}
else
{
String aStr;
pFCell->GetString(aStr);
return new formula::FormulaStringToken(aStr);
}
}
default:
OSL_FAIL("attempted to convert an unknown cell type.");
}
return NULL;
}
static ScTokenArray* lcl_convertToTokenArray(const ScDocument* pSrcDoc, ScRange& rRange,
vector<ScExternalRefCache::SingleRangeData>& rCacheData)
{
ScAddress& s = rRange.aStart;
ScAddress& e = rRange.aEnd;
SCTAB nTab1 = s.Tab(), nTab2 = e.Tab();
SCCOL nCol1 = s.Col(), nCol2 = e.Col();
SCROW nRow1 = s.Row(), nRow2 = e.Row();
if (nTab2 != nTab1)
// For now, we don't support multi-sheet ranges intentionally because
// we don't have a way to express them in a single token. In the
// future we can introduce a new stack variable type svMatrixList with
// a new token type that can store a 3D matrix value and convert a 3D
// range to it.
return NULL;
::boost::scoped_ptr<ScRange> pUsedRange;
auto_ptr<ScTokenArray> pArray(new ScTokenArray);
bool bFirstTab = true;
vector<ScExternalRefCache::SingleRangeData>::iterator
itrCache = rCacheData.begin(), itrCacheEnd = rCacheData.end();
for (SCTAB nTab = nTab1; nTab <= nTab2 && itrCache != itrCacheEnd; ++nTab, ++itrCache)
{
// Only loop within the data area.
SCCOL nDataCol1 = nCol1, nDataCol2 = nCol2;
SCROW nDataRow1 = nRow1, nDataRow2 = nRow2;
bool bShrunk;
if (!pSrcDoc->ShrinkToUsedDataArea( bShrunk, nTab, nDataCol1, nDataRow1, nDataCol2, nDataRow2, false))
// no data within specified range.
continue;
if (pUsedRange.get())
// Make sure the used area only grows, not shrinks.
pUsedRange->ExtendTo(ScRange(nDataCol1, nDataRow1, 0, nDataCol2, nDataRow2, 0));
else
pUsedRange.reset(new ScRange(nDataCol1, nDataRow1, 0, nDataCol2, nDataRow2, 0));
ScMatrixRef xMat = new ScMatrix(
static_cast<SCSIZE>(nDataCol2-nDataCol1+1),
static_cast<SCSIZE>(nDataRow2-nDataRow1+1));
for (SCCOL nCol = nDataCol1; nCol <= nDataCol2; ++nCol)
{
for (SCROW nRow = nDataRow1; nRow <= nDataRow2; ++nRow)
{
SCSIZE nC = nCol - nCol1, nR = nRow - nRow1;
ScBaseCell* pCell;
pSrcDoc->GetCell(nCol, nRow, nTab, pCell);
if (!pCell || pCell->HasEmptyData())
xMat->PutEmpty(nC, nR);
else
{
switch (pCell->GetCellType())
{
case CELLTYPE_EDIT:
{
String aStr;
static_cast<ScEditCell*>(pCell)->GetString(aStr);
xMat->PutString(aStr, nC, nR);
}
break;
case CELLTYPE_STRING:
{
String aStr;
static_cast<ScStringCell*>(pCell)->GetString(aStr);
xMat->PutString(aStr, nC, nR);
}
break;
case CELLTYPE_VALUE:
{
double fVal = static_cast<ScValueCell*>(pCell)->GetValue();
xMat->PutDouble(fVal, nC, nR);
}
break;
case CELLTYPE_FORMULA:
{
ScFormulaCell* pFCell = static_cast<ScFormulaCell*>(pCell);
sal_uInt16 nError = pFCell->GetErrCode();
if (nError)
xMat->PutDouble( CreateDoubleError( nError), nC, nR);
else if (pFCell->IsValue())
{
double fVal = pFCell->GetValue();
xMat->PutDouble(fVal, nC, nR);
}
else
{
String aStr;
pFCell->GetString(aStr);
xMat->PutString(aStr, nC, nR);
}
}
break;
default:
OSL_FAIL("attempted to convert an unknown cell type.");
}
}
}
}
if (!bFirstTab)
pArray->AddOpCode(ocSep);
ScMatrixToken aToken(xMat);
pArray->AddToken(aToken);
itrCache->mpRangeData = xMat;
bFirstTab = false;
}
if (!pUsedRange.get())
return NULL;
s.SetCol(pUsedRange->aStart.Col());
s.SetRow(pUsedRange->aStart.Row());
e.SetCol(pUsedRange->aEnd.Col());
e.SetRow(pUsedRange->aEnd.Row());
return pArray.release();
}
static ScTokenArray* lcl_fillEmptyMatrix(const ScRange& rRange)
{
SCSIZE nC = static_cast<SCSIZE>(rRange.aEnd.Col()-rRange.aStart.Col()+1);
SCSIZE nR = static_cast<SCSIZE>(rRange.aEnd.Row()-rRange.aStart.Row()+1);
ScMatrixRef xMat = new ScMatrix(nC, nR);
for (SCSIZE i = 0; i < nC; ++i)
for (SCSIZE j = 0; j < nR; ++j)
xMat->PutEmpty(i, j);
ScMatrixToken aToken(xMat);
auto_ptr<ScTokenArray> pArray(new ScTokenArray);
pArray->AddToken(aToken);
return pArray.release();
}
ScExternalRefManager::ScExternalRefManager(ScDocument* pDoc) :
mpDoc(pDoc),
mbInReferenceMarking(false),
mbUserInteractionEnabled(true)
{
maSrcDocTimer.SetTimeoutHdl( LINK(this, ScExternalRefManager, TimeOutHdl) );
maSrcDocTimer.SetTimeout(SRCDOC_SCAN_INTERVAL);
}
ScExternalRefManager::~ScExternalRefManager()
{
clear();
}
String ScExternalRefManager::getCacheTableName(sal_uInt16 nFileId, size_t nTabIndex) const
{
return maRefCache.getTableName(nFileId, nTabIndex);
}
ScExternalRefCache::TableTypeRef ScExternalRefManager::getCacheTable(sal_uInt16 nFileId, size_t nTabIndex) const
{
return maRefCache.getCacheTable(nFileId, nTabIndex);
}
ScExternalRefCache::TableTypeRef ScExternalRefManager::getCacheTable(sal_uInt16 nFileId, const String& rTabName, bool bCreateNew, size_t* pnIndex)
{
return maRefCache.getCacheTable(nFileId, rTabName, bCreateNew, pnIndex);
}
// ============================================================================
ScExternalRefManager::LinkListener::LinkListener()
{
}
ScExternalRefManager::LinkListener::~LinkListener()
{
}
// ----------------------------------------------------------------------------
ScExternalRefManager::ApiGuard::ApiGuard(ScDocument* pDoc) :
mpMgr(pDoc->GetExternalRefManager()),
mbOldInteractionEnabled(mpMgr->mbUserInteractionEnabled)
{
// We don't want user interaction handled in the API.
mpMgr->mbUserInteractionEnabled = false;
}
ScExternalRefManager::ApiGuard::~ApiGuard()
{
// Restore old value.
mpMgr->mbUserInteractionEnabled = mbOldInteractionEnabled;
}
// ----------------------------------------------------------------------------
void ScExternalRefManager::getAllCachedTableNames(sal_uInt16 nFileId, vector<OUString>& rTabNames) const
{
maRefCache.getAllTableNames(nFileId, rTabNames);
}
SCsTAB ScExternalRefManager::getCachedTabSpan( sal_uInt16 nFileId, const String& rStartTabName, const String& rEndTabName ) const
{
return maRefCache.getTabSpan( nFileId, rStartTabName, rEndTabName);
}
void ScExternalRefManager::getAllCachedNumberFormats(vector<sal_uInt32>& rNumFmts) const
{
maRefCache.getAllNumberFormats(rNumFmts);
}
bool ScExternalRefManager::hasCacheTable(sal_uInt16 nFileId, const String& rTabName) const
{
return maRefCache.hasCacheTable(nFileId, rTabName);
}
size_t ScExternalRefManager::getCacheTableCount(sal_uInt16 nFileId) const
{
return maRefCache.getCacheTableCount(nFileId);
}
sal_uInt16 ScExternalRefManager::getExternalFileCount() const
{
return static_cast< sal_uInt16 >( maSrcFiles.size() );
}
bool ScExternalRefManager::markUsedByLinkListeners()
{
bool bAllMarked = false;
for (LinkListenerMap::const_iterator itr = maLinkListeners.begin();
itr != maLinkListeners.end() && !bAllMarked; ++itr)
{
if (!(*itr).second.empty())
bAllMarked = maRefCache.setCacheDocReferenced( (*itr).first);
/* TODO: LinkListeners should remember the table they're listening to.
* As is, listening to one table will mark all tables of the document
* being referenced. */
}
return bAllMarked;
}
bool ScExternalRefManager::markUsedExternalRefCells()
{
RefCellMap::iterator itr = maRefCells.begin(), itrEnd = maRefCells.end();
for (; itr != itrEnd; ++itr)
{
RefCellSet::iterator itrCell = itr->second.begin(), itrCellEnd = itr->second.end();
for (; itrCell != itrCellEnd; ++itrCell)
{
ScFormulaCell* pCell = *itrCell;
bool bUsed = pCell->MarkUsedExternalReferences();
if (bUsed)
// Return true when at least one cell references external docs.
return true;
}
}
return false;
}
bool ScExternalRefManager::setCacheTableReferenced( sal_uInt16 nFileId, const String& rTabName, size_t nSheets )
{
return maRefCache.setCacheTableReferenced( nFileId, rTabName, nSheets, false);
}
void ScExternalRefManager::setCacheTableReferencedPermanently( sal_uInt16 nFileId, const String& rTabName, size_t nSheets )
{
if (isInReferenceMarking())
// Do all maintenance work.
maRefCache.setCacheTableReferenced( nFileId, rTabName, nSheets, true);
else
// Set only the permanent flag.
maRefCache.setCacheTableReferencedPermanently( nFileId, rTabName, nSheets);
}
void ScExternalRefManager::setAllCacheTableReferencedStati( bool bReferenced )
{
mbInReferenceMarking = !bReferenced;
maRefCache.setAllCacheTableReferencedStati( bReferenced );
}
void ScExternalRefManager::storeRangeNameTokens(sal_uInt16 nFileId, const String& rName, const ScTokenArray& rArray)
{
ScExternalRefCache::TokenArrayRef pArray(rArray.Clone());
maRefCache.setRangeNameTokens(nFileId, rName, pArray);
}
namespace {
/**
* Put a single cell data into internal cache table.
*
* @param pFmt optional cell format index that may need to be stored with
* the cell value.
*/
void putCellDataIntoCache(
ScExternalRefCache& rRefCache, const ScExternalRefCache::TokenRef& pToken,
sal_uInt16 nFileId, const String& rTabName, const ScAddress& rCell,
const ScExternalRefCache::CellFormat* pFmt)
{
// Now, insert the token into cache table but don't cache empty cells.
if (pToken->GetType() != formula::svEmptyCell)
{
sal_uInt32 nFmtIndex = (pFmt && pFmt->mbIsSet) ? pFmt->mnIndex : 0;
rRefCache.setCellData(nFileId, rTabName, rCell.Col(), rCell.Row(), pToken, nFmtIndex);
}
}
/**
* Put the data into our internal cache table.
*
* @param rRefCache cache table set.
* @param pArray single range data to be returned.
* @param nFileId external file ID
* @param rTabName name of the table where the data should be cached.
* @param rCacheData range data to be cached.
* @param rCacheRange original cache range, including the empty region if
* any.
* @param rDataRange reduced cache range that includes only the non-empty
* data area.
*/
void putRangeDataIntoCache(
ScExternalRefCache& rRefCache, ScExternalRefCache::TokenArrayRef& pArray,
sal_uInt16 nFileId, const String& rTabName,
const vector<ScExternalRefCache::SingleRangeData>& rCacheData,
const ScRange& rCacheRange, const ScRange& rDataRange)
{
if (pArray)
// Cache these values.
rRefCache.setCellRangeData(nFileId, rDataRange, rCacheData, pArray);
else
{
// Array is empty. Fill it with an empty matrix of the required size.
pArray.reset(lcl_fillEmptyMatrix(rCacheRange));
// Make sure to set this range 'cached', to prevent unnecessarily
// accessing the src document time and time again.
ScExternalRefCache::TableTypeRef pCacheTab =
rRefCache.getCacheTable(nFileId, rTabName, true, NULL);
if (pCacheTab)
pCacheTab->setCachedCellRange(
rCacheRange.aStart.Col(), rCacheRange.aStart.Row(), rCacheRange.aEnd.Col(), rCacheRange.aEnd.Row());
}
}
/**
* When accessing an external document for the first time, we need to
* populate the cache with all its sheet names (whether they are referenced
* or not) in the correct order. Many client codes that use external
* references make this assumption.
*
* @param rRefCache cache table set.
* @param pSrcDoc source document instance.
* @param nFileId external file ID associated with the source document.
*/
void initDocInCache(ScExternalRefCache& rRefCache, const ScDocument* pSrcDoc, sal_uInt16 nFileId)
{
if (!pSrcDoc)
return;
if (rRefCache.isDocInitialized(nFileId))
// Already initialized. No need to do this twice.
return;
SCTAB nTabCount = pSrcDoc->GetTableCount();
if (nTabCount)
{
// Populate the cache with all table names in the source document.
vector<String> aTabNames;
aTabNames.reserve(nTabCount);
for (SCTAB i = 0; i < nTabCount; ++i)
{
String aName;
pSrcDoc->GetName(i, aName);
aTabNames.push_back(aName);
}
rRefCache.initializeDoc(nFileId, aTabNames);
}
}
}
ScExternalRefCache::TokenRef ScExternalRefManager::getSingleRefToken(
sal_uInt16 nFileId, const String& rTabName, const ScAddress& rCell,
const ScAddress* pCurPos, SCTAB* pTab, ScExternalRefCache::CellFormat* pFmt)
{
if (pCurPos)
insertRefCell(nFileId, *pCurPos);
maybeLinkExternalFile(nFileId);
if (pTab)
*pTab = -1;
if (pFmt)
pFmt->mbIsSet = false;
const ScDocument* pSrcDoc = getInMemorySrcDocument(nFileId);
if (pSrcDoc)
{
// source document already loaded in memory. Re-use this instance.
SCTAB nTab;
if (!pSrcDoc->GetTable(rTabName, nTab))
{
// specified table name doesn't exist in the source document.
ScExternalRefCache::TokenRef pToken(new FormulaErrorToken(errNoRef));
return pToken;
}
if (pTab)
*pTab = nTab;
ScExternalRefCache::TokenRef pToken =
getSingleRefTokenFromSrcDoc(
nFileId, pSrcDoc, ScAddress(rCell.Col(),rCell.Row(),nTab), pFmt);
putCellDataIntoCache(maRefCache, pToken, nFileId, rTabName, rCell, pFmt);
return pToken;
}
// Check if the given table name and the cell position is cached.
sal_uInt32 nFmtIndex = 0;
ScExternalRefCache::TokenRef pToken = maRefCache.getCellData(
nFileId, rTabName, rCell.Col(), rCell.Row(), &nFmtIndex);
if (pToken)
{
// Cache hit !
fillCellFormat(nFmtIndex, pFmt);
return pToken;
}
// reference not cached. read from the source document.
pSrcDoc = getSrcDocument(nFileId);
if (!pSrcDoc)
{
// Source document not reachable. Throw a reference error.
pToken.reset(new FormulaErrorToken(errNoRef));
return pToken;
}
SCTAB nTab;
if (!pSrcDoc->GetTable(rTabName, nTab))
{
// specified table name doesn't exist in the source document.
pToken.reset(new FormulaErrorToken(errNoRef));
return pToken;
}
if (pTab)
*pTab = nTab;
SCCOL nDataCol1 = 0, nDataCol2 = MAXCOL;
SCROW nDataRow1 = 0, nDataRow2 = MAXROW;
bool bData = pSrcDoc->ShrinkToDataArea(nTab, nDataCol1, nDataRow1, nDataCol2, nDataRow2);
if (!bData || rCell.Col() < nDataCol1 || nDataCol2 < rCell.Col() || rCell.Row() < nDataRow1 || nDataRow2 < rCell.Row())
{
// requested cell is outside the data area. Don't even bother caching
// this data, but add it to the cached range to prevent accessing the
// source document time and time again.
ScExternalRefCache::TableTypeRef pCacheTab =
maRefCache.getCacheTable(nFileId, rTabName, true, NULL);
if (pCacheTab)
pCacheTab->setCachedCell(rCell.Col(), rCell.Row());
pToken.reset(new ScEmptyCellToken(false, false));
return pToken;
}
pToken = getSingleRefTokenFromSrcDoc(
nFileId, pSrcDoc, ScAddress(rCell.Col(),rCell.Row(),nTab), pFmt);
putCellDataIntoCache(maRefCache, pToken, nFileId, rTabName, rCell, pFmt);
return pToken;
}
ScExternalRefCache::TokenArrayRef ScExternalRefManager::getDoubleRefTokens(
sal_uInt16 nFileId, const String& rTabName, const ScRange& rRange, const ScAddress* pCurPos)
{
if (pCurPos)
insertRefCell(nFileId, *pCurPos);
maybeLinkExternalFile(nFileId);
ScRange aDataRange(rRange);
const ScDocument* pSrcDoc = getInMemorySrcDocument(nFileId);
if (pSrcDoc)
{
// Document already loaded in memory.
vector<ScExternalRefCache::SingleRangeData> aCacheData;
ScExternalRefCache::TokenArrayRef pArray =
getDoubleRefTokensFromSrcDoc(pSrcDoc, rTabName, aDataRange, aCacheData);
// Put the data into cache.
putRangeDataIntoCache(maRefCache, pArray, nFileId, rTabName, aCacheData, rRange, aDataRange);
return pArray;
}
// Check if the given table name and the cell position is cached.
ScExternalRefCache::TokenArrayRef pArray =
maRefCache.getCellRangeData(nFileId, rTabName, rRange);
if (pArray)
// Cache hit !
return pArray;
pSrcDoc = getSrcDocument(nFileId);
if (!pSrcDoc)
{
// Source document is not reachable. Throw a reference error.
pArray.reset(new ScTokenArray);
pArray->AddToken(FormulaErrorToken(errNoRef));
return pArray;
}
vector<ScExternalRefCache::SingleRangeData> aCacheData;
pArray = getDoubleRefTokensFromSrcDoc(pSrcDoc, rTabName, aDataRange, aCacheData);
// Put the data into cache.
putRangeDataIntoCache(maRefCache, pArray, nFileId, rTabName, aCacheData, rRange, aDataRange);
return pArray;
}
ScExternalRefCache::TokenArrayRef ScExternalRefManager::getRangeNameTokens(sal_uInt16 nFileId, const String& rName, const ScAddress* pCurPos)
{
if (pCurPos)
insertRefCell(nFileId, *pCurPos);
maybeLinkExternalFile(nFileId);
String aName = rName; // make a copy to have the casing corrected.
const ScDocument* pSrcDoc = getInMemorySrcDocument(nFileId);
if (pSrcDoc)
{
// Document already loaded in memory.
ScExternalRefCache::TokenArrayRef pArray =
getRangeNameTokensFromSrcDoc(nFileId, pSrcDoc, aName);
if (pArray)
// Cache this range name array.
maRefCache.setRangeNameTokens(nFileId, aName, pArray);
return pArray;
}
ScExternalRefCache::TokenArrayRef pArray = maRefCache.getRangeNameTokens(nFileId, rName);
if (pArray.get())
// This range name is cached.
return pArray;
pSrcDoc = getSrcDocument(nFileId);
if (!pSrcDoc)
// failed to load document from disk.
return ScExternalRefCache::TokenArrayRef();
pArray = getRangeNameTokensFromSrcDoc(nFileId, pSrcDoc, aName);
if (pArray)
// Cache this range name array.
maRefCache.setRangeNameTokens(nFileId, aName, pArray);
return pArray;
}
void ScExternalRefManager::refreshAllRefCells(sal_uInt16 nFileId)
{
RefCellMap::iterator itrFile = maRefCells.find(nFileId);
if (itrFile == maRefCells.end())
return;
RefCellSet& rRefCells = itrFile->second;
for_each(rRefCells.begin(), rRefCells.end(), UpdateFormulaCell());
ScViewData* pViewData = ScDocShell::GetViewData();
if (!pViewData)
return;
ScTabViewShell* pVShell = pViewData->GetViewShell();
if (!pVShell)
return;
// Repainting the grid also repaints the texts, but is there a better way
// to refresh texts?
pVShell->Invalidate(FID_REPAINT);
pVShell->PaintGrid();
}
void ScExternalRefManager::insertRefCell(sal_uInt16 nFileId, const ScAddress& rCell)
{
RefCellMap::iterator itr = maRefCells.find(nFileId);
if (itr == maRefCells.end())
{
RefCellSet aRefCells;
pair<RefCellMap::iterator, bool> r = maRefCells.insert(
RefCellMap::value_type(nFileId, aRefCells));
if (!r.second)
// insertion failed.
return;
itr = r.first;
}
ScBaseCell* pCell = mpDoc->GetCell(rCell);
if (pCell && pCell->GetCellType() == CELLTYPE_FORMULA)
itr->second.insert(static_cast<ScFormulaCell*>(pCell));
}
void ScExternalRefManager::fillCellFormat(sal_uInt32 nFmtIndex, ScExternalRefCache::CellFormat* pFmt) const
{
if (!pFmt)
return;
short nFmtType = mpDoc->GetFormatTable()->GetType(nFmtIndex);
if (nFmtType != NUMBERFORMAT_UNDEFINED)
{
pFmt->mbIsSet = true;
pFmt->mnIndex = nFmtIndex;
pFmt->mnType = nFmtType;
}
}
ScExternalRefCache::TokenRef ScExternalRefManager::getSingleRefTokenFromSrcDoc(
sal_uInt16 nFileId, const ScDocument* pSrcDoc, const ScAddress& rCell,
ScExternalRefCache::CellFormat* pFmt)
{
// Get the cell from src doc, and convert it into a token.
ScBaseCell* pCell = NULL;
pSrcDoc->GetCell(rCell.Col(), rCell.Row(), rCell.Tab(), pCell);
ScExternalRefCache::TokenRef pToken(lcl_convertToToken(pCell));
if (!pToken.get())
{
// Generate an error for unresolvable cells.
pToken.reset( new FormulaErrorToken( errNoValue));
}
// Get number format information.
sal_uInt32 nFmtIndex = 0;
pSrcDoc->GetNumberFormat(rCell.Col(), rCell.Row(), rCell.Tab(), nFmtIndex);
nFmtIndex = getMappedNumberFormat(nFileId, nFmtIndex, pSrcDoc);
fillCellFormat(nFmtIndex, pFmt);
return pToken;
}
ScExternalRefCache::TokenArrayRef ScExternalRefManager::getDoubleRefTokensFromSrcDoc(
const ScDocument* pSrcDoc, const String& rTabName, ScRange& rRange,
vector<ScExternalRefCache::SingleRangeData>& rCacheData)
{
ScExternalRefCache::TokenArrayRef pArray;
SCTAB nTab1;
if (!pSrcDoc->GetTable(rTabName, nTab1))
{
// specified table name doesn't exist in the source document.
pArray.reset(new ScTokenArray);
pArray->AddToken(FormulaErrorToken(errNoRef));
return pArray;
}
ScRange aRange(rRange);
SCTAB nTabSpan = aRange.aEnd.Tab() - aRange.aStart.Tab();
vector<ScExternalRefCache::SingleRangeData> aCacheData;
aCacheData.reserve(nTabSpan+1);
aCacheData.push_back(ScExternalRefCache::SingleRangeData());
aCacheData.back().maTableName = ScGlobal::pCharClass->upper(rTabName);
for (SCTAB i = 1; i < nTabSpan + 1; ++i)
{
String aTabName;
if (!pSrcDoc->GetName(nTab1 + 1, aTabName))
// source document doesn't have any table by the specified name.
break;
aCacheData.push_back(ScExternalRefCache::SingleRangeData());
aCacheData.back().maTableName = ScGlobal::pCharClass->upper(aTabName);
}
aRange.aStart.SetTab(nTab1);
aRange.aEnd.SetTab(nTab1 + nTabSpan);
pArray.reset(lcl_convertToTokenArray(pSrcDoc, aRange, aCacheData));
rRange = aRange;
rCacheData.swap(aCacheData);
return pArray;
}
ScExternalRefCache::TokenArrayRef ScExternalRefManager::getRangeNameTokensFromSrcDoc(
sal_uInt16 nFileId, const ScDocument* pSrcDoc, String& rName)
{
ScRangeName* pExtNames = pSrcDoc->GetRangeName();
String aUpperName = ScGlobal::pCharClass->upper(rName);
const ScRangeData* pRangeData = pExtNames->findByUpperName(aUpperName);
if (!pRangeData)
return ScExternalRefCache::TokenArrayRef();
// Parse all tokens in this external range data, and replace each absolute
// reference token with an external reference token, and cache them. Also
// register the source document with the link manager if it's a new
// source.
ScExternalRefCache::TokenArrayRef pNew(new ScTokenArray);
ScTokenArray aCode(*pRangeData->GetCode());
for (const FormulaToken* pToken = aCode.First(); pToken; pToken = aCode.Next())
{
bool bTokenAdded = false;
switch (pToken->GetType())
{
case svSingleRef:
{
const ScSingleRefData& rRef = static_cast<const ScToken*>(pToken)->GetSingleRef();
String aTabName;
pSrcDoc->GetName(rRef.nTab, aTabName);
ScExternalSingleRefToken aNewToken(nFileId, aTabName, static_cast<const ScToken*>(pToken)->GetSingleRef());
pNew->AddToken(aNewToken);
bTokenAdded = true;
}
break;
case svDoubleRef:
{
const ScSingleRefData& rRef = static_cast<const ScToken*>(pToken)->GetSingleRef();
String aTabName;
pSrcDoc->GetName(rRef.nTab, aTabName);
ScExternalDoubleRefToken aNewToken(nFileId, aTabName, static_cast<const ScToken*>(pToken)->GetDoubleRef());
pNew->AddToken(aNewToken);
bTokenAdded = true;
}
break;
default:
; // nothing
}
if (!bTokenAdded)
pNew->AddToken(*pToken);
}
rName = pRangeData->GetName(); // Get the correctly-cased name.
return pNew;
}
const ScDocument* ScExternalRefManager::getInMemorySrcDocument(sal_uInt16 nFileId)
{
const String* pFileName = getExternalFileName(nFileId);
if (!pFileName)
return NULL;
ScDocument* pSrcDoc = NULL;
TypeId aType(TYPE(ScDocShell));
ScDocShell* pShell = static_cast<ScDocShell*>(SfxObjectShell::GetFirst(&aType, false));
while (pShell)
{
SfxMedium* pMedium = pShell->GetMedium();
if (pMedium)
{
String aName = pMedium->GetName();
// TODO: We should make the case sensitivity platform dependent.
if (pFileName->EqualsIgnoreCaseAscii(aName))
{
// Found !
pSrcDoc = pShell->GetDocument();
break;
}
}
pShell = static_cast<ScDocShell*>(SfxObjectShell::GetNext(*pShell, &aType, false));
}
initDocInCache(maRefCache, pSrcDoc, nFileId);
return pSrcDoc;
}
const ScDocument* ScExternalRefManager::getSrcDocument(sal_uInt16 nFileId)
{
if (!mpDoc->IsExecuteLinkEnabled())
return NULL;
DocShellMap::iterator itrEnd = maDocShells.end();
DocShellMap::iterator itr = maDocShells.find(nFileId);
if (itr != itrEnd)
{
// document already loaded.
SfxObjectShell* p = itr->second.maShell;
itr->second.maLastAccess = Time();
return static_cast<ScDocShell*>(p)->GetDocument();
}
const String* pFile = getExternalFileName(nFileId);
if (!pFile)
// no file name associated with this ID.
return NULL;
String aFilter;
SrcShell aSrcDoc;
aSrcDoc.maShell = loadSrcDocument(nFileId, aFilter);
if (!aSrcDoc.maShell.Is())
{
// source document could not be loaded.
return NULL;
}
if (maDocShells.empty())
{
// If this is the first source document insertion, start up the timer.
maSrcDocTimer.Start();
}
maDocShells.insert(DocShellMap::value_type(nFileId, aSrcDoc));
SfxObjectShell* p = aSrcDoc.maShell;
ScDocument* pSrcDoc = static_cast<ScDocShell*>(p)->GetDocument();
initDocInCache(maRefCache, pSrcDoc, nFileId);
return pSrcDoc;
}
SfxObjectShellRef ScExternalRefManager::loadSrcDocument(sal_uInt16 nFileId, String& rFilter)
{
const SrcFileData* pFileData = getExternalFileData(nFileId);
if (!pFileData)
return NULL;
// Always load the document by using the path created from the relative
// path. If the referenced document is not there, simply exit. The
// original file name should be used only when the relative path is not
// given.
String aFile = pFileData->maFileName;
maybeCreateRealFileName(nFileId);
if (pFileData->maRealFileName.Len())
aFile = pFileData->maRealFileName;
if (!isFileLoadable(aFile))
return NULL;
String aOptions( pFileData->maFilterOptions );
ScDocumentLoader::GetFilterName(aFile, rFilter, aOptions, true, false);
const SfxFilter* pFilter = ScDocShell::Factory().GetFilterContainer()->GetFilter4FilterName(rFilter);
if (!pFileData->maRelativeName.Len())
{
// Generate a relative file path.
INetURLObject aBaseURL(getOwnDocumentName());
aBaseURL.insertName(OUString(RTL_CONSTASCII_USTRINGPARAM("content.xml")));
String aStr = URIHelper::simpleNormalizedMakeRelative(
aBaseURL.GetMainURL(INetURLObject::NO_DECODE), aFile);
setRelativeFileName(nFileId, aStr);
}
SfxItemSet* pSet = new SfxAllItemSet(SFX_APP()->GetPool());
if (aOptions.Len())
pSet->Put(SfxStringItem(SID_FILE_FILTEROPTIONS, aOptions));
// make medium hidden to prevent assertion from progress bar
pSet->Put( SfxBoolItem( SID_HIDDEN, sal_True ) );
auto_ptr<SfxMedium> pMedium(new SfxMedium(aFile, STREAM_STD_READ, false, pFilter, pSet));
if (pMedium->GetError() != ERRCODE_NONE)
return NULL;
// To load encrypted documents with password, user interaction needs to be enabled.
pMedium->UseInteractionHandler(mbUserInteractionEnabled);
ScDocShell* pNewShell = new ScDocShell(SFX_CREATE_MODE_INTERNAL);
SfxObjectShellRef aRef = pNewShell;
// increment the recursive link count of the source document.
ScExtDocOptions* pExtOpt = mpDoc->GetExtDocOptions();
sal_uInt32 nLinkCount = pExtOpt ? pExtOpt->GetDocSettings().mnLinkCnt : 0;
ScDocument* pSrcDoc = pNewShell->GetDocument();
pSrcDoc->EnableExecuteLink(false); // to prevent circular access of external references.
pSrcDoc->EnableUndo(false);
pSrcDoc->EnableAdjustHeight(false);
ScExtDocOptions* pExtOptNew = pSrcDoc->GetExtDocOptions();
if (!pExtOptNew)
{
pExtOptNew = new ScExtDocOptions;
pSrcDoc->SetExtDocOptions(pExtOptNew);
}
pExtOptNew->GetDocSettings().mnLinkCnt = nLinkCount + 1;
pNewShell->DoLoad(pMedium.release());
// with UseInteractionHandler, options may be set by dialog during DoLoad
String aNew = ScDocumentLoader::GetOptions(*pNewShell->GetMedium());
if (aNew.Len() && aNew != aOptions)
aOptions = aNew;
setFilterData(nFileId, rFilter, aOptions); // update the filter data, including the new options
return aRef;
}
bool ScExternalRefManager::isFileLoadable(const String& rFile) const
{
if (!rFile.Len())
return false;
if (isOwnDocument(rFile))
return false;
if (utl::UCBContentHelper::IsFolder(rFile))
return false;
return utl::UCBContentHelper::Exists(rFile);
}
void ScExternalRefManager::maybeLinkExternalFile(sal_uInt16 nFileId)
{
if (maLinkedDocs.count(nFileId))
// file alerady linked, or the link has been broken.
return;
// Source document not linked yet. Link it now.
const String* pFileName = getExternalFileName(nFileId);
if (!pFileName)
return;
String aFilter, aOptions;
ScDocumentLoader::GetFilterName(*pFileName, aFilter, aOptions, true, false);
sfx2::LinkManager* pLinkMgr = mpDoc->GetLinkManager();
ScExternalRefLink* pLink = new ScExternalRefLink(mpDoc, nFileId, aFilter);
DBG_ASSERT(pFileName, "ScExternalRefManager::insertExternalFileLink: file name pointer is NULL");
pLinkMgr->InsertFileLink(*pLink, OBJECT_CLIENT_FILE, *pFileName, &aFilter);
pLink->SetDoReferesh(false);
pLink->Update();
pLink->SetDoReferesh(true);
maLinkedDocs.insert(LinkedDocMap::value_type(nFileId, true));
}
void ScExternalRefManager::SrcFileData::maybeCreateRealFileName(const String& rOwnDocName)
{
if (!maRelativeName.Len())
// No relative path given. Nothing to do.
return;
if (maRealFileName.Len())
// Real file name already created. Nothing to do.
return;
// Formulate the absolute file path from the relative path.
const String& rRelPath = maRelativeName;
INetURLObject aBaseURL(rOwnDocName);
aBaseURL.insertName(OUString(RTL_CONSTASCII_USTRINGPARAM("content.xml")));
bool bWasAbs = false;
maRealFileName = aBaseURL.smartRel2Abs(rRelPath, bWasAbs).GetMainURL(INetURLObject::NO_DECODE);
}
void ScExternalRefManager::maybeCreateRealFileName(sal_uInt16 nFileId)
{
if (nFileId >= maSrcFiles.size())
return;
maSrcFiles[nFileId].maybeCreateRealFileName(getOwnDocumentName());
}
const String& ScExternalRefManager::getOwnDocumentName() const
{
SfxObjectShell* pShell = mpDoc->GetDocumentShell();
if (!pShell)
// This should not happen!
return EMPTY_STRING;
SfxMedium* pMed = pShell->GetMedium();
if (!pMed)
return EMPTY_STRING;
return pMed->GetName();
}
bool ScExternalRefManager::isOwnDocument(const String& rFile) const
{
return getOwnDocumentName().Equals(rFile);
}
void ScExternalRefManager::convertToAbsName(String& rFile) const
{
SfxObjectShell* pDocShell = mpDoc->GetDocumentShell();
rFile = ScGlobal::GetAbsDocName(rFile, pDocShell);
}
sal_uInt16 ScExternalRefManager::getExternalFileId(const String& rFile)
{
vector<SrcFileData>::const_iterator itrBeg = maSrcFiles.begin(), itrEnd = maSrcFiles.end();
vector<SrcFileData>::const_iterator itr = find_if(itrBeg, itrEnd, FindSrcFileByName(rFile));
if (itr != itrEnd)
{
size_t nId = distance(itrBeg, itr);
return static_cast<sal_uInt16>(nId);
}
SrcFileData aData;
aData.maFileName = rFile;
maSrcFiles.push_back(aData);
return static_cast<sal_uInt16>(maSrcFiles.size() - 1);
}
const String* ScExternalRefManager::getExternalFileName(sal_uInt16 nFileId, bool bForceOriginal)
{
if (nFileId >= maSrcFiles.size())
return NULL;
if (bForceOriginal)
return &maSrcFiles[nFileId].maFileName;
maybeCreateRealFileName(nFileId);
if (maSrcFiles[nFileId].maRealFileName.Len())
return &maSrcFiles[nFileId].maRealFileName;
else
return &maSrcFiles[nFileId].maFileName;
}
bool ScExternalRefManager::hasExternalFile(sal_uInt16 nFileId) const
{
return nFileId < maSrcFiles.size();
}
bool ScExternalRefManager::hasExternalFile(const String& rFile) const
{
vector<SrcFileData>::const_iterator itrBeg = maSrcFiles.begin(), itrEnd = maSrcFiles.end();
vector<SrcFileData>::const_iterator itr = find_if(itrBeg, itrEnd, FindSrcFileByName(rFile));
return itr != itrEnd;
}
const ScExternalRefManager::SrcFileData* ScExternalRefManager::getExternalFileData(sal_uInt16 nFileId) const
{
if (nFileId >= maSrcFiles.size())
return NULL;
return &maSrcFiles[nFileId];
}
const String* ScExternalRefManager::getRealTableName(sal_uInt16 nFileId, const String& rTabName) const
{
return maRefCache.getRealTableName(nFileId, rTabName);
}
const String* ScExternalRefManager::getRealRangeName(sal_uInt16 nFileId, const String& rRangeName) const
{
return maRefCache.getRealRangeName(nFileId, rRangeName);
}
template<typename MapContainer>
void lcl_removeByFileId(sal_uInt16 nFileId, MapContainer& rMap)
{
typename MapContainer::iterator itr = rMap.find(nFileId);
if (itr != rMap.end())
rMap.erase(itr);
}
void ScExternalRefManager::refreshNames(sal_uInt16 nFileId)
{
maRefCache.clearCache(nFileId);
lcl_removeByFileId(nFileId, maDocShells);
if (maDocShells.empty())
maSrcDocTimer.Stop();
// Update all cells containing names from this source document.
refreshAllRefCells(nFileId);
notifyAllLinkListeners(nFileId, LINK_MODIFIED);
}
void ScExternalRefManager::breakLink(sal_uInt16 nFileId)
{
// Turn all formula cells referencing this external document into static
// cells.
RefCellMap::iterator itrRefs = maRefCells.find(nFileId);
if (itrRefs != maRefCells.end())
{
// Make a copy because removing the formula cells below will modify
// the original container.
RefCellSet aSet = itrRefs->second;
for_each(aSet.begin(), aSet.end(), ConvertFormulaToStatic(mpDoc));
maRefCells.erase(nFileId);
}
lcl_removeByFileId(nFileId, maDocShells);
if (maDocShells.empty())
maSrcDocTimer.Stop();
LinkedDocMap::iterator itr = maLinkedDocs.find(nFileId);
if (itr != maLinkedDocs.end())
itr->second = false;
notifyAllLinkListeners(nFileId, LINK_BROKEN);
}
void ScExternalRefManager::switchSrcFile(sal_uInt16 nFileId, const String& rNewFile, const String& rNewFilter)
{
maSrcFiles[nFileId].maFileName = rNewFile;
maSrcFiles[nFileId].maRelativeName.Erase();
maSrcFiles[nFileId].maRealFileName.Erase();
if (!maSrcFiles[nFileId].maFilterName.Equals(rNewFilter))
{
// Filter type has changed.
maSrcFiles[nFileId].maFilterName = rNewFilter;
maSrcFiles[nFileId].maFilterOptions.Erase();
}
refreshNames(nFileId);
}
void ScExternalRefManager::setRelativeFileName(sal_uInt16 nFileId, const String& rRelUrl)
{
if (nFileId >= maSrcFiles.size())
return;
maSrcFiles[nFileId].maRelativeName = rRelUrl;
}
void ScExternalRefManager::setFilterData(sal_uInt16 nFileId, const String& rFilterName, const String& rOptions)
{
if (nFileId >= maSrcFiles.size())
return;
maSrcFiles[nFileId].maFilterName = rFilterName;
maSrcFiles[nFileId].maFilterOptions = rOptions;
}
void ScExternalRefManager::clear()
{
DocShellMap::iterator itrEnd = maDocShells.end();
for (DocShellMap::iterator itr = maDocShells.begin(); itr != itrEnd; ++itr)
itr->second.maShell->DoClose();
maDocShells.clear();
maSrcDocTimer.Stop();
}
bool ScExternalRefManager::hasExternalData() const
{
return !maSrcFiles.empty();
}
void ScExternalRefManager::resetSrcFileData(const String& rBaseFileUrl)
{
for (vector<SrcFileData>::iterator itr = maSrcFiles.begin(), itrEnd = maSrcFiles.end();
itr != itrEnd; ++itr)
{
// Re-generate relative file name from the absolute file name.
String aAbsName = itr->maRealFileName;
if (!aAbsName.Len())
aAbsName = itr->maFileName;
itr->maRelativeName = URIHelper::simpleNormalizedMakeRelative(
rBaseFileUrl, aAbsName);
}
}
void ScExternalRefManager::removeRefCell(ScFormulaCell* pCell)
{
for_each(maRefCells.begin(), maRefCells.end(), RemoveFormulaCell(pCell));
}
void ScExternalRefManager::addLinkListener(sal_uInt16 nFileId, LinkListener* pListener)
{
LinkListenerMap::iterator itr = maLinkListeners.find(nFileId);
if (itr == maLinkListeners.end())
{
pair<LinkListenerMap::iterator, bool> r = maLinkListeners.insert(
LinkListenerMap::value_type(nFileId, LinkListeners()));
if (!r.second)
{
OSL_FAIL("insertion of new link listener list failed");
return;
}
itr = r.first;
}
LinkListeners& rList = itr->second;
rList.insert(pListener);
}
void ScExternalRefManager::removeLinkListener(sal_uInt16 nFileId, LinkListener* pListener)
{
LinkListenerMap::iterator itr = maLinkListeners.find(nFileId);
if (itr == maLinkListeners.end())
// no listeners for a specified file.
return;
LinkListeners& rList = itr->second;
rList.erase(pListener);
if (rList.empty())
// No more listeners for this file. Remove its entry.
maLinkListeners.erase(itr);
}
void ScExternalRefManager::removeLinkListener(LinkListener* pListener)
{
LinkListenerMap::iterator itr = maLinkListeners.begin(), itrEnd = maLinkListeners.end();
for (; itr != itrEnd; ++itr)
itr->second.erase(pListener);
}
void ScExternalRefManager::notifyAllLinkListeners(sal_uInt16 nFileId, LinkUpdateType eType)
{
LinkListenerMap::iterator itr = maLinkListeners.find(nFileId);
if (itr == maLinkListeners.end())
// no listeners for a specified file.
return;
LinkListeners& rList = itr->second;
for_each(rList.begin(), rList.end(), NotifyLinkListener(nFileId, eType));
}
void ScExternalRefManager::purgeStaleSrcDocument(sal_Int32 nTimeOut)
{
DocShellMap aNewDocShells;
DocShellMap::iterator itr = maDocShells.begin(), itrEnd = maDocShells.end();
for (; itr != itrEnd; ++itr)
{
// in 100th of a second.
sal_Int32 nSinceLastAccess = (Time() - itr->second.maLastAccess).GetTime();
if (nSinceLastAccess < nTimeOut)
aNewDocShells.insert(*itr);
}
maDocShells.swap(aNewDocShells);
if (maDocShells.empty())
maSrcDocTimer.Stop();
}
sal_uInt32 ScExternalRefManager::getMappedNumberFormat(sal_uInt16 nFileId, sal_uInt32 nNumFmt, const ScDocument* pSrcDoc)
{
NumFmtMap::iterator itr = maNumFormatMap.find(nFileId);
if (itr == maNumFormatMap.end())
{
// Number formatter map is not initialized for this external document.
pair<NumFmtMap::iterator, bool> r = maNumFormatMap.insert(
NumFmtMap::value_type(nFileId, SvNumberFormatterMergeMap()));
if (!r.second)
// insertion failed.
return nNumFmt;
itr = r.first;
mpDoc->GetFormatTable()->MergeFormatter( *pSrcDoc->GetFormatTable());
SvNumberFormatterMergeMap aMap = mpDoc->GetFormatTable()->ConvertMergeTableToMap();
itr->second.swap(aMap);
}
const SvNumberFormatterMergeMap& rMap = itr->second;
SvNumberFormatterMergeMap::const_iterator itrNumFmt = rMap.find(nNumFmt);
if (itrNumFmt != rMap.end())
// mapped value found.
return itrNumFmt->second;
return nNumFmt;
}
IMPL_LINK(ScExternalRefManager, TimeOutHdl, AutoTimer*, pTimer)
{
if (pTimer == &maSrcDocTimer)
purgeStaleSrcDocument(SRCDOC_LIFE_SPAN);
return 0;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|