xref: /trunk/main/ucb/source/ucp/ftp/ftpresultsetbase.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_ftp.hxx"
26 #include <ucbhelper/contentidentifier.hxx>
27 #include <com/sun/star/ucb/OpenMode.hpp>
28 #include <com/sun/star/uno/Reference.h>
29 #ifndef _COM_SUN_STAR_BEANS_PROPERTYATTRIBBUTE_HPP_
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #endif
32 #include <com/sun/star/ucb/ListActionType.hpp>
33 #include <com/sun/star/ucb/XSourceInitialization.hpp>
34 #include <ucbhelper/resultsetmetadata.hxx>
35 #include "ftpresultsetbase.hxx"
36 
37 using namespace ftp;
38 using namespace com::sun::star;
39 
ResultSetBase(const uno::Reference<lang::XMultiServiceFactory> & xMSF,const uno::Reference<ucb::XContentProvider> & xProvider,sal_Int32 nOpenMode,const uno::Sequence<beans::Property> & seq,const uno::Sequence<ucb::NumberedSortingInfo> & seqSort)40 ResultSetBase::ResultSetBase(
41     const uno::Reference< lang::XMultiServiceFactory >&  xMSF,
42     const uno::Reference< ucb::XContentProvider >&  xProvider,
43     sal_Int32 nOpenMode,
44     const uno::Sequence< beans::Property >& seq,
45     const uno::Sequence< ucb::NumberedSortingInfo >& seqSort )
46     : m_xMSF( xMSF ),
47       m_xProvider( xProvider ),
48       m_nRow( -1 ),
49       m_nWasNull( true ),
50       m_nOpenMode( nOpenMode ),
51       m_bRowCountFinal( true ),
52       m_sProperty( seq ),
53       m_sSortingInfo( seqSort ),
54       m_pDisposeEventListeners( 0 ),
55       m_pRowCountListeners( 0 ),
56       m_pIsFinalListeners( 0 )
57 {
58 }
59 
~ResultSetBase()60 ResultSetBase::~ResultSetBase()
61 {
62     delete m_pIsFinalListeners;
63     delete m_pRowCountListeners;
64     delete m_pDisposeEventListeners;
65 }
66 
67 
68 // XInterface
69 
70 void SAL_CALL
acquire(void)71 ResultSetBase::acquire(
72     void )
73     throw()
74 {
75     OWeakObject::acquire();
76 }
77 
78 
79 void SAL_CALL
release(void)80 ResultSetBase::release(
81     void )
82     throw()
83 {
84     OWeakObject::release();
85 }
86 
87 
88 
89 uno::Any SAL_CALL
queryInterface(const uno::Type & rType)90 ResultSetBase::queryInterface(
91     const uno::Type& rType )
92 {
93     uno::Any aRet = cppu::queryInterface(
94         rType,
95         SAL_STATIC_CAST( lang::XComponent*, this),
96         SAL_STATIC_CAST( sdbc::XRow*, this),
97         SAL_STATIC_CAST( sdbc::XResultSet*, this),
98         SAL_STATIC_CAST( sdbc::XResultSetMetaDataSupplier*, this),
99         SAL_STATIC_CAST( beans::XPropertySet*, this ),
100         SAL_STATIC_CAST( ucb::XContentAccess*, this) );
101     return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
102 }
103 
104 
105 
106 // XComponent
107 
108 
109 void SAL_CALL
addEventListener(const uno::Reference<lang::XEventListener> & Listener)110 ResultSetBase::addEventListener(
111     const uno::Reference< lang::XEventListener >& Listener )
112 {
113     osl::MutexGuard aGuard( m_aMutex );
114 
115     if ( ! m_pDisposeEventListeners )
116         m_pDisposeEventListeners =
117             new cppu::OInterfaceContainerHelper( m_aMutex );
118 
119     m_pDisposeEventListeners->addInterface( Listener );
120 }
121 
122 
123 void SAL_CALL
removeEventListener(const uno::Reference<lang::XEventListener> & Listener)124 ResultSetBase::removeEventListener(
125     const uno::Reference< lang::XEventListener >& Listener )
126 {
127     osl::MutexGuard aGuard( m_aMutex );
128 
129     if ( m_pDisposeEventListeners )
130         m_pDisposeEventListeners->removeInterface( Listener );
131 }
132 
133 
134 
135 void SAL_CALL
dispose()136 ResultSetBase::dispose()
137 {
138     osl::MutexGuard aGuard( m_aMutex );
139 
140     lang::EventObject aEvt;
141     aEvt.Source = static_cast< lang::XComponent * >( this );
142 
143     if ( m_pDisposeEventListeners && m_pDisposeEventListeners->getLength() )
144     {
145         m_pDisposeEventListeners->disposeAndClear( aEvt );
146     }
147     if( m_pRowCountListeners && m_pRowCountListeners->getLength() )
148     {
149         m_pRowCountListeners->disposeAndClear( aEvt );
150     }
151     if( m_pIsFinalListeners && m_pIsFinalListeners->getLength() )
152     {
153         m_pIsFinalListeners->disposeAndClear( aEvt );
154     }
155 }
156 
157 
158 
159 //  XResultSet
160 
161 sal_Bool SAL_CALL
next(void)162 ResultSetBase::next(
163     void )
164 {
165     sal_Bool test;
166     if( ++m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size()) )
167         test = true;
168     else
169         test = false;
170     return test;
171 }
172 
173 
174 sal_Bool SAL_CALL
isBeforeFirst(void)175 ResultSetBase::isBeforeFirst(
176     void )
177 {
178     return m_nRow == -1;
179 }
180 
181 
182 sal_Bool SAL_CALL
isAfterLast(void)183 ResultSetBase::isAfterLast(
184     void )
185 {
186     return m_nRow >= sal::static_int_cast<sal_Int32>(m_aItems.size());   // Cannot happen, if m_aFolder.isOpen()
187 }
188 
189 
190 sal_Bool SAL_CALL
isFirst(void)191 ResultSetBase::isFirst(
192     void )
193 {
194     return m_nRow == 0;
195 }
196 
197 
198 sal_Bool SAL_CALL
isLast(void)199 ResultSetBase::isLast(
200     void  )
201 {
202     if( m_nRow ==  sal::static_int_cast<sal_Int32>(m_aItems.size()) - 1 )
203         return true;
204     else
205         return false;
206 }
207 
208 
209 void SAL_CALL
beforeFirst(void)210 ResultSetBase::beforeFirst(
211     void  )
212 {
213     m_nRow = -1;
214 }
215 
216 
217 void SAL_CALL
afterLast(void)218 ResultSetBase::afterLast(
219     void  )
220 {
221     m_nRow = m_aItems.size();
222 }
223 
224 
225 sal_Bool SAL_CALL
first(void)226 ResultSetBase::first(
227     void  )
228 {
229     m_nRow = -1;
230     return next();
231 }
232 
233 
234 sal_Bool SAL_CALL
last(void)235 ResultSetBase::last(
236     void  )
237 {
238     m_nRow = m_aItems.size() - 1;
239     return true;
240 }
241 
242 
243 sal_Int32 SAL_CALL
getRow(void)244 ResultSetBase::getRow(
245     void )
246 {
247     // Test, whether behind last row
248     if( -1 == m_nRow || m_nRow >= sal::static_int_cast<sal_Int32>(m_aItems.size()) )
249         return 0;
250     else
251         return m_nRow+1;
252 }
253 
254 
absolute(sal_Int32 row)255 sal_Bool SAL_CALL ResultSetBase::absolute( sal_Int32 row )
256 {
257     if( row >= 0 )
258         m_nRow = row - 1;
259     else
260     {
261         last();
262         m_nRow += ( row + 1 );
263         if( m_nRow < -1 )
264             m_nRow = -1;
265     }
266 
267     return 0<= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size());
268 }
269 
270 
271 
272 
273 sal_Bool SAL_CALL
relative(sal_Int32 row)274 ResultSetBase::relative(
275     sal_Int32 row )
276 {
277     if( isAfterLast() || isBeforeFirst() )
278         throw sdbc::SQLException();
279 
280     if( row > 0 )
281         while( row-- )
282             next();
283     else if( row < 0 )
284         while( row++ && m_nRow > - 1 )
285             previous();
286 
287     return 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size());
288 }
289 
290 
291 
292 sal_Bool SAL_CALL
previous(void)293 ResultSetBase::previous(
294     void )
295 {
296     if( m_nRow > sal::static_int_cast<sal_Int32>(m_aItems.size()) )
297         m_nRow = m_aItems.size();  // Correct Handling of afterLast
298     if( 0 <= m_nRow ) -- m_nRow;
299 
300     return 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size());
301 }
302 
303 
304 void SAL_CALL
refreshRow(void)305 ResultSetBase::refreshRow(
306     void )
307 {
308 }
309 
310 
311 sal_Bool SAL_CALL
rowUpdated(void)312 ResultSetBase::rowUpdated(
313     void )
314 {
315     return false;
316 }
317 
318 sal_Bool SAL_CALL
rowInserted(void)319 ResultSetBase::rowInserted(
320     void  )
321 {
322     return false;
323 }
324 
325 sal_Bool SAL_CALL
rowDeleted(void)326 ResultSetBase::rowDeleted(
327     void  )
328 {
329     return false;
330 }
331 
332 
333 uno::Reference< uno::XInterface > SAL_CALL
getStatement(void)334 ResultSetBase::getStatement(
335     void  )
336 {
337     uno::Reference< uno::XInterface > test( 0 );
338     return test;
339 }
340 
341 
342 // XCloseable
343 
344 void SAL_CALL
close(void)345 ResultSetBase::close(
346     void )
347 {
348 }
349 
350 
351 rtl::OUString SAL_CALL
queryContentIdentifierString(void)352 ResultSetBase::queryContentIdentifierString(
353     void )
354 {
355     if( 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size()) )
356         return m_aPath[m_nRow];
357     else
358         return rtl::OUString();
359 }
360 
361 
362 uno::Reference< ucb::XContentIdentifier > SAL_CALL
queryContentIdentifier(void)363 ResultSetBase::queryContentIdentifier(
364     void
365 )
366 {
367     if( 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size()) )
368     {
369         if(!m_aIdents[m_nRow].is()) {
370             rtl::OUString url = queryContentIdentifierString();
371             if(url.getLength() )
372                 m_aIdents[m_nRow] =
373                     uno::Reference< ucb::XContentIdentifier >(
374                         new ::ucbhelper::ContentIdentifier(m_xMSF,url) );
375         }
376         return m_aIdents[m_nRow];
377     }
378 
379     return uno::Reference<ucb::XContentIdentifier>();
380 }
381 
382 
383 uno::Reference< ucb::XContent > SAL_CALL
queryContent(void)384 ResultSetBase::queryContent(
385     void )
386 {
387     if( 0 <= m_nRow && m_nRow < sal::static_int_cast<sal_Int32>(m_aItems.size()) )
388         return m_xProvider->queryContent(queryContentIdentifier());
389     else
390         return uno::Reference< ucb::XContent >();
391 }
392 
393 
394 
395 class XPropertySetInfoImpl
396     : public cppu::OWeakObject,
397       public beans::XPropertySetInfo
398 {
399 public:
400 
XPropertySetInfoImpl(const uno::Sequence<beans::Property> & aSeq)401     XPropertySetInfoImpl( const uno::Sequence< beans::Property >& aSeq )
402         : m_aSeq( aSeq )
403     {
404     }
405 
acquire(void)406     void SAL_CALL acquire( void )
407         throw()
408     {
409         OWeakObject::acquire();
410     }
411 
412 
release(void)413     void SAL_CALL release( void )
414         throw()
415     {
416         OWeakObject::release();
417     }
418 
queryInterface(const uno::Type & rType)419     uno::Any SAL_CALL queryInterface( const uno::Type& rType )
420     {
421         uno::Any aRet = cppu::queryInterface(
422             rType,
423             SAL_STATIC_CAST( beans::XPropertySetInfo*, this ) );
424         return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
425     }
426 
getProperties()427     uno::Sequence< beans::Property > SAL_CALL getProperties()
428     {
429         return m_aSeq;
430     }
431 
getPropertyByName(const::rtl::OUString & aName)432     beans::Property SAL_CALL getPropertyByName( const ::rtl::OUString& aName )
433     {
434         for( int i = 0; i < m_aSeq.getLength(); ++i )
435             if( aName == m_aSeq[i].Name )
436                 return m_aSeq[i];
437         throw beans::UnknownPropertyException();
438     }
439 
hasPropertyByName(const::rtl::OUString & Name)440     sal_Bool SAL_CALL hasPropertyByName( const ::rtl::OUString& Name )
441     {
442         for( int i = 0; i < m_aSeq.getLength(); ++i )
443             if( Name == m_aSeq[i].Name )
444                 return true;
445         return false;
446     }
447 
448 private:
449 
450     uno::Sequence< beans::Property > m_aSeq;
451 };
452 
453 
454 
455 // XPropertySet
456 uno::Reference< beans::XPropertySetInfo > SAL_CALL
getPropertySetInfo()457 ResultSetBase::getPropertySetInfo()
458 {
459     uno::Sequence< beans::Property > seq(2);
460     seq[0].Name = rtl::OUString::createFromAscii( "RowCount" );
461     seq[0].Handle = -1;
462     seq[0].Type = getCppuType( static_cast< sal_Int32* >(0) );
463     seq[0].Attributes = beans::PropertyAttribute::READONLY;
464 
465     seq[1].Name = rtl::OUString::createFromAscii( "IsRowCountFinal" );
466     seq[1].Handle = -1;
467     seq[1].Type = getCppuType( static_cast< sal_Bool* >(0) );
468     seq[1].Attributes = beans::PropertyAttribute::READONLY;
469 
470     //t
471     return uno::Reference< beans::XPropertySetInfo > (
472         new XPropertySetInfoImpl( seq ) );
473 }
474 
475 
476 
setPropertyValue(const rtl::OUString & aPropertyName,const uno::Any &)477 void SAL_CALL ResultSetBase::setPropertyValue(
478     const rtl::OUString& aPropertyName, const uno::Any& /*aValue*/ )
479 {
480     if( aPropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) ||
481         aPropertyName == rtl::OUString::createFromAscii( "RowCount" ) )
482         return;
483 
484     throw beans::UnknownPropertyException();
485 }
486 
487 
getPropertyValue(const rtl::OUString & PropertyName)488 uno::Any SAL_CALL ResultSetBase::getPropertyValue(
489     const rtl::OUString& PropertyName )
490 {
491     if( PropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) )
492     {
493         uno::Any aAny;
494         aAny <<= m_bRowCountFinal;
495         return aAny;
496     }
497     else if ( PropertyName == rtl::OUString::createFromAscii( "RowCount" ) )
498     {
499         uno::Any aAny;
500         sal_Int32 count = m_aItems.size();
501         aAny <<= count;
502         return aAny;
503     }
504     else
505         throw beans::UnknownPropertyException();
506 }
507 
508 
addPropertyChangeListener(const rtl::OUString & aPropertyName,const uno::Reference<beans::XPropertyChangeListener> & xListener)509 void SAL_CALL ResultSetBase::addPropertyChangeListener(
510     const rtl::OUString& aPropertyName,
511     const uno::Reference< beans::XPropertyChangeListener >& xListener )
512 {
513     if( aPropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) )
514     {
515         osl::MutexGuard aGuard( m_aMutex );
516         if ( ! m_pIsFinalListeners )
517             m_pIsFinalListeners =
518                 new cppu::OInterfaceContainerHelper( m_aMutex );
519 
520         m_pIsFinalListeners->addInterface( xListener );
521     }
522     else if ( aPropertyName == rtl::OUString::createFromAscii( "RowCount" ) )
523     {
524         osl::MutexGuard aGuard( m_aMutex );
525         if ( ! m_pRowCountListeners )
526             m_pRowCountListeners =
527                 new cppu::OInterfaceContainerHelper( m_aMutex );
528         m_pRowCountListeners->addInterface( xListener );
529     }
530     else
531         throw beans::UnknownPropertyException();
532 }
533 
534 
removePropertyChangeListener(const rtl::OUString & aPropertyName,const uno::Reference<beans::XPropertyChangeListener> & aListener)535 void SAL_CALL ResultSetBase::removePropertyChangeListener(
536     const rtl::OUString& aPropertyName,
537     const uno::Reference< beans::XPropertyChangeListener >& aListener )
538 {
539     if( aPropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) &&
540         m_pIsFinalListeners )
541     {
542         osl::MutexGuard aGuard( m_aMutex );
543         m_pIsFinalListeners->removeInterface( aListener );
544     }
545     else if ( aPropertyName == rtl::OUString::createFromAscii( "RowCount" ) &&
546               m_pRowCountListeners )
547     {
548         osl::MutexGuard aGuard( m_aMutex );
549         m_pRowCountListeners->removeInterface( aListener );
550     }
551     else
552         throw beans::UnknownPropertyException();
553 }
554 
555 
addVetoableChangeListener(const rtl::OUString &,const uno::Reference<beans::XVetoableChangeListener> &)556 void SAL_CALL ResultSetBase::addVetoableChangeListener(
557     const rtl::OUString& /*PropertyName*/,
558     const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
559 {
560 }
561 
562 
removeVetoableChangeListener(const rtl::OUString &,const uno::Reference<beans::XVetoableChangeListener> &)563 void SAL_CALL ResultSetBase::removeVetoableChangeListener(
564     const rtl::OUString& /*PropertyName*/,
565     const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
566 {
567 }
568 
569 
570 
571 // XResultSetMetaDataSupplier
572 uno::Reference< sdbc::XResultSetMetaData > SAL_CALL
getMetaData(void)573 ResultSetBase::getMetaData(
574     void )
575 {
576     ::ucbhelper::ResultSetMetaData* p =
577           new ::ucbhelper::ResultSetMetaData(
578               m_xMSF, m_sProperty );
579     return uno::Reference< sdbc::XResultSetMetaData >( p );
580 }
581