summaryrefslogtreecommitdiff
path: root/ucb/workben/ucb/ucbdemo.cxx
blob: 4dc3c920c5422b2fe74e4c18f8a32138ccad84d7 (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_ucb.hxx"

#include <stack>
#include <rtl/ustrbuf.hxx>
#include <osl/mutex.hxx>
#include <vos/process.hxx>
#include <cppuhelper/weak.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <com/sun/star/ucb/ContentAction.hpp>
#include <com/sun/star/ucb/OpenCommandArgument2.hpp>
#include <com/sun/star/ucb/ContentResultSetCapability.hpp>
#include <com/sun/star/ucb/SearchCommandArgument.hpp>
#include <com/sun/star/ucb/NameClash.hpp>
#include <com/sun/star/ucb/TransferInfo.hpp>
#include <com/sun/star/ucb/GlobalTransferCommandArgument.hpp>
#include <com/sun/star/ucb/XContentIdentifierFactory.hpp>
#include <com/sun/star/ucb/CommandInfo.hpp>
#include <com/sun/star/ucb/XContentProviderManager.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/beans/Property.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/ucb/CHAOSProgressStart.hpp>
#include <com/sun/star/ucb/OpenMode.hpp>
#include <com/sun/star/ucb/ResultSetException.hpp>
#include <com/sun/star/io/XOutputStream.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/XPropertyContainer.hpp>
#include <com/sun/star/ucb/XProgressHandler.hpp>
#include <com/sun/star/ucb/XCommandEnvironment.hpp>
#include <com/sun/star/beans/XPropertiesChangeListener.hpp>
#include <com/sun/star/beans/XPropertiesChangeNotifier.hpp>
#include <com/sun/star/ucb/XCommandProcessor.hpp>
#include <com/sun/star/ucb/XDynamicResultSet.hpp>
#include <com/sun/star/sdbc/XRow.hpp>
#include <com/sun/star/ucb/XContentAccess.hpp>
#include <com/sun/star/ucb/XCommandInfo.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/ucb/XSortedDynamicResultSetFactory.hpp>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <comphelper/processfactory.hxx>
#include <ucbhelper/configurationkeys.hxx>
#include <ucbhelper/fileidentifierconverter.hxx>
#include <ucbhelper/contentbroker.hxx>
#include <tools/debug.hxx>

#include "tools/time.hxx"
#include <vcl/wrkwin.hxx>
#include <vcl/toolbox.hxx>
#include <vcl/edit.hxx>
#include <vcl/lstbox.hxx>
#include <vcl/svapp.hxx>
#include <vcl/help.hxx>
#include <srcharg.hxx>

using ucbhelper::getLocalFileURL;
using ucbhelper::getSystemPathFromFileURL;
using ucbhelper::getFileURLFromSystemPath;

using namespace com::sun::star;

/*========================================================================
 *
 * MyOutWindow.
 *
 *======================================================================*/

#define MYOUTWINDOW_MAXLINES 4096

class MyOutWindow : public ListBox
{
public:
    MyOutWindow( Window *pParent, WinBits nWinStyle )
    : ListBox ( pParent, nWinStyle | WB_AUTOHSCROLL ) {}
    ~MyOutWindow() {}

    void Append( const String &rLine );
};

//-------------------------------------------------------------------------
void MyOutWindow::Append( const String &rLine )
{
    String aLine( rLine );

    xub_StrLen nPos = aLine.Search( '\n' );
    while ( nPos != STRING_NOTFOUND )
    {
        if ( GetEntryCount() >= MYOUTWINDOW_MAXLINES )
            RemoveEntry( 0 );

        InsertEntry( aLine.Copy( 0, nPos ) );

        aLine.Erase( 0, nPos + 1 );
        nPos = aLine.Search( '\n' );
    }

    if ( GetEntryCount() >= MYOUTWINDOW_MAXLINES )
        RemoveEntry( 0 );

    InsertEntry( aLine );

    SetTopEntry( MYOUTWINDOW_MAXLINES - 1 );
}

/*========================================================================
 *
 * MessagePrinter.
 *
 *=======================================================================*/

class MessagePrinter
{
protected:
    MyOutWindow* m_pOutEdit;

public:
    MessagePrinter( MyOutWindow* pOutEdit = NULL )
    : m_pOutEdit( pOutEdit ) {}
    void setOutEdit( MyOutWindow* pOutEdit )
    { m_pOutEdit = pOutEdit; }
    void print( const sal_Char* pText );
    void print( const UniString& rText );
};

//-------------------------------------------------------------------------
void MessagePrinter::print( const sal_Char* pText )
{
    print( UniString::CreateFromAscii( pText ) );
}

//-------------------------------------------------------------------------
void MessagePrinter::print( const UniString& rText )
{
    SolarMutexGuard aGuard;

    if ( m_pOutEdit )
    {
        m_pOutEdit->Append( rText );
        m_pOutEdit->Update();
    }
}

//============================================================================
//
//  TestOutputStream
//
//============================================================================

class TestOutputStream:
    public cppu::OWeakObject,
    public io::XOutputStream
{
    rtl::OUString m_sStart;
    bool m_bMore;

public:
    TestOutputStream(): m_bMore(false) {}

    virtual uno::Any SAL_CALL queryInterface(const uno::Type & rType)
    throw(uno::RuntimeException);
    virtual void SAL_CALL acquire() throw ()
    { OWeakObject::acquire(); }

    virtual void SAL_CALL release() throw ()
    { OWeakObject::release(); }

    virtual void SAL_CALL writeBytes(const uno::Sequence< sal_Int8 > & rData)
        throw(uno::RuntimeException);

    virtual void SAL_CALL flush() throw() {}

    virtual void SAL_CALL closeOutput() throw() {};

    rtl::OUString getStart() const;
};

//============================================================================
// virtual
uno::Any SAL_CALL
TestOutputStream::queryInterface(const uno::Type & rType)
    throw(uno::RuntimeException)
{
    uno::Any aRet = cppu::queryInterface(rType,
                        static_cast< io::XOutputStream * >(this));
    return aRet.hasValue() ? aRet : OWeakObject::queryInterface(rType);
}

//============================================================================
// virtual
void SAL_CALL TestOutputStream::writeBytes(
                                    const uno::Sequence< sal_Int8 > & rData)
    throw(uno::RuntimeException)
{
    sal_Int32 nLen = rData.getLength();
    if (m_sStart.getLength() + nLen > 500)
    {
        nLen = 500 - m_sStart.getLength();
        m_bMore = true;
    }
    m_sStart
        += rtl::OUString(reinterpret_cast< const sal_Char * >(rData.
                                                              getConstArray()),
                         nLen, RTL_TEXTENCODING_ISO_8859_1);
}

//============================================================================
rtl::OUString TestOutputStream::getStart() const
{
    rtl::OUString sResult = m_sStart;
    if (m_bMore)
        sResult += rtl::OUString::createFromAscii("...");
    return sResult;
}

/*========================================================================
 *
 * ProgressHandler.
 *
 *=======================================================================*/

class ProgressHandler:
    public cppu::OWeakObject,
    public ucb::XProgressHandler
{
    MessagePrinter & m_rPrinter;

    rtl::OUString toString(const uno::Any & rStatus);

public:
    ProgressHandler(MessagePrinter & rThePrinter): m_rPrinter(rThePrinter) {}

    virtual uno::Any SAL_CALL queryInterface(
                                const uno::Type & rType)
        throw(uno::RuntimeException);

    virtual void SAL_CALL acquire() throw ()
    { OWeakObject::acquire(); }

    virtual void SAL_CALL release() throw ()
    { OWeakObject::release(); }

    virtual void SAL_CALL push(const uno::Any & rStatus)
        throw (uno::RuntimeException);

    virtual void SAL_CALL update(const uno::Any & rStatus)
        throw (uno::RuntimeException);

    virtual void SAL_CALL pop() throw (uno::RuntimeException);
};

rtl::OUString ProgressHandler::toString(const uno::Any & rStatus)
{
    ucb::CHAOSProgressStart aStart;
    if (rStatus >>= aStart)
    {
        rtl::OUString sResult;
        if (aStart.Text.getLength() > 0)
        {
            sResult = aStart.Text;
            sResult += rtl::OUString::createFromAscii(" ");
        }
        sResult += rtl::OUString::createFromAscii("[");
        sResult += rtl::OUString::valueOf(aStart.Minimum);
        sResult += rtl::OUString::createFromAscii("..");
        sResult += rtl::OUString::valueOf(aStart.Maximum);
        sResult += rtl::OUString::createFromAscii("]");
        return sResult;
    }

    rtl::OUString sText;
    if (rStatus >>= sText)
        return sText;

    sal_Int32 nValue;
    if (rStatus >>= nValue)
    {
        rtl::OUString sResult = rtl::OUString::createFromAscii("..");
        sResult += rtl::OUString::valueOf(nValue);
        sResult += rtl::OUString::createFromAscii("..");
        return rtl::OUString(sResult);
    }

    return rtl::OUString::createFromAscii("(Unknown object)");
}

//============================================================================
// virtual
uno::Any SAL_CALL
ProgressHandler::queryInterface( const uno::Type & rType )
    throw(uno::RuntimeException)
{
    uno::Any aRet = cppu::queryInterface(
                        rType,
                        static_cast< ucb::XProgressHandler* >(this));
    return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
}

//============================================================================
// virtual
void SAL_CALL ProgressHandler::push(const uno::Any & rStatus)
    throw (uno::RuntimeException)
{
    rtl::OUString sMessage = rtl::OUString::createFromAscii("Status push: ");
    sMessage += toString(rStatus);
    m_rPrinter.print(sMessage);
}

//============================================================================
// virtual
void SAL_CALL ProgressHandler::update(const uno::Any & rStatus)
    throw (uno::RuntimeException)
{
    rtl::OUString sMessage = rtl::OUString::createFromAscii("Status update: ");
    sMessage += toString(rStatus);
    m_rPrinter.print(sMessage);
}

//============================================================================
// virtual
void SAL_CALL ProgressHandler::pop() throw (uno::RuntimeException)
{
    m_rPrinter.print("Status pop");
}

/*========================================================================
 *
 * Ucb.
 *
 *=======================================================================*/

#define UCB_MODULE_NAME  "ucb1"

class Ucb : public MessagePrinter
{
private:
    uno::Reference< lang::XMultiServiceFactory >      m_xFac;
    uno::Reference< ucb::XContentProvider >          m_xProv;
    uno::Reference< ucb::XContentIdentifierFactory > m_xIdFac;
    rtl::OUString m_aConfigurationKey1;
    rtl::OUString m_aConfigurationKey2;
    sal_Bool m_bInited : 1;

    static rtl::OUString getUnoURL();

public:
    Ucb( uno::Reference< lang::XMultiServiceFactory >& rxFactory,
         rtl::OUString const & rConfigurationKey1,
         rtl::OUString const & rConfigurationKey2 );
    ~Ucb();

    sal_Bool init();

    uno::Reference< lang::XMultiServiceFactory > getServiceFactory() const
    { return m_xFac; }

    uno::Reference< ucb::XContentIdentifierFactory >
    getContentIdentifierFactory();
    uno::Reference< ucb::XContentProvider >
    getContentProvider();

    static rtl::OUString m_aProtocol;
};

// static
rtl::OUString Ucb::m_aProtocol;

//-------------------------------------------------------------------------
// static
rtl::OUString Ucb::getUnoURL()
{
    rtl::OUString aUnoURL(rtl::OUString::createFromAscii(
                         "uno:socket,host=localhost,port=8121;"));
    if (m_aProtocol.getLength() == 0)
        aUnoURL += rtl::OUString::createFromAscii("urp");
    else
        aUnoURL += m_aProtocol;
    aUnoURL += rtl::OUString::createFromAscii(";UCB.Factory");
    return aUnoURL;
}

//-------------------------------------------------------------------------
Ucb::Ucb( uno::Reference< lang::XMultiServiceFactory >& rxFactory,
          rtl::OUString const & rConfigurationKey1,
          rtl::OUString const & rConfigurationKey2 )
: m_xFac( rxFactory ),
  m_aConfigurationKey1( rConfigurationKey1 ),
  m_aConfigurationKey2( rConfigurationKey2 ),
  m_bInited( sal_False )
{
}

//-------------------------------------------------------------------------
Ucb::~Ucb()
{
}

//-------------------------------------------------------------------------
sal_Bool Ucb::init()
{
    if ( m_bInited )
        return sal_True;

    // Create auto configured UCB:
    if (m_xFac.is())
        try
        {
            rtl::OUString aPipe;
            vos::OSecurity().getUserIdent(aPipe);
            uno::Sequence< uno::Any > aArgs(4);
            aArgs[0] <<= m_aConfigurationKey1;
            aArgs[1] <<= m_aConfigurationKey2;
            aArgs[2] <<= rtl::OUString::createFromAscii("PIPE");
            aArgs[3] <<= aPipe;

            ::ucbhelper::ContentBroker::initialize( m_xFac, aArgs );
            m_xProv
                = ::ucbhelper::ContentBroker::get()->getContentProviderInterface();

        }
        catch (uno::Exception const &) {}

    if (m_xProv.is())
    {
        print("UCB initialized");
        uno::Reference< ucb::XContentProviderManager > xProvMgr(
            m_xProv, uno::UNO_QUERY);
        if (xProvMgr.is())
        {
            print("Registered schemes:");
            uno::Sequence< ucb::ContentProviderInfo >
                aInfos(xProvMgr->queryContentProviders());
            for (sal_Int32 i = 0; i < aInfos.getLength(); ++i)
            {
                String aText(RTL_CONSTASCII_USTRINGPARAM("    "));
                aText += UniString(aInfos[i].Scheme);
                print(aText);
            }
        }
    }
    else
        print("Error initializing UCB");

    m_bInited = m_xProv.is();
    return m_bInited;
}

//-------------------------------------------------------------------------
uno::Reference< ucb::XContentIdentifierFactory >
Ucb::getContentIdentifierFactory()
{
    if ( !m_xIdFac.is() )
    {
        if ( init() )
            m_xIdFac = uno::Reference< ucb::XContentIdentifierFactory >(
                            m_xProv, uno::UNO_QUERY );
    }

    return m_xIdFac;
}

//-------------------------------------------------------------------------
uno::Reference< ucb::XContentProvider > Ucb::getContentProvider()
{
    if ( !m_xProv.is() )
        init();

    return m_xProv;
}

/*========================================================================
 *
 * UcbTaskEnvironment.
 *
 *=======================================================================*/

class UcbTaskEnvironment : public cppu::OWeakObject,
                           public ucb::XCommandEnvironment
{
    uno::Reference< task::XInteractionHandler > m_xInteractionHandler;
    uno::Reference< ucb::XProgressHandler > m_xProgressHandler;

public:
    UcbTaskEnvironment( const uno::Reference< task::XInteractionHandler>&
                         rxInteractionHandler,
                        const uno::Reference< ucb::XProgressHandler>&
                         rxProgressHandler );
    virtual ~UcbTaskEnvironment();

    // Interface implementations...

    // XInterface

    virtual uno::Any SAL_CALL queryInterface( const uno::Type & rType )
        throw( uno::RuntimeException );
    virtual void SAL_CALL acquire()
        throw();
    virtual void SAL_CALL release()
        throw();

     // XCommandEnvironemnt

    virtual uno::Reference<task::XInteractionHandler> SAL_CALL
    getInteractionHandler()
        throw (uno::RuntimeException)
    { return m_xInteractionHandler; }

    virtual uno::Reference<ucb::XProgressHandler> SAL_CALL
    getProgressHandler()
        throw (uno::RuntimeException)
    { return m_xProgressHandler; }
 };

//-------------------------------------------------------------------------
UcbTaskEnvironment::UcbTaskEnvironment(
                    const uno::Reference< task::XInteractionHandler >&
                     rxInteractionHandler,
                    const uno::Reference< ucb::XProgressHandler >&
                     rxProgressHandler )
: m_xInteractionHandler( rxInteractionHandler ),
  m_xProgressHandler( rxProgressHandler )
{
}

//-------------------------------------------------------------------------
// virtual
UcbTaskEnvironment::~UcbTaskEnvironment()
{
}

//----------------------------------------------------------------------------
//
// XInterface methods
//
//----------------------------------------------------------------------------

// virtual
uno::Any SAL_CALL
UcbTaskEnvironment::queryInterface( const uno::Type & rType )
    throw( uno::RuntimeException )
{
    uno::Any aRet = cppu::queryInterface(
            rType, static_cast< ucb::XCommandEnvironment* >( this ) );
    return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
}

//----------------------------------------------------------------------------
// virtual
void SAL_CALL UcbTaskEnvironment::acquire()
    throw()
{
    OWeakObject::acquire();
}

//----------------------------------------------------------------------------
// virtual
void SAL_CALL UcbTaskEnvironment::release()
    throw()
{
    OWeakObject::release();
}

/*========================================================================
 *
 * UcbCommandProcessor.
 *
 *=======================================================================*/

class UcbCommandProcessor : public MessagePrinter
{
protected:
    Ucb& m_rUCB;

private:
    uno::Reference< ucb::XCommandProcessor > m_xProcessor;
    sal_Int32 m_aCommandId;

public:
    UcbCommandProcessor( Ucb& rUCB,
                         const uno::Reference<
                            ucb::XCommandProcessor >& rxProcessor,
                         MyOutWindow* pOutEdit );

    virtual ~UcbCommandProcessor();

    uno::Any executeCommand( const rtl::OUString& rName,
                             const uno::Any& rArgument,
                             bool bPrint = true );
};

//-------------------------------------------------------------------------
UcbCommandProcessor::UcbCommandProcessor( Ucb& rUCB,
                                          const uno::Reference<
                                            ucb::XCommandProcessor >&
                                              rxProcessor,
                                          MyOutWindow* pOutEdit)
: MessagePrinter( pOutEdit ),
  m_rUCB( rUCB ),
  m_xProcessor( rxProcessor ),
  m_aCommandId( 0 )
{
    if ( m_xProcessor.is() )
    {
        // Generally, one command identifier per thread is enough. It
        // can be used for all commands executed by the processor which
        // created this id.
        m_aCommandId = m_xProcessor->createCommandIdentifier();
    }
}

//----------------------------------------------------------------------------
// virtual
UcbCommandProcessor::~UcbCommandProcessor()
{
}

//----------------------------------------------------------------------------
uno::Any UcbCommandProcessor::executeCommand( const rtl::OUString& rName,
                                              const uno::Any& rArgument,
                                              bool bPrint )
{
    if ( m_xProcessor.is() )
    {
        ucb::Command aCommand;
        aCommand.Name     = rName;
        aCommand.Handle   = -1; /* unknown */
        aCommand.Argument = rArgument;

        uno::Reference< task::XInteractionHandler > xInteractionHandler;
        if (m_rUCB.getServiceFactory().is())
            xInteractionHandler
                = uno::Reference< task::XInteractionHandler >(
                      m_rUCB.getServiceFactory()->
                          createInstance(
                              rtl::OUString::createFromAscii(
                                  "com.sun.star.task.InteractionHandler")),
                      uno::UNO_QUERY);
        uno::Reference< ucb::XProgressHandler >
            xProgressHandler(new ProgressHandler(m_rUCB));
        uno::Reference< ucb::XCommandEnvironment > xEnv(
            new UcbTaskEnvironment( xInteractionHandler, xProgressHandler ) );

        if ( bPrint )
        {
            UniString aText( UniString::CreateFromAscii(
                                RTL_CONSTASCII_STRINGPARAM(
                                    "Executing command: " ) ) );
            aText += UniString( rName );
            print( aText );
        }

        // Execute command
        uno::Any aResult;
        bool bException = false;
        bool bAborted = false;
        try
        {
            aResult = m_xProcessor->execute( aCommand, m_aCommandId, xEnv );
        }
        catch ( ucb::CommandAbortedException const & )
        {
            bAborted = true;
        }
        catch ( uno::Exception const & )
        {
            bException = true;
        }

        if ( bPrint )
        {
            if ( bException )
                print( "execute(...) threw an exception!" );

            if ( bAborted )
                print( "execute(...) aborted!" );

            if ( !bException && !bAborted )
                print( "execute() finished." );
        }

        return aResult;
    }

    print( "executeCommand failed!" );
    return uno::Any();
}

/*========================================================================
 *
 * UcbContent.
 *
 *=======================================================================*/

class UcbContent : public UcbCommandProcessor,
                   public cppu::OWeakObject,
                   public ucb::XContentEventListener,
                   public beans::XPropertiesChangeListener
{
    uno::Reference< ucb::XContent > m_xContent;

    struct OpenStackEntry
    {
        uno::Reference< ucb::XContentIdentifier > m_xIdentifier;
        uno::Reference< ucb::XContent > m_xContent;
        sal_uInt32 m_nLevel;
        bool m_bUseIdentifier;

        OpenStackEntry(uno::Reference< ucb::XContentIdentifier > const &
                        rTheIdentifier,
                       sal_uInt32 nTheLevel):
            m_xIdentifier(rTheIdentifier), m_nLevel(nTheLevel),
            m_bUseIdentifier(true) {}

        OpenStackEntry(uno::Reference< ucb::XContent > const & rTheContent,
                       sal_uInt32 nTheLevel):
            m_xContent(rTheContent), m_nLevel(nTheLevel),
            m_bUseIdentifier(false) {}
    };
    typedef std::stack< OpenStackEntry > OpenStack;

private:
    UcbContent( Ucb& rUCB,
                uno::Reference< ucb::XContent >& rxContent,
                MyOutWindow* pOutEdit );

protected:
    virtual ~UcbContent();

public:
    static UcbContent* create(
            Ucb& rUCB, const UniString& rURL, MyOutWindow* pOutEdit );
    void dispose();

    const UniString getURL() const;
    const UniString getType() const;

    uno::Sequence< ucb::CommandInfo > getCommands();
    uno::Sequence< beans::Property >    getProperties();

    uno::Any  getPropertyValue( const rtl::OUString& rName );
    void setPropertyValue( const rtl::OUString& rName, const uno::Any& rValue );
    void addProperty     ( const rtl::OUString& rName, const uno::Any& rValue );
    void removeProperty  ( const rtl::OUString& rName );

    rtl::OUString getStringPropertyValue( const rtl::OUString& rName );
    void setStringPropertyValue( const rtl::OUString& rName,
                                 const rtl::OUString& rValue );
    void addStringProperty( const rtl::OUString& rName,
                            const rtl::OUString& rValue );
    void open( const rtl::OUString & rName, const UniString& rInput,
               bool bPrint, bool bTiming, bool bSort,
               OpenStack * pStack = 0, sal_uInt32 nLevel = 0,
               sal_Int32 nFetchSize = 0 );
    void openAll( Ucb& rUCB, bool bPrint, bool bTiming, bool bSort,
                  sal_Int32 nFetchSize );
    void transfer( const rtl::OUString& rSourceURL, sal_Bool bMove );
    void destroy();

    // XInterface
    virtual uno::Any SAL_CALL queryInterface( const uno::Type & rType )
        throw( uno::RuntimeException );
    virtual void SAL_CALL
    acquire()
        throw();
    virtual void SAL_CALL
    release()
        throw();

    // XEventListener
    // ( base interface of XContentEventListener, XPropertiesChangeListener )
    virtual void SAL_CALL
    disposing( const lang::EventObject& Source )
        throw( uno::RuntimeException );

    // XContentEventListener
    virtual void SAL_CALL
    contentEvent( const ucb::ContentEvent& evt )
        throw( uno::RuntimeException );

    // XPropertiesChangeListener
    virtual void SAL_CALL
    propertiesChange( const uno::Sequence< beans::PropertyChangeEvent >& evt )
        throw( uno::RuntimeException );
};

//-------------------------------------------------------------------------
UcbContent::UcbContent( Ucb& rUCB,
                        uno::Reference< ucb::XContent >& rxContent,
                        MyOutWindow* pOutEdit)
: UcbCommandProcessor( rUCB,
                       uno::Reference< ucb::XCommandProcessor >(
                                                rxContent, uno::UNO_QUERY ),
                       pOutEdit ),
  m_xContent( rxContent )
{
}

//----------------------------------------------------------------------------
// virtual
UcbContent::~UcbContent()
{
}

//-------------------------------------------------------------------------
// static
UcbContent* UcbContent::create(
        Ucb& rUCB, const UniString& rURL, MyOutWindow* pOutEdit )
{
    if ( !rURL.Len() )
        return NULL;

    //////////////////////////////////////////////////////////////////////
    // Get XContentIdentifier interface from UCB and let it create an
    // identifer for the given URL.
    //////////////////////////////////////////////////////////////////////

    uno::Reference< ucb::XContentIdentifierFactory > xIdFac =
                                        rUCB.getContentIdentifierFactory();
    if ( !xIdFac.is() )
        return NULL;

    uno::Reference< ucb::XContentIdentifier > xId =
                            xIdFac->createContentIdentifier( rURL );
    if ( !xId.is() )
        return NULL;

    //////////////////////////////////////////////////////////////////////
    // Get XContentProvider interface from UCB and let it create a
    // content for the given identifier.
    //////////////////////////////////////////////////////////////////////

    uno::Reference< ucb::XContentProvider > xProv
        = rUCB.getContentProvider();
    if ( !xProv.is() )
        return NULL;

    uno::Reference< ucb::XContent > xContent;
    try
    {
        xContent = xProv->queryContent( xId );
    }
    catch (ucb::IllegalIdentifierException const &) {}
    if ( !xContent.is() )
        return NULL;

    UcbContent* pNew = new UcbContent( rUCB, xContent, pOutEdit );
    pNew->acquire();

    // Register listener(s).
    xContent->addContentEventListener( pNew );

    uno::Reference< beans::XPropertiesChangeNotifier > xNotifier(
        xContent, uno::UNO_QUERY );
    if ( xNotifier.is() )
    {
        // Empty sequence -> interested in any property changes.
        xNotifier->addPropertiesChangeListener(
            uno::Sequence< rtl::OUString >(), pNew );
    }

    return pNew;
}

//-------------------------------------------------------------------------
const UniString UcbContent::getURL() const
{
    uno::Reference< ucb::XContentIdentifier > xId(
        m_xContent->getIdentifier() );
    if ( xId.is() )
        return UniString( xId->getContentIdentifier() );

    return UniString();
}

//-------------------------------------------------------------------------
const UniString UcbContent::getType() const
{
    const UniString aType( m_xContent->getContentType() );
    return aType;
}

//-------------------------------------------------------------------------
void UcbContent::dispose()
{
    uno::Reference< lang::XComponent > xComponent( m_xContent, uno::UNO_QUERY );
    if ( xComponent.is() )
        xComponent->dispose();
}

//----------------------------------------------------------------------------
void UcbContent::open( const rtl::OUString & rName, const UniString& rInput,
                       bool bPrint, bool bTiming, bool bSort,
                       OpenStack * pStack, sal_uInt32 nLevel,
                       sal_Int32 nFetchSize )
{
    uno::Any aArg;

    bool bDoSort = false;

    ucb::OpenCommandArgument2 aOpenArg;
    if (rName.compareToAscii("search") == 0)
    {
        ucb::SearchCommandArgument aArgument;
        if (!parseSearchArgument(rInput, aArgument.Info))
        {
            print("Can't parse search argument");
            return;
        }
        aArgument.Properties.realloc(5);
        aArgument.Properties[0].Name = rtl::OUString::createFromAscii("Title");
        aArgument.Properties[0].Handle = -1;
        aArgument.Properties[1].Name
            = rtl::OUString::createFromAscii("DateCreated");
        aArgument.Properties[1].Handle = -1;
        aArgument.Properties[2].Name = rtl::OUString::createFromAscii("Size");
        aArgument.Properties[2].Handle = -1;
        aArgument.Properties[3].Name
            = rtl::OUString::createFromAscii("IsFolder");
        aArgument.Properties[3].Handle = -1;
        aArgument.Properties[4].Name
            = rtl::OUString::createFromAscii("IsDocument");
        aArgument.Properties[4].Handle = -1;
        aArg <<= aArgument;
    }
    else
    {
        aOpenArg.Mode = ucb::OpenMode::ALL;
        aOpenArg.Priority = 32768;
//		if ( bFolder )
        {
            // Property values which shall be in the result set...
            uno::Sequence< beans::Property > aProps( 5 );
            beans::Property* pProps = aProps.getArray();
            pProps[ 0 ].Name   = rtl::OUString::createFromAscii( "Title" );
            pProps[ 0 ].Handle = -1; // Important!
/**/		pProps[ 0 ].Type = getCppuType(static_cast< rtl::OUString * >(0));
                // HACK for sorting...
            pProps[ 1 ].Name   = rtl::OUString::createFromAscii( "DateCreated" );
            pProps[ 1 ].Handle = -1; // Important!
            pProps[ 2 ].Name   = rtl::OUString::createFromAscii( "Size" );
            pProps[ 2 ].Handle = -1; // Important!
            pProps[ 3 ].Name   = rtl::OUString::createFromAscii( "IsFolder" );
            pProps[ 3 ].Handle = -1; // Important!
/**/		pProps[ 3 ].Type = getCppuType(static_cast< sal_Bool * >(0));
                // HACK for sorting...
            pProps[ 4 ].Name   = rtl::OUString::createFromAscii( "IsDocument" );
            pProps[ 4 ].Handle = -1; // Important!
            aOpenArg.Properties = aProps;

            bDoSort = bSort;
            if (bDoSort)
            {
                // Sort criteria... Note that column numbering starts with 1!
                aOpenArg.SortingInfo.realloc(2);
                // primary sort criterium: column 4 --> IsFolder
                aOpenArg.SortingInfo[ 0 ].ColumnIndex = 4;
                aOpenArg.SortingInfo[ 0 ].Ascending   = sal_False;
                // secondary sort criterium: column 1 --> Title
                aOpenArg.SortingInfo[ 1 ].ColumnIndex = 1;
                aOpenArg.SortingInfo[ 1 ].Ascending   = sal_True;
            }
        }
//		else
            aOpenArg.Sink
                = static_cast< cppu::OWeakObject * >(new TestOutputStream);
        aArg <<= aOpenArg;
    }

//	putenv("PROT_REMOTE_ACTIVATE=1"); // to log remote uno traffic

    ULONG nTime = 0;
    if ( bTiming )
        nTime = Time::GetSystemTicks();

    uno::Any aResult = executeCommand( rName, aArg, bPrint );

    uno::Reference< ucb::XDynamicResultSet > xDynamicResultSet;
    if ( ( aResult >>= xDynamicResultSet ) && xDynamicResultSet.is() )
    {
        if (bDoSort)
        {
            sal_Int16 nCaps = xDynamicResultSet->getCapabilities();
            if (!(nCaps & ucb::ContentResultSetCapability::SORTED))
            {
                if (bPrint)
                    print("Result set rows are not sorted"
                              "---using sorting cursor");

                uno::Reference< ucb::XSortedDynamicResultSetFactory >
                    xSortedFactory;
                if (m_rUCB.getServiceFactory().is())
                    xSortedFactory
                        = uno::Reference<
                            ucb::XSortedDynamicResultSetFactory >(
                              m_rUCB.
                                  getServiceFactory()->
                                      createInstance(
                                          rtl::OUString::createFromAscii(
                                              "com.sun.star.ucb.SortedDynamic"
                                                  "ResultSetFactory")),
                              uno::UNO_QUERY);
                uno::Reference< ucb::XDynamicResultSet > xSorted;
                if (xSortedFactory.is())
                    xSorted
                        = xSortedFactory->
                              createSortedDynamicResultSet(xDynamicResultSet,
                                                           aOpenArg.
                                                               SortingInfo,
                                                           0);
                if (xSorted.is())
                    xDynamicResultSet = xSorted;
                else
                    print("Sorting cursor not available!");
            }
        }

        uno::Reference< sdbc::XResultSet > xResultSet(
                                    xDynamicResultSet->getStaticResultSet() );
        if ( xResultSet.is() )
        {
            if ( bPrint )
            {
                print( "Folder object opened - iterating:" );
                print( UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM(
                    "Content-ID : Title : Size : IsFolder : IsDocument\n"
                    "-------------------------------------------------" ) ) );
            }

            if (nFetchSize > 0)
            {
                bool bSet = false;
                uno::Reference< beans::XPropertySet > xProperties(
                    xResultSet, uno::UNO_QUERY);
                if (xProperties.is())
                    try
                    {
                        xProperties->
                            setPropertyValue(rtl::OUString::createFromAscii(
                                                 "FetchSize"),
                                             uno::makeAny(nFetchSize));
                        bSet = true;
                    }
                    catch (beans::UnknownPropertyException const &) {}
                    catch (beans::PropertyVetoException const &) {}
                    catch (lang::IllegalArgumentException const &) {}
                    catch (lang::WrappedTargetException const &) {}
                if (!bSet)
                    print("Fetch size not set!");
            }

            try
            {
                ULONG n = 0;
                uno::Reference< ucb::XContentAccess > xContentAccess(
                                                xResultSet, uno::UNO_QUERY );
                uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );

                while ( xResultSet->next() )
                {
                    UniString aText;

                    if ( bPrint )
                    {
                        rtl::OUString aId( xContentAccess->
                                          queryContentIdentifierString() );
                        aText += UniString::CreateFromInt32( ++n );
                        aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM(
                                               ") " ) );
                        aText += UniString( aId );
                        aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM(
                                               " : " ) );
                    }

                    // Title:
                    UniString aTitle( xRow->getString( 1 ) );
                    if ( bPrint )
                    {
                        if ( aTitle.Len() == 0 && xRow->wasNull() )
                            aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM(
                                                   "<null>" ) );
                        else
                            aText += aTitle;
                        aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM(
                                               " : " ) );
                    }

                    // Size:
                    sal_Int32 nSize = xRow->getInt( 3 );
                    if ( bPrint )
                    {
                        if ( nSize == 0 && xRow->wasNull() )
                            aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM(
                                                   "<null>" ) );
                        else
                            aText += UniString::CreateFromInt32( nSize );
                        aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM(
                                               " : " ) );
                    }

                    // IsFolder:
                    sal_Bool bFolder = xRow->getBoolean( 4 );
                    if ( bPrint )
                    {
                        if ( !bFolder && xRow->wasNull() )
                            aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM(
                                                   "<null>" ) );
                        else
                            aText
                                += bFolder ?
                                       UniString::CreateFromAscii(
                                           RTL_CONSTASCII_STRINGPARAM(
                                               "true" ) ) :
                                       UniString::CreateFromAscii(
                                           RTL_CONSTASCII_STRINGPARAM(
                                               "false" ) );
                        aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM(
                                               " : " ) );
                    }

                    // IsDocument:
                    sal_Bool bDocument = xRow->getBoolean( 5 );
                    if ( bPrint )
                    {
                        if ( !bFolder && xRow->wasNull() )
                            aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM(
                                "<null>" ) );
                        else
                            aText
                                += bDocument ?
                                       UniString::CreateFromAscii(
                                           RTL_CONSTASCII_STRINGPARAM(
                                               "true" ) ) :
                                       UniString::CreateFromAscii(
                                           RTL_CONSTASCII_STRINGPARAM(
                                               "false" ) ); //  IsDocument
                    }

                    if ( bPrint )
                        print( aText );

                    if ( pStack && bFolder )
                        pStack->push( OpenStackEntry(
#if 1
                                          xContentAccess->
                                              queryContentIdentifier(),
#else
                                          xContentAccess->queryContent(),
#endif
                                          nLevel + 1 ) );
                }
            }
            catch ( ucb::ResultSetException )
            {
                print( "ResultSetException caught!" );
            }

            if ( bPrint )
                print( "Iteration done." );
        }
    }

    uno::Reference< lang::XComponent > xComponent(
        xDynamicResultSet, uno::UNO_QUERY);
    if (xComponent.is())
        xComponent->dispose();

//	putenv("PROT_REMOTE_ACTIVATE="); // to log remote uno traffic

    if ( bTiming )
    {
        nTime = Time::GetSystemTicks() - nTime;
        UniString
            aText( UniString::CreateFromAscii(
                       RTL_CONSTASCII_STRINGPARAM( "Operation took " ) ) );
        aText += UniString::CreateFromInt64( nTime );
        aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " ms." ) );
        print( aText );
    }
}

//----------------------------------------------------------------------------
void UcbContent::openAll( Ucb& rUCB, bool bPrint, bool bTiming, bool bSort,
                          sal_Int32 nFetchSize )
{
    ULONG nTime = 0;
    if ( bTiming )
        nTime = Time::GetSystemTicks();

    OpenStack aStack;
    aStack.push( OpenStackEntry( m_xContent, 0 ) );

    while ( !aStack.empty() )
    {
        OpenStackEntry aEntry( aStack.top() );
        aStack.pop();

        if ( bPrint )
        {
            UniString aText;
            for ( sal_uInt32 i = aEntry.m_nLevel; i != 0; --i )
                aText += '=';
            aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "LEVEL " ) );
            aText += UniString::CreateFromInt64( aEntry.m_nLevel );

            uno::Reference< ucb::XContentIdentifier > xID;
            if ( aEntry.m_bUseIdentifier )
                xID = aEntry.m_xIdentifier;
            else if ( aEntry.m_xContent.is() )
                xID = aEntry.m_xContent->getIdentifier();
            if ( xID.is() )
            {
                aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ": " ) );
                aText += UniString( xID->getContentIdentifier() );
            }

            print( aText );
        }

        uno::Reference< ucb::XContent > xChild;
        if ( aEntry.m_bUseIdentifier )
        {
            uno::Reference< ucb::XContentProvider > xProv
                = rUCB.getContentProvider();
            if ( !xProv.is() )
            {
                print( "No content provider" );
                return;
            }

            try
            {
                xChild = xProv->queryContent( aEntry.m_xIdentifier );
            }
            catch (ucb::IllegalIdentifierException const &) {}
        }
        else
            xChild = aEntry.m_xContent;
        if ( !xChild.is() )
        {
            print( "No content" );
            return;
        }

        UcbContent( m_rUCB, xChild, m_pOutEdit ).
            open( UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM(
                                                  "open" ) ),
                  UniString(), bPrint, false, bSort, &aStack,
                  aEntry.m_nLevel, nFetchSize );
    }

    if ( bTiming )
    {
        nTime = Time::GetSystemTicks() - nTime;
        UniString
            aText( UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM(
                                                   "Operation took " ) ) );
        aText += UniString::CreateFromInt64( nTime );
        aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " ms." ) );
        print( aText );
    }
}

//----------------------------------------------------------------------------
void UcbContent::transfer( const rtl::OUString& rSourceURL, sal_Bool bMove  )
{
    if ( bMove )
        print( "Moving content..." );
    else
        print( "Copying content..." );

#if 1 /* globalTransfer */

    uno::Reference< ucb::XCommandProcessor > xCommandProcessor(
                                m_rUCB.getContentProvider(), uno::UNO_QUERY );
    if ( xCommandProcessor.is() )
    {
        ucb::GlobalTransferCommandArgument aArg(
                            bMove ? ucb::TransferCommandOperation_MOVE
                                  : ucb::TransferCommandOperation_COPY,
                            rSourceURL,
                            getURL(),
                            rtl::OUString(),
                            //rtl::OUString::createFromAscii( "NewTitle" ),
                            ucb::NameClash::ERROR );

        ucb::Command aTransferCommand( rtl::OUString::createFromAscii(
                                                "globalTransfer" ),
                                             -1,
                                             uno::makeAny( aArg ) );

        uno::Reference< task::XInteractionHandler > xInteractionHandler;
        if (m_rUCB.getServiceFactory().is())
            xInteractionHandler
                = uno::Reference< task::XInteractionHandler >(
                          m_rUCB.getServiceFactory()->
                              createInstance(
                                rtl::OUString::createFromAscii(
                                      "com.sun.star.task.InteractionHandler")),
                        uno::UNO_QUERY);
        uno::Reference< ucb::XProgressHandler > xProgressHandler(
            new ProgressHandler(m_rUCB));
        uno::Reference< ucb::XCommandEnvironment > xEnv(
            new UcbTaskEnvironment( xInteractionHandler, xProgressHandler ) );

        try
        {
            xCommandProcessor->execute( aTransferCommand, 0, xEnv );
        }
        catch ( uno::Exception const & )
        {
            print( "globalTransfer threw exception!" );
            return;
        }

        print( "globalTransfer finished successfully" );
    }

#else /* transfer */

    uno::Any aArg;
    aArg <<= ucb::TransferInfo(
            bMove, rSourceURL, rtl::OUString(), ucb::NameClash::ERROR );
    executeCommand( rtl::OUString::createFromAscii( "transfer" ), aArg );

//  executeCommand( rtl::OUString::createFromAscii( "flush" ), Any() );

#endif
}

//----------------------------------------------------------------------------
void UcbContent::destroy()
{
    print( "Deleting content..." );

    uno::Any aArg;
    aArg <<= sal_Bool( sal_True ); // delete physically, not only to trash.
    executeCommand( rtl::OUString::createFromAscii( "delete" ), aArg );

//  executeCommand( rtl::OUString::createFromAscii( "flush" ), Any() );
}

//-------------------------------------------------------------------------
uno::Sequence< ucb::CommandInfo > UcbContent::getCommands()
{
    uno::Any aResult = executeCommand(
            rtl::OUString::createFromAscii( "getCommandInfo" ), uno::Any() );

    uno::Reference< ucb::XCommandInfo > xInfo;
    if ( aResult >>= xInfo )
    {
        uno::Sequence< ucb::CommandInfo > aCommands(
            xInfo->getCommands() );
        const ucb::CommandInfo* pCommands = aCommands.getConstArray();

        String aText( UniString::CreateFromAscii(
                        RTL_CONSTASCII_STRINGPARAM( "Commands:\n" ) ) );
        sal_uInt32 nCount = aCommands.getLength();
        for ( sal_uInt32 n = 0; n < nCount; ++n )
        {
            aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "    " ) );
            aText += String( pCommands[ n ].Name );
            aText += '\n';
        }
        print( aText );

        return aCommands;
    }

    print( "getCommands failed!" );
    return uno::Sequence< ucb::CommandInfo >();
}

//-------------------------------------------------------------------------
uno::Sequence< beans::Property > UcbContent::getProperties()
{
    uno::Any aResult = executeCommand(
        rtl::OUString::createFromAscii( "getPropertySetInfo" ), uno::Any() );

    uno::Reference< beans::XPropertySetInfo > xInfo;
    if ( aResult >>= xInfo )
    {
        uno::Sequence< beans::Property > aProps( xInfo->getProperties() );
        const beans::Property* pProps = aProps.getConstArray();

        String aText( UniString::CreateFromAscii(
                        RTL_CONSTASCII_STRINGPARAM( "Properties:\n" ) ) );
        sal_uInt32 nCount = aProps.getLength();
        for ( sal_uInt32 n = 0; n < nCount; ++n )
        {
            aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "    " ) );
            aText += UniString( pProps[ n ].Name );
            aText += '\n';
        }
        print( aText );

        return aProps;
    }

    print( "getProperties failed!" );
    return uno::Sequence< beans::Property >();
}

//----------------------------------------------------------------------------
uno::Any UcbContent::getPropertyValue( const rtl::OUString& rName )
{
    uno::Sequence< beans::Property > aProps( 1 );
    beans::Property& rProp = aProps.getArray()[ 0 ];

    rProp.Name	     = rName;
    rProp.Handle     = -1; /* unknown */
//	rProp.Type       = ;
//	rProp.Attributes = ;

    uno::Any aArg;
    aArg <<= aProps;

    uno::Any aResult = executeCommand(
        rtl::OUString::createFromAscii( "getPropertyValues" ), aArg );

    uno::Reference< sdbc::XRow > xValues;
    if ( aResult >>= xValues )
        return xValues->getObject(
            1, uno::Reference< container::XNameAccess>() );

    print( "getPropertyValue failed!" );
    return uno::Any();
}

//----------------------------------------------------------------------------
rtl::OUString UcbContent::getStringPropertyValue( const rtl::OUString& rName )
{
    uno::Any aAny = getPropertyValue( rName );
    if ( aAny.getValueType() == getCppuType( (const ::rtl::OUString *)0 ) )
    {
        const rtl::OUString aValue(
            * static_cast< const rtl::OUString * >( aAny.getValue() ) );

        UniString aText( rName );
        aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " value: '" ) );
        aText += UniString( aValue );
        aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "'" ) );
        print( aText );

        return aValue;
    }

    print( "getStringPropertyValue failed!" );
    return rtl::OUString();
}

//----------------------------------------------------------------------------
void UcbContent::setPropertyValue( const rtl::OUString& rName,
                                   const uno::Any& rValue )
{
    uno::Sequence< beans::PropertyValue > aProps( 1 );
    beans::PropertyValue& rProp = aProps.getArray()[ 0 ];

    rProp.Name	     = rName;
    rProp.Handle     = -1; /* unknown */
    rProp.Value	     = rValue;
//	rProp.State      = ;

    uno::Any aArg;
    aArg <<= aProps;

    executeCommand( rtl::OUString::createFromAscii( "setPropertyValues" ),
                    aArg );

//  executeCommand( rtl::OUString::createFromAscii( "flush" ), Any() );
}

//----------------------------------------------------------------------------
void UcbContent::setStringPropertyValue( const rtl::OUString& rName,
                                         const rtl::OUString& rValue )
{
    uno::Any aAny;
    aAny <<= rValue;
    setPropertyValue( rName, aAny );

    UniString aText( rName );
    aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " value set to: '" ) );
    aText += UniString( rValue );
    aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "'" ) );
    print( aText );
}

//----------------------------------------------------------------------------
void UcbContent::addProperty( const rtl::OUString& rName,
                              const uno::Any& rValue )
{
    uno::Reference< beans::XPropertyContainer > xContainer( m_xContent,
                                                            uno::UNO_QUERY );
    if ( xContainer.is() )
    {
        UniString aText( UniString::CreateFromAscii(
                            RTL_CONSTASCII_STRINGPARAM(
                                "Adding property: " ) ) );
        aText += UniString( rName );
        print( aText );

        try
        {
            xContainer->addProperty( rName, 0, rValue );
        }
        catch ( beans::PropertyExistException const & )
        {
            print( "Adding property failed. Already exists!" );
            return;
        }
        catch ( beans::IllegalTypeException const & )
        {
            print( "Adding property failed. Illegal Type!" );
            return;
        }
        catch ( lang::IllegalArgumentException const & )
        {
            print( "Adding property failed. Illegal Argument!" );
            return;
        }

        print( "Adding property succeeded." );
        return;
    }

    print( "Adding property failed. No XPropertyContainer!" );
}

//----------------------------------------------------------------------------
void UcbContent::addStringProperty(
                    const rtl::OUString& rName, const rtl::OUString& rValue )
{
    uno::Any aValue;
    aValue <<= rValue;
    addProperty( rName, aValue );
}

//----------------------------------------------------------------------------
void UcbContent::removeProperty( const rtl::OUString& rName )
{
    uno::Reference< beans::XPropertyContainer > xContainer( m_xContent,
                                                            uno::UNO_QUERY );
    if ( xContainer.is() )
    {
        UniString aText( UniString::CreateFromAscii(
                            RTL_CONSTASCII_STRINGPARAM(
                                "Removing property: " ) ) );
        aText += UniString( rName );
        print( aText );

        try
        {
            xContainer->removeProperty( rName );
        }
        catch ( beans::UnknownPropertyException const & )
        {
            print( "Adding property failed. Unknown!" );
            return;
        }

        print( "Removing property succeeded." );
        return;
    }

    print( "Removing property failed. No XPropertyContainer!" );
}

//----------------------------------------------------------------------------
//
// XInterface methods
//
//----------------------------------------------------------------------------

// virtual
uno::Any SAL_CALL UcbContent::queryInterface( const uno::Type & rType )
    throw(uno::RuntimeException)
{
    uno::Any aRet = cppu::queryInterface(
                rType,
                static_cast< lang::XEventListener* >(
                    static_cast< ucb::XContentEventListener* >( this ) ),
                static_cast< ucb::XContentEventListener* >( this ),
                static_cast< beans::XPropertiesChangeListener* >( this ) );
    return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
}

//----------------------------------------------------------------------------
// virtual
void SAL_CALL UcbContent::acquire()
    throw()
{
    OWeakObject::acquire();
}

//----------------------------------------------------------------------------
// virtual
void SAL_CALL UcbContent::release()
    throw()
{
    OWeakObject::release();
}

//----------------------------------------------------------------------------
//
// XEventListener methods.
//
//----------------------------------------------------------------------------

// virtual
void SAL_CALL UcbContent::disposing( const lang::EventObject& /*Source*/ )
    throw( uno::RuntimeException )
{
    print ( "Content: disposing..." );
}

//----------------------------------------------------------------------------
//
// XContentEventListener methods,
//
//----------------------------------------------------------------------------

// virtual
void SAL_CALL UcbContent::contentEvent( const ucb::ContentEvent& evt )
    throw( uno::RuntimeException )
{
    switch ( evt.Action )
    {
        case ucb::ContentAction::INSERTED:
        {
            UniString aText( UniString::CreateFromAscii(
                                RTL_CONSTASCII_STRINGPARAM(
                                    "contentEvent: INSERTED: " ) ) );
            if ( evt.Content.is() )
            {
                uno::Reference< ucb::XContentIdentifier > xId(
                                           evt.Content->getIdentifier() );
                aText += UniString( xId->getContentIdentifier() );
                aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " - " ) );
                aText += UniString( evt.Content->getContentType() );
            }

            print( aText );
            break;
        }
        case ucb::ContentAction::REMOVED:
            print( "contentEvent: REMOVED" );
            break;

        case ucb::ContentAction::DELETED:
            print( "contentEvent: DELETED" );
            break;

        case ucb::ContentAction::EXCHANGED:
            print( "contentEvent: EXCHANGED" );
            break;

        case ucb::ContentAction::SEARCH_MATCHED:
        {
            String aMatch(RTL_CONSTASCII_USTRINGPARAM(
                              "contentEvent: SEARCH MATCHED "));
            if (evt.Id.is())
            {
                aMatch += String(evt.Id->getContentIdentifier());
                if (evt.Content.is())
                {
                    aMatch.AppendAscii(RTL_CONSTASCII_STRINGPARAM(" - "));
                    aMatch += String(evt.Content->getContentType());
                }
            }
            else
                aMatch.AppendAscii(RTL_CONSTASCII_STRINGPARAM("<no id>"));
            print(aMatch);
            break;
        }

        default:
            print( "contentEvent..." );
            break;
    }
}

//----------------------------------------------------------------------------
//
// XPropertiesChangeListener methods.
//
//----------------------------------------------------------------------------

// virtual
void SAL_CALL UcbContent::propertiesChange(
                    const uno::Sequence< beans::PropertyChangeEvent >& evt )
    throw( uno::RuntimeException )
{
    print( "propertiesChange..." );

    sal_uInt32 nCount = evt.getLength();
    if ( nCount )
    {
        const beans::PropertyChangeEvent* pEvents = evt.getConstArray();
        for ( sal_uInt32 n = 0; n < nCount; ++n )
        {
            UniString aText( UniString::CreateFromAscii(
                                RTL_CONSTASCII_STRINGPARAM( "    " ) ) );
            aText += UniString( pEvents[ n ].PropertyName );
            print( aText );
        }
    }
}

/*========================================================================
 *
 * MyWin.
 *
 *=======================================================================*/

#define MYWIN_ITEMID_CLEAR    		1
#define MYWIN_ITEMID_CREATE   		2
#define MYWIN_ITEMID_RELEASE   		3
#define MYWIN_ITEMID_COMMANDS 		4
#define MYWIN_ITEMID_PROPS 			5
#define MYWIN_ITEMID_ADD_PROP		6
#define MYWIN_ITEMID_REMOVE_PROP	7
#define MYWIN_ITEMID_GET_PROP		8
#define MYWIN_ITEMID_SET_PROP		9
#define MYWIN_ITEMID_OPEN			10
#define MYWIN_ITEMID_OPEN_ALL		11
#define MYWIN_ITEMID_UPDATE		   	12
#define MYWIN_ITEMID_SYNCHRONIZE 	13
#define MYWIN_ITEMID_COPY 			14
#define MYWIN_ITEMID_MOVE 			15
#define MYWIN_ITEMID_DELETE 		16
#define MYWIN_ITEMID_SEARCH			17
#define MYWIN_ITEMID_TIMING			18
#define MYWIN_ITEMID_SORT			19
#define MYWIN_ITEMID_FETCHSIZE		20
#define MYWIN_ITEMID_SYS2URI		21
#define MYWIN_ITEMID_URI2SYS		22
#define MYWIN_ITEMID_OFFLINE		23
#define MYWIN_ITEMID_ONLINE			24
#define MYWIN_ITEMID_REORGANIZE		25

//-------------------------------------------------------------------------
class MyWin : public WorkWindow
{
private:
    ToolBox*			m_pTool;
    Edit*				m_pCmdEdit;
    MyOutWindow*		m_pOutEdit;

    Ucb 		m_aUCB;
    UcbContent* m_pContent;

    sal_Int32 m_nFetchSize;
    bool m_bTiming;
    bool m_bSort;

public:
    MyWin( Window *pParent, WinBits nWinStyle,
           uno::Reference< lang::XMultiServiceFactory >& rxFactory,
           rtl::OUString const & rConfigurationKey1,
           rtl::OUString const & rConfigurationKey2 );
    virtual ~MyWin();

    void Resize( void );
    DECL_LINK ( ToolBarHandler, ToolBox* );

    void print( const UniString& rText );
    void print( const sal_Char* pText );
};

//-------------------------------------------------------------------------
MyWin::MyWin( Window *pParent, WinBits nWinStyle,
              uno::Reference< lang::XMultiServiceFactory >& rxFactory,
              rtl::OUString const & rConfigurationKey1,
              rtl::OUString const & rConfigurationKey2 )
: WorkWindow( pParent, nWinStyle ),
  m_pTool( NULL ),
  m_pOutEdit( NULL ),
  m_aUCB( rxFactory, rConfigurationKey1, rConfigurationKey2 ),
  m_pContent( NULL ),
  m_nFetchSize( 0 ),
  m_bTiming( false ),
  m_bSort( false )
{
    // ToolBox.
    m_pTool = new ToolBox( this, WB_3DLOOK | WB_BORDER  | WB_SCROLL );

    m_pTool->InsertItem ( MYWIN_ITEMID_CLEAR,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                  "Clear" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_CLEAR,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                  "Clear the Output Window" ) ) );
    m_pTool->InsertSeparator();
    m_pTool->InsertItem ( MYWIN_ITEMID_CREATE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Create" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_CREATE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Create a content" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_RELEASE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Release" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_RELEASE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Release current content" ) ) );
    m_pTool->InsertSeparator();
    m_pTool->InsertItem ( MYWIN_ITEMID_COMMANDS,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Commands" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_COMMANDS,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                  "Get Commands supported by the content" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_PROPS,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Properties" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_PROPS,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Get Properties supported by the content" ) ) );
    m_pTool->InsertSeparator();
    m_pTool->InsertItem ( MYWIN_ITEMID_ADD_PROP,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "addProperty" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_ADD_PROP,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                  "Add a new string(!) property to the content. "
                                  "Type the property name in the entry field and "
                                  "push this button. The default value for the "
                                  "property will be set to the string 'DefaultValue'" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_REMOVE_PROP,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "removeProperty" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_REMOVE_PROP,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                  "Removes a property from the content. "
                                  "Type the property name in the entry field and "
                                  "push this button." ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_GET_PROP,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "getPropertyValue" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_GET_PROP,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                  "Get a string(!) property value from the content. "
                                  "Type the property name in the entry field and "
                                  "push this button to obtain the value" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_SET_PROP,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "setPropertyValue" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_SET_PROP,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                  "Set a string(!) property value of the content."
                                  "Type the property name in the entry field and "
                                  "push this button to set the value to the string "
                                  "'NewValue'" ) ) );
    m_pTool->InsertSeparator();
    m_pTool->InsertItem ( MYWIN_ITEMID_OPEN,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Open" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_OPEN,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Open the content" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_OPEN_ALL,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Open All" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_OPEN_ALL,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Open the content and all of its"
                                    " children" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_UPDATE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Update" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_UPDATE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Update the content" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_SYNCHRONIZE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Synchronize" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_SYNCHRONIZE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Synchronize the content" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_SEARCH,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Search" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_SEARCH,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Search the content" ) ) );

    m_pTool->InsertItem ( MYWIN_ITEMID_REORGANIZE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Reorganize" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_REORGANIZE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Reorganize the content storage" ) ) );

    m_pTool->InsertSeparator();
    m_pTool->InsertItem ( MYWIN_ITEMID_COPY,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Copy" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_COPY,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Copy a content. Type the URL of the source "
                                "content into the entry field." ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_MOVE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Move" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_MOVE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Move a content. Type the URL of the source "
                                "content into the entry field." ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_DELETE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Delete" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_DELETE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Delete the content." ) ) );

    m_pTool->InsertSeparator();
    m_pTool->InsertItem ( MYWIN_ITEMID_TIMING,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Timing" ) ),
                          TIB_CHECKABLE | TIB_AUTOCHECK );
    m_pTool->SetHelpText( MYWIN_ITEMID_TIMING,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Display execution times instead of"
                                    " output" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_SORT,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Sort" ) ),
                          TIB_CHECKABLE | TIB_AUTOCHECK );
    m_pTool->SetHelpText( MYWIN_ITEMID_SORT,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Sort result sets" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_FETCHSIZE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Fetch Size" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_FETCHSIZE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Set cached cursor fetch size to positive value" ) ) );

    m_pTool->InsertSeparator();
    m_pTool->InsertItem ( MYWIN_ITEMID_SYS2URI,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "UNC>URI" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_SYS2URI,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Translate 'System File Path' to URI,"
                                    " if possible" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_URI2SYS,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "URI>UNC" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_URI2SYS,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Translate URI to 'System File Path',"
                                    " if possible" ) ) );

    m_pTool->InsertSeparator();
    m_pTool->InsertItem ( MYWIN_ITEMID_OFFLINE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Offline" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_OFFLINE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Go offline" ) ) );
    m_pTool->InsertItem ( MYWIN_ITEMID_ONLINE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Online" ) ) );
    m_pTool->SetHelpText( MYWIN_ITEMID_ONLINE,
                          UniString::CreateFromAscii(
                              RTL_CONSTASCII_STRINGPARAM(
                                "Go back online" ) ) );

    m_pTool->SetSelectHdl( LINK( this, MyWin, ToolBarHandler ) );
    m_pTool->Show();

    // Edit.
    m_pCmdEdit = new Edit( this );
    m_pCmdEdit->SetReadOnly( FALSE );
    m_pCmdEdit->SetText( UniString::CreateFromAscii(
                            RTL_CONSTASCII_STRINGPARAM( "file:///" ) ) );
    m_pCmdEdit->Show();

    // MyOutWindow.
    m_pOutEdit = new MyOutWindow( this, WB_HSCROLL | WB_VSCROLL | WB_BORDER );
    m_pOutEdit->SetReadOnly( TRUE );
    m_pOutEdit->Show();

    m_aUCB.setOutEdit( m_pOutEdit );
}

//-------------------------------------------------------------------------
// virtual
MyWin::~MyWin()
{
    if ( m_pContent )
    {
        m_pContent->dispose();
        m_pContent->release();
    }

    delete m_pTool;
    delete m_pCmdEdit;
    delete m_pOutEdit;
}

//-------------------------------------------------------------------------
void MyWin::Resize()
{
    Size aWinSize = GetOutputSizePixel();
    int nWinW = aWinSize.Width();
    int nWinH = aWinSize.Height();
    int nBoxH = m_pTool->CalcWindowSizePixel().Height();

    m_pTool->SetPosSizePixel   (
        Point( 0, 0 ), Size ( nWinW, nBoxH ) );
    m_pCmdEdit->SetPosSizePixel(
        Point( 0, nBoxH ), Size( nWinW, nBoxH ) );
    m_pOutEdit->SetPosSizePixel(
        Point( 0, nBoxH + nBoxH ), Size ( nWinW, nWinH - ( nBoxH + nBoxH ) ) );
}

//-------------------------------------------------------------------------
void MyWin::print( const sal_Char* pText )
{
    print( UniString::CreateFromAscii( pText ) );
}

//-------------------------------------------------------------------------
void MyWin::print( const UniString& rText )
{
    SolarMutexGuard aGuard;

    if ( m_pOutEdit )
    {
        m_pOutEdit->Append( rText );
        m_pOutEdit->Update();
    }
}

//-------------------------------------------------------------------------
IMPL_LINK( MyWin, ToolBarHandler, ToolBox*, pToolBox )
{
    USHORT nItemId   = pToolBox->GetCurItemId();
    UniString aCmdLine = m_pCmdEdit->GetText();

    ULONG n = Application::ReleaseSolarMutex();

    switch( nItemId )
    {
        case MYWIN_ITEMID_CLEAR:
        {
            SolarMutexGuard aGuard;

            m_pOutEdit->Clear();
            m_pOutEdit->Show();
            break;
        }

        case MYWIN_ITEMID_CREATE:
            if ( m_pContent )
            {
                UniString aText( UniString::CreateFromAscii(
                                    RTL_CONSTASCII_STRINGPARAM(
                                        "Content released: " ) ) );
                aText += m_pContent->getURL();

                m_pContent->dispose();
                m_pContent->release();
                m_pContent = NULL;

                print( aText );
            }

            m_pContent = UcbContent::create( m_aUCB, aCmdLine, m_pOutEdit );
            if ( m_pContent )
            {
                String aText( UniString::CreateFromAscii(
                                RTL_CONSTASCII_STRINGPARAM(
                                    "Created content: " ) ) );
                aText += String( m_pContent->getURL() );
                aText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " - " ) );
                aText += String( m_pContent->getType() );
                print( aText );
            }
            else
            {
                String aText( UniString::CreateFromAscii(
                                RTL_CONSTASCII_STRINGPARAM(
                                    "Creation failed for content: " ) ) );
                aText += String( aCmdLine );
                print( aText );
            }
            break;

        case MYWIN_ITEMID_RELEASE:
            if ( m_pContent )
            {
                UniString aText( UniString::CreateFromAscii(
                                    RTL_CONSTASCII_STRINGPARAM(
                                        "Content released: " ) ) );
                aText += m_pContent->getURL();

                m_pContent->dispose();
                m_pContent->release();
                m_pContent = NULL;

                print( aText );
            }
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_COMMANDS:
            if ( m_pContent )
                m_pContent->getCommands();
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_PROPS:
            if ( m_pContent )
                m_pContent->getProperties();
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_ADD_PROP:
            if ( m_pContent )
                m_pContent->addStringProperty(
                        aCmdLine,
                        rtl::OUString::createFromAscii( "DefaultValue" ) );
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_REMOVE_PROP:
            if ( m_pContent )
                m_pContent->removeProperty(	aCmdLine );
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_GET_PROP:
            if ( m_pContent )
                m_pContent->getStringPropertyValue( aCmdLine );
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_SET_PROP:
            if ( m_pContent )
                m_pContent->setStringPropertyValue(
                                aCmdLine,
                                rtl::OUString::createFromAscii( "NewValue" ) );
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_OPEN:
            if ( m_pContent )
                m_pContent->open(rtl::OUString::createFromAscii("open"),
                                 aCmdLine, !m_bTiming, m_bTiming, m_bSort, 0,
                                 0, m_nFetchSize);
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_OPEN_ALL:
            if ( m_pContent )
                m_pContent->openAll(m_aUCB, !m_bTiming, m_bTiming, m_bSort,
                                    m_nFetchSize);
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_UPDATE:
            if ( m_pContent )
                m_pContent->open(rtl::OUString::createFromAscii("update"),
                                 aCmdLine, !m_bTiming, m_bTiming, m_bSort, 0,
                                 0, m_nFetchSize);
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_SYNCHRONIZE:
            if ( m_pContent )
                m_pContent->open(rtl::OUString::createFromAscii("synchronize"),
                                 aCmdLine, !m_bTiming, m_bTiming, m_bSort, 0,
                                 0, m_nFetchSize);
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_SEARCH:
            if ( m_pContent )
                m_pContent->open(rtl::OUString::createFromAscii("search"),
                                 aCmdLine, !m_bTiming, m_bTiming, m_bSort, 0,
                                 0, m_nFetchSize);
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_REORGANIZE:
            if ( m_pContent )
                m_pContent->executeCommand (
                    rtl::OUString::createFromAscii ("reorganizeData"),
                    uno::Any());
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_COPY:
            if ( m_pContent )
                m_pContent->transfer( aCmdLine, sal_False );
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_MOVE:
            if ( m_pContent )
                m_pContent->transfer( aCmdLine, sal_True );
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_DELETE:
            if ( m_pContent )
                m_pContent->destroy();
            else
                print( "No content!" );

            break;

        case MYWIN_ITEMID_TIMING:
            m_bTiming = m_pTool->IsItemChecked(MYWIN_ITEMID_TIMING) != false;
            break;

        case MYWIN_ITEMID_SORT:
            m_bSort = m_pTool->IsItemChecked(MYWIN_ITEMID_SORT) != false;
            break;

        case MYWIN_ITEMID_FETCHSIZE:
        {
            m_nFetchSize = aCmdLine.ToInt32();
            String aText;
            if (m_nFetchSize > 0)
            {
                aText.AssignAscii("Fetch size set to ");
                aText += String::CreateFromInt32(m_nFetchSize);
            }
            else
                aText.AssignAscii("Fetch size reset to default");
            print(aText);
            break;
        }

        case MYWIN_ITEMID_SYS2URI:
        {
            uno::Reference< ucb::XContentProviderManager >
                xManager(m_aUCB.getContentProvider(), uno::UNO_QUERY);
            DBG_ASSERT(xManager.is(),
                       "MyWin::ToolBarHandler(): Service lacks interface");

            rtl::OUString aURL(getLocalFileURL(xManager));

            String aText(RTL_CONSTASCII_USTRINGPARAM("Local file URL: "));
            aText += String(aURL);
            aText.AppendAscii("\nConversion: ");
            aText += aCmdLine;
            aText.AppendAscii(" to ");
            aText += String(getFileURLFromSystemPath(xManager,
                                                          aURL,
                                                          aCmdLine));
            print(aText);
            break;
        }

        case MYWIN_ITEMID_URI2SYS:
        {
            uno::Reference< ucb::XContentProviderManager >
                xManager(m_aUCB.getContentProvider(), uno::UNO_QUERY);
            DBG_ASSERT(xManager.is(),
                       "MyWin::ToolBarHandler(): Service lacks interface");

            String aText(RTL_CONSTASCII_USTRINGPARAM("Conversion: "));
            aText += aCmdLine;
            aText.AppendAscii(" to ");
            aText += String(getSystemPathFromFileURL(xManager,
                                                          aCmdLine));
            print(aText);
            break;
        }

        case MYWIN_ITEMID_OFFLINE:
        case MYWIN_ITEMID_ONLINE:
        {
            uno::Reference< ucb::XContentProviderManager >
                xManager(m_aUCB.getContentProvider(), uno::UNO_QUERY);
            uno::Reference< ucb::XCommandProcessor > xProcessor;
            if (xManager.is())
                xProcessor
                    = uno::Reference< ucb::XCommandProcessor >(
                        xManager->queryContentProvider(aCmdLine),
                        uno::UNO_QUERY);
            if (!xProcessor.is())
            {
                String aText(RTL_CONSTASCII_USTRINGPARAM(
                                 "No offline support for URL "));
                aText += aCmdLine;
                print(aText);
                break;
            }

            rtl::OUString aName;
            uno::Any aArgument;
            if (nItemId == MYWIN_ITEMID_OFFLINE)
            {
                aName = rtl::OUString::createFromAscii("goOffline");

                uno::Sequence<
                    uno::Reference< ucb::XContentIdentifier > >
                        aIdentifiers(1);
                aIdentifiers[0]
                    = m_aUCB.getContentIdentifierFactory()->
                                 createContentIdentifier(aCmdLine);
                aArgument <<= aIdentifiers;
            }
            else
                aName = rtl::OUString::createFromAscii("goOnline");

            UcbCommandProcessor(m_aUCB, xProcessor, m_pOutEdit).
                executeCommand(aName, aArgument);
            break;
        }

        default: // Ignored.
            break;
    }

    Application::AcquireSolarMutex( n );
    return 0;
}

/*========================================================================
 *
 * MyApp.
 *
 *=======================================================================*/
class MyApp : public Application
{
public:
    virtual void Main();
};

MyApp aMyApp;

//-------------------------------------------------------------------------
// virtual
void MyApp::Main()
{
    //////////////////////////////////////////////////////////////////////
    // Read command line params.
    //////////////////////////////////////////////////////////////////////

    rtl::OUString aConfigurationKey1(rtl::OUString::createFromAscii(
                                         UCB_CONFIGURATION_KEY1_LOCAL));
    rtl::OUString aConfigurationKey2(rtl::OUString::createFromAscii(
                                         UCB_CONFIGURATION_KEY2_OFFICE));

    USHORT nParams = Application::GetCommandLineParamCount();
    for ( USHORT n = 0; n < nParams; ++n )
    {
        String aParam( Application::GetCommandLineParam( n ) );
        if (aParam.CompareIgnoreCaseToAscii("-key=",
                                            RTL_CONSTASCII_LENGTH("-key="))
                == COMPARE_EQUAL)
        {
            xub_StrLen nSlash
                = aParam.Search('/', RTL_CONSTASCII_LENGTH("-key="));
            if (nSlash == STRING_NOTFOUND)
            {
                aConfigurationKey1
                    = aParam.Copy(RTL_CONSTASCII_LENGTH("-key="));
                aConfigurationKey2 = rtl::OUString();
            }
            else
            {
                aConfigurationKey1
                    = aParam.Copy(RTL_CONSTASCII_LENGTH("-key="),
                                  nSlash - RTL_CONSTASCII_LENGTH("-key="));
                aConfigurationKey2
                    = aParam.Copy(nSlash + 1);
            }
        }
    }

    //////////////////////////////////////////////////////////////////////
    // Initialize local Service Manager and basic services.
    //////////////////////////////////////////////////////////////////////

    uno::Reference< lang::XMultiServiceFactory > xFac;
    try
    {
        uno::Reference< uno::XComponentContext > xCtx(
            cppu::defaultBootstrap_InitialComponentContext() );
        if ( !xCtx.is() )
        {
            DBG_ERROR( "Error creating initial component context!" );
            return;
        }

        xFac = uno::Reference< lang::XMultiServiceFactory >(
            xCtx->getServiceManager(), uno::UNO_QUERY );

        if ( !xFac.is() )
        {
            DBG_ERROR( "No service manager!" );
            return;
        }
    }
    catch ( uno::Exception )
    {
        DBG_ERROR( "Exception during creation of initial component context!" );
        return;
    }

    comphelper::setProcessServiceFactory( xFac );

    uno::Reference< lang::XComponent > xComponent( xFac, uno::UNO_QUERY );

    //////////////////////////////////////////////////////////////////////
    // Create Application Window...
    //////////////////////////////////////////////////////////////////////

    Help::EnableBalloonHelp();

    MyWin *pMyWin = new MyWin( NULL, WB_APP | WB_STDWORK, xFac,
                               aConfigurationKey1, aConfigurationKey2 );

    pMyWin->
        SetText(
            UniString::CreateFromAscii(
                RTL_CONSTASCII_STRINGPARAM( "UCB Demo/Test Application" ) ) );

    pMyWin->SetPosSizePixel( 0, 0, 1024, 768 );

    pMyWin->Show();

    //////////////////////////////////////////////////////////////////////
    // Go...
    //////////////////////////////////////////////////////////////////////

    Execute();

    //////////////////////////////////////////////////////////////////////
    // Destroy Application Window...
    //////////////////////////////////////////////////////////////////////

    delete pMyWin;

    //////////////////////////////////////////////////////////////////////
    // Cleanup.
    //////////////////////////////////////////////////////////////////////

    ::ucbhelper::ContentBroker::deinitialize();

    // Dispose local service manager.
    if ( xComponent.is() )
        xComponent->dispose();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */