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                     (void) e;
129 #if DEBUG
130                     dmapper_logger->startElement("exception");
131                     dmapper_logger->chars(e.Message);
132                     dmapper_logger->endElement("exception");
133 #endif
134                 }
135             }
136         }
137         break;
138         default:
139             OSL_ENSURE( false, "unknown attribute");
140     }
141 }
142 /*-- 23.04.2008 10:46:14---------------------------------------------------
143 
144   -----------------------------------------------------------------------*/
lcl_sprm(Sprm & rSprm)145 void OLEHandler::lcl_sprm(Sprm & rSprm)
146 {
147     sal_uInt32 nSprmId = rSprm.getId();
148     switch( nSprmId )
149     {
150         case NS_ooxml::LN_OLEObject_OLEObject:
151         {
152             writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
153             if( pProperties.get())
154             {
155                 pProperties->resolve(*this);
156             }
157         }
158         break;
159         case NS_ooxml::LN_wrap_wrap:
160         {
161             writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
162             if ( pProperties.get( ) )
163             {
164                 WrapHandlerPtr pHandler( new WrapHandler );
165                 pProperties->resolve( *pHandler );
166 
167                 m_nWrapMode = pHandler->getWrapMode( );
168 
169                 try
170                 {
171                     uno::Reference< beans::XPropertySet > xShapeProps( m_xShape, uno::UNO_QUERY_THROW );
172                     PropertyNameSupplier& rNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
173 
174                     xShapeProps->setPropertyValue(
175                         rNameSupplier.GetName( PROP_SURROUND ),
176                         uno::makeAny( m_nWrapMode ) );
177                 }
178                 catch( const uno::Exception& e )
179                 {
180                     (void) e;
181 #if DEBUG
182                     dmapper_logger->startElement("exception");
183                     dmapper_logger->chars(e.Message);
184                     dmapper_logger->endElement("exception");
185 #endif
186                 }
187             }
188         }
189         break;
190         default:
191         {
192             OSL_ENSURE( false, "unknown attribute");
193         }
194     }
195 }
196 /*-- 23.04.2008 11:15:19---------------------------------------------------
197 
198   -----------------------------------------------------------------------*/
copyOLEOStream(uno::Reference<text::XTextDocument> xTextDocument)199 ::rtl::OUString OLEHandler::copyOLEOStream( uno::Reference< text::XTextDocument > xTextDocument )
200 {
201     ::rtl::OUString sRet;
202     if( !m_xInputStream.is( ) )
203         return sRet;
204     try
205     {
206         uno::Reference < lang::XMultiServiceFactory > xFactory(xTextDocument, uno::UNO_QUERY_THROW);
207         uno::Reference< document::XEmbeddedObjectResolver > xEmbeddedResolver(
208             xFactory->createInstance(
209                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.document.ImportEmbeddedObjectResolver" ))), uno::UNO_QUERY_THROW );
210         //hack to work with the ImportEmbeddedObjectResolver
211         static sal_Int32 nObjectCount = 100;
212         uno::Reference< container::XNameAccess > xNA( xEmbeddedResolver, uno::UNO_QUERY_THROW );
213         ::rtl::OUString aURL(RTL_CONSTASCII_USTRINGPARAM("Obj" ));
214         aURL += ::rtl::OUString::valueOf( nObjectCount++ );
215         uno::Reference < io::XOutputStream > xOLEStream;
216         if( (xNA->getByName( aURL ) >>= xOLEStream) && xOLEStream.is() )
217         {
218             const sal_Int32 nReadRequest = 0x1000;
219             uno::Sequence< sal_Int8 > aData;
220 
221             while( true )
222             {
223                 sal_Int32 nRead = m_xInputStream->readBytes( aData, nReadRequest );
224                 xOLEStream->writeBytes( aData );
225                 if( nRead < nReadRequest )
226                 {
227                     xOLEStream->closeOutput();
228                     break;
229                 }
230             }
231 
232             static const ::rtl::OUString sProtocol = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.star.EmbeddedObject:" ));
233             ::rtl::OUString aPersistName( xEmbeddedResolver->resolveEmbeddedObjectURL( aURL ) );
234             sRet = aPersistName.copy( sProtocol.getLength() );
235 
236         }
237         uno::Reference< lang::XComponent > xComp( xEmbeddedResolver, uno::UNO_QUERY_THROW );
238         xComp->dispose();
239     }
240     catch( const uno::Exception& rEx)
241     {
242 		(void)rEx;
243 		OSL_ENSURE(false, "exception in OLEHandler::createOLEObject");
244     }
245     return sRet;
246 }
247 
248 } //namespace dmapper
249 } //namespace writerfilter
250