LibreOffice Module sc (master)  1
fapihelper.hxx
Go to the documentation of this file.
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 INCLUDED_SC_SOURCE_FILTER_INC_FAPIHELPER_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_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 <osl/diagnose.h>
27 #include <tools/color.hxx>
28 #include "ftools.hxx"
29 
30 namespace com::sun::star {
31  namespace lang { class XMultiServiceFactory; }
32 }
33 
34 namespace com::sun::star::beans { struct NamedValue; }
35 namespace com::sun::star::beans { class XPropertySet; }
36 namespace com::sun::star::beans { class XMultiPropertySet; }
37 
38 namespace comphelper { class IDocPasswordVerifier; }
39 
40 // Static helper functions ====================================================
41 
42 class SfxMedium;
43 class SfxObjectShell;
44 
47 {
48 public:
50  template< typename Type >
51  static css::uno::Sequence< Type >
52  VectorToSequence( const ::std::vector< Type >& rVector );
53 
55  static OUString GetServiceName( const css::uno::Reference< css::uno::XInterface >& xInt );
56 
58  static css::uno::Reference< css::lang::XMultiServiceFactory > GetServiceFactory( const SfxObjectShell* pShell );
59 
61  static css::uno::Reference< css::uno::XInterface > CreateInstance(
62  const css::uno::Reference< css::lang::XMultiServiceFactory >& xFactory,
63  const OUString& rServiceName );
64 
66  static css::uno::Reference< css::uno::XInterface > CreateInstance(
67  const SfxObjectShell* pShell,
68  const OUString& rServiceName );
69 
71  static css::uno::Reference< css::uno::XInterface > CreateInstance( const OUString& rServiceName );
72 
75  static css::uno::Sequence< css::beans::NamedValue > QueryEncryptionDataForMedium( SfxMedium& rMedium,
77  const ::std::vector< OUString >* pDefaultPasswords );
78 };
79 
80 template< typename Type >
81 css::uno::Sequence< Type > ScfApiHelper::VectorToSequence( const ::std::vector< Type >& rVector )
82 {
83  OSL_ENSURE( !rVector.empty(), "ScfApiHelper::VectorToSequence - vector is empty" );
84  return css::uno::Sequence<Type>(rVector.data(), static_cast< sal_Int32 >(rVector.size()));
85 }
86 
87 // Property sets ==============================================================
88 
105 {
106 public:
107  explicit ScfPropertySet() {}
109  explicit ScfPropertySet( const css::uno::Reference< css::beans::XPropertySet > & xPropSet ) { Set( xPropSet ); }
111  template< typename InterfaceType >
112  explicit ScfPropertySet( const css::uno::Reference< InterfaceType >& xInterface ) { Set( xInterface ); }
113 
114  ~ScfPropertySet();
115  //TODO:
116  ScfPropertySet(ScfPropertySet const &) = default;
117  ScfPropertySet(ScfPropertySet &&) = default;
118  ScfPropertySet & operator =(ScfPropertySet const &) = default;
119  ScfPropertySet & operator =(ScfPropertySet &&) = default;
120 
122  void Set( css::uno::Reference< css::beans::XPropertySet > const & xPropSet );
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 ) ); }
127 
129  bool Is() const { return mxPropSet.is(); }
130 
132  const css::uno::Reference< css::beans::XPropertySet >& GetApiPropertySet() const { return mxPropSet; }
133 
135  OUString GetServiceName() const;
136 
137  // Get properties ---------------------------------------------------------
138 
140  bool HasProperty( const OUString& rPropName ) const;
141 
144  bool GetAnyProperty( css::uno::Any& rValue, const OUString& rPropName ) const;
145 
148  template< typename Type >
149  bool GetProperty( Type& rValue, const OUString& rPropName ) const
150  { css::uno::Any aAny; return GetAnyProperty( aAny, rPropName ) && (aAny >>= rValue); }
151 
154  bool GetBoolProperty( const OUString& rPropName ) const;
155 
157  OUString GetStringProperty( const OUString& rPropName ) const;
158 
161  bool GetColorProperty( Color& rColor, const OUString& rPropName ) const;
162 
166  void GetProperties( css::uno::Sequence< css::uno::Any >& rValues, const css::uno::Sequence< OUString >& rPropNames ) const;
167 
168  // Set properties ---------------------------------------------------------
169 
171  void SetAnyProperty( const OUString& rPropName, const css::uno::Any& rValue );
172 
174  template< typename Type >
175  void SetProperty( const OUString& rPropName, const Type& rValue )
176  { SetAnyProperty( rPropName, css::uno::makeAny( rValue ) ); }
177 
179  void SetBoolProperty( const OUString& rPropName, bool bValue )
180  { SetAnyProperty( rPropName, css::uno::Any( bValue ) ); }
181 
183  void SetStringProperty( const OUString& rPropName, const OUString& rValue )
184  { SetProperty( rPropName, rValue ); }
185 
187  void SetColorProperty( const OUString& rPropName, const Color& rColor )
188  { SetProperty( rPropName, sal_Int32( rColor ) ); }
189 
193  void SetProperties( const css::uno::Sequence< OUString > & rPropNames, const css::uno::Sequence< css::uno::Any >& rValues );
194 
195 private:
196  css::uno::Reference< css::beans::XPropertySet > mxPropSet;
197  css::uno::Reference< css::beans::XMultiPropertySet > mxMultiPropSet;
198 };
199 
215 {
216 public:
218  explicit ScfPropSetHelper( const char* const* ppcPropNames );
219 
220  // read properties --------------------------------------------------------
221 
223  void ReadFromPropertySet( const ScfPropertySet& rPropSet );
224 
226  template< typename Type >
227  void ReadValue( Type& rValue );
229  void ReadValue( css::uno::Any& rAny );
231  void ReadValue( Color& rColor );
233  void ReadValue( bool& rbValue );
234 
235  // write properties -------------------------------------------------------
236 
238  void InitializeWrite();
239 
241  template< typename Type >
242  void WriteValue( const Type& rValue );
244  void WriteValue( const css::uno::Any& rAny );
246  void WriteValue( const Color& rColor )
247  { WriteValue( sal_Int32( rColor ) ); }
249  void WriteValue( bool rbValue );
250 
252  void WriteToPropertySet( ScfPropertySet& rPropSet ) const;
253 
254 private:
256  css::uno::Any* GetNextAny();
257 
258 private:
259  css::uno::Sequence< OUString > maNameSeq;
260  css::uno::Sequence< css::uno::Any > maValueSeq;
262  size_t mnNextIdx;
263 };
264 
265 template< typename Type >
267 {
268  css::uno::Any* pAny = GetNextAny();
269  if (pAny)
270  *pAny >>= rValue;
271 }
272 
273 template< typename Type >
274 void ScfPropSetHelper::WriteValue( const Type& rValue )
275 {
276  css::uno::Any* pAny = GetNextAny();
277  if( pAny )
278  *pAny <<= rValue;
279 }
280 
281 template< typename Type >
282 ScfPropSetHelper& operator>>( ScfPropSetHelper& rPropSetHelper, Type& rValue )
283 {
284  rPropSetHelper.ReadValue( rValue );
285  return rPropSetHelper;
286 }
287 
288 template< typename Type >
289 ScfPropSetHelper& operator<<( ScfPropSetHelper& rPropSetHelper, const Type& rValue )
290 {
291  rPropSetHelper.WriteValue( rValue );
292  return rPropSetHelper;
293 }
294 
295 #endif
296 
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
Static API helper functions.
Definition: fapihelper.hxx:46
ScfPropertySet(const css::uno::Reference< css::beans::XPropertySet > &xPropSet)
Constructs a property set wrapper with the passed UNO property set.
Definition: fapihelper.hxx:109
void SetProperties(const css::uno::Sequence< OUString > &rPropNames, const css::uno::Sequence< css::uno::Any > &rValues)
Puts the passed properties into the property set.
Definition: fapihelper.cxx:246
bool GetBoolProperty(const OUString &rPropName) const
Gets the specified Boolean property from the property set.
Definition: fapihelper.cxx:188
css::uno::Any * GetNextAny()
Returns a pointer to the next Any to be written to.
Definition: fapihelper.cxx:359
OUString GetStringProperty(const OUString &rPropName) const
Gets the specified Boolean property from the property set.
Definition: fapihelper.cxx:194
css::uno::Any const & rValue
void Set(css::uno::Reference< InterfaceType > xInterface)
Queries the passed interface for an XPropertySet and releases the old UNO property set...
Definition: fapihelper.hxx:125
static css::uno::Sequence< css::beans::NamedValue > QueryEncryptionDataForMedium(SfxMedium &rMedium,::comphelper::IDocPasswordVerifier &rVerifier, const ::std::vector< OUString > *pDefaultPasswords)
Opens a password dialog and returns the encryption data.
Definition: fapihelper.cxx:102
css::uno::Sequence< OUString > maNameSeq
Definition: fapihelper.hxx:259
void InitializeWrite()
Must be called before reading or storing property values in the helper.
Definition: fapihelper.cxx:335
OUString GetServiceName() const
Returns the service name provided via the XServiceName interface, or an empty string on error...
Definition: fapihelper.cxx:150
void ReadFromPropertySet(const ScfPropertySet &rPropSet)
Reads all values from the passed property set.
Definition: fapihelper.cxx:306
ScfPropSetHelper & operator<<(ScfPropSetHelper &rPropSetHelper, const Type &rValue)
Definition: fapihelper.hxx:289
void WriteToPropertySet(ScfPropertySet &rPropSet) const
Writes all values to the passed property set.
Definition: fapihelper.cxx:352
void WriteValue(const Type &rValue)
Writes the next value to the value sequence.
Definition: fapihelper.hxx:274
class SAL_NO_VTABLE XPropertySet
void WriteValue(const Color &rColor)
Writes a color value to the value sequence.
Definition: fapihelper.hxx:246
static css::uno::Reference< css::lang::XMultiServiceFactory > GetServiceFactory(const SfxObjectShell *pShell)
Returns the multi service factory from a document shell.
Definition: fapihelper.cxx:66
css::uno::Sequence< css::uno::Any > maValueSeq
Sequence of property names.
Definition: fapihelper.hxx:260
static OUString GetServiceName(const css::uno::Reference< css::uno::XInterface > &xInt)
Returns the service name provided via the XServiceName interface, or an empty string on error...
Definition: fapihelper.cxx:57
bool GetColorProperty(Color &rColor, const OUString &rPropName) const
Gets the specified color property from the property set.
Definition: fapihelper.cxx:201
static css::uno::Sequence< Type > VectorToSequence(const ::std::vector< Type > &rVector)
Converts a non-empty vector into a UNO sequence containing elements of the same type.
Definition: fapihelper.hxx:81
ScfPropSetHelper(const char *const *ppcPropNames)
Definition: fapihelper.cxx:271
static css::uno::Reference< css::uno::XInterface > CreateInstance(const css::uno::Reference< css::lang::XMultiServiceFactory > &xFactory, const OUString &rServiceName)
Creates an instance from the passed service name, using the passed service factory.
css::uno::Reference< css::beans::XMultiPropertySet > mxMultiPropSet
The mandatory property set interface.
Definition: fapihelper.hxx:197
ScfPropertySet(const css::uno::Reference< InterfaceType > &xInterface)
Constructs a property set wrapper after querying the XPropertySet interface.
Definition: fapihelper.hxx:112
css::uno::Reference< css::beans::XPropertySet > mxPropSet
Definition: fapihelper.hxx:196
void SetAnyProperty(const OUString &rPropName, const css::uno::Any &rValue)
Puts the passed Any into the property set.
Definition: fapihelper.cxx:233
size_t mnNextIdx
Maps initial order to alphabetical order.
Definition: fapihelper.hxx:262
bool Is() const
Returns true, if the contained XPropertySet interface is valid.
Definition: fapihelper.hxx:129
A wrapper for a UNO property set.
Definition: fapihelper.hxx:104
void SetBoolProperty(const OUString &rPropName, bool bValue)
Puts the passed Boolean value into the property set.
Definition: fapihelper.hxx:179
bool GetAnyProperty(css::uno::Any &rValue, const OUString &rPropName) const
Gets the specified property from the property set.
Definition: fapihelper.cxx:171
void SetStringProperty(const OUString &rPropName, const OUString &rValue)
Puts the passed string into the property set.
Definition: fapihelper.hxx:183
const css::uno::Reference< css::beans::XPropertySet > & GetApiPropertySet() const
Returns the contained XPropertySet interface.
Definition: fapihelper.hxx:132
void ReadValue(Type &rValue)
Reads the next value from the value sequence.
Definition: fapihelper.hxx:266
ScfPropSetHelper & operator>>(ScfPropSetHelper &rPropSetHelper, Type &rValue)
Definition: fapihelper.hxx:282
void SetColorProperty(const OUString &rPropName, const Color &rColor)
Puts the passed color into the property set.
Definition: fapihelper.hxx:187
void SetProperty(const OUString &rPropName, const Type &rValue)
Puts the passed value into the property set.
Definition: fapihelper.hxx:175
Reference< XSingleServiceFactory > xFactory
bool HasProperty(const OUString &rPropName) const
Returns true, if the property set contains the specified property.
Definition: fapihelper.cxx:157
void GetProperties(css::uno::Sequence< css::uno::Any > &rValues, const css::uno::Sequence< OUString > &rPropNames) const
Gets the specified properties from the property set.
Definition: fapihelper.cxx:209
::std::vector< sal_Int32 > ScfInt32Vec
Definition: ftools.hxx:254
ScfInt32Vec maNameOrder
Sequence of property values.
Definition: fapihelper.hxx:261
Generic helper class for reading from and writing to property sets.
Definition: fapihelper.hxx:214
void Set(css::uno::Reference< css::beans::XPropertySet > const &xPropSet)
Sets the passed UNO property set and releases the old UNO property set.
bool GetProperty(Type &rValue, const OUString &rPropName) const
Gets the specified property from the property set.
Definition: fapihelper.hxx:149
ScfPropertySet & operator=(ScfPropertySet const &)=default