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"
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
)
70 Reference
< XServiceName
> xServiceName( xInt
, UNO_QUERY
);
71 if( xServiceName
.is() )
72 aService
= xServiceName
->getServiceName();
76 Reference
< XMultiServiceFactory
> ScfApiHelper::GetServiceFactory( SfxObjectShell
* pShell
)
78 Reference
< XMultiServiceFactory
> xFactory
;
80 xFactory
.set( pShell
->GetModel(), UNO_QUERY
);
84 Reference
< XInterface
> ScfApiHelper::CreateInstance(
85 Reference
< XMultiServiceFactory
> xFactory
, const OUString
& rServiceName
)
87 Reference
< XInterface
> xInt
;
92 xInt
= xFactory
->createInstance( rServiceName
);
96 DBG_ERRORFILE( "ScfApiHelper::CreateInstance - cannot create instance" );
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
;
120 xInt
= xFactory
->createInstanceWithArguments( rServiceName
, rArgs
);
124 DBG_ERRORFILE( "ScfApiHelper::CreateInstanceWithArgs - cannot create instance" );
130 //UNUSED2008-05 Reference< XInterface > ScfApiHelper::CreateInstanceWithArgs(
131 //UNUSED2008-05 SfxObjectShell* pShell, const OUString& rServiceName, const Sequence< Any >& rArgs )
133 //UNUSED2008-05 return CreateInstanceWithArgs( GetServiceFactory( pShell ), rServiceName, rArgs );
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
) );
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
;
192 bool ScfPropertySet::GetAnyProperty( Any
& rValue
, const OUString
& rPropName
) const
194 bool bHasValue
= false;
199 rValue
= mxPropSet
->getPropertyValue( rPropName
);
209 bool ScfPropertySet::GetBoolProperty( const ::rtl::OUString
& rPropName
) const
212 return GetAnyProperty( aAny
, rPropName
) && ScUnoHelpFunctions::GetBoolFromAny( aAny
);
215 bool ScfPropertySet::GetStringProperty( String
& rValue
, const OUString
& rPropName
) const
218 bool bRet
= GetProperty( aOUString
, rPropName
);
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
);
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
);
256 // Set properties -------------------------------------------------------------
258 void ScfPropertySet::SetAnyProperty( const OUString
& rPropName
, const Any
& rValue
)
263 mxPropSet
->setPropertyValue( rPropName
, rValue
);
268 ByteString( "ScfPropertySet::SetAnyProperty - cannot set property \"" ).
269 Append( ByteString( String( rPropName
), RTL_TEXTENCODING_ASCII_US
) ).
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
);
296 DBG_ERRORFILE( "ScfPropertySet::SetAnyProperty - cannot set multiple properties" );
300 // ============================================================================
302 ScfPropSetHelper::ScfPropSetHelper( const sal_Char
* const* ppcPropNames
) :
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
);
344 bool ScfPropSetHelper::ReadValue( UnoAny
& rAny
)
346 UnoAny
* pAny
= GetNextAny();
352 bool ScfPropSetHelper::ReadValue( String
& rString
)
355 bool bRet
= ReadValue( aOUString
);
360 bool ScfPropSetHelper::ReadValue( Color
& rColor
)
362 sal_Int32
nApiColor(0);
363 bool bRet
= ReadValue( nApiColor
);
364 rColor
= ScfApiHelper::ConvertFromApiColor( nApiColor
);
368 bool ScfPropSetHelper::ReadValue( bool& rbValue
)
371 bool bRet
= ReadValue( aAny
);
372 rbValue
= ScUnoHelpFunctions::GetBoolFromAny( aAny
);
376 // write properties -----------------------------------------------------------
378 void ScfPropSetHelper::InitializeWrite( bool 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() )
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" );
409 if( mnNextIdx
< maNameOrder
.size() )
410 pAny
= &maValueSeq
[ maNameOrder
[ mnNextIdx
++ ] ];
414 // ============================================================================