Update ooo320-m1
[ooovba.git] / sc / source / filter / ftools / fapihelper.cxx
blob31190b6407b1bcb500fcb8526ed6c20df423da85
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fapihelper.cxx,v $
10 * $Revision: 1.10.32.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
33 #include "fapihelper.hxx"
35 #include <algorithm>
36 #include <com/sun/star/lang/XServiceName.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/beans/XPropertyState.hpp>
39 #include <comphelper/docpasswordhelper.hxx>
40 #include <comphelper/processfactory.hxx>
41 #include <tools/urlobj.hxx>
42 #include <sfx2/objsh.hxx>
43 #include <sfx2/docfile.hxx>
44 #include <sfx2/sfxsids.hrc>
45 #include <svtools/stritem.hxx>
46 #include <svtools/itemset.hxx>
47 #include "miscuno.hxx"
49 using ::rtl::OUString;
50 using ::com::sun::star::uno::Any;
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::Sequence;
53 using ::com::sun::star::uno::Exception;
54 using ::com::sun::star::uno::UNO_QUERY;
55 using ::com::sun::star::uno::UNO_QUERY_THROW;
56 using ::com::sun::star::uno::UNO_SET_THROW;
57 using ::com::sun::star::uno::TypeClass_BOOLEAN;
58 using ::com::sun::star::uno::XInterface;
59 using ::com::sun::star::beans::XPropertySet;
60 using ::com::sun::star::beans::XPropertyState;
61 using ::com::sun::star::lang::XServiceName;
62 using ::com::sun::star::lang::XMultiServiceFactory;
63 using ::com::sun::star::task::PasswordRequestMode_PASSWORD_ENTER;
65 // Static helper functions ====================================================
67 OUString ScfApiHelper::GetServiceName( Reference< XInterface > xInt )
69 OUString aService;
70 Reference< XServiceName > xServiceName( xInt, UNO_QUERY );
71 if( xServiceName.is() )
72 aService = xServiceName->getServiceName();
73 return aService;
76 Reference< XMultiServiceFactory > ScfApiHelper::GetServiceFactory( SfxObjectShell* pShell )
78 Reference< XMultiServiceFactory > xFactory;
79 if( pShell )
80 xFactory.set( pShell->GetModel(), UNO_QUERY );
81 return xFactory;
84 Reference< XInterface > ScfApiHelper::CreateInstance(
85 Reference< XMultiServiceFactory > xFactory, const OUString& rServiceName )
87 Reference< XInterface > xInt;
88 if( xFactory.is() )
90 try
92 xInt = xFactory->createInstance( rServiceName );
94 catch( Exception& )
96 DBG_ERRORFILE( "ScfApiHelper::CreateInstance - cannot create instance" );
99 return xInt;
102 Reference< XInterface > ScfApiHelper::CreateInstance( SfxObjectShell* pShell, const OUString& rServiceName )
104 return CreateInstance( GetServiceFactory( pShell ), rServiceName );
107 Reference< XInterface > ScfApiHelper::CreateInstance( const OUString& rServiceName )
109 return CreateInstance( ::comphelper::getProcessServiceFactory(), rServiceName );
112 Reference< XInterface > ScfApiHelper::CreateInstanceWithArgs(
113 Reference< XMultiServiceFactory > xFactory, const OUString& rServiceName, const Sequence< Any >& rArgs )
115 Reference< XInterface > xInt;
116 if( xFactory.is() )
120 xInt = xFactory->createInstanceWithArguments( rServiceName, rArgs );
122 catch( Exception& )
124 DBG_ERRORFILE( "ScfApiHelper::CreateInstanceWithArgs - cannot create instance" );
127 return xInt;
130 //UNUSED2008-05 Reference< XInterface > ScfApiHelper::CreateInstanceWithArgs(
131 //UNUSED2008-05 SfxObjectShell* pShell, const OUString& rServiceName, const Sequence< Any >& rArgs )
132 //UNUSED2008-05 {
133 //UNUSED2008-05 return CreateInstanceWithArgs( GetServiceFactory( pShell ), rServiceName, rArgs );
134 //UNUSED2008-05 }
136 Reference< XInterface > ScfApiHelper::CreateInstanceWithArgs(
137 const OUString& rServiceName, const Sequence< Any >& rArgs )
139 return CreateInstanceWithArgs( ::comphelper::getProcessServiceFactory(), rServiceName, rArgs );
142 String ScfApiHelper::QueryPasswordForMedium( SfxMedium& rMedium,
143 ::comphelper::IDocPasswordVerifier& rVerifier, const ::std::vector< OUString >* pDefaultPasswords )
145 OUString aMediaPassword;
146 SfxItemSet* pItemSet = rMedium.GetItemSet();
147 const SfxPoolItem *pPasswordItem;
148 if( pItemSet && (SFX_ITEM_SET == pItemSet->GetItemState( SID_PASSWORD, TRUE, &pPasswordItem )) )
149 aMediaPassword = static_cast< const SfxStringItem* >( pPasswordItem )->GetValue();
150 OUString aDocName = INetURLObject( rMedium.GetOrigURL() ).GetName( INetURLObject::DECODE_WITH_CHARSET );
152 bool bIsDefaultPassword = false;
153 OUString aPassword = ::comphelper::DocPasswordHelper::requestAndVerifyDocPassword(
154 rVerifier, aMediaPassword, rMedium.GetInteractionHandler(), aDocName,
155 ::comphelper::DocPasswordRequestType_MS, pDefaultPasswords, &bIsDefaultPassword );
157 if( !bIsDefaultPassword && (aPassword.getLength() > 0) && pItemSet )
158 pItemSet->Put( SfxStringItem( SID_PASSWORD, aPassword ) );
160 return aPassword;
163 // Property sets ==============================================================
165 void ScfPropertySet::Set( Reference< XPropertySet > xPropSet )
167 mxPropSet = xPropSet;
168 mxMultiPropSet.set( mxPropSet, UNO_QUERY );
171 OUString ScfPropertySet::GetServiceName() const
173 return ScfApiHelper::GetServiceName( mxPropSet );
176 // Get properties -------------------------------------------------------------
178 bool ScfPropertySet::HasProperty( const OUString& rPropName ) const
180 bool bHasProp = false;
183 Reference< XPropertyState > xPropState( mxPropSet, UNO_QUERY_THROW );
184 bHasProp = xPropState->getPropertyState( rPropName ) == ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
186 catch( Exception& )
189 return bHasProp;
192 bool ScfPropertySet::GetAnyProperty( Any& rValue, const OUString& rPropName ) const
194 bool bHasValue = false;
197 if( mxPropSet.is() )
199 rValue = mxPropSet->getPropertyValue( rPropName );
200 bHasValue = true;
203 catch( Exception& )
206 return bHasValue;
209 bool ScfPropertySet::GetBoolProperty( const ::rtl::OUString& rPropName ) const
211 Any aAny;
212 return GetAnyProperty( aAny, rPropName ) && ScUnoHelpFunctions::GetBoolFromAny( aAny );
215 bool ScfPropertySet::GetStringProperty( String& rValue, const OUString& rPropName ) const
217 OUString aOUString;
218 bool bRet = GetProperty( aOUString, rPropName );
219 rValue = aOUString;
220 return bRet;
223 bool ScfPropertySet::GetColorProperty( Color& rColor, const ::rtl::OUString& rPropName ) const
225 sal_Int32 nApiColor = 0;
226 bool bRet = GetProperty( nApiColor, rPropName );
227 rColor = ScfApiHelper::ConvertFromApiColor( nApiColor );
228 return bRet;
231 void ScfPropertySet::GetProperties( Sequence< Any >& rValues, const Sequence< OUString >& rPropNames ) const
235 DBG_ASSERT( mxMultiPropSet.is(), "ScfPropertySet::GetProperties - multi property set not available" );
236 if( mxMultiPropSet.is() ) // first try the XMultiPropertySet
238 rValues = mxMultiPropSet->getPropertyValues( rPropNames );
240 else if( mxPropSet.is() )
242 sal_Int32 nLen = rPropNames.getLength();
243 const OUString* pPropName = rPropNames.getConstArray();
244 const OUString* pPropNameEnd = pPropName + nLen;
245 rValues.realloc( nLen );
246 Any* pValue = rValues.getArray();
247 for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
248 *pValue = mxPropSet->getPropertyValue( *pPropName );
251 catch( Exception& )
256 // Set properties -------------------------------------------------------------
258 void ScfPropertySet::SetAnyProperty( const OUString& rPropName, const Any& rValue )
262 if( mxPropSet.is() )
263 mxPropSet->setPropertyValue( rPropName, rValue );
265 catch( Exception& )
267 DBG_ERRORFILE(
268 ByteString( "ScfPropertySet::SetAnyProperty - cannot set property \"" ).
269 Append( ByteString( String( rPropName ), RTL_TEXTENCODING_ASCII_US ) ).
270 Append( '"' ).
271 GetBuffer() );
275 void ScfPropertySet::SetProperties( const Sequence< OUString >& rPropNames, const Sequence< Any >& rValues )
277 DBG_ASSERT( rPropNames.getLength() == rValues.getLength(), "ScfPropertySet::SetProperties - length of sequences different" );
280 if( mxMultiPropSet.is() ) // first try the XMultiPropertySet
282 mxMultiPropSet->setPropertyValues( rPropNames, rValues );
284 else if( mxPropSet.is() )
286 DBG_ERRORFILE( "ScfPropertySet::SetProperties - multi property set not available" );
287 const OUString* pPropName = rPropNames.getConstArray();
288 const OUString* pPropNameEnd = pPropName + rPropNames.getLength();
289 const Any* pValue = rValues.getConstArray();
290 for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
291 mxPropSet->setPropertyValue( *pPropName, *pValue );
294 catch( Exception& )
296 DBG_ERRORFILE( "ScfPropertySet::SetAnyProperty - cannot set multiple properties" );
300 // ============================================================================
302 ScfPropSetHelper::ScfPropSetHelper( const sal_Char* const* ppcPropNames ) :
303 mnNextIdx( 0 )
305 DBG_ASSERT( ppcPropNames, "ScfPropSetHelper::ScfPropSetHelper - no strings found" );
307 // create OUStrings from ASCII property names
308 typedef ::std::pair< OUString, size_t > IndexedOUString;
309 typedef ::std::vector< IndexedOUString > IndexedOUStringVec;
310 IndexedOUStringVec aPropNameVec;
311 for( size_t nVecIdx = 0; *ppcPropNames; ++ppcPropNames, ++nVecIdx )
313 OUString aPropName = OUString::createFromAscii( *ppcPropNames );
314 aPropNameVec.push_back( IndexedOUString( aPropName, nVecIdx ) );
317 // sorts the pairs, which will be sorted by first component (the property name)
318 ::std::sort( aPropNameVec.begin(), aPropNameVec.end() );
320 // resize member sequences
321 size_t nSize = aPropNameVec.size();
322 maNameSeq.realloc( static_cast< sal_Int32 >( nSize ) );
323 maValueSeq.realloc( static_cast< sal_Int32 >( nSize ) );
324 maNameOrder.resize( nSize );
326 // fill the property name sequence and store original sort order
327 sal_Int32 nSeqIdx = 0;
328 for( IndexedOUStringVec::const_iterator aIt = aPropNameVec.begin(),
329 aEnd = aPropNameVec.end(); aIt != aEnd; ++aIt, ++nSeqIdx )
331 maNameSeq[ nSeqIdx ] = aIt->first;
332 maNameOrder[ aIt->second ] = nSeqIdx;
336 // read properties ------------------------------------------------------------
338 void ScfPropSetHelper::ReadFromPropertySet( const ScfPropertySet& rPropSet )
340 rPropSet.GetProperties( maValueSeq, maNameSeq );
341 mnNextIdx = 0;
344 bool ScfPropSetHelper::ReadValue( UnoAny& rAny )
346 UnoAny* pAny = GetNextAny();
347 if( pAny )
348 rAny = *pAny;
349 return pAny != 0;
352 bool ScfPropSetHelper::ReadValue( String& rString )
354 OUString aOUString;
355 bool bRet = ReadValue( aOUString );
356 rString = aOUString;
357 return bRet;
360 bool ScfPropSetHelper::ReadValue( Color& rColor )
362 sal_Int32 nApiColor(0);
363 bool bRet = ReadValue( nApiColor );
364 rColor = ScfApiHelper::ConvertFromApiColor( nApiColor );
365 return bRet;
368 bool ScfPropSetHelper::ReadValue( bool& rbValue )
370 Any aAny;
371 bool bRet = ReadValue( aAny );
372 rbValue = ScUnoHelpFunctions::GetBoolFromAny( aAny );
373 return bRet;
376 // write properties -----------------------------------------------------------
378 void ScfPropSetHelper::InitializeWrite( bool bClearAllAnys )
380 mnNextIdx = 0;
381 if( bClearAllAnys )
382 for( sal_Int32 nIdx = 0, nLen = maValueSeq.getLength(); nIdx < nLen; ++nIdx )
383 maValueSeq[ nIdx ].clear();
386 void ScfPropSetHelper::WriteValue( const Any& rAny )
388 if( UnoAny* pAny = GetNextAny() )
389 *pAny = rAny;
392 void ScfPropSetHelper::WriteValue( const bool& rbValue )
394 if( Any* pAny = GetNextAny() )
395 ScUnoHelpFunctions::SetBoolInAny( *pAny, rbValue );
398 void ScfPropSetHelper::WriteToPropertySet( ScfPropertySet& rPropSet ) const
400 rPropSet.SetProperties( maNameSeq, maValueSeq );
403 // private --------------------------------------------------------------------
405 Any* ScfPropSetHelper::GetNextAny()
407 DBG_ASSERT( mnNextIdx < maNameOrder.size(), "ScfPropSetHelper::GetNextAny - sequence overflow" );
408 Any* pAny = 0;
409 if( mnNextIdx < maNameOrder.size() )
410 pAny = &maValueSeq[ maNameOrder[ mnNextIdx++ ] ];
411 return pAny;
414 // ============================================================================