xref: /AOO41X/main/lingucomponent/source/languageguessing/guesslang.cxx (revision b0844812064e3450486bf80ca0c2a3ca72742d45)
1*b0844812SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b0844812SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b0844812SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b0844812SAndrew Rist  * distributed with this work for additional information
6*b0844812SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b0844812SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b0844812SAndrew Rist  * "License"); you may not use this file except in compliance
9*b0844812SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*b0844812SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*b0844812SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b0844812SAndrew Rist  * software distributed under the License is distributed on an
15*b0844812SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b0844812SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b0844812SAndrew Rist  * specific language governing permissions and limitations
18*b0844812SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*b0844812SAndrew Rist  *************************************************************/
21*b0844812SAndrew Rist 
22*b0844812SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_lingucomponent.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <iostream>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <tools/debug.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <sal/config.h>
32cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
33cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
34cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
35cdf0e10cSrcweir #include <tools/string.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <simpleguesser.hxx>
38cdf0e10cSrcweir #include <guess.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir //#include <cppuhelper/queryinterface.hxx> // helper for queryInterface() impl
41cdf0e10cSrcweir 
42cdf0e10cSrcweir //#include <com/sun/star/lang/XMultiServiceFactory.hpp>
43cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
44cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
45cdf0e10cSrcweir #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
46cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
47cdf0e10cSrcweir #include <unotools/localfilehelper.hxx>
48cdf0e10cSrcweir #include <osl/thread.h>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir using namespace ::rtl;
51cdf0e10cSrcweir using namespace ::osl;
52cdf0e10cSrcweir using namespace ::cppu;
53cdf0e10cSrcweir using namespace ::com::sun::star;
54cdf0e10cSrcweir using namespace ::com::sun::star::uno;
55cdf0e10cSrcweir using namespace ::com::sun::star::lang;
56cdf0e10cSrcweir using namespace ::com::sun::star::linguistic2;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir namespace css = ::com::sun::star;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //==================================================================================================
61cdf0e10cSrcweir 
62cdf0e10cSrcweir #define A2OU(x) ::rtl::OUString::createFromAscii( x )
63cdf0e10cSrcweir 
64cdf0e10cSrcweir #define SERVICENAME     "com.sun.star.linguistic2.LanguageGuessing"
65cdf0e10cSrcweir 
66cdf0e10cSrcweir #define IMPLNAME        "com.sun.star.lingu2.LanguageGuessing"
67cdf0e10cSrcweir 
getSupportedServiceNames_LangGuess_Impl()68cdf0e10cSrcweir static Sequence< OUString > getSupportedServiceNames_LangGuess_Impl()
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     Sequence<OUString> names(1);
71cdf0e10cSrcweir     names[0] = A2OU( SERVICENAME );
72cdf0e10cSrcweir     return names;
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
getImplementationName_LangGuess_Impl()75cdf0e10cSrcweir static OUString getImplementationName_LangGuess_Impl()
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     return A2OU( IMPLNAME );
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
GetLangGuessMutex()80cdf0e10cSrcweir static osl::Mutex &  GetLangGuessMutex()
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     static osl::Mutex aMutex;
83cdf0e10cSrcweir     return aMutex;
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 
87cdf0e10cSrcweir class LangGuess_Impl :
88cdf0e10cSrcweir     public ::cppu::WeakImplHelper2<
89cdf0e10cSrcweir         XLanguageGuessing,
90cdf0e10cSrcweir         XServiceInfo >
91cdf0e10cSrcweir {
92cdf0e10cSrcweir     SimpleGuesser   m_aGuesser;
93cdf0e10cSrcweir     bool            m_bInitialized;
94cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext >  m_xContext;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     LangGuess_Impl( const LangGuess_Impl & ); // not defined
97cdf0e10cSrcweir     LangGuess_Impl & operator =( const LangGuess_Impl & ); // not defined
98cdf0e10cSrcweir 
~LangGuess_Impl()99cdf0e10cSrcweir     virtual ~LangGuess_Impl() {}
100cdf0e10cSrcweir     void    EnsureInitialized();
101cdf0e10cSrcweir 
102cdf0e10cSrcweir public:
103cdf0e10cSrcweir     explicit LangGuess_Impl(css::uno::Reference< css::uno::XComponentContext > const & rxContext);
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     // XServiceInfo	implementation
106cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
107cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
108cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
109cdf0e10cSrcweir     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(  );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     // XLanguageGuessing implementation
112cdf0e10cSrcweir     virtual ::com::sun::star::lang::Locale SAL_CALL guessPrimaryLanguage( const ::rtl::OUString& aText, ::sal_Int32 nStartPos, ::sal_Int32 nLen ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
113cdf0e10cSrcweir     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);
114cdf0e10cSrcweir     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);
115cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > SAL_CALL getAvailableLanguages(  ) throw (::com::sun::star::uno::RuntimeException);
116cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > SAL_CALL getEnabledLanguages(  ) throw (::com::sun::star::uno::RuntimeException);
117cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > SAL_CALL getDisabledLanguages(  ) throw (::com::sun::star::uno::RuntimeException);
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     // implementation specific
120cdf0e10cSrcweir     void SetFingerPrintsDB( const rtl::OUString &fileName ) throw (RuntimeException);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     static const OUString & SAL_CALL getImplementationName_Static() throw();
123cdf0e10cSrcweir 
124cdf0e10cSrcweir };
125cdf0e10cSrcweir 
126cdf0e10cSrcweir //*************************************************************************
127cdf0e10cSrcweir 
LangGuess_Impl(css::uno::Reference<css::uno::XComponentContext> const & rxContext)128cdf0e10cSrcweir LangGuess_Impl::LangGuess_Impl(css::uno::Reference< css::uno::XComponentContext > const & rxContext) :
129cdf0e10cSrcweir     m_bInitialized( false ),
130cdf0e10cSrcweir     m_xContext( rxContext )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //*************************************************************************
135cdf0e10cSrcweir 
EnsureInitialized()136cdf0e10cSrcweir void LangGuess_Impl::EnsureInitialized()
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     if (!m_bInitialized)
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir         // set this to true at the very start to prevent loops because of
141cdf0e10cSrcweir         // implicitly called functions below
142cdf0e10cSrcweir         m_bInitialized = true;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         // set default fingerprint path to where those get installed
145cdf0e10cSrcweir         String aPhysPath;
146cdf0e10cSrcweir         String aURL( SvtPathOptions().GetFingerprintPath() );
147cdf0e10cSrcweir         utl::LocalFileHelper::ConvertURLToPhysicalName( aURL, aPhysPath );
148cdf0e10cSrcweir #ifdef WNT
149cdf0e10cSrcweir             aPhysPath += '\\';
150cdf0e10cSrcweir #else
151cdf0e10cSrcweir             aPhysPath += '/';
152cdf0e10cSrcweir #endif
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         SetFingerPrintsDB( aPhysPath );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         //
157cdf0e10cSrcweir         // disable currently not functional languages...
158cdf0e10cSrcweir         //
159cdf0e10cSrcweir         struct LangCountry
160cdf0e10cSrcweir         {
161cdf0e10cSrcweir             const char *pLang;
162cdf0e10cSrcweir             const char *pCountry;
163cdf0e10cSrcweir         };
164cdf0e10cSrcweir         LangCountry aDisable[] =
165cdf0e10cSrcweir         {
166cdf0e10cSrcweir             {"gv", ""}, {"sco", ""},                            // no lang-id available yet...
167cdf0e10cSrcweir //            {"hy", ""}, {"drt", ""},                          // 0 bytes fingerprints...
168cdf0e10cSrcweir             {"zh", "CN"}, {"zh", "TW"}, {"ja", ""}, {"ko", ""}, // not yet correct functional...
169cdf0e10cSrcweir             {"ka", ""}, {"hi", ""}, {"mr", ""}, {"ne", ""},
170cdf0e10cSrcweir             {"sa", ""}, {"ta", ""}, {"th", ""},
171cdf0e10cSrcweir             {"qu", ""}, {"yi", ""}
172cdf0e10cSrcweir         };
173cdf0e10cSrcweir         sal_Int32 nNum = sizeof(aDisable) / sizeof(aDisable[0]);
174cdf0e10cSrcweir         Sequence< Locale > aDisableSeq( nNum );
175cdf0e10cSrcweir         Locale *pDisableSeq = aDisableSeq.getArray();
176cdf0e10cSrcweir         for (sal_Int32 i = 0;  i < nNum;  ++i)
177cdf0e10cSrcweir         {
178cdf0e10cSrcweir             Locale aLocale;
179cdf0e10cSrcweir             aLocale.Language = OUString::createFromAscii( aDisable[i].pLang );
180cdf0e10cSrcweir             aLocale.Country  = OUString::createFromAscii( aDisable[i].pCountry );
181cdf0e10cSrcweir             pDisableSeq[i] = aLocale;
182cdf0e10cSrcweir         }
183cdf0e10cSrcweir         disableLanguages( aDisableSeq );
184cdf0e10cSrcweir         DBG_ASSERT( nNum == getDisabledLanguages().getLength(), "size mismatch" );
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir //*************************************************************************
189cdf0e10cSrcweir 
190cdf0e10cSrcweir /* TL: currently not part of the API
191cdf0e10cSrcweir Sequence< com::sun::star::lang::Locale > SAL_CALL LangGuess_Impl::guessLanguages(
192cdf0e10cSrcweir         const rtl::OUString   &rText,
193cdf0e10cSrcweir         sal_Int32       nStartPos,
194cdf0e10cSrcweir         sal_Int32       nLen )
195cdf0e10cSrcweir     throw (RuntimeException)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir     Sequence< com::sun::star::lang::Locale > aRes;
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     OString o = OUStringToOString( rText, RTL_TEXTENCODING_UTF8 );
200cdf0e10cSrcweir     vector<Guess> gs = m_aGuesser.GuessLanguage(o.pData->buffer);
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     aRes.realloc(gs.size());
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     com::sun::star::lang::Locale *pRes = aRes.getArray();
205cdf0e10cSrcweir 
206cdf0e10cSrcweir #ifdef DEBUG
207cdf0e10cSrcweir     std::cout << " We have " << gs.size() << " candidates" << std::endl;
208cdf0e10cSrcweir #endif
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     for(int i = 0; i < gs.size() ; i++ ){
211cdf0e10cSrcweir         com::sun::star::lang::Locale current_aRes;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         current_aRes.Language   = A2OU( gs[i].getLanguage().c_str() );
214cdf0e10cSrcweir         current_aRes.Country    = A2OU( gs[i].getCountry().c_str() );
215cdf0e10cSrcweir 
216cdf0e10cSrcweir         pRes[i] = current_aRes;
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     return aRes;
220cdf0e10cSrcweir }
221cdf0e10cSrcweir */
222cdf0e10cSrcweir //*************************************************************************
223cdf0e10cSrcweir 
guessPrimaryLanguage(const::rtl::OUString & rText,::sal_Int32 nStartPos,::sal_Int32 nLen)224cdf0e10cSrcweir Locale SAL_CALL LangGuess_Impl::guessPrimaryLanguage(
225cdf0e10cSrcweir         const ::rtl::OUString& rText,
226cdf0e10cSrcweir         ::sal_Int32 nStartPos,
227cdf0e10cSrcweir         ::sal_Int32 nLen )
228cdf0e10cSrcweir     throw (lang::IllegalArgumentException, uno::RuntimeException)
229cdf0e10cSrcweir {
230cdf0e10cSrcweir     osl::MutexGuard aGuard( GetLangGuessMutex() );
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     EnsureInitialized();
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 	lang::Locale aRes;
235cdf0e10cSrcweir 	if (nStartPos >=0 && nLen >= 0 && nStartPos + nLen <= rText.getLength())
236cdf0e10cSrcweir 	{
237cdf0e10cSrcweir 		OString o( OUStringToOString( rText.copy(nStartPos, nLen), RTL_TEXTENCODING_UTF8 ) );
238cdf0e10cSrcweir         Guess g = m_aGuesser.GuessPrimaryLanguage((char*)o.getStr());
239cdf0e10cSrcweir 		aRes.Language   = OUString::createFromAscii(g.GetLanguage().c_str());
240cdf0e10cSrcweir 		aRes.Country    = OUString::createFromAscii(g.GetCountry().c_str());
241cdf0e10cSrcweir 	}
242cdf0e10cSrcweir 	else
243cdf0e10cSrcweir 		throw lang::IllegalArgumentException();
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 	return aRes;
246cdf0e10cSrcweir }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir //*************************************************************************
249cdf0e10cSrcweir #define DEFAULT_CONF_FILE_NAME "fpdb.conf"
250cdf0e10cSrcweir 
SetFingerPrintsDB(const rtl::OUString & filePath)251cdf0e10cSrcweir void LangGuess_Impl::SetFingerPrintsDB(
252cdf0e10cSrcweir         const rtl::OUString &filePath )
253cdf0e10cSrcweir     throw (RuntimeException)
254cdf0e10cSrcweir {
255cdf0e10cSrcweir 	//! text encoding for file name / path needs to be in the same encoding the OS uses
256cdf0e10cSrcweir     OString path = OUStringToOString( filePath, osl_getThreadTextEncoding() );
257cdf0e10cSrcweir     OString conf_file_name( DEFAULT_CONF_FILE_NAME );
258cdf0e10cSrcweir     OString conf_file_path(path);
259cdf0e10cSrcweir     conf_file_path += conf_file_name;
260cdf0e10cSrcweir 
261cdf0e10cSrcweir     //cout << "Conf file : " << conf_file_path.getStr() << " directory : " << path.getStr() << endl;
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     m_aGuesser.SetDBPath((const char*)conf_file_path.getStr(), (const char*)path.getStr());
264cdf0e10cSrcweir }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir //*************************************************************************
getAvailableLanguages()267cdf0e10cSrcweir uno::Sequence< Locale > SAL_CALL LangGuess_Impl::getAvailableLanguages(  )
268cdf0e10cSrcweir         throw (uno::RuntimeException)
269cdf0e10cSrcweir {
270cdf0e10cSrcweir     osl::MutexGuard aGuard( GetLangGuessMutex() );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir     EnsureInitialized();
273cdf0e10cSrcweir 
274cdf0e10cSrcweir     Sequence< com::sun::star::lang::Locale > aRes;
275cdf0e10cSrcweir     vector<Guess> gs = m_aGuesser.GetAllManagedLanguages();
276cdf0e10cSrcweir     aRes.realloc(gs.size());
277cdf0e10cSrcweir 
278cdf0e10cSrcweir     com::sun::star::lang::Locale *pRes = aRes.getArray();
279cdf0e10cSrcweir 
280cdf0e10cSrcweir     for(size_t i = 0; i < gs.size() ; i++ ){
281cdf0e10cSrcweir         com::sun::star::lang::Locale current_aRes;
282cdf0e10cSrcweir         current_aRes.Language   = A2OU( gs[i].GetLanguage().c_str() );
283cdf0e10cSrcweir         current_aRes.Country    = A2OU( gs[i].GetCountry().c_str() );
284cdf0e10cSrcweir         pRes[i] = current_aRes;
285cdf0e10cSrcweir     }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir     return aRes;
288cdf0e10cSrcweir }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir //*************************************************************************
getEnabledLanguages()291cdf0e10cSrcweir uno::Sequence< Locale > SAL_CALL LangGuess_Impl::getEnabledLanguages(  )
292cdf0e10cSrcweir         throw (uno::RuntimeException)
293cdf0e10cSrcweir {
294cdf0e10cSrcweir     osl::MutexGuard aGuard( GetLangGuessMutex() );
295cdf0e10cSrcweir 
296cdf0e10cSrcweir     EnsureInitialized();
297cdf0e10cSrcweir 
298cdf0e10cSrcweir     Sequence< com::sun::star::lang::Locale > aRes;
299cdf0e10cSrcweir     vector<Guess> gs = m_aGuesser.GetAvailableLanguages();
300cdf0e10cSrcweir     aRes.realloc(gs.size());
301cdf0e10cSrcweir 
302cdf0e10cSrcweir     com::sun::star::lang::Locale *pRes = aRes.getArray();
303cdf0e10cSrcweir 
304cdf0e10cSrcweir     for(size_t i = 0; i < gs.size() ; i++ ){
305cdf0e10cSrcweir         com::sun::star::lang::Locale current_aRes;
306cdf0e10cSrcweir         current_aRes.Language   = A2OU( gs[i].GetLanguage().c_str() );
307cdf0e10cSrcweir         current_aRes.Country    = A2OU( gs[i].GetCountry().c_str() );
308cdf0e10cSrcweir         pRes[i] = current_aRes;
309cdf0e10cSrcweir     }
310cdf0e10cSrcweir 
311cdf0e10cSrcweir     return aRes;
312cdf0e10cSrcweir }
313cdf0e10cSrcweir 
314cdf0e10cSrcweir //*************************************************************************
getDisabledLanguages()315cdf0e10cSrcweir uno::Sequence< Locale > SAL_CALL LangGuess_Impl::getDisabledLanguages(  )
316cdf0e10cSrcweir         throw (uno::RuntimeException)
317cdf0e10cSrcweir {
318cdf0e10cSrcweir     osl::MutexGuard aGuard( GetLangGuessMutex() );
319cdf0e10cSrcweir 
320cdf0e10cSrcweir     EnsureInitialized();
321cdf0e10cSrcweir 
322cdf0e10cSrcweir     Sequence< com::sun::star::lang::Locale > aRes;
323cdf0e10cSrcweir     vector<Guess> gs = m_aGuesser.GetUnavailableLanguages();
324cdf0e10cSrcweir     aRes.realloc(gs.size());
325cdf0e10cSrcweir 
326cdf0e10cSrcweir     com::sun::star::lang::Locale *pRes = aRes.getArray();
327cdf0e10cSrcweir 
328cdf0e10cSrcweir     for(size_t i = 0; i < gs.size() ; i++ ){
329cdf0e10cSrcweir         com::sun::star::lang::Locale current_aRes;
330cdf0e10cSrcweir         current_aRes.Language   = A2OU( gs[i].GetLanguage().c_str() );
331cdf0e10cSrcweir         current_aRes.Country    = A2OU( gs[i].GetCountry().c_str() );
332cdf0e10cSrcweir         pRes[i] = current_aRes;
333cdf0e10cSrcweir     }
334cdf0e10cSrcweir 
335cdf0e10cSrcweir     return aRes;
336cdf0e10cSrcweir }
337cdf0e10cSrcweir 
338cdf0e10cSrcweir //*************************************************************************
disableLanguages(const uno::Sequence<Locale> & rLanguages)339cdf0e10cSrcweir void SAL_CALL LangGuess_Impl::disableLanguages(
340cdf0e10cSrcweir         const uno::Sequence< Locale >& rLanguages )
341cdf0e10cSrcweir     throw (lang::IllegalArgumentException, uno::RuntimeException)
342cdf0e10cSrcweir {
343cdf0e10cSrcweir     osl::MutexGuard aGuard( GetLangGuessMutex() );
344cdf0e10cSrcweir 
345cdf0e10cSrcweir     EnsureInitialized();
346cdf0e10cSrcweir 
347cdf0e10cSrcweir     sal_Int32 nLanguages = rLanguages.getLength();
348cdf0e10cSrcweir     const Locale *pLanguages = rLanguages.getConstArray();
349cdf0e10cSrcweir 
350cdf0e10cSrcweir     for (sal_Int32 i = 0;  i < nLanguages;  ++i)
351cdf0e10cSrcweir     {
352cdf0e10cSrcweir         string language;
353cdf0e10cSrcweir 
354cdf0e10cSrcweir         OString l = OUStringToOString( pLanguages[i].Language, RTL_TEXTENCODING_ASCII_US );
355cdf0e10cSrcweir         OString c = OUStringToOString( pLanguages[i].Country, RTL_TEXTENCODING_ASCII_US );
356cdf0e10cSrcweir 
357cdf0e10cSrcweir         language += l.getStr();
358cdf0e10cSrcweir         language += "-";
359cdf0e10cSrcweir         language += c.getStr();
360cdf0e10cSrcweir         m_aGuesser.DisableLanguage(language);
361cdf0e10cSrcweir     }
362cdf0e10cSrcweir }
363cdf0e10cSrcweir 
364cdf0e10cSrcweir //*************************************************************************
enableLanguages(const uno::Sequence<Locale> & rLanguages)365cdf0e10cSrcweir void SAL_CALL LangGuess_Impl::enableLanguages(
366cdf0e10cSrcweir         const uno::Sequence< Locale >& rLanguages )
367cdf0e10cSrcweir     throw (lang::IllegalArgumentException, uno::RuntimeException)
368cdf0e10cSrcweir {
369cdf0e10cSrcweir     osl::MutexGuard aGuard( GetLangGuessMutex() );
370cdf0e10cSrcweir 
371cdf0e10cSrcweir     EnsureInitialized();
372cdf0e10cSrcweir 
373cdf0e10cSrcweir     sal_Int32 nLanguages = rLanguages.getLength();
374cdf0e10cSrcweir     const Locale *pLanguages = rLanguages.getConstArray();
375cdf0e10cSrcweir 
376cdf0e10cSrcweir     for (sal_Int32 i = 0;  i < nLanguages;  ++i)
377cdf0e10cSrcweir     {
378cdf0e10cSrcweir         string language;
379cdf0e10cSrcweir 
380cdf0e10cSrcweir         OString l = OUStringToOString( pLanguages[i].Language, RTL_TEXTENCODING_ASCII_US );
381cdf0e10cSrcweir         OString c = OUStringToOString( pLanguages[i].Country, RTL_TEXTENCODING_ASCII_US );
382cdf0e10cSrcweir 
383cdf0e10cSrcweir         language += l.getStr();
384cdf0e10cSrcweir         language += "-";
385cdf0e10cSrcweir         language += c.getStr();
386cdf0e10cSrcweir         m_aGuesser.EnableLanguage(language);
387cdf0e10cSrcweir     }
388cdf0e10cSrcweir }
389cdf0e10cSrcweir 
390cdf0e10cSrcweir //*************************************************************************
getImplementationName()391cdf0e10cSrcweir OUString SAL_CALL LangGuess_Impl::getImplementationName(  )
392cdf0e10cSrcweir 	throw(RuntimeException)
393cdf0e10cSrcweir {
394cdf0e10cSrcweir     osl::MutexGuard aGuard( GetLangGuessMutex() );
395cdf0e10cSrcweir     return A2OU( IMPLNAME );
396cdf0e10cSrcweir }
397cdf0e10cSrcweir 
398cdf0e10cSrcweir //*************************************************************************
supportsService(const OUString & ServiceName)399cdf0e10cSrcweir sal_Bool SAL_CALL LangGuess_Impl::supportsService( const OUString& ServiceName )
400cdf0e10cSrcweir 	throw(RuntimeException)
401cdf0e10cSrcweir {
402cdf0e10cSrcweir     osl::MutexGuard aGuard( GetLangGuessMutex() );
403cdf0e10cSrcweir 	Sequence< OUString > aSNL = getSupportedServiceNames();
404cdf0e10cSrcweir 	const OUString * pArray = aSNL.getArray();
405cdf0e10cSrcweir 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
406cdf0e10cSrcweir 		if( pArray[i] == ServiceName )
407cdf0e10cSrcweir 			return sal_True;
408cdf0e10cSrcweir 	return sal_False;
409cdf0e10cSrcweir }
410cdf0e10cSrcweir 
411cdf0e10cSrcweir //*************************************************************************
getSupportedServiceNames()412cdf0e10cSrcweir Sequence<OUString> SAL_CALL LangGuess_Impl::getSupportedServiceNames(  )
413cdf0e10cSrcweir 	throw(RuntimeException)
414cdf0e10cSrcweir {
415cdf0e10cSrcweir     osl::MutexGuard aGuard( GetLangGuessMutex() );
416cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
417cdf0e10cSrcweir }
418cdf0e10cSrcweir 
419cdf0e10cSrcweir //*************************************************************************
getSupportedServiceNames_Static()420cdf0e10cSrcweir Sequence<OUString> SAL_CALL LangGuess_Impl::getSupportedServiceNames_Static(  )
421cdf0e10cSrcweir {
422cdf0e10cSrcweir     OUString aName( A2OU( SERVICENAME ) );
423cdf0e10cSrcweir 	return Sequence< OUString >( &aName, 1 );
424cdf0e10cSrcweir }
425cdf0e10cSrcweir 
426cdf0e10cSrcweir //*************************************************************************
427cdf0e10cSrcweir 
428cdf0e10cSrcweir 
429cdf0e10cSrcweir /**
430cdf0e10cSrcweir  * Function to create a new component instance; is needed by factory helper implementation.
431cdf0e10cSrcweir  * @param xMgr service manager to if the components needs other component instances
432cdf0e10cSrcweir  */
LangGuess_Impl_create(Reference<XComponentContext> const & xContext)433cdf0e10cSrcweir Reference< XInterface > SAL_CALL LangGuess_Impl_create(
434cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
435cdf0e10cSrcweir     SAL_THROW( () )
436cdf0e10cSrcweir {
437cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject * >( new LangGuess_Impl(xContext) );
438cdf0e10cSrcweir }
439cdf0e10cSrcweir 
440cdf0e10cSrcweir //##################################################################################################
441cdf0e10cSrcweir //#### EXPORTED ### functions to allow for registration and creation of the UNO component
442cdf0e10cSrcweir //##################################################################################################
443cdf0e10cSrcweir 
444cdf0e10cSrcweir static struct ::cppu::ImplementationEntry s_component_entries [] =
445cdf0e10cSrcweir {
446cdf0e10cSrcweir     {
447cdf0e10cSrcweir         LangGuess_Impl_create, getImplementationName_LangGuess_Impl,
448cdf0e10cSrcweir         getSupportedServiceNames_LangGuess_Impl,
449cdf0e10cSrcweir         ::cppu::createSingleComponentFactory,
450cdf0e10cSrcweir         0, 0
451cdf0e10cSrcweir     },
452cdf0e10cSrcweir     { 0, 0, 0, 0, 0, 0 }
453cdf0e10cSrcweir };
454cdf0e10cSrcweir 
455cdf0e10cSrcweir extern "C"
456cdf0e10cSrcweir {
457cdf0e10cSrcweir 
component_getImplementationEnvironment(sal_Char const ** ppEnvTypeName,uno_Environment **)458cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment(
459cdf0e10cSrcweir     sal_Char const ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ )
460cdf0e10cSrcweir {
461cdf0e10cSrcweir     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
462cdf0e10cSrcweir }
463cdf0e10cSrcweir 
component_getFactory(sal_Char const * implName,lang::XMultiServiceFactory * xMgr,registry::XRegistryKey * xRegistry)464cdf0e10cSrcweir void * SAL_CALL component_getFactory(
465cdf0e10cSrcweir     sal_Char const * implName, lang::XMultiServiceFactory * xMgr,
466cdf0e10cSrcweir     registry::XRegistryKey * xRegistry )
467cdf0e10cSrcweir {
468cdf0e10cSrcweir     return ::cppu::component_getFactoryHelper(
469cdf0e10cSrcweir         implName, xMgr, xRegistry, s_component_entries );
470cdf0e10cSrcweir }
471cdf0e10cSrcweir 
472cdf0e10cSrcweir }
473cdf0e10cSrcweir 
474