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