Debian Codegraph @

ucb/source/ucp/file/filrset.cxx

raw
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 <sal/config.h>
21 #include <sal/log.hxx>
22
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>
34
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>
40
41 using namespace fileaccess;
42 using namespace com::sun::star;
43
44 #if OSL_DEBUG_LEVEL > 0
45 #define THROW_WHERE SAL_WHERE
46 #else
47 #define THROW_WHERE ""
48 #endif
49
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( TaskHandlerErr::NO_ERROR )
65 , m_nMinorErrorCode( 0 )
66 {
67 osl::FileBase::RC err = m_aFolder.open();
68 if( err != osl::FileBase::E_None )
69 {
70 m_nIsOpen = false;
71 m_aFolder.close();
72
73 m_nErrorCode = TaskHandlerErr::OPEN_FOR_DIRECTORYLISTING;
74 m_nMinorErrorCode = err;
75 }
76 else
77 m_nIsOpen = true;
78 }
79
80
81 XResultSet_impl::~XResultSet_impl()
82 {
83 if( m_nIsOpen )
84 m_aFolder.close();
85 }
86
87
88 void SAL_CALL
89 XResultSet_impl::disposing( const lang::EventObject& )
90 {
91 // To do, but what
92 }
93
94
95 void SAL_CALL
96 XResultSet_impl::addEventListener(
97 const uno::Reference< lang::XEventListener >& Listener )
98 {
99 std::unique_lock aGuard( m_aMutex );
100
101 m_aDisposeEventListeners.addInterface( aGuard, Listener );
102 }
103
104
105 void SAL_CALL
106 XResultSet_impl::removeEventListener(
107 const uno::Reference< lang::XEventListener >& Listener )
108 {
109 std::unique_lock aGuard( m_aMutex );
110
111 m_aDisposeEventListeners.removeInterface( aGuard, Listener );
112 }
113
114
115 void SAL_CALL
116 XResultSet_impl::dispose()
117 {
118 std::unique_lock aGuard( m_aMutex );
119
120 lang::EventObject aEvt;
121 aEvt.Source = static_cast< lang::XComponent * >( this );
122
123 m_aDisposeEventListeners.disposeAndClear( aGuard, aEvt );
124 m_aRowCountListeners.disposeAndClear( aGuard, aEvt );
125 m_aIsFinalListeners.disposeAndClear( aGuard, aEvt );
126 }
127
128
129 void XResultSet_impl::rowCountChanged(std::unique_lock<std::mutex>& rGuard)
130 {
131 sal_Int32 aNewValue = m_aItems.size();
132 sal_Int32 aOldValue = aNewValue-1;
133 beans::PropertyChangeEvent aEv;
134 aEv.PropertyName = "RowCount";
135 aEv.Further = false;
136 aEv.PropertyHandle = -1;
137 aEv.OldValue <<= aOldValue;
138 aEv.NewValue <<= aNewValue;
139 m_aRowCountListeners.notifyEach(rGuard, &beans::XPropertyChangeListener::propertyChange, aEv);
140 }
141
142
143 void XResultSet_impl::isFinalChanged(std::unique_lock<std::mutex>& rGuard)
144 {
145 m_bRowCountFinal = true;
146 beans::PropertyChangeEvent aEv;
147 aEv.PropertyName = "IsRowCountFinal";
148 aEv.Further = false;
149 aEv.PropertyHandle = -1;
150 aEv.OldValue <<= false;
151 aEv.NewValue <<= true;
152 m_aIsFinalListeners.notifyEach(rGuard, &beans::XPropertyChangeListener::propertyChange, aEv);
153 }
154
155
156 bool
157 XResultSet_impl::OneMore(std::unique_lock<std::mutex>& rGuard)
158 {
159 if( ! m_nIsOpen )
160 return false;
161
162 osl::FileBase::RC err;
163 bool IsRegular;
164 OUString aUnqPath;
165 osl::DirectoryItem aDirIte;
166 uno::Reference< sdbc::XRow > aRow;
167
168 while( true )
169 {
170 err = m_aFolder.getNextItem( aDirIte );
171
172 if( err == osl::FileBase::E_NOENT || err == osl::FileBase::E_INVAL )
173 {
174 m_aFolder.close();
175 isFinalChanged(rGuard);
176 m_nIsOpen = false;
177 return m_nIsOpen;
178 }
179 else if( err == osl::FileBase::E_None )
180 {
181 if (!m_pMyShell->getv( m_sProperty, aDirIte, aUnqPath, IsRegular, aRow ))
182 {
183 SAL_WARN(
184 "ucb.ucp.file",
185 "getting dir item in <" << m_aBaseDirectory << "> failed");
186 continue;
187 }
188
189 if( m_nOpenMode == ucb::OpenMode::DOCUMENTS && IsRegular )
190 {
191 m_aItems.push_back( aRow );
192 m_aIdents.emplace_back( );
193 m_aUnqPath.push_back( aUnqPath );
194 rowCountChanged(rGuard);
195 return true;
196
197 }
198 else if( m_nOpenMode == ucb::OpenMode::DOCUMENTS && ! IsRegular )
199 {
200 continue;
201 }
202 else if( m_nOpenMode == ucb::OpenMode::FOLDERS && ! IsRegular )
203 {
204 m_aItems.push_back( aRow );
205 m_aIdents.emplace_back( );
206 m_aUnqPath.push_back( aUnqPath );
207 rowCountChanged(rGuard);
208 return true;
209 }
210 else if( m_nOpenMode == ucb::OpenMode::FOLDERS && IsRegular )
211 {
212 continue;
213 }
214 else
215 {
216 m_aItems.push_back( aRow );
217 m_aIdents.emplace_back( );
218 m_aUnqPath.push_back( aUnqPath );
219 rowCountChanged(rGuard);
220 return true;
221 }
222 }
223 else // error fetching anything
224 {
225 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
226 }
227 }
228 }
229
230
231 sal_Bool SAL_CALL
232 XResultSet_impl::next()
233 {
234 std::unique_lock aGuard( m_aMutex );
235 bool test;
236 if( ++m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size()) ) test = true;
237 else
238 test = OneMore(aGuard);
239 return test;
240 }
241
242
243 sal_Bool SAL_CALL
244 XResultSet_impl::isBeforeFirst()
245 {
246 return m_nRow == -1;
247 }
248
249
250 sal_Bool SAL_CALL
251 XResultSet_impl::isAfterLast()
252 {
253 return m_nRow >= sal::static_int_cast<sal_Int32>(m_aItems.size()); // Cannot happen, if m_aFolder.isOpen()
254 }
255
256
257 sal_Bool SAL_CALL
258 XResultSet_impl::isFirst()
259 {
260 return m_nRow == 0;
261 }
262
263
264 sal_Bool SAL_CALL
265 XResultSet_impl::isLast()
266 {
267 std::unique_lock aGuard( m_aMutex );
268 if( m_nRow == sal::static_int_cast<sal_Int32>(m_aItems.size()) - 1 )
269 return ! OneMore(aGuard);
270 else
271 return false;
272 }
273
274
275 void SAL_CALL
276 XResultSet_impl::beforeFirst()
277 {
278 m_nRow = -1;
279 }
280
281
282 void SAL_CALL
283 XResultSet_impl::afterLast()
284 {
285 std::unique_lock aGuard( m_aMutex );
286 m_nRow = sal::static_int_cast<sal_Int32>(m_aItems.size());
287 while( OneMore(aGuard) )
288 ++m_nRow;
289 }
290
291
292 sal_Bool SAL_CALL
293 XResultSet_impl::first()
294 {
295 m_nRow = -1;
296 return next();
297 }
298
299
300 sal_Bool SAL_CALL
301 XResultSet_impl::last()
302 {
303 std::unique_lock aGuard( m_aMutex );
304 m_nRow = sal::static_int_cast<sal_Int32>(m_aItems.size()) - 1;
305 while( OneMore(aGuard) )
306 ++m_nRow;
307 return true;
308 }
309
310
311 sal_Int32 SAL_CALL
312 XResultSet_impl::getRow()
313 {
314 // Test, whether behind last row
315 if( -1 == m_nRow || m_nRow >= sal::static_int_cast<sal_Int32>(m_aItems.size()) )
316 return 0;
317 else
318 return m_nRow+1;
319 }
320
321
322 sal_Bool SAL_CALL XResultSet_impl::absolute( sal_Int32 row )
323 {
324 std::unique_lock aGuard( m_aMutex );
325 if( row >= 0 )
326 {
327 m_nRow = row - 1;
328 if( row >= sal::static_int_cast<sal_Int32>(m_aItems.size()) )
329 while( row-- && OneMore(aGuard) )
330 ;
331 }
332 else
333 {
334 last();
335 m_nRow += ( row + 1 );
336 if( m_nRow < -1 )
337 m_nRow = -1;
338 }
339
340 return 0<= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size());
341 }
342
343
344 sal_Bool SAL_CALL
345 XResultSet_impl::relative( sal_Int32 row )
346 {
347 if( isAfterLast() || isBeforeFirst() )
348 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
349 if( row > 0 )
350 while( row-- ) next();
351 else if( row < 0 )
352 while( row++ && m_nRow > - 1 ) previous();
353
354 return 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size());
355 }
356
357
358 sal_Bool SAL_CALL
359 XResultSet_impl::previous()
360 {
361 if( m_nRow > sal::static_int_cast<sal_Int32>(m_aItems.size()) )
362 m_nRow = sal::static_int_cast<sal_Int32>(m_aItems.size()); // Correct Handling of afterLast
363 if( 0 <= m_nRow ) -- m_nRow;
364
365 return 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size());
366 }
367
368
369 void SAL_CALL
370 XResultSet_impl::refreshRow()
371 {
372 // get the row from the filesystem
373 }
374
375
376 sal_Bool SAL_CALL
377 XResultSet_impl::rowUpdated()
378 {
379 return false;
380 }
381
382 sal_Bool SAL_CALL
383 XResultSet_impl::rowInserted()
384 {
385 return false;
386 }
387
388 sal_Bool SAL_CALL
389 XResultSet_impl::rowDeleted()
390 {
391 return false;
392 }
393
394
395 uno::Reference< uno::XInterface > SAL_CALL
396 XResultSet_impl::getStatement()
397 {
398 return uno::Reference< uno::XInterface >();
399 }
400
401
402 // XCloseable
403
404 void SAL_CALL
405 XResultSet_impl::close()
406 {
407 std::unique_lock aGuard( m_aMutex );
408 if( m_nIsOpen )
409 {
410 m_aFolder.close();
411 isFinalChanged(aGuard);
412 m_nIsOpen = false;
413 }
414 }
415
416
417 OUString SAL_CALL
418 XResultSet_impl::queryContentIdentifierString()
419 {
420 uno::Reference< ucb::XContentIdentifier > xContentId
421 = queryContentIdentifier();
422
423 if( xContentId.is() )
424 return xContentId->getContentIdentifier();
425 else
426 return OUString();
427 }
428
429
430 uno::Reference< ucb::XContentIdentifier > SAL_CALL
431 XResultSet_impl::queryContentIdentifier()
432 {
433 std::unique_lock aGuard( m_aMutex );
434 if( 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size()) )
435 {
436 if( ! m_aIdents[m_nRow].is() )
437 {
438 m_aIdents[m_nRow].set( new FileContentIdentifier( m_aUnqPath[ m_nRow ] ) );
439 }
440 return m_aIdents[m_nRow];
441 }
442 return uno::Reference< ucb::XContentIdentifier >();
443 }
444
445
446 uno::Reference< ucb::XContent > SAL_CALL
447 XResultSet_impl::queryContent()
448 {
449 if( 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size()) )
450 return m_pMyShell->m_pProvider->queryContent( queryContentIdentifier() );
451 else
452 return uno::Reference< ucb::XContent >();
453 }
454
455
456 // XDynamicResultSet
457
458
459 // virtual
460 uno::Reference< sdbc::XResultSet > SAL_CALL
461 XResultSet_impl::getStaticResultSet()
462 {
463 std::unique_lock aGuard( m_aMutex );
464
465 if ( m_xListener.is() )
466 throw ucb::ListenerAlreadySetException( THROW_WHERE );
467
468 return uno::Reference< sdbc::XResultSet >( this );
469 }
470
471
472 // virtual
473 void SAL_CALL
474 XResultSet_impl::setListener(
475 const uno::Reference< ucb::XDynamicResultSetListener >& Listener )
476 {
477 std::unique_lock aGuard( m_aMutex );
478
479 if ( m_xListener.is() )
480 throw ucb::ListenerAlreadySetException( THROW_WHERE );
481
482 m_xListener = Listener;
483
484
485 // Create "welcome event" and send it to listener.
486
487
488 // Note: We only have the implementation for a static result set at the
489 // moment (src590). The dynamic result sets passed to the listener
490 // are a fake. This implementation will never call "notify" at the
491 // listener to propagate any changes!!!
492
493 uno::Any aInfo;
494 aInfo <<= ucb::WelcomeDynamicResultSetStruct( this, /* "old" */
495 this /* "new" */ );
496
497 uno::Sequence< ucb::ListAction > aActions( 1 );
498 aActions.getArray()[ 0 ] = ucb::ListAction( 0, // Position; not used
499 0, // Count; not used
500 ucb::ListActionType::WELCOME,
501 aInfo );
502 aGuard.unlock();
503
504 Listener->notify(
505 ucb::ListEvent(
506 getXWeak(), aActions ) );
507 }
508
509
510 // virtual
511 void SAL_CALL
512 XResultSet_impl::connectToCache(
513 const uno::Reference< ucb::XDynamicResultSet > & xCache )
514 {
515 {
516 std::unique_lock aGuard( m_aMutex );
517 if( m_xListener.is() )
518 throw ucb::ListenerAlreadySetException( THROW_WHERE );
519 }
520 uno::Reference< ucb::XSourceInitialization > xTarget(
521 xCache, uno::UNO_QUERY );
522 if( xTarget.is() && m_pMyShell->m_xContext.is() )
523 {
524 uno::Reference< ucb::XCachedDynamicResultSetStubFactory > xStubFactory;
525 try
526 {
527 xStubFactory
528 = ucb::CachedDynamicResultSetStubFactory::create(
529 m_pMyShell->m_xContext );
530 }
531 catch ( uno::Exception const & )
532 {
533 }
534
535 if( xStubFactory.is() )
536 {
537 xStubFactory->connectToCache(
538 this, xCache,m_sSortingInfo, nullptr );
539 return;
540 }
541 }
542 throw ucb::ServiceNotFoundException( THROW_WHERE );
543 }
544
545
546 // virtual
547 sal_Int16 SAL_CALL
548 XResultSet_impl::getCapabilities()
549 {
550 // Never set ucb::ContentResultSetCapability::SORTED
551 // - Underlying content cannot provide sorted data...
552 return 0;
553 }
554
555 // XResultSetMetaDataSupplier
556 uno::Reference< sdbc::XResultSetMetaData > SAL_CALL
557 XResultSet_impl::getMetaData()
558 {
559 auto pProp = std::find_if(std::cbegin(m_sProperty), std::cend(m_sProperty),
560 [](const beans::Property& rProp) { return rProp.Name == "Title"; });
561 if (pProp != std::cend(m_sProperty))
562 {
563 std::vector< ::ucbhelper::ResultSetColumnData >
564 aColumnData( m_sProperty.getLength() );
565 auto n = std::distance(std::cbegin(m_sProperty), pProp);
566 // @@@ #82177# - Determine correct value!
567 aColumnData[ n ].isCaseSensitive = false;
568
569 return new ::ucbhelper::ResultSetMetaData(
570 m_pMyShell->m_xContext,
571 m_sProperty,
572 std::move(aColumnData) );
573 }
574
575 return new ::ucbhelper::ResultSetMetaData( m_pMyShell->m_xContext, m_sProperty );
576 }
577
578
579 // XPropertySet
580 uno::Reference< beans::XPropertySetInfo > SAL_CALL
581 XResultSet_impl::getPropertySetInfo()
582 {
583
584 uno::Sequence< beans::Property > seq
585 {
586 { u"RowCount"_ustr, -1, cppu::UnoType<sal_Int32>::get(), beans::PropertyAttribute::READONLY },
587 { u"IsRowCountFinal"_ustr, -1, cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY }
588 };
589
590 return new XPropertySetInfo_impl( m_pMyShell, seq );
591 }
592
593
594 void SAL_CALL XResultSet_impl::setPropertyValue(
595 const OUString& aPropertyName, const uno::Any& )
596 {
597 if( aPropertyName == "IsRowCountFinal" ||
598 aPropertyName == "RowCount" )
599 return;
600 throw beans::UnknownPropertyException( aPropertyName );
601 }
602
603
604 uno::Any SAL_CALL XResultSet_impl::getPropertyValue(
605 const OUString& PropertyName )
606 {
607 std::unique_lock aGuard( m_aMutex );
608 if( PropertyName == "IsRowCountFinal" )
609 {
610 return uno::Any(m_bRowCountFinal);
611 }
612 else if ( PropertyName == "RowCount" )
613 {
614 sal_Int32 count = sal::static_int_cast<sal_Int32>(m_aItems.size());
615 return uno::Any(count);
616 }
617 else
618 throw beans::UnknownPropertyException( PropertyName );
619 }
620
621
622 void SAL_CALL XResultSet_impl::addPropertyChangeListener(
623 const OUString& aPropertyName,
624 const uno::Reference< beans::XPropertyChangeListener >& xListener )
625 {
626 if( aPropertyName == "IsRowCountFinal" )
627 {
628 std::unique_lock aGuard( m_aMutex );
629
630 m_aIsFinalListeners.addInterface( aGuard, xListener );
631 }
632 else if ( aPropertyName == "RowCount" )
633 {
634 std::unique_lock aGuard( m_aMutex );
635
636 m_aRowCountListeners.addInterface( aGuard, xListener );
637 }
638 else
639 throw beans::UnknownPropertyException( aPropertyName );
640 }
641
642
643 void SAL_CALL XResultSet_impl::removePropertyChangeListener(
644 const OUString& aPropertyName,
645 const uno::Reference< beans::XPropertyChangeListener >& aListener )
646 {
647 if( aPropertyName == "IsRowCountFinal" )
648 {
649 std::unique_lock aGuard( m_aMutex );
650
651 m_aIsFinalListeners.removeInterface( aGuard, aListener );
652 }
653 else if ( aPropertyName == "RowCount" )
654 {
655 std::unique_lock aGuard( m_aMutex );
656
657 m_aRowCountListeners.removeInterface( aGuard, aListener );
658 }
659 else
660 throw beans::UnknownPropertyException( aPropertyName );
661 }
662
663 void SAL_CALL XResultSet_impl::addVetoableChangeListener(
664 const OUString&,
665 const uno::Reference< beans::XVetoableChangeListener >& )
666 {
667 }
668
669
670 void SAL_CALL XResultSet_impl::removeVetoableChangeListener(
671 const OUString&,
672 const uno::Reference< beans::XVetoableChangeListener >& )
673 {
674 }
675
676 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */