Fix typo
[LibreOffice.git] / net_ure / source / bootstrap / bootstrap.cxx
blob4f4f45355a8a53eb61b75a539ac7e2cdd95702e4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
8 */
10 #include <string_view>
12 #include <bridges/net_uno/net_context.hxx>
13 #include <cppuhelper/bootstrap.hxx>
14 #include <o3tl/string_view.hxx>
15 #include <rtl/bootstrap.hxx>
16 #include <rtl/ustring.hxx>
17 #include <sal/log.hxx>
18 #include <uno/mapping.hxx>
20 #include <com/sun/star/lang/XComponent.hpp>
21 #include <com/sun/star/uno/XComponentContext.hpp>
23 using namespace ::com::sun::star;
24 using namespace ::com::sun::star::uno;
26 namespace net_uno
28 extern "C" {
29 SAL_DLLPUBLIC_EXPORT IntPtr bootstrap(const Context aContext)
31 try
33 // Bootstrap UNO and start a LibreOffice process if needed
34 Reference<XComponentContext> xContext(::cppu::bootstrap());
36 // Get a mapping between the C++ and .NET environments
37 Environment cpp_env(CPPU_CURRENT_LANGUAGE_BINDING_NAME);
38 Environment net_env(u"" UNO_LB_NET ""_ustr, new Context(aContext));
39 Mapping mapping(cpp_env.get(), net_env.get());
40 if (!mapping.is())
42 Reference<lang::XComponent> xComp(xContext, UNO_QUERY);
43 if (xComp.is())
45 xComp->dispose();
48 throw RuntimeException(u"could not get mapping between C++ and .NET"_ustr);
51 // Map the XComponentContext to a .NET proxy
52 return mapping.mapInterface(xContext.get(), cppu::UnoType<decltype(xContext)>::get());
54 catch (const Exception& exc)
56 SAL_WARN("net", ".NET bootstrap error: " << exc.Message);
57 aContext.throwError((u"" SAL_WHERE ""_ustr).getStr(), exc.Message.getStr());
58 return nullptr;
62 SAL_DLLPUBLIC_EXPORT IntPtr defaultBootstrap_InitialComponentContext(const sal_Unicode* sIniFile,
63 const sal_Unicode* sParams,
64 const Context aContext)
66 try
68 // Set bootstrap parameters, merged into a single string (at least for now)
69 // to avoid dealing with lifetimes and memory of multiple strings
70 if (sParams)
72 OUString paramsStr(sParams);
73 for (size_t i = 0; i != std::u16string_view::npos;)
75 std::u16string_view name(o3tl::getToken(paramsStr, u'|', i));
76 OUString key(name.substr(0, name.find_first_of('=')));
77 OUString val(name.substr(key.getLength() + 1));
78 ::rtl::Bootstrap::set(key, val);
82 // Bootstrap UNO
83 Reference<XComponentContext> xContext;
84 if (sIniFile)
86 xContext = ::cppu::defaultBootstrap_InitialComponentContext(OUString(sIniFile));
88 else
90 xContext = ::cppu::defaultBootstrap_InitialComponentContext();
93 // Get a mapping between the C++ and .NET environments
94 Environment cpp_env(CPPU_CURRENT_LANGUAGE_BINDING_NAME);
95 Environment net_env(u"" UNO_LB_NET ""_ustr, new Context(aContext));
96 Mapping mapping(cpp_env.get(), net_env.get());
97 if (!mapping.is())
99 Reference<lang::XComponent> xComp(xContext, UNO_QUERY);
100 if (xComp.is())
102 xComp->dispose();
105 throw RuntimeException(u"could not get mapping between C++ and .NET"_ustr);
108 // Map the XComponentContext to a .NET proxy
109 return mapping.mapInterface(xContext.get(), cppu::UnoType<decltype(xContext)>::get());
111 catch (const Exception& exc)
113 SAL_WARN("net", ".NET bootstrap error: " << exc.Message);
114 aContext.throwError((u"" SAL_WHERE ""_ustr).getStr(), exc.Message.getStr());
115 return nullptr;
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */