Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / embedserv / source / embed / servprov.cxx
blob739a3088f025da5971bfec347b9691d4afab30ab
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifdef _MSC_VER
20 #pragma warning(disable : 4917 4555)
21 #endif
23 #include "stdafx.h"
24 #include "servprov.hxx"
25 #include "embeddoc.hxx"
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <cppuhelper/typeprovider.hxx>
28 #include <osl/mutex.hxx>
29 #include <osl/thread.h>
31 using namespace com::sun::star;
33 const GUID* guidList[ SUPPORTED_FACTORIES_NUM ] = {
34 &OID_WriterTextServer,
35 &OID_WriterOASISTextServer,
36 &OID_CalcServer,
37 &OID_CalcOASISServer,
38 &OID_DrawingServer,
39 &OID_DrawingOASISServer,
40 &OID_PresentationServer,
41 &OID_PresentationOASISServer,
42 &OID_MathServer,
43 &OID_MathOASISServer
46 class CurThreadData
48 public:
49 CurThreadData();
50 virtual ~CurThreadData();
52 sal_Bool SAL_CALL setData(void *pData);
54 void* SAL_CALL getData();
56 protected:
57 oslThreadKey m_hKey;
60 CurThreadData::CurThreadData()
62 m_hKey = osl_createThreadKey( (oslThreadKeyCallbackFunction)NULL );
65 CurThreadData::~CurThreadData()
67 osl_destroyThreadKey(m_hKey);
70 sal_Bool CurThreadData::setData(void *pData)
72 OSL_ENSURE( m_hKey, "No thread key!\n" );
73 return (osl_setThreadKeyData(m_hKey, pData));
76 void *CurThreadData::getData()
78 OSL_ENSURE( m_hKey, "No thread key!\n" );
79 return (osl_getThreadKeyData(m_hKey));
83 // CoInitializeEx *
84 typedef DECLSPEC_IMPORT HRESULT (STDAPICALLTYPE *ptrCoInitEx)( LPVOID, DWORD);
85 // CoInitialize *
86 typedef DECLSPEC_IMPORT HRESULT (STDAPICALLTYPE *ptrCoInit)( LPVOID);
88 void o2u_attachCurrentThread()
90 static CurThreadData oleThreadData;
92 if ( oleThreadData.getData() != 0 )
94 HINSTANCE inst= LoadLibrary( _T("ole32.dll"));
95 if( inst )
97 HRESULT hr;
98 ptrCoInitEx initFuncEx= (ptrCoInitEx)GetProcAddress( inst, _T("CoInitializeEx"));
99 if( initFuncEx)
100 hr= initFuncEx( NULL, COINIT_MULTITHREADED);
101 else
103 ptrCoInit initFunc= (ptrCoInit)GetProcAddress( inst,_T("CoInitialize"));
104 if( initFunc)
105 hr= initFunc( NULL);
108 oleThreadData.setData((void*)sal_True);
113 //===============================================================================
114 // EmbedServer_Impl
116 EmbedServer_Impl::EmbedServer_Impl( const uno::Reference<lang::XMultiServiceFactory>& xFactory):
117 m_xFactory( xFactory)
119 for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
121 m_pOLEFactories[nInd] = new EmbedProviderFactory_Impl( m_xFactory, guidList[nInd] );
122 m_pOLEFactories[nInd]->registerClass();
126 EmbedServer_Impl::~EmbedServer_Impl()
128 for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
130 if ( m_pOLEFactories[nInd] )
131 m_pOLEFactories[nInd]->deregisterClass();
135 // XInterface --------------------------------------------------
136 uno::Any SAL_CALL
137 EmbedServer_Impl::queryInterface(
138 const uno::Type& aType )
139 throw(
140 uno::RuntimeException
143 uno::Any a=
144 ::cppu::queryInterface(
145 aType, static_cast<lang::XTypeProvider*>(this));
146 if( a == uno::Any())
147 return OWeakObject::queryInterface( aType);
148 else
149 return a;
152 void SAL_CALL EmbedServer_Impl::acquire( ) throw(uno::RuntimeException)
154 OWeakObject::acquire();
157 void SAL_CALL EmbedServer_Impl::release( ) throw (uno::RuntimeException)
159 OWeakObject::release();
163 // XTypeProvider --------------------------------------------------
164 uno::Sequence< uno::Type > SAL_CALL
165 EmbedServer_Impl::getTypes( )
166 throw(
167 uno::RuntimeException
170 static ::cppu::OTypeCollection *pCollection = 0;
171 if( ! pCollection )
173 ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
174 if( ! pCollection )
176 static ::cppu::OTypeCollection collection(
177 getCppuType(
178 reinterpret_cast<uno::Reference< uno::XWeak>*>(0)),
179 getCppuType(
180 reinterpret_cast<
181 uno::Reference< lang::XTypeProvider>*>(0)));
182 pCollection = &collection;
185 return (*pCollection).getTypes();
188 uno::Sequence< sal_Int8 > SAL_CALL EmbedServer_Impl::getImplementationId() throw(uno::RuntimeException)
190 static ::cppu::OImplementationId *pId = 0;
191 if( ! pId )
193 ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
194 if( ! pId )
196 static ::cppu::OImplementationId id( sal_False );
197 pId = &id;
200 return (*pId).getImplementationId();
203 //===============================================================================
204 // EmbedProviderFactory_Impl
206 EmbedProviderFactory_Impl::EmbedProviderFactory_Impl(const uno::Reference<lang::XMultiServiceFactory>& xFactory, const GUID* pGuid)
207 : m_refCount( 0L )
208 , m_xFactory( xFactory )
209 , m_guid( *pGuid )
213 EmbedProviderFactory_Impl::~EmbedProviderFactory_Impl()
217 sal_Bool EmbedProviderFactory_Impl::registerClass()
219 HRESULT hresult;
221 o2u_attachCurrentThread();
223 hresult = CoRegisterClassObject(
224 m_guid,
225 this,
226 CLSCTX_LOCAL_SERVER,
227 REGCLS_MULTIPLEUSE,
228 &m_factoryHandle);
230 return (hresult == NOERROR);
233 sal_Bool EmbedProviderFactory_Impl::deregisterClass()
235 HRESULT hresult = CoRevokeClassObject( m_factoryHandle );
237 return (hresult == NOERROR);
240 STDMETHODIMP EmbedProviderFactory_Impl::QueryInterface(REFIID riid, void FAR* FAR* ppv)
242 if(IsEqualIID(riid, IID_IUnknown))
244 AddRef();
245 *ppv = (IUnknown*) (IClassFactory*) this;
246 return NOERROR;
248 else if (IsEqualIID(riid, IID_IClassFactory))
250 AddRef();
251 *ppv = (IClassFactory*) this;
252 return NOERROR;
255 *ppv = NULL;
256 return ResultFromScode(E_NOINTERFACE);
259 STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::AddRef()
261 return osl_atomic_increment( &m_refCount);
264 STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::Release()
266 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
267 sal_Int32 nCount = --m_refCount;
268 if ( nCount == 0 )
270 delete this;
273 return nCount;
276 STDMETHODIMP EmbedProviderFactory_Impl::CreateInstance(IUnknown FAR* punkOuter,
277 REFIID riid,
278 void FAR* FAR* ppv)
280 punkOuter = NULL;
282 IUnknown* pEmbedDocument = (IUnknown*)(IPersistStorage*)( new EmbedDocument_Impl( m_xFactory, &m_guid ) );
284 return pEmbedDocument->QueryInterface( riid, ppv );
287 STDMETHODIMP EmbedProviderFactory_Impl::LockServer( int /*fLock*/ )
289 return NOERROR;
292 // Fix strange warnings about some
293 // ATL::CAxHostWindow::QueryInterface|AddRef|Releae functions.
294 // warning C4505: 'xxx' : unreferenced local function has been removed
295 #if defined(_MSC_VER)
296 #pragma warning(disable: 4505)
297 #endif
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */