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