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 <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>
29 #include <cppuhelper/implbase.hxx>
31 
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 
56 {
58  return names;
59 }
60 
61 static OUString getImplementationName_NumberText_Impl() { return IMPLNAME; }
62 
63 static osl::Mutex& GetNumberTextMutex()
64 {
65  static osl::Mutex aMutex;
66  return aMutex;
67 }
68 
69 namespace
70 {
71 class NumberText_Impl : public ::cppu::WeakImplHelper<XNumberText, XServiceInfo>
72 {
73 #if ENABLE_LIBNUMBERTEXT
74  Numbertext m_aNumberText;
75 #endif
76  bool m_bInitialized;
77 
78  virtual ~NumberText_Impl() override {}
79  void EnsureInitialized();
80 
81 public:
82  NumberText_Impl();
83  NumberText_Impl(const NumberText_Impl&) = delete;
84  NumberText_Impl& operator=(const NumberText_Impl&) = delete;
85 
86  // XServiceInfo implementation
87  virtual OUString SAL_CALL getImplementationName() override;
88  virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
89  virtual Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
90 
91  // XNumberText implementation
92  virtual OUString SAL_CALL getNumberText(const OUString& aText,
93  const ::css::lang::Locale& rLocale) override;
94  virtual css::uno::Sequence<css::lang::Locale> SAL_CALL getAvailableLanguages() override;
95 };
96 }
97 
98 NumberText_Impl::NumberText_Impl()
99  : m_bInitialized(false)
100 {
101 }
102 
103 void NumberText_Impl::EnsureInitialized()
104 {
105  if (!m_bInitialized)
106  {
107  // set this to true at the very start to prevent loops because of
108  // implicitly called functions below
109  m_bInitialized = true;
110 
111  // set default numbertext path to where those get installed
112  OUString aPhysPath;
113  OUString aURL(SvtPathOptions().GetNumbertextPath());
114  osl::FileBase::getSystemPathFromFileURL(aURL, aPhysPath);
115 #ifdef _WIN32
116  aPhysPath += "\\";
117 #else
118  aPhysPath += "/";
119 #endif
120 #if ENABLE_LIBNUMBERTEXT
121  OString path = OUStringToOString(aPhysPath, osl_getThreadTextEncoding());
122  m_aNumberText.set_prefix(path.getStr());
123 #endif
124  }
125 }
126 
127 OUString SAL_CALL NumberText_Impl::getNumberText(const OUString& rText, const Locale&
128 #if ENABLE_LIBNUMBERTEXT
129  rLocale)
130 #else
131 )
132 #endif
133 {
134  osl::MutexGuard aGuard(GetNumberTextMutex());
135  EnsureInitialized();
136 #if ENABLE_LIBNUMBERTEXT
137  // libnumbertext supports Language + Country tags (separated by "_" or "-")
138  LanguageTag aLanguageTag(rLocale);
139  OUString aCode(aLanguageTag.getLanguage());
140  OUString aCountry(aLanguageTag.getCountry());
141  if (!aCountry.isEmpty())
142  aCode += "-" + aCountry;
143  OString aLangCode(OUStringToOString(aCode, RTL_TEXTENCODING_ASCII_US));
144  OString aInput(OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
145  std::wstring aResult = Numbertext::string2wstring(aInput.getStr());
146  bool result = m_aNumberText.numbertext(aResult, aLangCode.getStr());
147  DBG_ASSERT(result, "numbertext: false");
148  OString aResult2(Numbertext::wstring2string(aResult).c_str());
149  return OUString::fromUtf8(aResult2);
150 #else
151  return rText;
152 #endif
153 }
154 
155 uno::Sequence<Locale> SAL_CALL NumberText_Impl::getAvailableLanguages()
156 {
157  osl::MutexGuard aGuard(GetNumberTextMutex());
158  // TODO
159  Sequence<css::lang::Locale> aRes;
160  return aRes;
161 }
162 
163 OUString SAL_CALL NumberText_Impl::getImplementationName() { return IMPLNAME; }
164 
165 sal_Bool SAL_CALL NumberText_Impl::supportsService(const OUString& ServiceName)
166 {
167  return cppu::supportsService(this, ServiceName);
168 }
169 
170 Sequence<OUString> SAL_CALL NumberText_Impl::getSupportedServiceNames() { return { SERVICENAME }; }
171 
177 {
178  return static_cast<::cppu::OWeakObject*>(new NumberText_Impl);
179 }
180 
181 //#### EXPORTED ### functions to allow for registration and creation of the UNO component
182 static const struct ::cppu::ImplementationEntry s_component_entries[]
184  getSupportedServiceNames_NumberText_Impl, ::cppu::createSingleComponentFactory, nullptr,
185  0 },
186  { nullptr, nullptr, nullptr, nullptr, nullptr, 0 } };
187 
188 extern "C" {
189 
190 SAL_DLLPUBLIC_EXPORT void* numbertext_component_getFactory(char const* implName, void* xMgr,
191  void* xRegistry)
192 {
193  return ::cppu::component_getFactoryHelper(implName, xMgr, xRegistry, s_component_entries);
194 }
195 }
196 
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL aURL
#define IMPLNAME
Definition: numbertext.cxx:53
static Reference< XInterface > NumberText_Impl_create(Reference< XComponentContext > const &)
Function to create a new component instance; is needed by factory helper implementation.
Definition: numbertext.cxx:176
OUString getImplementationName()
static const struct::cppu::ImplementationEntry s_component_entries[]
Definition: numbertext.cxx:183
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Mutex aMutex
#define SERVICENAME
Definition: numbertext.cxx:52
SAL_DLLPUBLIC_EXPORT void * numbertext_component_getFactory(char const *implName, void *xMgr, void *xRegistry)
Definition: numbertext.cxx:190
css::uno::Sequence< OUString > getSupportedServiceNames()
#define DBG_ASSERT(sCon, aError)
static Sequence< OUString > getSupportedServiceNames_NumberText_Impl()
Definition: numbertext.cxx:55
unsigned char sal_Bool
static OUString getImplementationName_NumberText_Impl()
Definition: numbertext.cxx:61
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
Any result
static osl::Mutex & GetNumberTextMutex()
Definition: numbertext.cxx:63