summaryrefslogtreecommitdiff
path: root/ucb/source/ucp/gvfs/content.cxx
blob: 743043c09fc7c97a4356bec764f9945925be6fa7 (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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: content.cxx,v $
 * $Revision: 1.12 $
 *
 * 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 <string.h>
#include <unistd.h>
#include <sys/types.h>

#include "osl/time.h"
#include <osl/diagnose.h>

#include "osl/doublecheckedlocking.h"

#ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUES_HPP_
#include <com/sun/star/beans/PropertyValue.hpp>
#endif
#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <com/sun/star/beans/PropertySetInfoChange.hpp>
#include <com/sun/star/beans/PropertySetInfoChangeEvent.hpp>
#include <com/sun/star/io/XActiveDataSink.hpp>
#include <com/sun/star/io/XOutputStream.hpp>
#include <com/sun/star/lang/IllegalAccessException.hpp>
#include <com/sun/star/ucb/ContentInfoAttribute.hpp>
#include <com/sun/star/ucb/InsertCommandArgument.hpp>
#ifndef _COM_SUN_STAR_UCB_INTERACTIVEBADTRANSFRERURLEXCEPTION_HPP_
#include <com/sun/star/ucb/InteractiveBadTransferURLException.hpp>
#endif
#include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
#include <com/sun/star/ucb/InteractiveNetworkConnectException.hpp>
#ifndef _COM_SUN_STAR_UCB_INTERACTIVENETWORKGENBERALEXCEPTION_HPP_
#include <com/sun/star/ucb/InteractiveNetworkGeneralException.hpp>
#endif
#include <com/sun/star/ucb/InteractiveNetworkReadException.hpp>
#include <com/sun/star/ucb/InteractiveNetworkResolveNameException.hpp>
#include <com/sun/star/ucb/InteractiveNetworkWriteException.hpp>
#include <com/sun/star/ucb/NameClash.hpp>
#include <com/sun/star/ucb/NameClashException.hpp>
#include <com/sun/star/ucb/OpenCommandArgument2.hpp>
#include <com/sun/star/ucb/OpenMode.hpp>
#include <com/sun/star/ucb/PostCommandArgument2.hpp>
#include <com/sun/star/ucb/TransferInfo.hpp>
#include <com/sun/star/ucb/XCommandInfo.hpp>
#include <com/sun/star/ucb/XPersistentPropertySet.hpp>
#include <com/sun/star/ucb/MissingInputStreamException.hpp>
#include <com/sun/star/ucb/MissingPropertiesException.hpp>
#include <com/sun/star/ucb/UnsupportedCommandException.hpp>
#include <com/sun/star/ucb/UnsupportedDataSinkException.hpp>
#include <com/sun/star/ucb/UnsupportedNameClashException.hpp>
#include <com/sun/star/ucb/UnsupportedOpenModeException.hpp>
#include <com/sun/star/ucb/UnsupportedOpenModeException.hpp>
#include <com/sun/star/ucb/NameClashException.hpp>
#include <ucbhelper/contentidentifier.hxx>
#include <ucbhelper/propertyvalueset.hxx>
#ifndef _UCBHELPER_INTERACTIONREQUEST_HXX
#include <ucbhelper/interactionrequest.hxx>
#endif
#include <ucbhelper/cancelcommandexecution.hxx>
#include <ucbhelper/simpleauthenticationrequest.hxx>

const int TRANSFER_BUFFER_SIZE = 65536;

/*
 * NB. Name escaping is done only for URIs
 * the 'Title' property is unescaped on set/get
 */
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libgnomevfs/gnome-vfs-result.h>
#include <libgnomevfs/gnome-vfs-standard-callbacks.h>
extern "C" { // missing in the header: doh.
#  include <libgnomevfs/gnome-vfs-module-callback.h>
}

#include "content.hxx"
#include "provider.hxx"
#include "directory.hxx"
#include "stream.hxx"

using namespace gvfs;
using namespace com::sun::star;

#define CLEAR_INFO(info) memset((info), 0, sizeof ((info)[0]))


static char *
OUStringToGnome( const rtl::OUString &str )
{
    rtl::OString aTempStr = rtl::OUStringToOString( str, RTL_TEXTENCODING_UTF8 );
    return g_strdup( (const sal_Char *) aTempStr );
}

static rtl::OUString
GnomeToOUString( const char *utf8_str)
{
    if (!utf8_str)
        return rtl::OUString();
    else
        return rtl::OUString( utf8_str, strlen( utf8_str ), RTL_TEXTENCODING_UTF8 );
}


Content::Content(
          const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
          ContentProvider* pProvider,
          const uno::Reference< ucb::XContentIdentifier >& Identifier)
  throw ( ucb::ContentCreationException )
    : ContentImplHelper( rxSMgr, pProvider, Identifier ),
      m_pProvider( pProvider ),
      m_bTransient( sal_False )
{
    CLEAR_INFO (&m_info);
#ifdef DEBUG
    g_warning ("New Content ('%s')", getURI());
#endif
}

Content::Content(
    const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
    ContentProvider                                   * pProvider,
    const uno::Reference< ucb::XContentIdentifier >&    Identifier,
    sal_Bool                                            IsFolder)
        throw ( ucb::ContentCreationException )
    : ContentImplHelper( rxSMgr, pProvider, Identifier ),
      m_pProvider( pProvider ),
      m_bTransient( sal_True )
{
    CLEAR_INFO (&m_info);

#ifdef DEBUG
    g_warning ("New Transient content ('%s') (%d)", getURI(), IsFolder);
#endif
//  m_info.name = FIXME: set name ?
    m_info.valid_fields = GNOME_VFS_FILE_INFO_FIELDS_TYPE;
    m_info.type = IsFolder ? GNOME_VFS_FILE_TYPE_DIRECTORY :
                         GNOME_VFS_FILE_TYPE_REGULAR;
}

// virtual
Content::~Content()
{
    gnome_vfs_file_info_clear( &m_info );
}

//
// XInterface methods.
//

void SAL_CALL Content::acquire()
    throw( )
{
    ContentImplHelper::acquire();
}
void SAL_CALL Content::release()
    throw( )
{
    ContentImplHelper::release();
}
uno::Any SAL_CALL Content::queryInterface( const uno::Type & rType )
    throw ( uno::RuntimeException )
{
    // Note: isFolder may require network activities! So call it only
    //       if it is really necessary!!!
    uno::Any aRet = cppu::queryInterface( rType,
                        static_cast< ucb::XContentCreator * >( this ) );
    if ( aRet.hasValue() )
            return isFolder( uno::Reference< ucb::XCommandEnvironment >() )
            ? aRet : uno::Any();
    else
        return aRet.hasValue() ? aRet : ContentImplHelper::queryInterface( rType );
}

//
// XTypeProvider methods.
//

XTYPEPROVIDER_COMMON_IMPL( Content );

uno::Sequence< uno::Type > SAL_CALL Content::getTypes()
    throw( uno::RuntimeException )
{
    static cppu::OTypeCollection *pFolderCollection = NULL;
    static cppu::OTypeCollection *pFileCollection = NULL;

    if (!pFolderCollection) {
        osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );

        if (!pFolderCollection) {
            static cppu::OTypeCollection aFolderCollection
                (CPPU_TYPE_REF( lang::XTypeProvider ),
                 CPPU_TYPE_REF( lang::XServiceInfo ),
                 CPPU_TYPE_REF( lang::XComponent ),
                 CPPU_TYPE_REF( ucb::XContent ),
                 CPPU_TYPE_REF( ucb::XCommandProcessor ),
                 CPPU_TYPE_REF( beans::XPropertiesChangeNotifier ),
                 CPPU_TYPE_REF( ucb::XCommandInfoChangeNotifier ),
                 CPPU_TYPE_REF( beans::XPropertyContainer ),
                 CPPU_TYPE_REF( beans::XPropertySetInfoChangeNotifier ),
                 CPPU_TYPE_REF( container::XChild ),
                 CPPU_TYPE_REF( ucb::XContentCreator ) ); // !!
            static cppu::OTypeCollection aFileCollection
                (CPPU_TYPE_REF( lang::XTypeProvider ),
                 CPPU_TYPE_REF( lang::XServiceInfo ),
                 CPPU_TYPE_REF( lang::XComponent ),
                 CPPU_TYPE_REF( ucb::XContent ),
                 CPPU_TYPE_REF( ucb::XCommandProcessor ),
                 CPPU_TYPE_REF( beans::XPropertiesChangeNotifier ),
                 CPPU_TYPE_REF( ucb::XCommandInfoChangeNotifier ),
                 CPPU_TYPE_REF( beans::XPropertyContainer ),
                 CPPU_TYPE_REF( beans::XPropertySetInfoChangeNotifier ),
                 CPPU_TYPE_REF( container::XChild ) );

            pFolderCollection = &aFolderCollection;
            pFileCollection = &aFileCollection;
            OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
        }
    }
    else {
        OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
    }

    if ( isFolder( uno::Reference< ucb::XCommandEnvironment >() ) )
        return pFolderCollection->getTypes();
    else
        return pFileCollection->getTypes();
}

//
// XServiceInfo methods.
//

rtl::OUString SAL_CALL Content::getImplementationName()
    throw( uno::RuntimeException )
{
    return rtl::OUString::createFromAscii("com.sun.star.comp.GnomeVFSContent" );
}

uno::Sequence< rtl::OUString > SAL_CALL Content::getSupportedServiceNames()
    throw( uno::RuntimeException )
{
    uno::Sequence< rtl::OUString > aSNS( 1 );
    aSNS.getArray()[ 0 ] = rtl::OUString::createFromAscii(
        "com.sun.star.ucb.GnomeVFSContent" );
    return aSNS;
}

//
// XContent methods.
//

rtl::OUString SAL_CALL Content::getContentType()
    throw( uno::RuntimeException )
{
    if ( isFolder( uno::Reference< ucb::XCommandEnvironment >() ) )
        return rtl::OUString::createFromAscii( GVFS_FOLDER_TYPE );
    else
        return rtl::OUString::createFromAscii( GVFS_FILE_TYPE );
}

//
// XCommandProcessor methods.
//

uno::Any Content::getBadArgExcept()
{
    return uno::makeAny( lang::IllegalArgumentException
                 ( rtl::OUString::createFromAscii( "Wrong argument type!" ),
                   static_cast< cppu::OWeakObject * >( this ),
                   -1 ) );
}

#include <stdio.h>

uno::Any SAL_CALL Content::execute(
        const ucb::Command& aCommand,
        sal_Int32 /*CommandId*/,
        const uno::Reference< ucb::XCommandEnvironment >& xEnv )
    throw( uno::Exception,
           ucb::CommandAbortedException,
           uno::RuntimeException )
{
    uno::Any aRet;

#ifdef DEBUG
    {
        uno::Reference< task::XInteractionHandler > xIH;

        if ( xEnv.is() )
            xIH = xEnv->getInteractionHandler();
        g_warning( "Execute command: '%s' with %s interaction env",
               OUStringToGnome( aCommand.Name ),
               xIH.is() ? "" : "NO" );
    }
#endif

#define COMMAND_IS(cmd,name) ( (cmd).Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( name ) ) )

    if ( COMMAND_IS( aCommand, "getPropertyValues" ) ) {
        uno::Sequence< beans::Property > Properties;

        if ( !( aCommand.Argument >>= Properties ) )
            ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );

        aRet <<= getPropertyValues( Properties, xEnv );

    } else if ( COMMAND_IS( aCommand, "setPropertyValues" ) ) {
        uno::Sequence< beans::PropertyValue > aProperties;

        if ( !( aCommand.Argument >>= aProperties ) ||
             !aProperties.getLength() )
            ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );

        aRet <<= setPropertyValues( aProperties, xEnv );

    } else if ( COMMAND_IS( aCommand, "getPropertySetInfo" ) ) {
        aRet <<= getPropertySetInfo( xEnv, sal_False );

    } else if ( COMMAND_IS( aCommand, "getCommandInfo" ) ) {
        aRet <<= getCommandInfo( xEnv, sal_False );

    } else if ( COMMAND_IS( aCommand, "open" ) ) {
        rtl::OUString str = m_xIdentifier->getContentIdentifier();
        rtl::OString stra(
            str.getStr(),
            str.getLength(),
            RTL_TEXTENCODING_UTF8);

        ucb::OpenCommandArgument2 aOpenCommand;
        if ( !( aCommand.Argument >>= aOpenCommand ) )
            ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );

        sal_Bool bOpenFolder =
            ( ( aOpenCommand.Mode == ucb::OpenMode::ALL ) ||
              ( aOpenCommand.Mode == ucb::OpenMode::FOLDERS ) ||
              ( aOpenCommand.Mode == ucb::OpenMode::DOCUMENTS ) );

        if ( bOpenFolder && isFolder( xEnv ) ) {
            uno::Reference< ucb::XDynamicResultSet > xSet
                = new DynamicResultSet(m_xSMgr, this, aOpenCommand, xEnv );
            aRet <<= xSet;

        } else if ( aOpenCommand.Sink.is() ) {

            if ( ( aOpenCommand.Mode
                   == ucb::OpenMode::DOCUMENT_SHARE_DENY_NONE ) ||
                 ( aOpenCommand.Mode
                   == ucb::OpenMode::DOCUMENT_SHARE_DENY_WRITE ) ) {
                ucbhelper::cancelCommandExecution
                    ( uno::makeAny ( ucb::UnsupportedOpenModeException
                             ( rtl::OUString(),
                               static_cast< cppu::OWeakObject * >( this ),
                               sal_Int16( aOpenCommand.Mode ) ) ),
                      xEnv );
            }
            if ( !feedSink( aOpenCommand.Sink, xEnv ) ) {
                // Note: aOpenCommand.Sink may contain an XStream
                //       implementation. Support for this type of
                //       sink is optional...
#ifdef DEBUG
                g_warning ("Failed to load data from '%s'", getURI());
#endif
                ucbhelper::cancelCommandExecution
                    ( uno::makeAny (ucb::UnsupportedDataSinkException
                            ( rtl::OUString(),
                              static_cast< cppu::OWeakObject * >( this ),
                              aOpenCommand.Sink ) ),
                      xEnv );
            }
        }
#ifdef DEBUG
        else
            g_warning ("Open falling through ...");
#endif

    } else if ( COMMAND_IS( aCommand, "insert" ) ) {
        ucb::InsertCommandArgument arg;
        if ( !( aCommand.Argument >>= arg ) )
            ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );

        insert( arg.Data, arg.ReplaceExisting, xEnv );

    } else if ( COMMAND_IS( aCommand, "delete" ) ) {

        sal_Bool bDeletePhysical = sal_False;
        aCommand.Argument >>= bDeletePhysical;

        ::rtl::OString aURI = getOURI();
        GnomeVFSResult result = gnome_vfs_unlink ((const sal_Char *) aURI);

        if (result != GNOME_VFS_OK)
            cancelCommandExecution( result, xEnv, sal_True );

        destroy( bDeletePhysical );

    } else if ( COMMAND_IS( aCommand, "transfer" ) && isFolder( xEnv ) ) {
        ucb::TransferInfo transferArgs;

        if ( !( aCommand.Argument >>= transferArgs ) )
            ucbhelper::cancelCommandExecution( getBadArgExcept(), xEnv );

        transfer( transferArgs, xEnv );

    } else { // Unsuported
#ifdef DEBUG
        g_warning( "Unsupported command: '%s'",
               OUStringToGnome( aCommand.Name ) );
#endif
        ucbhelper::cancelCommandExecution
            ( uno::makeAny( ucb::UnsupportedCommandException
                    ( rtl::OUString(),
                      static_cast< cppu::OWeakObject * >( this ) ) ),
              xEnv );
    }
#undef COMMAND_IS

    return aRet;
}

void SAL_CALL Content::abort( sal_Int32 /*CommandId*/ )
    throw( uno::RuntimeException )
{
    // FIXME: we should use the GnomeVFSCancellation APIs here ...
}

//
// XContentCreator methods.
//

uno::Sequence< ucb::ContentInfo > SAL_CALL
Content::queryCreatableContentsInfo()
    throw( uno::RuntimeException )
{
    uno::Sequence< ucb::ContentInfo > seq(2);

    // Minimum set of props we really need
    uno::Sequence< beans::Property > props( 1 );
    props[0] = beans::Property(
        rtl::OUString::createFromAscii( "Title" ),
        -1,
        getCppuType( static_cast< rtl::OUString* >( 0 ) ),
        beans::PropertyAttribute::MAYBEVOID | beans::PropertyAttribute::BOUND );

    // file
    seq[0].Type       = rtl::OUString::createFromAscii( GVFS_FILE_TYPE );
    seq[0].Attributes = ( ucb::ContentInfoAttribute::INSERT_WITH_INPUTSTREAM |
                  ucb::ContentInfoAttribute::KIND_DOCUMENT );
    seq[0].Properties = props;

    // folder
    seq[1].Type       = rtl::OUString::createFromAscii( GVFS_FOLDER_TYPE );
    seq[1].Attributes = ucb::ContentInfoAttribute::KIND_FOLDER;
    seq[1].Properties = props;

    return seq;
}

uno::Reference< ucb::XContent > SAL_CALL
Content::createNewContent( const ucb::ContentInfo& Info )
    throw( uno::RuntimeException )
{
    bool create_document;
    const char *name;

          if ( Info.Type.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( GVFS_FILE_TYPE ) ) )
        create_document = true;
    else if ( Info.Type.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( GVFS_FOLDER_TYPE ) ) )
        create_document = false;
    else {
#ifdef DEBUG
        g_warning( "Failed to create new content '%s'",
               OUStringToGnome( Info.Type ) );
#endif
        return uno::Reference< ucb::XContent >();
    }

#ifdef DEBUG
    g_warning( "createNewContent (%d)", (int) create_document );
#endif

        rtl::OUString aURL = getOUURI();

    if ( ( aURL.lastIndexOf( '/' ) + 1 ) != aURL.getLength() )
        aURL += rtl::OUString::createFromAscii( "/" );

    name = create_document ? "[New_Content]" : "[New_Collection]";
    // This looks problematic to me cf. webdav
    aURL += rtl::OUString::createFromAscii( name );

        uno::Reference< ucb::XContentIdentifier > xId
        ( new ::ucbhelper::ContentIdentifier( m_xSMgr, aURL ) );

          try {
        return new ::gvfs::Content( m_xSMgr, m_pProvider, xId, !create_document );
    } catch ( ucb::ContentCreationException & ) {
        return uno::Reference< ucb::XContent >();
          }
}

rtl::OUString Content::getParentURL()
{
    rtl::OUString aParentURL;
    // <scheme>://              -> ""
    // <scheme>://foo           -> ""
    // <scheme>://foo/          -> ""
    // <scheme>://foo/bar       -> <scheme>://foo/
    // <scheme>://foo/bar/      -> <scheme>://foo/
    // <scheme>://foo/bar/abc   -> <scheme>://foo/bar/

    rtl::OUString aURL = getOUURI();

    sal_Int32 nPos = aURL.lastIndexOf( '/' );
    if ( nPos == ( aURL.getLength() - 1 ) ) {
        // Trailing slash found. Skip.
        nPos = aURL.lastIndexOf( '/', nPos );
    }

    sal_Int32 nPos1 = aURL.lastIndexOf( '/', nPos );
    if ( nPos1 != -1 )
        nPos1 = aURL.lastIndexOf( '/', nPos1 );

    if ( nPos1 != -1 )
        aParentURL = rtl::OUString( aURL.copy( 0, nPos + 1 ) );

#ifdef DEBUG
    g_warning ("getParentURL '%s' -> '%s'",
           getURI(), (const sal_Char *) rtl::OUStringToOString
               ( aParentURL, RTL_TEXTENCODING_UTF8 ) );
#endif

    return aParentURL;
}

static util::DateTime
getDateFromUnix (time_t t)
{
    TimeValue tv;
    tv.Nanosec = 0;
    tv.Seconds = t;
    oslDateTime dt;

    if ( osl_getDateTimeFromTimeValue( &tv, &dt ) )
        return util::DateTime( 0, dt.Seconds, dt.Minutes, dt.Hours,
                               dt.Day, dt.Month, dt.Year);
    else
        return util::DateTime();
}

uno::Reference< sdbc::XRow > Content::getPropertyValues(
                const uno::Sequence< beans::Property >& rProperties,
                const uno::Reference< ucb::XCommandEnvironment >& xEnv )
{
    int nProps;
    GnomeVFSResult result;
    uno::Sequence< beans::Property > allProperties;

    if( ( result = getInfo( xEnv ) ) != GNOME_VFS_OK )
        cancelCommandExecution( result, xEnv, sal_False );

    const beans::Property* pProps;

    if( rProperties.getLength() ) {
        nProps = rProperties.getLength();
        pProps = rProperties.getConstArray();
    } else {
        allProperties = getPropertySetInfo( xEnv )->getProperties();
        nProps = allProperties.getLength();
        pProps = allProperties.getConstArray();
    }

    rtl::Reference< ::ucbhelper::PropertyValueSet > xRow
        = new ::ucbhelper::PropertyValueSet( m_xSMgr );

        osl::Guard< osl::Mutex > aGuard( m_aMutex );
    for( sal_Int32 n = 0; n < nProps; ++n ) {
        const beans::Property& rProp = pProps[ n ];

        if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Title" ) ) ) {
            if (m_info.name && m_info.name[0] == '/')
                g_warning ("Odd NFS title on item '%s' == '%s'",
                       getURI(), m_info.name);
            xRow->appendString( rProp, GnomeToOUString( m_info.name ) );
        }

        else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ContentType" ) ) )
            xRow->appendString( rProp, getContentType () );

        else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsDocument" ) ) ) {
            if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE)
                xRow->appendBoolean( rProp, ( m_info.type == GNOME_VFS_FILE_TYPE_REGULAR ||
                                  m_info.type == GNOME_VFS_FILE_TYPE_UNKNOWN ) );
            else
                xRow->appendVoid( rProp );
        }

        else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsFolder" ) ) ) {
            if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE)
                xRow->appendBoolean( rProp, ( m_info.type == GNOME_VFS_FILE_TYPE_DIRECTORY ) );
            else
                xRow->appendVoid( rProp );
        }

        else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Size" ) ) ) {
            if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE)
                xRow->appendLong( rProp, m_info.size );
            else
                xRow->appendVoid( rProp );
        }

        else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsReadOnly" ) ) ) {
            if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS) {
                bool read_only = true;

                if (m_info.uid == getuid () &&
                    m_info.permissions & GNOME_VFS_PERM_USER_WRITE)
                    read_only = false;
                else if (m_info.gid == getgid () &&
                    m_info.permissions & GNOME_VFS_PERM_GROUP_WRITE)
                    read_only = false;
                else if (m_info.permissions & GNOME_VFS_PERM_OTHER_WRITE)
                    read_only = false;
                xRow->appendBoolean( rProp, read_only );
            } else
                xRow->appendVoid( rProp );
        }

        else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsHidden" ) ) )
            xRow->appendBoolean( rProp, ( m_info.name && m_info.name[0] == '.' ) );

        else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsVolume" ) ) ||
             rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsCompactDisk" ) ) )
            xRow->appendBoolean( rProp, sal_False );

        else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DateCreated" ) ) ) {
            if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_CTIME)
                xRow->appendTimestamp( rProp, getDateFromUnix( m_info.ctime ) );
            else
                xRow->appendVoid( rProp );
        }

        else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DateModified" ) ) ) {
            if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MTIME)
                xRow->appendTimestamp( rProp, getDateFromUnix( m_info.mtime ) );
            else
                xRow->appendVoid( rProp );
        }

        else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) ) {
            // We do this by sniffing in gnome-vfs; rather expensively.
#ifdef DEBUG
            g_warning ("FIXME: Requested mime-type - an expensive op. indeed!");
#endif
            xRow->appendVoid( rProp );
        } else {
            xRow->appendVoid( rProp );
        }
    }
#ifdef DEBUG
    g_warning ("getPropertyValues on '%s' %d properties returned (of %d)",
           getURI(), (int)xRow->getLength(), (int)nProps);
#endif

    return uno::Reference< sdbc::XRow >( xRow.get() );
}

static lang::IllegalAccessException
getReadOnlyException( Content *ctnt )
{
    return lang::IllegalAccessException
        ( rtl::OUString::createFromAscii( "Property is read-only!" ),
          static_cast< cppu::OWeakObject * >( ctnt ) );
}

rtl::OUString
Content::makeNewURL( const char */*newName*/ )
{
    rtl::OUString aNewURL = getParentURL();
    if ( aNewURL.lastIndexOf( '/' ) != ( aNewURL.getLength() - 1 ) )
        aNewURL += rtl::OUString::createFromAscii( "/" );

    char *name = gnome_vfs_escape_string( m_info.name );
    aNewURL += GnomeToOUString( name );
    g_free( name );

    return aNewURL;
}

// This is slightly complicated by needing to support either 'move' or 'setname'
GnomeVFSResult
Content::doSetFileInfo( const GnomeVFSFileInfo *newInfo,
            GnomeVFSSetFileInfoMask setMask,
            const uno::Reference< ucb::XCommandEnvironment >& /*xEnv*/ )
{
    GnomeVFSResult result = GNOME_VFS_OK;

    g_assert (!m_bTransient);

    ::rtl::OString aURI = getOURI();

        osl::Guard< osl::Mutex > aGuard( m_aMutex );

    // The simple approach:
    if( setMask != GNOME_VFS_SET_FILE_INFO_NONE )
        result = gnome_vfs_set_file_info // missed a const in the API there
            ( (const sal_Char *) aURI, (GnomeVFSFileInfo *)newInfo, setMask );

    if ( result == GNOME_VFS_ERROR_NOT_SUPPORTED &&
         ( setMask & GNOME_VFS_SET_FILE_INFO_NAME ) ) {
        // Try a move instead
#ifdef DEBUG
        g_warning( "SetFileInfo not supported on '%s'", getURI() );
#endif

        char *newURI = OUStringToGnome( makeNewURL( newInfo->name ) );

        result = gnome_vfs_move ((const sal_Char *)aURI, newURI, FALSE);

        g_free (newURI);
    }

    return result;
}


uno::Sequence< uno::Any > Content::setPropertyValues(
                const uno::Sequence< beans::PropertyValue >& rValues,
                const uno::Reference< ucb::XCommandEnvironment >& xEnv )
{
    rtl::OUString    aNewTitle;
    GnomeVFSFileInfo newInfo;
    int              setMask = GNOME_VFS_SET_FILE_INFO_NONE;

    getInfo( xEnv );

      osl::ClearableGuard< osl::Mutex > aGuard( m_aMutex );

    gnome_vfs_file_info_copy( &newInfo, &m_info );

    Authentication aAuth( xEnv );

    int nChanged = 0, nTitlePos = 0;
    uno::Sequence< uno::Any > aRet( rValues.getLength() );
    uno::Sequence< beans::PropertyChangeEvent > aChanges( rValues.getLength() );

    beans::PropertyChangeEvent aEvent;
    aEvent.Source         = static_cast< cppu::OWeakObject * >( this );
      aEvent.Further        = sal_False;
      aEvent.PropertyHandle = -1;
      // aEvent.PropertyName = fill in later ...
      // aEvent.OldValue     =
      // aEvent.NewValue     =

      int nCount = rValues.getLength();
    const beans::PropertyValue* pValues = rValues.getConstArray();

      for ( sal_Int32 n = 0; n < nCount; ++n ) {
        const beans::PropertyValue& rValue = pValues[ n ];

#ifdef DEBUG
        g_warning( "Set prop '%s'", OUStringToGnome( rValue.Name ) );
#endif
        if ( rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ContentType" ) ) ||
             rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) ||
             rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsDocument" ) ) ||
             rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsFolder" ) ) ||
             rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Size" ) ) )
            aRet[ n ] <<= getReadOnlyException( this );

        else if ( rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Title" ) ) ) {
            if ( rValue.Value >>= aNewTitle ) {
                if ( aNewTitle.getLength() <= 0 )
                    aRet[ n ] <<= lang::IllegalArgumentException
                        ( rtl::OUString::createFromAscii( "Empty title not allowed!" ),
                          static_cast< cppu::OWeakObject * >( this ), -1 );
                else {
                    char *newName = OUStringToGnome( aNewTitle );

                    if( !newName || !m_info.name || strcmp( newName, m_info.name ) ) {
#ifdef DEBUG
                        g_warning ("Set new name to '%s'", newName);
#endif

                        aEvent.PropertyName = rtl::OUString::createFromAscii( "Title" );
                        aEvent.OldValue     = uno::makeAny( GnomeToOUString( newInfo.name ) );
                        aEvent.NewValue     = uno::makeAny( aNewTitle );
                        aChanges.getArray()[ nChanged ] = aEvent;
                        nTitlePos = nChanged++;

                        newInfo.name = newName;
                        setMask |= GNOME_VFS_SET_FILE_INFO_NAME;
                    } else // same name
                        g_free (newName);
                }
            } else
                aRet[ n ] <<= beans::IllegalTypeException
                    ( rtl::OUString::createFromAscii( "Property value has wrong type!" ),
                      static_cast< cppu::OWeakObject * >( this ) );

        } else if ( rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DateCreated" ) ) ||
                rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DateModified" ) ) ) {
            // FIXME: should be able to set the timestamps
            aRet[ n ] <<= getReadOnlyException( this );
        } else {
#ifdef DEBUG
            g_warning( "Unhandled property '%s'", OUStringToGnome( rValue.Name ) );
#endif
            aRet[ n ] <<= getReadOnlyException( this );
        }
    }

    GnomeVFSResult result = GNOME_VFS_OK;

    if ( !m_bTransient &&
         ( result = doSetFileInfo( &newInfo,
                       (GnomeVFSSetFileInfoMask) setMask,
                       xEnv ) ) != GNOME_VFS_OK ) {
        for (int i = 0; i < nChanged; i++)
            aRet[ i ] <<= mapVFSException( result, sal_True );

    }

    if ( result == GNOME_VFS_OK) {
        gnome_vfs_file_info_copy( &m_info, &newInfo );

        if ( setMask & GNOME_VFS_SET_FILE_INFO_NAME ) {
            uno::Reference< ucb::XContentIdentifier > xNewId
                = new ::ucbhelper::ContentIdentifier(
                    m_xSMgr, makeNewURL( newInfo.name ) );

            aGuard.clear();
            if (!exchangeIdentity( xNewId ) )
                aRet[ nTitlePos ] <<= uno::Exception
                    ( rtl::OUString::createFromAscii( "Exchange failed!" ),
                      static_cast< cppu::OWeakObject * >( this ) );
        }
    }

    gnome_vfs_file_info_clear( &newInfo );

    if ( nChanged > 0 ) {
            aGuard.clear();
        aChanges.realloc( nChanged );
        notifyPropertiesChange( aChanges );
    }

    return aRet;
}

void Content::queryChildren( ContentRefList& rChildren )
{
    // Obtain a list with a snapshot of all currently instanciated contents
    // from provider and extract the contents which are direct children
    // of this content.

    ::ucbhelper::ContentRefList aAllContents;
    m_xProvider->queryExistingContents( aAllContents );

    rtl::OUString aURL = getOUURI();
    sal_Int32 nURLPos = aURL.lastIndexOf( '/' );

    if ( nURLPos != ( aURL.getLength() - 1 ) )
        aURL += rtl::OUString::createFromAscii( "/" );

    sal_Int32 nLen = aURL.getLength();

    ::ucbhelper::ContentRefList::const_iterator it  = aAllContents.begin();
    ::ucbhelper::ContentRefList::const_iterator end = aAllContents.end();

    while ( it != end ) {
        ::ucbhelper::ContentImplHelperRef xChild = (*it);
        rtl::OUString aChildURL
            = xChild->getIdentifier()->getContentIdentifier();

        // Is aURL a prefix of aChildURL?
        if ( ( aChildURL.getLength() > nLen ) &&
             ( aChildURL.compareTo( aURL, nLen ) == 0 ) ) {
            sal_Int32 nPos = nLen;
            nPos = aChildURL.indexOf( '/', nPos );

            if ( ( nPos == -1 ) ||
                 ( nPos == ( aChildURL.getLength() - 1 ) ) ) {
                // No further slashes / only a final slash. It's a child!
                rChildren.push_back( ::gvfs::Content::ContentRef
                             (static_cast< ::gvfs::Content * >(xChild.get() ) ) );
            }
        }
        ++it;
    }
}

void Content::insert(
        const uno::Reference< io::XInputStream >               &xInputStream,
        sal_Bool                                                bReplaceExisting,
        const uno::Reference< ucb::XCommandEnvironment > &xEnv )
        throw( uno::Exception )
{
      osl::ClearableGuard< osl::Mutex > aGuard( m_aMutex );

#ifdef DEBUG
    g_warning( "Insert '%s' (%d) (0x%x:%d)", getURI(), bReplaceExisting,
           m_info.valid_fields, m_info.type );
#endif

    GnomeVFSResult result = getInfo( xEnv );
    // a racy design indeed.
    if( !bReplaceExisting && !m_bTransient &&
        result != GNOME_VFS_ERROR_NOT_FOUND) {
#ifdef DEBUG
        g_warning ("Nasty error inserting to '%s' ('%s')",
               getURI(), gnome_vfs_result_to_string( result ));
#endif
        cancelCommandExecution( GNOME_VFS_ERROR_FILE_EXISTS, xEnv, sal_True );
    }

    if ( m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE &&
         m_info.type == GNOME_VFS_FILE_TYPE_DIRECTORY ) {
        ::rtl::OString aURI = getOURI();
        int perm;

        perm = ( GNOME_VFS_PERM_USER_ALL |
             GNOME_VFS_PERM_GROUP_READ |
             GNOME_VFS_PERM_OTHER_READ );

#ifdef DEBUG
        g_warning ("Make directory");
#endif
        result = gnome_vfs_make_directory( (const sal_Char *) aURI, perm );

        if( result != GNOME_VFS_OK )
            cancelCommandExecution( result, xEnv, sal_True );

        return;
    }

    if ( !xInputStream.is() ) {
        // FIXME: slightly unclear whether to accept this and create an empty file
        ucbhelper::cancelCommandExecution
            ( uno::makeAny
              ( ucb::MissingInputStreamException
                ( rtl::OUString(),
                  static_cast< cppu::OWeakObject * >( this ) ) ),
              xEnv );
    }

    GnomeVFSHandle *handle = NULL;
    ::rtl::OString aURI = getOURI();

    result = GNOME_VFS_OK;
    if ( bReplaceExisting ) {
        Authentication aAuth( xEnv );
        result = gnome_vfs_open( &handle, (const sal_Char *)aURI,
                     GNOME_VFS_OPEN_WRITE );
    }

    if ( result != GNOME_VFS_OK ) {
        int perm;
        Authentication aAuth( xEnv );

        perm = ( ( GNOME_VFS_PERM_USER_WRITE | GNOME_VFS_PERM_USER_READ ) |
             ( GNOME_VFS_PERM_GROUP_WRITE | GNOME_VFS_PERM_GROUP_READ ) );

        result = gnome_vfs_create
            ( &handle, (const sal_Char *)aURI, GNOME_VFS_OPEN_WRITE, TRUE, perm );
    }

    if( result != GNOME_VFS_OK )
        cancelCommandExecution( result, xEnv, sal_True );

    if ( !xInputStream.is() ) {
        result = gnome_vfs_close( handle );
        if (result != GNOME_VFS_OK)
            cancelCommandExecution( result, xEnv, sal_True );

    } else { // copy it over
        uno::Reference < io::XOutputStream > xOutput =
            new gvfs::Stream( handle, &m_info );

        copyData( xInputStream, xOutput );
    }

    if (m_bTransient) {
          m_bTransient = sal_False;
        aGuard.clear();
          inserted();
    }
}

void Content::transfer(const ucb::TransferInfo & /*rArgs*/,
               const uno::Reference< ucb::XCommandEnvironment >& xEnv )
    throw( uno::Exception )
{
    // FIXME: see gnome-vfs-xfer.h - but we need to be able to easily
    // detect which are gnome-vfs owned URI types ...
    ucbhelper::cancelCommandExecution
        ( uno::makeAny
            ( ucb::InteractiveBadTransferURLException
                ( rtl::OUString::createFromAscii( "Unsupported URL scheme!" ),
                  static_cast< cppu::OWeakObject * >( this ) ) ),
          xEnv );
}

void Content::destroy( sal_Bool bDeletePhysical )
    throw( uno::Exception )
{
    // @@@ take care about bDeletePhysical -> trashcan support
    rtl::OUString aURL = getOUURI();

    uno::Reference< ucb::XContent > xThis = this;

    deleted();

    osl::Guard< osl::Mutex > aGuard( m_aMutex );

    // Process instanciated children...
    ::gvfs::Content::ContentRefList aChildren;
    queryChildren( aChildren );

    ContentRefList::const_iterator it  = aChildren.begin();
    ContentRefList::const_iterator end = aChildren.end();

    while ( it != end ) {
        (*it)->destroy( bDeletePhysical );
        ++it;
    }
}

// Used by the 'setPropertyValues' method for
// propagating the renaming of a Content.
sal_Bool Content::exchangeIdentity(
    const uno::Reference< ucb::XContentIdentifier >& xNewId )
{
    if ( !xNewId.is() )
        return sal_False;

    uno::Reference< ucb::XContent > xThis = this;

#ifdef DEBUG
    g_warning( "exchangeIdentity from '%s' to '%s'",
           getURI(), OUStringToGnome( xNewId->getContentIdentifier() ) );
#endif

    if ( m_bTransient ) {
        osl::Guard< osl::Mutex > aGuard( m_aMutex );
        /* FIXME: can we not screw up an identically named
         * Content pointing to ourself here ? */
        m_xIdentifier = xNewId;
        return sal_False;
    }

    rtl::OUString aOldURL = getOUURI();

    // Exchange own identitity.
    if ( exchange( xNewId ) ) {

        // Process instanciated children...
        ContentRefList aChildren;
        queryChildren( aChildren );

        ContentRefList::const_iterator it  = aChildren.begin();
        ContentRefList::const_iterator end = aChildren.end();

        while ( it != end ) {
            ContentRef xChild = (*it);

            // Create new content identifier for the child...
            uno::Reference< ucb::XContentIdentifier >
                xOldChildId = xChild->getIdentifier();
            rtl::OUString aOldChildURL
                = xOldChildId->getContentIdentifier();
            rtl::OUString aNewChildURL
                = aOldChildURL.replaceAt(
                             0,
                             aOldURL.getLength(),
                             xNewId->getContentIdentifier() );
            uno::Reference< ucb::XContentIdentifier >
                xNewChildId
                = new ::ucbhelper::ContentIdentifier( m_xSMgr, aNewChildURL );

            if ( !xChild->exchangeIdentity( xNewChildId ) )
                return sal_False;

            ++it;
        }
        return sal_True;
    }

    return sal_False;
}

GnomeVFSResult
Content::getInfo( const uno::Reference< ucb::XCommandEnvironment >& xEnv )
{
    GnomeVFSResult result;
        osl::Guard< osl::Mutex > aGuard( m_aMutex );

    if (m_bTransient)
        result = GNOME_VFS_OK;

    else if ( !m_info.valid_fields ) {
        ::rtl::OString aURI = getOURI();
        Authentication aAuth( xEnv );
        result = gnome_vfs_get_file_info
            ( (const sal_Char *)aURI, &m_info, GNOME_VFS_FILE_INFO_DEFAULT );
        if (result != GNOME_VFS_OK)
            gnome_vfs_file_info_clear( &m_info );
    } else
        result = GNOME_VFS_OK;
#ifdef DEBUG
    g_warning( "getInfo on '%s' returns '%s' (%d) (0x%x)",
           getURI(), gnome_vfs_result_to_string( result ),
           result, m_info.valid_fields );
#endif
    return result;
}

sal_Bool
Content::isFolder(const uno::Reference< ucb::XCommandEnvironment >& xEnv )
{
        osl::Guard< osl::Mutex > aGuard( m_aMutex );
    getInfo( xEnv );
    return (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE &&
        m_info.type == GNOME_VFS_FILE_TYPE_DIRECTORY);
}

uno::Any Content::mapVFSException( const GnomeVFSResult result, sal_Bool bWrite )
{
    uno::Any aException;
    const char *gvfs_message;
    rtl::OUString message;
    uno::Sequence< uno::Any > aArgs( 1 );

#ifdef DEBUG
    g_warning ("Map VFS exception '%s' (%d)",
           gnome_vfs_result_to_string( result ), result );
#endif

    if ((gvfs_message = gnome_vfs_result_to_string (result)))
        message = GnomeToOUString( gvfs_message );

    switch (result) {
    case GNOME_VFS_OK:
        g_error ("VFS_OK mapped to exception.");
        break;
    case GNOME_VFS_ERROR_EOF:
        g_warning ("VFS_EOF not handled somewhere.");
        break;
    case GNOME_VFS_ERROR_NOT_FOUND:
        aArgs[ 0 ] <<= m_xIdentifier->getContentIdentifier();
        aException <<=
            ucb::InteractiveAugmentedIOException
            ( rtl::OUString::createFromAscii( "Not found!" ),
              static_cast< cppu::OWeakObject * >( this ),
              task::InteractionClassification_ERROR,
              ucb::IOErrorCode_NOT_EXISTING,
              aArgs );
        break;
    case GNOME_VFS_ERROR_BAD_PARAMETERS:
        aException <<=
            lang::IllegalArgumentException
                ( rtl::OUString(),
                  static_cast< cppu::OWeakObject * >( this ),
                  -1 );
        break;
    case GNOME_VFS_ERROR_GENERIC:
    case GNOME_VFS_ERROR_INTERNAL:
    case GNOME_VFS_ERROR_NOT_SUPPORTED:
#ifdef DEBUG
        g_warning ("Internal - un-mapped error");
#endif
        aException <<= io::IOException();
        break;
    case GNOME_VFS_ERROR_IO:
        if ( bWrite )
            aException <<=
                ucb::InteractiveNetworkWriteException
                ( rtl::OUString(),
                  static_cast< cppu::OWeakObject * >( this ),
                  task::InteractionClassification_ERROR,
                  message );
        else
            aException <<=
                ucb::InteractiveNetworkReadException
                ( rtl::OUString(),
                  static_cast< cppu::OWeakObject * >( this ),
                  task::InteractionClassification_ERROR,
                  message );
        break;
    case GNOME_VFS_ERROR_HOST_NOT_FOUND:
    case GNOME_VFS_ERROR_INVALID_HOST_NAME:
        aException <<=
            ucb::InteractiveNetworkResolveNameException
            ( rtl::OUString(),
              static_cast< cppu::OWeakObject * >( this ),
              task::InteractionClassification_ERROR,
              message );
        break;
    case GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE:
    case GNOME_VFS_ERROR_SERVICE_OBSOLETE:
    case GNOME_VFS_ERROR_PROTOCOL_ERROR:
    case GNOME_VFS_ERROR_NO_MASTER_BROWSER:
        aException <<=
            ucb::InteractiveNetworkConnectException
                ( rtl::OUString(),
                  static_cast< cppu::OWeakObject * >( this ),
                  task::InteractionClassification_ERROR,
                  message );
        break;

    case GNOME_VFS_ERROR_FILE_EXISTS:
        aException <<= ucb::NameClashException
                ( rtl::OUString(),
                  static_cast< cppu::OWeakObject * >( this ),
                  task::InteractionClassification_ERROR,
                  message );
        break;

    case GNOME_VFS_ERROR_INVALID_OPEN_MODE:
        aException <<= ucb::UnsupportedOpenModeException();
        break;

    case GNOME_VFS_ERROR_CORRUPTED_DATA:
    case GNOME_VFS_ERROR_WRONG_FORMAT:
    case GNOME_VFS_ERROR_BAD_FILE:
    case GNOME_VFS_ERROR_TOO_BIG:
    case GNOME_VFS_ERROR_NO_SPACE:
    case GNOME_VFS_ERROR_READ_ONLY:
    case GNOME_VFS_ERROR_INVALID_URI:
    case GNOME_VFS_ERROR_NOT_OPEN:
    case GNOME_VFS_ERROR_ACCESS_DENIED:
    case GNOME_VFS_ERROR_TOO_MANY_OPEN_FILES:
    case GNOME_VFS_ERROR_NOT_A_DIRECTORY:
    case GNOME_VFS_ERROR_IN_PROGRESS:
    case GNOME_VFS_ERROR_INTERRUPTED:
    case GNOME_VFS_ERROR_LOOP:
    case GNOME_VFS_ERROR_NOT_PERMITTED:
    case GNOME_VFS_ERROR_IS_DIRECTORY:
    case GNOME_VFS_ERROR_NO_MEMORY:
    case GNOME_VFS_ERROR_HOST_HAS_NO_ADDRESS:
    case GNOME_VFS_ERROR_LOGIN_FAILED:
    case GNOME_VFS_ERROR_CANCELLED:
    case GNOME_VFS_ERROR_DIRECTORY_BUSY:
    case GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY:
    case GNOME_VFS_ERROR_TOO_MANY_LINKS:
    case GNOME_VFS_ERROR_READ_ONLY_FILE_SYSTEM:
    case GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM:
    case GNOME_VFS_ERROR_NAME_TOO_LONG:
#ifdef DEBUG
        g_warning( "FIXME: Un-mapped VFS exception '%s' (%d)",
               gnome_vfs_result_to_string( result ), result );
#endif
    default:
        aException <<= ucb::InteractiveNetworkGeneralException
            ( rtl::OUString(),
              static_cast< cppu::OWeakObject * >( this ),
              task::InteractionClassification_ERROR );
        break;
    }

    return aException;
}

void Content::cancelCommandExecution(
    GnomeVFSResult result,
    const uno::Reference< ucb::XCommandEnvironment > & xEnv,
    sal_Bool bWrite /* = sal_False */ )
    throw ( uno::Exception )
{
    ucbhelper::cancelCommandExecution( mapVFSException( result, bWrite ), xEnv );
    // Unreachable
}

uno::Sequence< beans::Property > Content::getProperties(
    const uno::Reference< ucb::XCommandEnvironment > & /*xEnv*/ )
{
    static const beans::Property aGenericProperties[] = {
                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ContentType" ) ),
                 -1, getCppuType( static_cast< const rtl::OUString * >( 0 ) ),
                 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsDocument" ) ),
                 -1, getCppuBooleanType(),
                 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsFolder" ) ),
                 -1, getCppuBooleanType(),
                 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Title" ) ),
                 -1, getCppuType( static_cast< const rtl::OUString * >( 0 ) ),
                 beans::PropertyAttribute::BOUND ),
        // Optional ...
        beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DateCreated" ) ),
                 -1, getCppuType( static_cast< const util::DateTime * >( 0 ) ),
                 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DateModified" ) ),
                 -1, getCppuType( static_cast< const util::DateTime * >( 0 ) ),
                 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
// FIXME: Too expensive for now (?)
//                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ),
//               -1, getCppuType( static_cast< const rtl::OUString * >( 0 ) ),
//               beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Size" ) ),
                 -1, getCppuType( static_cast< const sal_Int64 * >( 0 ) ),
                 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsReadOnly" ) ),
                 -1, getCppuBooleanType(),
                 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsVolume" ) ),
                 -1, getCppuBooleanType(),
                 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsCompactDisk" ) ),
                 -1, getCppuBooleanType(),
                 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsHidden" ) ),
                 -1, getCppuBooleanType(),
                 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY )
    };

    const int nProps = sizeof (aGenericProperties) / sizeof (aGenericProperties[0]);

    return uno::Sequence< beans::Property > ( aGenericProperties, nProps );

}

uno::Sequence< ucb::CommandInfo > Content::getCommands(
    const uno::Reference< ucb::XCommandEnvironment > & xEnv )
{
    static ucb::CommandInfo aDocumentCommandInfoTable[] = {
        // Required commands
        ucb::CommandInfo
        ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "getCommandInfo" ) ),
          -1, getCppuVoidType() ),
        ucb::CommandInfo
        ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "getPropertySetInfo" ) ),
          -1, getCppuVoidType() ),
        ucb::CommandInfo
        ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "getPropertyValues" ) ),
          -1, getCppuType( static_cast<uno::Sequence< beans::Property > * >( 0 ) ) ),
        ucb::CommandInfo
        ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "setPropertyValues" ) ),
          -1, getCppuType( static_cast<uno::Sequence< beans::PropertyValue > * >( 0 ) ) ),

        // Optional standard commands
        ucb::CommandInfo
        ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "delete" ) ),
          -1, getCppuBooleanType() ),
        ucb::CommandInfo
        ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "insert" ) ),
          -1, getCppuType( static_cast<ucb::InsertCommandArgument * >( 0 ) ) ),
        ucb::CommandInfo
        ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "open" ) ),
          -1, getCppuType( static_cast<ucb::OpenCommandArgument2 * >( 0 ) ) ),

        // Folder only
        ucb::CommandInfo
        ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "transfer" ) ),
          -1, getCppuType( static_cast<ucb::TransferInfo * >( 0 ) ) )
    };
    int num = 7;

       if ( isFolder( xEnv ) )
         num += 1;

    return uno::Sequence< ucb::CommandInfo >(aDocumentCommandInfoTable, num );
}

rtl::OUString
Content::getOUURI ()
{
        osl::Guard< osl::Mutex > aGuard( m_aMutex );
    return m_xIdentifier->getContentIdentifier();
}

rtl::OString
Content::getOURI ()
{
    return rtl::OUStringToOString( getOUURI(), RTL_TEXTENCODING_UTF8 );
}

char *
Content::getURI ()
{
    return OUStringToGnome( getOUURI() );
}

void
Content::copyData( uno::Reference< io::XInputStream > xIn,
                   uno::Reference< io::XOutputStream > xOut )
{
    uno::Sequence< sal_Int8 > theData( TRANSFER_BUFFER_SIZE );

    g_return_if_fail( xIn.is() && xOut.is() );

    while ( xIn->readBytes( theData, TRANSFER_BUFFER_SIZE ) > 0 )
        xOut->writeBytes( theData );

    xOut->closeOutput();
}

// Inherits an authentication context
uno::Reference< io::XInputStream >
Content::createTempStream(
    const uno::Reference< ucb::XCommandEnvironment >& xEnv )
        throw( uno::Exception )
{
    GnomeVFSResult result;
    GnomeVFSHandle *handle = NULL;
    ::rtl::OString aURI = getOURI();

        osl::Guard< osl::Mutex > aGuard( m_aMutex );
    // Something badly wrong happened - can't seek => stream to a temporary file
    const rtl::OUString sServiceName ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.io.TempFile" ) );
    uno::Reference < io::XOutputStream > xTempOut =
        uno::Reference < io::XOutputStream >
            ( m_xSMgr->createInstance( sServiceName ), uno::UNO_QUERY );

    if ( !xTempOut.is() )
        cancelCommandExecution( GNOME_VFS_ERROR_IO, xEnv );

    result = gnome_vfs_open
        ( &handle, (const sal_Char *)aURI, GNOME_VFS_OPEN_READ );
    if (result != GNOME_VFS_OK)
        cancelCommandExecution( result, xEnv );

    uno::Reference < io::XInputStream > pStream = new ::gvfs::Stream( handle, &m_info );
    copyData( pStream, xTempOut );

    return uno::Reference < io::XInputStream > ( xTempOut, uno::UNO_QUERY );
}

uno::Reference< io::XInputStream >
Content::createInputStream(
    const uno::Reference< ucb::XCommandEnvironment >& xEnv )
        throw( uno::Exception )
{
    GnomeVFSHandle *handle = NULL;
    GnomeVFSResult  result;
    uno::Reference<io::XInputStream > xIn;

    Authentication aAuth( xEnv );
        osl::Guard< osl::Mutex > aGuard( m_aMutex );

    getInfo( xEnv );
    ::rtl::OString aURI = getOURI();

    if ( !(m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE) )
        return createTempStream( xEnv );

    result = gnome_vfs_open
        ( &handle, (const sal_Char *)aURI,
          (GnomeVFSOpenMode) (GNOME_VFS_OPEN_READ | GNOME_VFS_OPEN_RANDOM ) );

    if (result == GNOME_VFS_ERROR_INVALID_OPEN_MODE ||
        result == GNOME_VFS_ERROR_NOT_SUPPORTED)
        return createTempStream( xEnv );

    if (result != GNOME_VFS_OK)
        cancelCommandExecution( result, xEnv );

    // Try a seek just to make sure it's Random access: some lie.
    result = gnome_vfs_seek( handle, GNOME_VFS_SEEK_START, 0);
    if (result == GNOME_VFS_ERROR_NOT_SUPPORTED) {
        gnome_vfs_close( handle );
        return createTempStream( xEnv );
    }

    if (result != GNOME_VFS_OK)
        cancelCommandExecution( result, xEnv );

    if (handle != NULL)
        xIn = new ::gvfs::Stream( handle, &m_info );

    return xIn;
}

sal_Bool
Content::feedSink( uno::Reference< uno::XInterface > aSink,
                   const uno::Reference< ucb::XCommandEnvironment >& xEnv )
{
    if ( !aSink.is() )
        return sal_False;

    uno::Reference< io::XOutputStream > xOut
        = uno::Reference< io::XOutputStream >(aSink, uno::UNO_QUERY );
    uno::Reference< io::XActiveDataSink > xDataSink
        = uno::Reference< io::XActiveDataSink >(aSink, uno::UNO_QUERY );

    if ( !xOut.is() && !xDataSink.is() )
        return sal_False;

    uno::Reference< io::XInputStream > xIn = createInputStream( xEnv );
    if ( !xIn.is() )
        return sal_False;

    if ( xOut.is() )
        copyData( xIn, xOut );

    if ( xDataSink.is() )
        xDataSink->setInputStream( xIn );

    return sal_True;
}

extern "C" {

#ifndef GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION
#  error "We require Gnome VFS 2.6.x to compile (will run fine with < 2.6)"
#endif

    static void
    vfs_authentication_callback (gconstpointer in_void,
                     gsize         in_size,
                     gpointer      out_void,
                     gsize         out_size,
                     gpointer      callback_data)
    {
        task::XInteractionHandler *xIH;

#ifdef DEBUG
        g_warning ("Full authentication callback (%p) ...", callback_data);
#endif

        if( !( xIH = (task::XInteractionHandler *) callback_data ) )
            return;

        const GnomeVFSModuleCallbackFullAuthenticationIn *in =
            (const GnomeVFSModuleCallbackFullAuthenticationIn *) in_void;
        GnomeVFSModuleCallbackFullAuthenticationOut *out =
            (GnomeVFSModuleCallbackFullAuthenticationOut *) out_void;

        g_return_if_fail (in != NULL && out != NULL);
        g_return_if_fail (sizeof (GnomeVFSModuleCallbackFullAuthenticationIn) == in_size &&
                  sizeof (GnomeVFSModuleCallbackFullAuthenticationOut) == out_size);

#ifdef DEBUG
#  define NNIL(x) (x?x:"<Null>")
        g_warning (" InComing data 0x%x uri '%s' prot '%s' server '%s' object '%s' "
                   "port %d auth_t '%s' user '%s' domain '%s' "
                   "def user '%s', def domain '%s'",
                   (int) in->flags, NNIL(in->uri), NNIL(in->protocol),
                   NNIL(in->server), NNIL(in->object),
                   (int) in->port, NNIL(in->authtype), NNIL(in->username), NNIL(in->domain),
                   NNIL(in->default_user), NNIL(in->default_domain));
#  undef NNIL
#endif

        ucbhelper::SimpleAuthenticationRequest::EntityType
                                   eDomain, eUserName, ePassword;
        ::rtl::OUString aHostName, aDomain, aUserName, aPassword;

        aHostName = GnomeToOUString( in->server );

        if (in->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_DOMAIN)
        {
            aDomain = GnomeToOUString( in->domain );
            eDomain = ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY;
            if (!aDomain.getLength())
                aDomain = GnomeToOUString( in->default_domain );
        }
        else // no underlying capability to display realm otherwise
            eDomain = ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;

        aUserName = GnomeToOUString( in->username );
        if (!aUserName.getLength())
            aUserName = GnomeToOUString( in->default_user );
        eUserName = (in->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_USERNAME) ?
            ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY :
                (aUserName.getLength() ?
                    ucbhelper::SimpleAuthenticationRequest::ENTITY_FIXED :
                    ucbhelper::SimpleAuthenticationRequest::ENTITY_NA);

        // No suggested password.
        ePassword = (in->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_PASSWORD) ?
            ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY :
            ucbhelper::SimpleAuthenticationRequest::ENTITY_FIXED;

        // Really, really bad things happen if we don't provide
        // the same user/password as was entered last time if
        // we failed to authenticate - infinite looping / flickering
        // madness etc. [ nice infrastructure ! ]
        static rtl::OUString aLastUserName, aLastPassword;
        if (in->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_PREVIOUS_ATTEMPT_FAILED)
        {
            osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
            aUserName = aLastUserName;
            aPassword = aLastPassword;
        }

        rtl::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
            = new ucbhelper::SimpleAuthenticationRequest (GnomeToOUString(in->uri),
                                                          aHostName, eDomain, aDomain,
                                                          eUserName, aUserName,
                                                          ePassword, aPassword);

        xIH->handle( xRequest.get() );

        rtl::Reference< ucbhelper::InteractionContinuation > xSelection
            = xRequest->getSelection();

        if ( xSelection.is() ) {
            // Handler handled the request.
            uno::Reference< task::XInteractionAbort > xAbort(xSelection.get(), uno::UNO_QUERY );
            if ( !xAbort.is() ) {
                const rtl::Reference<
                    ucbhelper::InteractionSupplyAuthentication > & xSupp
                    = xRequest->getAuthenticationSupplier();

                aUserName = xSupp->getUserName();
                aDomain   = xSupp->getRealm();
                aPassword = xSupp->getPassword();

                {
                    osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
                    aLastUserName = aUserName;
                    aLastPassword = aPassword;
                }

                out->username = OUStringToGnome( aUserName );
                out->domain   = OUStringToGnome( aDomain );
                out->password = OUStringToGnome( aPassword );
                out->save_password = xSupp->getRememberPasswordMode();

#ifdef DEBUG
                g_warning ("Got valid user/domain/password '%s' '%s' '%s', %s password",
                           out->username, out->domain, out->password,
                           out->save_password ? "save" : "don't save");
#endif
            }
            else
                out->abort_auth = TRUE;
        }
        else
            out->abort_auth = TRUE;
    }

    static void
    vfs_authentication_old_callback (gconstpointer in_void,
                                     gsize         in_size,
                                     gpointer      out_void,
                                     gsize         out_size,
                                     gpointer      callback_data)
    {
#ifdef DEBUG
        g_warning ("Old authentication callback (%p) [ UNTESTED ] ...", callback_data);
#endif
        const GnomeVFSModuleCallbackAuthenticationIn *in =
            (const GnomeVFSModuleCallbackAuthenticationIn *) in_void;
        GnomeVFSModuleCallbackAuthenticationOut *out =
            (GnomeVFSModuleCallbackAuthenticationOut *) out_void;

        g_return_if_fail (in != NULL && out != NULL);
        g_return_if_fail (sizeof (GnomeVFSModuleCallbackAuthenticationIn) == in_size &&
                  sizeof (GnomeVFSModuleCallbackAuthenticationOut) == out_size);

        GnomeVFSModuleCallbackFullAuthenticationIn mapped_in = {
                (GnomeVFSModuleCallbackFullAuthenticationFlags)
                (GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_PASSWORD |
                 GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_USERNAME |
                 GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_DOMAIN),
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        GnomeVFSModuleCallbackFullAuthenticationOut mapped_out = { 0, 0, 0, 0, 0, 0, 0, 0 };

        // Map the old style input auth. data to the new style structure.
        if (in->previous_attempt_failed)
            mapped_in.flags = (GnomeVFSModuleCallbackFullAuthenticationFlags)
                (mapped_in.flags |
                 GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_PREVIOUS_ATTEMPT_FAILED);

        GnomeVFSURI *pURI = NULL;
        // Urk - parse all this from the URL ...
        mapped_in.uri = in->uri;
        if (in->uri)
        {
            pURI = gnome_vfs_uri_new( in->uri );
            mapped_in.protocol = (char *) gnome_vfs_uri_get_scheme (pURI);
            mapped_in.server   = (char *) gnome_vfs_uri_get_host_name (pURI);
            mapped_in.port     = gnome_vfs_uri_get_host_port (pURI);
            mapped_in.username = (char *) gnome_vfs_uri_get_user_name (pURI);
        }
        mapped_in.domain = in->realm;
        mapped_in.default_user = mapped_in.username;
        mapped_in.default_domain = mapped_in.domain;

        vfs_authentication_callback ((gconstpointer) &mapped_in,
                                     sizeof (mapped_in),
                                     (gpointer) &mapped_out,
                                     sizeof (mapped_out),
                                     callback_data);

        if (pURI)
            gnome_vfs_uri_unref (pURI);

        // Map the new style auth. out data to the old style out structure.
        out->username = mapped_out.username;
        out->password = mapped_out.password;
        g_free (mapped_out.domain);
        g_free (mapped_out.keyring);
    }


    static void
    auth_destroy (gpointer data)
    {
        task::XInteractionHandler *xIH;
        if( ( xIH = ( task::XInteractionHandler * )data ) )
            xIH->release();
    }

    // This sucks, but gnome-vfs doesn't much like
    // repeated set / unsets - so we have to compensate.
    GPrivate *auth_queue = NULL;

    void auth_queue_destroy( gpointer data )
    {
        GList  *l;
        GQueue *vq = (GQueue *) data;

        for (l = vq->head; l; l = l->next)
            auth_destroy (l->data);
        g_queue_free (vq);
    }
}

static void
refresh_auth( GQueue *vq )
{
    GList *l;

    gnome_vfs_module_callback_pop( GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION );
    gnome_vfs_module_callback_pop( GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION );

    for (l = vq->head; l; l = l->next) {
        if (l->data) {
            gnome_vfs_module_callback_push
                ( GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION,
                  vfs_authentication_old_callback, l->data, NULL );
            gnome_vfs_module_callback_push
                ( GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION,
                  vfs_authentication_callback, l->data, NULL );
            break;
        }
    }
}

gvfs::Authentication::Authentication(
    const uno::Reference< ucb::XCommandEnvironment > & xEnv )
{
    GQueue *vq;
    uno::Reference< task::XInteractionHandler > xIH;

    if ( xEnv.is() )
        xIH = xEnv->getInteractionHandler();

    if ( xIH.is() )
        xIH->acquire();

    if( !(vq = (GQueue *)g_private_get( auth_queue ) ) ) {
        vq = g_queue_new();
        g_private_set( auth_queue, vq );
    }

    g_queue_push_head( vq, (gpointer) xIH.get() );
    refresh_auth( vq );
}

gvfs::Authentication::~Authentication()
{
    GQueue *vq;
    gpointer data;

    vq = (GQueue *)g_private_get( auth_queue );

    data = g_queue_pop_head( vq );
    auth_destroy (data);

    refresh_auth( vq );
}