LibreOffice Module lingucomponent (master) 1
numbertext.cxx
Go to the documentation of this file.
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 <mutex>
21
22#include <osl/file.hxx>
23#include <tools/debug.hxx>
25
26#include <sal/config.h>
30
32#include <com/sun/star/lang/XServiceInfo.hpp>
33#include <com/sun/star/linguistic2/XNumberText.hpp>
35#include <osl/thread.h>
36
37#include <Numbertext.hxx>
38
39using namespace ::osl;
40using namespace ::cppu;
41using namespace ::com::sun::star;
42using namespace ::com::sun::star::uno;
43using namespace ::com::sun::star::lang;
44using namespace ::com::sun::star::linguistic2;
45
46static std::mutex& GetNumberTextMutex()
47{
48 static std::mutex aMutex;
49 return aMutex;
50}
51
52namespace
53{
54class NumberText_Impl : public ::cppu::WeakImplHelper<XNumberText, XServiceInfo>
55{
56 Numbertext m_aNumberText;
57 bool m_bInitialized;
58
59 virtual ~NumberText_Impl() override {}
60 void EnsureInitialized();
61
62public:
63 NumberText_Impl();
64 NumberText_Impl(const NumberText_Impl&) = delete;
65 NumberText_Impl& operator=(const NumberText_Impl&) = delete;
66
67 // XServiceInfo implementation
68 virtual OUString SAL_CALL getImplementationName() override;
69 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
70 virtual Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
71
72 // XNumberText implementation
73 virtual OUString SAL_CALL getNumberText(const OUString& aText,
74 const ::css::lang::Locale& rLocale) override;
75 virtual css::uno::Sequence<css::lang::Locale> SAL_CALL getAvailableLanguages() override;
76};
77}
78
79NumberText_Impl::NumberText_Impl()
80 : m_bInitialized(false)
81{
82}
83
84void NumberText_Impl::EnsureInitialized()
85{
86 if (m_bInitialized)
87 return;
88
89 // set this to true at the very start to prevent loops because of
90 // implicitly called functions below
91 m_bInitialized = true;
92
93 // set default numbertext path to where those get installed
94 OUString aPhysPath;
95 OUString aURL(SvtPathOptions().GetNumbertextPath());
96 osl::FileBase::getSystemPathFromFileURL(aURL, aPhysPath);
97#ifdef _WIN32
98 aPhysPath += "\\";
99 const rtl_TextEncoding eEnc = RTL_TEXTENCODING_UTF8;
100#else
101 aPhysPath += "/";
102 const rtl_TextEncoding eEnc = osl_getThreadTextEncoding();
103#endif
104 OString path = OUStringToOString(aPhysPath, eEnc);
105 m_aNumberText.set_prefix(std::string(path));
106}
107
108OUString SAL_CALL NumberText_Impl::getNumberText(const OUString& rText, const Locale& rLocale)
109{
110 std::scoped_lock aGuard(GetNumberTextMutex());
111 EnsureInitialized();
112 // libnumbertext supports Language + Country tags (separated by "_" or "-")
113 LanguageTag aLanguageTag(rLocale);
114 OUString aCode(aLanguageTag.getLanguage());
115 OUString aCountry(aLanguageTag.getCountry());
116 OUString aScript(aLanguageTag.getScript());
117 if (!aScript.isEmpty())
118 aCode += "-" + aScript;
119 if (!aCountry.isEmpty())
120 aCode += "-" + aCountry;
121 OString aLangCode(OUStringToOString(aCode, RTL_TEXTENCODING_ASCII_US));
122#if defined(_WIN32)
123 std::wstring sResult(o3tl::toW(rText.getStr()));
124#else
125 OString aInput(OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
126 std::wstring sResult = Numbertext::string2wstring(std::string(aInput));
127#endif
128 bool result = m_aNumberText.numbertext(sResult, std::string(aLangCode));
129 DBG_ASSERT(result, "numbertext: false");
130#if defined(_WIN32)
131 OUString aResult(o3tl::toU(sResult));
132#else
133 OUString aResult = OUString::fromUtf8(Numbertext::wstring2string(sResult));
134#endif
135 return aResult;
136}
137
138uno::Sequence<Locale> SAL_CALL NumberText_Impl::getAvailableLanguages()
139{
140 std::scoped_lock aGuard(GetNumberTextMutex());
141 // TODO
142 Sequence<css::lang::Locale> aRes;
143 return aRes;
144}
145
146OUString SAL_CALL NumberText_Impl::getImplementationName()
147{
148 return "com.sun.star.lingu2.NumberText";
149}
150
151sal_Bool SAL_CALL NumberText_Impl::supportsService(const OUString& ServiceName)
152{
153 return cppu::supportsService(this, ServiceName);
154}
155
156Sequence<OUString> SAL_CALL NumberText_Impl::getSupportedServiceNames()
157{
158 return { "com.sun.star.linguistic2.NumberText" };
159}
160
161extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
163 css::uno::Sequence<css::uno::Any> const&)
164{
165 return cppu::acquire(new NumberText_Impl());
166}
167
168/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define DBG_ASSERT(sCon, aError)
URL aURL
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
std::mutex aMutex
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * lingucomponent_NumberText_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Definition: numbertext.cxx:162
static std::mutex & GetNumberTextMutex()
Definition: numbertext.cxx:46
unsigned char sal_Bool
Any result