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

Generated by: LCOV version 1.10