build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / dbaccess / source / core / dataaccess / myucp_datasupplier.cxx
bloba9ea47f3ad9ed613eefff085fe1372f9f9d042d5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vector>
22 #include <ucbhelper/contentidentifier.hxx>
23 #include <ucbhelper/providerhelper.hxx>
25 #include "myucp_datasupplier.hxx"
26 #include "ContentHelper.hxx"
27 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
28 #include <tools/debug.hxx>
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::ucb;
32 using namespace ::com::sun::star::beans;
33 using namespace ::com::sun::star::lang;
34 using namespace ::com::sun::star::sdbc;
35 using namespace ::com::sun::star::io;
36 using namespace ::com::sun::star::container;
38 // @@@ Adjust namespace name.
39 using namespace dbaccess;
41 // @@@ Adjust namespace name.
42 namespace dbaccess
45 // struct ResultListEntry.
46 struct ResultListEntry
48 OUString aId;
49 Reference< XContentIdentifier > xId;
50 ::rtl::Reference< OContentHelper > xContent;
51 Reference< XRow > xRow;
52 const ContentProperties& rData;
54 explicit ResultListEntry(const ContentProperties& rEntry) : rData( rEntry ) {}
57 // ResultList.
58 typedef std::vector< ResultListEntry* > ResultList;
60 // struct DataSupplier_Impl.
61 struct DataSupplier_Impl
63 osl::Mutex m_aMutex;
64 ResultList m_aResults;
65 rtl::Reference< ODocumentContainer > m_xContent;
66 bool m_bCountFinal;
68 explicit DataSupplier_Impl(const rtl::Reference< ODocumentContainer >& rContent)
69 : m_xContent(rContent)
70 , m_bCountFinal(false)
73 ~DataSupplier_Impl();
76 DataSupplier_Impl::~DataSupplier_Impl()
78 ResultList::const_iterator it = m_aResults.begin();
79 ResultList::const_iterator end = m_aResults.end();
81 while ( it != end )
83 delete (*it);
84 ++it;
90 // DataSupplier Implementation.
92 DataSupplier::DataSupplier( const rtl::Reference< ODocumentContainer >& rContent )
93 : m_pImpl( new DataSupplier_Impl( rContent ) )
98 DataSupplier::~DataSupplier()
103 OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
105 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
107 if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
109 OUString aId = m_pImpl->m_aResults[ nIndex ]->aId;
110 if ( !aId.isEmpty() )
112 // Already cached.
113 return aId;
117 if ( getResult( nIndex ) )
119 OUString aId = m_pImpl->m_xContent->getIdentifier()->getContentIdentifier();
121 if ( !aId.isEmpty() )
122 aId += "/";
124 aId += m_pImpl->m_aResults[ nIndex ]->rData.aTitle;
126 m_pImpl->m_aResults[ nIndex ]->aId = aId;
127 return aId;
129 return OUString();
132 Reference< XContentIdentifier >
133 DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
135 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
137 if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
139 Reference< XContentIdentifier > xId = m_pImpl->m_aResults[ nIndex ]->xId;
140 if ( xId.is() )
142 // Already cached.
143 return xId;
147 OUString aId = queryContentIdentifierString( nIndex );
148 if ( !aId.isEmpty() )
150 Reference< XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( aId );
151 m_pImpl->m_aResults[ nIndex ]->xId = xId;
152 return xId;
154 return Reference< XContentIdentifier >();
157 Reference< XContent >
158 DataSupplier::queryContent( sal_uInt32 _nIndex )
160 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
162 if ( (size_t)_nIndex < m_pImpl->m_aResults.size() )
164 Reference< XContent > xContent = m_pImpl->m_aResults[ _nIndex ]->xContent.get();
165 if ( xContent.is() )
167 // Already cached.
168 return xContent;
172 Reference< XContentIdentifier > xId = queryContentIdentifier( _nIndex );
173 if ( xId.is() )
177 Reference< XContent > xContent;
178 OUString sName = xId->getContentIdentifier();
179 sal_Int32 nIndex = sName.lastIndexOf('/') + 1;
180 sName = sName.getToken(0,'/',nIndex);
182 m_pImpl->m_aResults[ _nIndex ]->xContent = m_pImpl->m_xContent->getContent(sName);
184 xContent = m_pImpl->m_aResults[ _nIndex ]->xContent.get();
185 return xContent;
188 catch ( IllegalIdentifierException& )
192 return Reference< XContent >();
195 bool DataSupplier::getResult( sal_uInt32 nIndex )
197 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
199 if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
201 // Result already present.
202 return true;
205 // Result not (yet) present.
207 if ( m_pImpl->m_bCountFinal )
208 return false;
210 // Try to obtain result...
212 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
213 bool bFound = false;
214 sal_uInt32 nPos = nOldCount;
216 // @@@ Obtain data and put it into result list...
217 Sequence< OUString> aSeq = m_pImpl->m_xContent->getElementNames();
218 if ( nIndex < sal::static_int_cast< sal_uInt32 >( aSeq.getLength() ) )
220 const OUString* pIter = aSeq.getConstArray();
221 const OUString* pEnd = pIter + aSeq.getLength();
222 for(pIter = pIter + nPos;pIter != pEnd;++pIter,++nPos)
224 m_pImpl->m_aResults.push_back(
225 new ResultListEntry( m_pImpl->m_xContent->getContent(*pIter)->getContentProperties() ) );
227 if ( nPos == nIndex )
229 // Result obtained.
230 bFound = true;
231 break;
236 if ( !bFound )
237 m_pImpl->m_bCountFinal = true;
239 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
240 if ( xResultSet.is() )
242 // Callbacks follow!
243 aGuard.clear();
245 if ( (size_t)nOldCount < m_pImpl->m_aResults.size() )
246 xResultSet->rowCountChanged(
247 nOldCount, m_pImpl->m_aResults.size() );
249 if ( m_pImpl->m_bCountFinal )
250 xResultSet->rowCountFinal();
253 return bFound;
256 sal_uInt32 DataSupplier::totalCount()
258 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
260 if ( m_pImpl->m_bCountFinal )
261 return m_pImpl->m_aResults.size();
263 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
265 // @@@ Obtain data and put it into result list...
266 Sequence< OUString> aSeq = m_pImpl->m_xContent->getElementNames();
267 const OUString* pIter = aSeq.getConstArray();
268 const OUString* pEnd = pIter + aSeq.getLength();
269 for(;pIter != pEnd;++pIter)
270 m_pImpl->m_aResults.push_back(
271 new ResultListEntry( m_pImpl->m_xContent->getContent(*pIter)->getContentProperties() ) );
273 m_pImpl->m_bCountFinal = true;
275 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
276 if ( xResultSet.is() )
278 // Callbacks follow!
279 aGuard.clear();
281 if ( (size_t)nOldCount < m_pImpl->m_aResults.size() )
282 xResultSet->rowCountChanged(
283 nOldCount, m_pImpl->m_aResults.size() );
285 xResultSet->rowCountFinal();
288 return m_pImpl->m_aResults.size();
291 sal_uInt32 DataSupplier::currentCount()
293 return m_pImpl->m_aResults.size();
296 bool DataSupplier::isCountFinal()
298 return m_pImpl->m_bCountFinal;
301 Reference< XRow >
302 DataSupplier::queryPropertyValues( sal_uInt32 nIndex )
304 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
306 if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
308 Reference< XRow > xRow = m_pImpl->m_aResults[ nIndex ]->xRow;
309 if ( xRow.is() )
311 // Already cached.
312 return xRow;
316 if ( getResult( nIndex ) )
318 if ( !m_pImpl->m_aResults[ nIndex ]->xContent.is() )
319 queryContent(nIndex);
321 Reference< XRow > xRow = m_pImpl->m_aResults[ nIndex ]->xContent->getPropertyValues(getResultSet()->getProperties());
322 m_pImpl->m_aResults[ nIndex ]->xRow = xRow;
323 return xRow;
326 return Reference< XRow >();
329 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
331 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
333 if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
334 m_pImpl->m_aResults[ nIndex ]->xRow.clear();
337 void DataSupplier::close()
341 void DataSupplier::validate()
342 throw( ResultSetException )
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */