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// The mutex to synchronize access to containers.
40static osl::Mutex& getContainerMutex()
41{
42 static osl::Mutex ourMutex;
43
44 return ourMutex;
45}
46
47
48// SortedDynamicResultSet
49
51 const Reference < XDynamicResultSet > &xOriginal,
54 const Reference < XComponentContext > &rxContext )
55{
58
59 mxOriginal = xOriginal;
60 maOptions = aOptions;
61 mxCompFac = xCompFac;
62 m_xContext = rxContext;
63
64 mbGotWelcome = false;
65 mbUseOne = true;
66 mbStatic = false;
67}
68
69
71{
72 mxOwnListener->impl_OwnerDies();
73 mxOwnListener.clear();
74
76
77 mxOne.clear();
78 mxTwo.clear();
79 mxOriginal.clear();
80}
81
82// XServiceInfo methods.
83
85{
86 return "com.sun.star.comp.ucb.SortedDynamicResultSet";
87}
88
89sal_Bool SAL_CALL SortedDynamicResultSet::supportsService( const OUString& ServiceName )
90{
91 return cppu::supportsService( this, ServiceName );
92}
93
94css::uno::Sequence< OUString > SAL_CALL SortedDynamicResultSet::getSupportedServiceNames()
95{
96 return { "com.sun.star.ucb.SortedDynamicResultSet" };
97}
98
99// XComponent methods.
100
102{
103 osl::Guard< osl::Mutex > aGuard( maMutex );
104
106 {
107 EventObject aEvt;
108 aEvt.Source = static_cast< XComponent * >( this );
109 mpDisposeEventListeners->disposeAndClear( aEvt );
110 }
111
112 mxOne.clear();
113 mxTwo.clear();
114 mxOriginal.clear();
115
116 mbUseOne = true;
117}
118
120 const Reference< XEventListener >& Listener )
121{
122 osl::Guard< osl::Mutex > aGuard( maMutex );
123
127
128 mpDisposeEventListeners->addInterface( Listener );
129}
130
132 const Reference< XEventListener >& Listener )
133{
134 osl::Guard< osl::Mutex > aGuard( maMutex );
135
137 mpDisposeEventListeners->removeInterface( Listener );
138}
139
140
141// XDynamicResultSet methods.
142
145{
146 osl::Guard< osl::Mutex > aGuard( maMutex );
147
148 if ( mxListener.is() )
149 throw ListenerAlreadySetException();
150
151 mbStatic = true;
152
153 if ( mxOriginal.is() )
154 {
155 mxOne = new SortedResultSet( mxOriginal->getStaticResultSet() );
156 mxOne->Initialize( maOptions, mxCompFac );
157 }
158
159 return mxOne;
160}
161
162
163void SAL_CALL
165{
166 osl::Guard< osl::Mutex > aGuard( maMutex );
167
168 if ( mxListener.is() )
169 throw ListenerAlreadySetException();
170
171 addEventListener( Listener );
172
173 mxListener = Listener;
174
175 if ( mxOriginal.is() )
176 mxOriginal->setListener( mxOwnListener );
177}
178
179
180void SAL_CALL
182{
183 if( mxListener.is() )
184 throw ListenerAlreadySetException();
185
186 if( mbStatic )
187 throw ListenerAlreadySetException();
188
189 Reference< XSourceInitialization > xTarget( xCache, UNO_QUERY );
190 if( xTarget.is() && m_xContext.is() )
191 {
193 try
194 {
195 xStubFactory = CachedDynamicResultSetStubFactory::create( m_xContext );
196 }
197 catch ( Exception const & )
198 {
199 }
200
201 if( xStubFactory.is() )
202 {
203 xStubFactory->connectToCache(
204 this, xCache, Sequence< NumberedSortingInfo > (), nullptr );
205 return;
206 }
207 }
208 throw ServiceNotFoundException();
209}
210
211
213{
214 osl::Guard< osl::Mutex > aGuard( maMutex );
215
216 sal_Int16 nCaps = 0;
217
218 if ( mxOriginal.is() )
219 nCaps = mxOriginal->getCapabilities();
220
221 nCaps |= ContentResultSetCapability::SORTED;
222
223 return nCaps;
224}
225
226
227// XDynamicResultSetListener methods.
228
229
245void SortedDynamicResultSet::impl_notify( const ListEvent& Changes )
246{
247 osl::Guard< osl::Mutex > aGuard( maMutex );
248
249 bool bHasNew = false;
250 bool bHasModified = false;
251
252 SortedResultSet *pCurSet = nullptr;
253
254 // exchange mxNew and mxOld and immediately afterwards copy the tables
255 // from Old to New
256 if ( mbGotWelcome )
257 {
258 if ( mbUseOne )
259 {
260 mbUseOne = false;
261 mxTwo->CopyData( mxOne.get() );
262 pCurSet = mxTwo.get();
263 }
264 else
265 {
266 mbUseOne = true;
267 mxOne->CopyData( mxTwo.get() );
268 pCurSet = mxOne.get();
269 }
270 }
271
272 if (!pCurSet)
273 return;
274
275 Any aRet;
276
277 try {
278 aRet = pCurSet->getPropertyValue("IsRowCountFinal");
279 }
280 catch (const UnknownPropertyException&) {}
281 catch (const WrappedTargetException&) {}
282
283 sal_Int32 nOldCount = pCurSet->GetCount();
284 bool bWasFinal = false;
285
286 aRet >>= bWasFinal;
287
288 // handle the actions in the list
289 for ( const ListAction& aAction : Changes.Changes )
290 {
291 switch ( aAction.ListActionType )
292 {
293 case ListActionType::WELCOME:
294 {
295 WelcomeDynamicResultSetStruct aWelcome;
296 if ( aAction.ActionInfo >>= aWelcome )
297 {
298 mxTwo = new SortedResultSet( aWelcome.Old );
299 mxOne = new SortedResultSet( aWelcome.New );
300 mxOne->Initialize( maOptions, mxCompFac );
301 mbGotWelcome = true;
302 mbUseOne = true;
303 pCurSet = mxOne.get();
304
305 aWelcome.Old = mxTwo.get();
306 aWelcome.New = mxOne.get();
307
308 ListAction aWelcomeAction;
309 aWelcomeAction.ActionInfo <<= aWelcome;
310 aWelcomeAction.Position = 0;
311 aWelcomeAction.Count = 0;
312 aWelcomeAction.ListActionType = ListActionType::WELCOME;
313
314 maActions.Insert( aWelcomeAction );
315 }
316 else
317 {
318 // throw RuntimeException();
319 }
320 break;
321 }
322 case ListActionType::INSERTED:
323 {
324 pCurSet->InsertNew( aAction.Position, aAction.Count );
325 bHasNew = true;
326 break;
327 }
328 case ListActionType::REMOVED:
329 {
330 pCurSet->Remove( aAction.Position,
331 aAction.Count,
332 &maActions );
333 break;
334 }
335 case ListActionType::MOVED:
336 {
337 sal_Int32 nOffset = 0;
338 if ( aAction.ActionInfo >>= nOffset )
339 {
340 pCurSet->Move( aAction.Position,
341 aAction.Count,
342 nOffset );
343 }
344 break;
345 }
346 case ListActionType::PROPERTIES_CHANGED:
347 {
348 pCurSet->SetChanged( aAction.Position, aAction.Count );
349 bHasModified = true;
350 break;
351 }
352 default: break;
353 }
354 }
355
356 if ( bHasModified )
357 pCurSet->ResortModified( &maActions );
358
359 if ( bHasNew )
360 pCurSet->ResortNew( &maActions );
361
362 // send the new actions with a notify to the listeners
363 SendNotify();
364
365 // check for propertyChangeEvents
366 pCurSet->CheckProperties( nOldCount, bWasFinal );
367}
368
369// XEventListener
370
372{
373 mxListener.clear();
374 mxOriginal.clear();
375}
376
377// private methods
378
380{
381 sal_Int32 nCount = maActions.Count();
382
383 if ( nCount && mxListener.is() )
384 {
385 Sequence< ListAction > aActionList( maActions.Count() );
386 ListAction *pActionList = aActionList.getArray();
387
388 for ( sal_Int32 i=0; i<nCount; i++ )
389 {
390 pActionList[ i ] = maActions.GetAction( i );
391 }
392
393 ListEvent aNewEvent;
394 aNewEvent.Changes = aActionList;
395
396 mxListener->notify( aNewEvent );
397 }
398
399 // clean up
401}
402
403// SortedDynamicResultSetFactory
404
406 const Reference< XComponentContext > & rxContext )
407{
408 m_xContext = rxContext;
409}
410
411
413{
414}
415
416
417// XServiceInfo methods.
418
420{
421 return "com.sun.star.comp.ucb.SortedDynamicResultSetFactory";
422}
423
424sal_Bool SAL_CALL SortedDynamicResultSetFactory::supportsService( const OUString& ServiceName )
425{
426 return cppu::supportsService( this, ServiceName );
427}
428
429css::uno::Sequence< OUString > SAL_CALL SortedDynamicResultSetFactory::getSupportedServiceNames()
430{
431 return { "com.sun.star.ucb.SortedDynamicResultSetFactory" };
432}
433
434
435extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
437 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
438{
439 return cppu::acquire(new SortedDynamicResultSetFactory(context));
440}
441
442// SortedDynamicResultSetFactory methods.
443
446 const Reference< XDynamicResultSet > & Source,
448 const Reference< XAnyCompareFactory > & CompareFactory )
449{
451 return xRet;
452}
453
454// EventList
455
457{
458 maData.clear();
459}
460
461void EventList::AddEvent( sal_IntPtr nType, sal_Int32 nPos )
462{
463 ListAction aAction;
464 aAction.Position = nPos;
465 aAction.Count = 1;
466 aAction.ListActionType = nType;
467
468 Insert( aAction );
469}
470
471// SortedDynamicResultSetListener
472
474 SortedDynamicResultSet *mOwner )
475{
476 mpOwner = mOwner;
477}
478
479
481{
482}
483
484// XEventListener ( base of XDynamicResultSetListener )
485
486void SAL_CALL
487SortedDynamicResultSetListener::disposing( const EventObject& /*Source*/ )
488{
489 osl::Guard< osl::Mutex > aGuard( maMutex );
490
491 if ( mpOwner )
493}
494
495
496// XDynamicResultSetListener
497
498void SAL_CALL
499SortedDynamicResultSetListener::notify( const ListEvent& Changes )
500{
501 osl::Guard< osl::Mutex > aGuard( maMutex );
502
503 if ( mpOwner )
504 mpOwner->impl_notify( Changes );
505}
506
507// own methods:
508
509void
511{
512 osl::Guard< osl::Mutex > aGuard( maMutex );
513 mpOwner = nullptr;
514}
515
516/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::ucb::ListAction & GetAction(sal_Int32 nIndex)
Definition: sortresult.hxx:79
void AddEvent(sal_IntPtr nType, sal_Int32 nPos)
Definition: sortdynres.cxx:461
sal_uInt32 Count() const
Definition: sortresult.hxx:74
void Insert(const css::ucb::ListAction &rAction)
Definition: sortresult.hxx:77
void Clear()
Definition: sortdynres.cxx:456
std::deque< css::ucb::ListAction > maData
Definition: sortresult.hxx:69
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:445
SortedDynamicResultSetFactory(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
Definition: sortdynres.cxx:405
virtual ~SortedDynamicResultSetFactory() override
Definition: sortdynres.cxx:412
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: sortdynres.cxx:429
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: sortdynres.hxx:142
virtual OUString SAL_CALL getImplementationName() override
Definition: sortdynres.cxx:419
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: sortdynres.cxx:424
SortedDynamicResultSetListener(SortedDynamicResultSet *mOwner)
Definition: sortdynres.cxx:473
virtual ~SortedDynamicResultSetListener() override
Definition: sortdynres.cxx:480
SortedDynamicResultSet * mpOwner
Definition: sortdynres.hxx:115
virtual void SAL_CALL notify(const css::ucb::ListEvent &Changes) override
Definition: sortdynres.cxx:499
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
Definition: sortdynres.cxx:487
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:50
virtual OUString SAL_CALL getImplementationName() override
Definition: sortdynres.cxx:84
css::uno::Reference< css::ucb::XAnyCompareFactory > mxCompFac
Definition: sortdynres.hxx:47
virtual sal_Int16 SAL_CALL getCapabilities() override
Definition: sortdynres.cxx:212
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:70
virtual void SAL_CALL dispose() override
Definition: sortdynres.cxx:101
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:164
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &Listener) override
Definition: sortdynres.cxx:131
css::uno::Reference< css::ucb::XDynamicResultSetListener > mxListener
Definition: sortdynres.hxx:43
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: sortdynres.cxx:94
rtl::Reference< SortedResultSet > mxTwo
Definition: sortdynres.hxx:51
std::unique_ptr< comphelper::OInterfaceContainerHelper3< css::lang::XEventListener > > mpDisposeEventListeners
Definition: sortdynres.hxx:41
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &Listener) override
Definition: sortdynres.cxx:119
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:181
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:245
virtual css::uno::Reference< css::sdbc::XResultSet > SAL_CALL getStaticResultSet() override
Definition: sortdynres.cxx:144
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: sortdynres.cxx:89
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:762
sal_Int32 GetCount() const
Definition: sortresult.hxx:143
void CheckProperties(sal_Int32 nOldCount, bool bWasFinal)
void InsertNew(sal_Int32 nPos, sal_Int32 nCount)
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:436
static osl::Mutex & getContainerMutex()
Definition: sortdynres.cxx:40
unsigned char sal_Bool