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 
31 #include <osl/diagnose.h>
32 #include <advisesink.hxx>
33 #include <olecomponent.hxx>
34 
35 #include <rtl/ref.hxx>
36 
37 OleWrapperAdviseSink::OleWrapperAdviseSink( OleComponent* pOleComp )
38 : m_nRefCount( 0 )
39 , m_pOleComp( pOleComp )
40 {
41 	OSL_ENSURE( m_pOleComp, "No ole component is provided!\n" );
42 }
43 
44 OleWrapperAdviseSink::~OleWrapperAdviseSink()
45 {
46 }
47 
48 STDMETHODIMP OleWrapperAdviseSink::QueryInterface( REFIID riid , void** ppv )
49 {
50 	*ppv=NULL;
51 
52 	if ( riid == IID_IUnknown )
53 		*ppv = (IUnknown*)this;
54 
55 	if ( riid == IID_IAdviseSink )
56 		*ppv = (IAdviseSink*)this;
57 
58 	if ( *ppv != NULL )
59 	{
60 		((IUnknown*)*ppv)->AddRef();
61 		return S_OK;
62 	}
63 
64 	return E_NOINTERFACE;
65 }
66 
67 STDMETHODIMP_(ULONG) OleWrapperAdviseSink::AddRef()
68 {
69 	return osl_incrementInterlockedCount( &m_nRefCount);
70 }
71 
72 STDMETHODIMP_(ULONG) OleWrapperAdviseSink::Release()
73 {
74 	ULONG nReturn = --m_nRefCount;
75 	if ( m_nRefCount == 0 )
76 		delete this;
77 
78     return nReturn;
79 }
80 
81 void OleWrapperAdviseSink::disconnectOleComponent()
82 {
83 	// must not be called from the descructor of OleComponent!!!
84 	osl::MutexGuard aGuard( m_aMutex );
85 	m_pOleComp = NULL;
86 }
87 
88 STDMETHODIMP_(void) OleWrapperAdviseSink::OnDataChange(LPFORMATETC, LPSTGMEDIUM)
89 {
90 	// Unused for now ( no registration for IDataObject events )
91 }
92 
93 STDMETHODIMP_(void) OleWrapperAdviseSink::OnViewChange(DWORD dwAspect, LONG)
94 {
95 	::rtl::Reference< OleComponent > xLockComponent;
96 
97 	{
98 		osl::MutexGuard aGuard( m_aMutex );
99 		if ( m_pOleComp )
100 			xLockComponent = m_pOleComp;
101 	}
102 
103 	if ( xLockComponent.is() )
104 		xLockComponent->OnViewChange_Impl( dwAspect );
105 }
106 
107 STDMETHODIMP_(void) OleWrapperAdviseSink::OnRename(LPMONIKER)
108 {
109 	// handled by default inprocess handler
110 }
111 
112 STDMETHODIMP_(void) OleWrapperAdviseSink::OnSave(void)
113 {
114 	// TODO: ???
115 	// The object knows about document saving already since it contolls it as ClienSite
116 	// other interested listeners must be registered for the object
117 }
118 
119 STDMETHODIMP_(void) OleWrapperAdviseSink::OnClose(void)
120 {
121 	::rtl::Reference< OleComponent > xLockComponent;
122 
123 	{
124 		osl::MutexGuard aGuard( m_aMutex );
125 		if ( m_pOleComp )
126 			xLockComponent = m_pOleComp;
127 	}
128 
129 	if ( xLockComponent.is() )
130 		xLockComponent->OnClose_Impl();
131 
132 	// TODO: sometimes it can be necessary to simulate OnShowWindow( False ) here
133 }
134 
135