Line data Source code
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 <vector>
21 :
22 : #include <ucbhelper/contentidentifier.hxx>
23 : #include <ucbhelper/providerhelper.hxx>
24 :
25 : #include "myucp_datasupplier.hxx"
26 : #include "ContentHelper.hxx"
27 : #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
28 : #include <tools/debug.hxx>
29 :
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;
37 :
38 : // @@@ Adjust namespace name.
39 : using namespace dbaccess;
40 :
41 : // @@@ Adjust namespace name.
42 : namespace dbaccess
43 : {
44 :
45 : //=========================================================================
46 : //
47 : // struct ResultListEntry.
48 : //
49 : //=========================================================================
50 :
51 0 : struct ResultListEntry
52 : {
53 : rtl::OUString aId;
54 : Reference< XContentIdentifier > xId;
55 : ::rtl::Reference< OContentHelper > xContent;
56 : Reference< XRow > xRow;
57 : const ContentProperties& rData;
58 :
59 0 : ResultListEntry( const ContentProperties& rEntry ) : rData( rEntry ) {}
60 : };
61 :
62 : //=========================================================================
63 : //
64 : // ResultList.
65 : //
66 : //=========================================================================
67 :
68 : typedef std::vector< ResultListEntry* > ResultList;
69 :
70 : //=========================================================================
71 : //
72 : // struct DataSupplier_Impl.
73 : //
74 : //=========================================================================
75 :
76 : struct DataSupplier_Impl
77 : {
78 : osl::Mutex m_aMutex;
79 : ResultList m_aResults;
80 : rtl::Reference< ODocumentContainer > m_xContent;
81 : sal_Int32 m_nOpenMode;
82 : sal_Bool m_bCountFinal;
83 :
84 0 : DataSupplier_Impl( const rtl::Reference< ODocumentContainer >& rContent,
85 : sal_Int32 nOpenMode )
86 : : m_xContent(rContent)
87 : , m_nOpenMode( nOpenMode )
88 0 : , m_bCountFinal( sal_False ) {}
89 : ~DataSupplier_Impl();
90 : };
91 :
92 : //=========================================================================
93 0 : DataSupplier_Impl::~DataSupplier_Impl()
94 : {
95 0 : ResultList::const_iterator it = m_aResults.begin();
96 0 : ResultList::const_iterator end = m_aResults.end();
97 :
98 0 : while ( it != end )
99 : {
100 0 : delete (*it);
101 0 : ++it;
102 : }
103 0 : }
104 :
105 : }
106 :
107 : //=========================================================================
108 : //
109 : // DataSupplier Implementation.
110 : //
111 : //=========================================================================
112 : DBG_NAME(DataSupplier)
113 :
114 0 : DataSupplier::DataSupplier( const rtl::Reference< ODocumentContainer >& rContent,
115 : sal_Int32 nOpenMode )
116 0 : : m_pImpl( new DataSupplier_Impl( rContent,nOpenMode ) )
117 : {
118 : DBG_CTOR(DataSupplier,NULL);
119 :
120 0 : }
121 :
122 0 : DataSupplier::~DataSupplier()
123 : {
124 :
125 : DBG_DTOR(DataSupplier,NULL);
126 0 : }
127 :
128 0 : rtl::OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
129 : {
130 0 : osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
131 :
132 0 : if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
133 : {
134 0 : rtl::OUString aId = m_pImpl->m_aResults[ nIndex ]->aId;
135 0 : if ( !aId.isEmpty() )
136 : {
137 : // Already cached.
138 0 : return aId;
139 0 : }
140 : }
141 :
142 0 : if ( getResult( nIndex ) )
143 : {
144 : rtl::OUString aId
145 0 : = m_pImpl->m_xContent->getIdentifier()->getContentIdentifier();
146 :
147 0 : if ( !aId.isEmpty() )
148 0 : aId += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
149 :
150 0 : aId += m_pImpl->m_aResults[ nIndex ]->rData.aTitle;
151 :
152 0 : m_pImpl->m_aResults[ nIndex ]->aId = aId;
153 0 : return aId;
154 : }
155 0 : return rtl::OUString();
156 : }
157 :
158 : Reference< XContentIdentifier >
159 0 : DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
160 : {
161 0 : osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
162 :
163 0 : if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
164 : {
165 0 : Reference< XContentIdentifier > xId = m_pImpl->m_aResults[ nIndex ]->xId;
166 0 : if ( xId.is() )
167 : {
168 : // Already cached.
169 0 : return xId;
170 0 : }
171 : }
172 :
173 0 : rtl::OUString aId = queryContentIdentifierString( nIndex );
174 0 : if ( !aId.isEmpty() )
175 : {
176 0 : Reference< XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( aId );
177 0 : m_pImpl->m_aResults[ nIndex ]->xId = xId;
178 0 : return xId;
179 : }
180 0 : return Reference< XContentIdentifier >();
181 : }
182 :
183 : Reference< XContent >
184 0 : DataSupplier::queryContent( sal_uInt32 _nIndex )
185 : {
186 0 : osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
187 :
188 0 : if ( (size_t)_nIndex < m_pImpl->m_aResults.size() )
189 : {
190 0 : Reference< XContent > xContent = m_pImpl->m_aResults[ _nIndex ]->xContent.get();
191 0 : if ( xContent.is() )
192 : {
193 : // Already cached.
194 0 : return xContent;
195 0 : }
196 : }
197 :
198 0 : Reference< XContentIdentifier > xId = queryContentIdentifier( _nIndex );
199 0 : if ( xId.is() )
200 : {
201 : try
202 : {
203 0 : Reference< XContent > xContent;
204 0 : ::rtl::OUString sName = xId->getContentIdentifier();
205 0 : sal_Int32 nIndex = sName.lastIndexOf('/') + 1;
206 0 : sName = sName.getToken(0,'/',nIndex);
207 :
208 0 : m_pImpl->m_aResults[ _nIndex ]->xContent = m_pImpl->m_xContent->getContent(sName);
209 :
210 0 : xContent = m_pImpl->m_aResults[ _nIndex ]->xContent.get();
211 0 : return xContent;
212 :
213 : }
214 0 : catch ( IllegalIdentifierException& )
215 : {
216 : }
217 : }
218 0 : return Reference< XContent >();
219 : }
220 :
221 0 : sal_Bool DataSupplier::getResult( sal_uInt32 nIndex )
222 : {
223 0 : osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
224 :
225 0 : if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
226 : {
227 : // Result already present.
228 0 : return sal_True;
229 : }
230 :
231 : // Result not (yet) present.
232 :
233 0 : if ( m_pImpl->m_bCountFinal )
234 0 : return sal_False;
235 :
236 : // Try to obtain result...
237 :
238 0 : sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
239 0 : sal_Bool bFound = sal_False;
240 0 : sal_uInt32 nPos = nOldCount;
241 :
242 : // @@@ Obtain data and put it into result list...
243 0 : Sequence< ::rtl::OUString> aSeq = m_pImpl->m_xContent->getElementNames();
244 0 : if ( nIndex < sal::static_int_cast< sal_uInt32 >( aSeq.getLength() ) )
245 : {
246 0 : const ::rtl::OUString* pIter = aSeq.getConstArray();
247 0 : const ::rtl::OUString* pEnd = pIter + aSeq.getLength();
248 0 : for(pIter = pIter + nPos;pIter != pEnd;++pIter,++nPos)
249 : {
250 0 : m_pImpl->m_aResults.push_back(
251 0 : new ResultListEntry( m_pImpl->m_xContent->getContent(*pIter)->getContentProperties() ) );
252 :
253 0 : if ( nPos == nIndex )
254 : {
255 : // Result obtained.
256 0 : bFound = sal_True;
257 0 : break;
258 : }
259 : }
260 : }
261 :
262 0 : if ( !bFound )
263 0 : m_pImpl->m_bCountFinal = sal_True;
264 :
265 0 : rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
266 0 : if ( xResultSet.is() )
267 : {
268 : // Callbacks follow!
269 0 : aGuard.clear();
270 :
271 0 : if ( (size_t)nOldCount < m_pImpl->m_aResults.size() )
272 : xResultSet->rowCountChanged(
273 0 : nOldCount, m_pImpl->m_aResults.size() );
274 :
275 0 : if ( m_pImpl->m_bCountFinal )
276 0 : xResultSet->rowCountFinal();
277 : }
278 :
279 0 : return bFound;
280 : }
281 :
282 0 : sal_uInt32 DataSupplier::totalCount()
283 : {
284 0 : osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
285 :
286 0 : if ( m_pImpl->m_bCountFinal )
287 0 : return m_pImpl->m_aResults.size();
288 :
289 0 : sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
290 :
291 : // @@@ Obtain data and put it into result list...
292 0 : Sequence< ::rtl::OUString> aSeq = m_pImpl->m_xContent->getElementNames();
293 0 : const ::rtl::OUString* pIter = aSeq.getConstArray();
294 0 : const ::rtl::OUString* pEnd = pIter + aSeq.getLength();
295 0 : for(;pIter != pEnd;++pIter)
296 0 : m_pImpl->m_aResults.push_back(
297 0 : new ResultListEntry( m_pImpl->m_xContent->getContent(*pIter)->getContentProperties() ) );
298 :
299 0 : m_pImpl->m_bCountFinal = sal_True;
300 :
301 0 : rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
302 0 : if ( xResultSet.is() )
303 : {
304 : // Callbacks follow!
305 0 : aGuard.clear();
306 :
307 0 : if ( (size_t)nOldCount < m_pImpl->m_aResults.size() )
308 : xResultSet->rowCountChanged(
309 0 : nOldCount, m_pImpl->m_aResults.size() );
310 :
311 0 : xResultSet->rowCountFinal();
312 : }
313 :
314 0 : return m_pImpl->m_aResults.size();
315 : }
316 :
317 0 : sal_uInt32 DataSupplier::currentCount()
318 : {
319 0 : return m_pImpl->m_aResults.size();
320 : }
321 :
322 0 : sal_Bool DataSupplier::isCountFinal()
323 : {
324 0 : return m_pImpl->m_bCountFinal;
325 : }
326 :
327 : Reference< XRow >
328 0 : DataSupplier::queryPropertyValues( sal_uInt32 nIndex )
329 : {
330 0 : osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
331 :
332 0 : if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
333 : {
334 0 : Reference< XRow > xRow = m_pImpl->m_aResults[ nIndex ]->xRow;
335 0 : if ( xRow.is() )
336 : {
337 : // Already cached.
338 0 : return xRow;
339 0 : }
340 : }
341 :
342 0 : if ( getResult( nIndex ) )
343 : {
344 0 : if ( !m_pImpl->m_aResults[ nIndex ]->xContent.is() )
345 0 : queryContent(nIndex);
346 :
347 0 : Reference< XRow > xRow = m_pImpl->m_aResults[ nIndex ]->xContent->getPropertyValues(getResultSet()->getProperties());
348 0 : m_pImpl->m_aResults[ nIndex ]->xRow = xRow;
349 0 : return xRow;
350 : }
351 :
352 0 : return Reference< XRow >();
353 : }
354 :
355 0 : void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
356 : {
357 0 : osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
358 :
359 0 : if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
360 0 : m_pImpl->m_aResults[ nIndex ]->xRow = Reference< XRow >();
361 0 : }
362 :
363 0 : void DataSupplier::close()
364 : {
365 0 : }
366 :
367 0 : void DataSupplier::validate()
368 : throw( ResultSetException )
369 : {
370 0 : }
371 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|