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 #pragma once
21 
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"
28 
29 namespace com::sun::star {
30  namespace lang { class XMultiServiceFactory; }
31 }
32 
33 namespace com::sun::star::beans { struct NamedValue; }
34 namespace com::sun::star::beans { class XPropertySet; }
35 namespace com::sun::star::beans { class XMultiPropertySet; }
36 
37 namespace comphelper { class IDocPasswordVerifier; }
38 
39 // Static helper functions ====================================================
40 
41 class SfxMedium;
42 class SfxObjectShell;
43 
46 {
47 public:
49  template< typename Type >
50  static css::uno::Sequence< Type >
51  VectorToSequence( const ::std::vector< Type >& rVector );
52 
54  static OUString GetServiceName( const css::uno::Reference< css::uno::XInterface >& xInt );
55 
57  static css::uno::Reference< css::lang::XMultiServiceFactory > GetServiceFactory( const SfxObjectShell* pShell );
58 
60  static css::uno::Reference< css::uno::XInterface > CreateInstance(
61  const css::uno::Reference< css::lang::XMultiServiceFactory >& xFactory,
62  const OUString& rServiceName );
63 
65  static css::uno::Reference< css::uno::XInterface > CreateInstance(
66  const SfxObjectShell* pShell,
67  const OUString& rServiceName );
68 
70  static css::uno::Reference< css::uno::XInterface > CreateInstance( const OUString& rServiceName );
71 
74  static css::uno::Sequence< css::beans::NamedValue > QueryEncryptionDataForMedium( SfxMedium& rMedium,
76  const ::std::vector< OUString >* pDefaultPasswords );
77 };
78 
79 template< typename Type >
80 css::uno::Sequence< Type > ScfApiHelper::VectorToSequence( const ::std::vector< Type >& rVector )
81 {
82  OSL_ENSURE( !rVector.empty(), "ScfApiHelper::VectorToSequence - vector is empty" );
83  return css::uno::Sequence<Type>(rVector.data(), static_cast< sal_Int32 >(rVector.size()));
84 }
85 
86 // Property sets ==============================================================
87 
104 {
105 public:
106  explicit ScfPropertySet() {}
108  explicit ScfPropertySet( const css::uno::Reference< css::beans::XPropertySet > & xPropSet ) { Set( xPropSet ); }
110  template< typename InterfaceType >
111  explicit ScfPropertySet( const css::uno::Reference< InterfaceType >& xInterface ) { Set( xInterface ); }
112 
113  ~ScfPropertySet();
114  //TODO:
115  ScfPropertySet(ScfPropertySet const &) = default;
116  ScfPropertySet(ScfPropertySet &&) = default;
117  ScfPropertySet & operator =(ScfPropertySet const &) = default;
118  ScfPropertySet & operator =(ScfPropertySet &&) = default;
119 
121  void Set( css::uno::Reference< css::beans::XPropertySet > const & xPropSet );
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 ) ); }
126 
128  bool Is() const { return mxPropSet.is(); }
129 
131  const css::uno::Reference< css::beans::XPropertySet >& GetApiPropertySet() const { return mxPropSet; }
132 
134  OUString GetServiceName() const;
135 
136  // Get properties ---------------------------------------------------------
137 
139  bool HasProperty( const OUString& rPropName ) const;
140 
143  bool GetAnyProperty( css::uno::Any& rValue, const OUString& rPropName ) const;
144 
147  template< typename Type >
148  bool GetProperty( Type& rValue, const OUString& rPropName ) const
149  { css::uno::Any aAny; return GetAnyProperty( aAny, rPropName ) && (aAny >>= rValue); }
150 
153  bool GetBoolProperty( const OUString& rPropName ) const;
154 
156  OUString GetStringProperty( const OUString& rPropName ) const;
157 
160  bool GetColorProperty( Color& rColor, const OUString& rPropName ) const;
161 
165  void GetProperties( css::uno::Sequence< css::uno::Any >& rValues, const css::uno::Sequence< OUString >& rPropNames ) const;
166 
167  // Set properties ---------------------------------------------------------
168 
170  void SetAnyProperty( const OUString& rPropName, const css::uno::Any& rValue );
171 
173  template< typename Type >
174  void SetProperty( const OUString& rPropName, const Type& rValue )
175  { SetAnyProperty( rPropName, css::uno::makeAny( rValue ) ); }
176 
178  void SetBoolProperty( const OUString& rPropName, bool bValue )
179  { SetAnyProperty( rPropName, css::uno::Any( bValue ) ); }
180 
182  void SetStringProperty( const OUString& rPropName, const OUString& rValue )
183  { SetProperty( rPropName, rValue ); }
184 
186  void SetColorProperty( const OUString& rPropName, const Color& rColor )
187  { SetProperty( rPropName, sal_Int32( rColor ) ); }
188 
192  void SetProperties( const css::uno::Sequence< OUString > & rPropNames, const css::uno::Sequence< css::uno::Any >& rValues );
193 
194 private:
195  css::uno::Reference< css::beans::XPropertySet > mxPropSet;
196  css::uno::Reference< css::beans::XMultiPropertySet > mxMultiPropSet;
197 };
198 
214 {
215 public:
217  explicit ScfPropSetHelper( const char* const* ppcPropNames );
218 
219  // read properties --------------------------------------------------------
220 
222  void ReadFromPropertySet( const ScfPropertySet& rPropSet );
223 
225  template< typename Type >
226  void ReadValue( Type& rValue );
228  void ReadValue( css::uno::Any& rAny );
230  void ReadValue( Color& rColor );
232  void ReadValue( bool& rbValue );
233 
234  // write properties -------------------------------------------------------
235 
237  void InitializeWrite();
238 
240  template< typename Type >
241  void WriteValue( const Type& rValue );
243  void WriteValue( const css::uno::Any& rAny );
245  void WriteValue( const Color& rColor )
246  { WriteValue( sal_Int32( rColor ) ); }
248  void WriteValue( bool rbValue );
249 
251  void WriteToPropertySet( ScfPropertySet& rPropSet ) const;
252 
253 private:
255  css::uno::Any* GetNextAny();
256 
257 private:
258  css::uno::Sequence< OUString > maNameSeq;
259  css::uno::Sequence< css::uno::Any > maValueSeq;
261  size_t mnNextIdx;
262 };
263 
264 template< typename Type >
266 {
267  css::uno::Any* pAny = GetNextAny();
268  if (pAny)
269  *pAny >>= rValue;
270 }
271 
272 template< typename Type >
273 void ScfPropSetHelper::WriteValue( const Type& rValue )
274 {
275  css::uno::Any* pAny = GetNextAny();
276  if( pAny )
277  *pAny <<= rValue;
278 }
279 
280 template< typename Type >
281 ScfPropSetHelper& operator>>( ScfPropSetHelper& rPropSetHelper, Type& rValue )
282 {
283  rPropSetHelper.ReadValue( rValue );
284  return rPropSetHelper;
285 }
286 
287 template< typename Type >
288 ScfPropSetHelper& operator<<( ScfPropSetHelper& rPropSetHelper, const Type& rValue )
289 {
290  rPropSetHelper.WriteValue( rValue );
291  return rPropSetHelper;
292 }
293 
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
Static API helper functions.
Definition: fapihelper.hxx:45
ScfPropertySet(const css::uno::Reference< css::beans::XPropertySet > &xPropSet)
Constructs a property set wrapper with the passed UNO property set.
Definition: fapihelper.hxx:108
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
void Set(css::uno::Reference< InterfaceType > xInterface)
Queries the passed interface for an XPropertySet and releases the old UNO property set...
Definition: fapihelper.hxx:124
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:258
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:288
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:273
class SAL_NO_VTABLE XPropertySet
void WriteValue(const Color &rColor)
Writes a color value to the value sequence.
Definition: fapihelper.hxx:245
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:259
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:80
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:196
ScfPropertySet(const css::uno::Reference< InterfaceType > &xInterface)
Constructs a property set wrapper after querying the XPropertySet interface.
Definition: fapihelper.hxx:111
css::uno::Reference< css::beans::XPropertySet > mxPropSet
Definition: fapihelper.hxx:195
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:261
bool Is() const
Returns true, if the contained XPropertySet interface is valid.
Definition: fapihelper.hxx:128
A wrapper for a UNO property set.
Definition: fapihelper.hxx:103
void SetBoolProperty(const OUString &rPropName, bool bValue)
Puts the passed Boolean value into the property set.
Definition: fapihelper.hxx:178
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:182
const css::uno::Reference< css::beans::XPropertySet > & GetApiPropertySet() const
Returns the contained XPropertySet interface.
Definition: fapihelper.hxx:131
void ReadValue(Type &rValue)
Reads the next value from the value sequence.
Definition: fapihelper.hxx:265
ScfPropSetHelper & operator>>(ScfPropSetHelper &rPropSetHelper, Type &rValue)
Definition: fapihelper.hxx:281
void SetColorProperty(const OUString &rPropName, const Color &rColor)
Puts the passed color into the property set.
Definition: fapihelper.hxx:186
void SetProperty(const OUString &rPropName, const Type &rValue)
Puts the passed value into the property set.
Definition: fapihelper.hxx:174
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:256
ScfInt32Vec maNameOrder
Sequence of property values.
Definition: fapihelper.hxx:260
Generic helper class for reading from and writing to property sets.
Definition: fapihelper.hxx:213
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:148
ScfPropertySet & operator=(ScfPropertySet const &)=default