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