Branch data 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 : :
21 : : #include <sfx2/docinf.hxx>
22 : :
23 : : #include <com/sun/star/beans/PropertyAttribute.hpp>
24 : : #include <com/sun/star/beans/XPropertySet.hpp>
25 : : #include <com/sun/star/beans/XPropertyContainer.hpp>
26 : : #include <com/sun/star/document/XDocumentProperties.hpp>
27 : : #include <com/sun/star/document/XCompatWriterDocProperties.hpp>
28 : : #include <com/sun/star/uno/Exception.hpp>
29 : :
30 : : #include <rtl/ustring.hxx>
31 : : #include <tools/debug.hxx>
32 : : #include <comphelper/string.hxx>
33 : : #include <sot/storage.hxx>
34 : : #include <vcl/gdimtf.hxx>
35 : :
36 : : #include "oleprops.hxx"
37 : : // ============================================================================
38 : :
39 : : // stream names
40 : : #define STREAM_SUMMARYINFO "\005SummaryInformation"
41 : : #define STREAM_DOCSUMMARYINFO "\005DocumentSummaryInformation"
42 : :
43 : : // usings
44 : : using namespace ::com::sun::star;
45 : :
46 : :
47 : : namespace sfx2 {
48 : :
49 : 169 : sal_uInt32 SFX2_DLLPUBLIC LoadOlePropertySet(
50 : : uno::Reference< document::XDocumentProperties> i_xDocProps,
51 : : SotStorage* i_pStorage )
52 : : {
53 : : // *** global properties from stream "005SummaryInformation" ***
54 : :
55 : : // load the property set
56 [ + - ]: 169 : SfxOlePropertySet aGlobSet;
57 : : ErrCode nGlobError = aGlobSet.LoadPropertySet(i_pStorage,
58 [ + - ][ + - ]: 169 : String( STREAM_SUMMARYINFO ) );
[ + - ]
59 : :
60 : : // global section
61 [ + - ]: 169 : SfxOleSectionRef xGlobSect = aGlobSet.GetSection( SECTION_GLOBAL );
62 [ + + ]: 169 : if( xGlobSect.get() )
63 : : {
64 : : // set supported properties
65 [ + - ]: 151 : String aStrValue;
66 : 151 : util::DateTime aDateTime;
67 : :
68 [ + + ][ + - ]: 151 : if( xGlobSect->GetStringValue( aStrValue, PROPID_TITLE ) )
69 [ + - ][ + - ]: 72 : i_xDocProps->setTitle( aStrValue );
[ + - ]
70 [ + - ][ + + ]: 151 : if( xGlobSect->GetStringValue( aStrValue, PROPID_SUBJECT ) )
71 [ + - ][ + - ]: 66 : i_xDocProps->setSubject( aStrValue );
[ + - ]
72 [ + - ][ + + ]: 151 : if( xGlobSect->GetStringValue( aStrValue, PROPID_KEYWORDS ) ) {
73 [ + - ]: 66 : i_xDocProps->setKeywords(
74 [ + - ][ + - ]: 66 : ::comphelper::string::convertCommaSeparated(aStrValue) );
[ + - ][ + - ]
75 : : }
76 [ + - ][ + + ]: 151 : if( xGlobSect->GetStringValue( aStrValue, PROPID_TEMPLATE ) )
77 [ + - ][ + - ]: 81 : i_xDocProps->setTemplateName( aStrValue );
[ + - ]
78 [ + - ][ + + ]: 151 : if( xGlobSect->GetStringValue( aStrValue, PROPID_COMMENTS ) )
79 [ + - ][ + - ]: 51 : i_xDocProps->setDescription( aStrValue );
[ + - ]
80 : :
81 : 151 : util::DateTime aInvalid;
82 [ + + ][ + - ]: 151 : if( xGlobSect->GetStringValue( aStrValue, PROPID_AUTHOR) )
83 [ + - ][ + - ]: 130 : i_xDocProps->setAuthor( aStrValue );
[ + - ]
84 : : else
85 [ + - ][ + - ]: 21 : i_xDocProps->setAuthor( ::rtl::OUString() );
86 [ + - ][ + + ]: 151 : if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_CREATED ) )
87 [ + - ][ + - ]: 142 : i_xDocProps->setCreationDate( aDateTime );
88 : : else
89 [ + - ][ + - ]: 9 : i_xDocProps->setCreationDate( aInvalid );
90 : :
91 [ + - ][ + + ]: 151 : if( xGlobSect->GetStringValue( aStrValue, PROPID_LASTAUTHOR) )
92 [ + - ][ + - ]: 127 : i_xDocProps->setModifiedBy( aStrValue );
[ + - ]
93 : : else
94 [ + - ][ + - ]: 24 : i_xDocProps->setModifiedBy( ::rtl::OUString() );
95 [ + - ][ + + ]: 151 : if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_LASTSAVED ) )
96 [ + - ][ + - ]: 139 : i_xDocProps->setModificationDate( aDateTime );
97 : : else
98 [ + - ][ + - ]: 12 : i_xDocProps->setModificationDate( aInvalid );
99 : :
100 [ + - ][ + - ]: 151 : i_xDocProps->setPrintedBy( ::rtl::OUString() );
[ # # ]
101 [ + + ][ + - ]: 151 : if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_LASTPRINTED ) )
102 [ + - ][ + - ]: 63 : i_xDocProps->setPrintDate( aDateTime );
103 : : else
104 [ + - ][ + - ]: 88 : i_xDocProps->setPrintDate( aInvalid );
105 : :
106 [ + - ][ + + ]: 151 : if( xGlobSect->GetStringValue( aStrValue, PROPID_REVNUMBER ) )
107 : : {
108 [ + - ]: 117 : sal_Int16 nRevision = static_cast< sal_Int16 >( aStrValue.ToInt32() );
109 [ + + ]: 117 : if ( nRevision > 0 )
110 [ + - ][ + - ]: 108 : i_xDocProps->setEditingCycles( nRevision );
111 : : }
112 : :
113 [ + - ][ + + ]: 151 : if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_EDITTIME ) )
114 : : {
115 : : // subtract offset 1601-01-01
116 : 114 : aDateTime.Year -= 1601;
117 : 114 : aDateTime.Month -= 1;
118 : 114 : aDateTime.Day -= 1;
119 : : try
120 : : {
121 [ + - ]: 114 : i_xDocProps->setEditingDuration(
122 : : aDateTime.Day * 60*60*24 +
123 : : aDateTime.Hours * 60*60 +
124 : : aDateTime.Minutes * 60 +
125 [ + - ]: 114 : aDateTime.Seconds );
126 : : }
127 [ # # ]: 0 : catch (const lang::IllegalArgumentException &)
128 : : {
129 : : // ignore
130 : : }
131 [ + - ]: 151 : }
132 : : }
133 : :
134 : : // *** custom properties from stream "005DocumentSummaryInformation" ***
135 : :
136 : : // load the property set
137 [ + - ]: 169 : SfxOlePropertySet aDocSet;
138 : : ErrCode nDocError = aDocSet.LoadPropertySet(i_pStorage,
139 [ + - ][ + - ]: 169 : String( STREAM_DOCSUMMARYINFO ) );
[ + - ]
140 : :
141 : : // custom properties
142 [ + - ]: 169 : SfxOleSectionRef xCustomSect = aDocSet.GetSection( SECTION_CUSTOM );
143 [ + + ]: 169 : if( xCustomSect.get() )
144 : : {
145 : : uno::Reference < beans::XPropertyContainer > xUserDefined(
146 [ + - ][ + - ]: 72 : i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
[ + - ]
147 [ + - ]: 72 : ::std::vector< sal_Int32 > aPropIds;
148 [ + - ]: 72 : xCustomSect->GetPropertyIds( aPropIds );
149 [ + - ][ # # ]: 144 : for( ::std::vector< sal_Int32 >::const_iterator aIt = aPropIds.begin(),
[ + - ][ - + ]
150 [ + - # # ]: 72 : aEnd = aPropIds.end(); aIt != aEnd; ++aIt )
151 : : {
152 [ # # ][ # # ]: 0 : ::rtl::OUString aPropName = xCustomSect->GetPropertyName( *aIt );
[ # # ]
153 [ # # ][ # # ]: 0 : uno::Any aPropValue = xCustomSect->GetAnyValue( *aIt );
154 [ # # ][ # # ]: 0 : if( !aPropName.isEmpty() && aPropValue.hasValue() )
[ # # ]
155 : : {
156 : : try
157 : : {
158 [ # # ]: 0 : xUserDefined->addProperty( aPropName,
159 [ # # ]: 0 : beans::PropertyAttribute::REMOVEABLE, aPropValue );
160 : : }
161 [ # # ]: 0 : catch (const uno::Exception&)
162 : : {
163 : : //ignore
164 : : }
165 : : }
166 : 72 : }
167 : : }
168 : :
169 [ + - ]: 169 : uno::Reference< document::XCompatWriterDocProperties > xWriterProps( i_xDocProps, uno::UNO_QUERY );
170 [ - + ]: 169 : if ( xWriterProps.is() )
171 : : {
172 [ # # ]: 0 : SfxOleSectionRef xBuiltin = aDocSet.GetSection( SECTION_BUILTIN );
173 [ # # ]: 0 : if ( xBuiltin.get() )
174 : : {
175 : : try
176 : : {
177 [ # # ]: 0 : String aStrValue;
178 [ # # ][ # # ]: 0 : if ( xBuiltin->GetStringValue( aStrValue, PROPID_MANAGER ) )
179 [ # # ][ # # ]: 0 : xWriterProps->setManager( aStrValue );
[ # # ]
180 [ # # ][ # # ]: 0 : if ( xBuiltin->GetStringValue( aStrValue, PROPID_CATEGORY ) )
181 [ # # ][ # # ]: 0 : xWriterProps->setCategory( aStrValue );
[ # # ]
182 [ # # ][ # # ]: 0 : if ( xBuiltin->GetStringValue( aStrValue, PROPID_COMPANY ) )
183 [ # # ][ # # ]: 0 : xWriterProps->setCompany( aStrValue );
[ # # ][ # # ]
[ # # ]
184 : : }
185 [ # # ]: 0 : catch (const uno::Exception&)
186 : : {
187 : : }
188 [ # # ]: 0 : }
189 : : }
190 : :
191 : : // return code
192 [ + + ][ + - ]: 169 : return (nGlobError != ERRCODE_NONE) ? nGlobError : nDocError;
[ + - ][ + - ]
[ + - ]
193 : : }
194 : :
195 : 9 : bool SFX2_DLLPUBLIC SaveOlePropertySet(
196 : : uno::Reference< document::XDocumentProperties> i_xDocProps,
197 : : SotStorage* i_pStorage,
198 : : const uno::Sequence<sal_uInt8> * i_pThumb,
199 : : const uno::Sequence<sal_uInt8> * i_pGuid,
200 : : const uno::Sequence<sal_uInt8> * i_pHyperlinks)
201 : : {
202 : : // *** global properties into stream "005SummaryInformation" ***
203 : :
204 [ + - ]: 9 : SfxOlePropertySet aGlobSet;
205 : :
206 : : // set supported properties
207 [ + - ]: 9 : SfxOleSection& rGlobSect = aGlobSet.AddSection( SECTION_GLOBAL );
208 [ + - ][ + - ]: 9 : rGlobSect.SetStringValue( PROPID_TITLE, i_xDocProps->getTitle() );
[ + - ][ + - ]
[ + - ]
209 [ + - ][ + - ]: 9 : rGlobSect.SetStringValue( PROPID_SUBJECT, i_xDocProps->getSubject() );
[ + - ][ + - ]
[ + - ]
210 : : String aStr = ::comphelper::string::convertCommaSeparated(
211 [ + - ][ + - ]: 9 : i_xDocProps->getKeywords() );
[ + - ][ + - ]
[ + - ]
212 [ + - ]: 9 : rGlobSect.SetStringValue( PROPID_KEYWORDS, aStr );
213 [ + - ][ + - ]: 9 : rGlobSect.SetStringValue( PROPID_TEMPLATE, i_xDocProps->getTemplateName() );
[ + - ][ + - ]
[ + - ]
214 [ + - ][ + - ]: 9 : rGlobSect.SetStringValue( PROPID_COMMENTS, i_xDocProps->getDescription() );
[ + - ][ + - ]
[ + - ]
215 [ + - ][ + - ]: 9 : rGlobSect.SetStringValue( PROPID_AUTHOR, i_xDocProps->getAuthor() );
[ + - ][ + - ]
[ + - ]
216 [ + - ][ + - ]: 9 : rGlobSect.SetFileTimeValue(PROPID_CREATED, i_xDocProps->getCreationDate());
[ + - ]
217 [ + - ][ + - ]: 9 : rGlobSect.SetStringValue( PROPID_LASTAUTHOR, i_xDocProps->getModifiedBy() );
[ + - ][ + - ]
[ + - ]
218 : : rGlobSect.SetFileTimeValue(PROPID_LASTSAVED,
219 [ + - ][ + - ]: 9 : i_xDocProps->getModificationDate() );
[ + - ]
220 : : // note: apparently PrintedBy is not supported in file format
221 [ + - ][ + - ]: 9 : rGlobSect.SetFileTimeValue(PROPID_LASTPRINTED, i_xDocProps->getPrintDate());
[ + - ]
222 : :
223 [ + - ][ + - ]: 9 : sal_Int32 dur = i_xDocProps->getEditingDuration();
224 : 9 : util::DateTime aEditTime;
225 : : // add offset 1601-01-01
226 : 9 : aEditTime.Year = 1601;
227 : 9 : aEditTime.Month = 1;
228 : 9 : aEditTime.Day = 1;
229 : 9 : aEditTime.Hours = static_cast<sal_Int16>(dur / 3600);
230 : 9 : aEditTime.Minutes = static_cast<sal_Int16>((dur % 3600) / 60);
231 : 9 : aEditTime.Seconds = static_cast<sal_Int16>(dur % 60);
232 [ + - ]: 9 : rGlobSect.SetFileTimeValue( PROPID_EDITTIME, aEditTime );
233 : :
234 : : rGlobSect.SetStringValue( PROPID_REVNUMBER,
235 [ + - ][ + - ]: 9 : rtl::OUString::valueOf( static_cast<sal_Int32>(i_xDocProps->getEditingCycles()) ) );
[ + - ][ + - ]
[ + - ]
236 [ # # ][ - + ]: 9 : if ( i_pThumb && i_pThumb->getLength() )
[ - + ]
237 [ # # ]: 0 : rGlobSect.SetThumbnailValue( PROPID_THUMBNAIL, *i_pThumb );
238 : :
239 : : // save the property set
240 : : ErrCode nGlobError = aGlobSet.SavePropertySet(i_pStorage,
241 [ + - ][ + - ]: 9 : ::rtl::OUString(STREAM_SUMMARYINFO));
[ + - ]
242 : :
243 : : // *** custom properties into stream "005DocumentSummaryInformation" ***
244 : :
245 [ + - ]: 9 : SfxOlePropertySet aDocSet;
246 : :
247 : : // set builtin properties
248 [ + - ]: 9 : aDocSet.AddSection( SECTION_BUILTIN );
249 : :
250 : : // set custom properties
251 [ + - ]: 9 : SfxOleSection& rCustomSect = aDocSet.AddSection( SECTION_CUSTOM );
252 : :
253 : : // write GUID
254 [ - + ]: 9 : if (i_pGuid) {
255 [ # # ]: 0 : const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
256 [ # # ]: 0 : rCustomSect.SetBlobValue( nPropId, *i_pGuid );
257 : : rCustomSect.SetPropertyName( nPropId,
258 [ # # ][ # # ]: 0 : ::rtl::OUString("_PID_GUID") );
[ # # ]
259 : : }
260 : :
261 : : // write hyperlinks
262 [ - + ]: 9 : if (i_pHyperlinks) {
263 [ # # ]: 0 : const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
264 [ # # ]: 0 : rCustomSect.SetBlobValue( nPropId, *i_pHyperlinks );
265 : : rCustomSect.SetPropertyName( nPropId,
266 [ # # ][ # # ]: 0 : ::rtl::OUString("_PID_HLINKS") );
[ # # ]
267 : : }
268 : :
269 : : uno::Reference<beans::XPropertySet> xUserDefinedProps(
270 [ + - ][ + - ]: 9 : i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
[ + - ]
271 : : DBG_ASSERT(xUserDefinedProps.is(), "UserDefinedProperties is null");
272 : : uno::Reference<beans::XPropertySetInfo> xPropInfo =
273 [ + - ][ + - ]: 9 : xUserDefinedProps->getPropertySetInfo();
274 : : DBG_ASSERT(xPropInfo.is(), "UserDefinedProperties Info is null");
275 [ + - ][ + - ]: 9 : uno::Sequence<beans::Property> props = xPropInfo->getProperties();
276 [ + + ]: 33 : for (sal_Int32 i = 0; i < props.getLength(); ++i)
277 : : {
278 : : try
279 : : {
280 : : // skip transient properties
281 [ + - ][ + - ]: 24 : if (~props[i].Attributes & beans::PropertyAttribute::TRANSIENT)
282 : : {
283 [ + - ]: 24 : const ::rtl::OUString name = props[i].Name;
284 [ + - ]: 24 : const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
285 [ + - - + ]: 48 : if (rCustomSect.SetAnyValue( nPropId,
286 [ + - ][ + - ]: 48 : xUserDefinedProps->getPropertyValue(name))) {
287 [ # # ][ # # ]: 0 : rCustomSect.SetPropertyName( nPropId, name );
[ # # ]
288 [ # # ]: 24 : }
289 : : }
290 : : }
291 [ # # ]: 0 : catch (const uno::Exception &)
292 : : {
293 : : // may happen with concurrent modification...
294 : : DBG_WARNING("SavePropertySet: exception");
295 : : }
296 : : }
297 : :
298 : : // save the property set
299 : : ErrCode nDocError = aDocSet.SavePropertySet(i_pStorage,
300 [ + - ][ + - ]: 9 : String( STREAM_DOCSUMMARYINFO ) );
[ + - ]
301 : :
302 : : // return code
303 [ + - ][ + - ]: 9 : return (nGlobError == ERRCODE_NONE) && (nDocError == ERRCODE_NONE);
[ + - ][ + - ]
[ + - ][ + - ]
304 : : }
305 : :
306 : 0 : uno::Sequence<sal_uInt8> SFX2_DLLPUBLIC convertMetaFile(GDIMetaFile* i_pThumb)
307 : : {
308 [ # # ]: 0 : if (i_pThumb) {
309 [ # # ]: 0 : BitmapEx aBitmap;
310 [ # # ]: 0 : SvMemoryStream aStream;
311 : : // magic value 160 taken from GraphicHelper::getThumbnailFormatFromGDI_Impl()
312 [ # # ][ # # ]: 0 : if( i_pThumb->CreateThumbnail( 160, aBitmap ) ) {
313 [ # # ][ # # ]: 0 : aBitmap.GetBitmap().Write( aStream, sal_False, sal_False );
[ # # ]
314 [ # # ]: 0 : aStream.Seek(STREAM_SEEK_TO_END);
315 [ # # ]: 0 : uno::Sequence<sal_uInt8> aSeq(aStream.Tell());
316 : : const sal_uInt8* pBlob(
317 [ # # ]: 0 : static_cast<const sal_uInt8*>(aStream.GetData()));
318 [ # # ]: 0 : for (sal_Int32 j = 0; j < aSeq.getLength(); ++j) {
319 [ # # ]: 0 : aSeq[j] = pBlob[j];
320 : : }
321 [ # # ][ # # ]: 0 : return aSeq;
322 [ # # ][ # # ]: 0 : }
[ # # ][ # # ]
323 : : }
324 : 0 : return uno::Sequence<sal_uInt8>();
325 : : }
326 : :
327 : : } // namespace sfx2
328 : :
329 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|