1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include <OLEHandler.hxx>
24 #include <PropertyMap.hxx>
25 #include "GraphicHelpers.hxx"
26 
27 #include <doctok/resourceids.hxx>
28 #include <ooxml/resourceids.hxx>
29 #include <com/sun/star/beans/PropertyValue.hpp>
30 #include <com/sun/star/container/XNameAccess.hpp>
31 #include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
32 #include <com/sun/star/document/XStorageBasedDocument.hpp>
33 #include <com/sun/star/drawing/XShape.hpp>
34 #include <com/sun/star/embed/XEmbeddedObject.hpp>
35 #include <com/sun/star/embed/XEmbedObjectCreator.hpp>
36 #include <com/sun/star/graphic/XGraphic.hpp>
37 #include <com/sun/star/io/XStream.hpp>
38 #include <com/sun/star/lang/XComponent.hpp>
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #include <com/sun/star/text/XTextDocument.hpp>
41 #include <com/sun/star/uno/XComponentContext.hpp>
42 
43 #include "dmapperLoggers.hxx"
44 
45 namespace writerfilter {
46 namespace dmapper {
47 
48 using namespace ::com::sun::star;
49 /*-- 23.04.2008 10:46:14---------------------------------------------------
50 
51   -----------------------------------------------------------------------*/
OLEHandler()52 OLEHandler::OLEHandler() :
53 LoggedProperties(dmapper_logger, "OLEHandler"),
54 m_nDxaOrig(0),
55 m_nDyaOrig(0),
56 m_nWrapMode(0)
57 {
58 }
59 /*-- 23.04.2008 10:46:14---------------------------------------------------
60 
61   -----------------------------------------------------------------------*/
~OLEHandler()62 OLEHandler::~OLEHandler()
63 {
64 }
65 /*-- 23.04.2008 10:46:14---------------------------------------------------
66 
67   -----------------------------------------------------------------------*/
lcl_attribute(Id rName,Value & rVal)68 void OLEHandler::lcl_attribute(Id rName, Value & rVal)
69 {
70     rtl::OUString sStringValue = rVal.getString();
71     (void)rName;
72     /* WRITERFILTERSTATUS: table: OLEHandler_attributedata */
73     switch( rName )
74     {
75         /* WRITERFILTERSTATUS: done: 1, planned: 0, spent: 0 */
76         case NS_ooxml::LN_CT_OLEObject_Type:
77             m_sObjectType = sStringValue;
78         break;
79         case NS_ooxml::LN_CT_OLEObject_ProgID:
80             m_sProgId = sStringValue;
81         break;
82         case NS_ooxml::LN_CT_OLEObject_ShapeID:
83             m_sShapeId = sStringValue;
84         break;
85         case NS_ooxml::LN_CT_OLEObject_DrawAspect:
86             m_sDrawAspect = sStringValue;
87         break;
88         case NS_ooxml::LN_CT_OLEObject_ObjectID:
89             m_sObjectId = sStringValue;
90         break;
91         case NS_ooxml::LN_CT_OLEObject_r_id:
92             m_sr_id = sStringValue;
93         break;
94 		case NS_ooxml::LN_inputstream:
95 			rVal.getAny() >>= m_xInputStream;
96         break;
97         case NS_ooxml::LN_CT_Object_dxaOrig:
98             m_nDxaOrig = rVal.getInt();
99         break;
100         case NS_ooxml::LN_CT_Object_dyaOrig:
101             m_nDyaOrig = rVal.getInt();
102         break;
103         case NS_ooxml::LN_shape:
104         /* WRITERFILTERSTATUS: done: 0, planned: 0.5, spent: 0 */
105         {
106             uno::Reference< drawing::XShape > xTempShape;
107             rVal.getAny() >>= xTempShape;
108             if( xTempShape.is() )
109             {
110                 m_xShape.set( xTempShape );
111 
112                 try
113                 {
114                     m_aShapeSize = xTempShape->getSize();
115                     m_aShapePosition = xTempShape->getPosition();
116 
117                     uno::Reference< beans::XPropertySet > xShapeProps( xTempShape, uno::UNO_QUERY_THROW );
118                     PropertyNameSupplier& rNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
119 
120                     xShapeProps->getPropertyValue( rNameSupplier.GetName( PROP_BITMAP ) ) >>= m_xReplacement;
121 
122                     xShapeProps->setPropertyValue(
123                         rNameSupplier.GetName( PROP_SURROUND ),
124                         uno::makeAny( m_nWrapMode ) );
125                 }
126                 catch( const uno::Exception& e )
127                 {
128 #if DEBUG
129                     clog << "Exception in OLE Handler: ";
130                     clog << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr( ) << endl;
131 #endif
132                 }
133             }
134         }
135         break;
136         default:
137             OSL_ENSURE( false, "unknown attribute");
138     }
139 }
140 /*-- 23.04.2008 10:46:14---------------------------------------------------
141 
142   -----------------------------------------------------------------------*/
lcl_sprm(Sprm & rSprm)143 void OLEHandler::lcl_sprm(Sprm & rSprm)
144 {
145     sal_uInt32 nSprmId = rSprm.getId();
146     switch( nSprmId )
147     {
148         case NS_ooxml::LN_OLEObject_OLEObject:
149         {
150             writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
151             if( pProperties.get())
152             {
153                 pProperties->resolve(*this);
154             }
155         }
156         break;
157         case NS_ooxml::LN_wrap_wrap:
158         {
159             writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
160             if ( pProperties.get( ) )
161             {
162                 WrapHandlerPtr pHandler( new WrapHandler );
163                 pProperties->resolve( *pHandler );
164 
165                 m_nWrapMode = pHandler->getWrapMode( );
166 
167                 try
168                 {
169                     uno::Reference< beans::XPropertySet > xShapeProps( m_xShape, uno::UNO_QUERY_THROW );
170                     PropertyNameSupplier& rNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
171 
172                     xShapeProps->setPropertyValue(
173                         rNameSupplier.GetName( PROP_SURROUND ),
174                         uno::makeAny( m_nWrapMode ) );
175                 }
176                 catch( const uno::Exception& e )
177                 {
178 #if DEBUG
179                     clog << "Exception in OLE Handler: ";
180                     clog << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr( ) << endl;
181 #endif
182                 }
183             }
184         }
185         break;
186         default:
187         {
188             OSL_ENSURE( false, "unknown attribute");
189         }
190     }
191 }
192 /*-- 23.04.2008 11:15:19---------------------------------------------------
193 
194   -----------------------------------------------------------------------*/
copyOLEOStream(uno::Reference<text::XTextDocument> xTextDocument)195 ::rtl::OUString OLEHandler::copyOLEOStream( uno::Reference< text::XTextDocument > xTextDocument )
196 {
197     ::rtl::OUString sRet;
198     if( !m_xInputStream.is( ) )
199         return sRet;
200     try
201     {
202         uno::Reference < lang::XMultiServiceFactory > xFactory(xTextDocument, uno::UNO_QUERY_THROW);
203         uno::Reference< document::XEmbeddedObjectResolver > xEmbeddedResolver(
204             xFactory->createInstance(
205                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.document.ImportEmbeddedObjectResolver" ))), uno::UNO_QUERY_THROW );
206         //hack to work with the ImportEmbeddedObjectResolver
207         static sal_Int32 nObjectCount = 100;
208         uno::Reference< container::XNameAccess > xNA( xEmbeddedResolver, uno::UNO_QUERY_THROW );
209         ::rtl::OUString aURL(RTL_CONSTASCII_USTRINGPARAM("Obj" ));
210         aURL += ::rtl::OUString::valueOf( nObjectCount++ );
211         uno::Reference < io::XOutputStream > xOLEStream;
212         if( (xNA->getByName( aURL ) >>= xOLEStream) && xOLEStream.is() )
213         {
214             const sal_Int32 nReadRequest = 0x1000;
215             uno::Sequence< sal_Int8 > aData;
216 
217             while( true )
218             {
219                 sal_Int32 nRead = m_xInputStream->readBytes( aData, nReadRequest );
220                 xOLEStream->writeBytes( aData );
221                 if( nRead < nReadRequest )
222                 {
223                     xOLEStream->closeOutput();
224                     break;
225                 }
226             }
227 
228             static const ::rtl::OUString sProtocol = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.star.EmbeddedObject:" ));
229             ::rtl::OUString aPersistName( xEmbeddedResolver->resolveEmbeddedObjectURL( aURL ) );
230             sRet = aPersistName.copy( sProtocol.getLength() );
231 
232         }
233         uno::Reference< lang::XComponent > xComp( xEmbeddedResolver, uno::UNO_QUERY_THROW );
234         xComp->dispose();
235     }
236     catch( const uno::Exception& rEx)
237     {
238 		(void)rEx;
239 		OSL_ENSURE(false, "exception in OLEHandler::createOLEObject");
240     }
241     return sRet;
242 }
243 
244 } //namespace dmapper
245 } //namespace writerfilter
246