LCOV - code coverage report
Current view: top level - libreoffice/framework/source/fwi/helper - mischelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 24 77 31.2 %
Date: 2012-12-17 Functions: 1 3 33.3 %
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 <com/sun/star/lang/XMultiServiceFactory.hpp>
      22             : #include <com/sun/star/document/XDocumentLanguages.hpp>
      23             : #include <com/sun/star/frame/ModuleManager.hpp>
      24             : #include <com/sun/star/beans/PropertyValue.hpp>
      25             : 
      26             : #include <tools/debug.hxx>
      27             : #include <vcl/settings.hxx>
      28             : #include <vcl/svapp.hxx>
      29             : #include <i18npool/languagetag.hxx>
      30             : #include <svtools/langtab.hxx>
      31             : #include <comphelper/processfactory.hxx>
      32             : #include <helper/mischelper.hxx>
      33             : #include <services.h>
      34             : 
      35             : 
      36             : using namespace ::com::sun::star;
      37             : using namespace ::com::sun::star::uno;
      38             : using namespace ::com::sun::star::frame;
      39             : using namespace ::com::sun::star::beans;
      40             : using namespace ::com::sun::star::container;
      41             : using namespace ::com::sun::star::lang;
      42             : 
      43             : using ::rtl::OUString;
      44             : 
      45             : 
      46             : namespace framework
      47             : {
      48             : 
      49           0 : uno::Reference< linguistic2::XLanguageGuessing > LanguageGuessingHelper::GetGuesser() const
      50             : {
      51           0 :     if (!m_xLanguageGuesser.is())
      52             :     {
      53             :         try
      54             :         {
      55             :             m_xLanguageGuesser = uno::Reference< linguistic2::XLanguageGuessing >(
      56           0 :                     m_xServiceManager->createInstance(
      57           0 :                         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.linguistic2.LanguageGuessing")) ),
      58           0 :                         uno::UNO_QUERY );
      59             :         }
      60           0 :         catch (const uno::Exception &)
      61             :         {
      62             :             DBG_ASSERT( 0, "failed to get language guessing component" );
      63             :         }
      64             :     }
      65           0 :     return m_xLanguageGuesser;
      66             : }
      67             : 
      68             : 
      69          42 : ::rtl::OUString RetrieveLabelFromCommand(
      70             :     const ::rtl::OUString& aCmdURL,
      71             :     const uno::Reference< lang::XMultiServiceFactory >& _xServiceFactory,
      72             :     uno::Reference< container::XNameAccess >& _xUICommandLabels,
      73             :     const uno::Reference< frame::XFrame >& _xFrame,
      74             :     ::rtl::OUString& _rModuleIdentifier,
      75             :     sal_Bool& _rIni,
      76             :     const sal_Char* _pName)
      77             : {
      78          42 :     ::rtl::OUString aLabel;
      79             : 
      80             :     // Retrieve popup menu labels
      81          42 :     if ( !_xUICommandLabels.is() )
      82             :     {
      83             :       try
      84             :         {
      85           4 :             if ( !_rIni )
      86             :             {
      87           2 :                 _rIni = sal_True;
      88           2 :                 Reference< XModuleManager2 > xModuleManager = ModuleManager::create( comphelper::getComponentContext(_xServiceFactory) );
      89             : 
      90             :                 try
      91             :                 {
      92           2 :                     _rModuleIdentifier = xModuleManager->identify( _xFrame );
      93             :                 }
      94           0 :                 catch( const Exception& )
      95             :                 {
      96           2 :                 }
      97             :             }
      98             : 
      99           4 :             Reference< XNameAccess > xNameAccess( _xServiceFactory->createInstance( SERVICENAME_UICOMMANDDESCRIPTION ), UNO_QUERY );
     100           4 :             if ( xNameAccess.is() )
     101             :             {
     102           4 :                 xNameAccess->getByName( _rModuleIdentifier ) >>= _xUICommandLabels;
     103           4 :             }
     104             :         }
     105           0 :         catch ( const Exception& )
     106             :         {
     107             :         }
     108             :     }
     109             : 
     110          42 :     if ( _xUICommandLabels.is() )
     111             :     {
     112             :         try
     113             :         {
     114          42 :             if ( !aCmdURL.isEmpty() )
     115             :             {
     116          42 :                 rtl::OUString aStr;
     117          42 :                 Sequence< PropertyValue > aPropSeq;
     118          42 :                 if( _xUICommandLabels->hasByName( aCmdURL ) )
     119             :                 {
     120          36 :                     if ( _xUICommandLabels->getByName( aCmdURL ) >>= aPropSeq )
     121             :                     {
     122          54 :                         for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
     123             :                         {
     124          54 :                             if ( aPropSeq[i].Name.equalsAscii( _pName/*"Label"*/ ))
     125             :                             {
     126          36 :                                 aPropSeq[i].Value >>= aStr;
     127          36 :                                 break;
     128             :                             }
     129             :                         }
     130             :                     }
     131             :                 }
     132          42 :                 aLabel = aStr;
     133             :             }
     134             :         }
     135           0 :         catch ( const com::sun::star::uno::Exception& )
     136             :         {
     137             :         }
     138             :     }
     139             : 
     140          42 :     return aLabel;
     141             : }
     142             : 
     143             : 
     144           0 : void FillLangItems( std::set< OUString > &rLangItems,
     145             :         const SvtLanguageTable &    rLanguageTable,
     146             :         const uno::Reference< frame::XFrame > & rxFrame,
     147             :         const LanguageGuessingHelper & rLangGuessHelper,
     148             :         sal_Int16        nScriptType,
     149             :         const OUString & rCurLang,
     150             :         const OUString & rKeyboardLang,
     151             :         const OUString & rGuessedTextLang )
     152             : {
     153           0 :     rLangItems.clear();
     154             : 
     155             :     //1--add current language
     156           0 :     if( !rCurLang.isEmpty() &&
     157           0 :         LANGUAGE_DONTKNOW != rLanguageTable.GetType( rCurLang ))
     158           0 :         rLangItems.insert( rCurLang );
     159             : 
     160             :     //2--System
     161           0 :     const AllSettings& rAllSettings = Application::GetSettings();
     162           0 :     LanguageType rSystemLanguage = rAllSettings.GetLanguageTag().getLanguageType();
     163           0 :     if( rSystemLanguage != LANGUAGE_DONTKNOW )
     164             :     {
     165           0 :         if ( IsScriptTypeMatchingToLanguage( nScriptType, rSystemLanguage ))
     166           0 :             rLangItems.insert( OUString( rLanguageTable.GetString( rSystemLanguage )) );
     167             :     }
     168             : 
     169             :     //3--UI
     170           0 :     LanguageType rUILanguage = rAllSettings.GetUILanguageTag().getLanguageType();
     171           0 :     if( rUILanguage != LANGUAGE_DONTKNOW )
     172             :     {
     173           0 :         if ( IsScriptTypeMatchingToLanguage( nScriptType, rUILanguage ))
     174           0 :             rLangItems.insert( OUString( rLanguageTable.GetString( rUILanguage )) );
     175             :     }
     176             : 
     177             :     //4--guessed language
     178           0 :     uno::Reference< linguistic2::XLanguageGuessing > xLangGuesser( rLangGuessHelper.GetGuesser() );
     179           0 :     if ( xLangGuesser.is() && !rGuessedTextLang.isEmpty())
     180             :     {
     181           0 :         ::com::sun::star::lang::Locale aLocale(xLangGuesser->guessPrimaryLanguage( rGuessedTextLang, 0, rGuessedTextLang.getLength()) );
     182           0 :         LanguageType nLang = LanguageTag( aLocale ).makeFallback().getLanguageType();
     183           0 :         if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_NONE && nLang != LANGUAGE_SYSTEM
     184           0 :             && IsScriptTypeMatchingToLanguage( nScriptType, nLang ))
     185           0 :             rLangItems.insert( rLanguageTable.GetString( nLang ));
     186             :     }
     187             : 
     188             :     //5--keyboard language
     189           0 :     if( !rKeyboardLang.isEmpty() )
     190             :     {
     191           0 :         if ( IsScriptTypeMatchingToLanguage( nScriptType, rLanguageTable.GetType( rKeyboardLang )))
     192           0 :             rLangItems.insert( rKeyboardLang );
     193             :     }
     194             : 
     195             :     //6--all languages used in current document
     196           0 :     Reference< com::sun::star::frame::XModel > xModel;
     197           0 :     if ( rxFrame.is() )
     198             :     {
     199           0 :        Reference< com::sun::star::frame::XController > xController( rxFrame->getController(), UNO_QUERY );
     200           0 :        if ( xController.is() )
     201           0 :            xModel = xController->getModel();
     202             :     }
     203           0 :     Reference< document::XDocumentLanguages > xDocumentLanguages( xModel, UNO_QUERY );
     204             :     /*the description of nScriptType
     205             :       LATIN :   0x001
     206             :       ASIAN :   0x002
     207             :       COMPLEX:  0x004
     208             :     */
     209           0 :     const sal_Int16 nMaxCount = 7;
     210           0 :     if ( xDocumentLanguages.is() )
     211             :     {
     212           0 :         Sequence< Locale > rLocales( xDocumentLanguages->getDocumentLanguages( nScriptType, nMaxCount ));
     213           0 :         if ( rLocales.getLength() > 0 )
     214             :         {
     215           0 :             for ( sal_uInt16 i = 0; i < rLocales.getLength(); ++i )
     216             :             {
     217           0 :                 if ( rLangItems.size() == static_cast< size_t >(nMaxCount) )
     218           0 :                     break;
     219           0 :                 const Locale& rLocale=rLocales[i];
     220           0 :                 if( IsScriptTypeMatchingToLanguage( nScriptType, rLanguageTable.GetType( rLocale.Language )))
     221           0 :                     rLangItems.insert( OUString( rLocale.Language ) );
     222             :             }
     223           0 :         }
     224           0 :     }
     225           0 : }
     226             : 
     227             : } // namespace framework
     228             : 
     229             : 
     230             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10