1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #include "oox/ole/oleobjecthelper.hxx" 29 30 #include <com/sun/star/awt/Rectangle.hpp> 31 #include <com/sun/star/awt/Size.hpp> 32 #include <com/sun/star/container/XNameAccess.hpp> 33 #include <com/sun/star/document/XEmbeddedObjectResolver.hpp> 34 #include <com/sun/star/embed/Aspects.hpp> 35 #include <com/sun/star/io/XOutputStream.hpp> 36 #include <com/sun/star/lang/XComponent.hpp> 37 #include <com/sun/star/lang/XMultiServiceFactory.hpp> 38 #include "oox/helper/propertymap.hxx" 39 40 namespace oox { 41 namespace ole { 42 43 // ============================================================================ 44 45 using namespace ::com::sun::star::awt; 46 using namespace ::com::sun::star::container; 47 using namespace ::com::sun::star::embed; 48 using namespace ::com::sun::star::io; 49 using namespace ::com::sun::star::lang; 50 using namespace ::com::sun::star::uno; 51 52 using ::rtl::OUString; 53 54 // ============================================================================ 55 56 OleObjectInfo::OleObjectInfo() : 57 mbLinked( false ), 58 mbShowAsIcon( false ), 59 mbAutoUpdate( false ) 60 { 61 } 62 63 // ============================================================================ 64 65 OleObjectHelper::OleObjectHelper( const Reference< XMultiServiceFactory >& rxModelFactory ) : 66 maEmbeddedObjScheme( CREATE_OUSTRING( "vnd.sun.star.EmbeddedObject:" ) ), 67 mnObjectId( 100 ) 68 { 69 if( rxModelFactory.is() ) try 70 { 71 mxResolver.set( rxModelFactory->createInstance( CREATE_OUSTRING( "com.sun.star.document.ImportEmbeddedObjectResolver" ) ), UNO_QUERY ); 72 } 73 catch( Exception& ) 74 { 75 } 76 } 77 78 OleObjectHelper::~OleObjectHelper() 79 { 80 try 81 { 82 Reference< XComponent > xResolverComp( mxResolver, UNO_QUERY_THROW ); 83 xResolverComp->dispose(); 84 } 85 catch( Exception& ) 86 { 87 } 88 } 89 90 bool OleObjectHelper::importOleObject( PropertyMap& rPropMap, const OleObjectInfo& rOleObject, const Size& rObjSize ) 91 { 92 bool bRet = false; 93 94 if( rOleObject.mbLinked ) 95 { 96 // linked OLE object - set target URL 97 if( rOleObject.maTargetLink.getLength() > 0 ) 98 { 99 rPropMap[ PROP_LinkURL ] <<= rOleObject.maTargetLink; 100 bRet = true; 101 } 102 } 103 else 104 { 105 // embedded OLE object - import the embedded data 106 if( rOleObject.maEmbeddedData.hasElements() && mxResolver.is() ) try 107 { 108 OUString aObjectId = CREATE_OUSTRING( "Obj" ) + OUString::valueOf( mnObjectId++ ); 109 110 Reference< XNameAccess > xResolverNA( mxResolver, UNO_QUERY_THROW ); 111 Reference< XOutputStream > xOutStrm( xResolverNA->getByName( aObjectId ), UNO_QUERY_THROW ); 112 xOutStrm->writeBytes( rOleObject.maEmbeddedData ); 113 xOutStrm->closeOutput(); 114 115 OUString aUrl = mxResolver->resolveEmbeddedObjectURL( aObjectId ); 116 OSL_ENSURE( aUrl.match( maEmbeddedObjScheme ), "OleObjectHelper::importOleObject - unexpected URL scheme" ); 117 OUString aPersistName = aUrl.copy( maEmbeddedObjScheme.getLength() ); 118 if( aPersistName.getLength() > 0 ) 119 { 120 rPropMap[ PROP_PersistName ] <<= aPersistName; 121 bRet = true; 122 } 123 } 124 catch( Exception& ) 125 { 126 } 127 } 128 129 if( bRet ) 130 { 131 rPropMap[ PROP_Aspect ] <<= (rOleObject.mbShowAsIcon ? Aspects::MSOLE_ICON : Aspects::MSOLE_CONTENT); 132 rPropMap[ PROP_VisualArea ] <<= Rectangle( 0, 0, rObjSize.Width, rObjSize.Height ); 133 } 134 return bRet; 135 } 136 137 // ============================================================================ 138 139 } // namespace ole 140 } // namespace oox 141