1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hierarchydatasupplier.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
34 /**************************************************************************
36 **************************************************************************
38 *************************************************************************/
41 #include <ucbhelper/contentidentifier.hxx>
42 #include "hierarchydatasupplier.hxx"
43 #include "hierarchyprovider.hxx"
44 #include "hierarchycontent.hxx"
46 using namespace com::sun::star
;
47 using namespace hierarchy_ucp
;
49 namespace hierarchy_ucp
52 //=========================================================================
54 // struct ResultListEntry.
56 //=========================================================================
58 struct ResultListEntry
61 uno::Reference
< ucb::XContentIdentifier
> xId
;
62 uno::Reference
< ucb::XContent
> xContent
;
63 uno::Reference
< sdbc::XRow
> xRow
;
64 HierarchyEntryData aData
;
66 ResultListEntry( const HierarchyEntryData
& rEntry
) : aData( rEntry
) {}
69 //=========================================================================
73 //=========================================================================
75 typedef std::vector
< ResultListEntry
* > ResultList
;
77 //=========================================================================
79 // struct DataSupplier_Impl.
81 //=========================================================================
83 struct DataSupplier_Impl
86 ResultList m_aResults
;
87 rtl::Reference
< HierarchyContent
> m_xContent
;
88 uno::Reference
< lang::XMultiServiceFactory
> m_xSMgr
;
89 HierarchyEntry m_aFolder
;
90 HierarchyEntry::iterator m_aIterator
;
91 sal_Int32 m_nOpenMode
;
92 sal_Bool m_bCountFinal
;
95 const uno::Reference
< lang::XMultiServiceFactory
>& rxSMgr
,
96 const rtl::Reference
< HierarchyContent
>& rContent
,
98 : m_xContent( rContent
), m_xSMgr( rxSMgr
),
100 static_cast< HierarchyContentProvider
* >(
101 rContent
->getProvider().get() ),
102 rContent
->getIdentifier()->getContentIdentifier() ),
103 m_nOpenMode( nOpenMode
), m_bCountFinal( sal_False
) {}
104 ~DataSupplier_Impl();
107 //=========================================================================
108 DataSupplier_Impl::~DataSupplier_Impl()
110 ResultList::const_iterator it
= m_aResults
.begin();
111 ResultList::const_iterator end
= m_aResults
.end();
122 //=========================================================================
123 //=========================================================================
125 // HierarchyResultSetDataSupplier Implementation.
127 //=========================================================================
128 //=========================================================================
130 HierarchyResultSetDataSupplier::HierarchyResultSetDataSupplier(
131 const uno::Reference
< lang::XMultiServiceFactory
>& rxSMgr
,
132 const rtl::Reference
< HierarchyContent
>& rContent
,
133 sal_Int32 nOpenMode
)
134 : m_pImpl( new DataSupplier_Impl( rxSMgr
, rContent
, nOpenMode
) )
138 //=========================================================================
140 HierarchyResultSetDataSupplier::~HierarchyResultSetDataSupplier()
145 //=========================================================================
147 rtl::OUString
HierarchyResultSetDataSupplier::queryContentIdentifierString(
150 osl::Guard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
152 if ( nIndex
< m_pImpl
->m_aResults
.size() )
154 rtl::OUString aId
= m_pImpl
->m_aResults
[ nIndex
]->aId
;
155 if ( aId
.getLength() )
162 if ( getResult( nIndex
) )
165 = m_pImpl
->m_xContent
->getIdentifier()->getContentIdentifier();
167 if ( ( aId
.lastIndexOf( '/' ) + 1 ) != aId
.getLength() )
168 aId
+= rtl::OUString::createFromAscii( "/" );
170 aId
+= m_pImpl
->m_aResults
[ nIndex
]->aData
.getName();
172 m_pImpl
->m_aResults
[ nIndex
]->aId
= aId
;
175 return rtl::OUString();
178 //=========================================================================
180 uno::Reference
< ucb::XContentIdentifier
>
181 HierarchyResultSetDataSupplier::queryContentIdentifier( sal_uInt32 nIndex
)
183 osl::Guard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
185 if ( nIndex
< m_pImpl
->m_aResults
.size() )
187 uno::Reference
< ucb::XContentIdentifier
> xId
188 = m_pImpl
->m_aResults
[ nIndex
]->xId
;
196 rtl::OUString aId
= queryContentIdentifierString( nIndex
);
197 if ( aId
.getLength() )
199 uno::Reference
< ucb::XContentIdentifier
> xId
200 = new ::ucbhelper::ContentIdentifier( aId
);
201 m_pImpl
->m_aResults
[ nIndex
]->xId
= xId
;
204 return uno::Reference
< ucb::XContentIdentifier
>();
207 //=========================================================================
209 uno::Reference
< ucb::XContent
>
210 HierarchyResultSetDataSupplier::queryContent( sal_uInt32 nIndex
)
212 osl::Guard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
214 if ( nIndex
< m_pImpl
->m_aResults
.size() )
216 uno::Reference
< ucb::XContent
> xContent
217 = m_pImpl
->m_aResults
[ nIndex
]->xContent
;
225 uno::Reference
< ucb::XContentIdentifier
> xId
226 = queryContentIdentifier( nIndex
);
231 uno::Reference
< ucb::XContent
> xContent
232 = m_pImpl
->m_xContent
->getProvider()->queryContent( xId
);
233 m_pImpl
->m_aResults
[ nIndex
]->xContent
= xContent
;
237 catch ( ucb::IllegalIdentifierException
const & )
241 return uno::Reference
< ucb::XContent
>();
244 //=========================================================================
246 sal_Bool
HierarchyResultSetDataSupplier::getResult( sal_uInt32 nIndex
)
248 osl::ClearableGuard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
250 if ( m_pImpl
->m_aResults
.size() > nIndex
)
252 // Result already present.
256 // Result not (yet) present.
258 if ( m_pImpl
->m_bCountFinal
)
261 // Try to obtain result...
263 sal_uInt32 nOldCount
= m_pImpl
->m_aResults
.size();
264 sal_Bool bFound
= sal_False
;
265 sal_uInt32 nPos
= nOldCount
;
267 while ( m_pImpl
->m_aFolder
.next( m_pImpl
->m_aIterator
) )
269 const HierarchyEntryData
& rResult
= *m_pImpl
->m_aIterator
;
270 if ( checkResult( rResult
) )
272 m_pImpl
->m_aResults
.push_back( new ResultListEntry( rResult
) );
274 if ( nPos
== nIndex
)
285 m_pImpl
->m_bCountFinal
= sal_True
;
287 rtl::Reference
< ::ucbhelper::ResultSet
> xResultSet
= getResultSet().get();
288 if ( xResultSet
.is() )
293 if ( nOldCount
< m_pImpl
->m_aResults
.size() )
294 xResultSet
->rowCountChanged(
295 nOldCount
, m_pImpl
->m_aResults
.size() );
297 if ( m_pImpl
->m_bCountFinal
)
298 xResultSet
->rowCountFinal();
304 //=========================================================================
306 sal_uInt32
HierarchyResultSetDataSupplier::totalCount()
308 osl::ClearableGuard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
310 if ( m_pImpl
->m_bCountFinal
)
311 return m_pImpl
->m_aResults
.size();
313 sal_uInt32 nOldCount
= m_pImpl
->m_aResults
.size();
315 while ( m_pImpl
->m_aFolder
.next( m_pImpl
->m_aIterator
) )
317 const HierarchyEntryData
& rResult
= *m_pImpl
->m_aIterator
;
318 if ( checkResult( rResult
) )
319 m_pImpl
->m_aResults
.push_back( new ResultListEntry( rResult
) );
322 m_pImpl
->m_bCountFinal
= sal_True
;
324 rtl::Reference
< ::ucbhelper::ResultSet
> xResultSet
= getResultSet().get();
325 if ( xResultSet
.is() )
330 if ( nOldCount
< m_pImpl
->m_aResults
.size() )
331 xResultSet
->rowCountChanged(
332 nOldCount
, m_pImpl
->m_aResults
.size() );
334 xResultSet
->rowCountFinal();
337 return m_pImpl
->m_aResults
.size();
340 //=========================================================================
342 sal_uInt32
HierarchyResultSetDataSupplier::currentCount()
344 return m_pImpl
->m_aResults
.size();
347 //=========================================================================
349 sal_Bool
HierarchyResultSetDataSupplier::isCountFinal()
351 return m_pImpl
->m_bCountFinal
;
354 //=========================================================================
356 uno::Reference
< sdbc::XRow
>
357 HierarchyResultSetDataSupplier::queryPropertyValues( sal_uInt32 nIndex
)
359 osl::Guard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
361 if ( nIndex
< m_pImpl
->m_aResults
.size() )
363 uno::Reference
< sdbc::XRow
> xRow
364 = m_pImpl
->m_aResults
[ nIndex
]->xRow
;
372 if ( getResult( nIndex
) )
374 static rtl::OUString
aFolderType(
375 rtl::OUString::createFromAscii( HIERARCHY_FOLDER_CONTENT_TYPE
) );
376 static rtl::OUString
aLinkType(
377 rtl::OUString::createFromAscii( HIERARCHY_LINK_CONTENT_TYPE
) );
379 HierarchyContentProperties
aData(
380 m_pImpl
->m_aResults
[ nIndex
]->aData
);
382 uno::Reference
< sdbc::XRow
> xRow
383 = HierarchyContent::getPropertyValues(
385 getResultSet()->getProperties(),
387 static_cast< HierarchyContentProvider
* >(
388 m_pImpl
->m_xContent
->getProvider().get() ),
389 queryContentIdentifierString( nIndex
) );
390 m_pImpl
->m_aResults
[ nIndex
]->xRow
= xRow
;
394 return uno::Reference
< sdbc::XRow
>();
397 //=========================================================================
399 void HierarchyResultSetDataSupplier::releasePropertyValues( sal_uInt32 nIndex
)
401 osl::Guard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
403 if ( nIndex
< m_pImpl
->m_aResults
.size() )
404 m_pImpl
->m_aResults
[ nIndex
]->xRow
= uno::Reference
< sdbc::XRow
>();
407 //=========================================================================
409 void HierarchyResultSetDataSupplier::close()
413 //=========================================================================
415 void HierarchyResultSetDataSupplier::validate()
416 throw( ucb::ResultSetException
)
420 //=========================================================================
421 sal_Bool
HierarchyResultSetDataSupplier::checkResult(
422 const HierarchyEntryData
& rResult
)
424 switch ( m_pImpl
->m_nOpenMode
)
426 case ucb::OpenMode::FOLDERS
:
427 if ( rResult
.getType() == HierarchyEntryData::LINK
)
434 case ucb::OpenMode::DOCUMENTS
:
435 if ( rResult
.getType() == HierarchyEntryData::FOLDER
)
437 // Entry is a folder.
442 case ucb::OpenMode::ALL
: