Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / ucb / source / ucp / ext / ucpext_datasupplier.cxx
blobc276c946527eee4a1b8ff90c1f93aee94bf6a1ea
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>
27 #include <ucbhelper/contentidentifier.hxx>
28 #include <comphelper/componentcontext.hxx>
29 #include <comphelper/processfactory.hxx>
30 #include <ucbhelper/providerhelper.hxx>
31 #include <ucbhelper/content.hxx>
32 #include <ucbhelper/propertyvalueset.hxx>
33 #include <tools/diagnose_ex.h>
34 #include <rtl/ustrbuf.hxx>
36 #include <vector>
37 #include <boost/shared_ptr.hpp>
39 //......................................................................................................................
40 namespace ucb { namespace ucp { namespace ext
42 //......................................................................................................................
44 /** === begin UNO using === **/
45 using ::com::sun::star::uno::Reference;
46 using ::com::sun::star::uno::XInterface;
47 using ::com::sun::star::uno::UNO_QUERY;
48 using ::com::sun::star::uno::UNO_QUERY_THROW;
49 using ::com::sun::star::uno::UNO_SET_THROW;
50 using ::com::sun::star::uno::Exception;
51 using ::com::sun::star::uno::RuntimeException;
52 using ::com::sun::star::uno::Any;
53 using ::com::sun::star::uno::makeAny;
54 using ::com::sun::star::uno::Sequence;
55 using ::com::sun::star::uno::Type;
56 using ::com::sun::star::uno::XComponentContext;
57 using ::com::sun::star::ucb::XContent;
58 using ::com::sun::star::ucb::XContentIdentifier;
59 using ::com::sun::star::sdbc::XRow;
60 using ::com::sun::star::lang::XMultiServiceFactory;
61 using ::com::sun::star::ucb::IllegalIdentifierException;
62 using ::com::sun::star::ucb::ResultSetException;
63 using ::com::sun::star::deployment::PackageInformationProvider;
64 using ::com::sun::star::deployment::XPackageInformationProvider;
65 using ::com::sun::star::beans::Property;
66 using ::com::sun::star::sdbc::XResultSet;
67 using ::com::sun::star::sdbc::XRow;
68 using ::com::sun::star::ucb::XCommandEnvironment;
69 /** === end UNO using === **/
70 //==================================================================================================================
71 //= ResultListEntry
72 //==================================================================================================================
73 struct ResultListEntry
75 ::rtl::OUString sId;
76 Reference< XContentIdentifier > xId;
77 ::rtl::Reference< Content > pContent;
78 Reference< XRow > xRow;
81 typedef ::std::vector< ResultListEntry > ResultList;
83 //==================================================================================================================
84 //= DataSupplier_Impl
85 //==================================================================================================================
86 struct DataSupplier_Impl
88 ::osl::Mutex m_aMutex;
89 ResultList m_aResults;
90 ::rtl::Reference< Content > m_xContent;
91 Reference< XComponentContext > m_xContext;
92 sal_Int32 m_nOpenMode;
94 DataSupplier_Impl( const Reference< XComponentContext >& rxContext, const ::rtl::Reference< Content >& i_rContent,
95 const sal_Int32 i_nOpenMode )
96 :m_xContent( i_rContent )
97 ,m_xContext( rxContext )
98 ,m_nOpenMode( i_nOpenMode )
101 ~DataSupplier_Impl();
104 //------------------------------------------------------------------------------------------------------------------
105 DataSupplier_Impl::~DataSupplier_Impl()
109 //==================================================================================================================
110 //= helper
111 //==================================================================================================================
112 namespace
114 ::rtl::OUString lcl_compose( const ::rtl::OUString& i_rBaseURL, const ::rtl::OUString& i_rRelativeURL )
116 ENSURE_OR_RETURN( !i_rBaseURL.isEmpty(), "illegal base URL", i_rRelativeURL );
118 ::rtl::OUStringBuffer aComposer( i_rBaseURL );
119 if ( i_rBaseURL.getStr()[ i_rBaseURL.getLength() - 1 ] != '/' )
120 aComposer.append( sal_Unicode( '/' ) );
121 aComposer.append( i_rRelativeURL );
122 return aComposer.makeStringAndClear();
127 //==================================================================================================================
128 //= DataSupplier
129 //==================================================================================================================
130 //------------------------------------------------------------------------------------------------------------------
131 DataSupplier::DataSupplier( const Reference< XComponentContext >& rxContext,
132 const ::rtl::Reference< Content >& i_rContent,
133 const sal_Int32 i_nOpenMode )
134 :m_pImpl( new DataSupplier_Impl( rxContext, i_rContent, i_nOpenMode ) )
138 //------------------------------------------------------------------------------------------------------------------
139 void DataSupplier::fetchData()
143 const Reference< XPackageInformationProvider > xPackageInfo = PackageInformationProvider::get( m_pImpl->m_xContext );
145 const ::rtl::OUString sContentIdentifier( m_pImpl->m_xContent->getIdentifier()->getContentIdentifier() );
147 switch ( m_pImpl->m_xContent->getExtensionContentType() )
149 case E_ROOT:
151 Sequence< Sequence< ::rtl::OUString > > aExtensionInfo( xPackageInfo->getExtensionList() );
152 for ( const Sequence< ::rtl::OUString >* pExtInfo = aExtensionInfo.getConstArray();
153 pExtInfo != aExtensionInfo.getConstArray() + aExtensionInfo.getLength();
154 ++pExtInfo
157 if ( pExtInfo->getLength() <= 0 )
159 SAL_WARN( "ucb.ucp", "illegal extension info" );
160 continue;
163 const ::rtl::OUString& rLocalId = (*pExtInfo)[0];
164 ResultListEntry aEntry;
165 aEntry.sId = ContentProvider::getRootURL() + Content::encodeIdentifier( rLocalId ) + ::rtl::OUString( sal_Unicode( '/' ) );
166 m_pImpl->m_aResults.push_back( aEntry );
169 break;
170 case E_EXTENSION_ROOT:
171 case E_EXTENSION_CONTENT:
173 const ::rtl::OUString sPackageLocation( m_pImpl->m_xContent->getPhysicalURL() );
174 ::ucbhelper::Content aWrappedContent( sPackageLocation, getResultSet()->getEnvironment(), m_pImpl->m_xContext );
176 // obtain the properties which our result set is set up for from the wrapped content
177 Sequence< ::rtl::OUString > aPropertyNames(1);
178 aPropertyNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Title" ) );
180 const Reference< XResultSet > xFolderContent( aWrappedContent.createCursor( aPropertyNames ), UNO_SET_THROW );
181 const Reference< XRow > xContentRow( xFolderContent, UNO_QUERY_THROW );
182 while ( xFolderContent->next() )
184 ResultListEntry aEntry;
185 aEntry.sId = lcl_compose( sContentIdentifier, xContentRow->getString( 1 ) );
186 m_pImpl->m_aResults.push_back( aEntry );
189 break;
190 default:
191 OSL_FAIL( "DataSupplier::fetchData: unimplemented content type!" );
192 break;
195 catch( const Exception& )
197 DBG_UNHANDLED_EXCEPTION();
201 //------------------------------------------------------------------------------------------------------------------
202 DataSupplier::~DataSupplier()
206 //------------------------------------------------------------------------------------------------------------------
207 ::rtl::OUString DataSupplier::queryContentIdentifierString( sal_uInt32 i_nIndex )
209 ::osl::Guard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
211 if ( i_nIndex < m_pImpl->m_aResults.size() )
213 const ::rtl::OUString sId = m_pImpl->m_aResults[ i_nIndex ].sId;
214 if ( !sId.isEmpty() )
215 return sId;
218 OSL_FAIL( "DataSupplier::queryContentIdentifierString: illegal index, or illegal result entry id!" );
219 return ::rtl::OUString();
222 //------------------------------------------------------------------------------------------------------------------
223 Reference< XContentIdentifier > DataSupplier::queryContentIdentifier( sal_uInt32 i_nIndex )
225 ::osl::Guard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
227 if ( i_nIndex < m_pImpl->m_aResults.size() )
229 Reference< XContentIdentifier > xId( m_pImpl->m_aResults[ i_nIndex ].xId );
230 if ( xId.is() )
231 return xId;
234 ::rtl::OUString sId = queryContentIdentifierString( i_nIndex );
235 if ( !sId.isEmpty() )
237 Reference< XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( sId );
238 m_pImpl->m_aResults[ i_nIndex ].xId = xId;
239 return xId;
242 return Reference< XContentIdentifier >();
245 //------------------------------------------------------------------------------------------------------------------
246 Reference< XContent > DataSupplier::queryContent( sal_uInt32 i_nIndex )
248 ::osl::Guard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
249 ENSURE_OR_RETURN( i_nIndex < m_pImpl->m_aResults.size(), "illegal index!", NULL );
252 ::rtl::Reference< Content > pContent( m_pImpl->m_aResults[ i_nIndex ].pContent );
253 if ( pContent.is() )
254 return pContent.get();
256 Reference< XContentIdentifier > xId( queryContentIdentifier( i_nIndex ) );
257 if ( xId.is() )
261 Reference< XContent > xContent( m_pImpl->m_xContent->getProvider()->queryContent( xId ) );
262 pContent.set( dynamic_cast< Content* >( xContent.get() ) );
263 OSL_ENSURE( pContent.is() || !xContent.is(), "DataSupplier::queryContent: invalid content implementation!" );
264 m_pImpl->m_aResults[ i_nIndex ].pContent = pContent;
265 return pContent.get();
268 catch ( const IllegalIdentifierException& )
270 DBG_UNHANDLED_EXCEPTION();
274 return Reference< XContent >();
277 //------------------------------------------------------------------------------------------------------------------
278 sal_Bool DataSupplier::getResult( sal_uInt32 i_nIndex )
280 ::osl::ClearableGuard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
282 if ( m_pImpl->m_aResults.size() > i_nIndex )
283 // result already present.
284 return sal_True;
286 return sal_False;
289 //------------------------------------------------------------------------------------------------------------------
290 sal_uInt32 DataSupplier::totalCount()
292 ::osl::ClearableGuard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
293 return m_pImpl->m_aResults.size();
296 //------------------------------------------------------------------------------------------------------------------
297 sal_uInt32 DataSupplier::currentCount()
299 return m_pImpl->m_aResults.size();
302 //------------------------------------------------------------------------------------------------------------------
303 sal_Bool DataSupplier::isCountFinal()
305 return sal_True;
308 //------------------------------------------------------------------------------------------------------------------
309 Reference< XRow > DataSupplier::queryPropertyValues( sal_uInt32 i_nIndex )
311 ::osl::MutexGuard aGuard( m_pImpl->m_aMutex );
312 ENSURE_OR_RETURN( i_nIndex < m_pImpl->m_aResults.size(), "DataSupplier::queryPropertyValues: illegal index!", NULL );
314 Reference< XRow > xRow = m_pImpl->m_aResults[ i_nIndex ].xRow;
315 if ( xRow.is() )
316 return xRow;
318 ENSURE_OR_RETURN( queryContent( i_nIndex ).is(), "could not retrieve the content", NULL );
320 switch ( m_pImpl->m_xContent->getExtensionContentType() )
322 case E_ROOT:
324 const ::rtl::OUString& rId( m_pImpl->m_aResults[ i_nIndex ].sId );
325 const ::rtl::OUString sRootURL( ContentProvider::getRootURL() );
326 ::rtl::OUString sTitle = Content::decodeIdentifier( rId.copy( sRootURL.getLength() ) );
327 if ( !sTitle.isEmpty() && ( sTitle[ sTitle.getLength() - 1 ] == '/' ) )
328 sTitle = sTitle.copy( 0, sTitle.getLength() - 1 );
329 xRow = Content::getArtificialNodePropertyValues( m_pImpl->m_xContext, getResultSet()->getProperties(), sTitle );
331 break;
333 case E_EXTENSION_ROOT:
334 case E_EXTENSION_CONTENT:
336 xRow = m_pImpl->m_aResults[ i_nIndex ].pContent->getPropertyValues(
337 getResultSet()->getProperties(), getResultSet()->getEnvironment() );
339 break;
340 default:
341 OSL_FAIL( "DataSupplier::queryPropertyValues: unhandled case!" );
342 break;
345 m_pImpl->m_aResults[ i_nIndex ].xRow = xRow;
346 return xRow;
349 //------------------------------------------------------------------------------------------------------------------
350 void DataSupplier::releasePropertyValues( sal_uInt32 i_nIndex )
352 ::osl::Guard< ::osl::Mutex > aGuard( m_pImpl->m_aMutex );
354 if ( i_nIndex < m_pImpl->m_aResults.size() )
355 m_pImpl->m_aResults[ i_nIndex ].xRow.clear();
358 //------------------------------------------------------------------------------------------------------------------
359 void DataSupplier::close()
363 //------------------------------------------------------------------------------------------------------------------
364 void DataSupplier::validate() throw( ResultSetException )
368 //......................................................................................................................
369 } } } // namespace ucp::ext
370 //......................................................................................................................
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */