cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / sfx2 / source / doc / docinf.cxx
blob8a4558760ed2500696a9f7b510f448027d9a8ad8
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 .
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"
39 // stream names
40 constexpr OUString STREAM_SUMMARYINFO = u"\005SummaryInformation"_ustr;
41 constexpr OUString STREAM_DOCSUMMARYINFO = u"\005DocumentSummaryInformation"_ustr;
43 // usings
44 using namespace ::com::sun::star;
47 namespace sfx2 {
49 ErrCode LoadOlePropertySet(
50 const uno::Reference< document::XDocumentProperties>& i_xDocProps,
51 SotStorage* i_pStorage )
53 // *** global properties from stream "005SummaryInformation" ***
55 // load the property set
56 SfxOlePropertySet aGlobSet;
57 ErrCode nGlobError = aGlobSet.LoadPropertySet(i_pStorage,
58 STREAM_SUMMARYINFO );
60 // global section
61 SfxOleSectionRef xGlobSect = aGlobSet.GetSection( SECTION_GLOBAL );
62 if( xGlobSect )
64 // set supported properties
65 OUString aStrValue;
66 util::DateTime aDateTime;
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) );
76 if( xGlobSect->GetStringValue( aStrValue, PROPID_TEMPLATE ) )
77 i_xDocProps->setTemplateName( aStrValue );
78 if( xGlobSect->GetStringValue( aStrValue, PROPID_COMMENTS ) )
79 i_xDocProps->setDescription( aStrValue );
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 );
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 );
100 i_xDocProps->setPrintedBy( OUString() );
101 if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_LASTPRINTED ) )
102 i_xDocProps->setPrintDate( aDateTime );
103 else
104 i_xDocProps->setPrintDate( aInvalid );
106 if( xGlobSect->GetStringValue( aStrValue, PROPID_REVNUMBER ) )
108 sal_Int16 nRevision = static_cast< sal_Int16 >( aStrValue.toInt32() );
109 if ( nRevision > 0 )
110 i_xDocProps->setEditingCycles( nRevision );
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) )
119 assert(aDateTime.Day <= 31);
120 // subtract offset 1601-01-01
121 aDateTime.Year -= 1601;
122 aDateTime.Month -= 1;
123 aDateTime.Day -= 1;
126 i_xDocProps->setEditingDuration(
127 aDateTime.Day * 60*60*24 +
128 aDateTime.Hours * 60*60 +
129 aDateTime.Minutes * 60 +
130 aDateTime.Seconds );
132 catch (const lang::IllegalArgumentException &)
134 // ignore
139 // *** custom properties from stream "005DocumentSummaryInformation" ***
141 // load the property set
142 SfxOlePropertySet aDocSet;
143 ErrCode nDocError = aDocSet.LoadPropertySet(i_pStorage,
144 STREAM_DOCSUMMARYINFO );
146 // custom properties
147 SfxOleSectionRef xCustomSect = aDocSet.GetSection( SECTION_CUSTOM );
148 if( xCustomSect )
150 uno::Reference < beans::XPropertyContainer > xUserDefined(
151 i_xDocProps->getUserDefinedProperties(), uno::UNO_SET_THROW);
152 ::std::vector< sal_Int32 > aPropIds;
153 xCustomSect->GetPropertyIds( aPropIds );
154 for( const auto& rPropId : aPropIds )
156 const OUString aPropName = xCustomSect->GetPropertyName( rPropId );
157 uno::Any aPropValue = xCustomSect->GetAnyValue( rPropId );
158 if( !aPropName.isEmpty() && aPropValue.hasValue() )
162 xUserDefined->addProperty( aPropName,
163 beans::PropertyAttribute::REMOVABLE, aPropValue );
165 catch (const uno::Exception&)
167 //ignore
173 uno::Reference< document::XCompatWriterDocProperties > xWriterProps( i_xDocProps, uno::UNO_QUERY );
174 if ( xWriterProps.is() )
176 SfxOleSectionRef xBuiltin = aDocSet.GetSection( SECTION_BUILTIN );
177 if ( xBuiltin )
181 OUString aStrValue;
182 if ( xBuiltin->GetStringValue( aStrValue, PROPID_MANAGER ) )
183 xWriterProps->setManager( aStrValue );
184 if ( xBuiltin->GetStringValue( aStrValue, PROPID_CATEGORY ) )
185 xWriterProps->setCategory( aStrValue );
186 if ( xBuiltin->GetStringValue( aStrValue, PROPID_COMPANY ) )
187 xWriterProps->setCompany( aStrValue );
189 catch (const uno::Exception&)
195 // return code
196 return (nGlobError != ERRCODE_NONE) ? nGlobError : nDocError;
199 bool SaveOlePropertySet(
200 const uno::Reference< document::XDocumentProperties>& i_xDocProps,
201 SotStorage* i_pStorage,
202 const uno::Sequence<sal_Int8> * i_pThumb,
203 const uno::Sequence<sal_Int8> * i_pGuid,
204 const uno::Sequence<sal_Int8> * i_pHyperlinks)
206 // *** global properties into stream "005SummaryInformation" ***
208 SfxOlePropertySet aGlobSet;
210 // set supported properties
211 SfxOleSection& rGlobSect = aGlobSet.AddSection( SECTION_GLOBAL );
212 rGlobSect.SetStringValue( PROPID_TITLE, i_xDocProps->getTitle() );
213 rGlobSect.SetStringValue( PROPID_SUBJECT, i_xDocProps->getSubject() );
214 const OUString aStr = ::comphelper::string::convertCommaSeparated(
215 i_xDocProps->getKeywords() );
216 rGlobSect.SetStringValue( PROPID_KEYWORDS, aStr );
217 rGlobSect.SetStringValue( PROPID_TEMPLATE, i_xDocProps->getTemplateName() );
218 rGlobSect.SetStringValue( PROPID_COMMENTS, i_xDocProps->getDescription() );
219 rGlobSect.SetStringValue( PROPID_AUTHOR, i_xDocProps->getAuthor() );
220 rGlobSect.SetFileTimeValue(PROPID_CREATED, i_xDocProps->getCreationDate());
221 rGlobSect.SetStringValue( PROPID_LASTAUTHOR, i_xDocProps->getModifiedBy() );
222 rGlobSect.SetFileTimeValue(PROPID_LASTSAVED,
223 i_xDocProps->getModificationDate() );
224 // note: apparently PrintedBy is not supported in file format
225 rGlobSect.SetFileTimeValue(PROPID_LASTPRINTED, i_xDocProps->getPrintDate());
227 sal_Int32 dur = i_xDocProps->getEditingDuration();
228 util::DateTime aEditTime;
229 // add offset 1601-01-01
230 aEditTime.Year = 1601;
231 aEditTime.Month = 1;
232 aEditTime.Day = 1;
233 aEditTime.Hours = static_cast<sal_Int16>(dur / 3600);
234 aEditTime.Minutes = static_cast<sal_Int16>((dur % 3600) / 60);
235 aEditTime.Seconds = static_cast<sal_Int16>(dur % 60);
236 rGlobSect.SetFileTimeValue( PROPID_EDITTIME, aEditTime );
238 rGlobSect.SetStringValue( PROPID_REVNUMBER,
239 OUString::number( i_xDocProps->getEditingCycles() ) );
240 if ( i_pThumb && i_pThumb->hasElements() )
241 rGlobSect.SetThumbnailValue( PROPID_THUMBNAIL, *i_pThumb );
243 // save the property set
244 ErrCode nGlobError = aGlobSet.SavePropertySet(i_pStorage,
245 STREAM_SUMMARYINFO);
247 // *** custom properties into stream "005DocumentSummaryInformation" ***
249 SfxOlePropertySet aDocSet;
251 // set builtin properties
252 aDocSet.AddSection( SECTION_BUILTIN );
254 // set custom properties
255 SfxOleSection& rCustomSect = aDocSet.AddSection( SECTION_CUSTOM );
257 // write GUID
258 if (i_pGuid) {
259 const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
260 rCustomSect.SetBlobValue( nPropId, *i_pGuid );
261 rCustomSect.SetPropertyName( nPropId,
262 u"_PID_GUID"_ustr );
265 // write hyperlinks
266 if (i_pHyperlinks) {
267 const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
268 rCustomSect.SetBlobValue( nPropId, *i_pHyperlinks );
269 rCustomSect.SetPropertyName( nPropId,
270 u"_PID_HLINKS"_ustr );
273 uno::Reference<beans::XPropertySet> xUserDefinedProps(
274 i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
275 uno::Reference<beans::XPropertySetInfo> xPropInfo =
276 xUserDefinedProps->getPropertySetInfo();
277 DBG_ASSERT(xPropInfo.is(), "UserDefinedProperties Info is null");
278 const uno::Sequence<beans::Property> props = xPropInfo->getProperties();
279 for (const auto& rProp : props)
283 // skip transient properties
284 if (~rProp.Attributes & beans::PropertyAttribute::TRANSIENT)
286 const OUString name = rProp.Name;
287 const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
288 if (rCustomSect.SetAnyValue( nPropId,
289 xUserDefinedProps->getPropertyValue(name))) {
290 rCustomSect.SetPropertyName( nPropId, name );
294 catch (const uno::Exception &)
296 // may happen with concurrent modification...
297 SAL_INFO("sfx", "SavePropertySet: exception");
301 // save the property set
302 ErrCode nDocError = aDocSet.SavePropertySet(i_pStorage,
303 STREAM_DOCSUMMARYINFO );
305 // return code
306 return (nGlobError == ERRCODE_NONE) && (nDocError == ERRCODE_NONE);
309 uno::Sequence<sal_Int8> convertMetaFile(GDIMetaFile const * i_pThumb)
311 if (i_pThumb) {
312 BitmapEx aBitmap;
313 SvMemoryStream aStream;
314 if (i_pThumb->CreateThumbnail(aBitmap))
316 WriteDIB(aBitmap.GetBitmap(), aStream, false, false);
317 return uno::Sequence<sal_Int8>(static_cast< const sal_Int8* >( aStream.GetData() ), aStream.TellEnd());
320 return uno::Sequence<sal_Int8>();
323 } // namespace sfx2
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */