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 :
21 : /**************************************************************************
22 : TODO
23 : **************************************************************************
24 :
25 : *************************************************************************/
26 :
27 : #include <vector>
28 : #include <ucbhelper/contentidentifier.hxx>
29 : #include "hierarchydatasupplier.hxx"
30 : #include "hierarchyprovider.hxx"
31 : #include "hierarchycontent.hxx"
32 :
33 : using namespace com::sun::star;
34 : using namespace hierarchy_ucp;
35 :
36 : namespace hierarchy_ucp
37 : {
38 :
39 : //=========================================================================
40 : //
41 : // struct ResultListEntry.
42 : //
43 : //=========================================================================
44 :
45 33 : struct ResultListEntry
46 : {
47 : OUString aId;
48 : uno::Reference< ucb::XContentIdentifier > xId;
49 : uno::Reference< ucb::XContent > xContent;
50 : uno::Reference< sdbc::XRow > xRow;
51 : HierarchyEntryData aData;
52 :
53 33 : ResultListEntry( const HierarchyEntryData& rEntry ) : aData( rEntry ) {}
54 : };
55 :
56 : //=========================================================================
57 : //
58 : // ResultList.
59 : //
60 : //=========================================================================
61 :
62 : typedef std::vector< ResultListEntry* > ResultList;
63 :
64 : //=========================================================================
65 : //
66 : // struct DataSupplier_Impl.
67 : //
68 : //=========================================================================
69 :
70 : struct DataSupplier_Impl
71 : {
72 : osl::Mutex m_aMutex;
73 : ResultList m_aResults;
74 : rtl::Reference< HierarchyContent > m_xContent;
75 : uno::Reference< uno::XComponentContext > m_xContext;
76 : HierarchyEntry m_aFolder;
77 : HierarchyEntry::iterator m_aIterator;
78 : sal_Int32 m_nOpenMode;
79 : sal_Bool m_bCountFinal;
80 :
81 10 : DataSupplier_Impl(
82 : const uno::Reference< uno::XComponentContext >& rxContext,
83 : const rtl::Reference< HierarchyContent >& rContent,
84 : sal_Int32 nOpenMode )
85 : : m_xContent( rContent ), m_xContext( rxContext ),
86 : m_aFolder( rxContext,
87 : static_cast< HierarchyContentProvider * >(
88 10 : rContent->getProvider().get() ),
89 20 : rContent->getIdentifier()->getContentIdentifier() ),
90 40 : m_nOpenMode( nOpenMode ), m_bCountFinal( sal_False ) {}
91 : ~DataSupplier_Impl();
92 : };
93 :
94 : //=========================================================================
95 20 : DataSupplier_Impl::~DataSupplier_Impl()
96 : {
97 10 : ResultList::const_iterator it = m_aResults.begin();
98 10 : ResultList::const_iterator end = m_aResults.end();
99 :
100 53 : while ( it != end )
101 : {
102 33 : delete (*it);
103 33 : ++it;
104 : }
105 10 : }
106 :
107 : }
108 :
109 : //=========================================================================
110 : //=========================================================================
111 : //
112 : // HierarchyResultSetDataSupplier Implementation.
113 : //
114 : //=========================================================================
115 : //=========================================================================
116 :
117 10 : HierarchyResultSetDataSupplier::HierarchyResultSetDataSupplier(
118 : const uno::Reference< uno::XComponentContext >& rxContext,
119 : const rtl::Reference< HierarchyContent >& rContent,
120 : sal_Int32 nOpenMode )
121 10 : : m_pImpl( new DataSupplier_Impl( rxContext, rContent, nOpenMode ) )
122 : {
123 10 : }
124 :
125 : //=========================================================================
126 : // virtual
127 30 : HierarchyResultSetDataSupplier::~HierarchyResultSetDataSupplier()
128 : {
129 10 : delete m_pImpl;
130 20 : }
131 :
132 : //=========================================================================
133 : // virtual
134 48 : OUString HierarchyResultSetDataSupplier::queryContentIdentifierString(
135 : sal_uInt32 nIndex )
136 : {
137 48 : osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
138 :
139 48 : if ( nIndex < m_pImpl->m_aResults.size() )
140 : {
141 48 : OUString aId = m_pImpl->m_aResults[ nIndex ]->aId;
142 48 : if ( !aId.isEmpty() )
143 : {
144 : // Already cached.
145 15 : return aId;
146 33 : }
147 : }
148 :
149 33 : if ( getResult( nIndex ) )
150 : {
151 : OUString aId
152 33 : = m_pImpl->m_xContent->getIdentifier()->getContentIdentifier();
153 :
154 33 : if ( ( aId.lastIndexOf( '/' ) + 1 ) != aId.getLength() )
155 33 : aId += OUString("/");
156 :
157 33 : aId += m_pImpl->m_aResults[ nIndex ]->aData.getName();
158 :
159 33 : m_pImpl->m_aResults[ nIndex ]->aId = aId;
160 33 : return aId;
161 : }
162 0 : return OUString();
163 : }
164 :
165 : //=========================================================================
166 : // virtual
167 : uno::Reference< ucb::XContentIdentifier >
168 0 : HierarchyResultSetDataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
169 : {
170 0 : osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
171 :
172 0 : if ( nIndex < m_pImpl->m_aResults.size() )
173 : {
174 : uno::Reference< ucb::XContentIdentifier > xId
175 0 : = m_pImpl->m_aResults[ nIndex ]->xId;
176 0 : if ( xId.is() )
177 : {
178 : // Already cached.
179 0 : return xId;
180 0 : }
181 : }
182 :
183 0 : OUString aId = queryContentIdentifierString( nIndex );
184 0 : if ( !aId.isEmpty() )
185 : {
186 : uno::Reference< ucb::XContentIdentifier > xId
187 0 : = new ::ucbhelper::ContentIdentifier( aId );
188 0 : m_pImpl->m_aResults[ nIndex ]->xId = xId;
189 0 : return xId;
190 : }
191 0 : return uno::Reference< ucb::XContentIdentifier >();
192 : }
193 :
194 : //=========================================================================
195 : // virtual
196 : uno::Reference< ucb::XContent >
197 0 : HierarchyResultSetDataSupplier::queryContent( sal_uInt32 nIndex )
198 : {
199 0 : osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
200 :
201 0 : if ( nIndex < m_pImpl->m_aResults.size() )
202 : {
203 : uno::Reference< ucb::XContent > xContent
204 0 : = m_pImpl->m_aResults[ nIndex ]->xContent;
205 0 : if ( xContent.is() )
206 : {
207 : // Already cached.
208 0 : return xContent;
209 0 : }
210 : }
211 :
212 : uno::Reference< ucb::XContentIdentifier > xId
213 0 : = queryContentIdentifier( nIndex );
214 0 : if ( xId.is() )
215 : {
216 : try
217 : {
218 : uno::Reference< ucb::XContent > xContent
219 0 : = m_pImpl->m_xContent->getProvider()->queryContent( xId );
220 0 : m_pImpl->m_aResults[ nIndex ]->xContent = xContent;
221 0 : return xContent;
222 :
223 : }
224 0 : catch ( ucb::IllegalIdentifierException const & )
225 : {
226 : }
227 : }
228 0 : return uno::Reference< ucb::XContent >();
229 : }
230 :
231 : //=========================================================================
232 : // virtual
233 81 : sal_Bool HierarchyResultSetDataSupplier::getResult( sal_uInt32 nIndex )
234 : {
235 81 : osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
236 :
237 81 : if ( m_pImpl->m_aResults.size() > nIndex )
238 : {
239 : // Result already present.
240 66 : return sal_True;
241 : }
242 :
243 : // Result not (yet) present.
244 :
245 15 : if ( m_pImpl->m_bCountFinal )
246 0 : return sal_False;
247 :
248 : // Try to obtain result...
249 :
250 15 : sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
251 15 : sal_Bool bFound = sal_False;
252 15 : sal_uInt32 nPos = nOldCount;
253 :
254 30 : while ( m_pImpl->m_aFolder.next( m_pImpl->m_aIterator ) )
255 : {
256 11 : const HierarchyEntryData& rResult = *m_pImpl->m_aIterator;
257 11 : if ( checkResult( rResult ) )
258 : {
259 11 : m_pImpl->m_aResults.push_back( new ResultListEntry( rResult ) );
260 :
261 11 : if ( nPos == nIndex )
262 : {
263 : // Result obtained.
264 11 : bFound = sal_True;
265 11 : break;
266 : }
267 : }
268 0 : nPos++;
269 : }
270 :
271 15 : if ( !bFound )
272 4 : m_pImpl->m_bCountFinal = sal_True;
273 :
274 30 : rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
275 15 : if ( xResultSet.is() )
276 : {
277 : // Callbacks follow!
278 15 : aGuard.clear();
279 :
280 15 : if ( nOldCount < m_pImpl->m_aResults.size() )
281 : xResultSet->rowCountChanged(
282 11 : nOldCount, m_pImpl->m_aResults.size() );
283 :
284 15 : if ( m_pImpl->m_bCountFinal )
285 4 : xResultSet->rowCountFinal();
286 : }
287 :
288 96 : return bFound;
289 : }
290 :
291 : //=========================================================================
292 : // virtual
293 138 : sal_uInt32 HierarchyResultSetDataSupplier::totalCount()
294 : {
295 138 : osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
296 :
297 138 : if ( m_pImpl->m_bCountFinal )
298 132 : return m_pImpl->m_aResults.size();
299 :
300 6 : sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
301 :
302 34 : while ( m_pImpl->m_aFolder.next( m_pImpl->m_aIterator ) )
303 : {
304 22 : const HierarchyEntryData& rResult = *m_pImpl->m_aIterator;
305 22 : if ( checkResult( rResult ) )
306 22 : m_pImpl->m_aResults.push_back( new ResultListEntry( rResult ) );
307 : }
308 :
309 6 : m_pImpl->m_bCountFinal = sal_True;
310 :
311 12 : rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
312 6 : if ( xResultSet.is() )
313 : {
314 : // Callbacks follow!
315 6 : aGuard.clear();
316 :
317 6 : if ( nOldCount < m_pImpl->m_aResults.size() )
318 : xResultSet->rowCountChanged(
319 4 : nOldCount, m_pImpl->m_aResults.size() );
320 :
321 6 : xResultSet->rowCountFinal();
322 : }
323 :
324 144 : return m_pImpl->m_aResults.size();
325 : }
326 :
327 : //=========================================================================
328 : // virtual
329 0 : sal_uInt32 HierarchyResultSetDataSupplier::currentCount()
330 : {
331 0 : return m_pImpl->m_aResults.size();
332 : }
333 :
334 : //=========================================================================
335 : // virtual
336 0 : sal_Bool HierarchyResultSetDataSupplier::isCountFinal()
337 : {
338 0 : return m_pImpl->m_bCountFinal;
339 : }
340 :
341 : //=========================================================================
342 : // virtual
343 : uno::Reference< sdbc::XRow >
344 157 : HierarchyResultSetDataSupplier::queryPropertyValues( sal_uInt32 nIndex )
345 : {
346 157 : osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
347 :
348 157 : if ( nIndex < m_pImpl->m_aResults.size() )
349 : {
350 : uno::Reference< sdbc::XRow > xRow
351 157 : = m_pImpl->m_aResults[ nIndex ]->xRow;
352 157 : if ( xRow.is() )
353 : {
354 : // Already cached.
355 124 : return xRow;
356 33 : }
357 : }
358 :
359 33 : if ( getResult( nIndex ) )
360 : {
361 : HierarchyContentProperties aData(
362 33 : m_pImpl->m_aResults[ nIndex ]->aData );
363 :
364 : uno::Reference< sdbc::XRow > xRow
365 : = HierarchyContent::getPropertyValues(
366 : m_pImpl->m_xContext,
367 66 : getResultSet()->getProperties(),
368 : aData,
369 : static_cast< HierarchyContentProvider * >(
370 33 : m_pImpl->m_xContent->getProvider().get() ),
371 132 : queryContentIdentifierString( nIndex ) );
372 33 : m_pImpl->m_aResults[ nIndex ]->xRow = xRow;
373 66 : return xRow;
374 : }
375 :
376 0 : return uno::Reference< sdbc::XRow >();
377 : }
378 :
379 : //=========================================================================
380 : // virtual
381 0 : void HierarchyResultSetDataSupplier::releasePropertyValues( sal_uInt32 nIndex )
382 : {
383 0 : osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
384 :
385 0 : if ( nIndex < m_pImpl->m_aResults.size() )
386 0 : m_pImpl->m_aResults[ nIndex ]->xRow = uno::Reference< sdbc::XRow >();
387 0 : }
388 :
389 : //=========================================================================
390 : // virtual
391 0 : void HierarchyResultSetDataSupplier::close()
392 : {
393 0 : }
394 :
395 : //=========================================================================
396 : // virtual
397 310 : void HierarchyResultSetDataSupplier::validate()
398 : throw( ucb::ResultSetException )
399 : {
400 310 : }
401 :
402 : //=========================================================================
403 33 : sal_Bool HierarchyResultSetDataSupplier::checkResult(
404 : const HierarchyEntryData& rResult )
405 : {
406 33 : switch ( m_pImpl->m_nOpenMode )
407 : {
408 : case ucb::OpenMode::FOLDERS:
409 6 : if ( rResult.getType() == HierarchyEntryData::LINK )
410 : {
411 : // Entry is a link.
412 0 : return sal_False;
413 : }
414 6 : break;
415 :
416 : case ucb::OpenMode::DOCUMENTS:
417 27 : if ( rResult.getType() == HierarchyEntryData::FOLDER )
418 : {
419 : // Entry is a folder.
420 0 : return sal_False;
421 : }
422 27 : break;
423 :
424 : case ucb::OpenMode::ALL:
425 : default:
426 0 : break;
427 : }
428 :
429 33 : return sal_True;
430 : }
431 :
432 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|