LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / doc / oleprops.hxx
blob81eb744eab89ead7bb7a84c2f989510e36063467
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 <string_view>
27 #include <osl/thread.h>
28 #include <rtl/ustring.hxx>
29 #include <sot/storage.hxx>
31 #include <com/sun/star/util/DateTime.hpp>
32 #include <com/sun/star/util/Date.hpp>
35 //namespace {
38 // property type IDs
39 const sal_Int32 PROPTYPE_INT16 = 2;
40 const sal_Int32 PROPTYPE_INT32 = 3;
41 const sal_Int32 PROPTYPE_FLOAT = 4;
42 const sal_Int32 PROPTYPE_DOUBLE = 5;
43 const sal_Int32 PROPTYPE_DATE = 7;
44 const sal_Int32 PROPTYPE_STRING = 8;
45 const sal_Int32 PROPTYPE_STATUS = 10;
46 const sal_Int32 PROPTYPE_BOOL = 11;
47 const sal_Int32 PROPTYPE_VARIANT = 12;
48 const sal_Int32 PROPTYPE_INT8 = 16;
49 const sal_Int32 PROPTYPE_UINT8 = 17;
50 const sal_Int32 PROPTYPE_UINT16 = 18;
51 const sal_Int32 PROPTYPE_UINT32 = 19;
52 const sal_Int32 PROPTYPE_INT64 = 20;
53 const sal_Int32 PROPTYPE_UINT64 = 21;
54 const sal_Int32 PROPTYPE_STRING8 = 30;
55 const sal_Int32 PROPTYPE_STRING16 = 31;
56 const sal_Int32 PROPTYPE_FILETIME = 64;
57 const sal_Int32 PROPTYPE_BLOB = 65;
58 const sal_Int32 PROPTYPE_CLIPFMT = 71;
60 // static property IDs
61 const sal_Int32 PROPID_DICTIONARY = 0;
62 const sal_Int32 PROPID_CODEPAGE = 1;
63 const sal_Int32 PROPID_FIRSTCUSTOM = 2;
65 // property IDs for GlobalDocPropertySet
66 const sal_Int32 PROPID_TITLE = 2;
67 const sal_Int32 PROPID_SUBJECT = 3;
68 const sal_Int32 PROPID_AUTHOR = 4;
69 const sal_Int32 PROPID_KEYWORDS = 5;
70 const sal_Int32 PROPID_COMMENTS = 6;
71 const sal_Int32 PROPID_TEMPLATE = 7;
72 const sal_Int32 PROPID_LASTAUTHOR = 8;
73 const sal_Int32 PROPID_REVNUMBER = 9;
74 const sal_Int32 PROPID_EDITTIME = 10;
75 const sal_Int32 PROPID_LASTPRINTED = 11;
76 const sal_Int32 PROPID_CREATED = 12;
77 const sal_Int32 PROPID_LASTSAVED = 13;
78 const sal_Int32 PROPID_THUMBNAIL = 17;
80 // some Builtin properties
81 const sal_Int32 PROPID_CATEGORY = 0x2;
82 const sal_Int32 PROPID_COMPANY = 0xf;
83 const sal_Int32 PROPID_MANAGER = 0xe;
84 // predefined codepages
85 const sal_uInt16 CODEPAGE_UNKNOWN = 0;
86 const sal_uInt16 CODEPAGE_UNICODE = 1200;
87 const sal_uInt16 CODEPAGE_UTF8 = 65001;
89 // predefined clipboard format IDs
90 const sal_Int32 CLIPFMT_WIN = -1;
92 // predefined clipboard data format IDs
93 const sal_Int32 CLIPDATAFMT_DIB = 8;
96 /** Helper for classes that need text encoding settings.
98 Classes derived from this class will include functions to store and use
99 text encoding settings and to convert Windows codepage constants.
101 class SfxOleTextEncoding
103 public:
104 explicit SfxOleTextEncoding() :
105 mxTextEnc( std::make_shared<rtl_TextEncoding>( osl_getThreadTextEncoding() ) ) {}
106 explicit SfxOleTextEncoding( rtl_TextEncoding eTextEnc ) :
107 mxTextEnc( std::make_shared<rtl_TextEncoding>( eTextEnc ) ) {}
109 /** Returns the current text encoding identifier. */
110 rtl_TextEncoding GetTextEncoding() const { return *mxTextEnc; }
111 /** Sets the passed text encoding. */
112 void SetTextEncoding( rtl_TextEncoding eTextEnc ) { *mxTextEnc = eTextEnc; }
114 /** Returns true, if this object contains Unicode text encoding. */
115 bool IsUnicode() const { return GetTextEncoding() == RTL_TEXTENCODING_UCS2; }
116 /** Sets Unicode text encoding to this object. */
117 void SetUnicode() { SetTextEncoding( RTL_TEXTENCODING_UCS2 ); }
119 /** Converts the current settings to a Windows codepage identifier. */
120 sal_uInt16 GetCodePage() const;
121 /** Sets the current text encoding from a Windows codepage identifier. */
122 void SetCodePage( sal_uInt16 nCodePage );
124 private:
125 std::shared_ptr< rtl_TextEncoding > mxTextEnc;
129 /** Helper for classes that need to load or save string values.
131 Classes derived from this class contain functions to load and save string
132 values with the text encoding passed in the constructor.
134 class SfxOleStringHelper : public SfxOleTextEncoding
136 public:
137 /** Creates a string helper object depending on an external text encoding. */
138 explicit SfxOleStringHelper( const SfxOleTextEncoding& rTextEnc ) :
139 SfxOleTextEncoding( rTextEnc ) {}
140 /** Creates a string helper object with own text encoding. */
141 explicit SfxOleStringHelper( rtl_TextEncoding eTextEnc ) :
142 SfxOleTextEncoding( eTextEnc ) {}
144 /** Loads a string from the passed stream with current encoding (maybe Unicode). */
145 OUString LoadString8( SvStream& rStrm ) const;
146 /** Saves a string to the passed stream with current encoding (maybe Unicode). */
147 void SaveString8( SvStream& rStrm, const OUString& rValue ) const;
149 /** Loads a Unicode string from the passed stream, ignores own encoding. */
150 static OUString LoadString16( SvStream& rStrm );
151 /** Saves a Unicode string to the passed stream, ignores own encoding. */
152 static void SaveString16( SvStream& rStrm, const OUString& rValue );
154 private:
155 OUString ImplLoadString8( SvStream& rStrm ) const;
156 static OUString ImplLoadString16( SvStream& rStrm );
157 void ImplSaveString8( SvStream& rStrm, std::u16string_view rValue ) const;
158 static void ImplSaveString16( SvStream& rStrm, const OUString& rValue );
162 /** Base class for all classes related to OLE property sets.
164 Derived classes have to implement the pure virtual functions ImplLoad() and
165 ImplSave().
167 class SfxOleObjectBase
169 public:
170 explicit SfxOleObjectBase() : mnErrCode( ERRCODE_NONE ) {}
171 virtual ~SfxOleObjectBase();
173 /** Returns the current error code. */
174 ErrCode const & GetError() const { return mnErrCode; }
176 /** Loads this object from the passed stream. Calls virtual ImplLoad(). */
177 ErrCode const & Load( SvStream& rStrm );
178 /** Saves this object to the passed stream. Calls virtual ImplSave(). */
179 ErrCode const & Save( SvStream& rStrm );
181 protected:
182 /** Sets the passed error code. Will be returned by Load() and Save() functions.
183 Always the first error code is stored. Multiple calls have no effect. */
184 void SetError( ErrCode nErrCode ) { if( mnErrCode == ERRCODE_NONE ) mnErrCode = nErrCode; }
185 /** Loads the passed object from the stream. Sets returned error code as own error. */
186 void LoadObject( SvStream& rStrm, SfxOleObjectBase& rObj );
187 /** Saves the passed object to the stream. Sets returned error code as own error. */
188 void SaveObject( SvStream& rStrm, SfxOleObjectBase& rObj );
190 private:
191 /** Derived classes implement loading the object from the passed steam. */
192 virtual void ImplLoad( SvStream& rStrm ) = 0;
193 /** Derived classes implement saving the object to the passed steam. */
194 virtual void ImplSave( SvStream& rStrm ) = 0;
196 private:
197 ErrCode mnErrCode; /// Current error code.
201 /** Base class for all OLE property objects. */
202 class SfxOlePropertyBase : public SfxOleObjectBase
204 public:
205 explicit SfxOlePropertyBase( sal_Int32 nPropId, sal_Int32 nPropType ) :
206 mnPropId( nPropId ), mnPropType( nPropType ) {}
208 sal_Int32 GetPropId() const { return mnPropId; }
209 sal_Int32 GetPropType() const { return mnPropType; }
211 protected:
212 void SetPropType( sal_Int32 nPropType ) { mnPropType = nPropType; }
214 private:
215 sal_Int32 mnPropId;
216 sal_Int32 mnPropType;
219 typedef std::shared_ptr< SfxOlePropertyBase > SfxOlePropertyRef;
222 /** Property representing the codepage used to encode bytestrings in the entire property set. */
223 class SfxOleCodePageProperty : public SfxOlePropertyBase, public SfxOleTextEncoding
225 public:
226 explicit SfxOleCodePageProperty();
228 private:
229 virtual void ImplLoad( SvStream& rStrm ) override;
230 virtual void ImplSave( SvStream& rStrm ) override;
234 /** Property containing custom names for other properties in the property set. */
235 class SfxOleDictionaryProperty : public SfxOlePropertyBase, public SfxOleStringHelper
237 public:
238 explicit SfxOleDictionaryProperty( const SfxOleTextEncoding& rTextEnc );
240 /** Returns true, if the property contains at least one custom property name. */
241 bool HasPropertyNames() const { return !maPropNameMap.empty(); }
242 /** Prepares the property for loading. Does not affect contained names for its own. */
243 void SetNameCount( sal_Int32 nNameCount ) { SetPropType( nNameCount ); }
245 /** Returns the custom name for the passed property ID, or an empty string, if name not found. */
246 OUString GetPropertyName( sal_Int32 nPropId ) const;
247 /** Sets a custom name for the passed property ID. */
248 void SetPropertyName( sal_Int32 nPropId, const OUString& rPropName );
250 private:
251 virtual void ImplLoad( SvStream& rStrm ) override;
252 virtual void ImplSave( SvStream& rStrm ) override;
254 private:
255 typedef ::std::map< sal_Int32, OUString > SfxOlePropNameMap;
256 SfxOlePropNameMap maPropNameMap;
260 /** A section in a property set. Contains properties with unique identifiers. */
261 class SfxOleSection : public SfxOleObjectBase
263 private:
264 typedef ::std::map< sal_Int32, SfxOlePropertyRef > SfxOlePropMap;
266 public:
267 explicit SfxOleSection( bool bSupportsDict );
269 /** Returns the property with the passed ID, or an empty reference, if nothing found. */
270 SfxOlePropertyRef GetProperty( sal_Int32 nPropId ) const;
271 /** Returns the value of a signed int32 property with the passed ID in rnValue.
272 @return true = Property found, rnValue is valid; false = Property not found. */
273 bool GetInt32Value( sal_Int32& rnValue, sal_Int32 nPropId ) const;
274 /** Returns the value of a floating-point property with the passed ID in rfValue.
275 @return true = Property found, rfValue is valid; false = Property not found. */
276 bool GetDoubleValue( double& rfValue, sal_Int32 nPropId ) const;
277 /** Returns the value of a boolean property with the passed ID in rbValue.
278 @return true = Property found, rbValue is valid; false = Property not found. */
279 bool GetBoolValue( bool& rbValue, sal_Int32 nPropId ) const;
280 /** Returns the value of a string property with the passed ID in rValue.
281 @return true = Property found, rValue is valid; false = Property not found. */
282 bool GetStringValue( OUString& rValue, sal_Int32 nPropId ) const;
283 /** Returns the value of a time stamp property with the passed ID in rValue.
284 @return true = Property found, rValue is valid; false = Property not found. */
285 bool GetFileTimeValue( css::util::DateTime& rValue, sal_Int32 nPropId ) const;
286 /** Returns the value of a date property with the passed ID in rValue.
287 @return true = Property found, rValue is valid; false = Property not found. */
288 bool GetDateValue( css::util::Date& rValue, sal_Int32 nPropId ) const;
290 /** Adds the passed property to the property set. Drops an existing old property. */
291 void SetProperty( const SfxOlePropertyRef& xProp );
292 /** Inserts a signed int32 property with the passed value. */
293 void SetInt32Value( sal_Int32 nPropId, sal_Int32 nValue );
294 /** Inserts a floating-point property with the passed value. */
295 void SetDoubleValue( sal_Int32 nPropId, double fValue );
296 /** Inserts a boolean property with the passed value. */
297 void SetBoolValue( sal_Int32 nPropId, bool bValue );
298 /** Inserts a string property with the passed value.
299 @return true = Property inserted; false = String was empty, property not inserted. */
300 bool SetStringValue( sal_Int32 nPropId, const OUString& rValue );
301 /** Inserts a time stamp property with the passed value. */
302 void SetFileTimeValue( sal_Int32 nPropId, const css::util::DateTime& rValue );
303 /** Inserts a date property with the passed value. */
304 void SetDateValue( sal_Int32 nPropId, const css::util::Date& rValue );
305 /** Inserts a thumbnail property from the passed meta file. */
306 void SetThumbnailValue( sal_Int32 nPropId,
307 const css::uno::Sequence<sal_Int8> & i_rData);
308 /** Inserts a BLOB property with the passed data. */
309 void SetBlobValue( sal_Int32 nPropId,
310 const css::uno::Sequence<sal_Int8> & i_rData);
312 /** Returns the value of the property with the passed ID in a UNO any. */
313 css::uno::Any GetAnyValue( sal_Int32 nPropId ) const;
314 /** Inserts a property created from the passed any.
315 @return true = Property converted and inserted; false = Property type not supported. */
316 bool SetAnyValue( sal_Int32 nPropId, const css::uno::Any& rValue );
318 /** Returns the custom name for the passed property ID, or an empty string, if name not found. */
319 OUString GetPropertyName( sal_Int32 nPropId ) const;
320 /** Sets a custom name for the passed property ID. */
321 void SetPropertyName( sal_Int32 nPropId, const OUString& rPropName );
323 /** Returns the identifiers of all existing properties in the passed vector. */
324 void GetPropertyIds( ::std::vector< sal_Int32 >& rPropIds ) const;
325 /** Returns a property identifier not used in this section. */
326 sal_Int32 GetFreePropertyId() const;
328 private:
329 virtual void ImplLoad( SvStream& rStrm ) override;
330 virtual void ImplSave( SvStream& rStrm ) override;
332 bool SeekToPropertyPos( SvStream& rStrm, sal_uInt32 nPropPos ) const;
333 void LoadProperty( SvStream& rStrm, sal_Int32 nPropId );
334 void SaveProperty( SvStream& rStrm, SfxOlePropertyBase& rProp, sal_uInt64 & rnPropPosPos );
336 private:
337 SfxOlePropMap maPropMap; /// All properties in this section, by identifier.
338 SfxOleCodePageProperty maCodePageProp; /// The codepage property.
339 SfxOleDictionaryProperty maDictProp; /// The dictionary property.
340 sal_uInt64 mnStartPos; /// Start stream position of the section.
341 bool mbSupportsDict; /// true = section supports dictionary.
344 typedef std::shared_ptr< SfxOleSection > SfxOleSectionRef;
347 /** Enumerates different section types in OLE property sets. */
348 enum SfxOleSectionType
350 SECTION_GLOBAL, /// Globally defined properties.
351 SECTION_BUILTIN, /// Properties built into MS Office.
352 SECTION_CUSTOM /// Custom properties.
356 /** Represents a complete property set, may consist of several property sections. */
357 class SfxOlePropertySet : public SfxOleObjectBase
359 public:
360 explicit SfxOlePropertySet() {}
362 /** Loads this object from the passed storage. */
363 ErrCode const & LoadPropertySet( SotStorage* pStrg, const OUString& rStrmName );
364 /** Saves this object to the passed storage. */
365 ErrCode const & SavePropertySet( SotStorage* pStrg, const OUString& rStrmName );
367 /** Returns the specified section, or an empty reference, if nothing found. */
368 SfxOleSectionRef GetSection( SfxOleSectionType eSection ) const;
369 /** Returns the specified section, or an empty reference, if nothing found. */
370 SfxOleSectionRef GetSection( const SvGlobalName& rSectionGuid ) const;
372 /** Creates and returns the specified section, or just returns it if it already exists. */
373 SfxOleSection& AddSection( SfxOleSectionType eSection );
374 /** Creates and returns the specified section, or just returns it if it already exists. */
375 SfxOleSection& AddSection( const SvGlobalName& rSectionGuid );
377 private:
378 virtual void ImplLoad( SvStream& rStrm ) override;
379 virtual void ImplSave( SvStream& rStrm ) override;
381 /** Returns the GUID for the specified section. */
382 static const SvGlobalName& GetSectionGuid( SfxOleSectionType eSection );
384 private:
385 typedef ::std::map< SvGlobalName, SfxOleSectionRef > SfxOleSectionMap;
386 SfxOleSectionMap maSectionMap;
389 #endif
391 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */