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