1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <config_libnumbertext.h>
23 #include <osl/file.hxx>
24 #include <tools/debug.hxx>
26 #include <sal/config.h>
27 #include <cppuhelper/factory.hxx>
28 #include <cppuhelper/implementationentry.hxx>
29 #include <cppuhelper/implbase.hxx>
30 #include <cppuhelper/supportsservice.hxx>
32 #include <i18nlangtag/languagetag.hxx>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/linguistic2/XNumberText.hpp>
36 #include <unotools/pathoptions.hxx>
37 #include <osl/thread.h>
39 #include <sal/macros.h>
41 #if ENABLE_LIBNUMBERTEXT
42 #include <Numbertext.hxx>
45 using namespace ::osl
;
46 using namespace ::cppu
;
47 using namespace ::com::sun::star
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::lang
;
50 using namespace ::com::sun::star::linguistic2
;
52 #define SERVICENAME "com.sun.star.linguistic2.NumberText"
53 #define IMPLNAME "com.sun.star.lingu2.NumberText"
55 static Sequence
<OUString
> getSupportedServiceNames_NumberText_Impl()
57 Sequence
<OUString
> names
{ SERVICENAME
};
61 static OUString
getImplementationName_NumberText_Impl() { return OUString(IMPLNAME
); }
63 static osl::Mutex
& GetNumberTextMutex()
65 static osl::Mutex aMutex
;
69 class NumberText_Impl
: public ::cppu::WeakImplHelper
<XNumberText
, XServiceInfo
>
71 #if ENABLE_LIBNUMBERTEXT
72 Numbertext m_aNumberText
;
76 virtual ~NumberText_Impl() override
{}
77 void EnsureInitialized();
81 NumberText_Impl(const NumberText_Impl
&) = delete;
82 NumberText_Impl
& operator=(const NumberText_Impl
&) = delete;
84 // XServiceInfo implementation
85 virtual OUString SAL_CALL
getImplementationName() override
;
86 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
87 virtual Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
;
88 static Sequence
<OUString
> getSupportedServiceNames_Static();
90 // XNumberText implementation
91 virtual OUString SAL_CALL
getNumberText(const OUString
& aText
,
92 const ::css::lang::Locale
& rLocale
) override
;
93 virtual css::uno::Sequence
<css::lang::Locale
> SAL_CALL
getAvailableLanguages() override
;
96 NumberText_Impl::NumberText_Impl()
97 : m_bInitialized(false)
101 void NumberText_Impl::EnsureInitialized()
105 // set this to true at the very start to prevent loops because of
106 // implicitly called functions below
107 m_bInitialized
= true;
109 // set default numbertext path to where those get installed
111 OUString
aURL(SvtPathOptions().GetNumbertextPath());
112 osl::FileBase::getSystemPathFromFileURL(aURL
, aPhysPath
);
118 #if ENABLE_LIBNUMBERTEXT
119 OString path
= OUStringToOString(aPhysPath
, osl_getThreadTextEncoding());
120 m_aNumberText
.set_prefix(path
.getStr());
125 OUString SAL_CALL
NumberText_Impl::getNumberText(const OUString
& rText
, const Locale
&
126 #if ENABLE_LIBNUMBERTEXT
132 osl::MutexGuard
aGuard(GetNumberTextMutex());
134 #if ENABLE_LIBNUMBERTEXT
135 // libnumbertext supports Language + Country tags (separated by "_" or "-")
136 LanguageTag
aLanguageTag(rLocale
);
137 OUString
aCode(aLanguageTag
.getLanguage());
138 OUString
aCountry(aLanguageTag
.getCountry());
139 if (!aCountry
.isEmpty())
140 aCode
+= "-" + aCountry
;
141 OString
aLangCode(OUStringToOString(aCode
, RTL_TEXTENCODING_ASCII_US
));
142 OString
aInput(OUStringToOString(rText
, RTL_TEXTENCODING_UTF8
));
143 std::wstring aResult
= Numbertext::string2wstring(aInput
.getStr());
144 bool result
= m_aNumberText
.numbertext(aResult
, aLangCode
.getStr());
145 DBG_ASSERT(result
, "numbertext: false");
146 OString
aResult2(Numbertext::wstring2string(aResult
).c_str());
147 return OUString::fromUtf8(aResult2
);
153 uno::Sequence
<Locale
> SAL_CALL
NumberText_Impl::getAvailableLanguages()
155 osl::MutexGuard
aGuard(GetNumberTextMutex());
157 Sequence
<css::lang::Locale
> aRes
;
161 OUString SAL_CALL
NumberText_Impl::getImplementationName() { return OUString(IMPLNAME
); }
163 sal_Bool SAL_CALL
NumberText_Impl::supportsService(const OUString
& ServiceName
)
165 return cppu::supportsService(this, ServiceName
);
168 Sequence
<OUString
> SAL_CALL
NumberText_Impl::getSupportedServiceNames()
170 return getSupportedServiceNames_Static();
173 Sequence
<OUString
> NumberText_Impl::getSupportedServiceNames_Static()
175 OUString
aName(SERVICENAME
);
176 return Sequence
<OUString
>(&aName
, 1);
180 * Function to create a new component instance; is needed by factory helper implementation.
181 * @param xMgr service manager to if the components needs other component instances
183 static Reference
<XInterface
> NumberText_Impl_create(Reference
<XComponentContext
> const&)
185 return static_cast<::cppu::OWeakObject
*>(new NumberText_Impl
);
188 //#### EXPORTED ### functions to allow for registration and creation of the UNO component
189 static const struct ::cppu::ImplementationEntry s_component_entries
[]
190 = { { NumberText_Impl_create
, getImplementationName_NumberText_Impl
,
191 getSupportedServiceNames_NumberText_Impl
, ::cppu::createSingleComponentFactory
, nullptr,
193 { nullptr, nullptr, nullptr, nullptr, nullptr, 0 } };
197 SAL_DLLPUBLIC_EXPORT
void* numbertext_component_getFactory(sal_Char
const* implName
, void* xMgr
,
200 return ::cppu::component_getFactoryHelper(implName
, xMgr
, xRegistry
, s_component_entries
);
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */