LibreOffice Module sfx2 (master) 1
docinf.cxx
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
21#include <sfx2/docinf.hxx>
22#include <com/sun/star/beans/PropertyAttribute.hpp>
23#include <com/sun/star/beans/XPropertySet.hpp>
24#include <com/sun/star/beans/XPropertyContainer.hpp>
25#include <com/sun/star/document/XDocumentProperties.hpp>
26#include <com/sun/star/document/XCompatWriterDocProperties.hpp>
27#include <com/sun/star/uno/Exception.hpp>
28#include <rtl/ustring.hxx>
29#include <sal/log.hxx>
30#include <tools/debug.hxx>
31#include <comphelper/string.hxx>
32#include <sot/storage.hxx>
33#include <vcl/bitmapex.hxx>
34#include <vcl/gdimtf.hxx>
35#include <vcl/dibtools.hxx>
36#include "oleprops.hxx"
37
38
39// stream names
40constexpr OUStringLiteral STREAM_SUMMARYINFO = u"\005SummaryInformation";
41constexpr OUStringLiteral STREAM_DOCSUMMARYINFO = u"\005DocumentSummaryInformation";
42
43// usings
44using namespace ::com::sun::star;
45
46
47namespace sfx2 {
48
50 const uno::Reference< document::XDocumentProperties>& i_xDocProps,
51 SotStorage* i_pStorage )
52{
53 // *** global properties from stream "005SummaryInformation" ***
54
55 // load the property set
56 SfxOlePropertySet aGlobSet;
57 ErrCode nGlobError = aGlobSet.LoadPropertySet(i_pStorage,
59
60 // global section
61 SfxOleSectionRef xGlobSect = aGlobSet.GetSection( SECTION_GLOBAL );
62 if( xGlobSect )
63 {
64 // set supported properties
65 OUString aStrValue;
66 util::DateTime aDateTime;
67
68 if( xGlobSect->GetStringValue( aStrValue, PROPID_TITLE ) )
69 i_xDocProps->setTitle( aStrValue );
70 if( xGlobSect->GetStringValue( aStrValue, PROPID_SUBJECT ) )
71 i_xDocProps->setSubject( aStrValue );
72 if( xGlobSect->GetStringValue( aStrValue, PROPID_KEYWORDS ) ) {
73 i_xDocProps->setKeywords(
74 ::comphelper::string::convertCommaSeparated(aStrValue) );
75 }
76 if( xGlobSect->GetStringValue( aStrValue, PROPID_TEMPLATE ) )
77 i_xDocProps->setTemplateName( aStrValue );
78 if( xGlobSect->GetStringValue( aStrValue, PROPID_COMMENTS ) )
79 i_xDocProps->setDescription( aStrValue );
80
81 util::DateTime aInvalid;
82 if( xGlobSect->GetStringValue( aStrValue, PROPID_AUTHOR) )
83 i_xDocProps->setAuthor( aStrValue );
84 else
85 i_xDocProps->setAuthor( OUString() );
86 if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_CREATED ) )
87 i_xDocProps->setCreationDate( aDateTime );
88 else
89 i_xDocProps->setCreationDate( aInvalid );
90
91 if( xGlobSect->GetStringValue( aStrValue, PROPID_LASTAUTHOR) )
92 i_xDocProps->setModifiedBy( aStrValue );
93 else
94 i_xDocProps->setModifiedBy( OUString() );
95 if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_LASTSAVED ) )
96 i_xDocProps->setModificationDate( aDateTime );
97 else
98 i_xDocProps->setModificationDate( aInvalid );
99
100 i_xDocProps->setPrintedBy( OUString() );
101 if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_LASTPRINTED ) )
102 i_xDocProps->setPrintDate( aDateTime );
103 else
104 i_xDocProps->setPrintDate( aInvalid );
105
106 if( xGlobSect->GetStringValue( aStrValue, PROPID_REVNUMBER ) )
107 {
108 sal_Int16 nRevision = static_cast< sal_Int16 >( aStrValue.toInt32() );
109 if ( nRevision > 0 )
110 i_xDocProps->setEditingCycles( nRevision );
111 }
112
113 if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_EDITTIME )
114 && !(aDateTime.NanoSeconds == 0 && aDateTime.Seconds == 0
115 && aDateTime.Minutes == 0 && aDateTime.Hours == 0
116 && aDateTime.Day == 0 && aDateTime.Month == 0
117 && aDateTime.Year == 0) )
118 {
119 // subtract offset 1601-01-01
120 aDateTime.Year -= 1601;
121 aDateTime.Month -= 1;
122 aDateTime.Day -= 1;
123 try
124 {
125 i_xDocProps->setEditingDuration(
126 aDateTime.Day * 60*60*24 +
127 aDateTime.Hours * 60*60 +
128 aDateTime.Minutes * 60 +
129 aDateTime.Seconds );
130 }
131 catch (const lang::IllegalArgumentException &)
132 {
133 // ignore
134 }
135 }
136 }
137
138 // *** custom properties from stream "005DocumentSummaryInformation" ***
139
140 // load the property set
141 SfxOlePropertySet aDocSet;
142 ErrCode nDocError = aDocSet.LoadPropertySet(i_pStorage,
144
145 // custom properties
146 SfxOleSectionRef xCustomSect = aDocSet.GetSection( SECTION_CUSTOM );
147 if( xCustomSect )
148 {
149 uno::Reference < beans::XPropertyContainer > xUserDefined(
150 i_xDocProps->getUserDefinedProperties(), uno::UNO_SET_THROW);
151 ::std::vector< sal_Int32 > aPropIds;
152 xCustomSect->GetPropertyIds( aPropIds );
153 for( const auto& rPropId : aPropIds )
154 {
155 const OUString aPropName = xCustomSect->GetPropertyName( rPropId );
156 uno::Any aPropValue = xCustomSect->GetAnyValue( rPropId );
157 if( !aPropName.isEmpty() && aPropValue.hasValue() )
158 {
159 try
160 {
161 xUserDefined->addProperty( aPropName,
162 beans::PropertyAttribute::REMOVABLE, aPropValue );
163 }
164 catch (const uno::Exception&)
165 {
166 //ignore
167 }
168 }
169 }
170 }
171
172 uno::Reference< document::XCompatWriterDocProperties > xWriterProps( i_xDocProps, uno::UNO_QUERY );
173 if ( xWriterProps.is() )
174 {
175 SfxOleSectionRef xBuiltin = aDocSet.GetSection( SECTION_BUILTIN );
176 if ( xBuiltin )
177 {
178 try
179 {
180 OUString aStrValue;
181 if ( xBuiltin->GetStringValue( aStrValue, PROPID_MANAGER ) )
182 xWriterProps->setManager( aStrValue );
183 if ( xBuiltin->GetStringValue( aStrValue, PROPID_CATEGORY ) )
184 xWriterProps->setCategory( aStrValue );
185 if ( xBuiltin->GetStringValue( aStrValue, PROPID_COMPANY ) )
186 xWriterProps->setCompany( aStrValue );
187 }
188 catch (const uno::Exception&)
189 {
190 }
191 }
192 }
193
194 // return code
195 return (nGlobError != ERRCODE_NONE) ? nGlobError : nDocError;
196}
197
199 const uno::Reference< document::XDocumentProperties>& i_xDocProps,
200 SotStorage* i_pStorage,
201 const uno::Sequence<sal_Int8> * i_pThumb,
202 const uno::Sequence<sal_Int8> * i_pGuid,
203 const uno::Sequence<sal_Int8> * i_pHyperlinks)
204{
205 // *** global properties into stream "005SummaryInformation" ***
206
207 SfxOlePropertySet aGlobSet;
208
209 // set supported properties
210 SfxOleSection& rGlobSect = aGlobSet.AddSection( SECTION_GLOBAL );
211 rGlobSect.SetStringValue( PROPID_TITLE, i_xDocProps->getTitle() );
212 rGlobSect.SetStringValue( PROPID_SUBJECT, i_xDocProps->getSubject() );
213 const OUString aStr = ::comphelper::string::convertCommaSeparated(
214 i_xDocProps->getKeywords() );
215 rGlobSect.SetStringValue( PROPID_KEYWORDS, aStr );
216 rGlobSect.SetStringValue( PROPID_TEMPLATE, i_xDocProps->getTemplateName() );
217 rGlobSect.SetStringValue( PROPID_COMMENTS, i_xDocProps->getDescription() );
218 rGlobSect.SetStringValue( PROPID_AUTHOR, i_xDocProps->getAuthor() );
219 rGlobSect.SetFileTimeValue(PROPID_CREATED, i_xDocProps->getCreationDate());
220 rGlobSect.SetStringValue( PROPID_LASTAUTHOR, i_xDocProps->getModifiedBy() );
222 i_xDocProps->getModificationDate() );
223 // note: apparently PrintedBy is not supported in file format
224 rGlobSect.SetFileTimeValue(PROPID_LASTPRINTED, i_xDocProps->getPrintDate());
225
226 sal_Int32 dur = i_xDocProps->getEditingDuration();
227 util::DateTime aEditTime;
228 // add offset 1601-01-01
229 aEditTime.Year = 1601;
230 aEditTime.Month = 1;
231 aEditTime.Day = 1;
232 aEditTime.Hours = static_cast<sal_Int16>(dur / 3600);
233 aEditTime.Minutes = static_cast<sal_Int16>((dur % 3600) / 60);
234 aEditTime.Seconds = static_cast<sal_Int16>(dur % 60);
235 rGlobSect.SetFileTimeValue( PROPID_EDITTIME, aEditTime );
236
238 OUString::number( i_xDocProps->getEditingCycles() ) );
239 if ( i_pThumb && i_pThumb->hasElements() )
240 rGlobSect.SetThumbnailValue( PROPID_THUMBNAIL, *i_pThumb );
241
242 // save the property set
243 ErrCode nGlobError = aGlobSet.SavePropertySet(i_pStorage,
245
246 // *** custom properties into stream "005DocumentSummaryInformation" ***
247
248 SfxOlePropertySet aDocSet;
249
250 // set builtin properties
251 aDocSet.AddSection( SECTION_BUILTIN );
252
253 // set custom properties
254 SfxOleSection& rCustomSect = aDocSet.AddSection( SECTION_CUSTOM );
255
256 // write GUID
257 if (i_pGuid) {
258 const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
259 rCustomSect.SetBlobValue( nPropId, *i_pGuid );
260 rCustomSect.SetPropertyName( nPropId,
261 "_PID_GUID" );
262 }
263
264 // write hyperlinks
265 if (i_pHyperlinks) {
266 const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
267 rCustomSect.SetBlobValue( nPropId, *i_pHyperlinks );
268 rCustomSect.SetPropertyName( nPropId,
269 "_PID_HLINKS" );
270 }
271
272 uno::Reference<beans::XPropertySet> xUserDefinedProps(
273 i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
274 uno::Reference<beans::XPropertySetInfo> xPropInfo =
275 xUserDefinedProps->getPropertySetInfo();
276 DBG_ASSERT(xPropInfo.is(), "UserDefinedProperties Info is null");
277 const uno::Sequence<beans::Property> props = xPropInfo->getProperties();
278 for (const auto& rProp : props)
279 {
280 try
281 {
282 // skip transient properties
283 if (~rProp.Attributes & beans::PropertyAttribute::TRANSIENT)
284 {
285 const OUString name = rProp.Name;
286 const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
287 if (rCustomSect.SetAnyValue( nPropId,
288 xUserDefinedProps->getPropertyValue(name))) {
289 rCustomSect.SetPropertyName( nPropId, name );
290 }
291 }
292 }
293 catch (const uno::Exception &)
294 {
295 // may happen with concurrent modification...
296 SAL_INFO("sfx", "SavePropertySet: exception");
297 }
298 }
299
300 // save the property set
301 ErrCode nDocError = aDocSet.SavePropertySet(i_pStorage,
303
304 // return code
305 return (nGlobError == ERRCODE_NONE) && (nDocError == ERRCODE_NONE);
306}
307
308uno::Sequence<sal_Int8> convertMetaFile(GDIMetaFile const * i_pThumb)
309{
310 if (i_pThumb) {
311 BitmapEx aBitmap;
312 SvMemoryStream aStream;
313 if (i_pThumb->CreateThumbnail(aBitmap))
314 {
315 WriteDIB(aBitmap.GetBitmap(), aStream, false, false);
316 return uno::Sequence<sal_Int8>(static_cast< const sal_Int8* >( aStream.GetData() ), aStream.TellEnd());
317 }
318 }
319 return uno::Sequence<sal_Int8>();
320}
321
322} // namespace sfx2
323
324/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Bitmap GetBitmap(Color aTransparentReplaceColor) const
bool CreateThumbnail(BitmapEx &rBitmapEx, BmpConversion nColorConversion=BmpConversion::N24Bit, BmpScaleFlag nScaleFlag=BmpScaleFlag::BestQuality) const
Represents a complete property set, may consist of several property sections.
Definition: oleprops.hxx:358
SfxOleSection & AddSection(SfxOleSectionType eSection)
Creates and returns the specified section, or just returns it if it already exists.
Definition: oleprops.cxx:1137
ErrCode const & LoadPropertySet(SotStorage *pStrg, const OUString &rStrmName)
Loads this object from the passed storage.
Definition: oleprops.cxx:1090
ErrCode const & SavePropertySet(SotStorage *pStrg, const OUString &rStrmName)
Saves this object to the passed storage.
Definition: oleprops.cxx:1108
SfxOleSectionRef GetSection(SfxOleSectionType eSection) const
Returns the specified section, or an empty reference, if nothing found.
Definition: oleprops.cxx:1123
A section in a property set.
Definition: oleprops.hxx:262
void SetPropertyName(sal_Int32 nPropId, const OUString &rPropName)
Sets a custom name for the passed property ID.
Definition: oleprops.cxx:922
void SetFileTimeValue(sal_Int32 nPropId, const css::util::DateTime &rValue)
Inserts a time stamp property with the passed value.
Definition: oleprops.cxx:824
sal_Int32 GetFreePropertyId() const
Returns a property identifier not used in this section.
Definition: oleprops.cxx:934
void SetThumbnailValue(sal_Int32 nPropId, const css::uno::Sequence< sal_Int8 > &i_rData)
Inserts a thumbnail property from the passed meta file.
Definition: oleprops.cxx:846
bool SetStringValue(sal_Int32 nPropId, const OUString &rValue)
Inserts a string property with the passed value.
Definition: oleprops.cxx:816
void SetBlobValue(sal_Int32 nPropId, const css::uno::Sequence< sal_Int8 > &i_rData)
Inserts a BLOB property with the passed data.
Definition: oleprops.cxx:854
bool SetAnyValue(sal_Int32 nPropId, const css::uno::Any &rValue)
Inserts a property created from the passed any.
Definition: oleprops.cxx:891
const void * GetData()
virtual sal_uInt64 TellEnd() override
#define DBG_ASSERT(sCon, aError)
bool VCL_DLLPUBLIC WriteDIB(const Bitmap &rSource, SvStream &rOStm, bool bCompressed, bool bFileHeader)
constexpr OUStringLiteral STREAM_DOCSUMMARYINFO
Definition: docinf.cxx:41
constexpr OUStringLiteral STREAM_SUMMARYINFO
Definition: docinf.cxx:40
float u
#define ERRCODE_NONE
const char * name
#define SAL_INFO(area, stream)
aStr
Definition: mgetempl.cxx:407
OUString aPropName
dictionary props
ErrCode LoadOlePropertySet(const uno::Reference< document::XDocumentProperties > &i_xDocProps, SotStorage *i_pStorage)
Definition: docinf.cxx:49
uno::Sequence< sal_Int8 > convertMetaFile(GDIMetaFile const *i_pThumb)
Definition: docinf.cxx:308
bool SaveOlePropertySet(const uno::Reference< document::XDocumentProperties > &i_xDocProps, SotStorage *i_pStorage, const uno::Sequence< sal_Int8 > *i_pThumb, const uno::Sequence< sal_Int8 > *i_pGuid, const uno::Sequence< sal_Int8 > *i_pHyperlinks)
Definition: docinf.cxx:198
@ SECTION_BUILTIN
Globally defined properties.
Definition: oleprops.hxx:351
@ SECTION_GLOBAL
Definition: oleprops.hxx:350
@ SECTION_CUSTOM
Properties built into MS Office.
Definition: oleprops.hxx:352
const sal_Int32 PROPID_THUMBNAIL
Definition: oleprops.hxx:78
const sal_Int32 PROPID_AUTHOR
Definition: oleprops.hxx:68
const sal_Int32 PROPID_MANAGER
Definition: oleprops.hxx:83
const sal_Int32 PROPID_EDITTIME
Definition: oleprops.hxx:74
const sal_Int32 PROPID_CATEGORY
Definition: oleprops.hxx:81
const sal_Int32 PROPID_REVNUMBER
Definition: oleprops.hxx:73
const sal_Int32 PROPID_COMPANY
Definition: oleprops.hxx:82
const sal_Int32 PROPID_COMMENTS
Definition: oleprops.hxx:70
const sal_Int32 PROPID_TEMPLATE
Definition: oleprops.hxx:71
const sal_Int32 PROPID_SUBJECT
Definition: oleprops.hxx:67
std::shared_ptr< SfxOleSection > SfxOleSectionRef
Definition: oleprops.hxx:344
const sal_Int32 PROPID_KEYWORDS
Definition: oleprops.hxx:69
const sal_Int32 PROPID_CREATED
Definition: oleprops.hxx:76
const sal_Int32 PROPID_LASTSAVED
Definition: oleprops.hxx:77
const sal_Int32 PROPID_TITLE
Definition: oleprops.hxx:66
const sal_Int32 PROPID_LASTPRINTED
Definition: oleprops.hxx:75
const sal_Int32 PROPID_LASTAUTHOR
Definition: oleprops.hxx:72
bool hasValue()
signed char sal_Int8