Line data Source code
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 <iostream>
21 :
22 : #include <tools/debug.hxx>
23 :
24 : #include <sal/config.h>
25 : #include <cppuhelper/factory.hxx>
26 : #include <cppuhelper/implementationentry.hxx>
27 : #include <cppuhelper/implbase2.hxx>
28 : #include <cppuhelper/supportsservice.hxx>
29 :
30 : #include <simpleguesser.hxx>
31 : #include <guess.hxx>
32 :
33 : #include <com/sun/star/registry/XRegistryKey.hpp>
34 : #include <com/sun/star/lang/XServiceInfo.hpp>
35 : #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
36 : #include <unotools/pathoptions.hxx>
37 : #include <unotools/localfilehelper.hxx>
38 : #include <osl/thread.h>
39 :
40 : #include <sal/macros.h>
41 :
42 : #ifdef SYSTEM_LIBEXTTEXTCAT
43 : #include <libexttextcat/textcat.h>
44 : #else
45 : #include <textcat.h>
46 : #endif
47 :
48 : using namespace ::rtl;
49 : using namespace ::osl;
50 : using namespace ::cppu;
51 : using namespace ::com::sun::star;
52 : using namespace ::com::sun::star::uno;
53 : using namespace ::com::sun::star::lang;
54 : using namespace ::com::sun::star::linguistic2;
55 :
56 : #define SERVICENAME "com.sun.star.linguistic2.LanguageGuessing"
57 : #define IMPLNAME "com.sun.star.lingu2.LanguageGuessing"
58 :
59 0 : static Sequence< OUString > getSupportedServiceNames_LangGuess_Impl()
60 : {
61 0 : Sequence<OUString> names(1);
62 0 : names[0] = SERVICENAME;
63 0 : return names;
64 : }
65 :
66 0 : static OUString getImplementationName_LangGuess_Impl()
67 : {
68 0 : return OUString( IMPLNAME );
69 : }
70 :
71 0 : static osl::Mutex & GetLangGuessMutex()
72 : {
73 0 : static osl::Mutex aMutex;
74 0 : return aMutex;
75 : }
76 :
77 : class LangGuess_Impl :
78 : public ::cppu::WeakImplHelper2<
79 : XLanguageGuessing,
80 : XServiceInfo >
81 : {
82 : SimpleGuesser m_aGuesser;
83 : bool m_bInitialized;
84 : css::uno::Reference< css::uno::XComponentContext > m_xContext;
85 :
86 : LangGuess_Impl( const LangGuess_Impl & ); // not defined
87 : LangGuess_Impl & operator =( const LangGuess_Impl & ); // not defined
88 :
89 0 : virtual ~LangGuess_Impl() {}
90 : void EnsureInitialized();
91 :
92 : public:
93 : explicit LangGuess_Impl(css::uno::Reference< css::uno::XComponentContext > const & rxContext);
94 :
95 : // XServiceInfo implementation
96 : virtual OUString SAL_CALL getImplementationName( ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
97 : virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
98 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
99 : static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static( );
100 :
101 : // XLanguageGuessing implementation
102 : virtual ::com::sun::star::lang::Locale SAL_CALL guessPrimaryLanguage( const OUString& aText, ::sal_Int32 nStartPos, ::sal_Int32 nLen ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
103 : virtual void SAL_CALL disableLanguages( const ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale >& aLanguages ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
104 : virtual void SAL_CALL enableLanguages( const ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale >& aLanguages ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
105 : virtual ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > SAL_CALL getAvailableLanguages( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
106 : virtual ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > SAL_CALL getEnabledLanguages( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
107 : virtual ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > SAL_CALL getDisabledLanguages( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
108 :
109 : // implementation specific
110 : void SetFingerPrintsDB( const OUString &fileName ) throw (RuntimeException);
111 :
112 : static const OUString & SAL_CALL getImplementationName_Static() throw();
113 :
114 : };
115 :
116 0 : LangGuess_Impl::LangGuess_Impl(css::uno::Reference< css::uno::XComponentContext > const & rxContext) :
117 : m_bInitialized( false ),
118 0 : m_xContext( rxContext )
119 : {
120 0 : }
121 :
122 0 : void LangGuess_Impl::EnsureInitialized()
123 : {
124 0 : if (!m_bInitialized)
125 : {
126 : // set this to true at the very start to prevent loops because of
127 : // implicitly called functions below
128 0 : m_bInitialized = true;
129 :
130 : // set default fingerprint path to where those get installed
131 0 : OUString aPhysPath;
132 0 : OUString aURL( SvtPathOptions().GetFingerprintPath() );
133 0 : utl::LocalFileHelper::ConvertURLToPhysicalName( aURL, aPhysPath );
134 : #ifdef WNT
135 : aPhysPath = aPhysPath + OUString(static_cast<sal_Unicode>('\\'));
136 : #else
137 0 : aPhysPath = aPhysPath + OUString(static_cast<sal_Unicode>('/'));
138 : #endif
139 :
140 0 : SetFingerPrintsDB( aPhysPath );
141 :
142 : #if !defined(EXTTEXTCAT_VERSION_MAJOR)
143 :
144 : // disable currently not functional languages...
145 : struct LangCountry
146 : {
147 : const char *pLang;
148 : const char *pCountry;
149 : };
150 : LangCountry aDisable[] =
151 : {
152 : // not functional in modified libtextcat, but fixed in >= libexttextcat 3.1.0
153 : // which is the first with EXTTEXTCAT_VERSION_MAJOR defined
154 : {"sco", ""}, {"zh", "CN"}, {"zh", "TW"}, {"ja", ""}, {"ko", ""},
155 : {"ka", ""}, {"hi", ""}, {"mr", ""}, {"ne", ""}, {"sa", ""},
156 : {"ta", ""}, {"th", ""}, {"qu", ""}, {"yi", ""}
157 : };
158 : sal_Int32 nNum = SAL_N_ELEMENTS(aDisable);
159 : Sequence< Locale > aDisableSeq( nNum );
160 : Locale *pDisableSeq = aDisableSeq.getArray();
161 : for (sal_Int32 i = 0; i < nNum; ++i)
162 : {
163 : Locale aLocale;
164 : aLocale.Language = OUString::createFromAscii( aDisable[i].pLang );
165 : aLocale.Country = OUString::createFromAscii( aDisable[i].pCountry );
166 : pDisableSeq[i] = aLocale;
167 : }
168 : disableLanguages( aDisableSeq );
169 : DBG_ASSERT( nNum == getDisabledLanguages().getLength(), "size mismatch" );
170 : #endif
171 : }
172 0 : }
173 :
174 0 : Locale SAL_CALL LangGuess_Impl::guessPrimaryLanguage(
175 : const OUString& rText,
176 : ::sal_Int32 nStartPos,
177 : ::sal_Int32 nLen )
178 : throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
179 : {
180 0 : osl::MutexGuard aGuard( GetLangGuessMutex() );
181 :
182 0 : EnsureInitialized();
183 :
184 0 : lang::Locale aRes;
185 0 : if (nStartPos >=0 && nLen >= 0 && nStartPos + nLen <= rText.getLength())
186 : {
187 0 : OString o( OUStringToOString( rText.copy(nStartPos, nLen), RTL_TEXTENCODING_UTF8 ) );
188 0 : Guess g = m_aGuesser.GuessPrimaryLanguage(o.getStr());
189 0 : aRes.Language = OUString::createFromAscii( g.GetLanguage().c_str() );
190 0 : aRes.Country = OUString::createFromAscii( g.GetCountry().c_str() );
191 : }
192 : else
193 0 : throw lang::IllegalArgumentException();
194 :
195 0 : return aRes;
196 : }
197 :
198 : #define DEFAULT_CONF_FILE_NAME "fpdb.conf"
199 :
200 0 : void LangGuess_Impl::SetFingerPrintsDB(
201 : const OUString &filePath )
202 : throw (RuntimeException)
203 : {
204 : //! text encoding for file name / path needs to be in the same encoding the OS uses
205 0 : OString path = OUStringToOString( filePath, osl_getThreadTextEncoding() );
206 0 : OString conf_file_name( DEFAULT_CONF_FILE_NAME );
207 0 : OString conf_file_path(path);
208 0 : conf_file_path += conf_file_name;
209 :
210 0 : m_aGuesser.SetDBPath((const char*)conf_file_path.getStr(), (const char*)path.getStr());
211 0 : }
212 :
213 0 : uno::Sequence< Locale > SAL_CALL LangGuess_Impl::getAvailableLanguages( )
214 : throw (uno::RuntimeException, std::exception)
215 : {
216 0 : osl::MutexGuard aGuard( GetLangGuessMutex() );
217 :
218 0 : EnsureInitialized();
219 :
220 0 : Sequence< com::sun::star::lang::Locale > aRes;
221 0 : vector<Guess> gs = m_aGuesser.GetAllManagedLanguages();
222 0 : aRes.realloc(gs.size());
223 :
224 0 : com::sun::star::lang::Locale *pRes = aRes.getArray();
225 :
226 0 : for(size_t i = 0; i < gs.size() ; i++ ){
227 0 : com::sun::star::lang::Locale current_aRes;
228 0 : current_aRes.Language = OUString::createFromAscii( gs[i].GetLanguage().c_str() );
229 0 : current_aRes.Country = OUString::createFromAscii( gs[i].GetCountry().c_str() );
230 0 : pRes[i] = current_aRes;
231 0 : }
232 :
233 0 : return aRes;
234 : }
235 :
236 0 : uno::Sequence< Locale > SAL_CALL LangGuess_Impl::getEnabledLanguages( )
237 : throw (uno::RuntimeException, std::exception)
238 : {
239 0 : osl::MutexGuard aGuard( GetLangGuessMutex() );
240 :
241 0 : EnsureInitialized();
242 :
243 0 : Sequence< com::sun::star::lang::Locale > aRes;
244 0 : vector<Guess> gs = m_aGuesser.GetAvailableLanguages();
245 0 : aRes.realloc(gs.size());
246 :
247 0 : com::sun::star::lang::Locale *pRes = aRes.getArray();
248 :
249 0 : for(size_t i = 0; i < gs.size() ; i++ ){
250 0 : com::sun::star::lang::Locale current_aRes;
251 0 : current_aRes.Language = OUString::createFromAscii( gs[i].GetLanguage().c_str() );
252 0 : current_aRes.Country = OUString::createFromAscii( gs[i].GetCountry().c_str() );
253 0 : pRes[i] = current_aRes;
254 0 : }
255 :
256 0 : return aRes;
257 : }
258 :
259 0 : uno::Sequence< Locale > SAL_CALL LangGuess_Impl::getDisabledLanguages( )
260 : throw (uno::RuntimeException, std::exception)
261 : {
262 0 : osl::MutexGuard aGuard( GetLangGuessMutex() );
263 :
264 0 : EnsureInitialized();
265 :
266 0 : Sequence< com::sun::star::lang::Locale > aRes;
267 0 : vector<Guess> gs = m_aGuesser.GetUnavailableLanguages();
268 0 : aRes.realloc(gs.size());
269 :
270 0 : com::sun::star::lang::Locale *pRes = aRes.getArray();
271 :
272 0 : for(size_t i = 0; i < gs.size() ; i++ ){
273 0 : com::sun::star::lang::Locale current_aRes;
274 0 : current_aRes.Language = OUString::createFromAscii( gs[i].GetLanguage().c_str() );
275 0 : current_aRes.Country = OUString::createFromAscii( gs[i].GetCountry().c_str() );
276 0 : pRes[i] = current_aRes;
277 0 : }
278 :
279 0 : return aRes;
280 : }
281 :
282 0 : void SAL_CALL LangGuess_Impl::disableLanguages(
283 : const uno::Sequence< Locale >& rLanguages )
284 : throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
285 : {
286 0 : osl::MutexGuard aGuard( GetLangGuessMutex() );
287 :
288 0 : EnsureInitialized();
289 :
290 0 : sal_Int32 nLanguages = rLanguages.getLength();
291 0 : const Locale *pLanguages = rLanguages.getConstArray();
292 :
293 0 : for (sal_Int32 i = 0; i < nLanguages; ++i)
294 : {
295 0 : string language;
296 :
297 0 : OString l = OUStringToOString( pLanguages[i].Language, RTL_TEXTENCODING_ASCII_US );
298 0 : OString c = OUStringToOString( pLanguages[i].Country, RTL_TEXTENCODING_ASCII_US );
299 :
300 0 : language += l.getStr();
301 0 : language += "-";
302 0 : language += c.getStr();
303 0 : m_aGuesser.DisableLanguage(language);
304 0 : }
305 0 : }
306 :
307 0 : void SAL_CALL LangGuess_Impl::enableLanguages(
308 : const uno::Sequence< Locale >& rLanguages )
309 : throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
310 : {
311 0 : osl::MutexGuard aGuard( GetLangGuessMutex() );
312 :
313 0 : EnsureInitialized();
314 :
315 0 : sal_Int32 nLanguages = rLanguages.getLength();
316 0 : const Locale *pLanguages = rLanguages.getConstArray();
317 :
318 0 : for (sal_Int32 i = 0; i < nLanguages; ++i)
319 : {
320 0 : string language;
321 :
322 0 : OString l = OUStringToOString( pLanguages[i].Language, RTL_TEXTENCODING_ASCII_US );
323 0 : OString c = OUStringToOString( pLanguages[i].Country, RTL_TEXTENCODING_ASCII_US );
324 :
325 0 : language += l.getStr();
326 0 : language += "-";
327 0 : language += c.getStr();
328 0 : m_aGuesser.EnableLanguage(language);
329 0 : }
330 0 : }
331 :
332 0 : OUString SAL_CALL LangGuess_Impl::getImplementationName( )
333 : throw(RuntimeException, std::exception)
334 : {
335 0 : osl::MutexGuard aGuard( GetLangGuessMutex() );
336 0 : return OUString( IMPLNAME );
337 : }
338 :
339 0 : sal_Bool SAL_CALL LangGuess_Impl::supportsService( const OUString& ServiceName )
340 : throw(RuntimeException, std::exception)
341 : {
342 0 : return cppu::supportsService(this, ServiceName);
343 : }
344 :
345 0 : Sequence<OUString> SAL_CALL LangGuess_Impl::getSupportedServiceNames( )
346 : throw(RuntimeException, std::exception)
347 : {
348 0 : osl::MutexGuard aGuard( GetLangGuessMutex() );
349 0 : return getSupportedServiceNames_Static();
350 : }
351 :
352 0 : Sequence<OUString> SAL_CALL LangGuess_Impl::getSupportedServiceNames_Static( )
353 : {
354 0 : OUString aName( SERVICENAME );
355 0 : return Sequence< OUString >( &aName, 1 );
356 : }
357 :
358 : /**
359 : * Function to create a new component instance; is needed by factory helper implementation.
360 : * @param xMgr service manager to if the components needs other component instances
361 : */
362 0 : Reference< XInterface > SAL_CALL LangGuess_Impl_create(
363 : Reference< XComponentContext > const & xContext )
364 : SAL_THROW(())
365 : {
366 0 : return static_cast< ::cppu::OWeakObject * >( new LangGuess_Impl(xContext) );
367 : }
368 :
369 : //#### EXPORTED ### functions to allow for registration and creation of the UNO component
370 : static const struct ::cppu::ImplementationEntry s_component_entries [] =
371 : {
372 : {
373 : LangGuess_Impl_create, getImplementationName_LangGuess_Impl,
374 : getSupportedServiceNames_LangGuess_Impl,
375 : ::cppu::createSingleComponentFactory,
376 : 0, 0
377 : },
378 : { 0, 0, 0, 0, 0, 0 }
379 : };
380 :
381 : extern "C"
382 : {
383 :
384 0 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL guesslang_component_getFactory(
385 : sal_Char const * implName, lang::XMultiServiceFactory * xMgr,
386 : registry::XRegistryKey * xRegistry )
387 : {
388 : return ::cppu::component_getFactoryHelper(
389 0 : implName, xMgr, xRegistry, s_component_entries );
390 : }
391 :
392 0 : }
393 :
394 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|