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