Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / ucb / source / ucp / file / filrset.cxx
blob90c9646e8dbb01e333726092c38dd3fac005a1a2
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 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
23 #include <com/sun/star/sdbc/SQLException.hpp>
24 #include <com/sun/star/ucb/ListenerAlreadySetException.hpp>
25 #include <com/sun/star/ucb/ServiceNotFoundException.hpp>
26 #include <com/sun/star/ucb/WelcomeDynamicResultSetStruct.hpp>
27 #include "filid.hxx"
28 #include "filtask.hxx"
29 #include "filprp.hxx"
30 #include "filrset.hxx"
31 #include <com/sun/star/ucb/OpenMode.hpp>
32 #include "prov.hxx"
33 #include <com/sun/star/uno/Reference.h>
35 #include <com/sun/star/beans/PropertyAttribute.hpp>
36 #include <com/sun/star/ucb/ListActionType.hpp>
37 #include <com/sun/star/ucb/XSourceInitialization.hpp>
38 #include <com/sun/star/ucb/CachedDynamicResultSetStubFactory.hpp>
39 #include <ucbhelper/resultsetmetadata.hxx>
41 using namespace fileaccess;
42 using namespace com::sun::star;
44 #if OSL_DEBUG_LEVEL > 0
45 #define THROW_WHERE SAL_WHERE
46 #else
47 #define THROW_WHERE ""
48 #endif
50 XResultSet_impl::XResultSet_impl( TaskManager* pMyShell,
51 const OUString& aUnqPath,
52 sal_Int32 OpenMode,
53 const uno::Sequence< beans::Property >& seq,
54 const uno::Sequence< ucb::NumberedSortingInfo >& seqSort )
55 : m_pMyShell( pMyShell )
56 , m_nRow( -1 )
57 , m_nWasNull ( false )
58 , m_nOpenMode( OpenMode )
59 , m_bRowCountFinal( false )
60 , m_aBaseDirectory( aUnqPath )
61 , m_aFolder( aUnqPath )
62 , m_sProperty( seq )
63 , m_sSortingInfo( seqSort )
64 , m_nErrorCode( TASKHANDLER_NO_ERROR )
65 , m_nMinorErrorCode( TASKHANDLER_NO_ERROR )
67 osl::FileBase::RC err = m_aFolder.open();
68 if( err != osl::FileBase::E_None )
70 m_nIsOpen = false;
71 m_aFolder.close();
73 m_nErrorCode = TASKHANDLING_OPEN_FOR_DIRECTORYLISTING;
74 m_nMinorErrorCode = err;
76 else
77 m_nIsOpen = true;
81 XResultSet_impl::~XResultSet_impl()
83 if( m_nIsOpen )
84 m_aFolder.close();
88 void SAL_CALL
89 XResultSet_impl::disposing( const lang::EventObject& )
91 // To do, but what
95 void SAL_CALL
96 XResultSet_impl::addEventListener(
97 const uno::Reference< lang::XEventListener >& Listener )
99 osl::MutexGuard aGuard( m_aMutex );
101 if ( ! m_pDisposeEventListeners )
102 m_pDisposeEventListeners.reset(
103 new comphelper::OInterfaceContainerHelper2( m_aEventListenerMutex ) );
105 m_pDisposeEventListeners->addInterface( Listener );
109 void SAL_CALL
110 XResultSet_impl::removeEventListener(
111 const uno::Reference< lang::XEventListener >& Listener )
113 osl::MutexGuard aGuard( m_aMutex );
115 if ( m_pDisposeEventListeners )
116 m_pDisposeEventListeners->removeInterface( Listener );
120 void SAL_CALL
121 XResultSet_impl::dispose()
123 osl::MutexGuard aGuard( m_aMutex );
125 lang::EventObject aEvt;
126 aEvt.Source = static_cast< lang::XComponent * >( this );
128 if ( m_pDisposeEventListeners && m_pDisposeEventListeners->getLength() )
130 m_pDisposeEventListeners->disposeAndClear( aEvt );
132 if( m_pRowCountListeners && m_pRowCountListeners->getLength() )
134 m_pRowCountListeners->disposeAndClear( aEvt );
136 if( m_pIsFinalListeners && m_pIsFinalListeners->getLength() )
138 m_pIsFinalListeners->disposeAndClear( aEvt );
143 void XResultSet_impl::rowCountChanged()
145 sal_Int32 aOldValue,aNewValue;
146 std::vector< uno::Reference< uno::XInterface > > seq;
148 osl::MutexGuard aGuard( m_aMutex );
149 if( m_pRowCountListeners )
150 seq = m_pRowCountListeners->getElements();
151 aNewValue = m_aItems.size();
152 aOldValue = aNewValue-1;
154 beans::PropertyChangeEvent aEv;
155 aEv.PropertyName = "RowCount";
156 aEv.Further = false;
157 aEv.PropertyHandle = -1;
158 aEv.OldValue <<= aOldValue;
159 aEv.NewValue <<= aNewValue;
160 for( const auto& r : seq )
162 uno::Reference< beans::XPropertyChangeListener > listener(
163 r, uno::UNO_QUERY );
164 if( listener.is() )
165 listener->propertyChange( aEv );
170 void XResultSet_impl::isFinalChanged()
172 std::vector< uno::Reference< XInterface > > seq;
174 osl::MutexGuard aGuard( m_aMutex );
175 if( m_pIsFinalListeners )
176 seq = m_pIsFinalListeners->getElements();
177 m_bRowCountFinal = true;
179 beans::PropertyChangeEvent aEv;
180 aEv.PropertyName = "IsRowCountFinal";
181 aEv.Further = false;
182 aEv.PropertyHandle = -1;
183 aEv.OldValue <<= false;
184 aEv.NewValue <<= true;
185 for( const auto& r : seq )
187 uno::Reference< beans::XPropertyChangeListener > listener(
188 r, uno::UNO_QUERY );
189 if( listener.is() )
190 listener->propertyChange( aEv );
195 bool
196 XResultSet_impl::OneMore()
198 if( ! m_nIsOpen )
199 return false;
201 osl::FileBase::RC err;
202 bool IsRegular;
203 OUString aUnqPath;
204 osl::DirectoryItem aDirIte;
205 uno::Reference< sdbc::XRow > aRow;
207 while( true )
209 err = m_aFolder.getNextItem( aDirIte );
211 if( err == osl::FileBase::E_NOENT || err == osl::FileBase::E_INVAL )
213 m_aFolder.close();
214 isFinalChanged();
215 return ( m_nIsOpen = false );
217 else if( err == osl::FileBase::E_None )
219 if (!m_pMyShell->getv( m_sProperty, aDirIte, aUnqPath, IsRegular, aRow ))
221 SAL_WARN(
222 "ucb.ucp.file",
223 "getting dir item in <" << m_aBaseDirectory << "> failed");
224 continue;
227 if( m_nOpenMode == ucb::OpenMode::DOCUMENTS && IsRegular )
229 osl::MutexGuard aGuard( m_aMutex );
230 m_aItems.push_back( aRow );
231 m_aIdents.emplace_back( );
232 m_aUnqPath.push_back( aUnqPath );
233 rowCountChanged();
234 return true;
237 else if( m_nOpenMode == ucb::OpenMode::DOCUMENTS && ! IsRegular )
239 continue;
241 else if( m_nOpenMode == ucb::OpenMode::FOLDERS && ! IsRegular )
243 osl::MutexGuard aGuard( m_aMutex );
244 m_aItems.push_back( aRow );
245 m_aIdents.emplace_back( );
246 m_aUnqPath.push_back( aUnqPath );
247 rowCountChanged();
248 return true;
250 else if( m_nOpenMode == ucb::OpenMode::FOLDERS && IsRegular )
252 continue;
254 else
256 osl::MutexGuard aGuard( m_aMutex );
257 m_aItems.push_back( aRow );
258 m_aIdents.emplace_back( );
259 m_aUnqPath.push_back( aUnqPath );
260 rowCountChanged();
261 return true;
264 else // error fetching anything
266 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
272 sal_Bool SAL_CALL
273 XResultSet_impl::next()
275 bool test;
276 if( ++m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size()) ) test = true;
277 else
278 test = OneMore();
279 return test;
283 sal_Bool SAL_CALL
284 XResultSet_impl::isBeforeFirst()
286 return m_nRow == -1;
290 sal_Bool SAL_CALL
291 XResultSet_impl::isAfterLast()
293 return m_nRow >= sal::static_int_cast<sal_Int32>(m_aItems.size()); // Cannot happen, if m_aFolder.isOpen()
297 sal_Bool SAL_CALL
298 XResultSet_impl::isFirst()
300 return m_nRow == 0;
304 sal_Bool SAL_CALL
305 XResultSet_impl::isLast()
307 if( m_nRow == sal::static_int_cast<sal_Int32>(m_aItems.size()) - 1 )
308 return ! OneMore();
309 else
310 return false;
314 void SAL_CALL
315 XResultSet_impl::beforeFirst()
317 m_nRow = -1;
321 void SAL_CALL
322 XResultSet_impl::afterLast()
324 m_nRow = sal::static_int_cast<sal_Int32>(m_aItems.size());
325 while( OneMore() )
326 ++m_nRow;
330 sal_Bool SAL_CALL
331 XResultSet_impl::first()
333 m_nRow = -1;
334 return next();
338 sal_Bool SAL_CALL
339 XResultSet_impl::last()
341 m_nRow = sal::static_int_cast<sal_Int32>(m_aItems.size()) - 1;
342 while( OneMore() )
343 ++m_nRow;
344 return true;
348 sal_Int32 SAL_CALL
349 XResultSet_impl::getRow()
351 // Test, whether behind last row
352 if( -1 == m_nRow || m_nRow >= sal::static_int_cast<sal_Int32>(m_aItems.size()) )
353 return 0;
354 else
355 return m_nRow+1;
359 sal_Bool SAL_CALL XResultSet_impl::absolute( sal_Int32 row )
361 if( row >= 0 )
363 m_nRow = row - 1;
364 if( row >= sal::static_int_cast<sal_Int32>(m_aItems.size()) )
365 while( row-- && OneMore() )
368 else
370 last();
371 m_nRow += ( row + 1 );
372 if( m_nRow < -1 )
373 m_nRow = -1;
376 return 0<= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size());
380 sal_Bool SAL_CALL
381 XResultSet_impl::relative( sal_Int32 row )
383 if( isAfterLast() || isBeforeFirst() )
384 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
385 if( row > 0 )
386 while( row-- ) next();
387 else if( row < 0 )
388 while( row++ && m_nRow > - 1 ) previous();
390 return 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size());
394 sal_Bool SAL_CALL
395 XResultSet_impl::previous()
397 if( m_nRow > sal::static_int_cast<sal_Int32>(m_aItems.size()) )
398 m_nRow = sal::static_int_cast<sal_Int32>(m_aItems.size()); // Correct Handling of afterLast
399 if( 0 <= m_nRow ) -- m_nRow;
401 return 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size());
405 void SAL_CALL
406 XResultSet_impl::refreshRow()
408 // get the row from the filesystem
412 sal_Bool SAL_CALL
413 XResultSet_impl::rowUpdated()
415 return false;
418 sal_Bool SAL_CALL
419 XResultSet_impl::rowInserted()
421 return false;
424 sal_Bool SAL_CALL
425 XResultSet_impl::rowDeleted()
427 return false;
431 uno::Reference< uno::XInterface > SAL_CALL
432 XResultSet_impl::getStatement()
434 return uno::Reference< uno::XInterface >();
438 // XCloseable
440 void SAL_CALL
441 XResultSet_impl::close()
443 if( m_nIsOpen )
445 m_aFolder.close();
446 isFinalChanged();
447 osl::MutexGuard aGuard( m_aMutex );
448 m_nIsOpen = false;
453 OUString SAL_CALL
454 XResultSet_impl::queryContentIdentifierString()
456 uno::Reference< ucb::XContentIdentifier > xContentId
457 = queryContentIdentifier();
459 if( xContentId.is() )
460 return xContentId->getContentIdentifier();
461 else
462 return OUString();
466 uno::Reference< ucb::XContentIdentifier > SAL_CALL
467 XResultSet_impl::queryContentIdentifier()
469 if( 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size()) )
471 if( ! m_aIdents[m_nRow].is() )
473 m_aIdents[m_nRow].set( new FileContentIdentifier( m_aUnqPath[ m_nRow ] ) );
475 return m_aIdents[m_nRow];
477 return uno::Reference< ucb::XContentIdentifier >();
481 uno::Reference< ucb::XContent > SAL_CALL
482 XResultSet_impl::queryContent()
484 if( 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size()) )
485 return m_pMyShell->m_pProvider->queryContent( queryContentIdentifier() );
486 else
487 return uno::Reference< ucb::XContent >();
491 // XDynamicResultSet
494 // virtual
495 uno::Reference< sdbc::XResultSet > SAL_CALL
496 XResultSet_impl::getStaticResultSet()
498 osl::MutexGuard aGuard( m_aMutex );
500 if ( m_xListener.is() )
501 throw ucb::ListenerAlreadySetException( THROW_WHERE );
503 return uno::Reference< sdbc::XResultSet >( this );
507 // virtual
508 void SAL_CALL
509 XResultSet_impl::setListener(
510 const uno::Reference< ucb::XDynamicResultSetListener >& Listener )
512 osl::ClearableMutexGuard aGuard( m_aMutex );
514 if ( m_xListener.is() )
515 throw ucb::ListenerAlreadySetException( THROW_WHERE );
517 m_xListener = Listener;
520 // Create "welcome event" and send it to listener.
523 // Note: We only have the implementation for a static result set at the
524 // moment (src590). The dynamic result sets passed to the listener
525 // are a fake. This implementation will never call "notify" at the
526 // listener to propagate any changes!!!
528 uno::Any aInfo;
529 aInfo <<= ucb::WelcomeDynamicResultSetStruct( this, /* "old" */
530 this /* "new" */ );
532 uno::Sequence< ucb::ListAction > aActions( 1 );
533 aActions.getArray()[ 0 ] = ucb::ListAction( 0, // Position; not used
534 0, // Count; not used
535 ucb::ListActionType::WELCOME,
536 aInfo );
537 aGuard.clear();
539 Listener->notify(
540 ucb::ListEvent(
541 static_cast< cppu::OWeakObject * >( this ), aActions ) );
545 // virtual
546 void SAL_CALL
547 XResultSet_impl::connectToCache(
548 const uno::Reference< ucb::XDynamicResultSet > & xCache )
550 if( m_xListener.is() )
551 throw ucb::ListenerAlreadySetException( THROW_WHERE );
553 uno::Reference< ucb::XSourceInitialization > xTarget(
554 xCache, uno::UNO_QUERY );
555 if( xTarget.is() && m_pMyShell->m_xContext.is() )
557 uno::Reference< ucb::XCachedDynamicResultSetStubFactory > xStubFactory;
560 xStubFactory
561 = ucb::CachedDynamicResultSetStubFactory::create(
562 m_pMyShell->m_xContext );
564 catch ( uno::Exception const & )
568 if( xStubFactory.is() )
570 xStubFactory->connectToCache(
571 this, xCache,m_sSortingInfo, nullptr );
572 return;
575 throw ucb::ServiceNotFoundException( THROW_WHERE );
579 // virtual
580 sal_Int16 SAL_CALL
581 XResultSet_impl::getCapabilities()
583 // Never set ucb::ContentResultSetCapability::SORTED
584 // - Underlying content cannot provide sorted data...
585 return 0;
588 // XResultSetMetaDataSupplier
589 uno::Reference< sdbc::XResultSetMetaData > SAL_CALL
590 XResultSet_impl::getMetaData()
592 auto pProp = std::find_if(m_sProperty.begin(), m_sProperty.end(),
593 [](const beans::Property& rProp) { return rProp.Name == "Title"; });
594 if (pProp != m_sProperty.end())
596 std::vector< ::ucbhelper::ResultSetColumnData >
597 aColumnData( m_sProperty.getLength() );
598 auto n = std::distance(m_sProperty.begin(), pProp);
599 // @@@ #82177# - Determine correct value!
600 aColumnData[ n ].isCaseSensitive = false;
602 ::ucbhelper::ResultSetMetaData* p =
603 new ::ucbhelper::ResultSetMetaData(
604 m_pMyShell->m_xContext,
605 m_sProperty,
606 aColumnData );
607 return uno::Reference< sdbc::XResultSetMetaData >( p );
610 ::ucbhelper::ResultSetMetaData* p =
611 new ::ucbhelper::ResultSetMetaData( m_pMyShell->m_xContext, m_sProperty );
612 return uno::Reference< sdbc::XResultSetMetaData >( p );
616 // XPropertySet
617 uno::Reference< beans::XPropertySetInfo > SAL_CALL
618 XResultSet_impl::getPropertySetInfo()
621 uno::Sequence< beans::Property > seq(2);
622 seq[0].Name = "RowCount";
623 seq[0].Handle = -1;
624 seq[0].Type = cppu::UnoType<sal_Int32>::get();
625 seq[0].Attributes = beans::PropertyAttribute::READONLY;
627 seq[1].Name = "IsRowCountFinal";
628 seq[1].Handle = -1;
629 seq[1].Type = cppu::UnoType<sal_Bool>::get();
630 seq[1].Attributes = beans::PropertyAttribute::READONLY;
632 XPropertySetInfo_impl* p = new XPropertySetInfo_impl( m_pMyShell,
633 seq );
634 return uno::Reference< beans::XPropertySetInfo > ( p );
638 void SAL_CALL XResultSet_impl::setPropertyValue(
639 const OUString& aPropertyName, const uno::Any& )
641 if( aPropertyName == "IsRowCountFinal" ||
642 aPropertyName == "RowCount" )
643 return;
644 throw beans::UnknownPropertyException( aPropertyName );
648 uno::Any SAL_CALL XResultSet_impl::getPropertyValue(
649 const OUString& PropertyName )
651 if( PropertyName == "IsRowCountFinal" )
653 return uno::Any(m_bRowCountFinal);
655 else if ( PropertyName == "RowCount" )
657 sal_Int32 count = sal::static_int_cast<sal_Int32>(m_aItems.size());
658 return uno::Any(count);
660 else
661 throw beans::UnknownPropertyException( PropertyName );
665 void SAL_CALL XResultSet_impl::addPropertyChangeListener(
666 const OUString& aPropertyName,
667 const uno::Reference< beans::XPropertyChangeListener >& xListener )
669 if( aPropertyName == "IsRowCountFinal" )
671 osl::MutexGuard aGuard( m_aMutex );
672 if ( ! m_pIsFinalListeners )
673 m_pIsFinalListeners.reset(
674 new comphelper::OInterfaceContainerHelper2( m_aEventListenerMutex ) );
676 m_pIsFinalListeners->addInterface( xListener );
678 else if ( aPropertyName == "RowCount" )
680 osl::MutexGuard aGuard( m_aMutex );
681 if ( ! m_pRowCountListeners )
682 m_pRowCountListeners.reset(
683 new comphelper::OInterfaceContainerHelper2( m_aEventListenerMutex ) );
684 m_pRowCountListeners->addInterface( xListener );
686 else
687 throw beans::UnknownPropertyException( aPropertyName );
691 void SAL_CALL XResultSet_impl::removePropertyChangeListener(
692 const OUString& aPropertyName,
693 const uno::Reference< beans::XPropertyChangeListener >& aListener )
695 if( aPropertyName == "IsRowCountFinal" &&
696 m_pIsFinalListeners )
698 osl::MutexGuard aGuard( m_aMutex );
699 m_pIsFinalListeners->removeInterface( aListener );
701 else if ( aPropertyName == "RowCount" &&
702 m_pRowCountListeners )
704 osl::MutexGuard aGuard( m_aMutex );
706 m_pRowCountListeners->removeInterface( aListener );
708 else
709 throw beans::UnknownPropertyException( aPropertyName );
712 void SAL_CALL XResultSet_impl::addVetoableChangeListener(
713 const OUString&,
714 const uno::Reference< beans::XVetoableChangeListener >& )
719 void SAL_CALL XResultSet_impl::removeVetoableChangeListener(
720 const OUString&,
721 const uno::Reference< beans::XVetoableChangeListener >& )
725 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */