1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SC_SOURCE_FILTER_INC_FAPIHELPER_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_FAPIHELPER_HXX
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 <osl/diagnose.h>
27 #include <tools/color.hxx>
30 namespace com
{ namespace sun
{ namespace star
{
31 namespace lang
{ class XMultiServiceFactory
; }
34 namespace com
{ namespace sun
{ namespace star
{ namespace beans
{ struct NamedValue
; } } } }
35 namespace com
{ namespace sun
{ namespace star
{ namespace beans
{ class XPropertySet
; } } } }
36 namespace com
{ namespace sun
{ namespace star
{ namespace beans
{ class XMultiPropertySet
; } } } }
38 namespace comphelper
{ class IDocPasswordVerifier
; }
40 // Static helper functions ====================================================
45 /** Static API helper functions. */
49 /** Converts a non-empty vector into a UNO sequence containing elements of the same type. */
50 template< typename Type
>
51 static css::uno::Sequence
< Type
>
52 VectorToSequence( const ::std::vector
< Type
>& rVector
);
54 /** Returns the service name provided via the XServiceName interface, or an empty string on error. */
55 static OUString
GetServiceName( const css::uno::Reference
< css::uno::XInterface
>& xInt
);
57 /** Returns the multi service factory from a document shell. */
58 static css::uno::Reference
< css::lang::XMultiServiceFactory
> GetServiceFactory( const SfxObjectShell
* pShell
);
60 /** Creates an instance from the passed service name, using the passed service factory. */
61 static css::uno::Reference
< css::uno::XInterface
> CreateInstance(
62 const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xFactory
,
63 const OUString
& rServiceName
);
65 /** Creates an instance from the passed service name, using the service factory of the passed object. */
66 static css::uno::Reference
< css::uno::XInterface
> CreateInstance(
67 const SfxObjectShell
* pShell
,
68 const OUString
& rServiceName
);
70 /** Creates an instance from the passed service name, using the process service factory. */
71 static css::uno::Reference
< css::uno::XInterface
> CreateInstance( const OUString
& rServiceName
);
73 /** Opens a password dialog and returns the encryption data.
74 @return The encryption data or an empty sequence on 'Cancel' or any error. */
75 static css::uno::Sequence
< css::beans::NamedValue
> QueryEncryptionDataForMedium( SfxMedium
& rMedium
,
76 ::comphelper::IDocPasswordVerifier
& rVerifier
,
77 const ::std::vector
< OUString
>* pDefaultPasswords
);
80 template< typename Type
>
81 css::uno::Sequence
< Type
> ScfApiHelper::VectorToSequence( const ::std::vector
< Type
>& rVector
)
83 OSL_ENSURE( !rVector
.empty(), "ScfApiHelper::VectorToSequence - vector is empty" );
84 return css::uno::Sequence
<Type
>(rVector
.data(), static_cast< sal_Int32
>(rVector
.size()));
87 // Property sets ==============================================================
89 /** A wrapper for a UNO property set.
91 This class provides functions to silently get and set properties (without
92 exceptions, without the need to check validity of the UNO property set).
94 An instance is constructed with the reference to a UNO property set or any
95 other interface (the constructor will query for the XPropertySet interface
96 then). The reference to the property set will be kept as long as the
97 instance of this class is alive.
99 The functions GetProperties() and SetProperties() try to handle all passed
100 values at once, using the XMultiPropertySet interface. If the
101 implementation does not support the XMultiPropertySet interface, all
102 properties are handled separately in a loop.
107 explicit ScfPropertySet() {}
108 /** Constructs a property set wrapper with the passed UNO property set. */
109 explicit ScfPropertySet( const css::uno::Reference
< css::beans::XPropertySet
> & xPropSet
) { Set( xPropSet
); }
110 /** Constructs a property set wrapper after querying the XPropertySet interface. */
111 template< typename InterfaceType
>
112 explicit ScfPropertySet( const css::uno::Reference
< InterfaceType
>& xInterface
) { Set( xInterface
); }
116 ScfPropertySet(ScfPropertySet
const &) = default;
117 ScfPropertySet(ScfPropertySet
&&) = default;
118 ScfPropertySet
& operator =(ScfPropertySet
const &) = default;
119 ScfPropertySet
& operator =(ScfPropertySet
&&) = default;
121 /** Sets the passed UNO property set and releases the old UNO property set. */
122 void Set( css::uno::Reference
< css::beans::XPropertySet
> const & xPropSet
);
123 /** Queries the passed interface for an XPropertySet and releases the old UNO property set. */
124 template< typename InterfaceType
>
125 void Set( css::uno::Reference
< InterfaceType
> xInterface
)
126 { Set( css::uno::Reference
< css::beans::XPropertySet
>( xInterface
, css::uno::UNO_QUERY
) ); }
128 /** Returns true, if the contained XPropertySet interface is valid. */
129 bool Is() const { return mxPropSet
.is(); }
131 /** Returns the contained XPropertySet interface. */
132 const css::uno::Reference
< css::beans::XPropertySet
>& GetApiPropertySet() const { return mxPropSet
; }
134 /** Returns the service name provided via the XServiceName interface, or an empty string on error. */
135 OUString
GetServiceName() const;
137 // Get properties ---------------------------------------------------------
139 /** Returns true, if the property set contains the specified property. */
140 bool HasProperty( const OUString
& rPropName
) const;
142 /** Gets the specified property from the property set.
143 @return true, if the Any could be filled with the property value. */
144 bool GetAnyProperty( css::uno::Any
& rValue
, const OUString
& rPropName
) const;
146 /** Gets the specified property from the property set.
147 @return true, if the passed variable could be filled with the property value. */
148 template< typename Type
>
149 bool GetProperty( Type
& rValue
, const OUString
& rPropName
) const
150 { css::uno::Any aAny
; return GetAnyProperty( aAny
, rPropName
) && (aAny
>>= rValue
); }
152 /** Gets the specified Boolean property from the property set.
153 @return true = property contains true; false = property contains false or error occurred. */
154 bool GetBoolProperty( const OUString
& rPropName
) const;
156 /** Gets the specified Boolean property from the property set. */
157 OUString
GetStringProperty( const OUString
& rPropName
) const;
159 /** Gets the specified color property from the property set.
160 @return true, if the passed color variable could be filled with the property value. */
161 bool GetColorProperty( Color
& rColor
, const OUString
& rPropName
) const;
163 /** Gets the specified properties from the property set. Tries to use the XMultiPropertySet interface.
164 @param rPropNames The property names. MUST be ordered alphabetically.
165 @param rValues The related property values. */
166 void GetProperties( css::uno::Sequence
< css::uno::Any
>& rValues
, const css::uno::Sequence
< OUString
>& rPropNames
) const;
168 // Set properties ---------------------------------------------------------
170 /** Puts the passed Any into the property set. */
171 void SetAnyProperty( const OUString
& rPropName
, const css::uno::Any
& rValue
);
173 /** Puts the passed value into the property set. */
174 template< typename Type
>
175 void SetProperty( const OUString
& rPropName
, const Type
& rValue
)
176 { SetAnyProperty( rPropName
, css::uno::makeAny( rValue
) ); }
178 /** Puts the passed Boolean value into the property set. */
179 void SetBoolProperty( const OUString
& rPropName
, bool bValue
)
180 { SetAnyProperty( rPropName
, css::uno::Any( bValue
) ); }
182 /** Puts the passed string into the property set. */
183 void SetStringProperty( const OUString
& rPropName
, const OUString
& rValue
)
184 { SetProperty( rPropName
, rValue
); }
186 /** Puts the passed color into the property set. */
187 void SetColorProperty( const OUString
& rPropName
, const Color
& rColor
)
188 { SetProperty( rPropName
, sal_Int32( rColor
) ); }
190 /** Puts the passed properties into the property set. Tries to use the XMultiPropertySet interface.
191 @param rPropNames The property names. MUST be ordered alphabetically.
192 @param rValues The related property values. */
193 void SetProperties( const css::uno::Sequence
< OUString
> & rPropNames
, const css::uno::Sequence
< css::uno::Any
>& rValues
);
196 css::uno::Reference
< css::beans::XPropertySet
> mxPropSet
; /// The mandatory property set interface.
197 css::uno::Reference
< css::beans::XMultiPropertySet
> mxMultiPropSet
; /// The optional multi property set interface.
200 /** Generic helper class for reading from and writing to property sets.
203 1) Call the constructor with a null-terminated array of ASCII strings.
204 2a) Read properties from a property set: Call the ReadFromPropertySet()
205 function, then get the properties with the ReadValue() functions or the
206 operator>> stream operator. The properties are returned in order of the
207 array of property names passed in the constructor.
208 2b) Write properties to a property set: Call InitializeWrite() to start a
209 new cycle. Set the values with the WriteValue() functions or the
210 operator<< stream operator. The order of the properties is equal to the
211 array of property names passed in the constructor. Finally, call the
212 WriteToPropertySet() function.
214 class ScfPropSetHelper
217 /** @param ppPropNames A null-terminated array of ASCII property names. */
218 explicit ScfPropSetHelper( const sal_Char
* const* ppcPropNames
);
220 // read properties --------------------------------------------------------
222 /** Reads all values from the passed property set. */
223 void ReadFromPropertySet( const ScfPropertySet
& rPropSet
);
225 /** Reads the next value from the value sequence. */
226 template< typename Type
>
227 void ReadValue( Type
& rValue
);
228 /** Reads an Any from the value sequence. */
229 void ReadValue( css::uno::Any
& rAny
);
230 /** Reads a color value from the value sequence. */
231 void ReadValue( Color
& rColor
);
232 /** Reads a C++ boolean value from the value sequence. */
233 void ReadValue( bool& rbValue
);
235 // write properties -------------------------------------------------------
237 /** Must be called before reading or storing property values in the helper. */
238 void InitializeWrite();
240 /** Writes the next value to the value sequence. */
241 template< typename Type
>
242 void WriteValue( const Type
& rValue
);
243 /** Writes an Any to the value sequence. */
244 void WriteValue( const css::uno::Any
& rAny
);
245 /** Writes a color value to the value sequence. */
246 void WriteValue( const Color
& rColor
)
247 { WriteValue( sal_Int32( rColor
) ); }
248 /** Writes a C++ boolean value to the value sequence. */
249 void WriteValue( bool rbValue
);
251 /** Writes all values to the passed property set. */
252 void WriteToPropertySet( ScfPropertySet
& rPropSet
) const;
255 /** Returns a pointer to the next Any to be written to. */
256 css::uno::Any
* GetNextAny();
259 css::uno::Sequence
< OUString
> maNameSeq
; /// Sequence of property names.
260 css::uno::Sequence
< css::uno::Any
> maValueSeq
; /// Sequence of property values.
261 ScfInt32Vec maNameOrder
; /// Maps initial order to alphabetical order.
262 size_t mnNextIdx
; /// Counter for next Any to be processed.
265 template< typename Type
>
266 void ScfPropSetHelper::ReadValue( Type
& rValue
)
268 css::uno::Any
* pAny
= GetNextAny();
273 template< typename Type
>
274 void ScfPropSetHelper::WriteValue( const Type
& rValue
)
276 css::uno::Any
* pAny
= GetNextAny();
281 template< typename Type
>
282 ScfPropSetHelper
& operator>>( ScfPropSetHelper
& rPropSetHelper
, Type
& rValue
)
284 rPropSetHelper
.ReadValue( rValue
);
285 return rPropSetHelper
;
288 template< typename Type
>
289 ScfPropSetHelper
& operator<<( ScfPropSetHelper
& rPropSetHelper
, const Type
& rValue
)
291 rPropSetHelper
.WriteValue( rValue
);
292 return rPropSetHelper
;
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */