LCOV - code coverage report
Current view: top level - libreoffice/sfx2/source/doc - docinf.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 102 143 71.3 %
Date: 2012-12-17 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             : 
      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         128 : 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         128 :     SfxOlePropertySet aGlobSet;
      57             :     ErrCode nGlobError = aGlobSet.LoadPropertySet(i_pStorage,
      58         128 :         String( STREAM_SUMMARYINFO  ) );
      59             : 
      60             :     // global section
      61         128 :     SfxOleSectionRef xGlobSect = aGlobSet.GetSection( SECTION_GLOBAL );
      62         128 :     if( xGlobSect.get() )
      63             :     {
      64             :         // set supported properties
      65         116 :         String aStrValue;
      66         116 :         util::DateTime aDateTime;
      67             : 
      68         116 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_TITLE ) )
      69          54 :             i_xDocProps->setTitle( aStrValue );
      70         116 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_SUBJECT ) )
      71          48 :             i_xDocProps->setSubject( aStrValue );
      72         116 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_KEYWORDS ) ) {
      73          48 :             i_xDocProps->setKeywords(
      74          48 :                 ::comphelper::string::convertCommaSeparated(aStrValue) );
      75             :         }
      76         116 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_TEMPLATE ) )
      77          62 :             i_xDocProps->setTemplateName( aStrValue );
      78         116 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_COMMENTS ) )
      79          34 :             i_xDocProps->setDescription( aStrValue );
      80             : 
      81         116 :         util::DateTime aInvalid;
      82         116 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_AUTHOR) )
      83         102 :             i_xDocProps->setAuthor( aStrValue );
      84             :         else
      85          14 :             i_xDocProps->setAuthor( ::rtl::OUString() );
      86         116 :         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_CREATED ) )
      87         110 :             i_xDocProps->setCreationDate( aDateTime );
      88             :         else
      89           6 :             i_xDocProps->setCreationDate( aInvalid );
      90             : 
      91         116 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_LASTAUTHOR) )
      92          98 :             i_xDocProps->setModifiedBy( aStrValue );
      93             :         else
      94          18 :             i_xDocProps->setModifiedBy( ::rtl::OUString() );
      95         116 :         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_LASTSAVED ) )
      96         108 :             i_xDocProps->setModificationDate( aDateTime );
      97             :         else
      98           8 :             i_xDocProps->setModificationDate( aInvalid );
      99             : 
     100         116 :         i_xDocProps->setPrintedBy( ::rtl::OUString() );
     101         116 :         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_LASTPRINTED ) )
     102          48 :             i_xDocProps->setPrintDate( aDateTime );
     103             :         else
     104          68 :             i_xDocProps->setPrintDate( aInvalid );
     105             : 
     106         116 :         if( xGlobSect->GetStringValue( aStrValue, PROPID_REVNUMBER ) )
     107             :         {
     108          86 :             sal_Int16 nRevision = static_cast< sal_Int16 >( aStrValue.ToInt32() );
     109          86 :             if ( nRevision > 0 )
     110          80 :                 i_xDocProps->setEditingCycles( nRevision );
     111             :         }
     112             : 
     113         116 :         if( xGlobSect->GetFileTimeValue( aDateTime, PROPID_EDITTIME ) )
     114             :         {
     115             :             // subtract offset 1601-01-01
     116          84 :             aDateTime.Year  -= 1601;
     117          84 :             aDateTime.Month -= 1;
     118          84 :             aDateTime.Day   -= 1;
     119             :             try
     120             :             {
     121          84 :                 i_xDocProps->setEditingDuration(
     122             :                     aDateTime.Day     * 60*60*24 +
     123             :                     aDateTime.Hours   * 60*60    +
     124             :                     aDateTime.Minutes * 60       +
     125          84 :                     aDateTime.Seconds            );
     126             :             }
     127           0 :             catch (const lang::IllegalArgumentException &)
     128             :             {
     129             :                 // ignore
     130             :             }
     131         116 :         }
     132             :     }
     133             : 
     134             :     // *** custom properties from stream "005DocumentSummaryInformation" ***
     135             : 
     136             :     // load the property set
     137         128 :     SfxOlePropertySet aDocSet;
     138             :     ErrCode nDocError = aDocSet.LoadPropertySet(i_pStorage,
     139         128 :         String( STREAM_DOCSUMMARYINFO  ) );
     140             : 
     141             :     // custom properties
     142         128 :     SfxOleSectionRef xCustomSect = aDocSet.GetSection( SECTION_CUSTOM );
     143         128 :     if( xCustomSect.get() )
     144             :     {
     145             :         uno::Reference < beans::XPropertyContainer > xUserDefined(
     146          52 :             i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
     147          52 :         ::std::vector< sal_Int32 > aPropIds;
     148          52 :         xCustomSect->GetPropertyIds( aPropIds );
     149         104 :         for( ::std::vector< sal_Int32 >::const_iterator aIt = aPropIds.begin(),
     150          52 :              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          52 :         }
     167             :     }
     168             : 
     169         128 :     uno::Reference< document::XCompatWriterDocProperties > xWriterProps( i_xDocProps, uno::UNO_QUERY );
     170         128 :     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         128 :     return (nGlobError != ERRCODE_NONE) ? nGlobError : nDocError;
     193             : }
     194             : 
     195           8 : 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           8 :     SfxOlePropertySet aGlobSet;
     205             : 
     206             :     // set supported properties
     207           8 :     SfxOleSection& rGlobSect = aGlobSet.AddSection( SECTION_GLOBAL );
     208           8 :     rGlobSect.SetStringValue( PROPID_TITLE,    i_xDocProps->getTitle() );
     209           8 :     rGlobSect.SetStringValue( PROPID_SUBJECT,  i_xDocProps->getSubject() );
     210             :     String aStr = ::comphelper::string::convertCommaSeparated(
     211           8 :         i_xDocProps->getKeywords() );
     212           8 :     rGlobSect.SetStringValue( PROPID_KEYWORDS, aStr );
     213           8 :     rGlobSect.SetStringValue( PROPID_TEMPLATE, i_xDocProps->getTemplateName() );
     214           8 :     rGlobSect.SetStringValue( PROPID_COMMENTS, i_xDocProps->getDescription() );
     215           8 :     rGlobSect.SetStringValue( PROPID_AUTHOR,   i_xDocProps->getAuthor() );
     216           8 :     rGlobSect.SetFileTimeValue(PROPID_CREATED, i_xDocProps->getCreationDate());
     217           8 :     rGlobSect.SetStringValue( PROPID_LASTAUTHOR, i_xDocProps->getModifiedBy() );
     218             :     rGlobSect.SetFileTimeValue(PROPID_LASTSAVED,
     219           8 :                                 i_xDocProps->getModificationDate() );
     220             :     // note: apparently PrintedBy is not supported in file format
     221           8 :     rGlobSect.SetFileTimeValue(PROPID_LASTPRINTED, i_xDocProps->getPrintDate());
     222             : 
     223           8 :     sal_Int32 dur = i_xDocProps->getEditingDuration();
     224           8 :     util::DateTime aEditTime;
     225             :     // add offset 1601-01-01
     226           8 :     aEditTime.Year    = 1601;
     227           8 :     aEditTime.Month   = 1;
     228           8 :     aEditTime.Day     = 1;
     229           8 :     aEditTime.Hours   = static_cast<sal_Int16>(dur / 3600);
     230           8 :     aEditTime.Minutes = static_cast<sal_Int16>((dur % 3600) / 60);
     231           8 :     aEditTime.Seconds = static_cast<sal_Int16>(dur % 60);
     232           8 :     rGlobSect.SetFileTimeValue( PROPID_EDITTIME, aEditTime );
     233             : 
     234             :     rGlobSect.SetStringValue( PROPID_REVNUMBER,
     235           8 :                 rtl::OUString::valueOf( static_cast<sal_Int32>(i_xDocProps->getEditingCycles()) ) );
     236           8 :     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           8 :         ::rtl::OUString(STREAM_SUMMARYINFO));
     242             : 
     243             :     // *** custom properties into stream "005DocumentSummaryInformation" ***
     244             : 
     245           8 :     SfxOlePropertySet aDocSet;
     246             : 
     247             :     // set builtin properties
     248           8 :     aDocSet.AddSection( SECTION_BUILTIN );
     249             : 
     250             :     // set custom properties
     251           8 :     SfxOleSection& rCustomSect = aDocSet.AddSection( SECTION_CUSTOM );
     252             : 
     253             :     // write GUID
     254           8 :     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           8 :     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           8 :         i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
     271             :     DBG_ASSERT(xUserDefinedProps.is(), "UserDefinedProperties is null");
     272             :     uno::Reference<beans::XPropertySetInfo> xPropInfo =
     273           8 :         xUserDefinedProps->getPropertySetInfo();
     274             :     DBG_ASSERT(xPropInfo.is(), "UserDefinedProperties Info is null");
     275           8 :     uno::Sequence<beans::Property> props = xPropInfo->getProperties();
     276          16 :     for (sal_Int32 i = 0; i < props.getLength(); ++i)
     277             :     {
     278             :         try
     279             :         {
     280             :             // skip transient properties
     281           8 :             if (~props[i].Attributes & beans::PropertyAttribute::TRANSIENT)
     282             :             {
     283           8 :                 const ::rtl::OUString name = props[i].Name;
     284           8 :                 const sal_Int32 nPropId = rCustomSect.GetFreePropertyId();
     285          16 :                 if (rCustomSect.SetAnyValue( nPropId,
     286          16 :                             xUserDefinedProps->getPropertyValue(name))) {
     287           0 :                     rCustomSect.SetPropertyName( nPropId, name );
     288           8 :                 }
     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           8 :         String( STREAM_DOCSUMMARYINFO  ) );
     301             : 
     302             :     // return code
     303           8 :     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: */

Generated by: LCOV version 1.10