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 OUString(IMPLNAME); }
62 
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
75 
76  virtual ~NumberText_Impl() override {}
77  void EnsureInitialized();
78 
79 public:
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;
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 
97  : m_bInitialized(false)
98 {
99 }
100 
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 
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 
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 
161 OUString SAL_CALL NumberText_Impl::getImplementationName() { return OUString(IMPLNAME); }
162 
163 sal_Bool SAL_CALL NumberText_Impl::supportsService(const OUString& ServiceName)
164 {
165  return cppu::supportsService(this, ServiceName);
166 }
167 
169 {
171 }
172 
174 
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[]
187  getSupportedServiceNames_NumberText_Impl, ::cppu::createSingleComponentFactory, nullptr,
188  0 },
189  { nullptr, nullptr, nullptr, nullptr, nullptr, 0 } };
190 
191 extern "C" {
192 
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: */
SAL_DLLPUBLIC_EXPORT void * numbertext_component_getFactory(sal_Char const *implName, void *xMgr, void *xRegistry)
Definition: numbertext.cxx:193
#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:179
static const struct::cppu::ImplementationEntry s_component_entries[]
Definition: numbertext.cxx:186
static Sequence< OUString > getSupportedServiceNames_Static()
Definition: numbertext.cxx:173
OUString getLanguage() const
char sal_Char
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
#define SERVICENAME
Definition: numbertext.cxx:52
NumberText_Impl & operator=(const NumberText_Impl &)=delete
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: numbertext.cxx:163
#define DBG_ASSERT(sCon, aError)
static Sequence< OUString > getSupportedServiceNames_NumberText_Impl()
Definition: numbertext.cxx:55
MetadataImporterPluginType * result
virtual OUString SAL_CALL getNumberText(const OUString &aText, const ::css::lang::Locale &rLocale) override
Definition: numbertext.cxx:125
virtual OUString SAL_CALL getImplementationName() override
Definition: numbertext.cxx:161
unsigned char sal_Bool
static OUString getImplementationName_NumberText_Impl()
Definition: numbertext.cxx:61
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
void EnsureInitialized()
Definition: numbertext.cxx:101
virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: numbertext.cxx:168
virtual css::uno::Sequence< css::lang::Locale > SAL_CALL getAvailableLanguages() override
Definition: numbertext.cxx:153
static osl::Mutex & GetNumberTextMutex()
Definition: numbertext.cxx:63
virtual ~NumberText_Impl() override
Definition: numbertext.cxx:76
OUString getCountry() const