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