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