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