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