Avoid potential negative array index access to cached text.
[LibreOffice.git] / ucbhelper / source / provider / resultsethelper.cxx
blob788e6811a1008dc4129a8a1b49f7299f28eacf3d
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 /* TODO
22 - This implementation is far away from completion. It has no interface
23 for changes notifications etc.
26 #include <com/sun/star/ucb/ListActionType.hpp>
27 #include <com/sun/star/ucb/ListenerAlreadySetException.hpp>
28 #include <com/sun/star/ucb/ServiceNotFoundException.hpp>
29 #include <com/sun/star/ucb/WelcomeDynamicResultSetStruct.hpp>
30 #include <com/sun/star/ucb/CachedDynamicResultSetStubFactory.hpp>
31 #include <com/sun/star/ucb/XSourceInitialization.hpp>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <ucbhelper/resultsethelper.hxx>
34 #include <utility>
36 #include <osl/diagnose.h>
38 using namespace com::sun::star;
41 // ResultSetImplHelper Implementation.
44 namespace ucbhelper {
47 ResultSetImplHelper::ResultSetImplHelper(
48 uno::Reference< uno::XComponentContext > xContext,
49 css::ucb::OpenCommandArgument2 aCommand )
50 : m_bStatic( false ),
51 m_bInitDone( false ),
52 m_aCommand(std::move( aCommand )),
53 m_xContext(std::move( xContext ))
58 // virtual
59 ResultSetImplHelper::~ResultSetImplHelper()
64 // XServiceInfo methods.
66 OUString SAL_CALL ResultSetImplHelper::getImplementationName()
68 return "ResultSetImplHelper";
71 sal_Bool SAL_CALL ResultSetImplHelper::supportsService( const OUString& ServiceName )
73 return cppu::supportsService( this, ServiceName );
76 css::uno::Sequence< OUString > SAL_CALL ResultSetImplHelper::getSupportedServiceNames()
78 return { DYNAMICRESULTSET_SERVICE_NAME };
81 // XComponent methods.
84 // virtual
85 void SAL_CALL ResultSetImplHelper::dispose()
87 std::unique_lock aGuard( m_aMutex );
89 if ( m_aDisposeEventListeners.getLength(aGuard) )
91 lang::EventObject aEvt;
92 aEvt.Source = static_cast< lang::XComponent * >( this );
93 m_aDisposeEventListeners.disposeAndClear( aGuard, aEvt );
98 // virtual
99 void SAL_CALL ResultSetImplHelper::addEventListener(
100 const uno::Reference< lang::XEventListener >& Listener )
102 std::unique_lock aGuard( m_aMutex );
104 m_aDisposeEventListeners.addInterface( aGuard, Listener );
108 // virtual
109 void SAL_CALL ResultSetImplHelper::removeEventListener(
110 const uno::Reference< lang::XEventListener >& Listener )
112 std::unique_lock aGuard( m_aMutex );
114 m_aDisposeEventListeners.removeInterface( aGuard, Listener );
118 // XDynamicResultSet methods.
121 // virtual
122 uno::Reference< sdbc::XResultSet > SAL_CALL
123 ResultSetImplHelper::getStaticResultSet()
125 std::unique_lock aGuard( m_aMutex );
127 if ( m_xListener.is() )
128 throw css::ucb::ListenerAlreadySetException();
130 init( true );
131 return m_xResultSet1;
135 // virtual
136 void SAL_CALL ResultSetImplHelper::setListener(
137 const uno::Reference< css::ucb::XDynamicResultSetListener >& Listener )
139 std::unique_lock aGuard( m_aMutex );
141 if ( m_bStatic || m_xListener.is() )
142 throw css::ucb::ListenerAlreadySetException();
144 m_xListener = Listener;
147 // Create "welcome event" and send it to listener.
150 // Note: We only have the implementation for a static result set at the
151 // moment (src590). The dynamic result sets passed to the listener
152 // are a fake. This implementation will never call "notify" at the
153 // listener to propagate any changes!!!
155 init( false );
157 uno::Any aInfo;
158 aInfo <<= css::ucb::WelcomeDynamicResultSetStruct(
159 m_xResultSet1 /* "old" */,
160 m_xResultSet2 /* "new" */ );
162 uno::Sequence< css::ucb::ListAction > aActions {
163 css::ucb::ListAction(
164 0, // Position; not used
165 0, // Count; not used
166 css::ucb::ListActionType::WELCOME,
167 aInfo ) };
168 aGuard.unlock();
170 Listener->notify(
171 css::ucb::ListEvent(
172 getXWeak(), aActions ) );
176 // virtual
177 sal_Int16 SAL_CALL ResultSetImplHelper::getCapabilities()
179 // ! css::ucb::ContentResultSetCapability::SORTED
180 return 0;
184 // virtual
185 void SAL_CALL ResultSetImplHelper::connectToCache(
186 const uno::Reference< css::ucb::XDynamicResultSet > & xCache )
188 if ( m_xListener.is() )
189 throw css::ucb::ListenerAlreadySetException();
191 if ( m_bStatic )
192 throw css::ucb::ListenerAlreadySetException();
194 uno::Reference< css::ucb::XSourceInitialization > xTarget( xCache, uno::UNO_QUERY );
195 if ( xTarget.is() )
197 uno::Reference< css::ucb::XCachedDynamicResultSetStubFactory > xStubFactory;
200 xStubFactory
201 = css::ucb::CachedDynamicResultSetStubFactory::create(
202 m_xContext );
204 catch ( uno::Exception const & )
208 if ( xStubFactory.is() )
210 xStubFactory->connectToCache(
211 this, xCache, m_aCommand.SortingInfo, nullptr );
212 return;
215 throw css::ucb::ServiceNotFoundException();
219 // Non-interface methods.
222 void ResultSetImplHelper::init( bool bStatic )
224 if ( m_bInitDone )
225 return;
227 if ( bStatic )
229 // virtual... derived class fills m_xResultSet1
230 initStatic();
232 OSL_ENSURE( m_xResultSet1.is(),
233 "ResultSetImplHelper::init - No 1st result set!" );
234 m_bStatic = true;
236 else
238 // virtual... derived class fills m_xResultSet1 and m_xResultSet2
239 initDynamic();
241 OSL_ENSURE( m_xResultSet1.is(),
242 "ResultSetImplHelper::init - No 1st result set!" );
243 OSL_ENSURE( m_xResultSet2.is(),
244 "ResultSetImplHelper::init - No 2nd result set!" );
245 m_bStatic = false;
247 m_bInitDone = true;
250 } // namespace ucbhelper
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */