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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_embeddedobj.hxx"
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <com/sun/star/embed/Aspects.hpp>
30 #include <com/sun/star/io/XInputStream.hpp>
31 #include <com/sun/star/io/XOutputStream.hpp>
32 #include <com/sun/star/graphic/XGraphicProvider.hpp>
33 #include <com/sun/star/graphic/XGraphic.hpp>
34 #include <com/sun/star/beans/PropertyValue.hpp>
35 #include <tools/link.hxx>
36 #include <vos/mutex.hxx>
37 #include <unotools/streamwrap.hxx>
38 #include <comphelper/processfactory.hxx>
39 #include <comphelper/seqstream.hxx>
40 #include <tools/stream.hxx>
41 
42 #include "mtnotification.hxx"
43 #include "oleembobj.hxx"
44 
45 
46 using namespace ::com::sun::star;
47 
48 
ConvertBufferToFormat(void * pBuf,sal_uInt32 nBufSize,const::rtl::OUString & aMimeType,uno::Any & aResult)49 sal_Bool ConvertBufferToFormat( void* pBuf,
50 								sal_uInt32 nBufSize,
51 								const ::rtl::OUString& aMimeType,
52 								uno::Any& aResult )
53 {
54 	// produces sequence with data in requested format and returns it in aResult
55 	if ( pBuf )
56 	{
57 		uno::Sequence < sal_Int8 > aData( (sal_Int8*)pBuf, nBufSize );
58 		uno::Reference < io::XInputStream > xIn = new comphelper::SequenceInputStream( aData );
59 		try
60 		{
61 			uno::Reference < graphic::XGraphicProvider > xGraphicProvider( comphelper::getProcessServiceFactory()->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.graphic.GraphicProvider") ), uno::UNO_QUERY );
62 			if( xGraphicProvider.is() )
63 			{
64 				uno::Sequence< beans::PropertyValue > aMediaProperties( 1 );
65 				aMediaProperties[0].Name = ::rtl::OUString::createFromAscii( "InputStream" );
66 				aMediaProperties[0].Value <<= xIn;
67 				uno::Reference< graphic::XGraphic > xGraphic( xGraphicProvider->queryGraphic( aMediaProperties  ) );
68 				if( xGraphic.is() )
69 				{
70 					SvMemoryStream aNewStream( 65535, 65535 );
71 //					uno::Reference < io::XOutputStream > xOut = new utl::OOutputStreamHelper( aNewStream.GetLockBytes() );
72 					uno::Reference < io::XStream > xOut = new utl::OStreamWrapper( aNewStream );
73 					uno::Sequence< beans::PropertyValue > aOutMediaProperties( 2 );
74 					aOutMediaProperties[0].Name = ::rtl::OUString::createFromAscii( "OutputStream" );
75 					aOutMediaProperties[0].Value <<= xOut;
76 					aOutMediaProperties[1].Name = ::rtl::OUString::createFromAscii( "MimeType" );
77 					aOutMediaProperties[1].Value <<= aMimeType;
78 
79 					xGraphicProvider->storeGraphic( xGraphic, aOutMediaProperties );
80 					aResult <<= uno::Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aNewStream.GetData() ), aNewStream.Seek( STREAM_SEEK_TO_END ) );
81 					return sal_True;
82 				}
83 			}
84 		}
85 		catch (uno::Exception&)
86 		{}
87 	}
88 
89 	return sal_False;
90 }
91 
92 // =====================================================================
93 // MainThreadNotificationRequest
94 // =====================================================================
MainThreadNotificationRequest(const::rtl::Reference<OleEmbeddedObject> & xObj,sal_uInt16 nNotificationType,sal_uInt32 nAspect)95 MainThreadNotificationRequest::MainThreadNotificationRequest( const ::rtl::Reference< OleEmbeddedObject >& xObj, sal_uInt16 nNotificationType, sal_uInt32 nAspect )
96 : m_pObject( xObj.get() )
97 , m_xObject( static_cast< embed::XEmbeddedObject* >( xObj.get() ) )
98 , m_nNotificationType( nNotificationType )
99 , m_nAspect( nAspect )
100 {}
101 
notify(const uno::Any &)102 void SAL_CALL MainThreadNotificationRequest::notify (const uno::Any& ) throw (uno::RuntimeException)
103 {
104 	if ( m_pObject )
105 	{
106 		try
107 		{
108 			uno::Reference< uno::XInterface > xLock = m_xObject.get();
109 			if ( xLock.is() )
110 			{
111 				// this is the main thread, the solar mutex must be locked
112 				if ( m_nNotificationType == OLECOMP_ONCLOSE )
113 					m_pObject->OnClosed_Impl();
114 				else if ( m_nAspect == embed::Aspects::MSOLE_CONTENT )
115 					m_pObject->OnViewChanged_Impl();
116 				else if ( m_nAspect == embed::Aspects::MSOLE_ICON )
117 					m_pObject->OnIconChanged_Impl();
118 			}
119 		}
120 		catch( uno::Exception& )
121 		{
122 			// ignore all the errors
123 		}
124 	}
125 }
126 
~MainThreadNotificationRequest()127 MainThreadNotificationRequest::~MainThreadNotificationRequest()
128 {
129 }
130