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