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 <com/sun/star/ucb/ResultSetException.hpp>
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 <vector>
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  //= ResultListEntry
60 
61  namespace {
62 
63  struct ResultListEntry
64  {
65  OUString sId;
66  Reference< XContentIdentifier > xId;
68  Reference< XRow > xRow;
69  };
70 
71  }
72 
73  typedef ::std::vector< ResultListEntry > ResultList;
74 
75 
76  //= DataSupplier_Impl
77 
79  {
80  ::osl::Mutex m_aMutex;
81  ResultList m_aResults;
84 
85  DataSupplier_Impl( const Reference< XComponentContext >& rxContext, const ::rtl::Reference< Content >& i_rContent )
86  :m_xContent( i_rContent )
87  ,m_xContext( rxContext )
88  {
89  }
90  };
91 
92 
93  //= helper
94 
95  namespace
96  {
97  OUString lcl_compose( const OUString& i_rBaseURL, const OUString& i_rRelativeURL )
98  {
99  ENSURE_OR_RETURN( !i_rBaseURL.isEmpty(), "illegal base URL", i_rRelativeURL );
100 
101  OUStringBuffer aComposer( i_rBaseURL );
102  if ( !i_rBaseURL.endsWith("/") )
103  aComposer.append( '/' );
104  aComposer.append( i_rRelativeURL );
105  return aComposer.makeStringAndClear();
106  }
107  }
108 
109 
110  //= DataSupplier
111 
112 
114  const ::rtl::Reference< Content >& i_rContent )
115  :m_pImpl( new DataSupplier_Impl( rxContext, i_rContent ) )
116  {
117  }
118 
119 
121  {
122  try
123  {
124  const Reference< XPackageInformationProvider > xPackageInfo = PackageInformationProvider::get( m_pImpl->m_xContext );
125 
126  const OUString sContentIdentifier( m_pImpl->m_xContent->getIdentifier()->getContentIdentifier() );
127 
128  switch ( m_pImpl->m_xContent->getExtensionContentType() )
129  {
130  case E_ROOT:
131  {
132  const Sequence< Sequence< OUString > > aExtensionInfo( xPackageInfo->getExtensionList() );
133  for ( auto const & extInfo : aExtensionInfo )
134  {
135  if ( !extInfo.hasElements() )
136  {
137  SAL_WARN( "ucb.ucp.ext", "illegal extension info" );
138  continue;
139  }
140 
141  const OUString& rLocalId = extInfo[0];
142  ResultListEntry aEntry;
143  aEntry.sId = ContentProvider::getRootURL() + Content::encodeIdentifier( rLocalId ) + "/";
144  m_pImpl->m_aResults.push_back( aEntry );
145  }
146  }
147  break;
148  case E_EXTENSION_ROOT:
149  case E_EXTENSION_CONTENT:
150  {
151  const OUString sPackageLocation( m_pImpl->m_xContent->getPhysicalURL() );
152  ::ucbhelper::Content aWrappedContent( sPackageLocation, getResultSet()->getEnvironment(), m_pImpl->m_xContext );
153 
154  // obtain the properties which our result set is set up for from the wrapped content
155  Sequence< OUString > aPropertyNames { "Title" };
156 
157  const Reference< XResultSet > xFolderContent( aWrappedContent.createCursor( aPropertyNames ), UNO_SET_THROW );
158  const Reference< XRow > xContentRow( xFolderContent, UNO_QUERY_THROW );
159  while ( xFolderContent->next() )
160  {
161  ResultListEntry aEntry;
162  aEntry.sId = lcl_compose( sContentIdentifier, xContentRow->getString( 1 ) );
163  m_pImpl->m_aResults.push_back( aEntry );
164  }
165  }
166  break;
167  default:
168  OSL_FAIL( "DataSupplier::fetchData: unimplemented content type!" );
169  break;
170  }
171  }
172  catch( const Exception& )
173  {
174  DBG_UNHANDLED_EXCEPTION("ucb.ucp.ext");
175  }
176  }
177 
178 
180  {
181  }
182 
183 
184  OUString DataSupplier::queryContentIdentifierString( sal_uInt32 i_nIndex )
185  {
186  ::osl::Guard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
187 
188  if ( i_nIndex < m_pImpl->m_aResults.size() )
189  {
190  const OUString sId = m_pImpl->m_aResults[ i_nIndex ].sId;
191  if ( !sId.isEmpty() )
192  return sId;
193  }
194 
195  OSL_FAIL( "DataSupplier::queryContentIdentifierString: illegal index, or illegal result entry id!" );
196  return OUString();
197  }
198 
199 
200  Reference< XContentIdentifier > DataSupplier::queryContentIdentifier( sal_uInt32 i_nIndex )
201  {
202  ::osl::Guard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
203 
204  if ( i_nIndex < m_pImpl->m_aResults.size() )
205  {
206  Reference< XContentIdentifier > xId( m_pImpl->m_aResults[ i_nIndex ].xId );
207  if ( xId.is() )
208  return xId;
209  }
210 
211  OUString sId = queryContentIdentifierString( i_nIndex );
212  if ( !sId.isEmpty() )
213  {
214  Reference< XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( sId );
215  m_pImpl->m_aResults[ i_nIndex ].xId = xId;
216  return xId;
217  }
218 
219  return Reference< XContentIdentifier >();
220  }
221 
222 
224  {
225  ::osl::Guard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
226  ENSURE_OR_RETURN( i_nIndex < m_pImpl->m_aResults.size(), "illegal index!", nullptr );
227 
228 
229  ::rtl::Reference< Content > pContent( m_pImpl->m_aResults[ i_nIndex ].pContent );
230  if ( pContent.is() )
231  return pContent.get();
232 
233  Reference< XContentIdentifier > xId( queryContentIdentifier( i_nIndex ) );
234  if ( xId.is() )
235  {
236  try
237  {
238  Reference< XContent > xContent( m_pImpl->m_xContent->getProvider()->queryContent( xId ) );
239  pContent.set( dynamic_cast< Content* >( xContent.get() ) );
240  OSL_ENSURE( pContent.is() || !xContent.is(), "DataSupplier::queryContent: invalid content implementation!" );
241  m_pImpl->m_aResults[ i_nIndex ].pContent = pContent;
242  return pContent.get();
243 
244  }
245  catch ( const IllegalIdentifierException& )
246  {
247  DBG_UNHANDLED_EXCEPTION("ucb.ucp.ext");
248  }
249  }
250 
251  return Reference< XContent >();
252  }
253 
254 
255  bool DataSupplier::getResult( sal_uInt32 i_nIndex )
256  {
257  ::osl::ClearableGuard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
258 
259  // true if result already present.
260  return m_pImpl->m_aResults.size() > i_nIndex;
261  }
262 
263 
265  {
266  ::osl::ClearableGuard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
267  return m_pImpl->m_aResults.size();
268  }
269 
270 
272  {
273  return m_pImpl->m_aResults.size();
274  }
275 
276 
278  {
279  return true;
280  }
281 
282 
283  Reference< XRow > DataSupplier::queryPropertyValues( sal_uInt32 i_nIndex )
284  {
285  ::osl::MutexGuard aGuard( m_pImpl->m_aMutex );
286  ENSURE_OR_RETURN( i_nIndex < m_pImpl->m_aResults.size(), "DataSupplier::queryPropertyValues: illegal index!", nullptr );
287 
288  Reference< XRow > xRow = m_pImpl->m_aResults[ i_nIndex ].xRow;
289  if ( xRow.is() )
290  return xRow;
291 
292  ENSURE_OR_RETURN( queryContent( i_nIndex ).is(), "could not retrieve the content", nullptr );
293 
294  switch ( m_pImpl->m_xContent->getExtensionContentType() )
295  {
296  case E_ROOT:
297  {
298  const OUString& rId( m_pImpl->m_aResults[ i_nIndex ].sId );
299  const OUString sRootURL( ContentProvider::getRootURL() );
300  OUString sTitle = Content::decodeIdentifier( rId.copy( sRootURL.getLength() ) );
301  if ( sTitle.endsWith("/") )
302  sTitle = sTitle.copy( 0, sTitle.getLength() - 1 );
303  xRow = Content::getArtificialNodePropertyValues( m_pImpl->m_xContext, getResultSet()->getProperties(), sTitle );
304  }
305  break;
306 
307  case E_EXTENSION_ROOT:
308  case E_EXTENSION_CONTENT:
309  {
310  xRow = m_pImpl->m_aResults[ i_nIndex ].pContent->getPropertyValues(
311  getResultSet()->getProperties(), getResultSet()->getEnvironment() );
312  }
313  break;
314  default:
315  OSL_FAIL( "DataSupplier::queryPropertyValues: unhandled case!" );
316  break;
317  }
318 
319  m_pImpl->m_aResults[ i_nIndex ].xRow = xRow;
320  return xRow;
321  }
322 
323 
324  void DataSupplier::releasePropertyValues( sal_uInt32 i_nIndex )
325  {
326  ::osl::Guard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
327 
328  if ( i_nIndex < m_pImpl->m_aResults.size() )
329  m_pImpl->m_aResults[ i_nIndex ].xRow.clear();
330  }
331 
332 
334  {
335  }
336 
337 
339  {
340  }
341 
342 
343 } // namespace ucb::ucp::ext
344 
345 
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Environment getEnvironment(OUString const &name, OUString const &implementation)
virtual sal_uInt32 totalCount() override
virtual void validate() override
virtual void close() override
virtual void releasePropertyValues(sal_uInt32 nIndex) override
std::unique_ptr< DataSupplier_Impl > m_pImpl
Reference< XRow > xRow
DataSupplier(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const rtl::Reference< Content > &rContent)
virtual OUString queryContentIdentifierString(sal_uInt32 nIndex) override
Reference< XComponentContext > m_xContext
#define DBG_UNHANDLED_EXCEPTION(...)
virtual css::uno::Reference< css::ucb::XContentIdentifier > queryContentIdentifier(sal_uInt32 nIndex) override
#define ENSURE_OR_RETURN(c, m, r)
::rtl::Reference< Content > pContent
::std::vector< ResultListEntry > ResultList
DataSupplier_Impl(const Reference< XComponentContext > &rxContext, const ::rtl::Reference< Content > &i_rContent)
rtl::Reference< ResultSet > getResultSet() const
virtual bool isCountFinal() override
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)
virtual css::uno::Reference< css::sdbc::XRow > queryPropertyValues(sal_uInt32 nIndex) override
Reference< XContentIdentifier > xId
OUString sId
uno::Reference< ucb::XContent > xContent
#define SAL_WARN(area, stream)
static OUString encodeIdentifier(const OUString &i_rIdentifier)
::rtl::Reference< Content > m_xContent
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
virtual bool getResult(sal_uInt32 nIndex) override
virtual sal_uInt32 currentCount() override
virtual css::uno::Reference< css::ucb::XContent > queryContent(sal_uInt32 nIndex) override
static OUString decodeIdentifier(const OUString &i_rIdentifier)