Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / ucb / source / ucp / gvfs / gvfs_directory.cxx
blob9ca0c2382e44fb36c302fa449cbce6030c850d0f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 * This file pinched from webdavdatasupplier (etc.)
22 * cut & paste + new getData impl. & collate ResultSet code.
24 #include <vector>
25 #include <osl/diagnose.h>
26 #include <com/sun/star/ucb/OpenMode.hpp>
27 #include <ucbhelper/contentidentifier.hxx>
28 #include <ucbhelper/providerhelper.hxx>
30 #include "gvfs_directory.hxx"
32 #include <libgnomevfs/gnome-vfs-utils.h>
33 #include <libgnomevfs/gnome-vfs-directory.h>
34 #include <comphelper/processfactory.hxx>
36 using namespace com::sun::star;
37 using namespace gvfs;
39 // DynamicResultSet Implementation.
41 DynamicResultSet::DynamicResultSet(
42 const uno::Reference< uno::XComponentContext >& rxContext,
43 const rtl::Reference< Content >& rxContent,
44 const ucb::OpenCommandArgument2& rCommand,
45 const uno::Reference< ucb::XCommandEnvironment >& rxEnv )
46 : ResultSetImplHelper( rxContext, rCommand ),
47 m_xContent( rxContent ),
48 m_xEnv( rxEnv )
51 void DynamicResultSet::initStatic()
53 m_xResultSet1
54 = new ::ucbhelper::ResultSet( m_xContext,
55 m_aCommand.Properties,
56 new DataSupplier( m_xContent,
57 m_aCommand.Mode ),
58 m_xEnv );
60 void DynamicResultSet::initDynamic()
62 initStatic();
63 m_xResultSet2 = m_xResultSet1;
66 //=========================================================================
69 // DataSupplier Implementation.
73 struct ResultListEntry
75 rtl::OUString aId;
76 uno::Reference< ucb::XContentIdentifier > xId;
77 uno::Reference< ucb::XContent > xContent;
78 uno::Reference< sdbc::XRow > xRow;
79 GnomeVFSFileInfo aInfo;
81 ResultListEntry( const GnomeVFSFileInfo *fileInfo)
83 gnome_vfs_file_info_copy (&aInfo, fileInfo);
86 ~ResultListEntry()
88 gnome_vfs_file_info_clear (&aInfo);
92 //=========================================================================
94 // ResultList.
96 //=========================================================================
98 typedef std::vector< ResultListEntry* > ResultList;
100 //=========================================================================
102 // struct DataSupplier_Impl.
104 //=========================================================================
106 struct gvfs::DataSupplier_Impl
108 osl::Mutex m_aMutex;
109 ResultList m_aResults;
110 rtl::Reference< Content > m_xContent;
111 sal_Int32 m_nOpenMode;
112 sal_Bool m_bCountFinal;
114 DataSupplier_Impl(
115 const rtl::Reference< Content >& rContent,
116 sal_Int32 nOpenMode )
117 : m_xContent( rContent ),
118 m_nOpenMode( nOpenMode ), m_bCountFinal( sal_False ) {}
119 ~DataSupplier_Impl()
121 ResultList::const_iterator it = m_aResults.begin();
122 ResultList::const_iterator end = m_aResults.end();
124 while ( it != end )
126 delete (*it);
127 ++it;
132 DataSupplier::DataSupplier(
133 const rtl::Reference< Content >& rContent,
134 sal_Int32 nOpenMode )
135 : m_pImpl( new DataSupplier_Impl( rContent, nOpenMode ) )
139 //=========================================================================
140 // virtual
141 DataSupplier::~DataSupplier()
143 delete m_pImpl;
146 // virtual
147 rtl::OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
149 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
151 if ( nIndex < m_pImpl->m_aResults.size() ) {
152 rtl::OUString aId = m_pImpl->m_aResults[ nIndex ]->aId;
153 if ( !aId.isEmpty() ) // cached
154 return aId;
157 if ( getResult( nIndex ) ) {
158 rtl::OUString aId = m_pImpl->m_xContent->getOUURI();
160 char *escaped_name;
161 escaped_name = gnome_vfs_escape_string( m_pImpl->m_aResults[ nIndex ]->aInfo.name );
163 if ( ( aId.lastIndexOf( '/' ) + 1 ) != aId.getLength() )
164 aId += rtl::OUString("/");
166 aId += rtl::OUString::createFromAscii( escaped_name );
168 g_free( escaped_name );
170 m_pImpl->m_aResults[ nIndex ]->aId = aId;
171 return aId;
174 return rtl::OUString();
177 // virtual
178 uno::Reference< ucb::XContentIdentifier >
179 DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
181 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
183 if ( nIndex < m_pImpl->m_aResults.size() ) {
184 uno::Reference< ucb::XContentIdentifier > xId
185 = m_pImpl->m_aResults[ nIndex ]->xId;
186 if ( xId.is() ) // Already cached.
187 return xId;
190 rtl::OUString aId = queryContentIdentifierString( nIndex );
191 if ( !aId.isEmpty() ) {
192 uno::Reference< ucb::XContentIdentifier > xId
193 = new ::ucbhelper::ContentIdentifier( aId );
194 m_pImpl->m_aResults[ nIndex ]->xId = xId;
195 return xId;
198 return uno::Reference< ucb::XContentIdentifier >();
201 // virtual
202 uno::Reference< ucb::XContent >
203 DataSupplier::queryContent( sal_uInt32 nIndex )
205 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
207 if ( nIndex < m_pImpl->m_aResults.size() ) {
208 uno::Reference< ucb::XContent > xContent
209 = m_pImpl->m_aResults[ nIndex ]->xContent;
210 if ( xContent.is() ) // Already cached.
211 return xContent;
214 uno::Reference< ucb::XContentIdentifier > xId
215 = queryContentIdentifier( nIndex );
216 if ( xId.is() ) {
219 // FIXME:
220 // It would be really nice to propagate this information
221 // to the Content, but we can't then register it with the
222 // ContentProvider, and the ucbhelper hinders here.
223 uno::Reference< ucb::XContent > xContent
224 = m_pImpl->m_xContent->getProvider()->queryContent( xId );
225 m_pImpl->m_aResults[ nIndex ]->xContent = xContent;
226 return xContent;
229 catch ( ucb::IllegalIdentifierException& ) {
232 return uno::Reference< ucb::XContent >();
235 // virtual
236 sal_Bool DataSupplier::getResult( sal_uInt32 nIndex )
238 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
240 if ( m_pImpl->m_aResults.size() > nIndex ) // Result already present.
241 return sal_True;
243 if ( getData() && m_pImpl->m_aResults.size() > nIndex )
244 return sal_True;
246 return sal_False;
249 // virtual
250 sal_uInt32 DataSupplier::totalCount()
252 getData();
254 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
256 return m_pImpl->m_aResults.size();
259 // virtual
260 sal_uInt32 DataSupplier::currentCount()
262 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
263 return m_pImpl->m_aResults.size();
266 // virtual
267 sal_Bool DataSupplier::isCountFinal()
269 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
270 return m_pImpl->m_bCountFinal;
273 // virtual
274 uno::Reference< sdbc::XRow > DataSupplier::queryPropertyValues( sal_uInt32 nIndex )
276 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
278 if ( nIndex < m_pImpl->m_aResults.size() ) {
279 uno::Reference< sdbc::XRow > xRow = m_pImpl->m_aResults[ nIndex ]->xRow;
280 if ( xRow.is() ) // Already cached.
281 return xRow;
284 if ( getResult( nIndex ) ) {
285 // Inefficient - but we can't create xContent's sensibly
286 // nor can we do the property code sensibly cleanly staticaly.
287 Content *pContent = static_cast< ::gvfs::Content * >(queryContent( nIndex ).get());
289 uno::Reference< sdbc::XRow > xRow =
290 pContent->getPropertyValues( getResultSet()->getProperties(),
291 getResultSet()->getEnvironment() );
293 m_pImpl->m_aResults[ nIndex ]->xRow = xRow;
295 return xRow;
298 return uno::Reference< sdbc::XRow >();
301 // virtual
302 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
304 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
306 if ( nIndex < m_pImpl->m_aResults.size() )
307 m_pImpl->m_aResults[ nIndex ]->xRow = uno::Reference< sdbc::XRow >();
310 // virtual
311 void DataSupplier::close()
315 // virtual
316 void DataSupplier::validate()
317 throw( ucb::ResultSetException )
321 sal_Bool DataSupplier::getData()
323 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
325 if ( !m_pImpl->m_bCountFinal ) {
326 GnomeVFSResult result;
327 GnomeVFSDirectoryHandle *dirHandle = NULL;
330 Authentication aAuth( getResultSet()->getEnvironment() );
331 char *uri = m_pImpl->m_xContent->getURI();
332 result = gnome_vfs_directory_open
333 ( &dirHandle, uri, GNOME_VFS_FILE_INFO_DEFAULT );
335 if (result != GNOME_VFS_OK) {
336 #ifdef DEBUG
337 g_warning ("Failed open of '%s' with '%s'",
338 uri, gnome_vfs_result_to_string( result ));
339 #endif
340 g_free( uri );
341 return sal_False;
344 g_free( uri );
347 GnomeVFSFileInfo* fileInfo = gnome_vfs_file_info_new ();
349 while ((result = gnome_vfs_directory_read_next (dirHandle, fileInfo)) == GNOME_VFS_OK) {
350 if( fileInfo->name && fileInfo->name[0] == '.' &&
351 ( fileInfo->name[1] == '\0' ||
352 ( fileInfo->name[1] == '.' && fileInfo->name[2] == '\0' ) ) )
353 continue;
355 switch ( m_pImpl->m_nOpenMode ) {
356 case ucb::OpenMode::FOLDERS:
357 if ( !(fileInfo->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) ||
358 fileInfo->type != GNOME_VFS_FILE_TYPE_DIRECTORY )
359 continue;
360 break;
362 case ucb::OpenMode::DOCUMENTS:
363 if ( !(fileInfo->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) ||
364 fileInfo->type != GNOME_VFS_FILE_TYPE_REGULAR )
365 continue;
366 break;
368 case ucb::OpenMode::ALL:
369 default:
370 break;
373 m_pImpl->m_aResults.push_back( new ResultListEntry( fileInfo ) );
376 gnome_vfs_file_info_unref (fileInfo);
378 #if OSL_DEBUG_LEVEL > 1
379 g_warning ("Got %d directory entries", result);
380 #endif
382 m_pImpl->m_bCountFinal = sal_True;
384 // Callback possible, because listeners may be informed!
385 aGuard.clear();
386 getResultSet()->rowCountFinal();
388 if (result != GNOME_VFS_ERROR_EOF) {
389 #ifdef DEBUG
390 g_warning( "Failed read_next '%s'",
391 gnome_vfs_result_to_string( result ) );
392 #endif
393 return sal_False;
396 result = gnome_vfs_directory_close (dirHandle);
397 if (result != GNOME_VFS_OK) {
398 #ifdef DEBUG
399 g_warning( "Failed close '%s'",
400 gnome_vfs_result_to_string( result ) );
401 #endif
402 return sal_False;
406 return sal_True;
411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */