Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / ftools / fapihelper.cxx
blob4abfc79be67126677eec2c6350ccc1ab76ee5ab6
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/frame/XModel.hpp>
24 #include <com/sun/star/lang/XServiceName.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/beans/NamedValue.hpp>
27 #include <com/sun/star/beans/XPropertyState.hpp>
28 #include <com/sun/star/beans/XPropertySetOption.hpp>
29 #include <com/sun/star/beans/XMultiPropertySet.hpp>
30 #include <comphelper/docpasswordhelper.hxx>
31 #include <comphelper/processfactory.hxx>
32 #include <sal/log.hxx>
33 #include <sfx2/objsh.hxx>
34 #include <sfx2/docfile.hxx>
35 #include <sfx2/frame.hxx>
36 #include <sfx2/sfxsids.hrc>
37 #include <svl/stritem.hxx>
38 #include <svl/itemset.hxx>
39 #include <miscuno.hxx>
41 using ::com::sun::star::uno::Any;
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::Sequence;
44 using ::com::sun::star::uno::Exception;
45 using ::com::sun::star::uno::UNO_QUERY;
46 using ::com::sun::star::uno::UNO_QUERY_THROW;
47 using ::com::sun::star::uno::XInterface;
48 using ::com::sun::star::beans::XPropertySet;
49 using ::com::sun::star::beans::XPropertyState;
50 using ::com::sun::star::lang::XServiceName;
51 using ::com::sun::star::lang::XMultiServiceFactory;
53 using namespace ::com::sun::star;
55 // Static helper functions ====================================================
57 OUString ScfApiHelper::GetServiceName( const Reference< XInterface >& xInt )
59 OUString aService;
60 Reference< XServiceName > xServiceName( xInt, UNO_QUERY );
61 if( xServiceName.is() )
62 aService = xServiceName->getServiceName();
63 return aService;
66 Reference< XMultiServiceFactory > ScfApiHelper::GetServiceFactory( const SfxObjectShell* pShell )
68 Reference< XMultiServiceFactory > xFactory;
69 if( pShell )
70 xFactory.set( pShell->GetModel(), UNO_QUERY );
71 return xFactory;
74 Reference< XInterface > ScfApiHelper::CreateInstance(
75 const Reference< XMultiServiceFactory >& xFactory, const OUString& rServiceName )
77 Reference< XInterface > xInt;
78 if( xFactory.is() )
80 try
82 xInt = xFactory->createInstance( rServiceName );
84 catch( Exception& )
86 OSL_FAIL( "ScfApiHelper::CreateInstance - cannot create instance" );
89 return xInt;
92 Reference< XInterface > ScfApiHelper::CreateInstance( const SfxObjectShell* pShell, const OUString& rServiceName )
94 return CreateInstance( GetServiceFactory( pShell ), rServiceName );
97 Reference< XInterface > ScfApiHelper::CreateInstance( const OUString& rServiceName )
99 return CreateInstance( ::comphelper::getProcessServiceFactory(), rServiceName );
102 uno::Sequence< beans::NamedValue > ScfApiHelper::QueryEncryptionDataForMedium( SfxMedium& rMedium,
103 ::comphelper::IDocPasswordVerifier& rVerifier, const ::std::vector< OUString >* pDefaultPasswords )
105 uno::Sequence< beans::NamedValue > aEncryptionData;
106 const SfxUnoAnyItem* pEncryptionDataItem = SfxItemSet::GetItem<SfxUnoAnyItem>(rMedium.GetItemSet(), SID_ENCRYPTIONDATA, false);
107 if ( pEncryptionDataItem )
108 pEncryptionDataItem->GetValue() >>= aEncryptionData;
110 OUString aPassword;
111 const SfxStringItem* pPasswordItem = SfxItemSet::GetItem<SfxStringItem>(rMedium.GetItemSet(), SID_PASSWORD, false);
112 if ( pPasswordItem )
113 aPassword = pPasswordItem->GetValue();
115 bool bIsDefaultPassword = false;
116 aEncryptionData = ::comphelper::DocPasswordHelper::requestAndVerifyDocPassword(
117 rVerifier, aEncryptionData, aPassword, rMedium.GetInteractionHandler(), rMedium.GetOrigURL(),
118 ::comphelper::DocPasswordRequestType::MS, pDefaultPasswords, &bIsDefaultPassword );
120 rMedium.GetItemSet()->ClearItem( SID_PASSWORD );
121 rMedium.GetItemSet()->ClearItem( SID_ENCRYPTIONDATA );
123 if( !bIsDefaultPassword && aEncryptionData.hasElements() )
124 rMedium.GetItemSet()->Put( SfxUnoAnyItem( SID_ENCRYPTIONDATA, uno::Any( aEncryptionData ) ) );
126 return aEncryptionData;
129 // Property sets ==============================================================
131 ScfPropertySet::~ScfPropertySet()
133 Reference<beans::XPropertySetOption> xPropSetOpt(mxPropSet, UNO_QUERY);
134 if (xPropSetOpt.is())
135 // Turn the property value change notification back on when finished.
136 xPropSetOpt->enableChangeListenerNotification(true);
139 void ScfPropertySet::Set( Reference< XPropertySet > const & xPropSet )
141 mxPropSet = xPropSet;
142 mxMultiPropSet.set( mxPropSet, UNO_QUERY );
143 Reference<beans::XPropertySetOption> xPropSetOpt(mxPropSet, UNO_QUERY);
144 if (xPropSetOpt.is())
145 // We don't want to broadcast property value changes during import to
146 // improve performance.
147 xPropSetOpt->enableChangeListenerNotification(false);
150 OUString ScfPropertySet::GetServiceName() const
152 return ScfApiHelper::GetServiceName( mxPropSet );
155 // Get properties -------------------------------------------------------------
157 bool ScfPropertySet::HasProperty( const OUString& rPropName ) const
159 bool bHasProp = false;
162 Reference< XPropertyState > xPropState( mxPropSet, UNO_QUERY_THROW );
163 bHasProp = xPropState->getPropertyState( rPropName ) == css::beans::PropertyState_DIRECT_VALUE;
165 catch( Exception& )
168 return bHasProp;
171 bool ScfPropertySet::GetAnyProperty( Any& rValue, const OUString& rPropName ) const
173 bool bHasValue = false;
176 if( mxPropSet.is() )
178 rValue = mxPropSet->getPropertyValue( rPropName );
179 bHasValue = true;
182 catch( Exception& )
185 return bHasValue;
188 bool ScfPropertySet::GetBoolProperty( const OUString& rPropName ) const
190 Any aAny;
191 return GetAnyProperty( aAny, rPropName ) && ScUnoHelpFunctions::GetBoolFromAny( aAny );
194 OUString ScfPropertySet::GetStringProperty( const OUString& rPropName ) const
196 OUString aOUString;
197 GetProperty( aOUString, rPropName );
198 return aOUString;
201 bool ScfPropertySet::GetColorProperty( Color& rColor, const OUString& rPropName ) const
203 sal_Int32 nApiColor = 0;
204 bool bRet = GetProperty( nApiColor, rPropName );
205 rColor = Color( ColorTransparency, nApiColor );
206 return bRet;
209 void ScfPropertySet::GetProperties( Sequence< Any >& rValues, const Sequence< OUString >& rPropNames ) const
213 OSL_ENSURE( mxMultiPropSet.is(), "ScfPropertySet::GetProperties - multi property set not available" );
214 if( mxMultiPropSet.is() ) // first try the XMultiPropertySet
216 rValues = mxMultiPropSet->getPropertyValues( rPropNames );
218 else if( mxPropSet.is() )
220 sal_Int32 nLen = rPropNames.getLength();
221 rValues.realloc( nLen );
222 std::transform(rPropNames.begin(), rPropNames.end(), rValues.getArray(),
223 [this](const OUString& rPropName) -> Any { return mxPropSet->getPropertyValue(rPropName); });
226 catch( Exception& )
231 // Set properties -------------------------------------------------------------
233 void ScfPropertySet::SetAnyProperty( const OUString& rPropName, const Any& rValue )
237 if( mxPropSet.is() )
238 mxPropSet->setPropertyValue( rPropName, rValue );
240 catch (const Exception&)
242 SAL_WARN("sc", "ScfPropertySet::SetAnyProperty - cannot set property \"" + rPropName + "\"");
246 void ScfPropertySet::SetProperties( const Sequence< OUString >& rPropNames, const Sequence< Any >& rValues )
248 OSL_ENSURE( rPropNames.getLength() == rValues.getLength(), "ScfPropertySet::SetProperties - length of sequences different" );
251 if( mxMultiPropSet.is() ) // first try the XMultiPropertySet
253 mxMultiPropSet->setPropertyValues( rPropNames, rValues );
255 else if( mxPropSet.is() )
257 OSL_FAIL( "ScfPropertySet::SetProperties - multi property set not available" );
258 const OUString* pPropName = rPropNames.getConstArray();
259 const OUString* pPropNameEnd = pPropName + rPropNames.getLength();
260 const Any* pValue = rValues.getConstArray();
261 for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
262 mxPropSet->setPropertyValue( *pPropName, *pValue );
265 catch( Exception& )
267 OSL_FAIL( "ScfPropertySet::SetAnyProperty - cannot set multiple properties" );
271 ScfPropSetHelper::ScfPropSetHelper( const char* const* ppcPropNames ) :
272 mnNextIdx( 0 )
274 OSL_ENSURE( ppcPropNames, "ScfPropSetHelper::ScfPropSetHelper - no strings found" );
276 // create OUStrings from ASCII property names
277 typedef ::std::pair< OUString, size_t > IndexedOUString;
278 std::vector<IndexedOUString> aPropNameVec;
279 for( size_t nVecIdx = 0; *ppcPropNames; ++ppcPropNames, ++nVecIdx )
281 OUString aPropName = OUString::createFromAscii( *ppcPropNames );
282 aPropNameVec.emplace_back( aPropName, nVecIdx );
285 // sorts the pairs, which will be sorted by first component (the property name)
286 ::std::sort( aPropNameVec.begin(), aPropNameVec.end() );
288 // resize member sequences
289 size_t nSize = aPropNameVec.size();
290 maNameSeq.realloc( static_cast< sal_Int32 >( nSize ) );
291 auto pNameSeq = maNameSeq.getArray();
292 maValueSeq.realloc( static_cast< sal_Int32 >( nSize ) );
293 maNameOrder.resize( nSize );
295 // fill the property name sequence and store original sort order
296 sal_Int32 nSeqIdx = 0;
297 for( auto& aPropName : aPropNameVec )
299 pNameSeq[ nSeqIdx ] = aPropName.first;
300 maNameOrder[ aPropName.second ] = nSeqIdx;
301 ++nSeqIdx;
305 // read properties ------------------------------------------------------------
307 void ScfPropSetHelper::ReadFromPropertySet( const ScfPropertySet& rPropSet )
309 rPropSet.GetProperties( maValueSeq, maNameSeq );
310 mnNextIdx = 0;
313 void ScfPropSetHelper::ReadValue( Any& rAny )
315 Any* pAny = GetNextAny();
316 if( pAny )
317 rAny = *pAny;
320 void ScfPropSetHelper::ReadValue( Color& rColor )
322 sal_Int32 nApiColor(0);
323 ReadValue( nApiColor );
324 rColor = Color( ColorTransparency, nApiColor );
327 void ScfPropSetHelper::ReadValue( bool& rbValue )
329 Any aAny;
330 ReadValue( aAny );
331 rbValue = ScUnoHelpFunctions::GetBoolFromAny( aAny );
334 // write properties -----------------------------------------------------------
336 void ScfPropSetHelper::InitializeWrite()
338 mnNextIdx = 0;
341 void ScfPropSetHelper::WriteValue( const Any& rAny )
343 if( Any* pAny = GetNextAny() )
344 *pAny = rAny;
347 void ScfPropSetHelper::WriteValue( bool rbValue )
349 if( Any* pAny = GetNextAny() )
350 *pAny <<= rbValue;
353 void ScfPropSetHelper::WriteToPropertySet( ScfPropertySet& rPropSet ) const
355 rPropSet.SetProperties( maNameSeq, maValueSeq );
358 // private --------------------------------------------------------------------
360 Any* ScfPropSetHelper::GetNextAny()
362 OSL_ENSURE( mnNextIdx < maNameOrder.size(), "ScfPropSetHelper::GetNextAny - sequence overflow" );
363 Any* pAny = nullptr;
364 if( mnNextIdx < maNameOrder.size() )
365 pAny = &maValueSeq.getArray()[ maNameOrder[ mnNextIdx++ ] ];
366 return pAny;
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */