LibreOffice Module ucb (master) 1
ucpext_datasupplier.cxx
Go to the documentation of this file.
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
22#include "ucpext_content.hxx"
23#include "ucpext_provider.hxx"
24
25#include <com/sun/star/deployment/PackageInformationProvider.hpp>
26#include <com/sun/star/ucb/IllegalIdentifierException.hpp>
27#include <o3tl/string_view.hxx>
30#include <ucbhelper/content.hxx>
33#include <rtl/ustrbuf.hxx>
34#include <sal/log.hxx>
35
36#include <memory>
37#include <string_view>
38#include <utility>
39
40
41namespace ucb::ucp::ext
42{
43
44
45 using ::com::sun::star::uno::Reference;
46 using ::com::sun::star::uno::UNO_QUERY_THROW;
47 using ::com::sun::star::uno::UNO_SET_THROW;
48 using ::com::sun::star::uno::Exception;
49 using ::com::sun::star::uno::Sequence;
50 using ::com::sun::star::uno::XComponentContext;
51 using ::com::sun::star::ucb::XContent;
52 using ::com::sun::star::ucb::XContentIdentifier;
53 using ::com::sun::star::sdbc::XRow;
54 using ::com::sun::star::ucb::IllegalIdentifierException;
55 using ::com::sun::star::deployment::PackageInformationProvider;
56 using ::com::sun::star::deployment::XPackageInformationProvider;
57 using ::com::sun::star::sdbc::XResultSet;
58
59
60 //= helper
61
62 namespace
63 {
64 OUString lcl_compose( std::u16string_view i_rBaseURL, const OUString& i_rRelativeURL )
65 {
66 ENSURE_OR_RETURN( !i_rBaseURL.empty(), "illegal base URL", i_rRelativeURL );
67
68 OUStringBuffer aComposer( i_rBaseURL );
69 if ( !o3tl::ends_with(i_rBaseURL, u"/") )
70 aComposer.append( '/' );
71 aComposer.append( i_rRelativeURL );
72 return aComposer.makeStringAndClear();
73 }
74 }
75
76
77 //= DataSupplier
78
79
80 DataSupplier::DataSupplier( const Reference< XComponentContext >& rxContext,
82 :m_xContent(std::move( i_xContent ))
83 ,m_xContext( rxContext )
84 {
85 }
86
87
88 void DataSupplier::fetchData()
89 {
90 try
91 {
92 const Reference< XPackageInformationProvider > xPackageInfo = PackageInformationProvider::get( m_xContext );
93
94 const OUString sContentIdentifier( m_xContent->getIdentifier()->getContentIdentifier() );
95
96 switch ( m_xContent->getExtensionContentType() )
97 {
98 case E_ROOT:
99 {
100 const Sequence< Sequence< OUString > > aExtensionInfo( xPackageInfo->getExtensionList() );
101 for ( auto const & extInfo : aExtensionInfo )
102 {
103 if ( !extInfo.hasElements() )
104 {
105 SAL_WARN( "ucb.ucp.ext", "illegal extension info" );
106 continue;
107 }
108
109 const OUString& rLocalId = extInfo[0];
110 ResultListEntry aEntry;
111 aEntry.sId = ContentProvider::getRootURL() + Content::encodeIdentifier( rLocalId ) + "/";
112 m_aResults.push_back( aEntry );
113 }
114 }
115 break;
116 case E_EXTENSION_ROOT:
118 {
119 const OUString sPackageLocation( m_xContent->getPhysicalURL() );
120 ::ucbhelper::Content aWrappedContent( sPackageLocation, getResultSet()->getEnvironment(), m_xContext );
121
122 // obtain the properties which our result set is set up for from the wrapped content
123 Sequence< OUString > aPropertyNames { "Title" };
124
125 const Reference< XResultSet > xFolderContent( aWrappedContent.createCursor( aPropertyNames ), UNO_SET_THROW );
126 const Reference< XRow > xContentRow( xFolderContent, UNO_QUERY_THROW );
127 while ( xFolderContent->next() )
128 {
129 ResultListEntry aEntry;
130 aEntry.sId = lcl_compose( sContentIdentifier, xContentRow->getString( 1 ) );
131 m_aResults.push_back( aEntry );
132 }
133 }
134 break;
135 default:
136 OSL_FAIL( "DataSupplier::fetchData: unimplemented content type!" );
137 break;
138 }
139 }
140 catch( const Exception& )
141 {
142 DBG_UNHANDLED_EXCEPTION("ucb.ucp.ext");
143 }
144 }
145
146
147 DataSupplier::~DataSupplier()
148 {
149 }
150
151
152 OUString DataSupplier::queryContentIdentifierString( sal_uInt32 i_nIndex )
153 {
154 std::unique_lock aGuard( m_aMutex );
155
156 if ( i_nIndex < m_aResults.size() )
157 {
158 const OUString sId = m_aResults[ i_nIndex ].sId;
159 if ( !sId.isEmpty() )
160 return sId;
161 }
162
163 OSL_FAIL( "DataSupplier::queryContentIdentifierString: illegal index, or illegal result entry id!" );
164 return OUString();
165 }
166
167
168 Reference< XContentIdentifier > DataSupplier::queryContentIdentifier( sal_uInt32 i_nIndex )
169 {
170 std::unique_lock aGuard( m_aMutex );
171
172 if ( i_nIndex < m_aResults.size() )
173 {
174 Reference< XContentIdentifier > xId( m_aResults[ i_nIndex ].xId );
175 if ( xId.is() )
176 return xId;
177 }
178
179 OUString sId = queryContentIdentifierString( i_nIndex );
180 if ( !sId.isEmpty() )
181 {
182 Reference< XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( sId );
183 m_aResults[ i_nIndex ].xId = xId;
184 return xId;
185 }
186
187 return Reference< XContentIdentifier >();
188 }
189
190
191 Reference< XContent > DataSupplier::queryContent( sal_uInt32 i_nIndex )
192 {
193 std::unique_lock aGuard( m_aMutex );
194 ENSURE_OR_RETURN( i_nIndex < m_aResults.size(), "illegal index!", nullptr );
195
196
197 ::rtl::Reference< Content > pContent( m_aResults[ i_nIndex ].pContent );
198 if ( pContent.is() )
199 return pContent;
200
201 Reference< XContentIdentifier > xId( queryContentIdentifier( i_nIndex ) );
202 if ( xId.is() )
203 {
204 try
205 {
206 Reference< XContent > xContent( m_xContent->getProvider()->queryContent( xId ) );
207 pContent.set( dynamic_cast< Content* >( xContent.get() ) );
208 OSL_ENSURE( pContent.is() || !xContent.is(), "DataSupplier::queryContent: invalid content implementation!" );
209 m_aResults[ i_nIndex ].pContent = pContent;
210 return pContent;
211
212 }
213 catch ( const IllegalIdentifierException& )
214 {
215 DBG_UNHANDLED_EXCEPTION("ucb.ucp.ext");
216 }
217 }
218
219 return Reference< XContent >();
220 }
221
222
223 bool DataSupplier::getResult( sal_uInt32 i_nIndex )
224 {
225 std::unique_lock aGuard( m_aMutex );
226
227 // true if result already present.
228 return m_aResults.size() > i_nIndex;
229 }
230
231
232 sal_uInt32 DataSupplier::totalCount()
233 {
234 std::unique_lock aGuard( m_aMutex );
235 return m_aResults.size();
236 }
237
238
239 sal_uInt32 DataSupplier::currentCount()
240 {
241 return m_aResults.size();
242 }
243
244
245 bool DataSupplier::isCountFinal()
246 {
247 return true;
248 }
249
250
251 Reference< XRow > DataSupplier::queryPropertyValues( sal_uInt32 i_nIndex )
252 {
253 std::unique_lock aGuard( m_aMutex );
254 ENSURE_OR_RETURN( i_nIndex < m_aResults.size(), "DataSupplier::queryPropertyValues: illegal index!", nullptr );
255
256 Reference< XRow > xRow = m_aResults[ i_nIndex ].xRow;
257 if ( xRow.is() )
258 return xRow;
259
260 ENSURE_OR_RETURN( queryContent( i_nIndex ).is(), "could not retrieve the content", nullptr );
261
262 switch ( m_xContent->getExtensionContentType() )
263 {
264 case E_ROOT:
265 {
266 const OUString& rId( m_aResults[ i_nIndex ].sId );
267 const OUString sRootURL( ContentProvider::getRootURL() );
268 OUString sTitle = Content::decodeIdentifier( rId.copy( sRootURL.getLength() ) );
269 if ( sTitle.endsWith("/") )
270 sTitle = sTitle.copy( 0, sTitle.getLength() - 1 );
271 xRow = Content::getArtificialNodePropertyValues( m_xContext, getResultSet()->getProperties(), sTitle );
272 }
273 break;
274
275 case E_EXTENSION_ROOT:
277 {
278 xRow = m_aResults[ i_nIndex ].pContent->getPropertyValues(
279 getResultSet()->getProperties(), getResultSet()->getEnvironment() );
280 }
281 break;
282 default:
283 OSL_FAIL( "DataSupplier::queryPropertyValues: unhandled case!" );
284 break;
285 }
286
287 m_aResults[ i_nIndex ].xRow = xRow;
288 return xRow;
289 }
290
291
292 void DataSupplier::releasePropertyValues( sal_uInt32 i_nIndex )
293 {
294 std::unique_lock aGuard( m_aMutex );
295
296 if ( i_nIndex < m_aResults.size() )
297 m_aResults[ i_nIndex ].xRow.clear();
298 }
299
300
301 void DataSupplier::close()
302 {
303 }
304
305
306 void DataSupplier::validate()
307 {
308 }
309
310
311} // namespace ucb::ucp::ext
312
313
314/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno::Reference< ucb::XContent > m_xContent
Reference< XComponentContext > m_xContext
DataSupplier(const css::uno::Reference< css::uno::XComponentContext > &rxContext, rtl::Reference< Content > xContent)
css::uno::Reference< css::sdbc::XResultSet > createCursor(const css::uno::Sequence< OUString > &rPropertyNames, ResultSetInclude eMode=INCLUDE_FOLDERS_AND_DOCUMENTS)
#define ENSURE_OR_RETURN(c, m, r)
#define DBG_UNHANDLED_EXCEPTION(...)
std::mutex m_aMutex
#define SAL_WARN(area, stream)
@ Exception
css::uno::Environment getEnvironment(OUString const &name, std::u16string_view implementation)
constexpr bool ends_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
OUString sId