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 : : #ifndef SC_FAPIHELPER_HXX
30 : : #define SC_FAPIHELPER_HXX
31 : :
32 : : #include <com/sun/star/uno/Any.hxx>
33 : : #include <com/sun/star/uno/Reference.hxx>
34 : : #include <com/sun/star/uno/Sequence.hxx>
35 : : #include <com/sun/star/beans/XPropertySet.hpp>
36 : : #include <com/sun/star/beans/XMultiPropertySet.hpp>
37 : : #include <com/sun/star/beans/NamedValue.hpp>
38 : : #include <tools/color.hxx>
39 : : #include <comphelper/types.hxx>
40 : : #include "ftools.hxx"
41 : : #include "scdllapi.h"
42 : :
43 : : namespace com { namespace sun { namespace star {
44 : : namespace lang { class XMultiServiceFactory; }
45 : : } } }
46 : :
47 : : namespace comphelper { class IDocPasswordVerifier; }
48 : :
49 : : // Static helper functions ====================================================
50 : :
51 : : class SfxMedium;
52 : : class SfxObjectShell;
53 : :
54 : : /** Static API helper functions. */
55 : : class ScfApiHelper
56 : : {
57 : : public:
58 : : typedef ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > XInterfaceRef;
59 : : typedef ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > XServiceFactoryRef;
60 : : typedef ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > UnoAnySequence;
61 : :
62 : : public:
63 : : /** Converts a tools color to a UNO color value. */
64 : 108 : inline static sal_Int32 ConvertToApiColor( const Color& rColor )
65 : 108 : { return static_cast< sal_Int32 >( rColor.GetColor() ); }
66 : : /** Converts a UNO color value to a tools color. */
67 : 0 : inline static Color ConvertFromApiColor( sal_Int32 nApiColor )
68 : 0 : { return Color( static_cast< ColorData >( nApiColor ) ); }
69 : :
70 : : /** Converts a non-empty vector into a UNO sequence containing elements of the same type. */
71 : : template< typename Type >
72 : : static ::com::sun::star::uno::Sequence< Type >
73 : : VectorToSequence( const ::std::vector< Type >& rVector );
74 : :
75 : : /** Returns the service name provided via the XServiceName interface, or an empty string on error. */
76 : : static ::rtl::OUString GetServiceName( XInterfaceRef xInt );
77 : :
78 : : /** Returns the multi service factory from a document shell. */
79 : : static XServiceFactoryRef GetServiceFactory( SfxObjectShell* pShell );
80 : :
81 : : /** Creates an instance from the passed service name, using the passed service factory. */
82 : : static XInterfaceRef CreateInstance(
83 : : XServiceFactoryRef xFactory,
84 : : const ::rtl::OUString& rServiceName );
85 : :
86 : : /** Creates an instance from the passed service name, using the service factory of the passed object. */
87 : : static XInterfaceRef CreateInstance(
88 : : SfxObjectShell* pShell,
89 : : const ::rtl::OUString& rServiceName );
90 : :
91 : : /** Creates an instance from the passed service name, using the process service factory. */
92 : : static XInterfaceRef CreateInstance( const ::rtl::OUString& rServiceName );
93 : :
94 : : /** Opens a password dialog and returns the encryption data.
95 : : @return The encryption data or an empty sequence on 'Cancel' or any error. */
96 : : static ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > QueryEncryptionDataForMedium( SfxMedium& rMedium,
97 : : ::comphelper::IDocPasswordVerifier& rVerifier,
98 : : const ::std::vector< ::rtl::OUString >* pDefaultPasswords = 0 );
99 : : };
100 : :
101 : : template< typename Type >
102 : 12 : ::com::sun::star::uno::Sequence< Type > ScfApiHelper::VectorToSequence( const ::std::vector< Type >& rVector )
103 : : {
104 : : OSL_ENSURE( !rVector.empty(), "ScfApiHelper::VectorToSequence - vector is empty" );
105 : 12 : return ::com::sun::star::uno::Sequence< Type >( &rVector.front(), static_cast< sal_Int32 >( rVector.size() ) );
106 : : }
107 : :
108 : : // Property sets ==============================================================
109 : :
110 : : /** A wrapper for a UNO property set.
111 : :
112 : : This class provides functions to silently get and set properties (without
113 : : exceptions, without the need to check validity of the UNO property set).
114 : :
115 : : An instance is constructed with the reference to a UNO property set or any
116 : : other interface (the constructor will query for the XPropertySet interface
117 : : then). The reference to the property set will be kept as long as the
118 : : instance of this class is alive.
119 : :
120 : : The functions GetProperties() and SetProperties() try to handle all passed
121 : : values at once, using the XMultiPropertySet interface. If the
122 : : implementation does not support the XMultiPropertySet interface, all
123 : : properties are handled separately in a loop.
124 : : */
125 : : class ScfPropertySet
126 : : {
127 : : public:
128 : : typedef ::com::sun::star::uno::Reference<
129 : : ::com::sun::star::beans::XPropertySet > XPropertySetRef;
130 : : typedef ::com::sun::star::uno::Reference<
131 : : ::com::sun::star::beans::XMultiPropertySet > XMultiPropSetRef;
132 : : typedef ::com::sun::star::uno::Any UnoAny;
133 : : typedef ::com::sun::star::uno::Sequence< UnoAny > UnoAnySequence;
134 : : typedef ::com::sun::star::uno::Sequence< ::rtl::OUString > OUStringSequence;
135 : :
136 : : public:
137 : 0 : inline explicit ScfPropertySet() {}
138 : : /** Constructs a property set wrapper with the passed UNO property set. */
139 [ + - ]: 36 : inline explicit ScfPropertySet( XPropertySetRef xPropSet ) { Set( xPropSet ); }
140 : : /** Constructs a property set wrapper after querying the XPropertySet interface. */
141 : : template< typename InterfaceType >
142 [ + - ][ + - ]: 168 : inline explicit ScfPropertySet( ::com::sun::star::uno::Reference< InterfaceType > xInterface ) { Set( xInterface ); }
[ # # ][ # # ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ # # ]
[ + - ][ + - ]
143 : :
144 : : ~ScfPropertySet();
145 : :
146 : : /** Sets the passed UNO property set and releases the old UNO property set. */
147 : : void Set( XPropertySetRef xPropSet );
148 : : /** Queries the passed interface for an XPropertySet and releases the old UNO property set. */
149 : : template< typename InterfaceType >
150 : 168 : inline void Set( ::com::sun::star::uno::Reference< InterfaceType > xInterface )
151 [ + - ][ + - ]: 168 : { Set( XPropertySetRef( xInterface, ::com::sun::star::uno::UNO_QUERY ) ); }
[ # # ][ # # ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ # # ]
[ + - ][ + - ]
152 : :
153 : : /** Returns true, if the contained XPropertySet interface is valid. */
154 : 0 : inline bool Is() const { return mxPropSet.is(); }
155 : :
156 : : /** Returns the contained XPropertySet interface. */
157 : 0 : inline XPropertySetRef GetApiPropertySet() const { return mxPropSet; }
158 : :
159 : : /** Returns the service name provided via the XServiceName interface, or an empty string on error. */
160 : : ::rtl::OUString GetServiceName() const;
161 : :
162 : : // Get properties ---------------------------------------------------------
163 : :
164 : : /** Returns true, if the property set contains the specified property. */
165 : : bool HasProperty( const ::rtl::OUString& rPropName ) const;
166 : :
167 : : /** Gets the specified property from the property set.
168 : : @return true, if the Any could be filled with the property value. */
169 : : bool GetAnyProperty( UnoAny& rValue, const ::rtl::OUString& rPropName ) const;
170 : :
171 : : /** Gets the specified property from the property set.
172 : : @return true, if the passed variable could be filled with the property value. */
173 : : template< typename Type >
174 : 0 : inline bool GetProperty( Type& rValue, const ::rtl::OUString& rPropName ) const
175 [ # # ][ # # ]: 0 : { UnoAny aAny; return GetAnyProperty( aAny, rPropName ) && (aAny >>= rValue); }
[ # # # ]
[ # # ][ # # ]
[ # # # ]
[ # # # ]
[ # # # ]
[ # # ][ # # ]
[ # # ]
[ # # # ]
[ # # # ]
[ # # # ]
[ # # ][ # # ]
[ # # # ]
[ # # ]
[ # # # ]
[ # # ][ # # ]
[ # # # ]
[ # # ]
[ # # # ]
[ # # ][ # # ]
[ # # # ]
[ # # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
176 : :
177 : : /** Gets the specified Boolean property from the property set.
178 : : @return true = property contains true; false = property contains false or error occurred. */
179 : : bool GetBoolProperty( const ::rtl::OUString& rPropName ) const;
180 : :
181 : : /** Gets the specified Boolean property from the property set. */
182 : : rtl::OUString GetStringProperty( const ::rtl::OUString& rPropName ) const;
183 : :
184 : : /** Gets the specified color property from the property set.
185 : : @return true, if the passed color variable could be filled with the property value. */
186 : : bool GetColorProperty( Color& rColor, const ::rtl::OUString& rPropName ) const;
187 : :
188 : : /** Gets the specified properties from the property set. Tries to use the XMultiPropertySet interface.
189 : : @param rPropNames The property names. MUST be ordered alphabetically.
190 : : @param rValues The related property values. */
191 : : void GetProperties( UnoAnySequence& rValues, const OUStringSequence& rPropNames ) const;
192 : :
193 : : // Set properties ---------------------------------------------------------
194 : :
195 : : /** Puts the passed Any into the property set. */
196 : : void SetAnyProperty( const ::rtl::OUString& rPropName, const UnoAny& rValue );
197 : :
198 : : /** Puts the passed value into the property set. */
199 : : template< typename Type >
200 : 280 : inline void SetProperty( const ::rtl::OUString& rPropName, const Type& rValue )
201 [ + - ][ + - ]: 280 : { SetAnyProperty( rPropName, ::com::sun::star::uno::makeAny( rValue ) ); }
[ + - ][ + - ]
[ # # ][ + - ]
[ + - ][ + - ]
[ # # ][ + - ]
[ # # ][ # # ]
[ # # ][ + - ]
[ # # ][ + - ]
[ + - ][ # # ]
[ # # ][ + - ]
202 : :
203 : : /** Puts the passed Boolean value into the property set. */
204 : 142 : inline void SetBoolProperty( const ::rtl::OUString& rPropName, bool bValue )
205 [ + - ]: 142 : { SetAnyProperty( rPropName, ::comphelper::makeBoolAny( bValue ) ); }
206 : :
207 : : /** Puts the passed string into the property set. */
208 : 76 : inline void SetStringProperty( const ::rtl::OUString& rPropName, const String& rValue )
209 [ + - ]: 76 : { SetProperty( rPropName, ::rtl::OUString( rValue ) ); }
210 : :
211 : : /** Puts the passed color into the property set. */
212 : 0 : inline void SetColorProperty( const ::rtl::OUString& rPropName, const Color& rColor )
213 [ # # ]: 0 : { SetProperty( rPropName, ScfApiHelper::ConvertToApiColor( rColor ) ); }
214 : :
215 : : /** Puts the passed properties into the property set. Tries to use the XMultiPropertySet interface.
216 : : @param rPropNames The property names. MUST be ordered alphabetically.
217 : : @param rValues The related property values. */
218 : : void SetProperties( const OUStringSequence& rPropNames, const UnoAnySequence& rValues );
219 : :
220 : : // ------------------------------------------------------------------------
221 : : private:
222 : : XPropertySetRef mxPropSet; /// The mandatory property set interface.
223 : : XMultiPropSetRef mxMultiPropSet; /// The optional multi property set interface.
224 : : };
225 : :
226 : : // ----------------------------------------------------------------------------
227 : :
228 : : /** Generic helper class for reading from and writing to property sets.
229 : :
230 : : Usage:
231 : : 1) Call the constructor with a null-terminated array of ASCII strings.
232 : : 2a) Read properties from a property set: Call the ReadFromPropertySet()
233 : : function, then get the properties with the ReadValue() functions or the
234 : : operator>> stream operator. The properties are returned in order of the
235 : : array of property names passed in the constructor.
236 : : 2b) Write properties to a property set: Call InitializeWrite() to start a
237 : : new cycle. Set the values with the WriteValue() functions or the
238 : : operator<< stream operator. The order of the properties is equal to the
239 : : array of property names passed in the constructor. Finally, call the
240 : : WriteToPropertySet() function.
241 : : */
242 [ + - ]: 1045 : class ScfPropSetHelper
243 : : {
244 : : public:
245 : : typedef ::com::sun::star::uno::Any UnoAny;
246 : :
247 : : public:
248 : : /** @param ppPropNames A null-terminated array of ASCII property names. */
249 : : explicit ScfPropSetHelper( const sal_Char* const* ppcPropNames );
250 : :
251 : : // read properties --------------------------------------------------------
252 : :
253 : : /** Reads all values from the passed property set. */
254 : : void ReadFromPropertySet( const ScfPropertySet& rPropSet );
255 : :
256 : : /** Reads the next value from the value sequence. */
257 : : template< typename Type >
258 : : bool ReadValue( Type& rValue );
259 : : /** Reads an Any from the value sequence. */
260 : : bool ReadValue( UnoAny& rAny );
261 : : /** Reads a tools string from the value sequence. */
262 : : bool ReadValue( String& rString );
263 : : /** Reads a color value from the value sequence. */
264 : : bool ReadValue( Color& rColor );
265 : : /** Reads a C++ boolean value from the value sequence. */
266 : : bool ReadValue( bool& rbValue );
267 : :
268 : : // write properties -------------------------------------------------------
269 : :
270 : : /** Must be called before reading or storing property values in the helper. */
271 : : void InitializeWrite( bool bClearAllAnys = false );
272 : :
273 : : /** Writes the next value to the value sequence. */
274 : : template< typename Type >
275 : : void WriteValue( const Type& rValue );
276 : : /** Writes an Any to the value sequence. */
277 : : void WriteValue( const UnoAny& rAny );
278 : : /** Writes a tools string to the value sequence. */
279 : 39 : inline void WriteValue( const String& rString )
280 [ + - ]: 39 : { WriteValue( ::rtl::OUString( rString ) ); }
281 : : /** Writes a color value to the value sequence. */
282 : 60 : inline void WriteValue( const Color& rColor )
283 [ + - ]: 60 : { WriteValue( ScfApiHelper::ConvertToApiColor( rColor ) ); }
284 : : /** Writes a C++ boolean value to the value sequence. */
285 : : void WriteValue( const bool& rbValue );
286 : :
287 : : /** Writes all values to the passed property set. */
288 : : void WriteToPropertySet( ScfPropertySet& rPropSet ) const;
289 : :
290 : : // ------------------------------------------------------------------------
291 : : private:
292 : : /** Returns a pointer to the next Any to be written to. */
293 : : UnoAny* GetNextAny();
294 : :
295 : : private:
296 : : typedef ::com::sun::star::uno::Sequence< ::rtl::OUString > OUStringSequence;
297 : : typedef ::com::sun::star::uno::Sequence< UnoAny > UnoAnySequence;
298 : :
299 : : OUStringSequence maNameSeq; /// Sequence of property names.
300 : : UnoAnySequence maValueSeq; /// Sequence of property values.
301 : : ScfInt32Vec maNameOrder; /// Maps initial order to alphabetical order.
302 : : size_t mnNextIdx; /// Counter for next Any to be processed.
303 : : };
304 : :
305 : : // ----------------------------------------------------------------------------
306 : :
307 : : template< typename Type >
308 : 0 : bool ScfPropSetHelper::ReadValue( Type& rValue )
309 : : {
310 : 0 : UnoAny* pAny = GetNextAny();
311 [ # # ][ # # ]: 0 : return pAny && (*pAny >>= rValue);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
312 : : }
313 : :
314 : : template< typename Type >
315 : 678 : void ScfPropSetHelper::WriteValue( const Type& rValue )
316 : : {
317 : 678 : UnoAny* pAny = GetNextAny();
318 [ # # + - : 678 : if( pAny )
+ - + - +
- + - ]
319 : 678 : *pAny <<= rValue;
320 : 678 : }
321 : :
322 : : template< typename Type >
323 : 0 : ScfPropSetHelper& operator>>( ScfPropSetHelper& rPropSetHelper, Type& rValue )
324 : : {
325 : 0 : rPropSetHelper.ReadValue( rValue );
326 : 0 : return rPropSetHelper;
327 : : }
328 : :
329 : : template< typename Type >
330 : 768 : ScfPropSetHelper& operator<<( ScfPropSetHelper& rPropSetHelper, const Type& rValue )
331 : : {
332 : 768 : rPropSetHelper.WriteValue( rValue );
333 : 768 : return rPropSetHelper;
334 : : }
335 : :
336 : : // ============================================================================
337 : :
338 : : #endif
339 : :
340 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|