Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / inc / fapihelper.hxx
blob7449f1193797cdbf68e31f0fd801dde9d4a46011
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #pragma once
22 #include <com/sun/star/uno/Any.hxx>
23 #include <com/sun/star/uno/Reference.hxx>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <osl/diagnose.h>
26 #include <tools/color.hxx>
27 #include "ftools.hxx"
29 namespace com::sun::star {
30 namespace lang { class XMultiServiceFactory; }
33 namespace com::sun::star::beans { struct NamedValue; }
34 namespace com::sun::star::beans { class XPropertySet; }
35 namespace com::sun::star::beans { class XMultiPropertySet; }
37 namespace comphelper { class IDocPasswordVerifier; }
39 // Static helper functions ====================================================
41 class SfxMedium;
42 class SfxObjectShell;
44 /** Static API helper functions. */
45 class ScfApiHelper
47 public:
48 /** Converts a non-empty vector into a UNO sequence containing elements of the same type. */
49 template< typename Type >
50 static css::uno::Sequence< Type >
51 VectorToSequence( const ::std::vector< Type >& rVector );
53 /** Returns the service name provided via the XServiceName interface, or an empty string on error. */
54 static OUString GetServiceName( const css::uno::Reference< css::uno::XInterface >& xInt );
56 /** Returns the multi service factory from a document shell. */
57 static css::uno::Reference< css::lang::XMultiServiceFactory > GetServiceFactory( const SfxObjectShell* pShell );
59 /** Creates an instance from the passed service name, using the passed service factory. */
60 static css::uno::Reference< css::uno::XInterface > CreateInstance(
61 const css::uno::Reference< css::lang::XMultiServiceFactory >& xFactory,
62 const OUString& rServiceName );
64 /** Creates an instance from the passed service name, using the service factory of the passed object. */
65 static css::uno::Reference< css::uno::XInterface > CreateInstance(
66 const SfxObjectShell* pShell,
67 const OUString& rServiceName );
69 /** Creates an instance from the passed service name, using the process service factory. */
70 static css::uno::Reference< css::uno::XInterface > CreateInstance( const OUString& rServiceName );
72 /** Opens a password dialog and returns the encryption data.
73 @return The encryption data or an empty sequence on 'Cancel' or any error. */
74 static css::uno::Sequence< css::beans::NamedValue > QueryEncryptionDataForMedium( SfxMedium& rMedium,
75 ::comphelper::IDocPasswordVerifier& rVerifier,
76 const ::std::vector< OUString >* pDefaultPasswords );
79 template< typename Type >
80 css::uno::Sequence< Type > ScfApiHelper::VectorToSequence( const ::std::vector< Type >& rVector )
82 OSL_ENSURE( !rVector.empty(), "ScfApiHelper::VectorToSequence - vector is empty" );
83 return css::uno::Sequence<Type>(rVector.data(), static_cast< sal_Int32 >(rVector.size()));
86 // Property sets ==============================================================
88 /** A wrapper for a UNO property set.
90 This class provides functions to silently get and set properties (without
91 exceptions, without the need to check validity of the UNO property set).
93 An instance is constructed with the reference to a UNO property set or any
94 other interface (the constructor will query for the XPropertySet interface
95 then). The reference to the property set will be kept as long as the
96 instance of this class is alive.
98 The functions GetProperties() and SetProperties() try to handle all passed
99 values at once, using the XMultiPropertySet interface. If the
100 implementation does not support the XMultiPropertySet interface, all
101 properties are handled separately in a loop.
103 class ScfPropertySet
105 public:
106 explicit ScfPropertySet() {}
107 /** Constructs a property set wrapper with the passed UNO property set. */
108 explicit ScfPropertySet( const css::uno::Reference< css::beans::XPropertySet > & xPropSet ) { Set( xPropSet ); }
109 /** Constructs a property set wrapper after querying the XPropertySet interface. */
110 template< typename InterfaceType >
111 explicit ScfPropertySet( const css::uno::Reference< InterfaceType >& xInterface ) { Set( xInterface ); }
113 ~ScfPropertySet();
114 //TODO:
115 ScfPropertySet(ScfPropertySet const &) = default;
116 ScfPropertySet(ScfPropertySet &&) = default;
117 ScfPropertySet & operator =(ScfPropertySet const &) = default;
118 ScfPropertySet & operator =(ScfPropertySet &&) = default;
120 /** Sets the passed UNO property set and releases the old UNO property set. */
121 void Set( css::uno::Reference< css::beans::XPropertySet > const & xPropSet );
122 /** Queries the passed interface for an XPropertySet and releases the old UNO property set. */
123 template< typename InterfaceType >
124 void Set( css::uno::Reference< InterfaceType > xInterface )
125 { Set( css::uno::Reference< css::beans::XPropertySet >( xInterface, css::uno::UNO_QUERY ) ); }
127 /** Returns true, if the contained XPropertySet interface is valid. */
128 bool Is() const { return mxPropSet.is(); }
130 /** Returns the contained XPropertySet interface. */
131 const css::uno::Reference< css::beans::XPropertySet >& GetApiPropertySet() const { return mxPropSet; }
133 /** Returns the service name provided via the XServiceName interface, or an empty string on error. */
134 OUString GetServiceName() const;
136 // Get properties ---------------------------------------------------------
138 /** Returns true, if the property set contains the specified property. */
139 bool HasProperty( const OUString& rPropName ) const;
141 /** Gets the specified property from the property set.
142 @return true, if the Any could be filled with the property value. */
143 bool GetAnyProperty( css::uno::Any& rValue, const OUString& rPropName ) const;
145 /** Gets the specified property from the property set.
146 @return true, if the passed variable could be filled with the property value. */
147 template< typename Type >
148 bool GetProperty( Type& rValue, const OUString& rPropName ) const
149 { css::uno::Any aAny; return GetAnyProperty( aAny, rPropName ) && (aAny >>= rValue); }
151 /** Gets the specified Boolean property from the property set.
152 @return true = property contains true; false = property contains false or error occurred. */
153 bool GetBoolProperty( const OUString& rPropName ) const;
155 /** Gets the specified Boolean property from the property set. */
156 OUString GetStringProperty( const OUString& rPropName ) const;
158 /** Gets the specified color property from the property set.
159 @return true, if the passed color variable could be filled with the property value. */
160 bool GetColorProperty( Color& rColor, const OUString& rPropName ) const;
162 /** Gets the specified properties from the property set. Tries to use the XMultiPropertySet interface.
163 @param rPropNames The property names. MUST be ordered alphabetically.
164 @param rValues The related property values. */
165 void GetProperties( css::uno::Sequence< css::uno::Any >& rValues, const css::uno::Sequence< OUString >& rPropNames ) const;
167 // Set properties ---------------------------------------------------------
169 /** Puts the passed Any into the property set. */
170 void SetAnyProperty( const OUString& rPropName, const css::uno::Any& rValue );
172 /** Puts the passed value into the property set. */
173 template< typename Type >
174 void SetProperty( const OUString& rPropName, const Type& rValue )
175 { SetAnyProperty( rPropName, css::uno::Any( rValue ) ); }
177 /** Puts the passed Boolean value into the property set. */
178 void SetBoolProperty( const OUString& rPropName, bool bValue )
179 { SetAnyProperty( rPropName, css::uno::Any( bValue ) ); }
181 /** Puts the passed string into the property set. */
182 void SetStringProperty( const OUString& rPropName, const OUString& rValue )
183 { SetProperty( rPropName, rValue ); }
185 /** Puts the passed color into the property set. */
186 void SetColorProperty( const OUString& rPropName, const Color& rColor )
187 { SetProperty( rPropName, sal_Int32( rColor ) ); }
189 /** Puts the passed properties into the property set. Tries to use the XMultiPropertySet interface.
190 @param rPropNames The property names. MUST be ordered alphabetically.
191 @param rValues The related property values. */
192 void SetProperties( const css::uno::Sequence< OUString > & rPropNames, const css::uno::Sequence< css::uno::Any >& rValues );
194 private:
195 css::uno::Reference< css::beans::XPropertySet > mxPropSet; /// The mandatory property set interface.
196 css::uno::Reference< css::beans::XMultiPropertySet > mxMultiPropSet; /// The optional multi property set interface.
199 /** Generic helper class for reading from and writing to property sets.
201 Usage:
202 1) Call the constructor with a null-terminated array of ASCII strings.
203 2a) Read properties from a property set: Call the ReadFromPropertySet()
204 function, then get the properties with the ReadValue() functions or the
205 operator>> stream operator. The properties are returned in order of the
206 array of property names passed in the constructor.
207 2b) Write properties to a property set: Call InitializeWrite() to start a
208 new cycle. Set the values with the WriteValue() functions or the
209 operator<< stream operator. The order of the properties is equal to the
210 array of property names passed in the constructor. Finally, call the
211 WriteToPropertySet() function.
213 class ScfPropSetHelper
215 public:
216 /** @param ppPropNames A null-terminated array of ASCII property names. */
217 explicit ScfPropSetHelper( const char* const* ppcPropNames );
219 // read properties --------------------------------------------------------
221 /** Reads all values from the passed property set. */
222 void ReadFromPropertySet( const ScfPropertySet& rPropSet );
224 /** Reads the next value from the value sequence. */
225 template< typename Type >
226 void ReadValue( Type& rValue );
227 /** Reads an Any from the value sequence. */
228 void ReadValue( css::uno::Any& rAny );
229 /** Reads a color value from the value sequence. */
230 void ReadValue( Color& rColor );
231 /** Reads a C++ boolean value from the value sequence. */
232 void ReadValue( bool& rbValue );
234 // write properties -------------------------------------------------------
236 /** Must be called before reading or storing property values in the helper. */
237 void InitializeWrite();
239 /** Writes the next value to the value sequence. */
240 template< typename Type >
241 void WriteValue( const Type& rValue );
242 /** Writes an Any to the value sequence. */
243 void WriteValue( const css::uno::Any& rAny );
244 /** Writes a color value to the value sequence. */
245 void WriteValue( const Color& rColor )
246 { WriteValue( sal_Int32( rColor ) ); }
247 /** Writes a C++ boolean value to the value sequence. */
248 void WriteValue( bool rbValue );
250 /** Writes all values to the passed property set. */
251 void WriteToPropertySet( ScfPropertySet& rPropSet ) const;
253 private:
254 /** Returns a pointer to the next Any to be written to. */
255 css::uno::Any* GetNextAny();
257 private:
258 css::uno::Sequence< OUString > maNameSeq; /// Sequence of property names.
259 css::uno::Sequence< css::uno::Any > maValueSeq; /// Sequence of property values.
260 ScfInt32Vec maNameOrder; /// Maps initial order to alphabetical order.
261 size_t mnNextIdx; /// Counter for next Any to be processed.
264 template< typename Type >
265 void ScfPropSetHelper::ReadValue( Type& rValue )
267 css::uno::Any* pAny = GetNextAny();
268 if (pAny)
269 *pAny >>= rValue;
272 template< typename Type >
273 void ScfPropSetHelper::WriteValue( const Type& rValue )
275 css::uno::Any* pAny = GetNextAny();
276 if( pAny )
277 *pAny <<= rValue;
280 template< typename Type >
281 ScfPropSetHelper& operator>>( ScfPropSetHelper& rPropSetHelper, Type& rValue )
283 rPropSetHelper.ReadValue( rValue );
284 return rPropSetHelper;
287 template< typename Type >
288 ScfPropSetHelper& operator<<( ScfPropSetHelper& rPropSetHelper, const Type& rValue )
290 rPropSetHelper.WriteValue( rValue );
291 return rPropSetHelper;
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */