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