Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "fapihelper.hxx"
30 : :
31 : : #include <algorithm>
32 : : #include <com/sun/star/lang/XServiceName.hpp>
33 : : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 : : #include <com/sun/star/beans/XPropertyState.hpp>
35 : : #include <com/sun/star/beans/XPropertySetOption.hpp>
36 : : #include <comphelper/docpasswordhelper.hxx>
37 : : #include <comphelper/processfactory.hxx>
38 : : #include <tools/urlobj.hxx>
39 : : #include <rtl/strbuf.hxx>
40 : : #include <sfx2/objsh.hxx>
41 : : #include <sfx2/docfile.hxx>
42 : : #include <sfx2/request.hxx>
43 : : #include <sfx2/frame.hxx>
44 : : #include <sfx2/sfxsids.hrc>
45 : : #include <svl/stritem.hxx>
46 : : #include <svl/itemset.hxx>
47 : : #include "miscuno.hxx"
48 : :
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;
64 : :
65 : : using namespace ::com::sun::star;
66 : :
67 : : // Static helper functions ====================================================
68 : :
69 : 0 : OUString ScfApiHelper::GetServiceName( Reference< XInterface > xInt )
70 : : {
71 : 0 : OUString aService;
72 [ # # ]: 0 : Reference< XServiceName > xServiceName( xInt, UNO_QUERY );
73 [ # # ]: 0 : if( xServiceName.is() )
74 [ # # ][ # # ]: 0 : aService = xServiceName->getServiceName();
75 : 0 : return aService;
76 : : }
77 : :
78 : 52 : Reference< XMultiServiceFactory > ScfApiHelper::GetServiceFactory( SfxObjectShell* pShell )
79 : : {
80 : 52 : Reference< XMultiServiceFactory > xFactory;
81 [ + - ]: 52 : if( pShell )
82 [ + - ][ + - ]: 52 : xFactory.set( pShell->GetModel(), UNO_QUERY );
83 : 52 : return xFactory;
84 : : }
85 : :
86 : 124 : Reference< XInterface > ScfApiHelper::CreateInstance(
87 : : Reference< XMultiServiceFactory > xFactory, const OUString& rServiceName )
88 : : {
89 : 124 : Reference< XInterface > xInt;
90 [ + - ]: 124 : if( xFactory.is() )
91 : : {
92 : : try
93 : : {
94 [ + - ][ + - ]: 124 : xInt = xFactory->createInstance( rServiceName );
[ + - ][ # # ]
95 : : }
96 [ # # ]: 0 : catch( Exception& )
97 : : {
98 : : OSL_FAIL( "ScfApiHelper::CreateInstance - cannot create instance" );
99 : : }
100 : : }
101 : 124 : return xInt;
102 : : }
103 : :
104 : 52 : Reference< XInterface > ScfApiHelper::CreateInstance( SfxObjectShell* pShell, const OUString& rServiceName )
105 : : {
106 [ + - ]: 52 : return CreateInstance( GetServiceFactory( pShell ), rServiceName );
107 : : }
108 : :
109 : 72 : Reference< XInterface > ScfApiHelper::CreateInstance( const OUString& rServiceName )
110 : : {
111 [ + - ]: 72 : return CreateInstance( ::comphelper::getProcessServiceFactory(), rServiceName );
112 : : }
113 : :
114 : 0 : uno::Sequence< beans::NamedValue > ScfApiHelper::QueryEncryptionDataForMedium( SfxMedium& rMedium,
115 : : ::comphelper::IDocPasswordVerifier& rVerifier, const ::std::vector< OUString >* pDefaultPasswords )
116 : : {
117 [ # # ]: 0 : uno::Sequence< beans::NamedValue > aEncryptionData;
118 [ # # ][ # # ]: 0 : SFX_ITEMSET_ARG( rMedium.GetItemSet(), pEncryptionDataItem, SfxUnoAnyItem, SID_ENCRYPTIONDATA, false);
[ # # ]
119 [ # # ]: 0 : if ( pEncryptionDataItem )
120 [ # # ]: 0 : pEncryptionDataItem->GetValue() >>= aEncryptionData;
121 : :
122 : 0 : ::rtl::OUString aPassword;
123 [ # # ][ # # ]: 0 : SFX_ITEMSET_ARG( rMedium.GetItemSet(), pPasswordItem, SfxStringItem, SID_PASSWORD, false);
[ # # ]
124 [ # # ]: 0 : if ( pPasswordItem )
125 [ # # ]: 0 : aPassword = pPasswordItem->GetValue();
126 : :
127 [ # # ][ # # ]: 0 : OUString aDocName = INetURLObject( rMedium.GetOrigURL() ).GetName( INetURLObject::DECODE_WITH_CHARSET );
[ # # ][ # # ]
128 : :
129 : 0 : bool bIsDefaultPassword = false;
130 : : aEncryptionData = ::comphelper::DocPasswordHelper::requestAndVerifyDocPassword(
131 : : rVerifier, aEncryptionData, aPassword, rMedium.GetInteractionHandler(), aDocName,
132 [ # # ][ # # ]: 0 : ::comphelper::DocPasswordRequestType_MS, pDefaultPasswords, &bIsDefaultPassword );
[ # # ][ # # ]
133 : :
134 [ # # ][ # # ]: 0 : rMedium.GetItemSet()->ClearItem( SID_PASSWORD );
135 [ # # ][ # # ]: 0 : rMedium.GetItemSet()->ClearItem( SID_ENCRYPTIONDATA );
136 : :
137 [ # # ][ # # ]: 0 : if( !bIsDefaultPassword && (aEncryptionData.getLength() > 0) )
[ # # ]
138 [ # # ][ # # ]: 0 : rMedium.GetItemSet()->Put( SfxUnoAnyItem( SID_ENCRYPTIONDATA, uno::makeAny( aEncryptionData ) ) );
[ # # ][ # # ]
[ # # ]
139 : :
140 : 0 : return aEncryptionData;
141 : : }
142 : :
143 : : // Property sets ==============================================================
144 : :
145 : 204 : ScfPropertySet::~ScfPropertySet()
146 : : {
147 [ + - ]: 204 : Reference<beans::XPropertySetOption> xPropSetOpt(mxPropSet, UNO_QUERY);
148 [ + + ]: 204 : if (xPropSetOpt.is())
149 : : // Turn the property value change notification back on when finished.
150 [ + - ][ + - ]: 204 : xPropSetOpt->enableChangeListenerNotification(true);
151 : 204 : }
152 : :
153 : 204 : void ScfPropertySet::Set( Reference< XPropertySet > xPropSet )
154 : : {
155 [ + - ]: 204 : mxPropSet = xPropSet;
156 [ + - ]: 204 : mxMultiPropSet.set( mxPropSet, UNO_QUERY );
157 [ + - ]: 204 : Reference<beans::XPropertySetOption> xPropSetOpt(mxPropSet, UNO_QUERY);
158 [ + + ]: 204 : if (xPropSetOpt.is())
159 : : // We don't want to broadcast property value changes during import to
160 : : // improve performance.
161 [ + - ][ + - ]: 204 : xPropSetOpt->enableChangeListenerNotification(false);
162 : 204 : }
163 : :
164 : 0 : OUString ScfPropertySet::GetServiceName() const
165 : : {
166 [ # # ]: 0 : return ScfApiHelper::GetServiceName( mxPropSet );
167 : : }
168 : :
169 : : // Get properties -------------------------------------------------------------
170 : :
171 : 0 : bool ScfPropertySet::HasProperty( const OUString& rPropName ) const
172 : : {
173 : 0 : bool bHasProp = false;
174 : : try
175 : : {
176 [ # # ]: 0 : Reference< XPropertyState > xPropState( mxPropSet, UNO_QUERY_THROW );
177 [ # # ][ # # ]: 0 : bHasProp = xPropState->getPropertyState( rPropName ) == ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
[ # # ]
178 : : }
179 : 0 : catch( Exception& )
180 : : {
181 : : }
182 : 0 : return bHasProp;
183 : : }
184 : :
185 : 3 : bool ScfPropertySet::GetAnyProperty( Any& rValue, const OUString& rPropName ) const
186 : : {
187 : 3 : bool bHasValue = false;
188 : : try
189 : : {
190 [ + - ]: 3 : if( mxPropSet.is() )
191 : : {
192 [ + - ][ + - ]: 3 : rValue = mxPropSet->getPropertyValue( rPropName );
193 : 3 : bHasValue = true;
194 : : }
195 : : }
196 : 0 : catch( Exception& )
197 : : {
198 : : }
199 [ # # ]: 3 : return bHasValue;
200 : : }
201 : :
202 : 3 : bool ScfPropertySet::GetBoolProperty( const ::rtl::OUString& rPropName ) const
203 : : {
204 : 3 : Any aAny;
205 [ + - ][ + - ]: 3 : return GetAnyProperty( aAny, rPropName ) && ScUnoHelpFunctions::GetBoolFromAny( aAny );
[ + - ][ + - ]
206 : : }
207 : :
208 : 0 : OUString ScfPropertySet::GetStringProperty( const OUString& rPropName ) const
209 : : {
210 : 0 : OUString aOUString;
211 [ # # ]: 0 : GetProperty( aOUString, rPropName );
212 : 0 : return aOUString;
213 : : }
214 : :
215 : 0 : bool ScfPropertySet::GetColorProperty( Color& rColor, const ::rtl::OUString& rPropName ) const
216 : : {
217 : 0 : sal_Int32 nApiColor = 0;
218 [ # # ]: 0 : bool bRet = GetProperty( nApiColor, rPropName );
219 : 0 : rColor = ScfApiHelper::ConvertFromApiColor( nApiColor );
220 : 0 : return bRet;
221 : : }
222 : :
223 : 0 : void ScfPropertySet::GetProperties( Sequence< Any >& rValues, const Sequence< OUString >& rPropNames ) const
224 : : {
225 : : try
226 : : {
227 : : OSL_ENSURE( mxMultiPropSet.is(), "ScfPropertySet::GetProperties - multi property set not available" );
228 [ # # ]: 0 : if( mxMultiPropSet.is() ) // first try the XMultiPropertySet
229 : : {
230 [ # # ][ # # ]: 0 : rValues = mxMultiPropSet->getPropertyValues( rPropNames );
[ # # ][ # # ]
[ # # ]
231 : : }
232 [ # # ]: 0 : else if( mxPropSet.is() )
233 : : {
234 : 0 : sal_Int32 nLen = rPropNames.getLength();
235 : 0 : const OUString* pPropName = rPropNames.getConstArray();
236 : 0 : const OUString* pPropNameEnd = pPropName + nLen;
237 [ # # ]: 0 : rValues.realloc( nLen );
238 [ # # ]: 0 : Any* pValue = rValues.getArray();
239 [ # # ]: 0 : for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
240 [ # # ][ # # ]: 0 : *pValue = mxPropSet->getPropertyValue( *pPropName );
241 : : }
242 : : }
243 : 0 : catch( Exception& )
244 : : {
245 : : }
246 : 0 : }
247 : :
248 : : // Set properties -------------------------------------------------------------
249 : :
250 : 422 : void ScfPropertySet::SetAnyProperty( const OUString& rPropName, const Any& rValue )
251 : : {
252 : : try
253 : : {
254 [ + - ]: 422 : if( mxPropSet.is() )
255 [ + - ][ + - ]: 422 : mxPropSet->setPropertyValue( rPropName, rValue );
256 : : }
257 : 0 : catch (const Exception&)
258 : : {
259 : : OSL_FAIL(rtl::OStringBuffer(RTL_CONSTASCII_STRINGPARAM(
260 : : "ScfPropertySet::SetAnyProperty - cannot set property \""))
261 : : .append(rtl::OUStringToOString(rPropName,
262 : : RTL_TEXTENCODING_ASCII_US))
263 : : .append('"').getStr());
264 : : }
265 [ # # ]: 422 : }
266 : :
267 : 174 : void ScfPropertySet::SetProperties( const Sequence< OUString >& rPropNames, const Sequence< Any >& rValues )
268 : : {
269 : : OSL_ENSURE( rPropNames.getLength() == rValues.getLength(), "ScfPropertySet::SetProperties - length of sequences different" );
270 : : try
271 : : {
272 [ + - ]: 174 : if( mxMultiPropSet.is() ) // first try the XMultiPropertySet
273 : : {
274 [ + - ][ + - ]: 174 : mxMultiPropSet->setPropertyValues( rPropNames, rValues );
275 : : }
276 [ # # ]: 0 : else if( mxPropSet.is() )
277 : : {
278 : : OSL_FAIL( "ScfPropertySet::SetProperties - multi property set not available" );
279 : 0 : const OUString* pPropName = rPropNames.getConstArray();
280 : 0 : const OUString* pPropNameEnd = pPropName + rPropNames.getLength();
281 : 0 : const Any* pValue = rValues.getConstArray();
282 [ # # ]: 0 : for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
283 [ # # ][ # # ]: 0 : mxPropSet->setPropertyValue( *pPropName, *pValue );
284 : : }
285 : : }
286 : 0 : catch( Exception& )
287 : : {
288 : : OSL_FAIL( "ScfPropertySet::SetAnyProperty - cannot set multiple properties" );
289 : : }
290 [ # # ]: 174 : }
291 : :
292 : : // ============================================================================
293 : :
294 : 1045 : ScfPropSetHelper::ScfPropSetHelper( const sal_Char* const* ppcPropNames ) :
295 [ + - ][ + - ]: 1045 : mnNextIdx( 0 )
296 : : {
297 : : OSL_ENSURE( ppcPropNames, "ScfPropSetHelper::ScfPropSetHelper - no strings found" );
298 : :
299 : : // create OUStrings from ASCII property names
300 : : typedef ::std::pair< OUString, size_t > IndexedOUString;
301 : : typedef ::std::vector< IndexedOUString > IndexedOUStringVec;
302 [ + - ]: 1045 : IndexedOUStringVec aPropNameVec;
303 [ + + ]: 5060 : for( size_t nVecIdx = 0; *ppcPropNames; ++ppcPropNames, ++nVecIdx )
304 : : {
305 : 4015 : OUString aPropName = OUString::createFromAscii( *ppcPropNames );
306 [ + - ]: 4015 : aPropNameVec.push_back( IndexedOUString( aPropName, nVecIdx ) );
307 : 4015 : }
308 : :
309 : : // sorts the pairs, which will be sorted by first component (the property name)
310 [ + - ]: 1045 : ::std::sort( aPropNameVec.begin(), aPropNameVec.end() );
311 : :
312 : : // resize member sequences
313 : 1045 : size_t nSize = aPropNameVec.size();
314 [ + - ]: 1045 : maNameSeq.realloc( static_cast< sal_Int32 >( nSize ) );
315 [ + - ]: 1045 : maValueSeq.realloc( static_cast< sal_Int32 >( nSize ) );
316 [ + - ]: 1045 : maNameOrder.resize( nSize );
317 : :
318 : : // fill the property name sequence and store original sort order
319 : 1045 : sal_Int32 nSeqIdx = 0;
320 [ + - ][ + - ]: 6105 : for( IndexedOUStringVec::const_iterator aIt = aPropNameVec.begin(),
[ + + ]
321 [ + - ]: 1045 : aEnd = aPropNameVec.end(); aIt != aEnd; ++aIt, ++nSeqIdx )
322 : : {
323 [ + - ]: 4015 : maNameSeq[ nSeqIdx ] = aIt->first;
324 [ + - ]: 4015 : maNameOrder[ aIt->second ] = nSeqIdx;
325 : 1045 : }
326 : 1045 : }
327 : :
328 : : // read properties ------------------------------------------------------------
329 : :
330 : 0 : void ScfPropSetHelper::ReadFromPropertySet( const ScfPropertySet& rPropSet )
331 : : {
332 : 0 : rPropSet.GetProperties( maValueSeq, maNameSeq );
333 : 0 : mnNextIdx = 0;
334 : 0 : }
335 : :
336 : 0 : bool ScfPropSetHelper::ReadValue( UnoAny& rAny )
337 : : {
338 : 0 : UnoAny* pAny = GetNextAny();
339 [ # # ]: 0 : if( pAny )
340 : 0 : rAny = *pAny;
341 : 0 : return pAny != 0;
342 : : }
343 : :
344 : 0 : bool ScfPropSetHelper::ReadValue( String& rString )
345 : : {
346 : 0 : OUString aOUString;
347 [ # # ]: 0 : bool bRet = ReadValue( aOUString );
348 [ # # ]: 0 : rString = aOUString;
349 : 0 : return bRet;
350 : : }
351 : :
352 : 0 : bool ScfPropSetHelper::ReadValue( Color& rColor )
353 : : {
354 : 0 : sal_Int32 nApiColor(0);
355 [ # # ]: 0 : bool bRet = ReadValue( nApiColor );
356 : 0 : rColor = ScfApiHelper::ConvertFromApiColor( nApiColor );
357 : 0 : return bRet;
358 : : }
359 : :
360 : 0 : bool ScfPropSetHelper::ReadValue( bool& rbValue )
361 : : {
362 : 0 : Any aAny;
363 [ # # ]: 0 : bool bRet = ReadValue( aAny );
364 [ # # ]: 0 : rbValue = ScUnoHelpFunctions::GetBoolFromAny( aAny );
365 : 0 : return bRet;
366 : : }
367 : :
368 : : // write properties -----------------------------------------------------------
369 : :
370 : 174 : void ScfPropSetHelper::InitializeWrite( bool bClearAllAnys )
371 : : {
372 : 174 : mnNextIdx = 0;
373 [ - + ]: 174 : if( bClearAllAnys )
374 [ # # ]: 0 : for( sal_Int32 nIdx = 0, nLen = maValueSeq.getLength(); nIdx < nLen; ++nIdx )
375 : 0 : maValueSeq[ nIdx ].clear();
376 : 174 : }
377 : :
378 : 42 : void ScfPropSetHelper::WriteValue( const Any& rAny )
379 : : {
380 [ + - ]: 42 : if( UnoAny* pAny = GetNextAny() )
381 : 42 : *pAny = rAny;
382 : 42 : }
383 : :
384 : 48 : void ScfPropSetHelper::WriteValue( const bool& rbValue )
385 : : {
386 [ + - ]: 48 : if( Any* pAny = GetNextAny() )
387 : 48 : ScUnoHelpFunctions::SetBoolInAny( *pAny, rbValue );
388 : 48 : }
389 : :
390 : 174 : void ScfPropSetHelper::WriteToPropertySet( ScfPropertySet& rPropSet ) const
391 : : {
392 : 174 : rPropSet.SetProperties( maNameSeq, maValueSeq );
393 : 174 : }
394 : :
395 : : // private --------------------------------------------------------------------
396 : :
397 : 768 : Any* ScfPropSetHelper::GetNextAny()
398 : : {
399 : : OSL_ENSURE( mnNextIdx < maNameOrder.size(), "ScfPropSetHelper::GetNextAny - sequence overflow" );
400 : 768 : Any* pAny = 0;
401 [ + - ]: 768 : if( mnNextIdx < maNameOrder.size() )
402 : 768 : pAny = &maValueSeq[ maNameOrder[ mnNextIdx++ ] ];
403 : 768 : return pAny;
404 : : }
405 : :
406 : : // ============================================================================
407 : :
408 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|