Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / doc / oleprops.hxx
blob66b4e1c7d1aa815238490ed26e7e3a65fbb5a57f
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 #ifndef INCLUDED_SFX2_SOURCE_DOC_OLEPROPS_HXX
21 #define INCLUDED_SFX2_SOURCE_DOC_OLEPROPS_HXX
23 #include <map>
24 #include <memory>
25 #include <osl/thread.h>
26 #include <rtl/ustring.hxx>
27 #include <sot/storage.hxx>
29 #include <com/sun/star/util/DateTime.hpp>
30 #include <com/sun/star/util/Date.hpp>
33 //namespace {
36 // property type IDs
37 const sal_Int32 PROPTYPE_INT16 = 2;
38 const sal_Int32 PROPTYPE_INT32 = 3;
39 const sal_Int32 PROPTYPE_FLOAT = 4;
40 const sal_Int32 PROPTYPE_DOUBLE = 5;
41 const sal_Int32 PROPTYPE_DATE = 7;
42 const sal_Int32 PROPTYPE_STRING = 8;
43 const sal_Int32 PROPTYPE_STATUS = 10;
44 const sal_Int32 PROPTYPE_BOOL = 11;
45 const sal_Int32 PROPTYPE_VARIANT = 12;
46 const sal_Int32 PROPTYPE_INT8 = 16;
47 const sal_Int32 PROPTYPE_UINT8 = 17;
48 const sal_Int32 PROPTYPE_UINT16 = 18;
49 const sal_Int32 PROPTYPE_UINT32 = 19;
50 const sal_Int32 PROPTYPE_INT64 = 20;
51 const sal_Int32 PROPTYPE_UINT64 = 21;
52 const sal_Int32 PROPTYPE_STRING8 = 30;
53 const sal_Int32 PROPTYPE_STRING16 = 31;
54 const sal_Int32 PROPTYPE_FILETIME = 64;
55 const sal_Int32 PROPTYPE_BLOB = 65;
56 const sal_Int32 PROPTYPE_CLIPFMT = 71;
58 // static property IDs
59 const sal_Int32 PROPID_DICTIONARY = 0;
60 const sal_Int32 PROPID_CODEPAGE = 1;
61 const sal_Int32 PROPID_FIRSTCUSTOM = 2;
63 // property IDs for GlobalDocPropertySet
64 const sal_Int32 PROPID_TITLE = 2;
65 const sal_Int32 PROPID_SUBJECT = 3;
66 const sal_Int32 PROPID_AUTHOR = 4;
67 const sal_Int32 PROPID_KEYWORDS = 5;
68 const sal_Int32 PROPID_COMMENTS = 6;
69 const sal_Int32 PROPID_TEMPLATE = 7;
70 const sal_Int32 PROPID_LASTAUTHOR = 8;
71 const sal_Int32 PROPID_REVNUMBER = 9;
72 const sal_Int32 PROPID_EDITTIME = 10;
73 const sal_Int32 PROPID_LASTPRINTED = 11;
74 const sal_Int32 PROPID_CREATED = 12;
75 const sal_Int32 PROPID_LASTSAVED = 13;
76 const sal_Int32 PROPID_THUMBNAIL = 17;
78 // some Builtin properties
79 const sal_Int32 PROPID_CATEGORY = 0x2;
80 const sal_Int32 PROPID_COMPANY = 0xf;
81 const sal_Int32 PROPID_MANAGER = 0xe;
82 // predefined codepages
83 const sal_uInt16 CODEPAGE_UNKNOWN = 0;
84 const sal_uInt16 CODEPAGE_UNICODE = 1200;
85 const sal_uInt16 CODEPAGE_UTF8 = 65001;
87 // predefined clipboard format IDs
88 const sal_Int32 CLIPFMT_WIN = -1;
90 // predefined clipboard data format IDs
91 const sal_Int32 CLIPDATAFMT_DIB = 8;
94 /** Helper for classes that need text encoding settings.
96 Classes derived from this class will include functions to store and use
97 text encoding settings and to convert Windows codepage constants.
99 class SfxOleTextEncoding
101 public:
102 explicit SfxOleTextEncoding() :
103 mxTextEnc( std::make_shared<rtl_TextEncoding>( osl_getThreadTextEncoding() ) ) {}
104 explicit SfxOleTextEncoding( rtl_TextEncoding eTextEnc ) :
105 mxTextEnc( std::make_shared<rtl_TextEncoding>( eTextEnc ) ) {}
107 /** Returns the current text encoding identifier. */
108 rtl_TextEncoding GetTextEncoding() const { return *mxTextEnc; }
109 /** Sets the passed text encoding. */
110 void SetTextEncoding( rtl_TextEncoding eTextEnc ) { *mxTextEnc = eTextEnc; }
112 /** Returns true, if this object contains Unicode text encoding. */
113 bool IsUnicode() const { return GetTextEncoding() == RTL_TEXTENCODING_UCS2; }
114 /** Sets Unicode text encoding to this object. */
115 void SetUnicode() { SetTextEncoding( RTL_TEXTENCODING_UCS2 ); }
117 /** Converts the current settings to a Windows codepage identifier. */
118 sal_uInt16 GetCodePage() const;
119 /** Sets the current text encoding from a Windows codepage identifier. */
120 void SetCodePage( sal_uInt16 nCodePage );
122 private:
123 std::shared_ptr< rtl_TextEncoding > mxTextEnc;
127 /** Helper for classes that need to load or save string values.
129 Classes derived from this class contain functions to load and save string
130 values with the text encoding passed in the constructor.
132 class SfxOleStringHelper : public SfxOleTextEncoding
134 public:
135 /** Creates a string helper object depending on an external text encoding. */
136 explicit SfxOleStringHelper( const SfxOleTextEncoding& rTextEnc ) :
137 SfxOleTextEncoding( rTextEnc ) {}
138 /** Creates a string helper object with own text encoding. */
139 explicit SfxOleStringHelper( rtl_TextEncoding eTextEnc ) :
140 SfxOleTextEncoding( eTextEnc ) {}
142 /** Loads a string from the passed stream with current encoding (maybe Unicode). */
143 OUString LoadString8( SvStream& rStrm ) const;
144 /** Saves a string to the passed stream with current encoding (maybe Unicode). */
145 void SaveString8( SvStream& rStrm, const OUString& rValue ) const;
147 /** Loads a Unicode string from the passed stream, ignores own encoding. */
148 static OUString LoadString16( SvStream& rStrm );
149 /** Saves a Unicode string to the passed stream, ignores own encoding. */
150 static void SaveString16( SvStream& rStrm, const OUString& rValue );
152 private:
153 OUString ImplLoadString8( SvStream& rStrm ) const;
154 static OUString ImplLoadString16( SvStream& rStrm );
155 void ImplSaveString8( SvStream& rStrm, const OUString& rValue ) const;
156 static void ImplSaveString16( SvStream& rStrm, const OUString& rValue );
160 /** Base class for all classes related to OLE property sets.
162 Derived classes have to implement the pure virtual functions ImplLoad() and
163 ImplSave().
165 class SfxOleObjectBase
167 public:
168 explicit SfxOleObjectBase() : mnErrCode( ERRCODE_NONE ) {}
169 virtual ~SfxOleObjectBase();
171 /** Returns the current error code. */
172 ErrCode const & GetError() const { return mnErrCode; }
174 /** Loads this object from the passed stream. Calls virtual ImplLoad(). */
175 ErrCode const & Load( SvStream& rStrm );
176 /** Saves this object to the passed stream. Calls virtual ImplSave(). */
177 ErrCode const & Save( SvStream& rStrm );
179 protected:
180 /** Sets the passed error code. Will be returned by Load() and Save() functions.
181 Always the first error code is stored. Multiple calls have no effect. */
182 void SetError( ErrCode nErrCode ) { if( mnErrCode == ERRCODE_NONE ) mnErrCode = nErrCode; }
183 /** Loads the passed object from the stream. Sets returned error code as own error. */
184 void LoadObject( SvStream& rStrm, SfxOleObjectBase& rObj );
185 /** Saves the passed object to the stream. Sets returned error code as own error. */
186 void SaveObject( SvStream& rStrm, SfxOleObjectBase& rObj );
188 private:
189 /** Derived classes implement loading the object from the passed steam. */
190 virtual void ImplLoad( SvStream& rStrm ) = 0;
191 /** Derived classes implement saving the object to the passed steam. */
192 virtual void ImplSave( SvStream& rStrm ) = 0;
194 private:
195 ErrCode mnErrCode; /// Current error code.
199 /** Base class for all OLE property objects. */
200 class SfxOlePropertyBase : public SfxOleObjectBase
202 public:
203 explicit SfxOlePropertyBase( sal_Int32 nPropId, sal_Int32 nPropType ) :
204 mnPropId( nPropId ), mnPropType( nPropType ) {}
206 sal_Int32 GetPropId() const { return mnPropId; }
207 sal_Int32 GetPropType() const { return mnPropType; }
209 protected:
210 void SetPropType( sal_Int32 nPropType ) { mnPropType = nPropType; }
212 private:
213 sal_Int32 mnPropId;
214 sal_Int32 mnPropType;
217 typedef std::shared_ptr< SfxOlePropertyBase > SfxOlePropertyRef;
220 /** Property representing the codepage used to encode bytestrings in the entire property set. */
221 class SfxOleCodePageProperty : public SfxOlePropertyBase, public SfxOleTextEncoding
223 public:
224 explicit SfxOleCodePageProperty();
226 private:
227 virtual void ImplLoad( SvStream& rStrm ) override;
228 virtual void ImplSave( SvStream& rStrm ) override;
232 /** Property containing custom names for other properties in the property set. */
233 class SfxOleDictionaryProperty : public SfxOlePropertyBase, public SfxOleStringHelper
235 public:
236 explicit SfxOleDictionaryProperty( const SfxOleTextEncoding& rTextEnc );
238 /** Returns true, if the property contains at least one custom property name. */
239 bool HasPropertyNames() const { return !maPropNameMap.empty(); }
240 /** Prepares the property for loading. Does not affect contained names for its own. */
241 void SetNameCount( sal_Int32 nNameCount ) { SetPropType( nNameCount ); }
243 /** Returns the custom name for the passed property ID, or an empty string, if name not found. */
244 OUString GetPropertyName( sal_Int32 nPropId ) const;
245 /** Sets a custom name for the passed property ID. */
246 void SetPropertyName( sal_Int32 nPropId, const OUString& rPropName );
248 private:
249 virtual void ImplLoad( SvStream& rStrm ) override;
250 virtual void ImplSave( SvStream& rStrm ) override;
252 private:
253 typedef ::std::map< sal_Int32, OUString > SfxOlePropNameMap;
254 SfxOlePropNameMap maPropNameMap;
258 /** A section in a property set. Contains properties with unique identifiers. */
259 class SfxOleSection : public SfxOleObjectBase
261 private:
262 typedef ::std::map< sal_Int32, SfxOlePropertyRef > SfxOlePropMap;
264 public:
265 explicit SfxOleSection( bool bSupportsDict );
267 /** Returns the property with the passed ID, or an empty reference, if nothing found. */
268 SfxOlePropertyRef GetProperty( sal_Int32 nPropId ) const;
269 /** Returns the value of a signed int32 property with the passed ID in rnValue.
270 @return true = Property found, rnValue is valid; false = Property not found. */
271 bool GetInt32Value( sal_Int32& rnValue, sal_Int32 nPropId ) const;
272 /** Returns the value of a floating-point property with the passed ID in rfValue.
273 @return true = Property found, rfValue is valid; false = Property not found. */
274 bool GetDoubleValue( double& rfValue, sal_Int32 nPropId ) const;
275 /** Returns the value of a boolean property with the passed ID in rbValue.
276 @return true = Property found, rbValue is valid; false = Property not found. */
277 bool GetBoolValue( bool& rbValue, sal_Int32 nPropId ) const;
278 /** Returns the value of a string property with the passed ID in rValue.
279 @return true = Property found, rValue is valid; false = Property not found. */
280 bool GetStringValue( OUString& rValue, sal_Int32 nPropId ) const;
281 /** Returns the value of a time stamp property with the passed ID in rValue.
282 @return true = Property found, rValue is valid; false = Property not found. */
283 bool GetFileTimeValue( css::util::DateTime& rValue, sal_Int32 nPropId ) const;
284 /** Returns the value of a date property with the passed ID in rValue.
285 @return true = Property found, rValue is valid; false = Property not found. */
286 bool GetDateValue( css::util::Date& rValue, sal_Int32 nPropId ) const;
288 /** Adds the passed property to the property set. Drops an existing old property. */
289 void SetProperty( const SfxOlePropertyRef& xProp );
290 /** Inserts a signed int32 property with the passed value. */
291 void SetInt32Value( sal_Int32 nPropId, sal_Int32 nValue );
292 /** Inserts a floating-point property with the passed value. */
293 void SetDoubleValue( sal_Int32 nPropId, double fValue );
294 /** Inserts a boolean property with the passed value. */
295 void SetBoolValue( sal_Int32 nPropId, bool bValue );
296 /** Inserts a string property with the passed value.
297 @return true = Property inserted; false = String was empty, property not inserted. */
298 bool SetStringValue( sal_Int32 nPropId, const OUString& rValue );
299 /** Inserts a time stamp property with the passed value. */
300 void SetFileTimeValue( sal_Int32 nPropId, const css::util::DateTime& rValue );
301 /** Inserts a date property with the passed value. */
302 void SetDateValue( sal_Int32 nPropId, const css::util::Date& rValue );
303 /** Inserts a thumbnail property from the passed meta file. */
304 void SetThumbnailValue( sal_Int32 nPropId,
305 const css::uno::Sequence<sal_Int8> & i_rData);
306 /** Inserts a BLOB property with the passed data. */
307 void SetBlobValue( sal_Int32 nPropId,
308 const css::uno::Sequence<sal_Int8> & i_rData);
310 /** Returns the value of the property with the passed ID in a UNO any. */
311 css::uno::Any GetAnyValue( sal_Int32 nPropId ) const;
312 /** Inserts a property created from the passed any.
313 @return true = Property converted and inserted; false = Property type not supported. */
314 bool SetAnyValue( sal_Int32 nPropId, const css::uno::Any& rValue );
316 /** Returns the custom name for the passed property ID, or an empty string, if name not found. */
317 OUString GetPropertyName( sal_Int32 nPropId ) const;
318 /** Sets a custom name for the passed property ID. */
319 void SetPropertyName( sal_Int32 nPropId, const OUString& rPropName );
321 /** Returns the identifiers of all existing properties in the passed vector. */
322 void GetPropertyIds( ::std::vector< sal_Int32 >& rPropIds ) const;
323 /** Returns a property identifier not used in this section. */
324 sal_Int32 GetFreePropertyId() const;
326 private:
327 virtual void ImplLoad( SvStream& rStrm ) override;
328 virtual void ImplSave( SvStream& rStrm ) override;
330 bool SeekToPropertyPos( SvStream& rStrm, sal_uInt32 nPropPos ) const;
331 void LoadProperty( SvStream& rStrm, sal_Int32 nPropId );
332 void SaveProperty( SvStream& rStrm, SfxOlePropertyBase& rProp, sal_uInt64 & rnPropPosPos );
334 private:
335 SfxOlePropMap maPropMap; /// All properties in this section, by identifier.
336 SfxOleCodePageProperty maCodePageProp; /// The codepage property.
337 SfxOleDictionaryProperty maDictProp; /// The dictionary property.
338 sal_uInt64 mnStartPos; /// Start stream position of the section.
339 bool mbSupportsDict; /// true = section supports dictionary.
342 typedef std::shared_ptr< SfxOleSection > SfxOleSectionRef;
345 /** Enumerates different section types in OLE property sets. */
346 enum SfxOleSectionType
348 SECTION_GLOBAL, /// Globally defined properties.
349 SECTION_BUILTIN, /// Properties built into MS Office.
350 SECTION_CUSTOM /// Custom properties.
354 /** Represents a complete property set, may consist of several property sections. */
355 class SfxOlePropertySet : public SfxOleObjectBase
357 public:
358 explicit SfxOlePropertySet() {}
360 /** Loads this object from the passed storage. */
361 ErrCode const & LoadPropertySet( SotStorage* pStrg, const OUString& rStrmName );
362 /** Saves this object to the passed storage. */
363 ErrCode const & SavePropertySet( SotStorage* pStrg, const OUString& rStrmName );
365 /** Returns the specified section, or an empty reference, if nothing found. */
366 SfxOleSectionRef GetSection( SfxOleSectionType eSection ) const;
367 /** Returns the specified section, or an empty reference, if nothing found. */
368 SfxOleSectionRef GetSection( const SvGlobalName& rSectionGuid ) const;
370 /** Creates and returns the specified section, or just returns it if it already exists. */
371 SfxOleSection& AddSection( SfxOleSectionType eSection );
372 /** Creates and returns the specified section, or just returns it if it already exists. */
373 SfxOleSection& AddSection( const SvGlobalName& rSectionGuid );
375 private:
376 virtual void ImplLoad( SvStream& rStrm ) override;
377 virtual void ImplSave( SvStream& rStrm ) override;
379 /** Returns the GUID for the specified section. */
380 static const SvGlobalName& GetSectionGuid( SfxOleSectionType eSection );
382 private:
383 typedef ::std::map< SvGlobalName, SfxOleSectionRef > SfxOleSectionMap;
384 SfxOleSectionMap maSectionMap;
387 #endif
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */