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