merged tag ooo/OOO330_m14
[LibreOffice.git] / framework / source / helper / mischelper.cxx
blobdeb5aa023c1bb5ea09429b6340dea22c212eeeb8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/document/XDocumentLanguages.hpp>
33 #include <com/sun/star/frame/XModuleManager.hpp>
34 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <tools/debug.hxx>
37 #include <vcl/settings.hxx>
38 #include <vcl/svapp.hxx>
39 #include <i18npool/mslangid.hxx>
40 #include <svtools/langtab.hxx>
41 #include <comphelper/processfactory.hxx>
42 #include <helper/mischelper.hxx>
43 #include <services.h>
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::frame;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::container;
51 using namespace ::com::sun::star::lang;
53 using ::rtl::OUString;
56 namespace framework
59 uno::Reference< linguistic2::XLanguageGuessing > LanguageGuessingHelper::GetGuesser() const
61 if (!m_xLanguageGuesser.is())
63 try
65 m_xLanguageGuesser = uno::Reference< linguistic2::XLanguageGuessing >(
66 m_xServiceManager->createInstance(
67 rtl::OUString::createFromAscii( "com.sun.star.linguistic2.LanguageGuessing" ) ),
68 uno::UNO_QUERY );
70 catch (uno::Exception &r)
72 (void) r;
73 DBG_ASSERT( 0, "failed to get language guessing component" );
76 return m_xLanguageGuesser;
79 ////////////////////////////////////////////////////////////
81 ::rtl::OUString RetrieveLabelFromCommand(
82 const ::rtl::OUString& aCmdURL,
83 const uno::Reference< lang::XMultiServiceFactory >& _xServiceFactory,
84 uno::Reference< container::XNameAccess >& _xUICommandLabels,
85 const uno::Reference< frame::XFrame >& _xFrame,
86 ::rtl::OUString& _rModuleIdentifier,
87 sal_Bool& _rIni,
88 const sal_Char* _pName)
90 ::rtl::OUString aLabel;
92 // Retrieve popup menu labels
93 if ( !_xUICommandLabels.is() )
95 try
97 if ( !_rIni )
99 _rIni = sal_True;
100 Reference< XModuleManager > xModuleManager( _xServiceFactory->createInstance( SERVICENAME_MODULEMANAGER ), UNO_QUERY_THROW );
104 _rModuleIdentifier = xModuleManager->identify( _xFrame );
106 catch( Exception& )
111 Reference< XNameAccess > xNameAccess( _xServiceFactory->createInstance( SERVICENAME_UICOMMANDDESCRIPTION ), UNO_QUERY );
112 if ( xNameAccess.is() )
114 xNameAccess->getByName( _rModuleIdentifier ) >>= _xUICommandLabels;
117 catch ( Exception& )
122 if ( _xUICommandLabels.is() )
126 if ( aCmdURL.getLength() > 0 )
128 rtl::OUString aStr;
129 Sequence< PropertyValue > aPropSeq;
130 if ( _xUICommandLabels->getByName( aCmdURL ) >>= aPropSeq )
132 for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
134 if ( aPropSeq[i].Name.equalsAscii( _pName/*"Label"*/ ))
136 aPropSeq[i].Value >>= aStr;
137 break;
141 aLabel = aStr;
144 catch ( com::sun::star::uno::Exception& )
149 return aLabel;
152 ////////////////////////////////////////////////////////////
154 void FillLangItems( std::set< OUString > &rLangItems,
155 const SvtLanguageTable & rLanguageTable,
156 const uno::Reference< frame::XFrame > & rxFrame,
157 const LanguageGuessingHelper & rLangGuessHelper,
158 sal_Int16 nScriptType,
159 const OUString & rCurLang,
160 const OUString & rKeyboardLang,
161 const OUString & rGuessedTextLang )
163 rLangItems.clear();
165 //1--add current language
166 if( rCurLang != OUString() &&
167 LANGUAGE_DONTKNOW != rLanguageTable.GetType( rCurLang ))
168 rLangItems.insert( rCurLang );
170 //2--System
171 const AllSettings& rAllSettings = Application::GetSettings();
172 LanguageType rSystemLanguage = rAllSettings.GetLanguage();
173 if( rSystemLanguage != LANGUAGE_DONTKNOW )
175 if ( IsScriptTypeMatchingToLanguage( nScriptType, rSystemLanguage ))
176 rLangItems.insert( OUString( rLanguageTable.GetString( rSystemLanguage )) );
179 //3--UI
180 LanguageType rUILanguage = rAllSettings.GetUILanguage();
181 if( rUILanguage != LANGUAGE_DONTKNOW )
183 if ( IsScriptTypeMatchingToLanguage( nScriptType, rUILanguage ))
184 rLangItems.insert( OUString( rLanguageTable.GetString( rUILanguage )) );
187 //4--guessed language
188 uno::Reference< linguistic2::XLanguageGuessing > xLangGuesser( rLangGuessHelper.GetGuesser() );
189 if ( xLangGuesser.is() && rGuessedTextLang.getLength() > 0)
191 ::com::sun::star::lang::Locale aLocale(xLangGuesser->guessPrimaryLanguage( rGuessedTextLang, 0, rGuessedTextLang.getLength()) );
192 LanguageType nLang = MsLangId::convertLocaleToLanguageWithFallback( aLocale );
193 if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_NONE && nLang != LANGUAGE_SYSTEM
194 && IsScriptTypeMatchingToLanguage( nScriptType, nLang ))
195 rLangItems.insert( rLanguageTable.GetString( nLang ));
198 //5--keyboard language
199 if( rKeyboardLang != OUString())
201 if ( IsScriptTypeMatchingToLanguage( nScriptType, rLanguageTable.GetType( rKeyboardLang )))
202 rLangItems.insert( rKeyboardLang );
205 //6--all languages used in current document
206 Reference< com::sun::star::frame::XModel > xModel;
207 if ( rxFrame.is() )
209 Reference< com::sun::star::frame::XController > xController( rxFrame->getController(), UNO_QUERY );
210 if ( xController.is() )
211 xModel = xController->getModel();
213 Reference< document::XDocumentLanguages > xDocumentLanguages( xModel, UNO_QUERY );
214 /*the description of nScriptType
215 LATIN : 0x001
216 ASIAN : 0x002
217 COMPLEX: 0x004
219 const sal_Int16 nMaxCount = 7;
220 if ( xDocumentLanguages.is() )
222 Sequence< Locale > rLocales( xDocumentLanguages->getDocumentLanguages( nScriptType, nMaxCount ));
223 if ( rLocales.getLength() > 0 )
225 for ( USHORT i = 0; i < rLocales.getLength(); ++i )
227 if ( rLangItems.size() == static_cast< size_t >(nMaxCount) )
228 break;
229 const Locale& rLocale=rLocales[i];
230 if( IsScriptTypeMatchingToLanguage( nScriptType, rLanguageTable.GetType( rLocale.Language )))
231 rLangItems.insert( OUString( rLocale.Language ) );
237 } // namespace framework