LibreOffice Module ucb (master) 1
sortdynres.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "sortdynres.hxx"
22#include <cppuhelper/weak.hxx>
23#include <com/sun/star/ucb/ContentResultSetCapability.hpp>
24#include <com/sun/star/ucb/ListActionType.hpp>
25#include <com/sun/star/ucb/ListenerAlreadySetException.hpp>
26#include <com/sun/star/ucb/ServiceNotFoundException.hpp>
27#include <com/sun/star/ucb/WelcomeDynamicResultSetStruct.hpp>
28#include <com/sun/star/ucb/CachedDynamicResultSetStubFactory.hpp>
29#include <com/sun/star/ucb/XSourceInitialization.hpp>
30
31using namespace com::sun::star::beans;
32using namespace com::sun::star::lang;
33using namespace com::sun::star::sdbc;
34using namespace com::sun::star::ucb;
35using namespace com::sun::star::uno;
36using namespace comphelper;
37
38
39// SortedDynamicResultSet
40
42 const Reference < XDynamicResultSet > &xOriginal,
45 const Reference < XComponentContext > &rxContext )
46{
48
49 mxOriginal = xOriginal;
50 maOptions = aOptions;
51 mxCompFac = xCompFac;
52 m_xContext = rxContext;
53
54 mbGotWelcome = false;
55 mbUseOne = true;
56 mbStatic = false;
57}
58
59
61{
62 mxOwnListener->impl_OwnerDies();
63 mxOwnListener.clear();
64
65 {
66 std::unique_lock aGuard(maMutex);
68 }
69
70 mxOne.clear();
71 mxTwo.clear();
72 mxOriginal.clear();
73}
74
75// XServiceInfo methods.
76
78{
79 return "com.sun.star.comp.ucb.SortedDynamicResultSet";
80}
81
82sal_Bool SAL_CALL SortedDynamicResultSet::supportsService( const OUString& ServiceName )
83{
84 return cppu::supportsService( this, ServiceName );
85}
86
87css::uno::Sequence< OUString > SAL_CALL SortedDynamicResultSet::getSupportedServiceNames()
88{
89 return { "com.sun.star.ucb.SortedDynamicResultSet" };
90}
91
92// XComponent methods.
93
95{
96 std::unique_lock aGuard( maMutex );
97
99 {
100 EventObject aEvt;
101 aEvt.Source = static_cast< XComponent * >( this );
103 }
104
105 mxOne.clear();
106 mxTwo.clear();
107 mxOriginal.clear();
108
109 mbUseOne = true;
110}
111
113 const Reference< XEventListener >& Listener )
114{
115 std::unique_lock aGuard( maMutex );
116
117 maDisposeEventListeners.addInterface( aGuard, Listener );
118}
119
121 const Reference< XEventListener >& Listener )
122{
123 std::unique_lock aGuard( maMutex );
124
125 maDisposeEventListeners.removeInterface( aGuard, Listener );
126}
127
128
129// XDynamicResultSet methods.
130
133{
134 std::unique_lock aGuard( maMutex );
135
136 if ( mxListener.is() )
137 throw ListenerAlreadySetException();
138
139 mbStatic = true;
140
141 if ( mxOriginal.is() )
142 {
143 mxOne = new SortedResultSet( mxOriginal->getStaticResultSet() );
144 mxOne->Initialize( maOptions, mxCompFac );
145 }
146
147 return mxOne;
148}
149
150
151void SAL_CALL
153{
154 std::unique_lock aGuard( maMutex );
155
156 if ( mxListener.is() )
157 throw ListenerAlreadySetException();
158
159 maDisposeEventListeners.addInterface( aGuard, Listener );
160
161 mxListener = Listener;
162
163 if ( mxOriginal.is() )
164 mxOriginal->setListener( mxOwnListener );
165}
166
167
168void SAL_CALL
170{
171 if( mxListener.is() )
172 throw ListenerAlreadySetException();
173
174 if( mbStatic )
175 throw ListenerAlreadySetException();
176
177 Reference< XSourceInitialization > xTarget( xCache, UNO_QUERY );
178 if( xTarget.is() && m_xContext.is() )
179 {
181 try
182 {
183 xStubFactory = CachedDynamicResultSetStubFactory::create( m_xContext );
184 }
185 catch ( Exception const & )
186 {
187 }
188
189 if( xStubFactory.is() )
190 {
191 xStubFactory->connectToCache(
192 this, xCache, Sequence< NumberedSortingInfo > (), nullptr );
193 return;
194 }
195 }
196 throw ServiceNotFoundException();
197}
198
199
201{
202 std::unique_lock aGuard( maMutex );
203
204 sal_Int16 nCaps = 0;
205
206 if ( mxOriginal.is() )
207 nCaps = mxOriginal->getCapabilities();
208
209 nCaps |= ContentResultSetCapability::SORTED;
210
211 return nCaps;
212}
213
214
215// XDynamicResultSetListener methods.
216
217
233void SortedDynamicResultSet::impl_notify( const ListEvent& Changes )
234{
235 std::unique_lock aGuard( maMutex );
236
237 bool bHasNew = false;
238 bool bHasModified = false;
239
240 SortedResultSet *pCurSet = nullptr;
241
242 // exchange mxNew and mxOld and immediately afterwards copy the tables
243 // from Old to New
244 if ( mbGotWelcome )
245 {
246 if ( mbUseOne )
247 {
248 mbUseOne = false;
249 mxTwo->CopyData( mxOne.get() );
250 pCurSet = mxTwo.get();
251 }
252 else
253 {
254 mbUseOne = true;
255 mxOne->CopyData( mxTwo.get() );
256 pCurSet = mxOne.get();
257 }
258 }
259
260 if (!pCurSet)
261 return;
262
263 Any aRet;
264
265 try {
266 aRet = pCurSet->getPropertyValue("IsRowCountFinal");
267 }
268 catch (const UnknownPropertyException&) {}
269 catch (const WrappedTargetException&) {}
270
271 sal_Int32 nOldCount = pCurSet->GetCount();
272 bool bWasFinal = false;
273
274 aRet >>= bWasFinal;
275
276 // handle the actions in the list
277 for ( const ListAction& aAction : Changes.Changes )
278 {
279 switch ( aAction.ListActionType )
280 {
281 case ListActionType::WELCOME:
282 {
283 WelcomeDynamicResultSetStruct aWelcome;
284 if ( aAction.ActionInfo >>= aWelcome )
285 {
286 mxTwo = new SortedResultSet( aWelcome.Old );
287 mxOne = new SortedResultSet( aWelcome.New );
288 mxOne->Initialize( maOptions, mxCompFac );
289 mbGotWelcome = true;
290 mbUseOne = true;
291 pCurSet = mxOne.get();
292
293 aWelcome.Old = mxTwo.get();
294 aWelcome.New = mxOne.get();
295
296 ListAction aWelcomeAction;
297 aWelcomeAction.ActionInfo <<= aWelcome;
298 aWelcomeAction.Position = 0;
299 aWelcomeAction.Count = 0;
300 aWelcomeAction.ListActionType = ListActionType::WELCOME;
301
302 maActions.Insert( aWelcomeAction );
303 }
304 else
305 {
306 // throw RuntimeException();
307 }
308 break;
309 }
310 case ListActionType::INSERTED:
311 {
312 pCurSet->InsertNew( aAction.Position, aAction.Count );
313 bHasNew = true;
314 break;
315 }
316 case ListActionType::REMOVED:
317 {
318 pCurSet->Remove( aAction.Position,
319 aAction.Count,
320 &maActions );
321 break;
322 }
323 case ListActionType::MOVED:
324 {
325 sal_Int32 nOffset = 0;
326 if ( aAction.ActionInfo >>= nOffset )
327 {
328 pCurSet->Move( aAction.Position,
329 aAction.Count,
330 nOffset );
331 }
332 break;
333 }
334 case ListActionType::PROPERTIES_CHANGED:
335 {
336 pCurSet->SetChanged( aAction.Position, aAction.Count );
337 bHasModified = true;
338 break;
339 }
340 default: break;
341 }
342 }
343
344 if ( bHasModified )
345 pCurSet->ResortModified( &maActions );
346
347 if ( bHasNew )
348 pCurSet->ResortNew( &maActions );
349
350 // send the new actions with a notify to the listeners
351 SendNotify();
352
353 // check for propertyChangeEvents
354 pCurSet->CheckProperties( nOldCount, bWasFinal );
355}
356
357// XEventListener
358
360{
361 mxListener.clear();
362 mxOriginal.clear();
363}
364
365// private methods
366
368{
369 sal_Int32 nCount = maActions.Count();
370
371 if ( nCount && mxListener.is() )
372 {
373 Sequence< ListAction > aActionList( maActions.Count() );
374 ListAction *pActionList = aActionList.getArray();
375
376 for ( sal_Int32 i=0; i<nCount; i++ )
377 {
378 pActionList[ i ] = maActions.GetAction( i );
379 }
380
381 ListEvent aNewEvent;
382 aNewEvent.Changes = aActionList;
383
384 mxListener->notify( aNewEvent );
385 }
386
387 // clean up
389}
390
391// SortedDynamicResultSetFactory
392
394 const Reference< XComponentContext > & rxContext )
395{
396 m_xContext = rxContext;
397}
398
399
401{
402}
403
404
405// XServiceInfo methods.
406
408{
409 return "com.sun.star.comp.ucb.SortedDynamicResultSetFactory";
410}
411
412sal_Bool SAL_CALL SortedDynamicResultSetFactory::supportsService( const OUString& ServiceName )
413{
414 return cppu::supportsService( this, ServiceName );
415}
416
417css::uno::Sequence< OUString > SAL_CALL SortedDynamicResultSetFactory::getSupportedServiceNames()
418{
419 return { "com.sun.star.ucb.SortedDynamicResultSetFactory" };
420}
421
422
423extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
425 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
426{
427 return cppu::acquire(new SortedDynamicResultSetFactory(context));
428}
429
430// SortedDynamicResultSetFactory methods.
431
434 const Reference< XDynamicResultSet > & Source,
436 const Reference< XAnyCompareFactory > & CompareFactory )
437{
439 return xRet;
440}
441
442// EventList
443
445{
446 maData.clear();
447}
448
449void EventList::AddEvent( sal_IntPtr nType, sal_Int32 nPos )
450{
451 ListAction aAction;
452 aAction.Position = nPos;
453 aAction.Count = 1;
454 aAction.ListActionType = nType;
455
456 Insert( aAction );
457}
458
459// SortedDynamicResultSetListener
460
462 SortedDynamicResultSet *mOwner )
463{
464 mpOwner = mOwner;
465}
466
467
469{
470}
471
472// XEventListener ( base of XDynamicResultSetListener )
473
474void SAL_CALL
475SortedDynamicResultSetListener::disposing( const EventObject& /*Source*/ )
476{
477 std::unique_lock aGuard( maMutex );
478
479 if ( mpOwner )
481}
482
483
484// XDynamicResultSetListener
485
486void SAL_CALL
487SortedDynamicResultSetListener::notify( const ListEvent& Changes )
488{
489 std::unique_lock aGuard( maMutex );
490
491 if ( mpOwner )
492 mpOwner->impl_notify( Changes );
493}
494
495// own methods:
496
497void
499{
500 std::unique_lock aGuard( maMutex );
501 mpOwner = nullptr;
502}
503
504/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::ucb::ListAction & GetAction(sal_Int32 nIndex)
Definition: sortresult.hxx:81
void AddEvent(sal_IntPtr nType, sal_Int32 nPos)
Definition: sortdynres.cxx:449
sal_uInt32 Count() const
Definition: sortresult.hxx:76
void Insert(const css::ucb::ListAction &rAction)
Definition: sortresult.hxx:79
void Clear()
Definition: sortdynres.cxx:444
std::deque< css::ucb::ListAction > maData
Definition: sortresult.hxx:71
virtual css::uno::Reference< css::ucb::XDynamicResultSet > SAL_CALL createSortedDynamicResultSet(const css::uno::Reference< css::ucb::XDynamicResultSet > &Source, const css::uno::Sequence< css::ucb::NumberedSortingInfo > &Info, const css::uno::Reference< css::ucb::XAnyCompareFactory > &CompareFactory) override
Definition: sortdynres.cxx:433
SortedDynamicResultSetFactory(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
Definition: sortdynres.cxx:393
virtual ~SortedDynamicResultSetFactory() override
Definition: sortdynres.cxx:400
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: sortdynres.cxx:417
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: sortdynres.hxx:142
virtual OUString SAL_CALL getImplementationName() override
Definition: sortdynres.cxx:407
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: sortdynres.cxx:412
SortedDynamicResultSetListener(SortedDynamicResultSet *mOwner)
Definition: sortdynres.cxx:461
virtual ~SortedDynamicResultSetListener() override
Definition: sortdynres.cxx:468
SortedDynamicResultSet * mpOwner
Definition: sortdynres.hxx:115
virtual void SAL_CALL notify(const css::ucb::ListEvent &Changes) override
Definition: sortdynres.cxx:487
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
Definition: sortdynres.cxx:475
SortedDynamicResultSet(const css::uno::Reference< css::ucb::XDynamicResultSet > &xOriginal, const css::uno::Sequence< css::ucb::NumberedSortingInfo > &aOptions, const css::uno::Reference< css::ucb::XAnyCompareFactory > &xCompFac, const css::uno::Reference< css::uno::XComponentContext > &rxContext)
Definition: sortdynres.cxx:41
virtual OUString SAL_CALL getImplementationName() override
Definition: sortdynres.cxx:77
css::uno::Reference< css::ucb::XAnyCompareFactory > mxCompFac
Definition: sortdynres.hxx:47
virtual sal_Int16 SAL_CALL getCapabilities() override
Definition: sortdynres.cxx:200
rtl::Reference< SortedResultSet > mxOne
Definition: sortdynres.hxx:50
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: sortdynres.hxx:48
virtual ~SortedDynamicResultSet() override
Definition: sortdynres.cxx:60
virtual void SAL_CALL dispose() override
Definition: sortdynres.cxx:94
rtl::Reference< SortedDynamicResultSetListener > mxOwnListener
Definition: sortdynres.hxx:52
virtual void SAL_CALL setListener(const css::uno::Reference< css::ucb::XDynamicResultSetListener > &Listener) override
Definition: sortdynres.cxx:152
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &Listener) override
Definition: sortdynres.cxx:120
css::uno::Reference< css::ucb::XDynamicResultSetListener > mxListener
Definition: sortdynres.hxx:43
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: sortdynres.cxx:87
rtl::Reference< SortedResultSet > mxTwo
Definition: sortdynres.hxx:51
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &Listener) override
Definition: sortdynres.cxx:112
css::uno::Sequence< css::ucb::NumberedSortingInfo > maOptions
Definition: sortdynres.hxx:46
virtual void SAL_CALL connectToCache(const css::uno::Reference< css::ucb::XDynamicResultSet > &xCache) override
Definition: sortdynres.cxx:169
void impl_notify(const css::ucb::ListEvent &Changes)
In the first notify-call the listener gets the two <type>XResultSet</type>s and has to hold them.
Definition: sortdynres.cxx:233
comphelper::OInterfaceContainerHelper4< css::lang::XEventListener > maDisposeEventListeners
Definition: sortdynres.hxx:41
virtual css::uno::Reference< css::sdbc::XResultSet > SAL_CALL getStaticResultSet() override
Definition: sortdynres.cxx:132
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: sortdynres.cxx:82
css::uno::Reference< css::ucb::XDynamicResultSet > mxOriginal
Definition: sortdynres.hxx:45
void ResortNew(EventList *pList)
void Remove(sal_Int32 nPos, sal_Int32 nCount, EventList *pList)
void ResortModified(EventList *pList)
void SetChanged(sal_Int32 nPos, sal_Int32 nCount)
void Move(sal_Int32 nPos, sal_Int32 nCount, sal_Int32 nOffset)
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
Definition: sortresult.cxx:742
sal_Int32 GetCount() const
Definition: sortresult.hxx:145
void CheckProperties(sal_Int32 nOldCount, bool bWasFinal)
void InsertNew(sal_Int32 nPos, sal_Int32 nCount)
sal_Int32 addInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
void disposeAndClear(::std::unique_lock<::std::mutex > &rGuard, const css::lang::EventObject &rEvt)
void clear(::std::unique_lock<::std::mutex > &rGuard)
sal_Int32 getLength(std::unique_lock< std::mutex > &rGuard) const
sal_Int32 removeInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
int nCount
Reference< XInterface > xTarget
sal_uInt16 nPos
@ Exception
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Info
int i
QPRO_FUNC_TYPE nType
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * ucb_SortedDynamicResultSetFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: sortdynres.cxx:424
unsigned char sal_Bool