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 
33 #include "olewrapclient.hxx"
34 #include "olecomponent.hxx"
35 
36 // TODO: May be a mutex must be introduced
37 
38 OleWrapperClientSite::OleWrapperClientSite( OleComponent* pOleComp )
39 : m_nRefCount( 0 )
40 , m_pOleComp( pOleComp )
41 {
42 	OSL_ENSURE( m_pOleComp, "No ole component is provided!\n" );
43 }
44 
45 OleWrapperClientSite::~OleWrapperClientSite()
46 {
47 }
48 
49 STDMETHODIMP OleWrapperClientSite::QueryInterface( REFIID riid , void** ppv )
50 {
51 	*ppv=NULL;
52 
53 	if ( riid == IID_IUnknown )
54 		*ppv = (IUnknown*)this;
55 
56 	if ( riid == IID_IOleClientSite )
57 		*ppv = (IOleClientSite*)this;
58 
59 	if ( *ppv != NULL )
60 	{
61 		((IUnknown*)*ppv)->AddRef();
62 		return S_OK;
63 	}
64 
65 	return E_NOINTERFACE;
66 }
67 
68 STDMETHODIMP_(ULONG) OleWrapperClientSite::AddRef()
69 {
70 	return osl_incrementInterlockedCount( &m_nRefCount);
71 }
72 
73 STDMETHODIMP_(ULONG) OleWrapperClientSite::Release()
74 {
75 	ULONG nReturn = --m_nRefCount;
76 	if ( m_nRefCount == 0 )
77 		delete this;
78 
79     return nReturn;
80 }
81 
82 void OleWrapperClientSite::disconnectOleComponent()
83 {
84 	// must not be called from the descructor of OleComponent!!!
85 	osl::MutexGuard aGuard( m_aMutex );
86 	m_pOleComp = NULL;
87 }
88 
89 STDMETHODIMP OleWrapperClientSite::SaveObject()
90 {
91 	OleComponent* pLockComponent = NULL;
92 	HRESULT hResult = E_FAIL;
93 
94 	{
95 		osl::MutexGuard aGuard( m_aMutex );
96 		if ( m_pOleComp )
97 		{
98 			pLockComponent = m_pOleComp;
99 			pLockComponent->acquire();
100 		}
101 	}
102 
103 	if ( pLockComponent )
104 	{
105 		if ( pLockComponent->SaveObject_Impl() )
106 			hResult = S_OK;
107 
108 		pLockComponent->release();
109 	}
110 
111 	return hResult;
112 }
113 
114 STDMETHODIMP OleWrapperClientSite::GetMoniker( DWORD, DWORD, LPMONIKER *ppmk )
115 {
116 	*ppmk = NULL;
117 	return E_NOTIMPL;
118 }
119 
120 STDMETHODIMP OleWrapperClientSite::GetContainer( LPOLECONTAINER* ppContainer )
121 {
122 	*ppContainer = NULL;
123 	return E_NOTIMPL;
124 }
125 
126 STDMETHODIMP OleWrapperClientSite::ShowObject(void)
127 {
128 	return S_OK;
129 }
130 
131 STDMETHODIMP OleWrapperClientSite::OnShowWindow( BOOL bShow )
132 {
133 	OleComponent* pLockComponent = NULL;
134 
135 	// TODO/LATER: redirect the notification to the main thread so that SolarMutex can be locked
136 	{
137 		osl::MutexGuard aGuard( m_aMutex );
138 		if ( m_pOleComp )
139 		{
140 			pLockComponent = m_pOleComp;
141 			pLockComponent->acquire();
142 		}
143 	}
144 
145 	if ( pLockComponent )
146 	{
147 		pLockComponent->OnShowWindow_Impl( bShow ); // the result is not interesting
148 		pLockComponent->release();
149 	}
150 
151 	return S_OK;
152 }
153 
154 STDMETHODIMP OleWrapperClientSite::RequestNewObjectLayout(void)
155 {
156 	return E_NOTIMPL;
157 }
158 
159