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