LCOV - code coverage report
Current view: top level - sfx2/source/doc - docinf.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 122 150 81.3 %
Date: 2015-06-13 12:38:46 Functions: 2 3 66.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11