1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
28 #include "filtask.hxx"
30 #include "filrset.hxx"
31 #include <com/sun/star/ucb/OpenMode.hpp>
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
47 #define THROW_WHERE ""
50 XResultSet_impl::XResultSet_impl( TaskManager
* pMyShell
,
51 const OUString
& aUnqPath
,
53 const uno::Sequence
< beans::Property
>& seq
,
54 const uno::Sequence
< ucb::NumberedSortingInfo
>& seqSort
)
55 : m_pMyShell( pMyShell
)
57 , m_nWasNull ( false )
58 , m_nOpenMode( OpenMode
)
59 , m_bRowCountFinal( false )
60 , m_aBaseDirectory( aUnqPath
)
61 , m_aFolder( aUnqPath
)
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
)
73 m_nErrorCode
= TASKHANDLING_OPEN_FOR_DIRECTORYLISTING
;
74 m_nMinorErrorCode
= err
;
81 XResultSet_impl::~XResultSet_impl()
89 XResultSet_impl::disposing( const lang::EventObject
& )
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
);
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
);
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";
157 aEv
.PropertyHandle
= -1;
158 aEv
.OldValue
<<= aOldValue
;
159 aEv
.NewValue
<<= aNewValue
;
160 for( const auto& r
: seq
)
162 uno::Reference
< beans::XPropertyChangeListener
> listener(
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";
182 aEv
.PropertyHandle
= -1;
183 aEv
.OldValue
<<= false;
184 aEv
.NewValue
<<= true;
185 for( const auto& r
: seq
)
187 uno::Reference
< beans::XPropertyChangeListener
> listener(
190 listener
->propertyChange( aEv
);
196 XResultSet_impl::OneMore()
201 osl::FileBase::RC err
;
204 osl::DirectoryItem aDirIte
;
205 uno::Reference
< sdbc::XRow
> aRow
;
209 err
= m_aFolder
.getNextItem( aDirIte
);
211 if( err
== osl::FileBase::E_NOENT
|| err
== osl::FileBase::E_INVAL
)
215 return ( m_nIsOpen
= false );
217 else if( err
== osl::FileBase::E_None
)
219 if (!m_pMyShell
->getv( m_sProperty
, aDirIte
, aUnqPath
, IsRegular
, aRow
))
223 "getting dir item in <" << m_aBaseDirectory
<< "> failed");
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
);
237 else if( m_nOpenMode
== ucb::OpenMode::DOCUMENTS
&& ! IsRegular
)
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
);
250 else if( m_nOpenMode
== ucb::OpenMode::FOLDERS
&& IsRegular
)
256 osl::MutexGuard
aGuard( m_aMutex
);
257 m_aItems
.push_back( aRow
);
258 m_aIdents
.emplace_back( );
259 m_aUnqPath
.push_back( aUnqPath
);
264 else // error fetching anything
266 throw sdbc::SQLException( THROW_WHERE
, uno::Reference
< uno::XInterface
>(), OUString(), 0, uno::Any() );
273 XResultSet_impl::next()
276 if( ++m_nRow
< sal::static_int_cast
<sal_Int32
>(m_aItems
.size()) ) test
= true;
284 XResultSet_impl::isBeforeFirst()
291 XResultSet_impl::isAfterLast()
293 return m_nRow
>= sal::static_int_cast
<sal_Int32
>(m_aItems
.size()); // Cannot happen, if m_aFolder.isOpen()
298 XResultSet_impl::isFirst()
305 XResultSet_impl::isLast()
307 if( m_nRow
== sal::static_int_cast
<sal_Int32
>(m_aItems
.size()) - 1 )
315 XResultSet_impl::beforeFirst()
322 XResultSet_impl::afterLast()
324 m_nRow
= sal::static_int_cast
<sal_Int32
>(m_aItems
.size());
331 XResultSet_impl::first()
339 XResultSet_impl::last()
341 m_nRow
= sal::static_int_cast
<sal_Int32
>(m_aItems
.size()) - 1;
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()) )
359 sal_Bool SAL_CALL
XResultSet_impl::absolute( sal_Int32 row
)
364 if( row
>= sal::static_int_cast
<sal_Int32
>(m_aItems
.size()) )
365 while( row
-- && OneMore() )
371 m_nRow
+= ( row
+ 1 );
376 return 0<= m_nRow
&& m_nRow
< sal::static_int_cast
<sal_Int32
>(m_aItems
.size());
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() );
386 while( row
-- ) next();
388 while( row
++ && m_nRow
> - 1 ) previous();
390 return 0 <= m_nRow
&& m_nRow
< sal::static_int_cast
<sal_Int32
>(m_aItems
.size());
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());
406 XResultSet_impl::refreshRow()
408 // get the row from the filesystem
413 XResultSet_impl::rowUpdated()
419 XResultSet_impl::rowInserted()
425 XResultSet_impl::rowDeleted()
431 uno::Reference
< uno::XInterface
> SAL_CALL
432 XResultSet_impl::getStatement()
434 return uno::Reference
< uno::XInterface
>();
441 XResultSet_impl::close()
447 osl::MutexGuard
aGuard( m_aMutex
);
454 XResultSet_impl::queryContentIdentifierString()
456 uno::Reference
< ucb::XContentIdentifier
> xContentId
457 = queryContentIdentifier();
459 if( xContentId
.is() )
460 return xContentId
->getContentIdentifier();
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() );
487 return uno::Reference
< ucb::XContent
>();
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 );
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!!!
529 aInfo
<<= ucb::WelcomeDynamicResultSetStruct( this, /* "old" */
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
,
541 static_cast< cppu::OWeakObject
* >( this ), aActions
) );
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
;
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 );
575 throw ucb::ServiceNotFoundException( THROW_WHERE
);
581 XResultSet_impl::getCapabilities()
583 // Never set ucb::ContentResultSetCapability::SORTED
584 // - Underlying content cannot provide sorted data...
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
,
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
);
617 uno::Reference
< beans::XPropertySetInfo
> SAL_CALL
618 XResultSet_impl::getPropertySetInfo()
621 uno::Sequence
< beans::Property
> seq(2);
622 seq
[0].Name
= "RowCount";
624 seq
[0].Type
= cppu::UnoType
<sal_Int32
>::get();
625 seq
[0].Attributes
= beans::PropertyAttribute::READONLY
;
627 seq
[1].Name
= "IsRowCountFinal";
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
,
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" )
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
);
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
);
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
);
709 throw beans::UnknownPropertyException( aPropertyName
);
712 void SAL_CALL
XResultSet_impl::addVetoableChangeListener(
714 const uno::Reference
< beans::XVetoableChangeListener
>& )
719 void SAL_CALL
XResultSet_impl::removeVetoableChangeListener(
721 const uno::Reference
< beans::XVetoableChangeListener
>& )
725 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */