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

Generated by: LCOV version 1.10