xref: /aoo42x/main/sfx2/source/doc/docinf.cxx (revision d119d52d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sfx2.hxx"
26 
27 #include <sfx2/docinf.hxx>
28 
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/beans/XPropertyContainer.hpp>
32 #include <com/sun/star/document/XDocumentProperties.hpp>
33 #include <com/sun/star/uno/Exception.hpp>
34 
35 #include <rtl/ustring.hxx>
36 #include <tools/debug.hxx>
37 #include <comphelper/string.hxx>
38 #include <sot/storage.hxx>
39 #include <vcl/gdimtf.hxx>
40 
41 #include "oleprops.hxx"
42 
43 // ============================================================================
44 
45 // stream names
46 #define STREAM_SUMMARYINFO      "\005SummaryInformation"
47 #define STREAM_DOCSUMMARYINFO   "\005DocumentSummaryInformation"
48 
49 // usings
50 using namespace ::com::sun::star;
51 
52 
53 namespace sfx2 {
54 
55 sal_uInt32 SFX2_DLLPUBLIC LoadOlePropertySet(
56     uno::Reference< document::XDocumentProperties> i_xDocProps,
57     SotStorage* i_pStorage )
58 {
59     // *** global properties from stream "005SummaryInformation" ***
60 
61     // load the property set
62     SfxOlePropertySet aGlobSet;
63     ErrCode nGlobError = aGlobSet.LoadPropertySet(i_pStorage,
64         String( RTL_CONSTASCII_USTRINGPARAM( STREAM_SUMMARYINFO ) ) );
65 
66     // global section
67     SfxOleSectionRef xGlobSect = aGlobSet.GetSection( SECTION_GLOBAL );
68     if( xGlobSect.get() )
69     {
70         // set supported properties
71         String aStrValue;
72         util::DateTime aDateTime;
73 
74         if( xGlobSect->GetStringValue( aStrValue, PROPID_TITLE ) )
75             i_xDocProps->setTitle( aStrValue );
76         if( xGlobSect->GetStringValue( aStrValue, PROPID_SUBJECT ) )
77             i_xDocProps->setSubject( aStrValue );
78         if( xGlobSect->GetStringValue( aStrValue, PROPID_KEYWORDS ) ) {
79             i_xDocProps->setKeywords(
80                 ::comphelper::string::convertCommaSeparated(aStrValue) );
81         }
82         if( xGlobSect->GetStringValue( aStrValue, PROPID_TEMPLATE ) )
83             i_xDocProps->setTemplateName( aStrValue );
84         if( xGlobSect->GetStringValue( aStrValue, PROPID_COMMENTS ) )
85             i_xDocProps->setDescription( aStrValue );
86 
87 		util::DateTime aInvalid;
88         if( xGlobSect->GetStringValue( aStrValue, PROPID_AUTHOR) )
89 			i_xDocProps->setAuthor( aStrValue );
90 		else
91 			i_xDocProps->setAuthor( ::rtl::OUString() );
92         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_CREATED ) )
93 			i_xDocProps->setCreationDate( aDateTime );
94 		else
95 			i_xDocProps->setCreationDate( aInvalid );
96 
97         if( xGlobSect->GetStringValue( aStrValue, PROPID_LASTAUTHOR) )
98 			i_xDocProps->setModifiedBy( aStrValue );
99 		else
100 			i_xDocProps->setModifiedBy( ::rtl::OUString() );
101         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_LASTSAVED ) )
102 			i_xDocProps->setModificationDate( aDateTime );
103 		else
104 			i_xDocProps->setModificationDate( aInvalid );
105 
106 		i_xDocProps->setPrintedBy( ::rtl::OUString() );
107         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_LASTPRINTED ) )
108 			i_xDocProps->setPrintDate( aDateTime );
109 		else
110 			i_xDocProps->setPrintDate( aInvalid );
111 
112         if( xGlobSect->GetStringValue( aStrValue, PROPID_REVNUMBER ) )
113         {
114             sal_Int16 nRevision = static_cast< sal_Int16 >( aStrValue.ToInt32() );
115             if ( nRevision > 0 )
116                 i_xDocProps->setEditingCycles( nRevision );
117         }
118 
119         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_EDITTIME ) )
120         {
121             // subtract offset 1601-01-01
122             aDateTime.Year  -= 1601;
123             aDateTime.Month -= 1;
124             aDateTime.Day   -= 1;
125             try
126             {
127                 i_xDocProps->setEditingDuration(
128                     aDateTime.Day     * 60*60*24 +
129                     aDateTime.Hours   * 60*60    +
130                     aDateTime.Minutes * 60       +
131                     aDateTime.Seconds            );
132             }
133             catch (lang::IllegalArgumentException &)
134             {
135                 // ignore
136             }
137         }
138     }
139 
140     // *** custom properties from stream "005DocumentSummaryInformation" ***
141 
142     // load the property set
143     SfxOlePropertySet aDocSet;
144     ErrCode nDocError = aDocSet.LoadPropertySet(i_pStorage,
145         String( RTL_CONSTASCII_USTRINGPARAM( STREAM_DOCSUMMARYINFO ) ) );
146 
147     // custom properties
148     SfxOleSectionRef xCustomSect = aDocSet.GetSection( SECTION_CUSTOM );
149     if( xCustomSect.get() )
150     {
151         uno::Reference < beans::XPropertyContainer > xUserDefined(
152             i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
153         ::std::vector< sal_Int32 > aPropIds;
154         xCustomSect->GetPropertyIds( aPropIds );
155         for( ::std::vector< sal_Int32 >::const_iterator aIt = aPropIds.begin(),
156              aEnd = aPropIds.end(); aIt != aEnd; ++aIt )
157         {
158             ::rtl::OUString aPropName = xCustomSect->GetPropertyName( *aIt );
159             uno::Any aPropValue = xCustomSect->GetAnyValue( *aIt );
160             if( (aPropName.getLength() > 0) && aPropValue.hasValue() ) {
161                 try {
162                     xUserDefined->addProperty( aPropName,
163                         beans::PropertyAttribute::REMOVEABLE, aPropValue );
164                 } catch ( uno::Exception& ) {
165                     //ignore
166                 }
167             }
168         }
169     }
170 
171     // return code
172     return (nGlobError != ERRCODE_NONE) ? nGlobError : nDocError;
173 }
174 
175 bool SFX2_DLLPUBLIC SaveOlePropertySet(
176     uno::Reference< document::XDocumentProperties> i_xDocProps,
177     SotStorage* i_pStorage,
178     const uno::Sequence<sal_uInt8> * i_pThumb,
179     const uno::Sequence<sal_uInt8> * i_pGuid,
180     const uno::Sequence<sal_uInt8> * i_pHyperlinks)
181 {
182     // *** global properties into stream "005SummaryInformation" ***
183 
184     SfxOlePropertySet aGlobSet;
185 
186     // set supported properties
187     SfxOleSection& rGlobSect = aGlobSet.AddSection( SECTION_GLOBAL );
188     rGlobSect.SetStringValue( PROPID_TITLE,    i_xDocProps->getTitle() );
189     rGlobSect.SetStringValue( PROPID_SUBJECT,  i_xDocProps->getSubject() );
190     String aStr = ::comphelper::string::convertCommaSeparated(
191         i_xDocProps->getKeywords() );
192     rGlobSect.SetStringValue( PROPID_KEYWORDS, aStr );
193     rGlobSect.SetStringValue( PROPID_TEMPLATE, i_xDocProps->getTemplateName() );
194     rGlobSect.SetStringValue( PROPID_COMMENTS, i_xDocProps->getDescription() );
195     rGlobSect.SetStringValue( PROPID_AUTHOR,   i_xDocProps->getAuthor() );
196     rGlobSect.SetFileTimeValue(PROPID_CREATED, i_xDocProps->getCreationDate());
197     rGlobSect.SetStringValue( PROPID_LASTAUTHOR, i_xDocProps->getModifiedBy() );
198     rGlobSect.SetFileTimeValue(PROPID_LASTSAVED,
199                                 i_xDocProps->getModificationDate() );
200     // note: apparently PrintedBy is not supported in file format
201     rGlobSect.SetFileTimeValue(PROPID_LASTPRINTED, i_xDocProps->getPrintDate());
202 
203     sal_Int32 dur = i_xDocProps->getEditingDuration();
204     util::DateTime aEditTime;
205     // add offset 1601-01-01
206     aEditTime.Year    = 1601;
207     aEditTime.Month   = 1;
208     aEditTime.Day     = 1;
209     aEditTime.Hours   = static_cast<sal_Int16>(dur / 3600);
210     aEditTime.Minutes = static_cast<sal_Int16>((dur % 3600) / 60);
211     aEditTime.Seconds = static_cast<sal_Int16>(dur % 60);
212     rGlobSect.SetFileTimeValue( PROPID_EDITTIME, aEditTime );
213 
214     rGlobSect.SetStringValue( PROPID_REVNUMBER,
215                 String::CreateFromInt32( i_xDocProps->getEditingCycles() ) );
216     if ( i_pThumb && i_pThumb->getLength() )
217 		rGlobSect.SetThumbnailValue( PROPID_THUMBNAIL, *i_pThumb );
218 
219     // save the property set
220     ErrCode nGlobError = aGlobSet.SavePropertySet(i_pStorage,
221         String( RTL_CONSTASCII_USTRINGPARAM( STREAM_SUMMARYINFO ) ) );
222 
223     // *** custom properties into stream "005DocumentSummaryInformation" ***
224 
225     SfxOlePropertySet aDocSet;
226 
227     // set builtin properties
228     aDocSet.AddSection( SECTION_BUILTIN );
229 
230     // set custom properties
231     SfxOleSection& rCustomSect = aDocSet.AddSection( SECTION_CUSTOM );
232 
233     // write GUID
234     if (i_pGuid) {
235         const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
236         rCustomSect.SetBlobValue( nPropId, *i_pGuid );
237         rCustomSect.SetPropertyName( nPropId,
238             ::rtl::OUString::createFromAscii("_PID_GUID") );
239     }
240 
241     // write hyperlinks
242     if (i_pHyperlinks) {
243         const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
244         rCustomSect.SetBlobValue( nPropId, *i_pHyperlinks );
245         rCustomSect.SetPropertyName( nPropId,
246             ::rtl::OUString::createFromAscii("_PID_HLINKS") );
247     }
248 
249     uno::Reference<beans::XPropertySet> xUserDefinedProps(
250         i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
251     DBG_ASSERT(xUserDefinedProps.is(), "UserDefinedProperties is null");
252     uno::Reference<beans::XPropertySetInfo> xPropInfo =
253         xUserDefinedProps->getPropertySetInfo();
254     DBG_ASSERT(xPropInfo.is(), "UserDefinedProperties Info is null");
255     uno::Sequence<beans::Property> props = xPropInfo->getProperties();
256     for (sal_Int32 i = 0; i < props.getLength(); ++i) {
257         try {
258             // skip transient properties
259             if (~props[i].Attributes & beans::PropertyAttribute::TRANSIENT)
260             {
261                 const ::rtl::OUString name = props[i].Name;
262                 const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
263                 if (rCustomSect.SetAnyValue( nPropId,
264                             xUserDefinedProps->getPropertyValue(name))) {
265                     rCustomSect.SetPropertyName( nPropId, name );
266                 }
267             }
268         } catch (uno::Exception &) {
269             // may happen with concurrent modification...
270             DBG_WARNING("SavePropertySet: exception");
271         }
272     }
273 
274     // save the property set
275     ErrCode nDocError = aDocSet.SavePropertySet(i_pStorage,
276         String( RTL_CONSTASCII_USTRINGPARAM( STREAM_DOCSUMMARYINFO ) ) );
277 
278     // return code
279     return (nGlobError == ERRCODE_NONE) && (nDocError == ERRCODE_NONE);
280 }
281 
282 uno::Sequence<sal_uInt8> SFX2_DLLPUBLIC convertMetaFile(GDIMetaFile* i_pThumb)
283 {
284     if (i_pThumb) {
285         BitmapEx aBitmap;
286         SvMemoryStream aStream;
287 // magic value 160 taken from GraphicHelper::getThumbnailFormatFromGDI_Impl()
288         if( i_pThumb->CreateThumbnail( 160, aBitmap ) ) {
289             aBitmap.GetBitmap().Write( aStream, sal_False, sal_False );
290 //            uno::Sequence<sal_uInt8> aSeq(aStream.GetSize()); // WRONG
291             aStream.Seek(STREAM_SEEK_TO_END);
292             uno::Sequence<sal_uInt8> aSeq(aStream.Tell());
293             const sal_uInt8* pBlob(
294                 static_cast<const sal_uInt8*>(aStream.GetData()));
295             for (sal_Int32 j = 0; j < aSeq.getLength(); ++j) {
296                 aSeq[j] = pBlob[j];
297             }
298             return aSeq;
299         }
300     }
301     return uno::Sequence<sal_uInt8>();
302 }
303 
304 } // namespace sfx2
305 
306