Fix GNU C++ version check
[LibreOffice.git] / ucb / source / ucp / ext / ucpext_datasupplier.cxx
blob43cb357598195cf9b7f6737217251260b53c2dcb
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 .
21 #include "ucpext_datasupplier.hxx"
22 #include "ucpext_content.hxx"
23 #include "ucpext_provider.hxx"
25 #include <com/sun/star/deployment/PackageInformationProvider.hpp>
26 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
27 #include <o3tl/string_view.hxx>
28 #include <ucbhelper/contentidentifier.hxx>
29 #include <ucbhelper/providerhelper.hxx>
30 #include <ucbhelper/content.hxx>
31 #include <ucbhelper/propertyvalueset.hxx>
32 #include <comphelper/diagnose_ex.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <sal/log.hxx>
36 #include <memory>
37 #include <string_view>
38 #include <utility>
41 namespace ucb::ucp::ext
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;
60 //= helper
62 namespace
64 OUString lcl_compose( std::u16string_view i_rBaseURL, const OUString& i_rRelativeURL )
66 ENSURE_OR_RETURN( !i_rBaseURL.empty(), "illegal base URL", i_rRelativeURL );
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();
77 //= DataSupplier
80 DataSupplier::DataSupplier( const Reference< XComponentContext >& rxContext,
81 ::rtl::Reference< Content > i_xContent )
82 :m_xContent(std::move( i_xContent ))
83 ,m_xContext( rxContext )
88 void DataSupplier::fetchData()
90 try
92 const Reference< XPackageInformationProvider > xPackageInfo = PackageInformationProvider::get( m_xContext );
94 const OUString sContentIdentifier( m_xContent->getIdentifier()->getContentIdentifier() );
96 switch ( m_xContent->getExtensionContentType() )
98 case E_ROOT:
100 const Sequence< Sequence< OUString > > aExtensionInfo( xPackageInfo->getExtensionList() );
101 for ( auto const & extInfo : aExtensionInfo )
103 if ( !extInfo.hasElements() )
105 SAL_WARN( "ucb.ucp.ext", "illegal extension info" );
106 continue;
109 const OUString& rLocalId = extInfo[0];
110 ResultListEntry aEntry;
111 aEntry.sId = ContentProvider::getRootURL() + Content::encodeIdentifier( rLocalId ) + "/";
112 m_aResults.push_back( aEntry );
115 break;
116 case E_EXTENSION_ROOT:
117 case E_EXTENSION_CONTENT:
119 const OUString sPackageLocation( m_xContent->getPhysicalURL() );
120 ::ucbhelper::Content aWrappedContent( sPackageLocation, getResultSet()->getEnvironment(), m_xContext );
122 // obtain the properties which our result set is set up for from the wrapped content
123 Sequence< OUString > aPropertyNames { u"Title"_ustr };
125 const Reference< XResultSet > xFolderContent( aWrappedContent.createCursor( aPropertyNames ), UNO_SET_THROW );
126 const Reference< XRow > xContentRow( xFolderContent, UNO_QUERY_THROW );
127 while ( xFolderContent->next() )
129 ResultListEntry aEntry;
130 aEntry.sId = lcl_compose( sContentIdentifier, xContentRow->getString( 1 ) );
131 m_aResults.push_back( aEntry );
134 break;
135 default:
136 OSL_FAIL( "DataSupplier::fetchData: unimplemented content type!" );
137 break;
140 catch( const Exception& )
142 DBG_UNHANDLED_EXCEPTION("ucb.ucp.ext");
147 DataSupplier::~DataSupplier()
152 OUString DataSupplier::queryContentIdentifierString( std::unique_lock<std::mutex>& /*rResultSetGuard*/, sal_uInt32 i_nIndex )
154 std::unique_lock aGuard( m_aMutex );
156 if ( i_nIndex < m_aResults.size() )
158 const OUString sId = m_aResults[ i_nIndex ].sId;
159 if ( !sId.isEmpty() )
160 return sId;
163 OSL_FAIL( "DataSupplier::queryContentIdentifierString: illegal index, or illegal result entry id!" );
164 return OUString();
168 Reference< XContentIdentifier > DataSupplier::queryContentIdentifier( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 i_nIndex )
170 std::unique_lock aGuard( m_aMutex );
172 if ( i_nIndex < m_aResults.size() )
174 Reference< XContentIdentifier > xId( m_aResults[ i_nIndex ].xId );
175 if ( xId.is() )
176 return xId;
179 OUString sId = queryContentIdentifierString( rResultSetGuard, i_nIndex );
180 if ( !sId.isEmpty() )
182 Reference< XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( sId );
183 m_aResults[ i_nIndex ].xId = xId;
184 return xId;
187 return Reference< XContentIdentifier >();
191 Reference< XContent > DataSupplier::queryContent( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 i_nIndex )
193 std::unique_lock aGuard( m_aMutex );
194 ENSURE_OR_RETURN( i_nIndex < m_aResults.size(), "illegal index!", nullptr );
197 ::rtl::Reference< Content > pContent( m_aResults[ i_nIndex ].pContent );
198 if ( pContent.is() )
199 return pContent;
201 Reference< XContentIdentifier > xId( queryContentIdentifier( rResultSetGuard, i_nIndex ) );
202 if ( xId.is() )
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;
213 catch ( const IllegalIdentifierException& )
215 DBG_UNHANDLED_EXCEPTION("ucb.ucp.ext");
219 return Reference< XContent >();
223 bool DataSupplier::getResult( std::unique_lock<std::mutex>& /*rResultSetGuard*/, sal_uInt32 i_nIndex )
225 std::unique_lock aGuard( m_aMutex );
227 // true if result already present.
228 return m_aResults.size() > i_nIndex;
232 sal_uInt32 DataSupplier::totalCount(std::unique_lock<std::mutex>& /*rResultSetGuard*/)
234 std::unique_lock aGuard( m_aMutex );
235 return m_aResults.size();
239 sal_uInt32 DataSupplier::currentCount()
241 return m_aResults.size();
245 bool DataSupplier::isCountFinal()
247 return true;
251 Reference< XRow > DataSupplier::queryPropertyValues( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 i_nIndex )
253 std::unique_lock aGuard( m_aMutex );
254 ENSURE_OR_RETURN( i_nIndex < m_aResults.size(), "DataSupplier::queryPropertyValues: illegal index!", nullptr );
256 Reference< XRow > xRow = m_aResults[ i_nIndex ].xRow;
257 if ( xRow.is() )
258 return xRow;
260 ENSURE_OR_RETURN( queryContent( rResultSetGuard, i_nIndex ).is(), "could not retrieve the content", nullptr );
262 switch ( m_xContent->getExtensionContentType() )
264 case E_ROOT:
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 );
273 break;
275 case E_EXTENSION_ROOT:
276 case E_EXTENSION_CONTENT:
278 xRow = m_aResults[ i_nIndex ].pContent->getPropertyValues(
279 getResultSet()->getProperties(), getResultSet()->getEnvironment() );
281 break;
282 default:
283 OSL_FAIL( "DataSupplier::queryPropertyValues: unhandled case!" );
284 break;
287 m_aResults[ i_nIndex ].xRow = xRow;
288 return xRow;
292 void DataSupplier::releasePropertyValues( sal_uInt32 i_nIndex )
294 std::unique_lock aGuard( m_aMutex );
296 if ( i_nIndex < m_aResults.size() )
297 m_aResults[ i_nIndex ].xRow.clear();
301 void DataSupplier::close()
306 void DataSupplier::validate()
311 } // namespace ucb::ucp::ext
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */