Update ooo320-m1
[ooovba.git] / embedserv / source / embed / servprov.cxx
blobed59c7657207e70022fe13cf01f559556bac802a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: servprov.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #if defined(_MSC_VER) && (_MSC_VER > 1310)
31 #pragma warning(disable : 4917 4555)
32 #endif
34 #include "stdafx.h"
35 #include "servprov.hxx"
36 #include "embeddoc.hxx"
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <cppuhelper/typeprovider.hxx>
39 #include <osl/mutex.hxx>
40 #include <osl/thread.h>
42 using namespace com::sun::star;
44 const GUID* guidList[ SUPPORTED_FACTORIES_NUM ] = {
45 &OID_WriterTextServer,
46 &OID_WriterOASISTextServer,
47 &OID_CalcServer,
48 &OID_CalcOASISServer,
49 &OID_DrawingServer,
50 &OID_DrawingOASISServer,
51 &OID_PresentationServer,
52 &OID_PresentationOASISServer,
53 &OID_MathServer,
54 &OID_MathOASISServer
57 class CurThreadData
59 public:
60 CurThreadData();
61 virtual ~CurThreadData();
63 sal_Bool SAL_CALL setData(void *pData);
65 void* SAL_CALL getData();
67 protected:
68 oslThreadKey m_hKey;
71 CurThreadData::CurThreadData()
73 m_hKey = osl_createThreadKey( (oslThreadKeyCallbackFunction)NULL );
76 CurThreadData::~CurThreadData()
78 osl_destroyThreadKey(m_hKey);
81 sal_Bool CurThreadData::setData(void *pData)
83 OSL_ENSURE( m_hKey, "No thread key!\n" );
84 return (osl_setThreadKeyData(m_hKey, pData));
87 void *CurThreadData::getData()
89 OSL_ENSURE( m_hKey, "No thread key!\n" );
90 return (osl_getThreadKeyData(m_hKey));
94 // CoInitializeEx *
95 typedef DECLSPEC_IMPORT HRESULT (STDAPICALLTYPE *ptrCoInitEx)( LPVOID, DWORD);
96 // CoInitialize *
97 typedef DECLSPEC_IMPORT HRESULT (STDAPICALLTYPE *ptrCoInit)( LPVOID);
99 void o2u_attachCurrentThread()
101 static CurThreadData oleThreadData;
103 if ( oleThreadData.getData() != 0 )
105 HINSTANCE inst= LoadLibrary( _T("ole32.dll"));
106 if( inst )
108 HRESULT hr;
109 ptrCoInitEx initFuncEx= (ptrCoInitEx)GetProcAddress( inst, _T("CoInitializeEx"));
110 if( initFuncEx)
111 hr= initFuncEx( NULL, COINIT_MULTITHREADED);
112 else
114 ptrCoInit initFunc= (ptrCoInit)GetProcAddress( inst,_T("CoInitialize"));
115 if( initFunc)
116 hr= initFunc( NULL);
119 oleThreadData.setData((void*)sal_True);
124 //===============================================================================
125 // EmbedServer_Impl
127 EmbedServer_Impl::EmbedServer_Impl( const uno::Reference<lang::XMultiServiceFactory>& xFactory):
128 m_xFactory( xFactory)
130 for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
132 m_pOLEFactories[nInd] = new EmbedProviderFactory_Impl( m_xFactory, guidList[nInd] );
133 m_pOLEFactories[nInd]->registerClass();
137 EmbedServer_Impl::~EmbedServer_Impl()
139 for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
141 if ( m_pOLEFactories[nInd] )
142 m_pOLEFactories[nInd]->deregisterClass();
146 // XInterface --------------------------------------------------
147 uno::Any SAL_CALL
148 EmbedServer_Impl::queryInterface(
149 const uno::Type& aType )
150 throw(
151 uno::RuntimeException
154 uno::Any a=
155 ::cppu::queryInterface(
156 aType, static_cast<lang::XTypeProvider*>(this));
157 if( a == uno::Any())
158 return OWeakObject::queryInterface( aType);
159 else
160 return a;
163 void SAL_CALL EmbedServer_Impl::acquire( ) throw(uno::RuntimeException)
165 OWeakObject::acquire();
168 void SAL_CALL EmbedServer_Impl::release( ) throw (uno::RuntimeException)
170 OWeakObject::release();
174 // XTypeProvider --------------------------------------------------
175 uno::Sequence< uno::Type > SAL_CALL
176 EmbedServer_Impl::getTypes( )
177 throw(
178 uno::RuntimeException
181 static ::cppu::OTypeCollection *pCollection = 0;
182 if( ! pCollection )
184 ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
185 if( ! pCollection )
187 static ::cppu::OTypeCollection collection(
188 getCppuType(
189 reinterpret_cast<uno::Reference< uno::XWeak>*>(0)),
190 getCppuType(
191 reinterpret_cast<
192 uno::Reference< lang::XTypeProvider>*>(0)));
193 pCollection = &collection;
196 return (*pCollection).getTypes();
199 uno::Sequence< sal_Int8 > SAL_CALL EmbedServer_Impl::getImplementationId() throw(uno::RuntimeException)
201 static ::cppu::OImplementationId *pId = 0;
202 if( ! pId )
204 ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
205 if( ! pId )
207 static ::cppu::OImplementationId id( sal_False );
208 pId = &id;
211 return (*pId).getImplementationId();
214 //===============================================================================
215 // EmbedProviderFactory_Impl
217 EmbedProviderFactory_Impl::EmbedProviderFactory_Impl(const uno::Reference<lang::XMultiServiceFactory>& xFactory, const GUID* pGuid)
218 : m_refCount( 0L )
219 , m_xFactory( xFactory )
220 , m_guid( *pGuid )
224 EmbedProviderFactory_Impl::~EmbedProviderFactory_Impl()
228 sal_Bool EmbedProviderFactory_Impl::registerClass()
230 HRESULT hresult;
232 o2u_attachCurrentThread();
234 hresult = CoRegisterClassObject(
235 m_guid,
236 this,
237 CLSCTX_LOCAL_SERVER,
238 REGCLS_MULTIPLEUSE,
239 &m_factoryHandle);
241 return (hresult == NOERROR);
244 sal_Bool EmbedProviderFactory_Impl::deregisterClass()
246 HRESULT hresult = CoRevokeClassObject( m_factoryHandle );
248 return (hresult == NOERROR);
251 STDMETHODIMP EmbedProviderFactory_Impl::QueryInterface(REFIID riid, void FAR* FAR* ppv)
253 if(IsEqualIID(riid, IID_IUnknown))
255 AddRef();
256 *ppv = (IUnknown*) (IClassFactory*) this;
257 return NOERROR;
259 else if (IsEqualIID(riid, IID_IClassFactory))
261 AddRef();
262 *ppv = (IClassFactory*) this;
263 return NOERROR;
266 *ppv = NULL;
267 return ResultFromScode(E_NOINTERFACE);
270 STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::AddRef()
272 return osl_incrementInterlockedCount( &m_refCount);
275 STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::Release()
277 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
278 sal_Int32 nCount = --m_refCount;
279 if ( nCount == 0 )
281 delete this;
284 return nCount;
287 STDMETHODIMP EmbedProviderFactory_Impl::CreateInstance(IUnknown FAR* punkOuter,
288 REFIID riid,
289 void FAR* FAR* ppv)
291 punkOuter = NULL;
293 IUnknown* pEmbedDocument = (IUnknown*)(IPersistStorage*)( new EmbedDocument_Impl( m_xFactory, &m_guid ) );
295 return pEmbedDocument->QueryInterface( riid, ppv );
298 STDMETHODIMP EmbedProviderFactory_Impl::LockServer( int /*fLock*/ )
300 return NOERROR;
303 // Fix strange warnings about some
304 // ATL::CAxHostWindow::QueryInterface|AddRef|Releae functions.
305 // warning C4505: 'xxx' : unreferenced local function has been removed
306 #if defined(_MSC_VER)
307 #pragma warning(disable: 4505)
308 #endif