LCOV - code coverage report
Current view: top level - lingucomponent/source/languageguessing - guesslang.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 15 128 11.7 %
Date: 2014-11-03 Functions: 9 22 40.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10