1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <osl/diagnose.h>
21 
22 #include "olewrapclient.hxx"
23 #include "olecomponent.hxx"
24 
25 // TODO: May be a mutex must be introduced
26 
OleWrapperClientSite(OleComponent * pOleComp)27 OleWrapperClientSite::OleWrapperClientSite( OleComponent* pOleComp )
28 : m_nRefCount( 0 )
29 , m_pOleComp( pOleComp )
30 {
31     OSL_ENSURE( m_pOleComp, "No ole component is provided!" );
32 }
33 
~OleWrapperClientSite()34 OleWrapperClientSite::~OleWrapperClientSite()
35 {
36 }
37 
QueryInterface(REFIID riid,void ** ppv)38 STDMETHODIMP OleWrapperClientSite::QueryInterface( REFIID riid , void** ppv )
39 {
40     *ppv=nullptr;
41 
42     if ( riid == IID_IUnknown )
43         *ppv = static_cast<IUnknown*>(this);
44 
45     if ( riid == IID_IOleClientSite )
46         *ppv = static_cast<IOleClientSite*>(this);
47 
48     if ( *ppv != nullptr )
49     {
50         static_cast<IUnknown*>(*ppv)->AddRef();
51         return S_OK;
52     }
53 
54     return E_NOINTERFACE;
55 }
56 
STDMETHODIMP_(ULONG)57 STDMETHODIMP_(ULONG) OleWrapperClientSite::AddRef()
58 {
59     return osl_atomic_increment( &m_nRefCount);
60 }
61 
STDMETHODIMP_(ULONG)62 STDMETHODIMP_(ULONG) OleWrapperClientSite::Release()
63 {
64     ULONG nReturn = --m_nRefCount;
65     if ( m_nRefCount == 0 )
66         delete this;
67 
68     return nReturn;
69 }
70 
disconnectOleComponent()71 void OleWrapperClientSite::disconnectOleComponent()
72 {
73     // must not be called from the descructor of OleComponent!!!
74     osl::MutexGuard aGuard( m_aMutex );
75     m_pOleComp = nullptr;
76 }
77 
SaveObject()78 STDMETHODIMP OleWrapperClientSite::SaveObject()
79 {
80     OleComponent* pLockComponent = nullptr;
81     HRESULT hResult = E_FAIL;
82 
83     {
84         osl::MutexGuard aGuard( m_aMutex );
85         if ( m_pOleComp )
86         {
87             pLockComponent = m_pOleComp;
88             pLockComponent->acquire();
89         }
90     }
91 
92     if ( pLockComponent )
93     {
94         if ( pLockComponent->SaveObject_Impl() )
95             hResult = S_OK;
96 
97         pLockComponent->release();
98     }
99 
100     return hResult;
101 }
102 
GetMoniker(DWORD,DWORD,IMoniker ** ppmk)103 STDMETHODIMP OleWrapperClientSite::GetMoniker( DWORD, DWORD, IMoniker **ppmk )
104 {
105     *ppmk = nullptr;
106     return E_NOTIMPL;
107 }
108 
GetContainer(IOleContainer ** ppContainer)109 STDMETHODIMP OleWrapperClientSite::GetContainer( IOleContainer** ppContainer )
110 {
111     *ppContainer = nullptr;
112     return E_NOTIMPL;
113 }
114 
ShowObject()115 STDMETHODIMP OleWrapperClientSite::ShowObject()
116 {
117     return S_OK;
118 }
119 
OnShowWindow(BOOL bShow)120 STDMETHODIMP OleWrapperClientSite::OnShowWindow( BOOL bShow )
121 {
122     OleComponent* pLockComponent = nullptr;
123 
124     // TODO/LATER: redirect the notification to the main thread so that SolarMutex can be locked
125     {
126         osl::MutexGuard aGuard( m_aMutex );
127         if ( m_pOleComp )
128         {
129             pLockComponent = m_pOleComp;
130             pLockComponent->acquire();
131         }
132     }
133 
134     if ( pLockComponent )
135     {
136         pLockComponent->OnShowWindow_Impl( bShow ); // the result is not interesting
137         pLockComponent->release();
138     }
139 
140     return S_OK;
141 }
142 
RequestNewObjectLayout()143 STDMETHODIMP OleWrapperClientSite::RequestNewObjectLayout()
144 {
145     return E_NOTIMPL;
146 }
147 
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
149