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/processfactory.hxx>
29 #include <ucbhelper/providerhelper.hxx>
30 #include <ucbhelper/content.hxx>
31 #include <ucbhelper/propertyvalueset.hxx>
32 #include <tools/diagnose_ex.h>
33 #include <rtl/ustrbuf.hxx>
36 #include <boost/shared_ptr.hpp>
39 namespace ucb
{ namespace ucp
{ namespace ext
43 using ::com::sun::star::uno::Reference
;
44 using ::com::sun::star::uno::XInterface
;
45 using ::com::sun::star::uno::UNO_QUERY
;
46 using ::com::sun::star::uno::UNO_QUERY_THROW
;
47 using ::com::sun::star::uno::UNO_SET_THROW
;
48 using ::com::sun::star::uno::Exception
;
49 using ::com::sun::star::uno::RuntimeException
;
50 using ::com::sun::star::uno::Any
;
51 using ::com::sun::star::uno::makeAny
;
52 using ::com::sun::star::uno::Sequence
;
53 using ::com::sun::star::uno::Type
;
54 using ::com::sun::star::uno::XComponentContext
;
55 using ::com::sun::star::ucb::XContent
;
56 using ::com::sun::star::ucb::XContentIdentifier
;
57 using ::com::sun::star::sdbc::XRow
;
58 using ::com::sun::star::lang::XMultiServiceFactory
;
59 using ::com::sun::star::ucb::IllegalIdentifierException
;
60 using ::com::sun::star::ucb::ResultSetException
;
61 using ::com::sun::star::deployment::PackageInformationProvider
;
62 using ::com::sun::star::deployment::XPackageInformationProvider
;
63 using ::com::sun::star::beans::Property
;
64 using ::com::sun::star::sdbc::XResultSet
;
65 using ::com::sun::star::ucb::XCommandEnvironment
;
70 struct ResultListEntry
73 Reference
< XContentIdentifier
> xId
;
74 ::rtl::Reference
< Content
> pContent
;
75 Reference
< XRow
> xRow
;
78 typedef ::std::vector
< ResultListEntry
> ResultList
;
83 struct DataSupplier_Impl
85 ::osl::Mutex m_aMutex
;
86 ResultList m_aResults
;
87 ::rtl::Reference
< Content
> m_xContent
;
88 Reference
< XComponentContext
> m_xContext
;
89 sal_Int32 m_nOpenMode
;
91 DataSupplier_Impl( const Reference
< XComponentContext
>& rxContext
, const ::rtl::Reference
< Content
>& i_rContent
,
92 const sal_Int32 i_nOpenMode
)
93 :m_xContent( i_rContent
)
94 ,m_xContext( rxContext
)
95 ,m_nOpenMode( i_nOpenMode
)
102 DataSupplier_Impl::~DataSupplier_Impl()
111 OUString
lcl_compose( const OUString
& i_rBaseURL
, const OUString
& i_rRelativeURL
)
113 ENSURE_OR_RETURN( !i_rBaseURL
.isEmpty(), "illegal base URL", i_rRelativeURL
);
115 OUStringBuffer
aComposer( i_rBaseURL
);
116 if ( !i_rBaseURL
.endsWith("/") )
117 aComposer
.append( '/' );
118 aComposer
.append( i_rRelativeURL
);
119 return aComposer
.makeStringAndClear();
128 DataSupplier::DataSupplier( const Reference
< XComponentContext
>& rxContext
,
129 const ::rtl::Reference
< Content
>& i_rContent
,
130 const sal_Int32 i_nOpenMode
)
131 :m_pImpl( new DataSupplier_Impl( rxContext
, i_rContent
, i_nOpenMode
) )
136 void DataSupplier::fetchData()
140 const Reference
< XPackageInformationProvider
> xPackageInfo
= PackageInformationProvider::get( m_pImpl
->m_xContext
);
142 const OUString
sContentIdentifier( m_pImpl
->m_xContent
->getIdentifier()->getContentIdentifier() );
144 switch ( m_pImpl
->m_xContent
->getExtensionContentType() )
148 Sequence
< Sequence
< OUString
> > aExtensionInfo( xPackageInfo
->getExtensionList() );
149 for ( const Sequence
< OUString
>* pExtInfo
= aExtensionInfo
.getConstArray();
150 pExtInfo
!= aExtensionInfo
.getConstArray() + aExtensionInfo
.getLength();
154 if ( pExtInfo
->getLength() <= 0 )
156 SAL_WARN( "ucb.ucp.ext", "illegal extension info" );
160 const OUString
& rLocalId
= (*pExtInfo
)[0];
161 ResultListEntry aEntry
;
162 aEntry
.sId
= ContentProvider::getRootURL() + Content::encodeIdentifier( rLocalId
) + "/";
163 m_pImpl
->m_aResults
.push_back( aEntry
);
167 case E_EXTENSION_ROOT
:
168 case E_EXTENSION_CONTENT
:
170 const OUString
sPackageLocation( m_pImpl
->m_xContent
->getPhysicalURL() );
171 ::ucbhelper::Content
aWrappedContent( sPackageLocation
, getResultSet()->getEnvironment(), m_pImpl
->m_xContext
);
173 // obtain the properties which our result set is set up for from the wrapped content
174 Sequence
< OUString
> aPropertyNames(1);
175 aPropertyNames
[0] = "Title";
177 const Reference
< XResultSet
> xFolderContent( aWrappedContent
.createCursor( aPropertyNames
), UNO_SET_THROW
);
178 const Reference
< XRow
> xContentRow( xFolderContent
, UNO_QUERY_THROW
);
179 while ( xFolderContent
->next() )
181 ResultListEntry aEntry
;
182 aEntry
.sId
= lcl_compose( sContentIdentifier
, xContentRow
->getString( 1 ) );
183 m_pImpl
->m_aResults
.push_back( aEntry
);
188 OSL_FAIL( "DataSupplier::fetchData: unimplemented content type!" );
192 catch( const Exception
& )
194 DBG_UNHANDLED_EXCEPTION();
199 DataSupplier::~DataSupplier()
204 OUString
DataSupplier::queryContentIdentifierString( sal_uInt32 i_nIndex
)
206 ::osl::Guard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
208 if ( i_nIndex
< m_pImpl
->m_aResults
.size() )
210 const OUString sId
= m_pImpl
->m_aResults
[ i_nIndex
].sId
;
211 if ( !sId
.isEmpty() )
215 OSL_FAIL( "DataSupplier::queryContentIdentifierString: illegal index, or illegal result entry id!" );
220 Reference
< XContentIdentifier
> DataSupplier::queryContentIdentifier( sal_uInt32 i_nIndex
)
222 ::osl::Guard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
224 if ( i_nIndex
< m_pImpl
->m_aResults
.size() )
226 Reference
< XContentIdentifier
> xId( m_pImpl
->m_aResults
[ i_nIndex
].xId
);
231 OUString sId
= queryContentIdentifierString( i_nIndex
);
232 if ( !sId
.isEmpty() )
234 Reference
< XContentIdentifier
> xId
= new ::ucbhelper::ContentIdentifier( sId
);
235 m_pImpl
->m_aResults
[ i_nIndex
].xId
= xId
;
239 return Reference
< XContentIdentifier
>();
243 Reference
< XContent
> DataSupplier::queryContent( sal_uInt32 i_nIndex
)
245 ::osl::Guard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
246 ENSURE_OR_RETURN( i_nIndex
< m_pImpl
->m_aResults
.size(), "illegal index!", NULL
);
249 ::rtl::Reference
< Content
> pContent( m_pImpl
->m_aResults
[ i_nIndex
].pContent
);
251 return pContent
.get();
253 Reference
< XContentIdentifier
> xId( queryContentIdentifier( i_nIndex
) );
258 Reference
< XContent
> xContent( m_pImpl
->m_xContent
->getProvider()->queryContent( xId
) );
259 pContent
.set( dynamic_cast< Content
* >( xContent
.get() ) );
260 OSL_ENSURE( pContent
.is() || !xContent
.is(), "DataSupplier::queryContent: invalid content implementation!" );
261 m_pImpl
->m_aResults
[ i_nIndex
].pContent
= pContent
;
262 return pContent
.get();
265 catch ( const IllegalIdentifierException
& )
267 DBG_UNHANDLED_EXCEPTION();
271 return Reference
< XContent
>();
275 bool DataSupplier::getResult( sal_uInt32 i_nIndex
)
277 ::osl::ClearableGuard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
279 if ( m_pImpl
->m_aResults
.size() > i_nIndex
)
280 // result already present.
287 sal_uInt32
DataSupplier::totalCount()
289 ::osl::ClearableGuard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
290 return m_pImpl
->m_aResults
.size();
294 sal_uInt32
DataSupplier::currentCount()
296 return m_pImpl
->m_aResults
.size();
300 bool DataSupplier::isCountFinal()
306 Reference
< XRow
> DataSupplier::queryPropertyValues( sal_uInt32 i_nIndex
)
308 ::osl::MutexGuard
aGuard( m_pImpl
->m_aMutex
);
309 ENSURE_OR_RETURN( i_nIndex
< m_pImpl
->m_aResults
.size(), "DataSupplier::queryPropertyValues: illegal index!", NULL
);
311 Reference
< XRow
> xRow
= m_pImpl
->m_aResults
[ i_nIndex
].xRow
;
315 ENSURE_OR_RETURN( queryContent( i_nIndex
).is(), "could not retrieve the content", NULL
);
317 switch ( m_pImpl
->m_xContent
->getExtensionContentType() )
321 const OUString
& rId( m_pImpl
->m_aResults
[ i_nIndex
].sId
);
322 const OUString
sRootURL( ContentProvider::getRootURL() );
323 OUString sTitle
= Content::decodeIdentifier( rId
.copy( sRootURL
.getLength() ) );
324 if ( sTitle
.endsWith("/") )
325 sTitle
= sTitle
.copy( 0, sTitle
.getLength() - 1 );
326 xRow
= Content::getArtificialNodePropertyValues( m_pImpl
->m_xContext
, getResultSet()->getProperties(), sTitle
);
330 case E_EXTENSION_ROOT
:
331 case E_EXTENSION_CONTENT
:
333 xRow
= m_pImpl
->m_aResults
[ i_nIndex
].pContent
->getPropertyValues(
334 getResultSet()->getProperties(), getResultSet()->getEnvironment() );
338 OSL_FAIL( "DataSupplier::queryPropertyValues: unhandled case!" );
342 m_pImpl
->m_aResults
[ i_nIndex
].xRow
= xRow
;
347 void DataSupplier::releasePropertyValues( sal_uInt32 i_nIndex
)
349 ::osl::Guard
< ::osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
351 if ( i_nIndex
< m_pImpl
->m_aResults
.size() )
352 m_pImpl
->m_aResults
[ i_nIndex
].xRow
.clear();
356 void DataSupplier::close()
361 void DataSupplier::validate() throw( ResultSetException
)
366 } } } // namespace ucp::ext
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */