fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / filter / ftools / fapihelper.cxx
blob1384c7ddbd31c6becb5a3ba693abd4137136d797
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 .
20 #include "fapihelper.hxx"
22 #include <algorithm>
23 #include <com/sun/star/lang/XServiceName.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/beans/XPropertyState.hpp>
26 #include <com/sun/star/beans/XPropertySetOption.hpp>
27 #include <comphelper/docpasswordhelper.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <tools/urlobj.hxx>
30 #include <rtl/strbuf.hxx>
31 #include <sfx2/objsh.hxx>
32 #include <sfx2/docfile.hxx>
33 #include <sfx2/request.hxx>
34 #include <sfx2/frame.hxx>
35 #include <sfx2/sfxsids.hrc>
36 #include <svl/stritem.hxx>
37 #include <svl/itemset.hxx>
38 #include "miscuno.hxx"
40 using ::com::sun::star::uno::Any;
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::Sequence;
43 using ::com::sun::star::uno::Exception;
44 using ::com::sun::star::uno::UNO_QUERY;
45 using ::com::sun::star::uno::UNO_QUERY_THROW;
46 using ::com::sun::star::uno::UNO_SET_THROW;
47 using ::com::sun::star::uno::TypeClass_BOOLEAN;
48 using ::com::sun::star::uno::XInterface;
49 using ::com::sun::star::beans::XPropertySet;
50 using ::com::sun::star::beans::XPropertyState;
51 using ::com::sun::star::lang::XServiceName;
52 using ::com::sun::star::lang::XMultiServiceFactory;
53 using ::com::sun::star::task::PasswordRequestMode_PASSWORD_ENTER;
55 using namespace ::com::sun::star;
57 // Static helper functions ====================================================
59 OUString ScfApiHelper::GetServiceName( const Reference< XInterface >& xInt )
61 OUString aService;
62 Reference< XServiceName > xServiceName( xInt, UNO_QUERY );
63 if( xServiceName.is() )
64 aService = xServiceName->getServiceName();
65 return aService;
68 Reference< XMultiServiceFactory > ScfApiHelper::GetServiceFactory( SfxObjectShell* pShell )
70 Reference< XMultiServiceFactory > xFactory;
71 if( pShell )
72 xFactory.set( pShell->GetModel(), UNO_QUERY );
73 return xFactory;
76 Reference< XInterface > ScfApiHelper::CreateInstance(
77 const Reference< XMultiServiceFactory >& xFactory, const OUString& rServiceName )
79 Reference< XInterface > xInt;
80 if( xFactory.is() )
82 try
84 xInt = xFactory->createInstance( rServiceName );
86 catch( Exception& )
88 OSL_FAIL( "ScfApiHelper::CreateInstance - cannot create instance" );
91 return xInt;
94 Reference< XInterface > ScfApiHelper::CreateInstance( SfxObjectShell* pShell, const OUString& rServiceName )
96 return CreateInstance( GetServiceFactory( pShell ), rServiceName );
99 Reference< XInterface > ScfApiHelper::CreateInstance( const OUString& rServiceName )
101 return CreateInstance( ::comphelper::getProcessServiceFactory(), rServiceName );
104 uno::Sequence< beans::NamedValue > ScfApiHelper::QueryEncryptionDataForMedium( SfxMedium& rMedium,
105 ::comphelper::IDocPasswordVerifier& rVerifier, const ::std::vector< OUString >* pDefaultPasswords )
107 uno::Sequence< beans::NamedValue > aEncryptionData;
108 SFX_ITEMSET_ARG( rMedium.GetItemSet(), pEncryptionDataItem, SfxUnoAnyItem, SID_ENCRYPTIONDATA, false);
109 if ( pEncryptionDataItem )
110 pEncryptionDataItem->GetValue() >>= aEncryptionData;
112 OUString aPassword;
113 SFX_ITEMSET_ARG( rMedium.GetItemSet(), pPasswordItem, SfxStringItem, SID_PASSWORD, false);
114 if ( pPasswordItem )
115 aPassword = pPasswordItem->GetValue();
117 OUString aDocName = INetURLObject( rMedium.GetOrigURL() ).GetName( INetURLObject::DECODE_WITH_CHARSET );
119 bool bIsDefaultPassword = false;
120 aEncryptionData = ::comphelper::DocPasswordHelper::requestAndVerifyDocPassword(
121 rVerifier, aEncryptionData, aPassword, rMedium.GetInteractionHandler(), aDocName,
122 ::comphelper::DocPasswordRequestType_MS, pDefaultPasswords, &bIsDefaultPassword );
124 rMedium.GetItemSet()->ClearItem( SID_PASSWORD );
125 rMedium.GetItemSet()->ClearItem( SID_ENCRYPTIONDATA );
127 if( !bIsDefaultPassword && (aEncryptionData.getLength() > 0) )
128 rMedium.GetItemSet()->Put( SfxUnoAnyItem( SID_ENCRYPTIONDATA, uno::makeAny( aEncryptionData ) ) );
130 return aEncryptionData;
133 // Property sets ==============================================================
135 ScfPropertySet::~ScfPropertySet()
137 Reference<beans::XPropertySetOption> xPropSetOpt(mxPropSet, UNO_QUERY);
138 if (xPropSetOpt.is())
139 // Turn the property value change notification back on when finished.
140 xPropSetOpt->enableChangeListenerNotification(true);
143 void ScfPropertySet::Set( Reference< XPropertySet > xPropSet )
145 mxPropSet = xPropSet;
146 mxMultiPropSet.set( mxPropSet, UNO_QUERY );
147 Reference<beans::XPropertySetOption> xPropSetOpt(mxPropSet, UNO_QUERY);
148 if (xPropSetOpt.is())
149 // We don't want to broadcast property value changes during import to
150 // improve performance.
151 xPropSetOpt->enableChangeListenerNotification(false);
154 OUString ScfPropertySet::GetServiceName() const
156 return ScfApiHelper::GetServiceName( mxPropSet );
159 // Get properties -------------------------------------------------------------
161 bool ScfPropertySet::HasProperty( const OUString& rPropName ) const
163 bool bHasProp = false;
166 Reference< XPropertyState > xPropState( mxPropSet, UNO_QUERY_THROW );
167 bHasProp = xPropState->getPropertyState( rPropName ) == ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
169 catch( Exception& )
172 return bHasProp;
175 bool ScfPropertySet::GetAnyProperty( Any& rValue, const OUString& rPropName ) const
177 bool bHasValue = false;
180 if( mxPropSet.is() )
182 rValue = mxPropSet->getPropertyValue( rPropName );
183 bHasValue = true;
186 catch( Exception& )
189 return bHasValue;
192 bool ScfPropertySet::GetBoolProperty( const OUString& rPropName ) const
194 Any aAny;
195 return GetAnyProperty( aAny, rPropName ) && ScUnoHelpFunctions::GetBoolFromAny( aAny );
198 OUString ScfPropertySet::GetStringProperty( const OUString& rPropName ) const
200 OUString aOUString;
201 GetProperty( aOUString, rPropName );
202 return aOUString;
205 bool ScfPropertySet::GetColorProperty( Color& rColor, const OUString& rPropName ) const
207 sal_Int32 nApiColor = 0;
208 bool bRet = GetProperty( nApiColor, rPropName );
209 rColor = ScfApiHelper::ConvertFromApiColor( nApiColor );
210 return bRet;
213 void ScfPropertySet::GetProperties( Sequence< Any >& rValues, const Sequence< OUString >& rPropNames ) const
217 OSL_ENSURE( mxMultiPropSet.is(), "ScfPropertySet::GetProperties - multi property set not available" );
218 if( mxMultiPropSet.is() ) // first try the XMultiPropertySet
220 rValues = mxMultiPropSet->getPropertyValues( rPropNames );
222 else if( mxPropSet.is() )
224 sal_Int32 nLen = rPropNames.getLength();
225 const OUString* pPropName = rPropNames.getConstArray();
226 const OUString* pPropNameEnd = pPropName + nLen;
227 rValues.realloc( nLen );
228 Any* pValue = rValues.getArray();
229 for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
230 *pValue = mxPropSet->getPropertyValue( *pPropName );
233 catch( Exception& )
238 // Set properties -------------------------------------------------------------
240 void ScfPropertySet::SetAnyProperty( const OUString& rPropName, const Any& rValue )
244 if( mxPropSet.is() )
245 mxPropSet->setPropertyValue( rPropName, rValue );
247 catch (const Exception&)
249 OSL_FAIL(OStringBuffer("ScfPropertySet::SetAnyProperty - cannot set property \"")
250 .append(OUStringToOString(rPropName,
251 RTL_TEXTENCODING_ASCII_US))
252 .append('"').getStr());
256 void ScfPropertySet::SetProperties( const Sequence< OUString >& rPropNames, const Sequence< Any >& rValues )
258 OSL_ENSURE( rPropNames.getLength() == rValues.getLength(), "ScfPropertySet::SetProperties - length of sequences different" );
261 if( mxMultiPropSet.is() ) // first try the XMultiPropertySet
263 mxMultiPropSet->setPropertyValues( rPropNames, rValues );
265 else if( mxPropSet.is() )
267 OSL_FAIL( "ScfPropertySet::SetProperties - multi property set not available" );
268 const OUString* pPropName = rPropNames.getConstArray();
269 const OUString* pPropNameEnd = pPropName + rPropNames.getLength();
270 const Any* pValue = rValues.getConstArray();
271 for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
272 mxPropSet->setPropertyValue( *pPropName, *pValue );
275 catch( Exception& )
277 OSL_FAIL( "ScfPropertySet::SetAnyProperty - cannot set multiple properties" );
281 ScfPropSetHelper::ScfPropSetHelper( const sal_Char* const* ppcPropNames ) :
282 mnNextIdx( 0 )
284 OSL_ENSURE( ppcPropNames, "ScfPropSetHelper::ScfPropSetHelper - no strings found" );
286 // create OUStrings from ASCII property names
287 typedef ::std::pair< OUString, size_t > IndexedOUString;
288 typedef ::std::vector< IndexedOUString > IndexedOUStringVec;
289 IndexedOUStringVec aPropNameVec;
290 for( size_t nVecIdx = 0; *ppcPropNames; ++ppcPropNames, ++nVecIdx )
292 OUString aPropName = OUString::createFromAscii( *ppcPropNames );
293 aPropNameVec.push_back( IndexedOUString( aPropName, nVecIdx ) );
296 // sorts the pairs, which will be sorted by first component (the property name)
297 ::std::sort( aPropNameVec.begin(), aPropNameVec.end() );
299 // resize member sequences
300 size_t nSize = aPropNameVec.size();
301 maNameSeq.realloc( static_cast< sal_Int32 >( nSize ) );
302 maValueSeq.realloc( static_cast< sal_Int32 >( nSize ) );
303 maNameOrder.resize( nSize );
305 // fill the property name sequence and store original sort order
306 sal_Int32 nSeqIdx = 0;
307 for( IndexedOUStringVec::const_iterator aIt = aPropNameVec.begin(),
308 aEnd = aPropNameVec.end(); aIt != aEnd; ++aIt, ++nSeqIdx )
310 maNameSeq[ nSeqIdx ] = aIt->first;
311 maNameOrder[ aIt->second ] = nSeqIdx;
315 // read properties ------------------------------------------------------------
317 void ScfPropSetHelper::ReadFromPropertySet( const ScfPropertySet& rPropSet )
319 rPropSet.GetProperties( maValueSeq, maNameSeq );
320 mnNextIdx = 0;
323 bool ScfPropSetHelper::ReadValue( Any& rAny )
325 Any* pAny = GetNextAny();
326 if( pAny )
327 rAny = *pAny;
328 return pAny != 0;
331 bool ScfPropSetHelper::ReadValue( Color& rColor )
333 sal_Int32 nApiColor(0);
334 bool bRet = ReadValue( nApiColor );
335 rColor = ScfApiHelper::ConvertFromApiColor( nApiColor );
336 return bRet;
339 bool ScfPropSetHelper::ReadValue( bool& rbValue )
341 Any aAny;
342 bool bRet = ReadValue( aAny );
343 rbValue = ScUnoHelpFunctions::GetBoolFromAny( aAny );
344 return bRet;
347 // write properties -----------------------------------------------------------
349 void ScfPropSetHelper::InitializeWrite( bool bClearAllAnys )
351 mnNextIdx = 0;
352 if( bClearAllAnys )
353 for( sal_Int32 nIdx = 0, nLen = maValueSeq.getLength(); nIdx < nLen; ++nIdx )
354 maValueSeq[ nIdx ].clear();
357 void ScfPropSetHelper::WriteValue( const Any& rAny )
359 if( Any* pAny = GetNextAny() )
360 *pAny = rAny;
363 void ScfPropSetHelper::WriteValue( const bool& rbValue )
365 if( Any* pAny = GetNextAny() )
366 ScUnoHelpFunctions::SetBoolInAny( *pAny, rbValue );
369 void ScfPropSetHelper::WriteToPropertySet( ScfPropertySet& rPropSet ) const
371 rPropSet.SetProperties( maNameSeq, maValueSeq );
374 // private --------------------------------------------------------------------
376 Any* ScfPropSetHelper::GetNextAny()
378 OSL_ENSURE( mnNextIdx < maNameOrder.size(), "ScfPropSetHelper::GetNextAny - sequence overflow" );
379 Any* pAny = 0;
380 if( mnNextIdx < maNameOrder.size() )
381 pAny = &maValueSeq[ maNameOrder[ mnNextIdx++ ] ];
382 return pAny;
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */