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 <config_libnumbertext.h>
21 #include <iostream>
22 
23 #include <osl/file.hxx>
24 #include <tools/debug.hxx>
25 
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>
31 
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>
38 
39 #include <sal/macros.h>
40 
41 #if ENABLE_LIBNUMBERTEXT
42 #include <Numbertext.hxx>
43 #endif
44 
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;
51 
52 #define SERVICENAME "com.sun.star.linguistic2.NumberText"
53 #define IMPLNAME "com.sun.star.lingu2.NumberText"
54 
getSupportedServiceNames_NumberText_Impl()55 static Sequence<OUString> getSupportedServiceNames_NumberText_Impl()
56 {
57     Sequence<OUString> names{ SERVICENAME };
58     return names;
59 }
60 
getImplementationName_NumberText_Impl()61 static OUString getImplementationName_NumberText_Impl() { return IMPLNAME; }
62 
GetNumberTextMutex()63 static osl::Mutex& GetNumberTextMutex()
64 {
65     static osl::Mutex aMutex;
66     return aMutex;
67 }
68 
69 class NumberText_Impl : public ::cppu::WeakImplHelper<XNumberText, XServiceInfo>
70 {
71 #if ENABLE_LIBNUMBERTEXT
72     Numbertext m_aNumberText;
73 #endif
74     bool m_bInitialized;
75 
~NumberText_Impl()76     virtual ~NumberText_Impl() override {}
77     void EnsureInitialized();
78 
79 public:
80     NumberText_Impl();
81     NumberText_Impl(const NumberText_Impl&) = delete;
82     NumberText_Impl& operator=(const NumberText_Impl&) = delete;
83 
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();
89 
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;
94 };
95 
NumberText_Impl()96 NumberText_Impl::NumberText_Impl()
97     : m_bInitialized(false)
98 {
99 }
100 
EnsureInitialized()101 void NumberText_Impl::EnsureInitialized()
102 {
103     if (!m_bInitialized)
104     {
105         // set this to true at the very start to prevent loops because of
106         // implicitly called functions below
107         m_bInitialized = true;
108 
109         // set default numbertext path to where those get installed
110         OUString aPhysPath;
111         OUString aURL(SvtPathOptions().GetNumbertextPath());
112         osl::FileBase::getSystemPathFromFileURL(aURL, aPhysPath);
113 #ifdef _WIN32
114         aPhysPath += "\\";
115 #else
116         aPhysPath += "/";
117 #endif
118 #if ENABLE_LIBNUMBERTEXT
119         OString path = OUStringToOString(aPhysPath, osl_getThreadTextEncoding());
120         m_aNumberText.set_prefix(path.getStr());
121 #endif
122     }
123 }
124 
getNumberText(const OUString & rText,const Locale & rLocale)125 OUString SAL_CALL NumberText_Impl::getNumberText(const OUString& rText, const Locale&
126 #if ENABLE_LIBNUMBERTEXT
127                                                                             rLocale)
128 #else
129 )
130 #endif
131 {
132     osl::MutexGuard aGuard(GetNumberTextMutex());
133     EnsureInitialized();
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);
148 #else
149     return rText;
150 #endif
151 }
152 
getAvailableLanguages()153 uno::Sequence<Locale> SAL_CALL NumberText_Impl::getAvailableLanguages()
154 {
155     osl::MutexGuard aGuard(GetNumberTextMutex());
156     // TODO
157     Sequence<css::lang::Locale> aRes;
158     return aRes;
159 }
160 
getImplementationName()161 OUString SAL_CALL NumberText_Impl::getImplementationName() { return IMPLNAME; }
162 
supportsService(const OUString & ServiceName)163 sal_Bool SAL_CALL NumberText_Impl::supportsService(const OUString& ServiceName)
164 {
165     return cppu::supportsService(this, ServiceName);
166 }
167 
getSupportedServiceNames()168 Sequence<OUString> SAL_CALL NumberText_Impl::getSupportedServiceNames()
169 {
170     return getSupportedServiceNames_Static();
171 }
172 
getSupportedServiceNames_Static()173 Sequence<OUString> NumberText_Impl::getSupportedServiceNames_Static() { return { SERVICENAME }; }
174 
175 /**
176  * Function to create a new component instance; is needed by factory helper implementation.
177  * @param xMgr service manager to if the components needs other component instances
178  */
NumberText_Impl_create(Reference<XComponentContext> const &)179 static Reference<XInterface> NumberText_Impl_create(Reference<XComponentContext> const&)
180 {
181     return static_cast<::cppu::OWeakObject*>(new NumberText_Impl);
182 }
183 
184 //#### EXPORTED ### functions to allow for registration and creation of the UNO component
185 static const struct ::cppu::ImplementationEntry s_component_entries[]
186     = { { NumberText_Impl_create, getImplementationName_NumberText_Impl,
187           getSupportedServiceNames_NumberText_Impl, ::cppu::createSingleComponentFactory, nullptr,
188           0 },
189         { nullptr, nullptr, nullptr, nullptr, nullptr, 0 } };
190 
191 extern "C" {
192 
numbertext_component_getFactory(sal_Char const * implName,void * xMgr,void * xRegistry)193 SAL_DLLPUBLIC_EXPORT void* numbertext_component_getFactory(sal_Char const* implName, void* xMgr,
194                                                            void* xRegistry)
195 {
196     return ::cppu::component_getFactoryHelper(implName, xMgr, xRegistry, s_component_entries);
197 }
198 }
199 
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
201