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