Debian Codegraph @

ucb/source/ucp/hierarchy/hierarchydatasupplier.cxx

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