1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
37 #include <boost/shared_ptr.hpp>
39 //......................................................................................................................
40 namespace ucb
{ namespace ucp
{ namespace ext
42 //......................................................................................................................
44 using ::com::sun::star::uno::Reference
;
45 using ::com::sun::star::uno::XInterface
;
46 using ::com::sun::star::uno::UNO_QUERY
;
47 using ::com::sun::star::uno::UNO_QUERY_THROW
;
48 using ::com::sun::star::uno::UNO_SET_THROW
;
49 using ::com::sun::star::uno::Exception
;
50 using ::com::sun::star::uno::RuntimeException
;
51 using ::com::sun::star::uno::Any
;
52 using ::com::sun::star::uno::makeAny
;
53 using ::com::sun::star::uno::Sequence
;
54 using ::com::sun::star::uno::Type
;
55 using ::com::sun::star::uno::XComponentContext
;
56 using ::com::sun::star::ucb::XContent
;
57 using ::com::sun::star::ucb::XContentIdentifier
;
58 using ::com::sun::star::sdbc::XRow
;
59 using ::com::sun::star::lang::XMultiServiceFactory
;
60 using ::com::sun::star::ucb::IllegalIdentifierException
;
61 using ::com::sun::star::ucb::ResultSetException
;
62 using ::com::sun::star::deployment::PackageInformationProvider
;
63 using ::com::sun::star::deployment::XPackageInformationProvider
;
64 using ::com::sun::star::beans::Property
;
65 using ::com::sun::star::sdbc::XResultSet
;
66 using ::com::sun::star::ucb::XCommandEnvironment
;
68 //==================================================================================================================
70 //==================================================================================================================
71 struct ResultListEntry
74 Reference
< XContentIdentifier
> xId
;
75 ::rtl::Reference
< Content
> pContent
;
76 Reference
< XRow
> xRow
;
79 typedef ::std::vector
< ResultListEntry
> ResultList
;
81 //==================================================================================================================
83 //==================================================================================================================
84 struct DataSupplier_Impl
86 ::osl::Mutex m_aMutex
;
87 ResultList m_aResults
;
88 ::rtl::Reference
< Content
> m_xContent
;
89 Reference
< XComponentContext
> m_xContext
;
90 sal_Int32 m_nOpenMode
;
92 DataSupplier_Impl( const Reference
< XComponentContext
>& rxContext
, const ::rtl::Reference
< Content
>& i_rContent
,
93 const sal_Int32 i_nOpenMode
)
94 :m_xContent( i_rContent
)
95 ,m_xContext( rxContext
)
96 ,m_nOpenMode( i_nOpenMode
)
102 //------------------------------------------------------------------------------------------------------------------
103 DataSupplier_Impl::~DataSupplier_Impl()
107 //==================================================================================================================
109 //==================================================================================================================
112 OUString
lcl_compose( const OUString
& i_rBaseURL
, const OUString
& i_rRelativeURL
)
114 ENSURE_OR_RETURN( !i_rBaseURL
.isEmpty(), "illegal base URL", i_rRelativeURL
);
116 OUStringBuffer
aComposer( i_rBaseURL
);
117 if ( i_rBaseURL
.getStr()[ i_rBaseURL
.getLength() - 1 ] != '/' )
118 aComposer
.append( sal_Unicode( '/' ) );
119 aComposer
.append( i_rRelativeURL
);
120 return aComposer
.makeStringAndClear();
125 //==================================================================================================================
127 //==================================================================================================================
128 //------------------------------------------------------------------------------------------------------------------
129 DataSupplier::DataSupplier( const Reference
< XComponentContext
>& rxContext
,
130 const ::rtl::Reference
< Content
>& i_rContent
,
131 const sal_Int32 i_nOpenMode
)
132 :m_pImpl( new DataSupplier_Impl( rxContext
, i_rContent
, i_nOpenMode
) )
136 //------------------------------------------------------------------------------------------------------------------
137 void DataSupplier::fetchData()
141 const Reference
< XPackageInformationProvider
> xPackageInfo
= PackageInformationProvider::get( m_pImpl
->m_xContext
);
143 const OUString
sContentIdentifier( m_pImpl
->m_xContent
->getIdentifier()->getContentIdentifier() );
145 switch ( m_pImpl
->m_xContent
->getExtensionContentType() )
149 Sequence
< Sequence
< OUString
> > aExtensionInfo( xPackageInfo
->getExtensionList() );
150 for ( const Sequence
< OUString
>* pExtInfo
= aExtensionInfo
.getConstArray();
151 pExtInfo
!= aExtensionInfo
.getConstArray() + aExtensionInfo
.getLength();
155 if ( pExtInfo
->getLength() <= 0 )
157 SAL_WARN( "ucb.ucp", "illegal extension info" );
161 const OUString
& rLocalId
= (*pExtInfo
)[0];
162 ResultListEntry aEntry
;
163 aEntry
.sId
= ContentProvider::getRootURL() + Content::encodeIdentifier( rLocalId
) + OUString( sal_Unicode( '/' ) );
164 m_pImpl
->m_aResults
.push_back( aEntry
);
168 case E_EXTENSION_ROOT
:
169 case E_EXTENSION_CONTENT
:
171 const OUString
sPackageLocation( m_pImpl
->m_xContent
->getPhysicalURL() );
172 ::ucbhelper::Content
aWrappedContent( sPackageLocation
, getResultSet()->getEnvironment(), m_pImpl
->m_xContext
);
174 // obtain the properties which our result set is set up for from the wrapped content
175 Sequence
< OUString
> aPropertyNames(1);
176 aPropertyNames
[0] = OUString( "Title" );
178 const Reference
< XResultSet
> xFolderContent( aWrappedContent
.createCursor( aPropertyNames
), UNO_SET_THROW
);
179 const Reference
< XRow
> xContentRow( xFolderContent
, UNO_QUERY_THROW
);
180 while ( xFolderContent
->next() )
182 ResultListEntry aEntry
;
183 aEntry
.sId
= lcl_compose( sContentIdentifier
, xContentRow
->getString( 1 ) );
184 m_pImpl
->m_aResults
.push_back( aEntry
);
189 OSL_FAIL( "DataSupplier::fetchData: unimplemented content type!" );
193 catch( const Exception
& )
195 DBG_UNHANDLED_EXCEPTION();
199 //------------------------------------------------------------------------------------------------------------------
200 DataSupplier::~DataSupplier()
204 //------------------------------------------------------------------------------------------------------------------
205 OUString
DataSupplier::queryContentIdentifierString( sal_uInt32 i_nIndex
)
207 ::osl::Guard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
209 if ( i_nIndex
< m_pImpl
->m_aResults
.size() )
211 const OUString sId
= m_pImpl
->m_aResults
[ i_nIndex
].sId
;
212 if ( !sId
.isEmpty() )
216 OSL_FAIL( "DataSupplier::queryContentIdentifierString: illegal index, or illegal result entry id!" );
220 //------------------------------------------------------------------------------------------------------------------
221 Reference
< XContentIdentifier
> DataSupplier::queryContentIdentifier( sal_uInt32 i_nIndex
)
223 ::osl::Guard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
225 if ( i_nIndex
< m_pImpl
->m_aResults
.size() )
227 Reference
< XContentIdentifier
> xId( m_pImpl
->m_aResults
[ i_nIndex
].xId
);
232 OUString sId
= queryContentIdentifierString( i_nIndex
);
233 if ( !sId
.isEmpty() )
235 Reference
< XContentIdentifier
> xId
= new ::ucbhelper::ContentIdentifier( sId
);
236 m_pImpl
->m_aResults
[ i_nIndex
].xId
= xId
;
240 return Reference
< XContentIdentifier
>();
243 //------------------------------------------------------------------------------------------------------------------
244 Reference
< XContent
> DataSupplier::queryContent( sal_uInt32 i_nIndex
)
246 ::osl::Guard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
247 ENSURE_OR_RETURN( i_nIndex
< m_pImpl
->m_aResults
.size(), "illegal index!", NULL
);
250 ::rtl::Reference
< Content
> pContent( m_pImpl
->m_aResults
[ i_nIndex
].pContent
);
252 return pContent
.get();
254 Reference
< XContentIdentifier
> xId( queryContentIdentifier( i_nIndex
) );
259 Reference
< XContent
> xContent( m_pImpl
->m_xContent
->getProvider()->queryContent( xId
) );
260 pContent
.set( dynamic_cast< Content
* >( xContent
.get() ) );
261 OSL_ENSURE( pContent
.is() || !xContent
.is(), "DataSupplier::queryContent: invalid content implementation!" );
262 m_pImpl
->m_aResults
[ i_nIndex
].pContent
= pContent
;
263 return pContent
.get();
266 catch ( const IllegalIdentifierException
& )
268 DBG_UNHANDLED_EXCEPTION();
272 return Reference
< XContent
>();
275 //------------------------------------------------------------------------------------------------------------------
276 sal_Bool
DataSupplier::getResult( sal_uInt32 i_nIndex
)
278 ::osl::ClearableGuard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
280 if ( m_pImpl
->m_aResults
.size() > i_nIndex
)
281 // result already present.
287 //------------------------------------------------------------------------------------------------------------------
288 sal_uInt32
DataSupplier::totalCount()
290 ::osl::ClearableGuard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
291 return m_pImpl
->m_aResults
.size();
294 //------------------------------------------------------------------------------------------------------------------
295 sal_uInt32
DataSupplier::currentCount()
297 return m_pImpl
->m_aResults
.size();
300 //------------------------------------------------------------------------------------------------------------------
301 sal_Bool
DataSupplier::isCountFinal()
306 //------------------------------------------------------------------------------------------------------------------
307 Reference
< XRow
> DataSupplier::queryPropertyValues( sal_uInt32 i_nIndex
)
309 ::osl::MutexGuard
aGuard( m_pImpl
->m_aMutex
);
310 ENSURE_OR_RETURN( i_nIndex
< m_pImpl
->m_aResults
.size(), "DataSupplier::queryPropertyValues: illegal index!", NULL
);
312 Reference
< XRow
> xRow
= m_pImpl
->m_aResults
[ i_nIndex
].xRow
;
316 ENSURE_OR_RETURN( queryContent( i_nIndex
).is(), "could not retrieve the content", NULL
);
318 switch ( m_pImpl
->m_xContent
->getExtensionContentType() )
322 const OUString
& rId( m_pImpl
->m_aResults
[ i_nIndex
].sId
);
323 const OUString
sRootURL( ContentProvider::getRootURL() );
324 OUString sTitle
= Content::decodeIdentifier( rId
.copy( sRootURL
.getLength() ) );
325 if ( !sTitle
.isEmpty() && ( sTitle
[ sTitle
.getLength() - 1 ] == '/' ) )
326 sTitle
= sTitle
.copy( 0, sTitle
.getLength() - 1 );
327 xRow
= Content::getArtificialNodePropertyValues( m_pImpl
->m_xContext
, getResultSet()->getProperties(), sTitle
);
331 case E_EXTENSION_ROOT
:
332 case E_EXTENSION_CONTENT
:
334 xRow
= m_pImpl
->m_aResults
[ i_nIndex
].pContent
->getPropertyValues(
335 getResultSet()->getProperties(), getResultSet()->getEnvironment() );
339 OSL_FAIL( "DataSupplier::queryPropertyValues: unhandled case!" );
343 m_pImpl
->m_aResults
[ i_nIndex
].xRow
= xRow
;
347 //------------------------------------------------------------------------------------------------------------------
348 void DataSupplier::releasePropertyValues( sal_uInt32 i_nIndex
)
350 ::osl::Guard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
352 if ( i_nIndex
< m_pImpl
->m_aResults
.size() )
353 m_pImpl
->m_aResults
[ i_nIndex
].xRow
.clear();
356 //------------------------------------------------------------------------------------------------------------------
357 void DataSupplier::close()
361 //------------------------------------------------------------------------------------------------------------------
362 void DataSupplier::validate() throw( ResultSetException
)
366 //......................................................................................................................
367 } } } // namespace ucp::ext
368 //......................................................................................................................
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */