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
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
|
/* -*- 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 <browserids.hxx>
#include <core_resource.hxx>
#include <helpids.h>
#include <dbtreelistbox.hxx>
#include "dbtreemodel.hxx"
#include <strings.hrc>
#include <imageprovider.hxx>
#include <sbagrid.hxx>
#include <strings.hxx>
#include <UITools.hxx>
#include <unodatbr.hxx>
#include <com/sun/star/awt/MouseWheelBehavior.hpp>
#include <com/sun/star/awt/TextAlign.hpp>
#include <com/sun/star/awt/VisualEffect.hpp>
#include <com/sun/star/beans/NamedValue.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/XMultiPropertySet.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/form/XGridColumnFactory.hpp>
#include <com/sun/star/form/XLoadable.hpp>
#include <com/sun/star/form/XReset.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/frame/FrameSearchFlag.hpp>
#include <com/sun/star/frame/XLayoutManager.hpp>
#include <com/sun/star/lang/DisposedException.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/i18n/Collator.hpp>
#include <com/sun/star/sdb/CommandType.hpp>
#include <com/sun/star/sdb/SQLContext.hpp>
#include <com/sun/star/sdb/XDatabaseContext.hpp>
#include <com/sun/star/sdb/XDatabaseRegistrations.hpp>
#include <com/sun/star/sdb/XDocumentDataSource.hpp>
#include <com/sun/star/sdb/XParametersSupplier.hpp>
#include <com/sun/star/sdb/XQueriesSupplier.hpp>
#include <com/sun/star/sdb/XQueryDefinitionsSupplier.hpp>
#include <com/sun/star/sdb/XResultSetAccess.hpp>
#include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
#include <com/sun/star/sdb/application/NamedDatabaseObject.hpp>
#include <com/sun/star/sdbc/ColumnValue.hpp>
#include <com/sun/star/sdbc/DataType.hpp>
#include <com/sun/star/sdbc/FetchDirection.hpp>
#include <com/sun/star/sdbc/SQLWarning.hpp>
#include <com/sun/star/sdbc/XDataSource.hpp>
#include <com/sun/star/sdbc/XWarningsSupplier.hpp>
#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
#include <com/sun/star/sdbcx/XViewsSupplier.hpp>
#include <com/sun/star/task/InteractionHandler.hpp>
#include <com/sun/star/util/XFlushable.hpp>
#include <com/sun/star/util/XNumberFormatter.hpp>
#include <com/sun/star/util/XURLTransformer.hpp>
#include <com/sun/star/document/MacroExecMode.hpp>
#include <com/sun/star/ui/XContextMenuInterceptor.hpp>
#include <comphelper/extract.hxx>
#include <comphelper/sequence.hxx>
#include <comphelper/types.hxx>
#include <connectivity/dbexception.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <svl/filenotation.hxx>
#include <svx/dataaccessdescriptor.hxx>
#include <svx/databaseregistrationui.hxx>
#include <toolkit/helper/vclunohelper.hxx>
#include <comphelper/diagnose_ex.hxx>
#include <osl/diagnose.h>
#include <sal/log.hxx>
#include <tools/multisel.hxx>
#include <tools/urlobj.hxx>
#include <unotools/confignode.hxx>
#include <utility>
#include <vcl/split.hxx>
#include <vcl/svapp.hxx>
#include <vcl/toolbox.hxx>
#include <vcl/settings.hxx>
#include <vcl/weld.hxx>
#include <memory>
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::awt;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::sdb::application;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::frame;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::ui::dialogs;
using namespace ::com::sun::star::task;
using namespace ::com::sun::star::form;
using namespace ::com::sun::star::i18n;
using namespace ::com::sun::star::view;
using namespace ::com::sun::star::datatransfer;
using namespace ::com::sun::star::document;
using namespace ::com::sun::star::ui;
using namespace ::dbtools;
using namespace ::comphelper;
using namespace ::svx;
// SbaTableQueryBrowser
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
org_openoffice_comp_dbu_ODatasourceBrowser_get_implementation(
css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
{
SolarMutexGuard aGuard;
return cppu::acquire(new ::dbaui::SbaTableQueryBrowser(context));
}
namespace dbaui
{
namespace DatabaseObject = css::sdb::application::DatabaseObject;
namespace DatabaseObjectContainer = css::sdb::application::DatabaseObjectContainer;
static void SafeAddPropertyListener(const Reference< XPropertySet > & xSet, const OUString& rPropName, XPropertyChangeListener* pListener)
{
Reference< XPropertySetInfo > xInfo = xSet->getPropertySetInfo();
if (xInfo->hasPropertyByName(rPropName))
xSet->addPropertyChangeListener(rPropName, pListener);
}
static void SafeRemovePropertyListener(const Reference< XPropertySet > & xSet, const OUString& rPropName, XPropertyChangeListener* pListener)
{
Reference< XPropertySetInfo > xInfo = xSet->getPropertySetInfo();
if (xInfo->hasPropertyByName(rPropName))
xSet->removePropertyChangeListener(rPropName, pListener);
}
OUString SAL_CALL SbaTableQueryBrowser::getImplementationName()
{
return u"org.openoffice.comp.dbu.ODatasourceBrowser"_ustr;
}
css::uno::Sequence<OUString> SAL_CALL SbaTableQueryBrowser::getSupportedServiceNames()
{
return { u"com.sun.star.sdb.DataSourceBrowser"_ustr };
}
SbaTableQueryBrowser::SbaTableQueryBrowser(const Reference< XComponentContext >& _rM)
:SbaXDataBrowserController(_rM)
,m_aSelectionListeners( getMutex() )
,m_aContextMenuInterceptors( getMutex() )
,m_aTableCopyHelper(this)
,m_pTreeView(nullptr)
,m_pSplitter(nullptr)
,m_nAsyncDrop(nullptr)
,m_bQueryEscapeProcessing( false )
,m_bShowMenu(false)
,m_bInSuspend(false)
,m_bEnableBrowser(true)
{
}
SbaTableQueryBrowser::~SbaTableQueryBrowser()
{
if ( !rBHelper.bDisposed && !rBHelper.bInDispose )
{
SAL_WARN("dbaccess.ui", "Please check who doesn't dispose this component!");
// increment ref count to prevent double call of Dtor
osl_atomic_increment( &m_refCount );
dispose();
}
SolarMutexGuard g;
m_pTreeView.reset();
m_pSplitter.reset();
}
Any SAL_CALL SbaTableQueryBrowser::queryInterface(const Type& _rType)
{
if ( _rType.equals( cppu::UnoType<XScriptInvocationContext>::get() ) )
{
OSL_PRECOND( m_aDocScriptSupport.has_value(), "SbaTableQueryBrowser::queryInterface: did not initialize this, yet!" );
if ( m_aDocScriptSupport.has_value() && *m_aDocScriptSupport )
return Any( Reference< XScriptInvocationContext >( this ) );
return Any();
}
Any aReturn = SbaXDataBrowserController::queryInterface(_rType);
if (!aReturn.hasValue())
aReturn = SbaTableQueryBrowser_Base::queryInterface(_rType);
return aReturn;
}
Sequence< Type > SAL_CALL SbaTableQueryBrowser::getTypes( )
{
Sequence< Type > aTypes( ::comphelper::concatSequences(
SbaXDataBrowserController::getTypes(),
SbaTableQueryBrowser_Base::getTypes()
) );
OSL_PRECOND( m_aDocScriptSupport.has_value(), "SbaTableQueryBrowser::getTypes: did not initialize this, yet!" );
if ( !m_aDocScriptSupport.has_value() || !*m_aDocScriptSupport )
{
auto [begin, end] = asNonConstRange(aTypes);
auto newEnd = std::remove_if( begin, end,
[](const Type& type)
{ return type == cppu::UnoType<XScriptInvocationContext>::get(); } );
aTypes.realloc( std::distance(begin, newEnd) );
}
return aTypes;
}
Sequence< sal_Int8 > SAL_CALL SbaTableQueryBrowser::getImplementationId( )
{
return css::uno::Sequence<sal_Int8>();
}
void SAL_CALL SbaTableQueryBrowser::disposing()
{
SolarMutexGuard aGuard;
// doin' a lot of VCL stuff here -> lock the SolarMutex
// kiss our listeners goodbye
css::lang::EventObject aEvt(*this);
m_aSelectionListeners.disposeAndClear(aEvt);
m_aContextMenuInterceptors.disposeAndClear(aEvt);
if (getBrowserView())
{
// Need to do some cleanup of the data pointed to the tree view entries before we remove the treeview
clearTreeModel();
m_pTreeView = nullptr;
getBrowserView()->setTreeView(nullptr);
}
// remove ourself as status listener
implRemoveStatusListeners();
// check out from all the objects we are listening
// the frame
if (m_xCurrentFrameParent.is())
m_xCurrentFrameParent->removeFrameActionListener(static_cast<css::frame::XFrameActionListener*>(this));
// remove the container listener from the database context
try
{
Reference< XDatabaseRegistrations > xDatabaseRegistrations( m_xDatabaseContext, UNO_QUERY_THROW );
xDatabaseRegistrations->removeDatabaseRegistrationsListener( this );
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
SbaXDataBrowserController::disposing();
}
bool SbaTableQueryBrowser::Construct(vcl::Window* pParent)
{
if ( !SbaXDataBrowserController::Construct( pParent ) )
return false;
try
{
Reference< XDatabaseRegistrations > xDatabaseRegistrations( m_xDatabaseContext, UNO_QUERY_THROW );
xDatabaseRegistrations->addDatabaseRegistrationsListener( this );
// the collator for the string compares
m_xCollator = Collator::create( getORB() );
m_xCollator->loadDefaultCollator( Application::GetSettings().GetLanguageTag().getLocale(), 0 );
}
catch(const Exception&)
{
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::Construct: could not create (or start listening at) the database context!");
}
// some help ids
if (!getBrowserView() || !getBrowserView()->getVclControl())
return true;
// create controls and set sizes
const tools::Long nFrameWidth = getBrowserView()->LogicToPixel(::Size(3, 0), MapMode(MapUnit::MapAppFont)).Width();
m_pSplitter = VclPtr<Splitter>::Create(getBrowserView(),WB_HSCROLL);
m_pSplitter->SetPosSizePixel( ::Point(0,0), ::Size(nFrameWidth,0) );
m_pSplitter->SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetDialogColor() ) );
m_pTreeView = VclPtr<InterimDBTreeListBox>::Create(getBrowserView());
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
rTreeView.connect_expanding(LINK(this, SbaTableQueryBrowser, OnExpandEntry));
m_pTreeView->setCopyHandler(LINK(this, SbaTableQueryBrowser, OnCopyEntry));
m_pTreeView->setContextMenuProvider( this );
m_pTreeView->setControlActionListener( this );
m_pTreeView->SetHelpId(HID_CTL_TREEVIEW);
// a default pos for the splitter, so that the listbox is about 80 (logical) pixels wide
m_pSplitter->SetSplitPosPixel(getBrowserView()->LogicToPixel(::Size(80, 0), MapMode(MapUnit::MapAppFont)).Width());
getBrowserView()->setSplitter(m_pSplitter);
getBrowserView()->setTreeView(m_pTreeView);
// fill view with data
rTreeView.set_sort_order(true);
rTreeView.set_sort_func([this](const weld::TreeIter& rLeft, const weld::TreeIter& rRight){
return OnTreeEntryCompare(rLeft, rRight);
});
rTreeView.make_sorted();
m_pTreeView->SetSelChangeHdl(LINK(this, SbaTableQueryBrowser, OnSelectionChange));
m_pTreeView->show_container();
// TODO
getBrowserView()->getVclControl()->SetHelpId(HID_CTL_TABBROWSER);
if (getBrowserView()->getVclControl()->GetHeaderBar())
getBrowserView()->getVclControl()->GetHeaderBar()->SetHelpId(HID_DATABROWSE_HEADER);
InvalidateFeature(ID_BROWSER_EXPLORER);
return true;
}
namespace
{
struct SelectValueByName
{
const Any& operator()( OUString const& i_name ) const
{
return m_rCollection.get( i_name );
}
explicit SelectValueByName( ::comphelper::NamedValueCollection const& i_collection )
:m_rCollection( i_collection )
{
}
::comphelper::NamedValueCollection const& m_rCollection;
};
}
void SbaTableQueryBrowser::impl_sanitizeRowSetClauses_nothrow()
{
try
{
Reference< XPropertySet > xRowSetProps( getRowSet(), UNO_QUERY_THROW );
bool bEscapeProcessing = false;
OSL_VERIFY( xRowSetProps->getPropertyValue( PROPERTY_ESCAPE_PROCESSING ) >>= bEscapeProcessing );
if ( !bEscapeProcessing )
// don't touch or interpret anything if escape processing is disabled
return;
Reference< XSingleSelectQueryComposer > xComposer( createParser_nothrow() );
if ( !xComposer.is() )
// can't do anything. Already reported via assertion in createParser_nothrow.
return;
// the tables participating in the statement
const Reference< XTablesSupplier > xSuppTables( xComposer, UNO_QUERY_THROW );
const Reference< XNameAccess > xTableNames( xSuppTables->getTables(), UNO_SET_THROW );
// the columns participating in the statement
const Reference< XColumnsSupplier > xSuppColumns( xComposer, UNO_QUERY_THROW );
const Reference< XNameAccess > xColumnNames( xSuppColumns->getColumns(), UNO_SET_THROW );
// check if the order columns apply to tables which really exist in the statement
const Reference< XIndexAccess > xOrderColumns( xComposer->getOrderColumns(), UNO_SET_THROW );
const sal_Int32 nOrderColumns( xOrderColumns->getCount() );
bool invalidColumn = nOrderColumns == 0;
for ( sal_Int32 c=0; ( c < nOrderColumns ) && !invalidColumn; ++c )
{
const Reference< XPropertySet > xOrderColumn( xOrderColumns->getByIndex(c), UNO_QUERY_THROW );
OUString sTableName;
OSL_VERIFY( xOrderColumn->getPropertyValue( PROPERTY_TABLENAME ) >>= sTableName );
OUString sColumnName;
OSL_VERIFY( xOrderColumn->getPropertyValue( PROPERTY_NAME ) >>= sColumnName );
if ( sTableName.isEmpty() )
{
if ( !xColumnNames->hasByName( sColumnName ) )
{
invalidColumn = true;
break;
}
}
else
{
if ( !xTableNames->hasByName( sTableName ) )
{
invalidColumn = true;
break;
}
const Reference< XColumnsSupplier > xSuppTableColumns( xTableNames->getByName( sTableName ), UNO_QUERY_THROW );
const Reference< XNameAccess > xTableColumnNames( xSuppTableColumns->getColumns(), UNO_SET_THROW );
if ( !xTableColumnNames->hasByName( sColumnName ) )
{
invalidColumn = true;
break;
}
}
}
if ( invalidColumn )
{
// reset the complete order statement at both the row set and the parser
xRowSetProps->setPropertyValue( PROPERTY_ORDER, Any( OUString() ) );
xComposer->setOrder( u""_ustr );
}
// check if the columns participating in the filter refer to existing tables
// TODO: there's no API at all for this. The method which comes nearest to what we need is
// "getStructuredFilter", but it returns pure column names only. That is, for a statement like
// "SELECT * FROM <table> WHERE <other_table>.<column> = <value>", it will return "<column>". But
// there's no API at all to retrieve the information about "<other_table>" - which is what would
// be needed here.
// That'd be a chance to replace getStructuredFilter with something more reasonable.
// So, what really would be handy, is some
// XNormalizedFilter getNormalizedFilter();
// with
// interface XDisjunctiveFilterExpression
// {
// XConjunctiveFilterTerm getTerm( int index );
// }
// interface XConjunctiveFilterTerm
// {
// ComparisonPredicate getPredicate( int index );
// }
// struct ComparisonPredicate
// {
// XComparisonOperand Lhs;
// SQLFilterOperator Operator;
// XComparisonOperand Rhs;
// }
// interface XComparisonOperand
// {
// SQLFilterOperand Type;
// XPropertySet getColumn();
// string getLiteral();
// ...
// }
// enum SQLFilterOperand { Column, Literal, ... }
// ... or something like this...
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
bool SbaTableQueryBrowser::InitializeForm( const Reference< XPropertySet > & i_formProperties )
{
if (!m_xCurrentlyDisplayed)
return true;
// this method set all format settings from the original table or query
try
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*m_xCurrentlyDisplayed));
ENSURE_OR_RETURN_FALSE( pData, "SbaTableQueryBrowser::InitializeForm: No user data set at the currently displayed entry!" );
ENSURE_OR_RETURN_FALSE( pData->xObjectProperties.is(), "SbaTableQueryBrowser::InitializeForm: No table available!" );
Reference< XPropertySetInfo > xPSI( pData->xObjectProperties->getPropertySetInfo(), UNO_SET_THROW );
::comphelper::NamedValueCollection aPropertyValues;
const OUString aTransferProperties[] =
{
PROPERTY_APPLYFILTER,
PROPERTY_FILTER,
PROPERTY_HAVING_CLAUSE,
PROPERTY_ORDER
};
for (const auto & aTransferProperty : aTransferProperties)
{
if ( !xPSI->hasPropertyByName( aTransferProperty ) )
continue;
aPropertyValues.put( aTransferProperty, pData->xObjectProperties->getPropertyValue( aTransferProperty ) );
}
std::vector< OUString > aNames( aPropertyValues.getNames() );
std::sort(aNames.begin(), aNames.end());
Sequence< OUString > aPropNames( comphelper::containerToSequence(aNames) );
Sequence< Any > aPropValues( aNames.size() );
std::transform( aNames.begin(), aNames.end(), aPropValues.getArray(), SelectValueByName( aPropertyValues ) );
Reference< XMultiPropertySet > xFormMultiSet( i_formProperties, UNO_QUERY_THROW );
xFormMultiSet->setPropertyValues( aPropNames, aPropValues );
impl_sanitizeRowSetClauses_nothrow();
}
catch ( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
return false;
}
return true;
}
void SbaTableQueryBrowser::initializePreviewMode()
{
if ( getBrowserView() && getBrowserView()->getVclControl() )
{
getBrowserView()->getVclControl()->AlwaysEnableInput( false );
getBrowserView()->getVclControl()->EnableInput( false );
getBrowserView()->getVclControl()->ForceHideScrollbars();
}
Reference< XPropertySet > xDataSourceSet(getRowSet(), UNO_QUERY);
if ( xDataSourceSet.is() )
{
xDataSourceSet->setPropertyValue(u"AllowInserts"_ustr,Any(false));
xDataSourceSet->setPropertyValue(u"AllowUpdates"_ustr,Any(false));
xDataSourceSet->setPropertyValue(u"AllowDeletes"_ustr,Any(false));
}
}
void SbaTableQueryBrowser::InitializeGridModel(const Reference< css::form::XFormComponent > & xGrid)
{
try
{
Reference< css::form::XGridColumnFactory > xColFactory(xGrid, UNO_QUERY);
Reference< XNameContainer > xColContainer(xGrid, UNO_QUERY);
clearGridColumns( xColContainer );
Reference< XLoadable > xFormAsLoadable;
if (xGrid.is())
xFormAsLoadable.set(xGrid->getParent(), css::uno::UNO_QUERY);
if (xFormAsLoadable.is() && xFormAsLoadable->isLoaded())
{
// set the formats from the table
if (m_xCurrentlyDisplayed)
{
Sequence< OUString> aProperties(6 + ( m_bPreview ? 5 : 0 ));
Sequence< Any> aValues(6 + ( m_bPreview ? 5 : 0 ));
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*m_xCurrentlyDisplayed));
OSL_ENSURE( pData->xObjectProperties.is(), "SbaTableQueryBrowser::InitializeGridModel: No table available!" );
if ( !pData->xObjectProperties.is() )
return;
OUString* pStringIter = aProperties.getArray();
Any* pValueIter = aValues.getArray();
if ( m_bPreview )
{
*pStringIter++ = "AlwaysShowCursor";
*pValueIter++ <<= false;
*pStringIter++ = PROPERTY_BORDER;
*pValueIter++ <<= sal_Int16(0);
}
*pStringIter++ = PROPERTY_FONT;
*pValueIter++ = pData->xObjectProperties->getPropertyValue(PROPERTY_FONT);
*pStringIter++ = PROPERTY_TEXTEMPHASIS;
*pValueIter++ = pData->xObjectProperties->getPropertyValue(PROPERTY_TEXTEMPHASIS);
*pStringIter++ = PROPERTY_TEXTRELIEF;
*pValueIter++ = pData->xObjectProperties->getPropertyValue(PROPERTY_TEXTRELIEF);
if ( m_bPreview )
{
*pStringIter++ = "HasNavigationBar";
*pValueIter++ <<= false;
*pStringIter++ = "HasRecordMarker";
*pValueIter++ <<= false;
}
*pStringIter++ = PROPERTY_ROW_HEIGHT;
*pValueIter++ = pData->xObjectProperties->getPropertyValue(PROPERTY_ROW_HEIGHT);
if ( m_bPreview )
{
*pStringIter++ = "Tabstop";
*pValueIter++ <<= false;
}
*pStringIter++ = PROPERTY_TEXTCOLOR;
*pValueIter++ = pData->xObjectProperties->getPropertyValue(PROPERTY_TEXTCOLOR);
*pStringIter++ = PROPERTY_TEXTLINECOLOR;
*pValueIter++ = pData->xObjectProperties->getPropertyValue(PROPERTY_TEXTLINECOLOR);
Reference< XMultiPropertySet > xFormMultiSet(xGrid, UNO_QUERY);
xFormMultiSet->setPropertyValues(aProperties, aValues);
}
// get the formats supplier of the database we're working with
Reference< css::util::XNumberFormatsSupplier > xSupplier = getNumberFormatter()->getNumberFormatsSupplier();
Reference<XConnection> xConnection;
Reference<XPropertySet> xRowSetProps(getRowSet(),UNO_QUERY);
xRowSetProps->getPropertyValue( PROPERTY_ACTIVE_CONNECTION ) >>= xConnection;
OSL_ENSURE(xConnection.is(),"A ActiveConnection should normally exists!");
Reference<XChild> xChild(xConnection,UNO_QUERY);
Reference<XPropertySet> xDataSourceProp(xChild->getParent(),UNO_QUERY);
bool bSuppressVersionCol = false;
OSL_VERIFY( xDataSourceProp->getPropertyValue( PROPERTY_SUPPRESSVERSIONCL ) >>= bSuppressVersionCol );
// insert the column into the gridcontrol so that we see something :-)
OUString aCurrentModelType;
Reference<XColumnsSupplier> xSupCols(getRowSet(),UNO_QUERY);
Reference<XNameAccess> xColumns = xSupCols->getColumns();
OUString sDefaultProperty;
Reference< XPropertySet > xColumn;
Reference< XPropertySetInfo > xColPSI;
const Sequence<OUString> aColNames = xColumns->getElementNames();
for (const OUString& rName : aColNames)
{
xColumn.set( xColumns->getByName( rName ), UNO_QUERY_THROW );
xColPSI.set( xColumn->getPropertySetInfo(), UNO_SET_THROW );
// ignore the column when it is a rowversion one
if ( bSuppressVersionCol
&& xColPSI->hasPropertyByName( PROPERTY_ISROWVERSION )
&& ::cppu::any2bool( xColumn->getPropertyValue( PROPERTY_ISROWVERSION ) )
)
continue;
// use the result set column's type to determine the type of grid column to create
bool bFormattedIsNumeric = true;
sal_Int32 nType = ::comphelper::getINT32( xColumn->getPropertyValue( PROPERTY_TYPE ) );
std::vector< NamedValue > aInitialValues;
std::vector< OUString > aCopyProperties;
Any aDefault;
switch(nType)
{
case DataType::BIT:
case DataType::BOOLEAN:
{
aCurrentModelType = "CheckBox";
aInitialValues.emplace_back( "VisualEffect", Any( VisualEffect::FLAT ) );
sDefaultProperty = PROPERTY_DEFAULTSTATE;
sal_Int32 nNullable = ColumnValue::NULLABLE_UNKNOWN;
OSL_VERIFY( xColumn->getPropertyValue( PROPERTY_ISNULLABLE ) >>= nNullable );
aInitialValues.emplace_back(
"TriState",
Any( ColumnValue::NO_NULLS != nNullable )
);
if ( ColumnValue::NO_NULLS == nNullable )
aDefault <<= sal_Int16(TRISTATE_FALSE);
}
break;
case DataType::LONGVARCHAR:
case DataType::CLOB:
aInitialValues.emplace_back( "MultiLine", Any( true ) );
[[fallthrough]];
case DataType::BINARY:
case DataType::VARBINARY:
case DataType::LONGVARBINARY:
aCurrentModelType = "TextField";
sDefaultProperty = PROPERTY_DEFAULTTEXT;
break;
case DataType::VARCHAR:
case DataType::CHAR:
bFormattedIsNumeric = false;
[[fallthrough]];
default:
aCurrentModelType = "FormattedField";
sDefaultProperty = PROPERTY_EFFECTIVEDEFAULT;
if ( xSupplier.is() )
aInitialValues.emplace_back( "FormatsSupplier", Any( xSupplier ) );
aInitialValues.emplace_back( "TreatAsNumber", Any( bFormattedIsNumeric ) );
aCopyProperties.emplace_back(PROPERTY_FORMATKEY );
break;
}
aInitialValues.emplace_back( PROPERTY_CONTROLSOURCE, Any( rName ) );
OUString sLabel;
xColumn->getPropertyValue(PROPERTY_LABEL) >>= sLabel;
if ( !sLabel.isEmpty() )
aInitialValues.emplace_back( PROPERTY_LABEL, Any( sLabel ) );
else
aInitialValues.emplace_back( PROPERTY_LABEL, Any( rName ) );
Reference< XPropertySet > xGridCol( xColFactory->createColumn( aCurrentModelType ), UNO_SET_THROW );
Reference< XPropertySetInfo > xGridColPSI( xGridCol->getPropertySetInfo(), UNO_SET_THROW );
// calculate the default
if ( xGridColPSI->hasPropertyByName( PROPERTY_CONTROLDEFAULT ) )
{
aDefault = xColumn->getPropertyValue( PROPERTY_CONTROLDEFAULT );
// default value
if ( nType == DataType::BIT || nType == DataType::BOOLEAN )
{
if ( aDefault.hasValue() )
aDefault <<= (comphelper::getString(aDefault).toInt32() == 0) ? sal_Int16(TRISTATE_FALSE) : sal_Int16(TRISTATE_TRUE);
else
aDefault <<= sal_Int16(TRISTATE_INDET);
}
}
if ( aDefault.hasValue() )
aInitialValues.emplace_back( sDefaultProperty, aDefault );
// transfer properties from the definition to the UNO-model :
aCopyProperties.emplace_back(PROPERTY_HIDDEN );
aCopyProperties.emplace_back(PROPERTY_WIDTH );
// help text to display for the column
Any aDescription;
if ( xColPSI->hasPropertyByName( PROPERTY_HELPTEXT ) )
aDescription = xColumn->getPropertyValue( PROPERTY_HELPTEXT );
OUString sTemp;
aDescription >>= sTemp;
if ( sTemp.isEmpty() )
xColumn->getPropertyValue( PROPERTY_DESCRIPTION ) >>= sTemp;
aDescription <<= sTemp;
aInitialValues.emplace_back( PROPERTY_HELPTEXT, aDescription );
// ... horizontal justify
Any aAlign; aAlign <<= sal_Int16( 0 );
Any aColAlign( xColumn->getPropertyValue( PROPERTY_ALIGN ) );
if ( aColAlign.hasValue() )
aAlign <<= sal_Int16( ::comphelper::getINT32( aColAlign ) );
aInitialValues.emplace_back( PROPERTY_ALIGN, aAlign );
// don't allow the mouse to scroll in the cells
if ( xGridColPSI->hasPropertyByName( PROPERTY_MOUSE_WHEEL_BEHAVIOR ) )
aInitialValues.emplace_back( PROPERTY_MOUSE_WHEEL_BEHAVIOR, Any( MouseWheelBehavior::SCROLL_DISABLED ) );
// now set all those values
for (auto const& property : aInitialValues)
{
xGridCol->setPropertyValue( property.Name, property.Value );
}
for (auto const& copyPropertyName : aCopyProperties)
xGridCol->setPropertyValue( copyPropertyName, xColumn->getPropertyValue(copyPropertyName) );
xColContainer->insertByName(rName, Any(xGridCol));
}
}
}
catch(const Exception&)
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
static Reference<XPropertySet> getColumnHelper(const weld::TreeView& rTreeView,
const weld::TreeIter* pCurrentlyDisplayed,
const Reference<XPropertySet>& rxSource)
{
Reference<XPropertySet> xRet;
if (pCurrentlyDisplayed)
{
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*pCurrentlyDisplayed));
Reference<XColumnsSupplier> xColumnsSup(pData->xObjectProperties,UNO_QUERY);
Reference<XNameAccess> xNames = xColumnsSup->getColumns();
OUString aName;
rxSource->getPropertyValue(PROPERTY_NAME) >>= aName;
if(xNames.is() && xNames->hasByName(aName))
xRet.set(xNames->getByName(aName),UNO_QUERY);
}
return xRet;
}
void SbaTableQueryBrowser::transferChangedControlProperty(const OUString& _rProperty, const Any& _rNewValue)
{
if (m_xCurrentlyDisplayed)
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*m_xCurrentlyDisplayed));
Reference< XPropertySet > xObjectProps = pData->xObjectProperties;
OSL_ENSURE(xObjectProps.is(),"SbaTableQueryBrowser::transferChangedControlProperty: no table/query object!");
if (xObjectProps.is())
xObjectProps->setPropertyValue(_rProperty, _rNewValue);
}
}
void SbaTableQueryBrowser::propertyChange(const PropertyChangeEvent& evt)
{
SbaXDataBrowserController::propertyChange(evt);
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
try
{
Reference< XPropertySet > xSource(evt.Source, UNO_QUERY);
if (!xSource.is())
return;
// one of the many properties which require us to update the definition ?
// a column's width ?
else if (evt.PropertyName == PROPERTY_WIDTH)
{ // a column width has changed -> update the model
// (the update of the view is done elsewhere)
Reference<XPropertySet> xProp = getColumnHelper(rTreeView, m_xCurrentlyDisplayed.get(), xSource);
if(xProp.is())
{
if(!evt.NewValue.hasValue())
xProp->setPropertyValue(PROPERTY_WIDTH,Any(sal_Int32(227)));
else
xProp->setPropertyValue(PROPERTY_WIDTH,evt.NewValue);
}
}
// a column's 'visible' state ?
else if (evt.PropertyName == PROPERTY_HIDDEN)
{
Reference<XPropertySet> xProp = getColumnHelper(rTreeView, m_xCurrentlyDisplayed.get(), xSource);
if(xProp.is())
xProp->setPropertyValue(PROPERTY_HIDDEN,evt.NewValue);
}
// a columns alignment ?
else if (evt.PropertyName == PROPERTY_ALIGN)
{
Reference<XPropertySet> xProp = getColumnHelper(rTreeView, m_xCurrentlyDisplayed.get(), xSource);
try
{
if(xProp.is())
{
if(evt.NewValue.hasValue())
{
sal_Int16 nAlign = 0;
if(evt.NewValue >>= nAlign)
xProp->setPropertyValue(PROPERTY_ALIGN,Any(sal_Int32(nAlign)));
else
xProp->setPropertyValue(PROPERTY_ALIGN,evt.NewValue);
}
else
xProp->setPropertyValue(PROPERTY_ALIGN,Any(css::awt::TextAlign::LEFT));
}
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
// a column's format ?
else if ( evt.PropertyName == PROPERTY_FORMATKEY
&& (TypeClass_LONG == evt.NewValue.getValueTypeClass())
)
{
// update the model (means the definition object)
Reference<XPropertySet> xProp = getColumnHelper(rTreeView, m_xCurrentlyDisplayed.get(), xSource);
if(xProp.is())
xProp->setPropertyValue(PROPERTY_FORMATKEY,evt.NewValue);
}
// some table definition properties ?
// the height of the rows in the grid ?
else if (evt.PropertyName == PROPERTY_ROW_HEIGHT)
{
if (m_xCurrentlyDisplayed)
{
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*m_xCurrentlyDisplayed));
OSL_ENSURE( pData->xObjectProperties.is(), "No table available!" );
bool bDefault = !evt.NewValue.hasValue();
if (bDefault)
pData->xObjectProperties->setPropertyValue(PROPERTY_ROW_HEIGHT,Any(sal_Int32(45)));
else
pData->xObjectProperties->setPropertyValue(PROPERTY_ROW_HEIGHT,evt.NewValue);
}
}
else if ( evt.PropertyName == PROPERTY_FONT // the font ?
|| evt.PropertyName == PROPERTY_TEXTCOLOR // the text color ?
|| evt.PropertyName == PROPERTY_FILTER // the filter ?
|| evt.PropertyName == PROPERTY_HAVING_CLAUSE // the having clause ?
|| evt.PropertyName == PROPERTY_ORDER // the sort ?
|| evt.PropertyName == PROPERTY_APPLYFILTER // the appliance of the filter ?
|| evt.PropertyName == PROPERTY_TEXTLINECOLOR // the text line color ?
|| evt.PropertyName == PROPERTY_TEXTEMPHASIS // the text emphasis ?
|| evt.PropertyName == PROPERTY_TEXTRELIEF // the text relief ?
)
{
transferChangedControlProperty(evt.PropertyName, evt.NewValue);
}
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
sal_Bool SbaTableQueryBrowser::suspend(sal_Bool bSuspend)
{
SolarMutexGuard aSolarGuard;
::osl::MutexGuard aGuard( getMutex() );
if ( getView() && getView()->IsInModalMode() )
return false;
bool bRet = false;
if ( !m_bInSuspend )
{
m_bInSuspend = true;
if ( rBHelper.bDisposed )
throw DisposedException( OUString(), *this );
bRet = SbaXDataBrowserController::suspend(bSuspend);
if ( bRet && getView() )
getView()->Hide();
m_bInSuspend = false;
}
return bRet;
}
void SAL_CALL SbaTableQueryBrowser::statusChanged( const FeatureStateEvent& _rEvent )
{
// search the external dispatcher causing this call
Reference< XDispatch > xSource(_rEvent.Source, UNO_QUERY);
bool bFound = false;
for (auto & externalFeature : m_aExternalFeatures)
{
if ( _rEvent.FeatureURL.Complete == externalFeature.second.aURL.Complete)
{
bFound = true;
OSL_ENSURE( xSource.get() == externalFeature.second.xDispatcher.get(), "SbaTableQueryBrowser::statusChanged: inconsistent!" );
// update the enabled state
externalFeature.second.bEnabled = _rEvent.IsEnabled;
switch ( externalFeature.first )
{
case ID_BROWSER_DOCUMENT_DATASOURCE:
{
// if it's the slot for the document data source, remember the state
Sequence< PropertyValue > aDescriptor;
bool bProperFormat = _rEvent.State >>= aDescriptor;
OSL_ENSURE(bProperFormat, "SbaTableQueryBrowser::statusChanged: need a data access descriptor here!");
m_aDocumentDataSource.initializeFrom(aDescriptor);
OSL_ENSURE( ( m_aDocumentDataSource.has(DataAccessDescriptorProperty::DataSource)
|| m_aDocumentDataSource.has(DataAccessDescriptorProperty::DatabaseLocation)
)
&& m_aDocumentDataSource.has(DataAccessDescriptorProperty::Command)
&& m_aDocumentDataSource.has(DataAccessDescriptorProperty::CommandType),
"SbaTableQueryBrowser::statusChanged: incomplete descriptor!");
// check if we know the object which is set as document data source
checkDocumentDataSource();
}
break;
default:
// update the toolbox
implCheckExternalSlot( externalFeature.first );
break;
}
break;
}
}
OSL_ENSURE(bFound, "SbaTableQueryBrowser::statusChanged: don't know who sent this!");
}
void SbaTableQueryBrowser::checkDocumentDataSource()
{
std::unique_ptr<weld::TreeIter> xDataSourceEntry;
std::unique_ptr<weld::TreeIter> xContainerEntry;
std::unique_ptr<weld::TreeIter> xObjectEntry = getObjectEntry(m_aDocumentDataSource, &xDataSourceEntry, &xContainerEntry);
bool bKnownDocDataSource = static_cast<bool>(xObjectEntry);
if (!bKnownDocDataSource)
{
if (xDataSourceEntry)
{
// at least the data source is known
if (xContainerEntry)
{
bKnownDocDataSource = true; // assume we know it.
// TODO: should we expand the object container? This may be too expensive just for checking...
}
else
{
if (m_aDocumentDataSource.has(DataAccessDescriptorProperty::CommandType)
&& m_aDocumentDataSource.has(DataAccessDescriptorProperty::Command))
{ // maybe we have a command to be displayed ?
sal_Int32 nCommandType = CommandType::TABLE;
m_aDocumentDataSource[DataAccessDescriptorProperty::CommandType] >>= nCommandType;
OUString sCommand;
m_aDocumentDataSource[DataAccessDescriptorProperty::Command] >>= sCommand;
bKnownDocDataSource = (CommandType::COMMAND == nCommandType) && (!sCommand.isEmpty());
}
}
}
}
if ( !bKnownDocDataSource )
m_aExternalFeatures[ ID_BROWSER_DOCUMENT_DATASOURCE ].bEnabled = false;
// update the toolbox
implCheckExternalSlot(ID_BROWSER_DOCUMENT_DATASOURCE);
}
void SbaTableQueryBrowser::extractDescriptorProps(const svx::ODataAccessDescriptor& _rDescriptor, OUString& _rDataSource, OUString& _rCommand, sal_Int32& _rCommandType, bool& _rEscapeProcessing)
{
_rDataSource = _rDescriptor.getDataSource();
if ( _rDescriptor.has(DataAccessDescriptorProperty::Command) )
_rDescriptor[DataAccessDescriptorProperty::Command] >>= _rCommand;
if ( _rDescriptor.has(DataAccessDescriptorProperty::CommandType) )
_rDescriptor[DataAccessDescriptorProperty::CommandType] >>= _rCommandType;
// escape processing is the only one allowed not to be present
_rEscapeProcessing = true;
if (_rDescriptor.has(DataAccessDescriptorProperty::EscapeProcessing))
_rEscapeProcessing = ::cppu::any2bool(_rDescriptor[DataAccessDescriptorProperty::EscapeProcessing]);
}
namespace
{
bool getDataSourceDisplayName_isURL( const OUString& _rDS, OUString& _rDisplayName, OUString& _rUniqueId )
{
INetURLObject aURL( _rDS );
if ( aURL.GetProtocol() != INetProtocol::NotValid )
{
_rDisplayName = aURL.getBase(INetURLObject::LAST_SEGMENT,true,INetURLObject::DecodeMechanism::WithCharset);
_rUniqueId = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
return true;
}
_rDisplayName = _rDS;
_rUniqueId.clear();
return false;
}
struct FilterByEntryDataId : public IEntryFilter
{
OUString sId;
explicit FilterByEntryDataId( OUString _aId ) : sId(std::move( _aId )) { }
virtual ~FilterByEntryDataId() {}
virtual bool includeEntry(const void* pEntry) const override;
};
bool FilterByEntryDataId::includeEntry(const void* pUserData) const
{
const DBTreeListUserData* pData = static_cast<const DBTreeListUserData*>(pUserData);
return ( !pData || ( pData->sAccessor == sId ) );
}
}
OUString SbaTableQueryBrowser::getDataSourceAccessor(const weld::TreeIter& rDataSourceEntry) const
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(rDataSourceEntry));
assert(pData && "SbaTableQueryBrowser::getDataSourceAccessor: invalid entry data!");
OSL_ENSURE( pData->eType == etDatasource, "SbaTableQueryBrowser::getDataSourceAccessor: entry does not denote a data source!" );
return !pData->sAccessor.isEmpty() ? pData->sAccessor : GetEntryText(rDataSourceEntry);
}
std::unique_ptr<weld::TreeIter> SbaTableQueryBrowser::getObjectEntry(const OUString& _rDataSource, const OUString& _rCommand, sal_Int32 nCommandType,
std::unique_ptr<weld::TreeIter>* ppDataSourceEntry, std::unique_ptr<weld::TreeIter>* ppContainerEntry, bool bExpandAncestors,
const SharedConnection& _rxConnection )
{
if (ppDataSourceEntry)
ppDataSourceEntry->reset();
if (ppContainerEntry)
ppContainerEntry->reset();
std::unique_ptr<weld::TreeIter> xObject;
if ( m_pTreeView )
{
// look for the data source entry
OUString sDisplayName, sDataSourceId;
bool bIsDataSourceURL = getDataSourceDisplayName_isURL( _rDataSource, sDisplayName, sDataSourceId );
// the display name may differ from the URL for readability reasons
// #i33699#
FilterByEntryDataId aFilter( sDataSourceId );
std::unique_ptr<weld::TreeIter> xDataSource = m_pTreeView->GetEntryPosByName( sDisplayName, nullptr, &aFilter );
if (!xDataSource) // check if the data source name is a file location
{
if ( bIsDataSourceURL )
{
// special case, the data source is a URL
// add new entries to the list box model
implAddDatasource( _rDataSource, _rxConnection );
xDataSource = m_pTreeView->GetEntryPosByName( sDisplayName, nullptr, &aFilter );
OSL_ENSURE( xDataSource, "SbaTableQueryBrowser::getObjectEntry: hmm - did not find it again!" );
}
}
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
if (xDataSource)
{
if (ppDataSourceEntry)
{
// (caller wants to have it...)
*ppDataSourceEntry = rTreeView.make_iterator(xDataSource.get());
}
// expand if required so
if (bExpandAncestors)
rTreeView.expand_row(*xDataSource);
// look for the object container
std::unique_ptr<weld::TreeIter> xCommandType;
if (nCommandType == CommandType::QUERY || nCommandType == CommandType::TABLE)
{
xCommandType = rTreeView.make_iterator(xDataSource.get());
if (!rTreeView.iter_children(*xCommandType))
xCommandType.reset();
else
{
// 1st child is queries, so we're already done if looking for CommandType::QUERY
// 2nd child is tables
if (nCommandType == CommandType::TABLE && !rTreeView.iter_next_sibling(*xCommandType))
xCommandType.reset();
}
}
if (xCommandType)
{
if (ppContainerEntry)
{
// (caller wants to have it...)
*ppContainerEntry = rTreeView.make_iterator(xCommandType.get());
}
rTreeView.make_unsorted();
// expand if required so
if (bExpandAncestors)
{
rTreeView.expand_row(*xCommandType);
}
// look for the object
sal_Int32 nIndex = 0;
do
{
OUString sPath;
switch (nCommandType)
{
case CommandType::TABLE:
sPath = _rCommand;
nIndex = -1;
break;
case CommandType::QUERY:
sPath = _rCommand.getToken( 0, '/', nIndex );
break;
default:
assert(false);
}
xObject = m_pTreeView->GetEntryPosByName(sPath, xCommandType.get());
if (xObject)
rTreeView.copy_iterator(*xObject, *xCommandType);
else
xCommandType.reset();
if ( nIndex >= 0 )
{
if (ensureEntryObject(*xObject))
{
DBTreeListUserData* pParentData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xObject));
Reference< XNameAccess > xCollection( pParentData->xContainer, UNO_QUERY );
sal_Int32 nIndex2 = nIndex;
sPath = _rCommand.getToken( 0, '/', nIndex2 );
try
{
if ( xCollection->hasByName(sPath) )
{
if(!m_pTreeView->GetEntryPosByName(sPath, xObject.get()))
{
Reference<XNameAccess> xChild(xCollection->getByName(sPath),UNO_QUERY);
DBTreeListUserData* pEntryData = new DBTreeListUserData;
pEntryData->eType = etQuery;
if ( xChild.is() )
{
pEntryData->eType = etQueryContainer;
}
implAppendEntry(xObject.get(), sPath, pEntryData);
}
}
}
catch(const Exception&)
{
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::populateTree: could not fill the tree");
}
}
}
}
while ( nIndex >= 0 );
rTreeView.make_sorted();
}
}
}
return xObject;
}
std::unique_ptr<weld::TreeIter> SbaTableQueryBrowser::getObjectEntry(const svx::ODataAccessDescriptor& rDescriptor,
std::unique_ptr<weld::TreeIter>* ppDataSourceEntry, std::unique_ptr<weld::TreeIter>* ppContainerEntry)
{
// extract the props from the descriptor
OUString sDataSource;
OUString sCommand;
sal_Int32 nCommandType = CommandType::COMMAND;
bool bEscapeProcessing = true;
extractDescriptorProps(rDescriptor, sDataSource, sCommand, nCommandType, bEscapeProcessing);
return getObjectEntry(sDataSource, sCommand, nCommandType, ppDataSourceEntry, ppContainerEntry, false /*bExpandAncestors*/);
}
void SbaTableQueryBrowser::connectExternalDispatches()
{
Reference< XDispatchProvider > xProvider( getFrame(), UNO_QUERY );
OSL_ENSURE(xProvider.is(), "SbaTableQueryBrowser::connectExternalDispatches: no DispatchProvider !");
if (!xProvider.is())
return;
if ( m_aExternalFeatures.empty() )
{
constexpr OUString aURLs[] {
u".uno:DataSourceBrowser/DocumentDataSource"_ustr,
u".uno:DataSourceBrowser/FormLetter"_ustr,
u".uno:DataSourceBrowser/InsertColumns"_ustr,
u".uno:DataSourceBrowser/InsertContent"_ustr,
};
constexpr sal_uInt16 nIds[] = {
ID_BROWSER_DOCUMENT_DATASOURCE,
ID_BROWSER_FORMLETTER,
ID_BROWSER_INSERTCOLUMNS,
ID_BROWSER_INSERTCONTENT
};
for ( size_t i=0; i < std::size( aURLs ); ++i )
{
URL aURL;
aURL.Complete = aURLs[i];
if ( m_xUrlTransformer.is() )
m_xUrlTransformer->parseStrict( aURL );
m_aExternalFeatures[ nIds[ i ] ] = ExternalFeature( std::move(aURL) );
}
}
for (auto & externalFeature : m_aExternalFeatures)
{
externalFeature.second.xDispatcher = xProvider->queryDispatch(
externalFeature.second.aURL, u"_parent"_ustr, FrameSearchFlag::PARENT
);
if ( externalFeature.second.xDispatcher.get() == static_cast< XDispatch* >( this ) )
{
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::connectExternalDispatches: this should not happen anymore!" );
// (nowadays, the URLs aren't in our SupportedFeatures list anymore, so we should
// not supply a dispatcher for this)
externalFeature.second.xDispatcher.clear();
}
if ( externalFeature.second.xDispatcher.is() )
{
try
{
externalFeature.second.xDispatcher->addStatusListener( this, externalFeature.second.aURL );
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
implCheckExternalSlot( externalFeature.first );
}
}
void SbaTableQueryBrowser::implCheckExternalSlot( sal_uInt16 _nId )
{
if ( !m_xMainToolbar.is() )
return;
VclPtr<vcl::Window> pToolboxWindow = VCLUnoHelper::GetWindow( m_xMainToolbar );
ToolBox* pToolbox = dynamic_cast< ToolBox* >( pToolboxWindow.get() );
OSL_ENSURE( pToolbox, "SbaTableQueryBrowser::implCheckExternalSlot: cannot obtain the toolbox window!" );
// check if we have to hide this item from the toolbox
if ( pToolbox )
{
bool bHaveDispatcher = m_aExternalFeatures[ _nId ].xDispatcher.is();
if ( bHaveDispatcher != pToolbox->IsItemVisible( ToolBoxItemId(_nId) ) )
bHaveDispatcher ? pToolbox->ShowItem( ToolBoxItemId(_nId) ) : pToolbox->HideItem( ToolBoxItemId(_nId) );
}
// and invalidate this feature in general
InvalidateFeature( _nId );
}
void SAL_CALL SbaTableQueryBrowser::disposing( const css::lang::EventObject& _rSource )
{
// our frame ?
Reference< css::frame::XFrame > xSourceFrame(_rSource.Source, UNO_QUERY);
if (m_xCurrentFrameParent.is() && (xSourceFrame == m_xCurrentFrameParent))
m_xCurrentFrameParent->removeFrameActionListener(static_cast<css::frame::XFrameActionListener*>(this));
else
{
// search the external dispatcher causing this call in our map
Reference< XDispatch > xSource(_rSource.Source, UNO_QUERY);
if(xSource.is())
{
ExternalFeaturesMap::const_iterator aLoop = m_aExternalFeatures.begin();
ExternalFeaturesMap::const_iterator aEnd = m_aExternalFeatures.end();
while (aLoop != aEnd)
{
if ( aLoop->second.xDispatcher.get() == xSource.get() )
{
sal_uInt16 nSlot = aLoop->first;
// remove it
aLoop = m_aExternalFeatures.erase(aLoop);
// maybe update the UI
implCheckExternalSlot(nSlot);
// continue, the same XDispatch may be responsible for more than one URL
}
++aLoop;
}
}
else
{
Reference<XConnection> xCon(_rSource.Source, UNO_QUERY);
if ( xCon.is() && m_pTreeView )
{
// our connection is in dispose so we have to find the entry equal with this connection
// and close it what means to collapse the entry
// get the top-level representing the removed data source
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xDSLoop(rTreeView.make_iterator());
if (rTreeView.get_iter_first(*xDSLoop))
{
do
{
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xDSLoop));
if ( pData && pData->xConnection == xCon )
{
// we set the connection to null to avoid a second disposing of the connection
pData->xConnection.clear();
closeConnection(*xDSLoop, false);
break;
}
}
while (rTreeView.iter_next_sibling(*xDSLoop));
}
}
else
SbaXDataBrowserController::disposing(_rSource);
}
}
}
void SbaTableQueryBrowser::implRemoveStatusListeners()
{
// clear all old dispatches
for (auto const& externalFeature : m_aExternalFeatures)
{
if ( externalFeature.second.xDispatcher.is() )
{
try
{
externalFeature.second.xDispatcher->removeStatusListener( this, externalFeature.second.aURL );
}
catch (Exception&)
{
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::implRemoveStatusListeners: could not remove a status listener!");
}
}
}
m_aExternalFeatures.clear();
}
sal_Bool SAL_CALL SbaTableQueryBrowser::select( const Any& _rSelection )
{
SolarMutexGuard aGuard;
// doin' a lot of VCL stuff here -> lock the SolarMutex
Sequence< PropertyValue > aDescriptorSequence;
if (!(_rSelection >>= aDescriptorSequence))
throw IllegalArgumentException(OUString(), *this, 1);
// TODO: error message
ODataAccessDescriptor aDescriptor;
try
{
aDescriptor = ODataAccessDescriptor(aDescriptorSequence);
}
catch(const Exception&)
{
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::select: could not extract the descriptor!");
}
// check the presence of the props we need
if ( !(aDescriptor.has(DataAccessDescriptorProperty::DataSource) || aDescriptor.has(DataAccessDescriptorProperty::DatabaseLocation)) || !aDescriptor.has(DataAccessDescriptorProperty::Command) || !aDescriptor.has(DataAccessDescriptorProperty::CommandType))
throw IllegalArgumentException(OUString(), *this, 1);
// TODO: error message
return implSelect(aDescriptor,true);
}
Any SAL_CALL SbaTableQueryBrowser::getSelection( )
{
Any aReturn;
try
{
Reference< XLoadable > xLoadable(getRowSet(), UNO_QUERY);
if (xLoadable.is() && xLoadable->isLoaded())
{
Reference< XPropertySet > aFormProps(getRowSet(), UNO_QUERY);
ODataAccessDescriptor aDescriptor(aFormProps);
// remove properties which are not part of our "selection"
aDescriptor.erase(DataAccessDescriptorProperty::Connection);
aDescriptor.erase(DataAccessDescriptorProperty::Cursor);
aReturn <<= aDescriptor.createPropertyValueSequence();
}
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
return aReturn;
}
void SAL_CALL SbaTableQueryBrowser::addSelectionChangeListener( const Reference< XSelectionChangeListener >& _rxListener )
{
m_aSelectionListeners.addInterface(_rxListener);
}
void SAL_CALL SbaTableQueryBrowser::removeSelectionChangeListener( const Reference< XSelectionChangeListener >& _rxListener )
{
m_aSelectionListeners.removeInterface(_rxListener);
}
void SbaTableQueryBrowser::attachFrame(const Reference< css::frame::XFrame > & _xFrame)
{
implRemoveStatusListeners();
if (m_xCurrentFrameParent.is())
m_xCurrentFrameParent->removeFrameActionListener(static_cast<css::frame::XFrameActionListener*>(this));
SbaXDataBrowserController::attachFrame(_xFrame);
Reference< XFrame > xCurrentFrame( getFrame() );
if ( xCurrentFrame.is() )
{
m_xCurrentFrameParent = xCurrentFrame->findFrame(u"_parent"_ustr,FrameSearchFlag::PARENT);
if ( m_xCurrentFrameParent.is() )
m_xCurrentFrameParent->addFrameActionListener(static_cast<css::frame::XFrameActionListener*>(this));
// obtain our toolbox
try
{
Reference< XPropertySet > xFrameProps( m_aCurrentFrame.getFrame(), UNO_QUERY_THROW );
Reference< XLayoutManager > xLayouter(
xFrameProps->getPropertyValue(u"LayoutManager"_ustr),
UNO_QUERY );
if ( xLayouter.is() )
{
Reference< XUIElement > xUI(
xLayouter->getElement( u"private:resource/toolbar/toolbar"_ustr ),
UNO_SET_THROW );
m_xMainToolbar.set(xUI->getRealInterface(), css::uno::UNO_QUERY);
OSL_ENSURE( m_xMainToolbar.is(), "SbaTableQueryBrowser::attachFrame: where's my toolbox?" );
}
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
// get the dispatchers for the external slots
connectExternalDispatches();
}
void SbaTableQueryBrowser::addModelListeners(const Reference< css::awt::XControlModel > & _xGridControlModel)
{
SbaXDataBrowserController::addModelListeners(_xGridControlModel);
Reference< XPropertySet > xSourceSet(_xGridControlModel, UNO_QUERY);
if (xSourceSet.is())
{
xSourceSet->addPropertyChangeListener(PROPERTY_ROW_HEIGHT, static_cast<XPropertyChangeListener*>(this));
xSourceSet->addPropertyChangeListener(PROPERTY_FONT, static_cast<XPropertyChangeListener*>(this));
xSourceSet->addPropertyChangeListener(PROPERTY_TEXTCOLOR, static_cast<XPropertyChangeListener*>(this));
xSourceSet->addPropertyChangeListener(PROPERTY_TEXTLINECOLOR, static_cast<XPropertyChangeListener*>(this));
xSourceSet->addPropertyChangeListener(PROPERTY_TEXTEMPHASIS, static_cast<XPropertyChangeListener*>(this));
xSourceSet->addPropertyChangeListener(PROPERTY_TEXTRELIEF, static_cast<XPropertyChangeListener*>(this));
}
}
void SbaTableQueryBrowser::removeModelListeners(const Reference< css::awt::XControlModel > & _xGridControlModel)
{
SbaXDataBrowserController::removeModelListeners(_xGridControlModel);
Reference< XPropertySet > xSourceSet(_xGridControlModel, UNO_QUERY);
if (xSourceSet.is())
{
xSourceSet->removePropertyChangeListener(PROPERTY_ROW_HEIGHT, static_cast<XPropertyChangeListener*>(this));
xSourceSet->removePropertyChangeListener(PROPERTY_FONT, static_cast<XPropertyChangeListener*>(this));
xSourceSet->removePropertyChangeListener(PROPERTY_TEXTCOLOR, static_cast<XPropertyChangeListener*>(this));
xSourceSet->removePropertyChangeListener(PROPERTY_TEXTLINECOLOR, static_cast<XPropertyChangeListener*>(this));
xSourceSet->removePropertyChangeListener(PROPERTY_TEXTEMPHASIS, static_cast<XPropertyChangeListener*>(this));
xSourceSet->removePropertyChangeListener(PROPERTY_TEXTRELIEF, static_cast<XPropertyChangeListener*>(this));
}
}
void SbaTableQueryBrowser::RowChanged()
{
if(getBrowserView())
{
SbaGridControl* pControl = getBrowserView()->getVclControl();
if (!pControl->IsEditing())
InvalidateFeature(ID_BROWSER_COPY);
}
SbaXDataBrowserController::RowChanged();
}
void SbaTableQueryBrowser::ColumnChanged()
{
if(getBrowserView())
{
SbaGridControl* pControl = getBrowserView()->getVclControl();
if (!pControl->IsEditing())
InvalidateFeature(ID_BROWSER_COPY);
}
SbaXDataBrowserController::ColumnChanged();
}
void SbaTableQueryBrowser::AddColumnListener(const Reference< XPropertySet > & xCol)
{
SbaXDataBrowserController::AddColumnListener(xCol);
SafeAddPropertyListener(xCol, PROPERTY_WIDTH, static_cast<XPropertyChangeListener*>(this));
SafeAddPropertyListener(xCol, PROPERTY_HIDDEN, static_cast<XPropertyChangeListener*>(this));
SafeAddPropertyListener(xCol, PROPERTY_ALIGN, static_cast<XPropertyChangeListener*>(this));
SafeAddPropertyListener(xCol, PROPERTY_FORMATKEY, static_cast<XPropertyChangeListener*>(this));
}
void SbaTableQueryBrowser::RemoveColumnListener(const Reference< XPropertySet > & xCol)
{
SbaXDataBrowserController::RemoveColumnListener(xCol);
SafeRemovePropertyListener(xCol, PROPERTY_WIDTH, static_cast<XPropertyChangeListener*>(this));
SafeRemovePropertyListener(xCol, PROPERTY_HIDDEN, static_cast<XPropertyChangeListener*>(this));
SafeRemovePropertyListener(xCol, PROPERTY_ALIGN, static_cast<XPropertyChangeListener*>(this));
SafeRemovePropertyListener(xCol, PROPERTY_FORMATKEY, static_cast<XPropertyChangeListener*>(this));
}
void SbaTableQueryBrowser::criticalFail()
{
SbaXDataBrowserController::criticalFail();
unloadAndCleanup( false );
}
void SbaTableQueryBrowser::LoadFinished(bool _bWasSynch)
{
SbaXDataBrowserController::LoadFinished(_bWasSynch);
m_sQueryCommand.clear();
m_bQueryEscapeProcessing = false;
if (isValid() && !loadingCancelled())
{
// did we load a query?
bool bTemporary; // needed because we m_bQueryEscapeProcessing is only one bit wide (and we want to pass it by reference)
if ( implGetQuerySignature( m_sQueryCommand, bTemporary ) )
m_bQueryEscapeProcessing = bTemporary;
}
// if the form has been loaded, this means that our "selection" has changed
css::lang::EventObject aEvent( *this );
m_aSelectionListeners.notifyEach( &XSelectionChangeListener::selectionChanged, aEvent );
}
bool SbaTableQueryBrowser::getExternalSlotState( sal_uInt16 _nId ) const
{
bool bEnabled = false;
ExternalFeaturesMap::const_iterator aPos = m_aExternalFeatures.find( _nId );
if ( ( m_aExternalFeatures.end() != aPos ) && aPos->second.xDispatcher.is() )
bEnabled = aPos->second.bEnabled;
return bEnabled;
}
FeatureState SbaTableQueryBrowser::GetState(sal_uInt16 nId) const
{
FeatureState aReturn;
// (disabled automatically)
// no chance without a view
if (!getBrowserView() || !getBrowserView()->getVclControl())
return aReturn;
switch ( nId )
{
case ID_TREE_ADMINISTRATE:
aReturn.bEnabled = true;
return aReturn;
case ID_BROWSER_CLOSE:
// the close button should always be enabled
aReturn.bEnabled = !m_bEnableBrowser;
return aReturn;
// "toggle explorer" is always enabled (if we have an explorer)
case ID_BROWSER_EXPLORER:
aReturn.bEnabled = m_bEnableBrowser;
aReturn.bChecked = haveExplorer();
return aReturn;
case ID_BROWSER_REMOVEFILTER:
return SbaXDataBrowserController::GetState( nId );
case ID_BROWSER_COPY:
if ( !m_pTreeView->HasChildPathFocus() )
// handled below
break;
[[fallthrough]];
case ID_TREE_CLOSE_CONN:
case ID_TREE_EDIT_DATABASE:
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xCurrentEntry(rTreeView.make_iterator());
if (!rTreeView.get_cursor(xCurrentEntry.get()))
return aReturn;
EntryType eType = getEntryType(*xCurrentEntry);
if ( eType == etUnknown )
return aReturn;
std::unique_ptr<weld::TreeIter> xDataSourceEntry = m_pTreeView->GetRootLevelParent(xCurrentEntry.get());
DBTreeListUserData* pDSData
= xDataSourceEntry
? weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xDataSourceEntry))
: nullptr;
if ( nId == ID_TREE_CLOSE_CONN )
{
aReturn.bEnabled = ( pDSData != nullptr ) && pDSData->xConnection.is();
}
else if ( nId == ID_TREE_EDIT_DATABASE )
{
::utl::OConfigurationTreeRoot aConfig( ::utl::OConfigurationTreeRoot::createWithComponentContext( getORB(),
u"/org.openoffice.Office.DataAccess/Policies/Features/Common"_ustr ) );
bool bHaveEditDatabase( true );
OSL_VERIFY( aConfig.getNodeValue( u"EditDatabaseFromDataSourceView"_ustr ) >>= bHaveEditDatabase );
aReturn.bEnabled = getORB().is() && xDataSourceEntry && bHaveEditDatabase;
}
else if ( nId == ID_BROWSER_COPY )
{
aReturn.bEnabled = isEntryCopyAllowed(*xCurrentEntry);
}
return aReturn;
}
}
// all slots not handled above are not available if no form is loaded
if (!isLoaded())
return aReturn;
try
{
bool bHandled = false;
switch (nId)
{
case ID_BROWSER_DOCUMENT_DATASOURCE:
// the slot is enabled if we have an external dispatcher able to handle it,
// and the dispatcher must have enabled the slot in general
aReturn.bEnabled = getExternalSlotState( ID_BROWSER_DOCUMENT_DATASOURCE );
bHandled = true;
break;
case ID_BROWSER_REFRESH:
aReturn.bEnabled = true;
bHandled = true;
break;
}
if (bHandled)
return aReturn;
// no chance without valid models
if (isValid() && !isValidCursor() && nId != ID_BROWSER_CLOSE)
return aReturn;
switch (nId)
{
case ID_BROWSER_INSERTCOLUMNS:
case ID_BROWSER_INSERTCONTENT:
case ID_BROWSER_FORMLETTER:
{
// the slot is enabled if we have an external dispatcher able to handle it,
// and the dispatcher must have enabled the slot in general
aReturn.bEnabled = getExternalSlotState( nId );
// for the Insert* slots, we need at least one selected row
if (ID_BROWSER_FORMLETTER != nId)
aReturn.bEnabled = aReturn.bEnabled && getBrowserView()->getVclControl()->GetSelectRowCount();
// disabled for native queries which are not saved within the database
Reference< XPropertySet > xDataSource(getRowSet(), UNO_QUERY);
try
{
aReturn.bEnabled = aReturn.bEnabled && xDataSource.is();
if (xDataSource.is())
{
sal_Int32 nType = ::comphelper::getINT32(xDataSource->getPropertyValue(PROPERTY_COMMAND_TYPE));
aReturn.bEnabled = aReturn.bEnabled &&
( ::comphelper::getBOOL(xDataSource->getPropertyValue(PROPERTY_ESCAPE_PROCESSING)) ||
(nType == css::sdb::CommandType::QUERY) );
}
}
catch(DisposedException&)
{
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::GetState: object already disposed!");
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
break;
case ID_BROWSER_TITLE:
{
Reference<XPropertySet> xProp(getRowSet(),UNO_QUERY);
sal_Int32 nCommandType = CommandType::TABLE;
xProp->getPropertyValue(PROPERTY_COMMAND_TYPE) >>= nCommandType;
OUString sTitle;
switch (nCommandType)
{
case CommandType::TABLE:
sTitle = DBA_RES(STR_TBL_TITLE); break;
case CommandType::QUERY:
case CommandType::COMMAND:
sTitle = DBA_RES(STR_QRY_TITLE); break;
default:
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::GetState: unknown command type!");
}
OUString aName;
xProp->getPropertyValue(PROPERTY_COMMAND) >>= aName;
OUString sObject(aName);
aReturn.sTitle = sTitle.replaceFirst("#", sObject);
aReturn.bEnabled = true;
}
break;
case ID_BROWSER_TABLEATTR:
case ID_BROWSER_ROWHEIGHT:
case ID_BROWSER_COLATTRSET:
case ID_BROWSER_COLWIDTH:
aReturn.bEnabled = getBrowserView()->getVclControl() && isValid() && isValidCursor();
// aReturn.bEnabled &= getDefinition() && !getDefinition()->GetDatabase()->IsReadOnly();
break;
case ID_BROWSER_COPY:
OSL_ENSURE( !m_pTreeView->HasChildPathFocus(), "SbaTableQueryBrowser::GetState( ID_BROWSER_COPY ): this should have been handled above!" );
if (getBrowserView()->getVclControl() && !getBrowserView()->getVclControl()->IsEditing())
{
SbaGridControl* pControl = getBrowserView()->getVclControl();
if ( pControl->GetSelectRowCount() )
{
aReturn.bEnabled = m_aCurrentFrame.isActive();
break;
}
else
aReturn.bEnabled = pControl->canCopyCellText(pControl->GetCurRow(), pControl->GetCurColumnId());
break;
}
[[fallthrough]];
default:
return SbaXDataBrowserController::GetState(nId);
}
}
catch(const Exception&)
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
return aReturn;
}
void SbaTableQueryBrowser::Execute(sal_uInt16 nId, const Sequence< PropertyValue >& aArgs)
{
switch (nId)
{
default:
SbaXDataBrowserController::Execute(nId,aArgs);
break;
case ID_TREE_EDIT_DATABASE:
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xIter(rTreeView.make_iterator());
if (rTreeView.get_cursor(xIter.get()))
implAdministrate(*xIter);
break;
}
case ID_TREE_CLOSE_CONN:
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xIter(rTreeView.make_iterator());
if (rTreeView.get_cursor(xIter.get()))
{
xIter = m_pTreeView->GetRootLevelParent(xIter.get());
closeConnection(*xIter);
}
break;
}
case ID_TREE_ADMINISTRATE:
svx::administrateDatabaseRegistration( getFrameWeld() );
break;
case ID_BROWSER_REFRESH:
{
if ( !SaveModified( ) )
// nothing to do
break;
bool bFullReinit = false;
// check if the query signature (if the form is based on a query) has changed
if ( !m_sQueryCommand.isEmpty() )
{
OUString sNewQueryCommand;
bool bNewQueryEP;
bool bIsQuery =
implGetQuerySignature( sNewQueryCommand, bNewQueryEP );
OSL_ENSURE( bIsQuery, "SbaTableQueryBrowser::Execute: was a query before, but is not anymore?" );
bFullReinit = ( sNewQueryCommand != m_sQueryCommand ) || ( m_bQueryEscapeProcessing != bNewQueryEP );
}
if ( !bFullReinit )
{
// let the base class do a simple reload
SbaXDataBrowserController::Execute(nId,aArgs);
break;
}
[[fallthrough]];
}
case ID_BROWSER_REFRESH_REBUILD:
{
if ( !SaveModified() )
// nothing to do
break;
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xSelected = m_xCurrentlyDisplayed ?
rTreeView.make_iterator(m_xCurrentlyDisplayed.get()) : nullptr;
// unload
unloadAndCleanup( false );
// reselect the entry
if ( xSelected )
{
implSelect(xSelected.get());
}
else
{
Reference<XPropertySet> xProp(getRowSet(),UNO_QUERY);
implSelect(svx::ODataAccessDescriptor(xProp));
}
}
break;
case ID_BROWSER_EXPLORER:
toggleExplorer();
break;
case ID_BROWSER_DOCUMENT_DATASOURCE:
implSelect(m_aDocumentDataSource);
break;
case ID_BROWSER_INSERTCOLUMNS:
case ID_BROWSER_INSERTCONTENT:
case ID_BROWSER_FORMLETTER:
if (getBrowserView() && isValidCursor())
{
// the URL the slot id is assigned to
OSL_ENSURE( m_aExternalFeatures.find( nId ) != m_aExternalFeatures.end(),
"SbaTableQueryBrowser::Execute( ID_BROWSER_?): how could this ever be enabled?" );
URL aParentUrl = m_aExternalFeatures[ nId ].aURL;
// let the dispatcher execute the slot
Reference< XDispatch > xDispatch( m_aExternalFeatures[ nId ].xDispatcher );
if (xDispatch.is())
{
// set the properties for the dispatch
// first fill the selection
SbaGridControl* pGrid = getBrowserView()->getVclControl();
MultiSelection* pSelection = const_cast<MultiSelection*>(pGrid->GetSelection());
Sequence< Any > aSelection;
if ( !pGrid->IsAllSelected() )
{ // transfer the selected rows only if not all rows are selected
// (all rows means the whole table)
// #i3832#
if (pSelection != nullptr)
{
aSelection.realloc(pSelection->GetSelectCount());
tools::Long nIdx = pSelection->FirstSelected();
Any* pSelectionNos = aSelection.getArray();
while (nIdx != SFX_ENDOFSELECTION)
{
*pSelectionNos++ <<= static_cast<sal_Int32>(nIdx + 1);
nIdx = pSelection->NextSelected();
}
}
}
Reference< XResultSet > xCursorClone;
try
{
Reference< XResultSetAccess > xResultSetAccess(getRowSet(),UNO_QUERY);
if (xResultSetAccess.is())
xCursorClone = xResultSetAccess->createResultSet();
}
catch(DisposedException&)
{
SAL_WARN("dbaccess.ui", "Object already disposed!");
}
catch(const Exception&)
{
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::Execute(ID_BROWSER_?): could not clone the cursor!");
}
Reference<XPropertySet> xProp(getRowSet(),UNO_QUERY);
try
{
ODataAccessDescriptor aDescriptor;
OUString sDataSourceName;
xProp->getPropertyValue(PROPERTY_DATASOURCENAME) >>= sDataSourceName;
aDescriptor.setDataSource(sDataSourceName);
aDescriptor[DataAccessDescriptorProperty::Command] = xProp->getPropertyValue(PROPERTY_COMMAND);
aDescriptor[DataAccessDescriptorProperty::CommandType] = xProp->getPropertyValue(PROPERTY_COMMAND_TYPE);
aDescriptor[DataAccessDescriptorProperty::Connection] = xProp->getPropertyValue(PROPERTY_ACTIVE_CONNECTION);
aDescriptor[DataAccessDescriptorProperty::Cursor] <<= xCursorClone;
if ( aSelection.hasElements() )
{
aDescriptor[DataAccessDescriptorProperty::Selection] <<= aSelection;
aDescriptor[DataAccessDescriptorProperty::BookmarkSelection] <<= false;
// these are selection indices
// before we change this, all clients have to be adjusted
// so that they recognize the new BookmarkSelection property!
}
xDispatch->dispatch(aParentUrl, aDescriptor.createPropertyValueSequence());
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
}
break;
case ID_BROWSER_CLOSE:
closeTask();
// if it's not 0, such an async close is already pending
break;
case ID_BROWSER_COPY:
if(m_pTreeView->HasChildPathFocus())
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xCursor(rTreeView.make_iterator());
if (rTreeView.get_cursor(xCursor.get()))
copyEntry(*xCursor);
}
else if (getBrowserView() && getBrowserView()->getVclControl() && !getBrowserView()->getVclControl()->IsEditing() && getBrowserView()->getVclControl()->GetSelectRowCount() < 1)
{
SbaGridControl* pControl = getBrowserView()->getVclControl();
pControl->copyCellText(pControl->GetCurRow(), pControl->GetCurColumnId());
}
else
SbaXDataBrowserController::Execute(nId,aArgs);
break;
}
}
void SbaTableQueryBrowser::implAddDatasource( const OUString& _rDataSourceName, const SharedConnection& _rxConnection )
{
OUString a, b, c, d, e;
implAddDatasource( _rDataSourceName, a, d, b, e, c, _rxConnection );
}
void SbaTableQueryBrowser::implAddDatasource(const OUString& _rDbName, OUString& _rDbImage,
OUString& _rQueryName, OUString& _rQueryImage, OUString& _rTableName, OUString& _rTableImage,
const SharedConnection& _rxConnection)
{
SolarMutexGuard aGuard;
// initialize the names/images if necessary
if (_rQueryName.isEmpty())
_rQueryName = DBA_RES(RID_STR_QUERIES_CONTAINER);
if (_rTableName.isEmpty())
_rTableName = DBA_RES(RID_STR_TABLES_CONTAINER);
if (_rQueryImage.isEmpty())
_rQueryImage = ImageProvider::getFolderImageId(DatabaseObject::QUERY);
if (_rTableImage.isEmpty())
_rTableImage = ImageProvider::getFolderImageId(DatabaseObject::TABLE);
if (_rDbImage.isEmpty())
_rDbImage = ImageProvider::getDatabaseImage();
// add the entry for the data source
// special handling for data sources denoted by URLs - we do not want to display this ugly URL, do we?
// #i33699#
OUString sDSDisplayName, sDataSourceId;
getDataSourceDisplayName_isURL( _rDbName, sDSDisplayName, sDataSourceId );
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
DBTreeListUserData* pDSData = new DBTreeListUserData;
pDSData->eType = etDatasource;
pDSData->sAccessor = sDataSourceId;
pDSData->xConnection = _rxConnection;
OUString sId(weld::toId(pDSData));
std::unique_ptr<weld::TreeIter> xDatasourceEntry(rTreeView.make_iterator());
rTreeView.insert(nullptr, -1, &sDSDisplayName, &sId, nullptr, nullptr, false, xDatasourceEntry.get());
rTreeView.set_image(*xDatasourceEntry, _rDbImage);
rTreeView.set_text_emphasis(*xDatasourceEntry, false, 0);
// the child for the queries container
{
DBTreeListUserData* pQueriesData = new DBTreeListUserData;
pQueriesData->eType = etQueryContainer;
sId = weld::toId(pQueriesData);
std::unique_ptr<weld::TreeIter> xRet(rTreeView.make_iterator());
rTreeView.insert(xDatasourceEntry.get(), -1, &_rQueryName, &sId,
nullptr, nullptr, true /*ChildrenOnDemand*/, xRet.get());
rTreeView.set_image(*xRet, _rQueryImage);
rTreeView.set_text_emphasis(*xRet, false, 0);
}
// the child for the tables container
{
DBTreeListUserData* pTablesData = new DBTreeListUserData;
pTablesData->eType = etTableContainer;
sId = weld::toId(pTablesData);
std::unique_ptr<weld::TreeIter> xRet(rTreeView.make_iterator());
rTreeView.insert(xDatasourceEntry.get(), -1, &_rTableName, &sId,
nullptr, nullptr, true /*ChildrenOnDemand*/, xRet.get());
rTreeView.set_image(*xRet, _rTableImage);
rTreeView.set_text_emphasis(*xRet, false, 0);
}
}
void SbaTableQueryBrowser::initializeTreeModel()
{
if (m_xDatabaseContext.is())
{
OUString aDBImage, aQueriesImage, aTablesImage;
OUString sQueriesName, sTablesName;
// fill the model with the names of the registered datasources
const Sequence<OUString> aDatasourceNames = m_xDatabaseContext->getElementNames();
for (const OUString& rDatasource : aDatasourceNames)
implAddDatasource( rDatasource, aDBImage, sQueriesName, aQueriesImage, sTablesName, aTablesImage, SharedConnection() );
}
}
void SbaTableQueryBrowser::populateTree(const Reference<XNameAccess>& _xNameAccess,
const weld::TreeIter& rParent,
EntryType eEntryType)
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
rTreeView.make_unsorted();
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(rParent));
if (pData) // don't ask if the nameaccess is already set see OnExpandEntry views and tables
pData->xContainer = _xNameAccess;
try
{
const Sequence<OUString> aNames = _xNameAccess->getElementNames();
for (const OUString& rName : aNames)
{
if( !m_pTreeView->GetEntryPosByName(rName, &rParent))
{
DBTreeListUserData* pEntryData = new DBTreeListUserData;
pEntryData->eType = eEntryType;
if ( eEntryType == etQuery )
{
Reference<XNameAccess> xChild(_xNameAccess->getByName(rName),UNO_QUERY);
if ( xChild.is() )
pEntryData->eType = etQueryContainer;
}
implAppendEntry(&rParent, rName, pEntryData);
}
}
}
catch(const Exception&)
{
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::populateTree: could not fill the tree");
}
rTreeView.make_sorted();
}
std::unique_ptr<weld::TreeIter> SbaTableQueryBrowser::implAppendEntry(const weld::TreeIter* pParent, const OUString& rName, const DBTreeListUserData* pUserData)
{
EntryType eEntryType = pUserData->eType;
std::unique_ptr<ImageProvider> xImageProvider(getImageProviderFor(pParent));
OUString aImage = xImageProvider->getImageId(rName, getDatabaseObjectType(eEntryType));
OUString sId(weld::toId(pUserData));
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xNewEntry(rTreeView.make_iterator());
rTreeView.insert(pParent, -1, &rName, &sId, nullptr, nullptr, eEntryType == etQueryContainer, xNewEntry.get());
rTreeView.set_image(*xNewEntry, aImage);
rTreeView.set_text_emphasis(*xNewEntry, false, 0);
return xNewEntry;
}
IMPL_LINK(SbaTableQueryBrowser, OnExpandEntry, const weld::TreeIter&, rParent, bool)
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
if (rTreeView.iter_has_child(rParent))
{
// nothing to do...
return true;
}
std::unique_ptr<weld::TreeIter> xFirstParent = m_pTreeView->GetRootLevelParent(&rParent);
OSL_ENSURE(xFirstParent,"SbaTableQueryBrowser::OnExpandEntry: No rootlevelparent!");
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(rParent));
assert(pData && "SbaTableQueryBrowser::OnExpandEntry: No user data!");
if (etTableContainer == pData->eType)
{
weld::WaitObject aWaitCursor(getFrameWeld());
// it could be that we already have a connection
SharedConnection xConnection;
if (ensureConnection(xFirstParent.get(), xConnection) && xConnection.is())
{
SQLExceptionInfo aInfo;
try
{
Reference< XWarningsSupplier > xWarnings(xConnection, UNO_QUERY);
if (xWarnings.is())
xWarnings->clearWarnings();
// first insert the views because the tables can also include
// views but that time the bitmap is the wrong one
// the nameaccess will be overwritten in populateTree
Reference<XViewsSupplier> xViewSup(xConnection,UNO_QUERY);
if(xViewSup.is())
populateTree( xViewSup->getViews(), rParent, etTableOrView );
Reference<XTablesSupplier> xTabSup(xConnection,UNO_QUERY);
if(xTabSup.is())
{
populateTree( xTabSup->getTables(), rParent, etTableOrView );
Reference<XContainer> xCont(xTabSup->getTables(),UNO_QUERY);
if(xCont.is())
// add as listener to know when elements are inserted or removed
xCont->addContainerListener(this);
}
if (xWarnings.is())
{
#if 0
SQLExceptionInfo aWarnings(xWarnings->getWarnings());
// Obviously this if test is always false. So to avoid a Clang warning
// "use of logical '&&' with constant operand" I put this in #if
// 0. Yeah, I know it is fairly likely nobody will ever read this
// comment and make a decision what to do here, so I could as well
// have just binned this...
if (aWarnings.isValid() && sal_False)
{
SQLContext aContext;
aContext.Message = DBA_RES(STR_OPENTABLES_WARNINGS);
aContext.Details = DBA_RES(STR_OPENTABLES_WARNINGS_DETAILS);
aContext.NextException = aWarnings.get();
aWarnings = aContext;
showError(aWarnings);
}
#endif
// TODO: we need a better concept for these warnings:
// something like "don't show any warnings for this datasource, again" would be nice
// But this requires an extension of the InteractionHandler and an additional property on the data source
}
}
catch(const SQLContext& e) { aInfo = e; }
catch(const SQLWarning& e) { aInfo = e; }
catch(const SQLException& e) { aInfo = e; }
catch(const WrappedTargetException& e)
{
SQLException aSql;
if(e.TargetException >>= aSql)
aInfo = aSql;
else
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::OnExpandEntry: something strange happened!");
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
if (aInfo.isValid())
showError(aInfo);
}
else
return false;
// 0 indicates that an error occurred
}
else
{
// we have to expand the queries or bookmarks
if (ensureEntryObject(rParent))
{
DBTreeListUserData* pParentData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(rParent));
Reference< XNameAccess > xCollection( pParentData->xContainer, UNO_QUERY );
populateTree(xCollection, rParent, etQuery);
}
}
return true;
}
bool SbaTableQueryBrowser::ensureEntryObject(const weld::TreeIter& rEntry)
{
EntryType eType = getEntryType(rEntry);
// the user data of the entry
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
DBTreeListUserData* pEntryData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(rEntry));
assert(pEntryData && "ensureEntryObject: user data should already be set!");
std::unique_ptr<weld::TreeIter> xDataSourceEntry = m_pTreeView->GetRootLevelParent(&rEntry);
bool bSuccess = false;
switch (eType)
{
case etQueryContainer:
{
if ( pEntryData->xContainer.is() )
{
// nothing to do
bSuccess = true;
break;
}
std::unique_ptr<weld::TreeIter> xParent(rTreeView.make_iterator(&rEntry));
if (rTreeView.iter_parent(*xParent))
{
if (rTreeView.iter_compare(*xParent, *xDataSourceEntry) != 0)
{
OUString aName(rTreeView.get_text(rEntry));
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xParent));
try
{
Reference< XNameAccess > xNameAccess(pData->xContainer,UNO_QUERY);
if ( xNameAccess.is() )
pEntryData->xContainer.set(xNameAccess->getByName(aName),UNO_QUERY);
}
catch(const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
bSuccess = pEntryData->xContainer.is();
}
else
{
try
{
Reference< XQueryDefinitionsSupplier > xQuerySup;
m_xDatabaseContext->getByName(getDataSourceAccessor(*xDataSourceEntry)) >>= xQuerySup;
if (xQuerySup.is())
{
Reference< XNameAccess > xQueryDefs = xQuerySup->getQueryDefinitions();
Reference< XContainer > xCont(xQueryDefs, UNO_QUERY);
if (xCont.is())
// add as listener to get notified if elements are inserted or removed
xCont->addContainerListener(this);
pEntryData->xContainer = xQueryDefs;
bSuccess = pEntryData->xContainer.is();
}
else {
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::ensureEntryObject: no XQueryDefinitionsSupplier interface!");
}
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
}
break;
}
default:
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::ensureEntryObject: ooops ... missing some implementation here!");
// TODO ...
break;
}
return bSuccess;
}
bool SbaTableQueryBrowser::implSelect(const svx::ODataAccessDescriptor& _rDescriptor, bool _bSelectDirect)
{
// extract the props
OUString sDataSource;
OUString sCommand;
sal_Int32 nCommandType = CommandType::COMMAND;
bool bEscapeProcessing = true;
extractDescriptorProps(_rDescriptor, sDataSource, sCommand, nCommandType, bEscapeProcessing);
// select it
return implSelect( sDataSource, sCommand, nCommandType, bEscapeProcessing, SharedConnection(), _bSelectDirect );
}
bool SbaTableQueryBrowser::implLoadAnything(const OUString& _rDataSourceName, const OUString& _rCommand,
const sal_Int32 nCommandType, const bool _bEscapeProcessing, const SharedConnection& _rxConnection)
{
try
{
Reference<XPropertySet> xProp( getRowSet(), UNO_QUERY_THROW );
Reference< XLoadable > xLoadable( xProp, UNO_QUERY_THROW );
// the values allowing the RowSet to re-execute
xProp->setPropertyValue(PROPERTY_DATASOURCENAME, Any(_rDataSourceName));
if(_rxConnection.is())
xProp->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, Any( _rxConnection.getTyped() ) );
// set this _before_ setting the connection, else the rowset would rebuild it ...
xProp->setPropertyValue(PROPERTY_COMMAND_TYPE, Any(nCommandType));
xProp->setPropertyValue(PROPERTY_COMMAND, Any(_rCommand));
xProp->setPropertyValue(PROPERTY_ESCAPE_PROCESSING, css::uno::Any(_bEscapeProcessing));
if ( m_bPreview )
{
xProp->setPropertyValue(PROPERTY_FETCHDIRECTION, Any(FetchDirection::FORWARD));
}
// the formatter depends on the data source we're working on, so rebuild it here ...
initFormatter();
// switch the grid to design mode while loading
getBrowserView()->getGridControl()->setDesignMode(true);
InitializeForm( xProp );
bool bSuccess = true;
{
{
Reference< XNameContainer > xColContainer(getFormComponent(), UNO_QUERY);
// first we have to clear the grid
clearGridColumns(xColContainer);
}
FormErrorHelper aHelper(this);
// load the form
bSuccess = reloadForm(xLoadable);
// initialize the model
InitializeGridModel(getFormComponent());
Any aVal = xProp->getPropertyValue(PROPERTY_ISNEW);
if (aVal.hasValue() && ::comphelper::getBOOL(aVal))
{
// then set the default values and the parameters given from the parent
Reference< XReset> xReset(xProp, UNO_QUERY);
xReset->reset();
}
if ( m_bPreview )
initializePreviewMode();
LoadFinished(true);
}
InvalidateAll();
return bSuccess;
}
catch( const SQLException& )
{
Any aException( ::cppu::getCaughtException() );
showError( SQLExceptionInfo( aException ) );
}
catch( const WrappedTargetException& e )
{
if ( e.TargetException.isExtractableTo( ::cppu::UnoType< SQLException >::get() ) )
showError( SQLExceptionInfo( e.TargetException ) );
else
{
TOOLS_WARN_EXCEPTION("dbaccess", "");
}
}
catch(const Exception&)
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
InvalidateAll();
return false;
}
bool SbaTableQueryBrowser::implSelect(const OUString& _rDataSourceName, const OUString& _rCommand,
const sal_Int32 nCommandType, const bool _bEscapeProcessing,
const SharedConnection& _rxConnection,
bool _bSelectDirect)
{
if (!_rDataSourceName.getLength() || !_rCommand.getLength() || (-1 == nCommandType))
return false;
std::unique_ptr<weld::TreeIter> xDataSource;
std::unique_ptr<weld::TreeIter> xCommandType;
std::unique_ptr<weld::TreeIter> xCommand = getObjectEntry( _rDataSourceName, _rCommand, nCommandType, &xDataSource, &xCommandType, true, _rxConnection );
if (xCommand)
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
bool bSuccess = true;
if ( _bSelectDirect )
{
bSuccess = implSelect(xCommand.get());
}
else
{
rTreeView.select(*xCommand);
}
if ( bSuccess )
{
rTreeView.scroll_to_row(*xCommand);
rTreeView.set_cursor(*xCommand);
}
}
else if (!xCommandType)
{
if (m_xCurrentlyDisplayed)
{
// tell the old entry (if any) it has been deselected
selectPath(m_xCurrentlyDisplayed.get(), false);
m_xCurrentlyDisplayed.reset();
}
// we have a command and need to display this in the rowset
return implLoadAnything(_rDataSourceName, _rCommand, nCommandType, _bEscapeProcessing, _rxConnection);
}
return false;
}
IMPL_LINK_NOARG(SbaTableQueryBrowser, OnSelectionChange, LinkParamNone*, void)
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xSelection(rTreeView.make_iterator());
if (!rTreeView.get_selected(xSelection.get()))
xSelection.reset();
implSelect(xSelection.get());
}
std::unique_ptr<weld::TreeIter> SbaTableQueryBrowser::implGetConnectionEntry(const weld::TreeIter& rEntry) const
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xCurrentEntry(rTreeView.make_iterator(&rEntry));
DBTreeListUserData* pEntryData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xCurrentEntry));
while (pEntryData->eType != etDatasource)
{
rTreeView.iter_parent(*xCurrentEntry);
pEntryData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xCurrentEntry));
}
return xCurrentEntry;
}
bool SbaTableQueryBrowser::implSelect(const weld::TreeIter* pEntry)
{
if ( !pEntry )
return false;
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
DBTreeListUserData* pEntryData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*pEntry));
switch (pEntryData->eType)
{
case etTableOrView:
case etQuery:
break;
default:
// nothing to do
return false;
}
OSL_ENSURE(rTreeView.get_iter_depth(*pEntry) >= 2, "SbaTableQueryBrowser::implSelect: invalid entry!");
// get the entry for the tables or queries
std::unique_ptr<weld::TreeIter> xContainer = rTreeView.make_iterator(pEntry);
rTreeView.iter_parent(*xContainer);
DBTreeListUserData* pContainerData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xContainer));
// get the entry for the datasource
std::unique_ptr<weld::TreeIter> xConnection = implGetConnectionEntry(*xContainer);
DBTreeListUserData* pConData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xConnection));
// reinitialize the rowset
// but first check if it is necessary
// get all old properties
Reference<XPropertySet> xRowSetProps(getRowSet(),UNO_QUERY);
OUString aOldName;
xRowSetProps->getPropertyValue(PROPERTY_COMMAND) >>= aOldName;
sal_Int32 nOldType = 0;
xRowSetProps->getPropertyValue(PROPERTY_COMMAND_TYPE) >>= nOldType;
Reference<XConnection> xOldConnection(xRowSetProps->getPropertyValue(PROPERTY_ACTIVE_CONNECTION),UNO_QUERY);
// the name of the table or query
const OUString sSimpleName = rTreeView.get_text(*pEntry);
OUStringBuffer sNameBuffer(sSimpleName);
if ( etQueryContainer == pContainerData->eType )
{
std::unique_ptr<weld::TreeIter> xTemp = rTreeView.make_iterator(xContainer.get());
std::unique_ptr<weld::TreeIter> xNextTemp = rTreeView.make_iterator(xTemp.get());
if (rTreeView.iter_parent(*xNextTemp))
{
while (rTreeView.iter_compare(*xNextTemp, *xConnection) != 0)
{
sNameBuffer.insert(0, rTreeView.get_text(*xTemp) + "/");
rTreeView.copy_iterator(*xNextTemp, *xTemp);
if (!rTreeView.iter_parent(*xNextTemp))
break;
}
}
}
OUString aName = sNameBuffer.makeStringAndClear();
sal_Int32 nCommandType = ( etTableContainer == pContainerData->eType)
? CommandType::TABLE
: CommandType::QUERY;
// check if need to rebuild the rowset
bool bRebuild = ( xOldConnection != pConData->xConnection )
|| ( nOldType != nCommandType )
|| ( aName != aOldName );
Reference< css::form::XLoadable > xLoadable = getLoadable();
bRebuild |= !xLoadable->isLoaded();
bool bSuccess = true;
if ( bRebuild )
{
try
{
weld::WaitObject aWaitCursor(getFrameWeld());
// tell the old entry it has been deselected
selectPath(m_xCurrentlyDisplayed.get(), false);
m_xCurrentlyDisplayed.reset();
// not really loaded
m_xCurrentlyDisplayed = rTreeView.make_iterator(pEntry);
// tell the new entry it has been selected
selectPath(m_xCurrentlyDisplayed.get());
// get the name of the data source currently selected
ensureConnection(m_xCurrentlyDisplayed.get(), pConData->xConnection);
if ( !pConData->xConnection.is() )
{
unloadAndCleanup( false );
return false;
}
Reference<XNameAccess> xNameAccess;
switch(nCommandType)
{
case CommandType::TABLE:
{
// only for tables
if ( !pContainerData->xContainer.is() )
{
Reference<XTablesSupplier> xSup( pConData->xConnection, UNO_QUERY );
if(xSup.is())
xNameAccess = xSup->getTables();
pContainerData->xContainer = xNameAccess;
}
else
xNameAccess.set( pContainerData->xContainer, UNO_QUERY );
}
break;
case CommandType::QUERY:
{
if ( pContainerData->xContainer.is() )
xNameAccess.set( pContainerData->xContainer, UNO_QUERY );
else
{
Reference<XQueriesSupplier> xSup( pConData->xConnection, UNO_QUERY );
if(xSup.is())
xNameAccess = xSup->getQueries();
}
}
break;
}
OUString sStatus(DBA_RES(CommandType::TABLE == nCommandType ? STR_LOADING_TABLE : STR_LOADING_QUERY));
sStatus = sStatus.replaceFirst("$name$", aName);
BrowserViewStatusDisplay aShowStatus(static_cast<UnoDataBrowserView*>(getView()), sStatus);
bool bEscapeProcessing = true;
if(xNameAccess.is() && xNameAccess->hasByName(sSimpleName))
{
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*pEntry));
if ( !pData->xObjectProperties.is() )
{
Reference<XInterface> xObject;
if(xNameAccess->getByName(sSimpleName) >>= xObject) // remember the table or query object
{
pData->xObjectProperties.set(xObject, css::uno::UNO_QUERY);
// if the query contains a parameterized statement and preview is enabled we won't get any data.
if ( nCommandType == CommandType::QUERY && xObject.is() )
{
Reference<XPropertySet> xObjectProps(xObject,UNO_QUERY);
xObjectProps->getPropertyValue(PROPERTY_ESCAPE_PROCESSING) >>= bEscapeProcessing;
if ( m_bPreview )
{
OUString sSql;
xObjectProps->getPropertyValue(PROPERTY_COMMAND) >>= sSql;
Reference< XMultiServiceFactory > xFactory( pConData->xConnection, UNO_QUERY );
if (xFactory.is())
{
try
{
Reference<XSingleSelectQueryAnalyzer> xAnalyzer(xFactory->createInstance(SERVICE_NAME_SINGLESELECTQUERYCOMPOSER),UNO_QUERY);
if ( xAnalyzer.is() )
{
xAnalyzer->setQuery(sSql);
Reference<XParametersSupplier> xParSup(xAnalyzer,UNO_QUERY);
if ( xParSup->getParameters()->getCount() > 0 )
{
OUString sFilter = " WHERE " + xAnalyzer->getFilter();
OUString sReplace = sSql.replaceFirst(sFilter, "");
xAnalyzer->setQuery(sReplace);
Reference<XSingleSelectQueryComposer> xComposer(xAnalyzer,UNO_QUERY);
xComposer->setFilter(u"0=1"_ustr);
aName = xAnalyzer->getQuery();
nCommandType = CommandType::COMMAND;
}
}
}
catch (Exception&)
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
}
}
}
}
}
OUString sDataSourceName(getDataSourceAccessor(*xConnection));
bSuccess = implLoadAnything( sDataSourceName, aName, nCommandType, bEscapeProcessing, pConData->xConnection );
if ( !bSuccess )
{ // clean up
criticalFail();
}
}
catch(const SQLException& e)
{
showError(SQLExceptionInfo(e));
// reset the values
xRowSetProps->setPropertyValue(PROPERTY_DATASOURCENAME,Any());
xRowSetProps->setPropertyValue(PROPERTY_ACTIVE_CONNECTION,Any());
bSuccess = false;
}
catch(WrappedTargetException& e)
{
SQLException aSql;
if(e.TargetException >>= aSql)
showError(SQLExceptionInfo(aSql));
else
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::implSelect: something strange happened!");
// reset the values
xRowSetProps->setPropertyValue(PROPERTY_DATASOURCENAME,Any());
xRowSetProps->setPropertyValue(PROPERTY_ACTIVE_CONNECTION,Any());
bSuccess = false;
}
catch(const Exception&)
{
// reset the values
xRowSetProps->setPropertyValue(PROPERTY_DATASOURCENAME,Any());
xRowSetProps->setPropertyValue(PROPERTY_ACTIVE_CONNECTION,Any());
bSuccess = false;
}
}
return bSuccess;
}
std::unique_ptr<weld::TreeIter> SbaTableQueryBrowser::getEntryFromContainer(const Reference<XNameAccess>& rxNameAccess)
{
std::unique_ptr<weld::TreeIter> xContainer;
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xDSLoop(rTreeView.make_iterator(xContainer.get()));
if (rTreeView.get_iter_first(*xDSLoop))
{
do
{
xContainer = rTreeView.make_iterator(xDSLoop.get());
if (rTreeView.iter_children(*xContainer))
{
// 1st child is queries
DBTreeListUserData* pQueriesData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xContainer));
if (pQueriesData && pQueriesData->xContainer == rxNameAccess)
break;
if (rTreeView.iter_next_sibling(*xContainer))
{
// 2nd child is tables
DBTreeListUserData* pTablesData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xContainer));
if (pTablesData && pTablesData->xContainer == rxNameAccess)
break;
}
}
xContainer.reset();
}
while (rTreeView.iter_next_sibling(*xDSLoop));
}
return xContainer;
}
void SAL_CALL SbaTableQueryBrowser::elementInserted(const ContainerEvent& rEvent)
{
SolarMutexGuard aSolarGuard;
Reference< XNameAccess > xNames(rEvent.Source, UNO_QUERY);
// first search for a definition container where we can insert this element
std::unique_ptr<weld::TreeIter> xEntry = getEntryFromContainer(xNames);
if (xEntry) // found one
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
rTreeView.make_unsorted();
// insert the new entry into the tree
DBTreeListUserData* pContainerData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xEntry));
assert(pContainerData && "elementInserted: There must be user data for this type!");
DBTreeListUserData* pNewData = new DBTreeListUserData;
bool bIsTable = etTableContainer == pContainerData->eType;
if ( bIsTable )
{
rEvent.Element >>= pNewData->xObjectProperties;// remember the new element
pNewData->eType = etTableOrView;
}
else
{
if (rTreeView.iter_n_children(*xEntry) < xNames->getElementNames().getLength() - 1)
{
// the item inserts its children on demand, but it has not been expanded yet. So ensure here and
// now that it has all items
populateTree(xNames, *xEntry, etQuery);
}
pNewData->eType = etQuery;
}
implAppendEntry(xEntry.get(), ::comphelper::getString(rEvent.Accessor), pNewData);
rTreeView.make_sorted();
}
else
SbaXDataBrowserController::elementInserted(rEvent);
}
bool SbaTableQueryBrowser::isCurrentlyDisplayedChanged(std::u16string_view rName, const weld::TreeIter& rContainer)
{
if (!m_xCurrentlyDisplayed)
return false;
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
if (getEntryType(*m_xCurrentlyDisplayed) != getChildType(rContainer))
return false;
if (rTreeView.get_text(*m_xCurrentlyDisplayed) != rName)
return false;
std::unique_ptr<weld::TreeIter> xParent = rTreeView.make_iterator(m_xCurrentlyDisplayed.get());
return rTreeView.iter_parent(*xParent) && rTreeView.iter_compare(*xParent, rContainer) == 0;
}
void SAL_CALL SbaTableQueryBrowser::elementRemoved( const ContainerEvent& _rEvent )
{
SolarMutexGuard aSolarGuard;
Reference< XNameAccess > xNames(_rEvent.Source, UNO_QUERY);
// get the top-level representing the removed data source
// and search for the queries and tables
std::unique_ptr<weld::TreeIter> xContainer = getEntryFromContainer(xNames);
if (xContainer)
{
// a query or table has been removed
OUString aName = ::comphelper::getString(_rEvent.Accessor);
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
if (isCurrentlyDisplayedChanged(aName, *xContainer))
{
// the element displayed currently has been replaced
// we need to remember the old value
std::unique_ptr<weld::TreeIter> xTemp = rTreeView.make_iterator(m_xCurrentlyDisplayed.get());
// unload
unloadAndCleanup( false ); // don't dispose the connection
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xTemp));
rTreeView.set_id(*xTemp, OUString());
delete pData; // the data could be null because we have a table which isn't correct
rTreeView.remove(*xTemp);
}
else
{
// remove the entry from the model
std::unique_ptr<weld::TreeIter> xChild(rTreeView.make_iterator(xContainer.get()));
if (rTreeView.get_iter_first(*xChild))
{
do
{
if (rTreeView.get_text(*xChild) == aName)
{
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xChild));
rTreeView.set_id(*xChild, OUString());
delete pData;
rTreeView.remove(*xChild);
break;
}
} while (rTreeView.iter_next_sibling(*xChild));
}
}
// maybe the object which is part of the document data source has been removed
checkDocumentDataSource();
}
else
SbaXDataBrowserController::elementRemoved(_rEvent);
}
void SAL_CALL SbaTableQueryBrowser::elementReplaced( const ContainerEvent& _rEvent )
{
SolarMutexGuard aSolarGuard;
Reference< XNameAccess > xNames(_rEvent.Source, UNO_QUERY);
std::unique_ptr<weld::TreeIter> xContainer = getEntryFromContainer(xNames);
if (xContainer)
{
// a table or query has been replaced
OUString aName = ::comphelper::getString(_rEvent.Accessor);
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
if (isCurrentlyDisplayedChanged(aName, *xContainer))
{ // the element displayed currently has been replaced
// we need to remember the old value
std::unique_ptr<weld::TreeIter> xTemp = rTreeView.make_iterator(m_xCurrentlyDisplayed.get());
unloadAndCleanup( false ); // don't dispose the connection
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xTemp));
if (pData)
{
if ( etTableOrView == pData->eType )
{
// only insert userdata when we have a table because the query is only a commanddefinition object and not a query
_rEvent.Element >>= pData->xObjectProperties; // remember the new element
}
else
{
rTreeView.set_id(*xTemp, OUString());
delete pData;
}
}
}
else
{
// find the entry for this name
std::unique_ptr<weld::TreeIter> xChild(rTreeView.make_iterator(xContainer.get()));
if (rTreeView.get_iter_first(*xChild))
{
do
{
if (rTreeView.get_text(*xChild) == aName)
{
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xChild));
if (pData)
{
if ( etTableOrView == pData->eType )
{
// only insert userdata when we have a table because the query is only a commanddefinition object and not a query
_rEvent.Element >>= pData->xObjectProperties; // remember the new element
}
else
{
rTreeView.set_id(*xChild, OUString());
delete pData;
}
}
break;
}
} while (rTreeView.iter_next_sibling(*xChild));
}
}
// maybe the object which is part of the document data source has been removed
checkDocumentDataSource();
}
else if (xNames.get() == m_xDatabaseContext.get())
{ // a datasource has been replaced in the context
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::elementReplaced: no support for replaced data sources!");
// very suspicious: the database context should not allow to replace data source, only to register
// and revoke them
}
else
SbaXDataBrowserController::elementReplaced(_rEvent);
}
void SbaTableQueryBrowser::impl_releaseConnection( SharedConnection& _rxConnection )
{
// remove as event listener
Reference< XComponent > xComponent( _rxConnection, UNO_QUERY );
if ( xComponent.is() )
{
Reference< XEventListener > xListener( static_cast< ::cppu::OWeakObject* >( this ), UNO_QUERY );
xComponent->removeEventListener( xListener );
}
try
{
// temporary (hopefully!) hack for #i55274#
Reference< XFlushable > xFlush( _rxConnection, UNO_QUERY );
if ( xFlush.is() )
xFlush->flush();
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
// clear
_rxConnection.clear();
// will implicitly dispose if we have the ownership, since xConnection is a SharedConnection
}
void SbaTableQueryBrowser::disposeConnection(const weld::TreeIter* pDSEntry)
{
OSL_ENSURE( pDSEntry, "SbaTableQueryBrowser::disposeConnection: invalid entry (NULL)!" );
OSL_ENSURE( impl_isDataSourceEntry( pDSEntry ), "SbaTableQueryBrowser::disposeConnection: invalid entry (not top-level)!" );
if (pDSEntry)
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
DBTreeListUserData* pTreeListData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*pDSEntry));
if (pTreeListData)
impl_releaseConnection(pTreeListData->xConnection);
}
}
void SbaTableQueryBrowser::closeConnection(const weld::TreeIter& rDSEntry, bool _bDisposeConnection)
{
OSL_ENSURE(impl_isDataSourceEntry(&rDSEntry), "SbaTableQueryBrowser::closeConnection: invalid entry (not top-level)!");
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
// if one of the entries of the given DS is displayed currently, unload the form
if (m_xCurrentlyDisplayed)
{
std::unique_ptr<weld::TreeIter> xRoot = m_pTreeView->GetRootLevelParent(m_xCurrentlyDisplayed.get());
if (rTreeView.iter_compare(*xRoot, rDSEntry) == 0)
unloadAndCleanup(_bDisposeConnection);
}
// collapse the query/table container
std::unique_ptr<weld::TreeIter> xContainers(rTreeView.make_iterator(&rDSEntry));
if (rTreeView.iter_children(*xContainers))
{
do
{
std::unique_ptr<weld::TreeIter> xElements(rTreeView.make_iterator(xContainers.get()));
if (rTreeView.iter_children(*xElements))
{
rTreeView.collapse_row(*xContainers);
// and delete their children (they are connection-relative)
bool bElements = true;
while (bElements)
{
std::unique_ptr<weld::TreeIter> xRemove(rTreeView.make_iterator(xElements.get()));
bElements = rTreeView.iter_next_sibling(*xElements);
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xRemove));
rTreeView.set_id(*xRemove, OUString());
delete pData;
rTreeView.remove(*xRemove);
}
}
}
while (rTreeView.iter_next_sibling(*xContainers));
}
// collapse the entry itself
rTreeView.collapse_row(rDSEntry);
// dispose/reset the connection
if ( _bDisposeConnection )
disposeConnection(&rDSEntry);
}
void SbaTableQueryBrowser::unloadAndCleanup( bool _bDisposeConnection )
{
if (!m_xCurrentlyDisplayed)
// nothing to do
return;
std::unique_ptr<weld::TreeIter> xDSEntry = m_pTreeView->GetRootLevelParent(m_xCurrentlyDisplayed.get());
// de-select the path for the currently displayed table/query
selectPath(m_xCurrentlyDisplayed.get(), false);
m_xCurrentlyDisplayed.reset();
try
{
// get the active connection. We need to dispose it.
// unload the form
Reference< XLoadable > xLoadable = getLoadable();
if (xLoadable->isLoaded())
xLoadable->unload();
// clear the grid control
Reference< XNameContainer > xConta(getControlModel(),UNO_QUERY);
clearGridColumns(xConta);
// dispose the connection
if(_bDisposeConnection)
disposeConnection(xDSEntry.get());
}
catch(SQLException& e)
{
showError(SQLExceptionInfo(e));
}
catch(WrappedTargetException& e)
{
SQLException aSql;
if(e.TargetException >>= aSql)
showError(SQLExceptionInfo(aSql));
else
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::unloadAndCleanup: something strange happened!");
}
catch(const Exception&)
{
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::unloadAndCleanup: could not reset the form");
}
}
namespace
{
Reference< XInterface > lcl_getDataSource( const Reference< XDatabaseContext >& _rxDatabaseContext,
const OUString& _rDataSourceName, const Reference< XConnection >& _rxConnection )
{
Reference< XDataSource > xDataSource;
try
{
if ( !_rDataSourceName.isEmpty() && _rxDatabaseContext->hasByName( _rDataSourceName ) )
xDataSource.set( _rxDatabaseContext->getByName( _rDataSourceName ), UNO_QUERY_THROW );
if ( !xDataSource.is() )
{
Reference< XChild > xConnAsChild( _rxConnection, UNO_QUERY );
if ( xConnAsChild.is() )
xDataSource.set( xConnAsChild->getParent(), UNO_QUERY_THROW );
}
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
return xDataSource;
}
}
void SbaTableQueryBrowser::impl_initialize(const ::comphelper::NamedValueCollection& rArguments)
{
SolarMutexGuard aGuard;
// doin' a lot of VCL stuff here -> lock the SolarMutex
// first initialize the parent
SbaXDataBrowserController::impl_initialize(rArguments);
Reference<XConnection> xForeignConnection;
Reference< XFrame > xFrame;
OUString aTableName, aCatalogName, aSchemaName;
bool bEscapeProcessing = true;
sal_Int32 nInitialDisplayCommandType = CommandType::COMMAND;
OUString sInitialDataSourceName;
OUString sInitialCommand;
rArguments.get_ensureType( PROPERTY_DATASOURCENAME, sInitialDataSourceName );
rArguments.get_ensureType( PROPERTY_COMMAND_TYPE, nInitialDisplayCommandType );
rArguments.get_ensureType( PROPERTY_COMMAND, sInitialCommand );
rArguments.get_ensureType( PROPERTY_ACTIVE_CONNECTION, xForeignConnection );
rArguments.get_ensureType( PROPERTY_UPDATE_CATALOGNAME, aCatalogName );
rArguments.get_ensureType( PROPERTY_UPDATE_SCHEMANAME, aSchemaName );
rArguments.get_ensureType( PROPERTY_UPDATE_TABLENAME, aTableName );
rArguments.get_ensureType( PROPERTY_ESCAPE_PROCESSING, bEscapeProcessing );
rArguments.get_ensureType( u"Frame"_ustr, xFrame );
rArguments.get_ensureType( PROPERTY_SHOWMENU, m_bShowMenu );
// disable the browser if either of ShowTreeViewButton (compatibility name) or EnableBrowser
// is present and set to FALSE
bool bDisableBrowser = !rArguments.getOrDefault( u"ShowTreeViewButton"_ustr, true ) // compatibility name
|| !rArguments.getOrDefault( PROPERTY_ENABLE_BROWSER, true );
OSL_ENSURE( !rArguments.has( u"ShowTreeViewButton"_ustr ),
"SbaTableQueryBrowser::impl_initialize: ShowTreeViewButton is superseded by EnableBrowser!" );
m_bEnableBrowser = !bDisableBrowser;
// hide the tree view it is disabled in general, or if the settings tell to hide it initially
bool bHideTreeView = ( !m_bEnableBrowser )
|| !rArguments.getOrDefault( u"ShowTreeView"_ustr, true ) // compatibility name
|| !rArguments.getOrDefault( PROPERTY_SHOW_BROWSER, true );
OSL_ENSURE( !rArguments.has( u"ShowTreeView"_ustr ),
"SbaTableQueryBrowser::impl_initialize: ShowTreeView is superseded by ShowBrowser!" );
if ( bHideTreeView )
hideExplorer();
else
showExplorer();
if ( m_bPreview )
{
try
{
Sequence< OUString> aProperties
{
u"AlwaysShowCursor"_ustr, PROPERTY_BORDER, u"HasNavigationBar"_ustr, u"HasRecordMarker"_ustr, u"Tabstop"_ustr
};
Sequence< Any> aValues
{
Any(false), Any(sal_Int16(0)), Any(false), Any(false), Any(false)
};
Reference< XMultiPropertySet > xFormMultiSet(getFormComponent(), UNO_QUERY);
if ( xFormMultiSet.is() )
xFormMultiSet->setPropertyValues(aProperties, aValues);
}
catch(const Exception&)
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
// are we loaded into a (sub)frame of an embedded document (i.e. a form belonging to a database
// document)?
bool bSubFrameOfEmbeddedDocument = false;
if ( xFrame.is() )
{
Reference<XFramesSupplier> xSup = xFrame->getCreator();
Reference<XController> xCont = xSup.is() ? xSup->getController() : Reference<XController>();
bSubFrameOfEmbeddedDocument = xCont.is() && ::dbtools::isEmbeddedInDatabase( xCont->getModel(), xForeignConnection );
}
// if we have a connection at this point, it was either passed from outside, our
// determined from an outer DB document. In both cases, do not dispose it later on.
SharedConnection xConnection( xForeignConnection, SharedConnection::NoTakeOwnership );
// should we display all registered databases in the left hand side tree?
// or only *one* special?
bool bLimitedTreeEntries = false;
// if we're part of a frame which is a secondary frame of a database document, then only
// display the database for this document, not all registered ones
bLimitedTreeEntries |= bSubFrameOfEmbeddedDocument;
// if the tree view is not to be displayed at all, then only display the data source
// which was given as initial selection
bLimitedTreeEntries |= !m_bEnableBrowser;
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
rTreeView.make_unsorted();
if ( bLimitedTreeEntries )
{
if ( xConnection.is() )
{
startConnectionListening( xConnection );
// if no initial name was given, try to obtain one from the data source
if ( sInitialDataSourceName.isEmpty() )
{
Reference< XChild > xChild( xConnection, UNO_QUERY );
Reference< XPropertySet > xDataSourceProperties;
if ( xChild.is() )
xDataSourceProperties.set(xChild->getParent(), css::uno::UNO_QUERY);
if ( xDataSourceProperties.is() )
{
try
{
OSL_VERIFY( xDataSourceProperties->getPropertyValue( PROPERTY_NAME ) >>= sInitialDataSourceName );
}
catch( const Exception& )
{
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::impl_initialize: a connection parent which does not have a 'Name'!??" );
}
}
}
}
implAddDatasource( sInitialDataSourceName, xConnection );
std::unique_ptr<weld::TreeIter> xFirst(rTreeView.make_iterator());
if (rTreeView.get_iter_first(*xFirst))
rTreeView.expand_row(*xFirst);
}
else
initializeTreeModel();
rTreeView.make_sorted();
if ( m_bEnableBrowser )
{
m_aDocScriptSupport = ::std::optional< bool >( false );
}
else
{
// we are not used as "browser", but as mere view for a single table/query/command. In particular,
// there is a specific database document which we belong to.
Reference< XOfficeDatabaseDocument > xDocument( getDataSourceOrModel(
lcl_getDataSource( m_xDatabaseContext, sInitialDataSourceName, xConnection ) ), UNO_QUERY );
m_aDocScriptSupport = ::std::optional< bool >( Reference< XEmbeddedScripts >( xDocument, UNO_QUERY ).is() );
}
if ( implSelect( sInitialDataSourceName, sInitialCommand, nInitialDisplayCommandType, bEscapeProcessing, xConnection, true ) )
{
try
{
Reference< XPropertySet > xRowSetProps(getRowSet(), UNO_QUERY);
xRowSetProps->setPropertyValue(PROPERTY_UPDATE_CATALOGNAME,Any(aCatalogName));
xRowSetProps->setPropertyValue(PROPERTY_UPDATE_SCHEMANAME,Any(aSchemaName));
xRowSetProps->setPropertyValue(PROPERTY_UPDATE_TABLENAME,Any(aTableName));
}
catch(const Exception&)
{
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::impl_initialize: could not set the update related names!");
}
}
InvalidateAll();
}
bool SbaTableQueryBrowser::haveExplorer() const
{
return m_pTreeView && m_pTreeView->IsVisible();
}
void SbaTableQueryBrowser::hideExplorer()
{
if (!haveExplorer())
return;
if (!getBrowserView())
return;
m_pTreeView->Hide();
m_pSplitter->Hide();
getBrowserView()->Resize();
InvalidateFeature(ID_BROWSER_EXPLORER);
}
void SbaTableQueryBrowser::showExplorer()
{
if (haveExplorer())
return;
if (!getBrowserView())
return;
m_pTreeView->Show();
m_pSplitter->Show();
getBrowserView()->Resize();
InvalidateFeature(ID_BROWSER_EXPLORER);
}
bool SbaTableQueryBrowser::ensureConnection(const weld::TreeIter* pAnyEntry, SharedConnection& rConnection)
{
std::unique_ptr<weld::TreeIter> xDSEntry = m_pTreeView->GetRootLevelParent(pAnyEntry);
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
DBTreeListUserData* pDSData =
xDSEntry
? weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xDSEntry))
: nullptr;
return ensureConnection(xDSEntry.get(), pDSData, rConnection);
}
std::unique_ptr< ImageProvider > SbaTableQueryBrowser::getImageProviderFor(const weld::TreeIter* pAnyEntry)
{
std::unique_ptr<ImageProvider> xImageProvider(new ImageProvider);
SharedConnection xConnection;
if (getExistentConnectionFor(pAnyEntry, xConnection))
xImageProvider.reset(new ImageProvider(xConnection));
return xImageProvider;
}
bool SbaTableQueryBrowser::getExistentConnectionFor(const weld::TreeIter* pAnyEntry, SharedConnection& rConnection)
{
std::unique_ptr<weld::TreeIter> xDSEntry = m_pTreeView->GetRootLevelParent(pAnyEntry);
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
DBTreeListUserData* pDSData =
xDSEntry
? weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xDSEntry))
: nullptr;
if (pDSData)
rConnection = pDSData->xConnection;
return rConnection.is();
}
bool SbaTableQueryBrowser::impl_isDataSourceEntry(const weld::TreeIter* pEntry) const
{
if (!pEntry)
return false;
std::unique_ptr<weld::TreeIter> xRoot(m_pTreeView->GetRootLevelParent(pEntry));
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
return rTreeView.iter_compare(*xRoot, *pEntry) == 0;
}
bool SbaTableQueryBrowser::ensureConnection(const weld::TreeIter* pDSEntry, void* pDSData, SharedConnection& rConnection)
{
OSL_ENSURE( impl_isDataSourceEntry( pDSEntry ), "SbaTableQueryBrowser::ensureConnection: this entry does not denote a data source!" );
if (pDSEntry)
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
OUString aDSName = rTreeView.get_text(*pDSEntry);
DBTreeListUserData* pTreeListData = static_cast<DBTreeListUserData*>(pDSData);
if ( pTreeListData )
rConnection = pTreeListData->xConnection;
if ( !rConnection.is() && pTreeListData )
{
// show the "connecting to ..." status
OUString sConnecting(DBA_RES(STR_CONNECTING_DATASOURCE));
sConnecting = sConnecting.replaceFirst("$name$", aDSName);
BrowserViewStatusDisplay aShowStatus(static_cast<UnoDataBrowserView*>(getView()), sConnecting);
// build a string showing context information in case of error
OUString sConnectingContext(DBA_RES(STR_COULDNOTCONNECT_DATASOURCE));
sConnectingContext = sConnectingContext.replaceFirst("$name$", aDSName);
// connect
rConnection.reset(
connect(getDataSourceAccessor(*pDSEntry), sConnectingContext, nullptr),
SharedConnection::TakeOwnership);
// remember the connection
pTreeListData->xConnection = rConnection;
}
}
return rConnection.is();
}
int SbaTableQueryBrowser::OnTreeEntryCompare(const weld::TreeIter& rLHS, const weld::TreeIter& rRHS)
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
// we want the table entry and the end so we have to do a check
if (isContainer(rRHS))
{
// don't use getEntryType (directly or indirectly) for the LHS:
// LHS is currently being inserted, so it is not "completely valid" at the moment
const EntryType eRight = getEntryType(rRHS);
if (etTableContainer == eRight)
// every other container should be placed _before_ the bookmark container
return -1;
const OUString sLeft = rTreeView.get_text(rLHS);
EntryType eLeft = etTableContainer;
if (DBA_RES(RID_STR_TABLES_CONTAINER) == sLeft)
eLeft = etTableContainer;
else if (DBA_RES(RID_STR_QUERIES_CONTAINER) == sLeft)
eLeft = etQueryContainer;
if ( eLeft == eRight )
return 0;
if ( ( eLeft == etTableContainer ) && ( eRight == etQueryContainer ) )
return 1;
if ( ( eLeft == etQueryContainer ) && ( eRight == etTableContainer ) )
return -1;
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::OnTreeEntryCompare: unexpected case!" );
return 0;
}
OUString sLeftText = rTreeView.get_text(rLHS);
OUString sRightText = rTreeView.get_text(rRHS);
sal_Int32 nCompareResult = 0; // equal by default
if (m_xCollator.is())
{
try
{
nCompareResult = m_xCollator->compareString(sLeftText, sRightText);
}
catch(const Exception&)
{
}
}
else
// default behaviour if we do not have a collator -> do the simple string compare
nCompareResult = sLeftText.compareTo(sRightText);
return nCompareResult;
}
void SbaTableQueryBrowser::implAdministrate(const weld::TreeIter& rApplyTo)
{
try
{
// get the desktop object
Reference< XDesktop2 > xFrameLoader = Desktop::create( getORB() );
// the initial selection
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xTopLevelSelected(rTreeView.make_iterator(&rApplyTo));
while (rTreeView.get_iter_depth(*xTopLevelSelected))
rTreeView.iter_parent(*xTopLevelSelected);
OUString sInitialSelection = getDataSourceAccessor(*xTopLevelSelected);
Reference< XDataSource > xDataSource( getDataSourceByName( sInitialSelection, getFrameWeld(), getORB(), nullptr ) );
Reference< XModel > xDocumentModel( getDataSourceOrModel( xDataSource ), UNO_QUERY );
if ( xDocumentModel.is() )
{
Reference< XInteractionHandler2 > xInteractionHandler(
InteractionHandler::createWithParent(getORB(), nullptr) );
::comphelper::NamedValueCollection aLoadArgs;
aLoadArgs.put( u"Model"_ustr, xDocumentModel );
aLoadArgs.put( u"InteractionHandler"_ustr, xInteractionHandler );
aLoadArgs.put( u"MacroExecutionMode"_ustr, MacroExecMode::USE_CONFIG );
Sequence< PropertyValue > aLoadArgPV;
aLoadArgs >>= aLoadArgPV;
xFrameLoader->loadComponentFromURL(
xDocumentModel->getURL(),
u"_default"_ustr,
FrameSearchFlag::ALL | FrameSearchFlag::GLOBAL,
aLoadArgPV
);
}
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
bool SbaTableQueryBrowser::requestQuickHelp(const void* pUserData, OUString& rText) const
{
const DBTreeListUserData* pData = static_cast<const DBTreeListUserData*>(pUserData);
if (pData->eType == etDatasource && !pData->sAccessor.isEmpty())
{
rText = ::svt::OFileNotation(pData->sAccessor).get( ::svt::OFileNotation::N_SYSTEM);
return true;
}
return false;
}
OUString SbaTableQueryBrowser::getContextMenuResourceName() const
{
return u"explorer"_ustr;
}
IController& SbaTableQueryBrowser::getCommandController()
{
return *this;
}
::comphelper::OInterfaceContainerHelper2* SbaTableQueryBrowser::getContextMenuInterceptors()
{
return &m_aContextMenuInterceptors;
}
Any SbaTableQueryBrowser::getCurrentSelection(weld::TreeView& rControl) const
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
OSL_PRECOND( &rTreeView == &rControl,
"SbaTableQueryBrowser::getCurrentSelection: where does this come from?" );
if (&rTreeView != &rControl)
return Any();
std::unique_ptr<weld::TreeIter> xSelected(rTreeView.make_iterator());
if (!rTreeView.get_selected(xSelected.get()))
return Any();
NamedDatabaseObject aSelectedObject;
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xSelected));
aSelectedObject.Type = static_cast< sal_Int32 >( pData->eType );
switch ( aSelectedObject.Type )
{
case DatabaseObject::QUERY:
case DatabaseObject::TABLE:
aSelectedObject.Name = rTreeView.get_text(*xSelected);
break;
case DatabaseObjectContainer::DATA_SOURCE:
case DatabaseObjectContainer::QUERIES:
case DatabaseObjectContainer::TABLES:
aSelectedObject.Name = getDataSourceAccessor(*xSelected);
break;
default:
SAL_WARN("dbaccess.ui", "SbaTableQueryBrowser::getCurrentSelection: invalid (unexpected) object type!" );
break;
}
return Any( aSelectedObject );
}
vcl::Window* SbaTableQueryBrowser::getMenuParent() const
{
return m_pTreeView;
}
void SbaTableQueryBrowser::adjustMenuPosition(const weld::TreeView&, ::Point&) const
{
}
bool SbaTableQueryBrowser::implGetQuerySignature( OUString& _rCommand, bool& _bEscapeProcessing )
{
_rCommand.clear();
_bEscapeProcessing = false;
try
{
// contain the dss (data source signature) of the form
OUString sDataSourceName;
OUString sCommand;
sal_Int32 nCommandType = CommandType::COMMAND;
Reference< XPropertySet > xRowsetProps( getRowSet(), UNO_QUERY );
ODataAccessDescriptor aDesc( xRowsetProps );
sDataSourceName = aDesc.getDataSource();
aDesc[ DataAccessDescriptorProperty::Command ] >>= sCommand;
aDesc[ DataAccessDescriptorProperty::CommandType ] >>= nCommandType;
// do we need to do anything?
if ( CommandType::QUERY != nCommandType )
return false;
// get the query object
Reference< XQueryDefinitionsSupplier > xSuppQueries;
Reference< XNameAccess > xQueries;
Reference< XPropertySet > xQuery;
m_xDatabaseContext->getByName( sDataSourceName ) >>= xSuppQueries;
if ( xSuppQueries.is() )
xQueries = xSuppQueries->getQueryDefinitions();
if ( xQueries.is() )
xQueries->getByName( sCommand ) >>= xQuery;
OSL_ENSURE( xQuery.is(), "SbaTableQueryBrowser::implGetQuerySignature: could not retrieve the query object!" );
// get the two properties we need
if ( xQuery.is() )
{
xQuery->getPropertyValue( PROPERTY_COMMAND ) >>= _rCommand;
_bEscapeProcessing = ::cppu::any2bool( xQuery->getPropertyValue( PROPERTY_ESCAPE_PROCESSING ) );
return true;
}
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
return false;
}
void SbaTableQueryBrowser::frameAction(const css::frame::FrameActionEvent& aEvent)
{
if (aEvent.Frame == m_xCurrentFrameParent)
{
if(aEvent.Action == FrameAction_COMPONENT_DETACHING)
implRemoveStatusListeners();
else if (aEvent.Action == FrameAction_COMPONENT_REATTACHED)
connectExternalDispatches();
}
else
SbaXDataBrowserController::frameAction(aEvent);
}
void SbaTableQueryBrowser::clearGridColumns(const Reference< XNameContainer >& _xColContainer)
{
// first we have to clear the grid
Reference< XInterface > xColumn;
const Sequence<OUString> aColNames = _xColContainer->getElementNames();
for (const OUString& rName : aColNames)
{
_xColContainer->getByName(rName) >>= xColumn;
_xColContainer->removeByName(rName);
::comphelper::disposeComponent(xColumn);
}
}
void SbaTableQueryBrowser::loadMenu(const Reference< XFrame >& _xFrame)
{
if ( m_bShowMenu )
{
OGenericUnoController::loadMenu(_xFrame);
}
else if ( !m_bPreview )
{
Reference< css::frame::XLayoutManager > xLayoutManager = getLayoutManager(_xFrame);
if ( xLayoutManager.is() )
{
xLayoutManager->lock();
xLayoutManager->createElement( u"private:resource/toolbar/toolbar"_ustr );
xLayoutManager->unlock();
xLayoutManager->doLayout();
}
onLoadedMenu( xLayoutManager );
}
}
OUString SbaTableQueryBrowser::getPrivateTitle() const
{
OUString sTitle;
if (m_xCurrentlyDisplayed)
{
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xContainer = rTreeView.make_iterator(m_xCurrentlyDisplayed.get());
if (!rTreeView.iter_parent(*xContainer))
return OUString();
// get the entry for the datasource
std::unique_ptr<weld::TreeIter> xConnection = implGetConnectionEntry(*xContainer);
OUString sName = rTreeView.get_text(*m_xCurrentlyDisplayed);
sTitle = GetEntryText(*xConnection);
INetURLObject aURL(sTitle);
if ( aURL.GetProtocol() != INetProtocol::NotValid )
sTitle = aURL.getBase(INetURLObject::LAST_SEGMENT,true,INetURLObject::DecodeMechanism::WithCharset);
if ( !sName.isEmpty() )
{
sName += " - " + sTitle;
sTitle = sName;
}
}
return sTitle;
}
bool SbaTableQueryBrowser::preReloadForm()
{
bool bIni = false;
if (!m_xCurrentlyDisplayed)
{
// switch the grid to design mode while loading
getBrowserView()->getGridControl()->setDesignMode(true);
// we had an invalid statement so we need to connect the column models
Reference<XPropertySet> xRowSetProps(getRowSet(),UNO_QUERY);
svx::ODataAccessDescriptor aDesc(xRowSetProps);
// extract the props
OUString sDataSource;
OUString sCommand;
sal_Int32 nCommandType = CommandType::COMMAND;
bool bEscapeProcessing = true;
extractDescriptorProps(aDesc, sDataSource, sCommand, nCommandType, bEscapeProcessing);
if ( !sDataSource.isEmpty() && !sCommand.isEmpty() && (-1 != nCommandType) )
{
m_xCurrentlyDisplayed = getObjectEntry(sDataSource, sCommand, nCommandType, nullptr, nullptr);
bIni = true;
}
}
return bIni;
}
void SbaTableQueryBrowser::postReloadForm()
{
InitializeGridModel(getFormComponent());
LoadFinished(true);
}
Reference< XEmbeddedScripts > SAL_CALL SbaTableQueryBrowser::getScriptContainer()
{
// update our database document
Reference< XModel > xDocument;
try
{
Reference< XPropertySet > xCursorProps( getRowSet(), UNO_QUERY_THROW );
Reference< XConnection > xConnection( xCursorProps->getPropertyValue( PROPERTY_ACTIVE_CONNECTION ), UNO_QUERY );
if ( xConnection.is() )
{
Reference< XChild > xChild( xConnection, UNO_QUERY_THROW );
Reference< XDocumentDataSource > xDataSource( xChild->getParent(), UNO_QUERY_THROW );
xDocument.set( xDataSource->getDatabaseDocument(), UNO_QUERY_THROW );
}
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
Reference< XEmbeddedScripts > xScripts( xDocument, UNO_QUERY );
OSL_ENSURE( xScripts.is() || !xDocument.is(),
"SbaTableQueryBrowser::getScriptContainer: invalid database document!" );
return xScripts;
}
void SAL_CALL SbaTableQueryBrowser::registerContextMenuInterceptor( const Reference< XContextMenuInterceptor >& Interceptor )
{
if ( Interceptor.is() )
m_aContextMenuInterceptors.addInterface( Interceptor );
}
void SAL_CALL SbaTableQueryBrowser::releaseContextMenuInterceptor( const Reference< XContextMenuInterceptor >& Interceptor )
{
if ( Interceptor.is() )
m_aContextMenuInterceptors.removeInterface( Interceptor );
}
void SAL_CALL SbaTableQueryBrowser::registeredDatabaseLocation( const DatabaseRegistrationEvent& Event )
{
SolarMutexGuard aGuard;
implAddDatasource( Event.Name, SharedConnection() );
}
void SbaTableQueryBrowser::impl_cleanupDataSourceEntry(std::u16string_view rDataSourceName)
{
// get the top-level representing the removed data source
weld::TreeView& rTreeView = m_pTreeView->GetWidget();
std::unique_ptr<weld::TreeIter> xDataSourceEntry(rTreeView.make_iterator());
bool bDataSourceEntry = rTreeView.get_iter_first(*xDataSourceEntry);
while (bDataSourceEntry)
{
if (rTreeView.get_text(*xDataSourceEntry) == rDataSourceName)
break;
bDataSourceEntry = rTreeView.iter_next_sibling(*xDataSourceEntry);
}
OSL_ENSURE( bDataSourceEntry, "SbaTableQueryBrowser::impl_cleanupDataSourceEntry: do not know this data source!" );
if (!bDataSourceEntry)
return;
if (isSelected(*xDataSourceEntry))
{
// a table or query belonging to the deleted data source is currently being displayed.
unloadAndCleanup();
}
std::unique_ptr<weld::TreeIter> xChild(rTreeView.make_iterator(xDataSourceEntry.get()));
if (rTreeView.iter_children(*xChild))
{
do
{
// delete any user data of the child entries of the to-be-removed entry
const DBTreeListUserData* pData = weld::fromId<const DBTreeListUserData*>(rTreeView.get_id(*xChild));
rTreeView.set_id(*xChild, OUString());
delete pData;
} while (rTreeView.iter_next_sibling(*xChild));
}
// remove the entry
DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(*xDataSourceEntry));
rTreeView.set_id(*xDataSourceEntry, OUString());
delete pData;
rTreeView.remove(*xDataSourceEntry);
}
void SAL_CALL SbaTableQueryBrowser::revokedDatabaseLocation( const DatabaseRegistrationEvent& Event )
{
SolarMutexGuard aGuard;
impl_cleanupDataSourceEntry( Event.Name );
// maybe the object which is part of the document data source has been removed
checkDocumentDataSource();
}
void SAL_CALL SbaTableQueryBrowser::changedDatabaseLocation( const DatabaseRegistrationEvent& Event )
{
SolarMutexGuard aGuard;
// in case the data source was expanded, and connected, we need to clean it up
// for simplicity, just do as if the data source were completely removed and re-added
impl_cleanupDataSourceEntry( Event.Name );
implAddDatasource( Event.Name, SharedConnection() );
}
} // namespace dbaui
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|