LCOV - code coverage report
Current view: top level - sfx2/source/doc - docinf.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 116 151 76.8 %
Date: 2014-11-03 Functions: 4 5 80.0 %
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         342 : 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         342 :     SfxOlePropertySet aGlobSet;
      55             :     ErrCode nGlobError = aGlobSet.LoadPropertySet(i_pStorage,
      56         342 :         OUString( STREAM_SUMMARYINFO  ) );
      57             : 
      58             :     // global section
      59         684 :     SfxOleSectionRef xGlobSect = aGlobSet.GetSection( SECTION_GLOBAL );
      60         342 :     if( xGlobSect.get() )
      61             :     {
      62             :         // set supported properties
      63         330 :         OUString aStrValue;
      64         330 :         util::DateTime aDateTime;
      65             : 
      66         330 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_TITLE ) )
      67         170 :             i_xDocProps->setTitle( aStrValue );
      68         330 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_SUBJECT ) )
      69         160 :             i_xDocProps->setSubject( aStrValue );
      70         330 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_KEYWORDS ) ) {
      71         160 :             i_xDocProps->setKeywords(
      72         160 :                 ::comphelper::string::convertCommaSeparated(aStrValue) );
      73             :         }
      74         330 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_TEMPLATE ) )
      75         196 :             i_xDocProps->setTemplateName( aStrValue );
      76         330 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_COMMENTS ) )
      77         130 :             i_xDocProps->setDescription( aStrValue );
      78             : 
      79         330 :         util::DateTime aInvalid;
      80         330 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_AUTHOR) )
      81         306 :             i_xDocProps->setAuthor( aStrValue );
      82             :         else
      83          24 :             i_xDocProps->setAuthor( OUString() );
      84         330 :         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_CREATED ) )
      85         324 :             i_xDocProps->setCreationDate( aDateTime );
      86             :         else
      87           6 :             i_xDocProps->setCreationDate( aInvalid );
      88             : 
      89         330 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_LASTAUTHOR) )
      90         304 :             i_xDocProps->setModifiedBy( aStrValue );
      91             :         else
      92          26 :             i_xDocProps->setModifiedBy( OUString() );
      93         330 :         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_LASTSAVED ) )
      94         322 :             i_xDocProps->setModificationDate( aDateTime );
      95             :         else
      96           8 :             i_xDocProps->setModificationDate( aInvalid );
      97             : 
      98         330 :         i_xDocProps->setPrintedBy( OUString() );
      99         330 :         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_LASTPRINTED ) )
     100         136 :             i_xDocProps->setPrintDate( aDateTime );
     101             :         else
     102         194 :             i_xDocProps->setPrintDate( aInvalid );
     103             : 
     104         330 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_REVNUMBER ) )
     105             :         {
     106         230 :             sal_Int16 nRevision = static_cast< sal_Int16 >( aStrValue.toInt32() );
     107         230 :             if ( nRevision > 0 )
     108         196 :                 i_xDocProps->setEditingCycles( nRevision );
     109             :         }
     110             : 
     111         660 :         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_EDITTIME )
     112         470 :             && !(aDateTime.NanoSeconds == 0 && aDateTime.Seconds == 0
     113         168 :                  && aDateTime.Minutes == 0 && aDateTime.Hours == 0
     114          88 :                  && aDateTime.Day == 0 && aDateTime.Month == 0
     115          88 :                  && aDateTime.Year == 0) )
     116             :         {
     117             :             // subtract offset 1601-01-01
     118         140 :             aDateTime.Year  -= 1601;
     119         140 :             aDateTime.Month -= 1;
     120         140 :             aDateTime.Day   -= 1;
     121             :             try
     122             :             {
     123         140 :                 i_xDocProps->setEditingDuration(
     124         280 :                     aDateTime.Day     * 60*60*24 +
     125         280 :                     aDateTime.Hours   * 60*60    +
     126         140 :                     aDateTime.Minutes * 60       +
     127         280 :                     aDateTime.Seconds            );
     128             :             }
     129           0 :             catch (const lang::IllegalArgumentException &)
     130             :             {
     131             :                 // ignore
     132             :             }
     133         330 :         }
     134             :     }
     135             : 
     136             :     // *** custom properties from stream "005DocumentSummaryInformation" ***
     137             : 
     138             :     // load the property set
     139         684 :     SfxOlePropertySet aDocSet;
     140             :     ErrCode nDocError = aDocSet.LoadPropertySet(i_pStorage,
     141         342 :         OUString( STREAM_DOCSUMMARYINFO  ) );
     142             : 
     143             :     // custom properties
     144         684 :     SfxOleSectionRef xCustomSect = aDocSet.GetSection( SECTION_CUSTOM );
     145         342 :     if( xCustomSect.get() )
     146             :     {
     147             :         uno::Reference < beans::XPropertyContainer > xUserDefined(
     148         132 :             i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
     149         264 :         ::std::vector< sal_Int32 > aPropIds;
     150         132 :         xCustomSect->GetPropertyIds( aPropIds );
     151         304 :         for( ::std::vector< sal_Int32 >::const_iterator aIt = aPropIds.begin(),
     152         132 :              aEnd = aPropIds.end(); aIt != aEnd; ++aIt )
     153             :         {
     154          40 :             OUString aPropName = xCustomSect->GetPropertyName( *aIt );
     155          80 :             uno::Any aPropValue = xCustomSect->GetAnyValue( *aIt );
     156          40 :             if( !aPropName.isEmpty() && aPropValue.hasValue() )
     157             :             {
     158             :                 try
     159             :                 {
     160          40 :                     xUserDefined->addProperty( aPropName,
     161          40 :                         beans::PropertyAttribute::REMOVABLE, aPropValue );
     162             :                 }
     163           0 :                 catch (const uno::Exception&)
     164             :                 {
     165             :                     //ignore
     166             :                 }
     167             :             }
     168         172 :         }
     169             :     }
     170             : 
     171         684 :     uno::Reference< document::XCompatWriterDocProperties > xWriterProps( i_xDocProps, uno::UNO_QUERY );
     172         342 :     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         684 :     return (nGlobError != ERRCODE_NONE) ? nGlobError : nDocError;
     195             : }
     196             : 
     197          60 : bool SFX2_DLLPUBLIC 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          60 :     SfxOlePropertySet aGlobSet;
     207             : 
     208             :     // set supported properties
     209          60 :     SfxOleSection& rGlobSect = aGlobSet.AddSection( SECTION_GLOBAL );
     210          60 :     rGlobSect.SetStringValue( PROPID_TITLE,    i_xDocProps->getTitle() );
     211          60 :     rGlobSect.SetStringValue( PROPID_SUBJECT,  i_xDocProps->getSubject() );
     212             :     OUString aStr = ::comphelper::string::convertCommaSeparated(
     213         120 :         i_xDocProps->getKeywords() );
     214          60 :     rGlobSect.SetStringValue( PROPID_KEYWORDS, aStr );
     215          60 :     rGlobSect.SetStringValue( PROPID_TEMPLATE, i_xDocProps->getTemplateName() );
     216          60 :     rGlobSect.SetStringValue( PROPID_COMMENTS, i_xDocProps->getDescription() );
     217          60 :     rGlobSect.SetStringValue( PROPID_AUTHOR,   i_xDocProps->getAuthor() );
     218          60 :     rGlobSect.SetFileTimeValue(PROPID_CREATED, i_xDocProps->getCreationDate());
     219          60 :     rGlobSect.SetStringValue( PROPID_LASTAUTHOR, i_xDocProps->getModifiedBy() );
     220             :     rGlobSect.SetFileTimeValue(PROPID_LASTSAVED,
     221          60 :                                 i_xDocProps->getModificationDate() );
     222             :     // note: apparently PrintedBy is not supported in file format
     223          60 :     rGlobSect.SetFileTimeValue(PROPID_LASTPRINTED, i_xDocProps->getPrintDate());
     224             : 
     225          60 :     sal_Int32 dur = i_xDocProps->getEditingDuration();
     226          60 :     util::DateTime aEditTime;
     227             :     // add offset 1601-01-01
     228          60 :     aEditTime.Year    = 1601;
     229          60 :     aEditTime.Month   = 1;
     230          60 :     aEditTime.Day     = 1;
     231          60 :     aEditTime.Hours   = static_cast<sal_Int16>(dur / 3600);
     232          60 :     aEditTime.Minutes = static_cast<sal_Int16>((dur % 3600) / 60);
     233          60 :     aEditTime.Seconds = static_cast<sal_Int16>(dur % 60);
     234          60 :     rGlobSect.SetFileTimeValue( PROPID_EDITTIME, aEditTime );
     235             : 
     236             :     rGlobSect.SetStringValue( PROPID_REVNUMBER,
     237          60 :                 OUString::number( i_xDocProps->getEditingCycles() ) );
     238          60 :     if ( i_pThumb && i_pThumb->getLength() )
     239           0 :         rGlobSect.SetThumbnailValue( PROPID_THUMBNAIL, *i_pThumb );
     240             : 
     241             :     // save the property set
     242             :     ErrCode nGlobError = aGlobSet.SavePropertySet(i_pStorage,
     243          60 :         OUString(STREAM_SUMMARYINFO));
     244             : 
     245             :     // *** custom properties into stream "005DocumentSummaryInformation" ***
     246             : 
     247         120 :     SfxOlePropertySet aDocSet;
     248             : 
     249             :     // set builtin properties
     250          60 :     aDocSet.AddSection( SECTION_BUILTIN );
     251             : 
     252             :     // set custom properties
     253          60 :     SfxOleSection& rCustomSect = aDocSet.AddSection( SECTION_CUSTOM );
     254             : 
     255             :     // write GUID
     256          60 :     if (i_pGuid) {
     257           0 :         const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
     258           0 :         rCustomSect.SetBlobValue( nPropId, *i_pGuid );
     259             :         rCustomSect.SetPropertyName( nPropId,
     260           0 :             OUString("_PID_GUID") );
     261             :     }
     262             : 
     263             :     // write hyperlinks
     264          60 :     if (i_pHyperlinks) {
     265           0 :         const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
     266           0 :         rCustomSect.SetBlobValue( nPropId, *i_pHyperlinks );
     267             :         rCustomSect.SetPropertyName( nPropId,
     268           0 :             OUString("_PID_HLINKS") );
     269             :     }
     270             : 
     271             :     uno::Reference<beans::XPropertySet> xUserDefinedProps(
     272         120 :         i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
     273             :     DBG_ASSERT(xUserDefinedProps.is(), "UserDefinedProperties is null");
     274             :     uno::Reference<beans::XPropertySetInfo> xPropInfo =
     275         120 :         xUserDefinedProps->getPropertySetInfo();
     276             :     DBG_ASSERT(xPropInfo.is(), "UserDefinedProperties Info is null");
     277         120 :     uno::Sequence<beans::Property> props = xPropInfo->getProperties();
     278          92 :     for (sal_Int32 i = 0; i < props.getLength(); ++i)
     279             :     {
     280             :         try
     281             :         {
     282             :             // skip transient properties
     283          32 :             if (~props[i].Attributes & beans::PropertyAttribute::TRANSIENT)
     284             :             {
     285          32 :                 const OUString name = props[i].Name;
     286          32 :                 const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
     287          64 :                 if (rCustomSect.SetAnyValue( nPropId,
     288          64 :                             xUserDefinedProps->getPropertyValue(name))) {
     289          32 :                     rCustomSect.SetPropertyName( nPropId, name );
     290          32 :                 }
     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          60 :         OUString( STREAM_DOCSUMMARYINFO  ) );
     303             : 
     304             :     // return code
     305         120 :     return (nGlobError == ERRCODE_NONE) && (nDocError == ERRCODE_NONE);
     306             : }
     307             : 
     308           0 : uno::Sequence<sal_uInt8> SFX2_DLLPUBLIC 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         951 : } // namespace sfx2
     330             : 
     331             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10