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 <writer/WDriver.hxx>
21 #include <cppuhelper/factory.hxx>
22 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
23 
24 using namespace com::sun::star;
25 
26 using createFactoryFunc = uno::Reference<lang::XSingleServiceFactory> (*)(
27     const uno::Reference<lang::XMultiServiceFactory>& rServiceManager,
28     const OUString& rComponentName, ::cppu::ComponentInstantiation pCreateFunction,
29     const uno::Sequence<OUString>& rServiceNames, rtl_ModuleCount*);
30 
31 struct ProviderRequest
32 {
33 private:
34     uno::Reference<lang::XSingleServiceFactory> xRet;
35     uno::Reference<lang::XMultiServiceFactory> const xServiceManager;
36     OUString const sImplementationName;
37 
38 public:
ProviderRequestProviderRequest39     ProviderRequest(void* pServiceManager, sal_Char const* pImplementationName)
40         : xServiceManager(static_cast<lang::XMultiServiceFactory*>(pServiceManager))
41         , sImplementationName(OUString::createFromAscii(pImplementationName))
42     {
43     }
44 
CREATE_PROVIDERProviderRequest45     bool CREATE_PROVIDER(const OUString& Implname, const uno::Sequence<OUString>& Services,
46                          ::cppu::ComponentInstantiation Factory, createFactoryFunc creator)
47     {
48         if (!xRet.is() && (Implname == sImplementationName))
49         {
50             try
51             {
52                 xRet = creator(xServiceManager, sImplementationName, Factory, Services, nullptr);
53             }
54             catch (...)
55             {
56             }
57         }
58         return xRet.is();
59     }
60 
getProviderProviderRequest61     uno::XInterface* getProvider() const { return xRet.get(); }
62 };
63 
64 extern "C" SAL_DLLPUBLIC_EXPORT void*
connectivity_writer_component_getFactory(const sal_Char * pImplementationName,void * pServiceManager,void *)65 connectivity_writer_component_getFactory(const sal_Char* pImplementationName, void* pServiceManager,
66                                          void* /*pRegistryKey*/)
67 {
68     void* pRet = nullptr;
69     if (pServiceManager)
70     {
71         ProviderRequest aReq(pServiceManager, pImplementationName);
72 
73         aReq.CREATE_PROVIDER(connectivity::writer::ODriver::getImplementationName_Static(),
74                              connectivity::writer::ODriver::getSupportedServiceNames_Static(),
75                              connectivity::writer::ODriver_CreateInstance,
76                              ::cppu::createSingleFactory);
77 
78         if (aReq.getProvider())
79             aReq.getProvider()->acquire();
80 
81         pRet = aReq.getProvider();
82     }
83 
84     return pRet;
85 };
86 
87 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
88