xref: /aoo41x/main/ucb/source/ucp/gvfs/gvfs_content.cxx (revision 2f86921c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_ucb.hxx"
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 
30 #include "osl/time.h"
31 #include <osl/diagnose.h>
32 
33 #include "osl/doublecheckedlocking.h"
34 
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <com/sun/star/beans/PropertySetInfoChange.hpp>
38 #include <com/sun/star/beans/PropertySetInfoChangeEvent.hpp>
39 #include <com/sun/star/io/XActiveDataSink.hpp>
40 #include <com/sun/star/io/XOutputStream.hpp>
41 #include <com/sun/star/lang/IllegalAccessException.hpp>
42 #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
43 #include <com/sun/star/ucb/InsertCommandArgument.hpp>
44 #include <com/sun/star/ucb/InteractiveBadTransferURLException.hpp>
45 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
46 #include <com/sun/star/ucb/InteractiveNetworkConnectException.hpp>
47 #include <com/sun/star/ucb/InteractiveNetworkGeneralException.hpp>
48 #include <com/sun/star/ucb/InteractiveNetworkReadException.hpp>
49 #include <com/sun/star/ucb/InteractiveNetworkResolveNameException.hpp>
50 #include <com/sun/star/ucb/InteractiveNetworkWriteException.hpp>
51 #include <com/sun/star/ucb/NameClash.hpp>
52 #include <com/sun/star/ucb/NameClashException.hpp>
53 #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
54 #include <com/sun/star/ucb/OpenMode.hpp>
55 #include <com/sun/star/ucb/PostCommandArgument2.hpp>
56 #include <com/sun/star/ucb/TransferInfo.hpp>
57 #include <com/sun/star/ucb/XCommandInfo.hpp>
58 #include <com/sun/star/ucb/XPersistentPropertySet.hpp>
59 #include <com/sun/star/ucb/MissingInputStreamException.hpp>
60 #include <com/sun/star/ucb/MissingPropertiesException.hpp>
61 #include <com/sun/star/ucb/UnsupportedCommandException.hpp>
62 #include <com/sun/star/ucb/UnsupportedDataSinkException.hpp>
63 #include <com/sun/star/ucb/UnsupportedNameClashException.hpp>
64 #include <com/sun/star/ucb/UnsupportedOpenModeException.hpp>
65 #include <com/sun/star/ucb/UnsupportedOpenModeException.hpp>
66 #include <com/sun/star/ucb/NameClashException.hpp>
67 #include <ucbhelper/contentidentifier.hxx>
68 #include <ucbhelper/propertyvalueset.hxx>
69 #include <ucbhelper/interactionrequest.hxx>
70 #include <ucbhelper/cancelcommandexecution.hxx>
71 #include <ucbhelper/simpleauthenticationrequest.hxx>
72 
73 const int TRANSFER_BUFFER_SIZE = 65536;
74 
75 /*
76  * NB. Name escaping is done only for URIs
77  * the 'Title' property is unescaped on set/get
78  */
79 #include <libgnomevfs/gnome-vfs-utils.h>
80 #include <libgnomevfs/gnome-vfs-result.h>
81 #include <libgnomevfs/gnome-vfs-standard-callbacks.h>
82 extern "C" { // missing in the header: doh.
83 #  include <libgnomevfs/gnome-vfs-module-callback.h>
84 }
85 
86 #include "gvfs_content.hxx"
87 #include "gvfs_provider.hxx"
88 #include "gvfs_directory.hxx"
89 #include "gvfs_stream.hxx"
90 
91 using namespace gvfs;
92 using namespace com::sun::star;
93 
94 #define CLEAR_INFO(info) memset((info), 0, sizeof ((info)[0]))
95 
96 
97 static char *
98 OUStringToGnome( const rtl::OUString &str )
99 {
100     rtl::OString aTempStr = rtl::OUStringToOString( str, RTL_TEXTENCODING_UTF8 );
101     return g_strdup( (const sal_Char *) aTempStr );
102 }
103 
104 static rtl::OUString
105 GnomeToOUString( const char *utf8_str)
106 {
107     if (!utf8_str)
108         return rtl::OUString();
109     else
110         return rtl::OUString( utf8_str, strlen( utf8_str ), RTL_TEXTENCODING_UTF8 );
111 }
112 
113 
114 Content::Content(
115           const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
116           ContentProvider* pProvider,
117           const uno::Reference< ucb::XContentIdentifier >& Identifier)
118   throw ( ucb::ContentCreationException )
119     : ContentImplHelper( rxSMgr, pProvider, Identifier ),
120       m_pProvider( pProvider ),
121       m_bTransient( sal_False )
122 {
123     CLEAR_INFO (&m_info);
124 #ifdef DEBUG
125     g_warning ("New Content ('%s')", getURI());
126 #endif
127 }
128 
129 Content::Content(
130     const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
131     ContentProvider                                   * pProvider,
132     const uno::Reference< ucb::XContentIdentifier >&    Identifier,
133     sal_Bool                                            IsFolder)
134         throw ( ucb::ContentCreationException )
135     : ContentImplHelper( rxSMgr, pProvider, Identifier ),
136       m_pProvider( pProvider ),
137       m_bTransient( sal_True )
138 {
139     CLEAR_INFO (&m_info);
140 
141 #ifdef DEBUG
142     g_warning ("New Transient content ('%s') (%d)", getURI(), IsFolder);
143 #endif
144 //	m_info.name = FIXME: set name ?
145     m_info.valid_fields = GNOME_VFS_FILE_INFO_FIELDS_TYPE;
146     m_info.type = IsFolder ? GNOME_VFS_FILE_TYPE_DIRECTORY :
147                          GNOME_VFS_FILE_TYPE_REGULAR;
148 }
149 
150 // virtual
151 Content::~Content()
152 {
153     gnome_vfs_file_info_clear( &m_info );
154 }
155 
156 //
157 // XInterface methods.
158 //
159 
160 void SAL_CALL Content::acquire()
161     throw( )
162 {
163     ContentImplHelper::acquire();
164 }
165 void SAL_CALL Content::release()
166     throw( )
167 {
168     ContentImplHelper::release();
169 }
170 uno::Any SAL_CALL Content::queryInterface( const uno::Type & rType )
171     throw ( uno::RuntimeException )
172 {
173     // Note: isFolder may require network activities! So call it only
174     //       if it is really necessary!!!
175     uno::Any aRet = cppu::queryInterface( rType,
176                         static_cast< ucb::XContentCreator * >( this ) );
177     if ( aRet.hasValue() )
178             return isFolder( uno::Reference< ucb::XCommandEnvironment >() )
179             ? aRet : uno::Any();
180     else
181         return aRet.hasValue() ? aRet : ContentImplHelper::queryInterface( rType );
182 }
183 
184 //
185 // XTypeProvider methods.
186 //
187 
188 XTYPEPROVIDER_COMMON_IMPL( Content );
189 
190 uno::Sequence< uno::Type > SAL_CALL Content::getTypes()
191     throw( uno::RuntimeException )
192 {
193     static cppu::OTypeCollection *pFolderCollection = NULL;
194     static cppu::OTypeCollection *pFileCollection = NULL;
195 
196     if (!pFolderCollection) {
197         osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
198 
199         if (!pFolderCollection) {
200             static cppu::OTypeCollection aFolderCollection
201                 (CPPU_TYPE_REF( lang::XTypeProvider ),
202                  CPPU_TYPE_REF( lang::XServiceInfo ),
203                  CPPU_TYPE_REF( lang::XComponent ),
204                  CPPU_TYPE_REF( ucb::XContent ),
205                  CPPU_TYPE_REF( ucb::XCommandProcessor ),
206                  CPPU_TYPE_REF( beans::XPropertiesChangeNotifier ),
207                  CPPU_TYPE_REF( ucb::XCommandInfoChangeNotifier ),
208                  CPPU_TYPE_REF( beans::XPropertyContainer ),
209                  CPPU_TYPE_REF( beans::XPropertySetInfoChangeNotifier ),
210                  CPPU_TYPE_REF( container::XChild ),
211                  CPPU_TYPE_REF( ucb::XContentCreator ) ); // !!
212             static cppu::OTypeCollection aFileCollection
213                 (CPPU_TYPE_REF( lang::XTypeProvider ),
214                  CPPU_TYPE_REF( lang::XServiceInfo ),
215                  CPPU_TYPE_REF( lang::XComponent ),
216                  CPPU_TYPE_REF( ucb::XContent ),
217                  CPPU_TYPE_REF( ucb::XCommandProcessor ),
218                  CPPU_TYPE_REF( beans::XPropertiesChangeNotifier ),
219                  CPPU_TYPE_REF( ucb::XCommandInfoChangeNotifier ),
220                  CPPU_TYPE_REF( beans::XPropertyContainer ),
221                  CPPU_TYPE_REF( beans::XPropertySetInfoChangeNotifier ),
222                  CPPU_TYPE_REF( container::XChild ) );
223 
224             pFolderCollection = &aFolderCollection;
225             pFileCollection = &aFileCollection;
226             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
227         }
228     }
229     else {
230         OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
231     }
232 
233     if ( isFolder( uno::Reference< ucb::XCommandEnvironment >() ) )
234         return pFolderCollection->getTypes();
235     else
236         return pFileCollection->getTypes();
237 }
238 
239 //
240 // XServiceInfo methods.
241 //
242 
243 rtl::OUString SAL_CALL Content::getImplementationName()
244     throw( uno::RuntimeException )
245 {
246     return rtl::OUString::createFromAscii("com.sun.star.comp.GnomeVFSContent" );
247 }
248 
249 uno::Sequence< rtl::OUString > SAL_CALL Content::getSupportedServiceNames()
250     throw( uno::RuntimeException )
251 {
252     uno::Sequence< rtl::OUString > aSNS( 1 );
253     aSNS.getArray()[ 0 ] = rtl::OUString::createFromAscii(
254         "com.sun.star.ucb.GnomeVFSContent" );
255     return aSNS;
256 }
257 
258 //
259 // XContent methods.
260 //
261 
262 rtl::OUString SAL_CALL Content::getContentType()
263     throw( uno::RuntimeException )
264 {
265     if ( isFolder( uno::Reference< ucb::XCommandEnvironment >() ) )
266         return rtl::OUString::createFromAscii( GVFS_FOLDER_TYPE );
267     else
268         return rtl::OUString::createFromAscii( GVFS_FILE_TYPE );
269 }
270 
271 //
272 // XCommandProcessor methods.
273 //
274 
275 uno::Any Content::getBadArgExcept()
276 {
277     return uno::makeAny( lang::IllegalArgumentException
278                  ( rtl::OUString::createFromAscii( "Wrong argument type!" ),
279                    static_cast< cppu::OWeakObject * >( this ),
280                    -1 ) );
281 }
282 
283 #include <stdio.h>
284 
285 uno::Any SAL_CALL Content::execute(
286         const ucb::Command& aCommand,
287         sal_Int32 /*CommandId*/,
288         const uno::Reference< ucb::XCommandEnvironment >& xEnv )
289     throw( uno::Exception,
290            ucb::CommandAbortedException,
291            uno::RuntimeException )
292 {
293     uno::Any aRet;
294 
295 #ifdef DEBUG
296     {
297         uno::Reference< task::XInteractionHandler > xIH;
298 
299         if ( xEnv.is() )
300             xIH = xEnv->getInteractionHandler();
301         g_warning( "Execute command: '%s' with %s interaction env",
302                OUStringToGnome( aCommand.Name ),
303                xIH.is() ? "" : "NO" );
304     }
305 #endif
306 
307 #define COMMAND_IS(cmd,name) ( (cmd).Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( name ) ) )
308 
309     if ( COMMAND_IS( aCommand, "getPropertyValues" ) ) {
310         uno::Sequence< beans::Property > Properties;
311 
312         if ( !( aCommand.Argument >>= Properties ) )
313             ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
314 
315         aRet <<= getPropertyValues( Properties, xEnv );
316 
317     } else if ( COMMAND_IS( aCommand, "setPropertyValues" ) ) {
318         uno::Sequence< beans::PropertyValue > aProperties;
319 
320         if ( !( aCommand.Argument >>= aProperties ) ||
321              !aProperties.getLength() )
322             ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
323 
324         aRet <<= setPropertyValues( aProperties, xEnv );
325 
326     } else if ( COMMAND_IS( aCommand, "getPropertySetInfo" ) ) {
327         aRet <<= getPropertySetInfo( xEnv, sal_False );
328 
329     } else if ( COMMAND_IS( aCommand, "getCommandInfo" ) ) {
330         aRet <<= getCommandInfo( xEnv, sal_False );
331 
332     } else if ( COMMAND_IS( aCommand, "open" ) ) {
333         rtl::OUString str = m_xIdentifier->getContentIdentifier();
334         rtl::OString stra(
335             str.getStr(),
336             str.getLength(),
337             RTL_TEXTENCODING_UTF8);
338 
339         ucb::OpenCommandArgument2 aOpenCommand;
340         if ( !( aCommand.Argument >>= aOpenCommand ) )
341             ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
342 
343         sal_Bool bOpenFolder =
344             ( ( aOpenCommand.Mode == ucb::OpenMode::ALL ) ||
345               ( aOpenCommand.Mode == ucb::OpenMode::FOLDERS ) ||
346               ( aOpenCommand.Mode == ucb::OpenMode::DOCUMENTS ) );
347 
348         if ( bOpenFolder && isFolder( xEnv ) ) {
349             uno::Reference< ucb::XDynamicResultSet > xSet
350                 = new DynamicResultSet(m_xSMgr, this, aOpenCommand, xEnv );
351             aRet <<= xSet;
352 
353         } else if ( aOpenCommand.Sink.is() ) {
354 
355             if ( ( aOpenCommand.Mode
356                    == ucb::OpenMode::DOCUMENT_SHARE_DENY_NONE ) ||
357                  ( aOpenCommand.Mode
358                    == ucb::OpenMode::DOCUMENT_SHARE_DENY_WRITE ) ) {
359                 ucbhelper::cancelCommandExecution
360                     ( uno::makeAny ( ucb::UnsupportedOpenModeException
361                              ( rtl::OUString(),
362                                static_cast< cppu::OWeakObject * >( this ),
363                                sal_Int16( aOpenCommand.Mode ) ) ),
364                       xEnv );
365             }
366             if ( !feedSink( aOpenCommand.Sink, xEnv ) ) {
367                 // Note: aOpenCommand.Sink may contain an XStream
368                 //       implementation. Support for this type of
369                 //       sink is optional...
370 #ifdef DEBUG
371                 g_warning ("Failed to load data from '%s'", getURI());
372 #endif
373                 ucbhelper::cancelCommandExecution
374                     ( uno::makeAny (ucb::UnsupportedDataSinkException
375                             ( rtl::OUString(),
376                               static_cast< cppu::OWeakObject * >( this ),
377                               aOpenCommand.Sink ) ),
378                       xEnv );
379             }
380         }
381 #ifdef DEBUG
382         else
383             g_warning ("Open falling through ...");
384 #endif
385 
386     } else if ( COMMAND_IS( aCommand, "createNewContent" ) && isFolder( xEnv ) ) {
387         ucb::ContentInfo arg;
388         if ( !( aCommand.Argument >>= arg ) )
389             ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
390 
391         aRet <<= createNewContent( arg );
392 
393     } else if ( COMMAND_IS( aCommand, "insert" ) ) {
394         ucb::InsertCommandArgument arg;
395         if ( !( aCommand.Argument >>= arg ) )
396             ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
397 
398         insert( arg.Data, arg.ReplaceExisting, xEnv );
399 
400     } else if ( COMMAND_IS( aCommand, "delete" ) ) {
401 
402         sal_Bool bDeletePhysical = sal_False;
403         aCommand.Argument >>= bDeletePhysical;
404 
405         ::rtl::OString aURI = getOURI();
406         GnomeVFSResult result = gnome_vfs_unlink ((const sal_Char *) aURI);
407 
408         if (result != GNOME_VFS_OK)
409             cancelCommandExecution( result, xEnv, sal_True );
410 
411         destroy( bDeletePhysical );
412 
413     } else if ( COMMAND_IS( aCommand, "transfer" ) && isFolder( xEnv ) ) {
414         ucb::TransferInfo transferArgs;
415 
416         if ( !( aCommand.Argument >>= transferArgs ) )
417             ucbhelper::cancelCommandExecution( getBadArgExcept(), xEnv );
418 
419         transfer( transferArgs, xEnv );
420 
421     } else { // Unsuported
422 #ifdef DEBUG
423         g_warning( "Unsupported command: '%s'",
424                OUStringToGnome( aCommand.Name ) );
425 #endif
426         ucbhelper::cancelCommandExecution
427             ( uno::makeAny( ucb::UnsupportedCommandException
428                     ( rtl::OUString(),
429                       static_cast< cppu::OWeakObject * >( this ) ) ),
430               xEnv );
431     }
432 #undef COMMAND_IS
433 
434     return aRet;
435 }
436 
437 void SAL_CALL Content::abort( sal_Int32 /*CommandId*/ )
438     throw( uno::RuntimeException )
439 {
440     // FIXME: we should use the GnomeVFSCancellation APIs here ...
441 }
442 
443 //
444 // XContentCreator methods.
445 //
446 
447 uno::Sequence< ucb::ContentInfo > Content::queryCreatableContentsInfo(
448     const uno::Reference< ucb::XCommandEnvironment >& xEnv)
449             throw( uno::RuntimeException )
450 {
451     if ( isFolder( xEnv ) )
452     {
453         uno::Sequence< ucb::ContentInfo > seq(2);
454 
455         // Minimum set of props we really need
456         uno::Sequence< beans::Property > props( 1 );
457         props[0] = beans::Property(
458             rtl::OUString::createFromAscii( "Title" ),
459             -1,
460             getCppuType( static_cast< rtl::OUString* >( 0 ) ),
461             beans::PropertyAttribute::MAYBEVOID | beans::PropertyAttribute::BOUND );
462 
463         // file
464         seq[0].Type       = rtl::OUString::createFromAscii( GVFS_FILE_TYPE );
465         seq[0].Attributes = ( ucb::ContentInfoAttribute::INSERT_WITH_INPUTSTREAM |
466                               ucb::ContentInfoAttribute::KIND_DOCUMENT );
467         seq[0].Properties = props;
468 
469         // folder
470         seq[1].Type       = rtl::OUString::createFromAscii( GVFS_FOLDER_TYPE );
471         seq[1].Attributes = ucb::ContentInfoAttribute::KIND_FOLDER;
472         seq[1].Properties = props;
473 
474         return seq;
475     }
476     else
477     {
478         return uno::Sequence< ucb::ContentInfo >();
479     }
480 }
481 
482 uno::Sequence< ucb::ContentInfo > SAL_CALL Content::queryCreatableContentsInfo()
483             throw( uno::RuntimeException )
484 {
485     return queryCreatableContentsInfo( uno::Reference< ucb::XCommandEnvironment >() );
486 }
487 
488 uno::Reference< ucb::XContent > SAL_CALL
489 Content::createNewContent( const ucb::ContentInfo& Info )
490     throw( uno::RuntimeException )
491 {
492     bool create_document;
493     const char *name;
494 
495         if ( Info.Type.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( GVFS_FILE_TYPE ) ) )
496         create_document = true;
497     else if ( Info.Type.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( GVFS_FOLDER_TYPE ) ) )
498         create_document = false;
499     else {
500 #ifdef DEBUG
501         g_warning( "Failed to create new content '%s'",
502                OUStringToGnome( Info.Type ) );
503 #endif
504         return uno::Reference< ucb::XContent >();
505     }
506 
507 #ifdef DEBUG
508     g_warning( "createNewContent (%d)", (int) create_document );
509 #endif
510 
511         rtl::OUString aURL = getOUURI();
512 
513     if ( ( aURL.lastIndexOf( '/' ) + 1 ) != aURL.getLength() )
514         aURL += rtl::OUString::createFromAscii( "/" );
515 
516     name = create_document ? "[New_Content]" : "[New_Collection]";
517     // This looks problematic to me cf. webdav
518     aURL += rtl::OUString::createFromAscii( name );
519 
520         uno::Reference< ucb::XContentIdentifier > xId
521         ( new ::ucbhelper::ContentIdentifier( m_xSMgr, aURL ) );
522 
523         try {
524         return new ::gvfs::Content( m_xSMgr, m_pProvider, xId, !create_document );
525     } catch ( ucb::ContentCreationException & ) {
526         return uno::Reference< ucb::XContent >();
527         }
528 }
529 
530 rtl::OUString Content::getParentURL()
531 {
532     rtl::OUString aParentURL;
533     // <scheme>://              -> ""
534     // <scheme>://foo           -> ""
535     // <scheme>://foo/          -> ""
536     // <scheme>://foo/bar       -> <scheme>://foo/
537     // <scheme>://foo/bar/      -> <scheme>://foo/
538     // <scheme>://foo/bar/abc   -> <scheme>://foo/bar/
539 
540     rtl::OUString aURL = getOUURI();
541 
542     sal_Int32 nPos = aURL.lastIndexOf( '/' );
543     if ( nPos == ( aURL.getLength() - 1 ) ) {
544         // Trailing slash found. Skip.
545         nPos = aURL.lastIndexOf( '/', nPos );
546     }
547 
548     sal_Int32 nPos1 = aURL.lastIndexOf( '/', nPos );
549     if ( nPos1 != -1 )
550         nPos1 = aURL.lastIndexOf( '/', nPos1 );
551 
552     if ( nPos1 != -1 )
553         aParentURL = rtl::OUString( aURL.copy( 0, nPos + 1 ) );
554 
555 #ifdef DEBUG
556     g_warning ("getParentURL '%s' -> '%s'",
557            getURI(), (const sal_Char *) rtl::OUStringToOString
558                ( aParentURL, RTL_TEXTENCODING_UTF8 ) );
559 #endif
560 
561     return aParentURL;
562 }
563 
564 static util::DateTime
565 getDateFromUnix (time_t t)
566 {
567     TimeValue tv;
568     tv.Nanosec = 0;
569     tv.Seconds = t;
570     oslDateTime dt;
571 
572     if ( osl_getDateTimeFromTimeValue( &tv, &dt ) )
573         return util::DateTime( 0, dt.Seconds, dt.Minutes, dt.Hours,
574                                dt.Day, dt.Month, dt.Year);
575     else
576         return util::DateTime();
577 }
578 
579 uno::Reference< sdbc::XRow > Content::getPropertyValues(
580                 const uno::Sequence< beans::Property >& rProperties,
581                 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
582 {
583     int nProps;
584     GnomeVFSResult result;
585     uno::Sequence< beans::Property > allProperties;
586 
587     if( ( result = getInfo( xEnv ) ) != GNOME_VFS_OK )
588         cancelCommandExecution( result, xEnv, sal_False );
589 
590     const beans::Property* pProps;
591 
592     if( rProperties.getLength() ) {
593         nProps = rProperties.getLength();
594         pProps = rProperties.getConstArray();
595     } else {
596         allProperties = getPropertySetInfo( xEnv )->getProperties();
597         nProps = allProperties.getLength();
598         pProps = allProperties.getConstArray();
599     }
600 
601     rtl::Reference< ::ucbhelper::PropertyValueSet > xRow
602         = new ::ucbhelper::PropertyValueSet( m_xSMgr );
603 
604         osl::Guard< osl::Mutex > aGuard( m_aMutex );
605     for( sal_Int32 n = 0; n < nProps; ++n ) {
606         const beans::Property& rProp = pProps[ n ];
607 
608         if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Title" ) ) ) {
609             if (m_info.name && m_info.name[0] == '/')
610                 g_warning ("Odd NFS title on item '%s' == '%s'",
611                        getURI(), m_info.name);
612             xRow->appendString( rProp, GnomeToOUString( m_info.name ) );
613         }
614 
615         else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ContentType" ) ) )
616             xRow->appendString( rProp, getContentType () );
617 
618         else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsDocument" ) ) ) {
619             if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE)
620                 xRow->appendBoolean( rProp, ( m_info.type == GNOME_VFS_FILE_TYPE_REGULAR ||
621                                   m_info.type == GNOME_VFS_FILE_TYPE_UNKNOWN ) );
622             else
623                 xRow->appendVoid( rProp );
624         }
625         else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsFolder" ) ) ) {
626             if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE)
627                 xRow->appendBoolean( rProp, ( m_info.type == GNOME_VFS_FILE_TYPE_DIRECTORY ) );
628             else
629                 xRow->appendVoid( rProp );
630         }
631         else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsReadOnly" ) ) ) {
632 
633             GnomeVFSFileInfo* fileInfo = gnome_vfs_file_info_new ();
634 
635             ::rtl::OString aURI = getOURI();
636             gnome_vfs_get_file_info
637                 ( (const sal_Char *)aURI, fileInfo,
638                         GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS );
639 
640             if (fileInfo->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_ACCESS) {
641                 bool read_only = true;
642 
643                 if (fileInfo->permissions & GNOME_VFS_PERM_ACCESS_WRITABLE)
644                                         read_only = false;
645 
646                 xRow->appendBoolean( rProp, read_only );
647             } else
648                 xRow->appendVoid( rProp );
649             gnome_vfs_file_info_unref (fileInfo);
650         }
651         else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Size" ) ) ) {
652             if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE)
653                 xRow->appendLong( rProp, m_info.size );
654             else
655                 xRow->appendVoid( rProp );
656         }
657         else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsHidden" ) ) )
658             xRow->appendBoolean( rProp, ( m_info.name && m_info.name[0] == '.' ) );
659 
660         else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsVolume" ) ) ||
661              rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsCompactDisk" ) ) )
662             xRow->appendBoolean( rProp, sal_False );
663 
664         else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DateCreated" ) ) ) {
665             if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_CTIME)
666                 xRow->appendTimestamp( rProp, getDateFromUnix( m_info.ctime ) );
667             else
668                 xRow->appendVoid( rProp );
669         }
670 
671         else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DateModified" ) ) ) {
672             if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MTIME)
673                 xRow->appendTimestamp( rProp, getDateFromUnix( m_info.mtime ) );
674             else
675                 xRow->appendVoid( rProp );
676         }
677 
678         else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) ) {
679             // We do this by sniffing in gnome-vfs; rather expensively.
680 #ifdef DEBUG
681             g_warning ("FIXME: Requested mime-type - an expensive op. indeed!");
682 #endif
683             xRow->appendVoid( rProp );
684         } else if (rProp.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "CreatableContentsInfo" ) ) )
685             xRow->appendObject( rProp, uno::makeAny( queryCreatableContentsInfo( xEnv ) ) );
686 
687         else {
688             xRow->appendVoid( rProp );
689         }
690     }
691 #ifdef DEBUG
692     g_warning ("getPropertyValues on '%s' %d properties returned (of %d)",
693            getURI(), (int)xRow->getLength(), (int)nProps);
694 #endif
695 
696     return uno::Reference< sdbc::XRow >( xRow.get() );
697 }
698 
699 static lang::IllegalAccessException
700 getReadOnlyException( Content *ctnt )
701 {
702     return lang::IllegalAccessException
703         ( rtl::OUString::createFromAscii( "Property is read-only!" ),
704           static_cast< cppu::OWeakObject * >( ctnt ) );
705 }
706 
707 rtl::OUString
708 Content::makeNewURL( const char */*newName*/ )
709 {
710     rtl::OUString aNewURL = getParentURL();
711     if ( aNewURL.lastIndexOf( '/' ) != ( aNewURL.getLength() - 1 ) )
712         aNewURL += rtl::OUString::createFromAscii( "/" );
713 
714     char *name = gnome_vfs_escape_string( m_info.name );
715     aNewURL += GnomeToOUString( name );
716     g_free( name );
717 
718     return aNewURL;
719 }
720 
721 // This is slightly complicated by needing to support either 'move' or 'setname'
722 GnomeVFSResult
723 Content::doSetFileInfo( const GnomeVFSFileInfo *newInfo,
724             GnomeVFSSetFileInfoMask setMask,
725             const uno::Reference< ucb::XCommandEnvironment >& /*xEnv*/ )
726 {
727     GnomeVFSResult result = GNOME_VFS_OK;
728 
729     g_assert (!m_bTransient);
730 
731     ::rtl::OString aURI = getOURI();
732 
733         osl::Guard< osl::Mutex > aGuard( m_aMutex );
734 
735     // The simple approach:
736     if( setMask != GNOME_VFS_SET_FILE_INFO_NONE )
737         result = gnome_vfs_set_file_info // missed a const in the API there
738             ( (const sal_Char *) aURI, (GnomeVFSFileInfo *)newInfo, setMask );
739 
740     if ( result == GNOME_VFS_ERROR_NOT_SUPPORTED &&
741          ( setMask & GNOME_VFS_SET_FILE_INFO_NAME ) ) {
742         // Try a move instead
743 #ifdef DEBUG
744         g_warning( "SetFileInfo not supported on '%s'", getURI() );
745 #endif
746 
747         char *newURI = OUStringToGnome( makeNewURL( newInfo->name ) );
748 
749         result = gnome_vfs_move ((const sal_Char *)aURI, newURI, FALSE);
750 
751         g_free (newURI);
752     }
753 
754     return result;
755 }
756 
757 
758 uno::Sequence< uno::Any > Content::setPropertyValues(
759                 const uno::Sequence< beans::PropertyValue >& rValues,
760                 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
761 {
762     rtl::OUString    aNewTitle;
763     GnomeVFSFileInfo newInfo;
764     int              setMask = GNOME_VFS_SET_FILE_INFO_NONE;
765 
766     getInfo( xEnv );
767 
768     osl::ClearableGuard< osl::Mutex > aGuard( m_aMutex );
769 
770     gnome_vfs_file_info_copy( &newInfo, &m_info );
771 
772     Authentication aAuth( xEnv );
773 
774     int nChanged = 0, nTitlePos = 0;
775     uno::Sequence< uno::Any > aRet( rValues.getLength() );
776     uno::Sequence< beans::PropertyChangeEvent > aChanges( rValues.getLength() );
777 
778     beans::PropertyChangeEvent aEvent;
779     aEvent.Source         = static_cast< cppu::OWeakObject * >( this );
780     aEvent.Further        = sal_False;
781     aEvent.PropertyHandle = -1;
782     // aEvent.PropertyName = fill in later ...
783     // aEvent.OldValue     =
784     // aEvent.NewValue     =
785 
786     int nCount = rValues.getLength();
787     const beans::PropertyValue* pValues = rValues.getConstArray();
788 
789     for ( sal_Int32 n = 0; n < nCount; ++n ) {
790         const beans::PropertyValue& rValue = pValues[ n ];
791 
792 #ifdef DEBUG
793         g_warning( "Set prop '%s'", OUStringToGnome( rValue.Name ) );
794 #endif
795         if ( rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ContentType" ) ) ||
796              rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) ||
797              rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsDocument" ) ) ||
798              rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsFolder" ) ) ||
799              rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Size" ) ) ||
800              rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "CreatableContentsInfo" ) ) )
801             aRet[ n ] <<= getReadOnlyException( this );
802 
803         else if ( rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Title" ) ) ) {
804             if ( rValue.Value >>= aNewTitle ) {
805                 if ( aNewTitle.getLength() <= 0 )
806                     aRet[ n ] <<= lang::IllegalArgumentException
807                         ( rtl::OUString::createFromAscii( "Empty title not allowed!" ),
808                           static_cast< cppu::OWeakObject * >( this ), -1 );
809                 else {
810                     char *newName = OUStringToGnome( aNewTitle );
811 
812                     if( !newName || !m_info.name || strcmp( newName, m_info.name ) ) {
813 #ifdef DEBUG
814                         g_warning ("Set new name to '%s'", newName);
815 #endif
816 
817                         aEvent.PropertyName = rtl::OUString::createFromAscii( "Title" );
818                         aEvent.OldValue     = uno::makeAny( GnomeToOUString( newInfo.name ) );
819                         aEvent.NewValue     = uno::makeAny( aNewTitle );
820                         aChanges.getArray()[ nChanged ] = aEvent;
821                         nTitlePos = nChanged++;
822 
823                         newInfo.name = newName;
824                         setMask |= GNOME_VFS_SET_FILE_INFO_NAME;
825                     } else // same name
826                         g_free (newName);
827                 }
828             } else
829                 aRet[ n ] <<= beans::IllegalTypeException
830                     ( rtl::OUString::createFromAscii( "Property value has wrong type!" ),
831                       static_cast< cppu::OWeakObject * >( this ) );
832 
833         } else if ( rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DateCreated" ) ) ||
834                 rValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DateModified" ) ) ) {
835             // FIXME: should be able to set the timestamps
836             aRet[ n ] <<= getReadOnlyException( this );
837         } else {
838 #ifdef DEBUG
839             g_warning( "Unhandled property '%s'", OUStringToGnome( rValue.Name ) );
840 #endif
841             aRet[ n ] <<= getReadOnlyException( this );
842         }
843     }
844 
845     GnomeVFSResult result = GNOME_VFS_OK;
846 
847     if ( !m_bTransient &&
848          ( result = doSetFileInfo( &newInfo,
849                        (GnomeVFSSetFileInfoMask) setMask,
850                        xEnv ) ) != GNOME_VFS_OK ) {
851         for (int i = 0; i < nChanged; i++)
852             aRet[ i ] <<= mapVFSException( result, sal_True );
853 
854     }
855 
856     if ( result == GNOME_VFS_OK) {
857         gnome_vfs_file_info_copy( &m_info, &newInfo );
858 
859         if ( setMask & GNOME_VFS_SET_FILE_INFO_NAME ) {
860             uno::Reference< ucb::XContentIdentifier > xNewId
861                 = new ::ucbhelper::ContentIdentifier(
862                     m_xSMgr, makeNewURL( newInfo.name ) );
863 
864             aGuard.clear();
865             if (!exchangeIdentity( xNewId ) )
866                 aRet[ nTitlePos ] <<= uno::Exception
867                     ( rtl::OUString::createFromAscii( "Exchange failed!" ),
868                       static_cast< cppu::OWeakObject * >( this ) );
869         }
870     }
871 
872     gnome_vfs_file_info_clear( &newInfo );
873 
874     if ( nChanged > 0 ) {
875             aGuard.clear();
876         aChanges.realloc( nChanged );
877         notifyPropertiesChange( aChanges );
878     }
879 
880     return aRet;
881 }
882 
883 void Content::queryChildren( ContentRefList& rChildren )
884 {
885     // Obtain a list with a snapshot of all currently instanciated contents
886     // from provider and extract the contents which are direct children
887     // of this content.
888 
889     ::ucbhelper::ContentRefList aAllContents;
890     m_xProvider->queryExistingContents( aAllContents );
891 
892     rtl::OUString aURL = getOUURI();
893     sal_Int32 nURLPos = aURL.lastIndexOf( '/' );
894 
895     if ( nURLPos != ( aURL.getLength() - 1 ) )
896         aURL += rtl::OUString::createFromAscii( "/" );
897 
898     sal_Int32 nLen = aURL.getLength();
899 
900     ::ucbhelper::ContentRefList::const_iterator it  = aAllContents.begin();
901     ::ucbhelper::ContentRefList::const_iterator end = aAllContents.end();
902 
903     while ( it != end ) {
904         ::ucbhelper::ContentImplHelperRef xChild = (*it);
905         rtl::OUString aChildURL
906             = xChild->getIdentifier()->getContentIdentifier();
907 
908         // Is aURL a prefix of aChildURL?
909         if ( ( aChildURL.getLength() > nLen ) &&
910              ( aChildURL.compareTo( aURL, nLen ) == 0 ) ) {
911             sal_Int32 nPos = nLen;
912             nPos = aChildURL.indexOf( '/', nPos );
913 
914             if ( ( nPos == -1 ) ||
915                  ( nPos == ( aChildURL.getLength() - 1 ) ) ) {
916                 // No further slashes / only a final slash. It's a child!
917                 rChildren.push_back( ::gvfs::Content::ContentRef
918                              (static_cast< ::gvfs::Content * >(xChild.get() ) ) );
919             }
920         }
921         ++it;
922     }
923 }
924 
925 void Content::insert(
926         const uno::Reference< io::XInputStream >               &xInputStream,
927         sal_Bool                                                bReplaceExisting,
928         const uno::Reference< ucb::XCommandEnvironment > &xEnv )
929         throw( uno::Exception )
930 {
931     osl::ClearableGuard< osl::Mutex > aGuard( m_aMutex );
932 
933 #ifdef DEBUG
934     g_warning( "Insert '%s' (%d) (0x%x:%d)", getURI(), bReplaceExisting,
935            m_info.valid_fields, m_info.type );
936 #endif
937 
938     GnomeVFSResult result = getInfo( xEnv );
939     // a racy design indeed.
940     if( !bReplaceExisting && !m_bTransient &&
941         result != GNOME_VFS_ERROR_NOT_FOUND) {
942 #ifdef DEBUG
943         g_warning ("Nasty error inserting to '%s' ('%s')",
944                getURI(), gnome_vfs_result_to_string( result ));
945 #endif
946         cancelCommandExecution( GNOME_VFS_ERROR_FILE_EXISTS, xEnv, sal_True );
947     }
948 
949     if ( m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE &&
950          m_info.type == GNOME_VFS_FILE_TYPE_DIRECTORY ) {
951         ::rtl::OString aURI = getOURI();
952         int perm;
953 
954         perm = ( GNOME_VFS_PERM_USER_ALL |
955              GNOME_VFS_PERM_GROUP_READ |
956              GNOME_VFS_PERM_OTHER_READ );
957 
958 #ifdef DEBUG
959         g_warning ("Make directory");
960 #endif
961         result = gnome_vfs_make_directory( (const sal_Char *) aURI, perm );
962 
963         if( result != GNOME_VFS_OK )
964             cancelCommandExecution( result, xEnv, sal_True );
965 
966         return;
967     }
968 
969     if ( !xInputStream.is() ) {
970         // FIXME: slightly unclear whether to accept this and create an empty file
971         ucbhelper::cancelCommandExecution
972             ( uno::makeAny
973               ( ucb::MissingInputStreamException
974                 ( rtl::OUString(),
975                   static_cast< cppu::OWeakObject * >( this ) ) ),
976               xEnv );
977     }
978 
979     GnomeVFSHandle *handle = NULL;
980     ::rtl::OString aURI = getOURI();
981 
982     result = GNOME_VFS_OK;
983     if ( bReplaceExisting ) {
984         Authentication aAuth( xEnv );
985         result = gnome_vfs_open( &handle, (const sal_Char *)aURI,
986                      GNOME_VFS_OPEN_WRITE );
987     }
988 
989     if ( result != GNOME_VFS_OK ) {
990         int perm;
991         Authentication aAuth( xEnv );
992 
993         perm = ( ( GNOME_VFS_PERM_USER_WRITE | GNOME_VFS_PERM_USER_READ ) |
994              ( GNOME_VFS_PERM_GROUP_WRITE | GNOME_VFS_PERM_GROUP_READ ) );
995 
996         result = gnome_vfs_create
997             ( &handle, (const sal_Char *)aURI, GNOME_VFS_OPEN_WRITE, TRUE, perm );
998     }
999 
1000     if( result != GNOME_VFS_OK )
1001         cancelCommandExecution( result, xEnv, sal_True );
1002 
1003     if ( !xInputStream.is() ) {
1004         result = gnome_vfs_close( handle );
1005         if (result != GNOME_VFS_OK)
1006             cancelCommandExecution( result, xEnv, sal_True );
1007 
1008     } else { // copy it over
1009         uno::Reference < io::XOutputStream > xOutput =
1010             new gvfs::Stream( handle, &m_info );
1011 
1012         copyData( xInputStream, xOutput );
1013     }
1014 
1015     if (m_bTransient) {
1016         m_bTransient = sal_False;
1017         aGuard.clear();
1018         inserted();
1019     }
1020 }
1021 
1022 void Content::transfer(const ucb::TransferInfo & /*rArgs*/,
1023                const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1024     throw( uno::Exception )
1025 {
1026     // FIXME: see gnome-vfs-xfer.h - but we need to be able to easily
1027     // detect which are gnome-vfs owned URI types ...
1028     ucbhelper::cancelCommandExecution
1029         ( uno::makeAny
1030             ( ucb::InteractiveBadTransferURLException
1031                 ( rtl::OUString::createFromAscii( "Unsupported URL scheme!" ),
1032                   static_cast< cppu::OWeakObject * >( this ) ) ),
1033           xEnv );
1034 }
1035 
1036 void Content::destroy( sal_Bool bDeletePhysical )
1037     throw( uno::Exception )
1038 {
1039     // @@@ take care about bDeletePhysical -> trashcan support
1040     rtl::OUString aURL = getOUURI();
1041 
1042     uno::Reference< ucb::XContent > xThis = this;
1043 
1044     deleted();
1045 
1046     osl::Guard< osl::Mutex > aGuard( m_aMutex );
1047 
1048     // Process instanciated children...
1049     ::gvfs::Content::ContentRefList aChildren;
1050     queryChildren( aChildren );
1051 
1052     ContentRefList::const_iterator it  = aChildren.begin();
1053     ContentRefList::const_iterator end = aChildren.end();
1054 
1055     while ( it != end ) {
1056         (*it)->destroy( bDeletePhysical );
1057         ++it;
1058     }
1059 }
1060 
1061 // Used by the 'setPropertyValues' method for
1062 // propagating the renaming of a Content.
1063 sal_Bool Content::exchangeIdentity(
1064     const uno::Reference< ucb::XContentIdentifier >& xNewId )
1065 {
1066     if ( !xNewId.is() )
1067         return sal_False;
1068 
1069     uno::Reference< ucb::XContent > xThis = this;
1070 
1071 #ifdef DEBUG
1072     g_warning( "exchangeIdentity from '%s' to '%s'",
1073            getURI(), OUStringToGnome( xNewId->getContentIdentifier() ) );
1074 #endif
1075 
1076     if ( m_bTransient ) {
1077         osl::Guard< osl::Mutex > aGuard( m_aMutex );
1078         /* FIXME: can we not screw up an identically named
1079          * Content pointing to ourself here ? */
1080         m_xIdentifier = xNewId;
1081         return sal_False;
1082     }
1083 
1084     rtl::OUString aOldURL = getOUURI();
1085 
1086     // Exchange own identitity.
1087     if ( exchange( xNewId ) ) {
1088 
1089         // Process instanciated children...
1090         ContentRefList aChildren;
1091         queryChildren( aChildren );
1092 
1093         ContentRefList::const_iterator it  = aChildren.begin();
1094         ContentRefList::const_iterator end = aChildren.end();
1095 
1096         while ( it != end ) {
1097             ContentRef xChild = (*it);
1098 
1099             // Create new content identifier for the child...
1100             uno::Reference< ucb::XContentIdentifier >
1101                 xOldChildId = xChild->getIdentifier();
1102             rtl::OUString aOldChildURL
1103                 = xOldChildId->getContentIdentifier();
1104             rtl::OUString aNewChildURL
1105                 = aOldChildURL.replaceAt(
1106                              0,
1107                              aOldURL.getLength(),
1108                              xNewId->getContentIdentifier() );
1109             uno::Reference< ucb::XContentIdentifier >
1110                 xNewChildId
1111                 = new ::ucbhelper::ContentIdentifier( m_xSMgr, aNewChildURL );
1112 
1113             if ( !xChild->exchangeIdentity( xNewChildId ) )
1114                 return sal_False;
1115 
1116             ++it;
1117         }
1118         return sal_True;
1119     }
1120 
1121     return sal_False;
1122 }
1123 
1124 GnomeVFSResult
1125 Content::getInfo( const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1126 {
1127     GnomeVFSResult result;
1128         osl::Guard< osl::Mutex > aGuard( m_aMutex );
1129 
1130     if (m_bTransient)
1131         result = GNOME_VFS_OK;
1132 
1133     else if ( !m_info.valid_fields ) {
1134         ::rtl::OString aURI = getOURI();
1135         Authentication aAuth( xEnv );
1136         result = gnome_vfs_get_file_info
1137             ( (const sal_Char *)aURI, &m_info, GNOME_VFS_FILE_INFO_DEFAULT );
1138         if (result != GNOME_VFS_OK)
1139             gnome_vfs_file_info_clear( &m_info );
1140     } else
1141         result = GNOME_VFS_OK;
1142 #ifdef DEBUG
1143     g_warning( "getInfo on '%s' returns '%s' (%d) (0x%x)",
1144            getURI(), gnome_vfs_result_to_string( result ),
1145            result, m_info.valid_fields );
1146 #endif
1147     return result;
1148 }
1149 
1150 sal_Bool
1151 Content::isFolder(const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1152 {
1153         osl::Guard< osl::Mutex > aGuard( m_aMutex );
1154     getInfo( xEnv );
1155     return (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE &&
1156         m_info.type == GNOME_VFS_FILE_TYPE_DIRECTORY);
1157 }
1158 
1159 uno::Any Content::mapVFSException( const GnomeVFSResult result, sal_Bool bWrite )
1160 {
1161     uno::Any aException;
1162     const char *gvfs_message;
1163     rtl::OUString message;
1164     uno::Sequence< uno::Any > aArgs( 1 );
1165 
1166 #ifdef DEBUG
1167     g_warning ("Map VFS exception '%s' (%d)",
1168            gnome_vfs_result_to_string( result ), result );
1169 #endif
1170 
1171     if ((gvfs_message = gnome_vfs_result_to_string (result)))
1172         message = GnomeToOUString( gvfs_message );
1173 
1174     switch (result) {
1175     case GNOME_VFS_OK:
1176         g_warning("VFS_OK mapped to exception.");
1177         break;
1178     case GNOME_VFS_ERROR_EOF:
1179         g_warning ("VFS_EOF not handled somewhere.");
1180         break;
1181     case GNOME_VFS_ERROR_NOT_FOUND:
1182         aArgs[ 0 ] <<= m_xIdentifier->getContentIdentifier();
1183         aException <<=
1184             ucb::InteractiveAugmentedIOException
1185             ( rtl::OUString::createFromAscii( "Not found!" ),
1186               static_cast< cppu::OWeakObject * >( this ),
1187               task::InteractionClassification_ERROR,
1188               ucb::IOErrorCode_NOT_EXISTING,
1189               aArgs );
1190         break;
1191     case GNOME_VFS_ERROR_BAD_PARAMETERS:
1192         aException <<=
1193             lang::IllegalArgumentException
1194                 ( rtl::OUString(),
1195                   static_cast< cppu::OWeakObject * >( this ),
1196                   -1 );
1197         break;
1198     case GNOME_VFS_ERROR_GENERIC:
1199     case GNOME_VFS_ERROR_INTERNAL:
1200     case GNOME_VFS_ERROR_NOT_SUPPORTED:
1201 #ifdef DEBUG
1202         g_warning ("Internal - un-mapped error");
1203 #endif
1204         aException <<= io::IOException();
1205         break;
1206     case GNOME_VFS_ERROR_IO:
1207         if ( bWrite )
1208             aException <<=
1209                 ucb::InteractiveNetworkWriteException
1210                 ( rtl::OUString(),
1211                   static_cast< cppu::OWeakObject * >( this ),
1212                   task::InteractionClassification_ERROR,
1213                   message );
1214         else
1215             aException <<=
1216                 ucb::InteractiveNetworkReadException
1217                 ( rtl::OUString(),
1218                   static_cast< cppu::OWeakObject * >( this ),
1219                   task::InteractionClassification_ERROR,
1220                   message );
1221         break;
1222     case GNOME_VFS_ERROR_HOST_NOT_FOUND:
1223     case GNOME_VFS_ERROR_INVALID_HOST_NAME:
1224         aException <<=
1225             ucb::InteractiveNetworkResolveNameException
1226             ( rtl::OUString(),
1227               static_cast< cppu::OWeakObject * >( this ),
1228               task::InteractionClassification_ERROR,
1229               message );
1230         break;
1231     case GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE:
1232     case GNOME_VFS_ERROR_SERVICE_OBSOLETE:
1233     case GNOME_VFS_ERROR_PROTOCOL_ERROR:
1234     case GNOME_VFS_ERROR_NO_MASTER_BROWSER:
1235         aException <<=
1236             ucb::InteractiveNetworkConnectException
1237                 ( rtl::OUString(),
1238                   static_cast< cppu::OWeakObject * >( this ),
1239                   task::InteractionClassification_ERROR,
1240                   message );
1241         break;
1242 
1243     case GNOME_VFS_ERROR_FILE_EXISTS:
1244         aException <<= ucb::NameClashException
1245                 ( rtl::OUString(),
1246                   static_cast< cppu::OWeakObject * >( this ),
1247                   task::InteractionClassification_ERROR,
1248                   message );
1249         break;
1250 
1251     case GNOME_VFS_ERROR_INVALID_OPEN_MODE:
1252         aException <<= ucb::UnsupportedOpenModeException();
1253         break;
1254 
1255     case GNOME_VFS_ERROR_CORRUPTED_DATA:
1256     case GNOME_VFS_ERROR_WRONG_FORMAT:
1257     case GNOME_VFS_ERROR_BAD_FILE:
1258     case GNOME_VFS_ERROR_TOO_BIG:
1259     case GNOME_VFS_ERROR_NO_SPACE:
1260     case GNOME_VFS_ERROR_READ_ONLY:
1261     case GNOME_VFS_ERROR_INVALID_URI:
1262     case GNOME_VFS_ERROR_NOT_OPEN:
1263     case GNOME_VFS_ERROR_ACCESS_DENIED:
1264     case GNOME_VFS_ERROR_TOO_MANY_OPEN_FILES:
1265     case GNOME_VFS_ERROR_NOT_A_DIRECTORY:
1266     case GNOME_VFS_ERROR_IN_PROGRESS:
1267     case GNOME_VFS_ERROR_INTERRUPTED:
1268     case GNOME_VFS_ERROR_LOOP:
1269     case GNOME_VFS_ERROR_NOT_PERMITTED:
1270     case GNOME_VFS_ERROR_IS_DIRECTORY:
1271     case GNOME_VFS_ERROR_NO_MEMORY:
1272     case GNOME_VFS_ERROR_HOST_HAS_NO_ADDRESS:
1273     case GNOME_VFS_ERROR_LOGIN_FAILED:
1274     case GNOME_VFS_ERROR_CANCELLED:
1275     case GNOME_VFS_ERROR_DIRECTORY_BUSY:
1276     case GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY:
1277     case GNOME_VFS_ERROR_TOO_MANY_LINKS:
1278     case GNOME_VFS_ERROR_READ_ONLY_FILE_SYSTEM:
1279     case GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM:
1280     case GNOME_VFS_ERROR_NAME_TOO_LONG:
1281 #ifdef DEBUG
1282         g_warning( "FIXME: Un-mapped VFS exception '%s' (%d)",
1283                gnome_vfs_result_to_string( result ), result );
1284 #endif
1285     default:
1286         aException <<= ucb::InteractiveNetworkGeneralException
1287             ( rtl::OUString(),
1288               static_cast< cppu::OWeakObject * >( this ),
1289               task::InteractionClassification_ERROR );
1290         break;
1291     }
1292 
1293     return aException;
1294 }
1295 
1296 void Content::cancelCommandExecution(
1297     GnomeVFSResult result,
1298     const uno::Reference< ucb::XCommandEnvironment > & xEnv,
1299     sal_Bool bWrite /* = sal_False */ )
1300     throw ( uno::Exception )
1301 {
1302     ucbhelper::cancelCommandExecution( mapVFSException( result, bWrite ), xEnv );
1303     // Unreachable
1304 }
1305 
1306 uno::Sequence< beans::Property > Content::getProperties(
1307     const uno::Reference< ucb::XCommandEnvironment > & /*xEnv*/ )
1308 {
1309     static const beans::Property aGenericProperties[] = {
1310                 beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ContentType" ) ),
1311                  -1, getCppuType( static_cast< const rtl::OUString * >( 0 ) ),
1312                  beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1313                 beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsDocument" ) ),
1314                  -1, getCppuBooleanType(),
1315                  beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1316                 beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsFolder" ) ),
1317                  -1, getCppuBooleanType(),
1318                  beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1319                 beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Title" ) ),
1320                  -1, getCppuType( static_cast< const rtl::OUString * >( 0 ) ),
1321                  beans::PropertyAttribute::BOUND ),
1322         // Optional ...
1323         beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DateCreated" ) ),
1324                  -1, getCppuType( static_cast< const util::DateTime * >( 0 ) ),
1325                  beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1326                 beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DateModified" ) ),
1327                  -1, getCppuType( static_cast< const util::DateTime * >( 0 ) ),
1328                  beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1329 // FIXME: Too expensive for now (?)
1330 //                beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ),
1331 //                 -1, getCppuType( static_cast< const rtl::OUString * >( 0 ) ),
1332 //                 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1333                 beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Size" ) ),
1334                  -1, getCppuType( static_cast< const sal_Int64 * >( 0 ) ),
1335                  beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1336                 beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsReadOnly" ) ),
1337                  -1, getCppuBooleanType(),
1338                  beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1339                 beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsVolume" ) ),
1340                  -1, getCppuBooleanType(),
1341                  beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1342                 beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsCompactDisk" ) ),
1343                  -1, getCppuBooleanType(),
1344                  beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1345                 beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsHidden" ) ),
1346                  -1, getCppuBooleanType(),
1347                  beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1348                 beans::Property( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CreatableContentsInfo" ) ),
1349                  -1, getCppuType( static_cast< const uno::Sequence< ucb::ContentInfo > * >( 0 ) ),
1350                  beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY )
1351     };
1352 
1353     const int nProps = sizeof (aGenericProperties) / sizeof (aGenericProperties[0]);
1354 
1355     return uno::Sequence< beans::Property > ( aGenericProperties, nProps );
1356 
1357 }
1358 
1359 uno::Sequence< ucb::CommandInfo > Content::getCommands(
1360     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
1361 {
1362     static ucb::CommandInfo aCommandInfoTable[] = {
1363         // Required commands
1364         ucb::CommandInfo
1365         ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "getCommandInfo" ) ),
1366           -1, getCppuVoidType() ),
1367         ucb::CommandInfo
1368         ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "getPropertySetInfo" ) ),
1369           -1, getCppuVoidType() ),
1370         ucb::CommandInfo
1371         ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "getPropertyValues" ) ),
1372           -1, getCppuType( static_cast<uno::Sequence< beans::Property > * >( 0 ) ) ),
1373         ucb::CommandInfo
1374         ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "setPropertyValues" ) ),
1375           -1, getCppuType( static_cast<uno::Sequence< beans::PropertyValue > * >( 0 ) ) ),
1376 
1377         // Optional standard commands
1378         ucb::CommandInfo
1379         ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "delete" ) ),
1380           -1, getCppuBooleanType() ),
1381         ucb::CommandInfo
1382         ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "insert" ) ),
1383           -1, getCppuType( static_cast<ucb::InsertCommandArgument * >( 0 ) ) ),
1384         ucb::CommandInfo
1385         ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "open" ) ),
1386           -1, getCppuType( static_cast<ucb::OpenCommandArgument2 * >( 0 ) ) ),
1387 
1388         // Folder Only, omitted if not a folder
1389         ucb::CommandInfo
1390         ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "transfer" ) ),
1391           -1, getCppuType( static_cast<ucb::TransferInfo * >( 0 ) ) ),
1392         ucb::CommandInfo
1393         ( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "createNewContent" ) ),
1394           -1, getCppuType( static_cast<ucb::ContentInfo * >( 0 ) ) )
1395     };
1396 
1397     const int nProps
1398         = sizeof( aCommandInfoTable ) / sizeof( aCommandInfoTable[ 0 ] );
1399     return uno::Sequence< ucb::CommandInfo >(
1400         aCommandInfoTable, isFolder( xEnv ) ? nProps : nProps - 2 );
1401 }
1402 
1403 rtl::OUString
1404 Content::getOUURI ()
1405 {
1406         osl::Guard< osl::Mutex > aGuard( m_aMutex );
1407     return m_xIdentifier->getContentIdentifier();
1408 }
1409 
1410 rtl::OString
1411 Content::getOURI ()
1412 {
1413     return rtl::OUStringToOString( getOUURI(), RTL_TEXTENCODING_UTF8 );
1414 }
1415 
1416 char *
1417 Content::getURI ()
1418 {
1419     return OUStringToGnome( getOUURI() );
1420 }
1421 
1422 void
1423 Content::copyData( uno::Reference< io::XInputStream > xIn,
1424                    uno::Reference< io::XOutputStream > xOut )
1425 {
1426     uno::Sequence< sal_Int8 > theData( TRANSFER_BUFFER_SIZE );
1427 
1428     g_return_if_fail( xIn.is() && xOut.is() );
1429 
1430     while ( xIn->readBytes( theData, TRANSFER_BUFFER_SIZE ) > 0 )
1431         xOut->writeBytes( theData );
1432 
1433     xOut->closeOutput();
1434 }
1435 
1436 // Inherits an authentication context
1437 uno::Reference< io::XInputStream >
1438 Content::createTempStream(
1439     const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1440         throw( uno::Exception )
1441 {
1442     GnomeVFSResult result;
1443     GnomeVFSHandle *handle = NULL;
1444     ::rtl::OString aURI = getOURI();
1445 
1446         osl::Guard< osl::Mutex > aGuard( m_aMutex );
1447     // Something badly wrong happened - can't seek => stream to a temporary file
1448     const rtl::OUString sServiceName ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.io.TempFile" ) );
1449     uno::Reference < io::XOutputStream > xTempOut =
1450         uno::Reference < io::XOutputStream >
1451             ( m_xSMgr->createInstance( sServiceName ), uno::UNO_QUERY );
1452 
1453     if ( !xTempOut.is() )
1454         cancelCommandExecution( GNOME_VFS_ERROR_IO, xEnv );
1455 
1456     result = gnome_vfs_open
1457         ( &handle, (const sal_Char *)aURI, GNOME_VFS_OPEN_READ );
1458     if (result != GNOME_VFS_OK)
1459         cancelCommandExecution( result, xEnv );
1460 
1461     uno::Reference < io::XInputStream > pStream = new ::gvfs::Stream( handle, &m_info );
1462     copyData( pStream, xTempOut );
1463 
1464     return uno::Reference < io::XInputStream > ( xTempOut, uno::UNO_QUERY );
1465 }
1466 
1467 uno::Reference< io::XInputStream >
1468 Content::createInputStream(
1469     const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1470         throw( uno::Exception )
1471 {
1472     GnomeVFSHandle *handle = NULL;
1473     GnomeVFSResult  result;
1474     uno::Reference<io::XInputStream > xIn;
1475 
1476     Authentication aAuth( xEnv );
1477         osl::Guard< osl::Mutex > aGuard( m_aMutex );
1478 
1479     getInfo( xEnv );
1480     ::rtl::OString aURI = getOURI();
1481 
1482     if ( !(m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE) )
1483         return createTempStream( xEnv );
1484 
1485     result = gnome_vfs_open
1486         ( &handle, (const sal_Char *)aURI,
1487           (GnomeVFSOpenMode) (GNOME_VFS_OPEN_READ | GNOME_VFS_OPEN_RANDOM ) );
1488 
1489     if (result == GNOME_VFS_ERROR_INVALID_OPEN_MODE ||
1490         result == GNOME_VFS_ERROR_NOT_SUPPORTED)
1491         return createTempStream( xEnv );
1492 
1493     if (result != GNOME_VFS_OK)
1494         cancelCommandExecution( result, xEnv );
1495 
1496     // Try a seek just to make sure it's Random access: some lie.
1497     result = gnome_vfs_seek( handle, GNOME_VFS_SEEK_START, 0);
1498     if (result == GNOME_VFS_ERROR_NOT_SUPPORTED) {
1499         gnome_vfs_close( handle );
1500         return createTempStream( xEnv );
1501     }
1502 
1503     if (result != GNOME_VFS_OK)
1504         cancelCommandExecution( result, xEnv );
1505 
1506     if (handle != NULL)
1507         xIn = new ::gvfs::Stream( handle, &m_info );
1508 
1509     return xIn;
1510 }
1511 
1512 sal_Bool
1513 Content::feedSink( uno::Reference< uno::XInterface > aSink,
1514                    const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1515 {
1516     if ( !aSink.is() )
1517         return sal_False;
1518 
1519     uno::Reference< io::XOutputStream > xOut
1520         = uno::Reference< io::XOutputStream >(aSink, uno::UNO_QUERY );
1521     uno::Reference< io::XActiveDataSink > xDataSink
1522         = uno::Reference< io::XActiveDataSink >(aSink, uno::UNO_QUERY );
1523 
1524     if ( !xOut.is() && !xDataSink.is() )
1525         return sal_False;
1526 
1527     uno::Reference< io::XInputStream > xIn = createInputStream( xEnv );
1528     if ( !xIn.is() )
1529         return sal_False;
1530 
1531     if ( xOut.is() )
1532         copyData( xIn, xOut );
1533 
1534     if ( xDataSink.is() )
1535         xDataSink->setInputStream( xIn );
1536 
1537     return sal_True;
1538 }
1539 
1540 extern "C" {
1541 
1542 #ifndef GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION
1543 #  error "We require Gnome VFS 2.6.x to compile (will run fine with < 2.6)"
1544 #endif
1545 
1546     static void
1547     vfs_authentication_callback (gconstpointer in_void,
1548                      gsize         in_size,
1549                      gpointer      out_void,
1550                      gsize         out_size,
1551                      gpointer      callback_data)
1552     {
1553         task::XInteractionHandler *xIH;
1554 
1555 #ifdef DEBUG
1556         g_warning ("Full authentication callback (%p) ...", callback_data);
1557 #endif
1558 
1559         if( !( xIH = (task::XInteractionHandler *) callback_data ) )
1560             return;
1561 
1562         const GnomeVFSModuleCallbackFullAuthenticationIn *in =
1563             (const GnomeVFSModuleCallbackFullAuthenticationIn *) in_void;
1564         GnomeVFSModuleCallbackFullAuthenticationOut *out =
1565             (GnomeVFSModuleCallbackFullAuthenticationOut *) out_void;
1566 
1567         g_return_if_fail (in != NULL && out != NULL);
1568         g_return_if_fail (sizeof (GnomeVFSModuleCallbackFullAuthenticationIn) == in_size &&
1569                   sizeof (GnomeVFSModuleCallbackFullAuthenticationOut) == out_size);
1570 
1571 #ifdef DEBUG
1572 #  define NNIL(x) (x?x:"<Null>")
1573         g_warning (" InComing data 0x%x uri '%s' prot '%s' server '%s' object '%s' "
1574                    "port %d auth_t '%s' user '%s' domain '%s' "
1575                    "def user '%s', def domain '%s'",
1576                    (int) in->flags, NNIL(in->uri), NNIL(in->protocol),
1577                    NNIL(in->server), NNIL(in->object),
1578                    (int) in->port, NNIL(in->authtype), NNIL(in->username), NNIL(in->domain),
1579                    NNIL(in->default_user), NNIL(in->default_domain));
1580 #  undef NNIL
1581 #endif
1582 
1583         ucbhelper::SimpleAuthenticationRequest::EntityType
1584                                    eDomain, eUserName, ePassword;
1585         ::rtl::OUString aHostName, aDomain, aUserName, aPassword;
1586 
1587         aHostName = GnomeToOUString( in->server );
1588 
1589         if (in->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_DOMAIN)
1590         {
1591             aDomain = GnomeToOUString( in->domain );
1592             eDomain = ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY;
1593             if (!aDomain.getLength())
1594                 aDomain = GnomeToOUString( in->default_domain );
1595         }
1596         else // no underlying capability to display realm otherwise
1597             eDomain = ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
1598 
1599         aUserName = GnomeToOUString( in->username );
1600         if (!aUserName.getLength())
1601             aUserName = GnomeToOUString( in->default_user );
1602         eUserName = (in->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_USERNAME) ?
1603             ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY :
1604                 (aUserName.getLength() ?
1605                     ucbhelper::SimpleAuthenticationRequest::ENTITY_FIXED :
1606                     ucbhelper::SimpleAuthenticationRequest::ENTITY_NA);
1607 
1608         // No suggested password.
1609         ePassword = (in->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_PASSWORD) ?
1610             ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY :
1611             ucbhelper::SimpleAuthenticationRequest::ENTITY_FIXED;
1612 
1613         // Really, really bad things happen if we don't provide
1614         // the same user/password as was entered last time if
1615         // we failed to authenticate - infinite looping / flickering
1616         // madness etc. [ nice infrastructure ! ]
1617         static rtl::OUString aLastUserName, aLastPassword;
1618         if (in->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_PREVIOUS_ATTEMPT_FAILED)
1619         {
1620             osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
1621             aUserName = aLastUserName;
1622             aPassword = aLastPassword;
1623         }
1624 
1625         rtl::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
1626             = new ucbhelper::SimpleAuthenticationRequest (GnomeToOUString(in->uri),
1627                                                           aHostName, eDomain, aDomain,
1628                                                           eUserName, aUserName,
1629                                                           ePassword, aPassword);
1630 
1631         xIH->handle( xRequest.get() );
1632 
1633         rtl::Reference< ucbhelper::InteractionContinuation > xSelection
1634             = xRequest->getSelection();
1635 
1636         if ( xSelection.is() ) {
1637             // Handler handled the request.
1638             uno::Reference< task::XInteractionAbort > xAbort(xSelection.get(), uno::UNO_QUERY );
1639             if ( !xAbort.is() ) {
1640                 const rtl::Reference<
1641                     ucbhelper::InteractionSupplyAuthentication > & xSupp
1642                     = xRequest->getAuthenticationSupplier();
1643 
1644                 aUserName = xSupp->getUserName();
1645                 aDomain   = xSupp->getRealm();
1646                 aPassword = xSupp->getPassword();
1647 
1648                 {
1649                     osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
1650                     aLastUserName = aUserName;
1651                     aLastPassword = aPassword;
1652                 }
1653 
1654                 out->username = OUStringToGnome( aUserName );
1655                 out->domain   = OUStringToGnome( aDomain );
1656                 out->password = OUStringToGnome( aPassword );
1657                 out->save_password = xSupp->getRememberPasswordMode();
1658 
1659 #ifdef DEBUG
1660                 g_warning ("Got valid user/domain/password '%s' '%s' '%s', %s password",
1661                            out->username, out->domain, out->password,
1662                            out->save_password ? "save" : "don't save");
1663 #endif
1664             }
1665             else
1666                 out->abort_auth = TRUE;
1667         }
1668         else
1669             out->abort_auth = TRUE;
1670     }
1671 
1672     static void
1673     vfs_authentication_old_callback (gconstpointer in_void,
1674                                      gsize         in_size,
1675                                      gpointer      out_void,
1676                                      gsize         out_size,
1677                                      gpointer      callback_data)
1678     {
1679 #ifdef DEBUG
1680         g_warning ("Old authentication callback (%p) [ UNTESTED ] ...", callback_data);
1681 #endif
1682         const GnomeVFSModuleCallbackAuthenticationIn *in =
1683             (const GnomeVFSModuleCallbackAuthenticationIn *) in_void;
1684         GnomeVFSModuleCallbackAuthenticationOut *out =
1685             (GnomeVFSModuleCallbackAuthenticationOut *) out_void;
1686 
1687         g_return_if_fail (in != NULL && out != NULL);
1688         g_return_if_fail (sizeof (GnomeVFSModuleCallbackAuthenticationIn) == in_size &&
1689                   sizeof (GnomeVFSModuleCallbackAuthenticationOut) == out_size);
1690 
1691         GnomeVFSModuleCallbackFullAuthenticationIn mapped_in = {
1692                 (GnomeVFSModuleCallbackFullAuthenticationFlags)
1693                 (GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_PASSWORD |
1694                  GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_USERNAME |
1695                  GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_DOMAIN),
1696                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1697         GnomeVFSModuleCallbackFullAuthenticationOut mapped_out = { 0, 0, 0, 0, 0, 0, 0, 0 };
1698 
1699         // Map the old style input auth. data to the new style structure.
1700         if (in->previous_attempt_failed)
1701             mapped_in.flags = (GnomeVFSModuleCallbackFullAuthenticationFlags)
1702                 (mapped_in.flags |
1703                  GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_PREVIOUS_ATTEMPT_FAILED);
1704 
1705         GnomeVFSURI *pURI = NULL;
1706         // Urk - parse all this from the URL ...
1707         mapped_in.uri = in->uri;
1708         if (in->uri)
1709         {
1710             pURI = gnome_vfs_uri_new( in->uri );
1711             mapped_in.protocol = (char *) gnome_vfs_uri_get_scheme (pURI);
1712             mapped_in.server   = (char *) gnome_vfs_uri_get_host_name (pURI);
1713             mapped_in.port     = gnome_vfs_uri_get_host_port (pURI);
1714             mapped_in.username = (char *) gnome_vfs_uri_get_user_name (pURI);
1715         }
1716         mapped_in.domain = in->realm;
1717         mapped_in.default_user = mapped_in.username;
1718         mapped_in.default_domain = mapped_in.domain;
1719 
1720         vfs_authentication_callback ((gconstpointer) &mapped_in,
1721                                      sizeof (mapped_in),
1722                                      (gpointer) &mapped_out,
1723                                      sizeof (mapped_out),
1724                                      callback_data);
1725 
1726         if (pURI)
1727             gnome_vfs_uri_unref (pURI);
1728 
1729         // Map the new style auth. out data to the old style out structure.
1730         out->username = mapped_out.username;
1731         out->password = mapped_out.password;
1732         g_free (mapped_out.domain);
1733         g_free (mapped_out.keyring);
1734     }
1735 
1736 
1737     static void
1738     auth_destroy (gpointer data)
1739     {
1740         task::XInteractionHandler *xIH;
1741         if( ( xIH = ( task::XInteractionHandler * )data ) )
1742             xIH->release();
1743     }
1744 
1745     // This sucks, but gnome-vfs doesn't much like
1746     // repeated set / unsets - so we have to compensate.
1747     GPrivate *auth_queue = NULL;
1748 
1749     void auth_queue_destroy( gpointer data )
1750     {
1751         GList  *l;
1752         GQueue *vq = (GQueue *) data;
1753 
1754         for (l = vq->head; l; l = l->next)
1755             auth_destroy (l->data);
1756         g_queue_free (vq);
1757     }
1758 }
1759 
1760 static void
1761 refresh_auth( GQueue *vq )
1762 {
1763     GList *l;
1764 
1765     gnome_vfs_module_callback_pop( GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION );
1766     gnome_vfs_module_callback_pop( GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION );
1767 
1768     for (l = vq->head; l; l = l->next) {
1769         if (l->data) {
1770             gnome_vfs_module_callback_push
1771                 ( GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION,
1772                   vfs_authentication_old_callback, l->data, NULL );
1773             gnome_vfs_module_callback_push
1774                 ( GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION,
1775                   vfs_authentication_callback, l->data, NULL );
1776             break;
1777         }
1778     }
1779 }
1780 
1781 gvfs::Authentication::Authentication(
1782     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
1783 {
1784     GQueue *vq;
1785     uno::Reference< task::XInteractionHandler > xIH;
1786 
1787     if ( xEnv.is() )
1788         xIH = xEnv->getInteractionHandler();
1789 
1790     if ( xIH.is() )
1791         xIH->acquire();
1792 
1793     if( !(vq = (GQueue *)g_private_get( auth_queue ) ) ) {
1794         vq = g_queue_new();
1795         g_private_set( auth_queue, vq );
1796     }
1797 
1798     g_queue_push_head( vq, (gpointer) xIH.get() );
1799     refresh_auth( vq );
1800 }
1801 
1802 gvfs::Authentication::~Authentication()
1803 {
1804     GQueue *vq;
1805     gpointer data;
1806 
1807     vq = (GQueue *)g_private_get( auth_queue );
1808 
1809     data = g_queue_pop_head( vq );
1810     auth_destroy (data);
1811 
1812     refresh_auth( vq );
1813 }
1814