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