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 0 : 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 0 : OUString aLabel;
72 :
73 : // Retrieve popup menu labels
74 0 : if ( !_xUICommandLabels.is() )
75 : {
76 : try
77 : {
78 0 : if ( !_rIni )
79 : {
80 0 : _rIni = true;
81 0 : Reference< XModuleManager2 > xModuleManager = ModuleManager::create( _xContext );
82 :
83 : try
84 : {
85 0 : _rModuleIdentifier = xModuleManager->identify( _xFrame );
86 : }
87 0 : catch( const Exception& )
88 : {
89 0 : }
90 : }
91 :
92 0 : Reference< XNameAccess > xNameAccess = frame::theUICommandDescription::get( _xContext );
93 0 : xNameAccess->getByName( _rModuleIdentifier ) >>= _xUICommandLabels;
94 : }
95 0 : catch ( const Exception& )
96 : {
97 : }
98 : }
99 :
100 0 : if ( _xUICommandLabels.is() )
101 : {
102 : try
103 : {
104 0 : if ( !aCmdURL.isEmpty() )
105 : {
106 0 : OUString aStr;
107 0 : Sequence< PropertyValue > aPropSeq;
108 0 : if( _xUICommandLabels->hasByName( aCmdURL ) )
109 : {
110 0 : if ( _xUICommandLabels->getByName( aCmdURL ) >>= aPropSeq )
111 : {
112 0 : for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
113 : {
114 0 : if ( aPropSeq[i].Name.equalsAscii( _pName/*"Label"*/ ))
115 : {
116 0 : aPropSeq[i].Value >>= aStr;
117 0 : break;
118 : }
119 : }
120 : }
121 : }
122 0 : aLabel = aStr;
123 : }
124 : }
125 0 : catch ( const com::sun::star::uno::Exception& )
126 : {
127 : }
128 : }
129 :
130 0 : return aLabel;
131 : }
132 :
133 0 : void FillLangItems( std::set< OUString > &rLangItems,
134 : const SvtLanguageTable & rLanguageTable,
135 : const uno::Reference< frame::XFrame > & rxFrame,
136 : const LanguageGuessingHelper & rLangGuessHelper,
137 : sal_Int16 nScriptType,
138 : const OUString & rCurLang,
139 : const OUString & rKeyboardLang,
140 : const OUString & rGuessedTextLang )
141 : {
142 0 : rLangItems.clear();
143 :
144 : //1--add current language
145 0 : if( !rCurLang.isEmpty() &&
146 0 : LANGUAGE_DONTKNOW != rLanguageTable.GetType( rCurLang ))
147 0 : rLangItems.insert( rCurLang );
148 :
149 : //2--System
150 0 : const AllSettings& rAllSettings = Application::GetSettings();
151 0 : LanguageType rSystemLanguage = rAllSettings.GetLanguageTag().getLanguageType();
152 0 : if( rSystemLanguage != LANGUAGE_DONTKNOW )
153 : {
154 0 : if ( IsScriptTypeMatchingToLanguage( nScriptType, rSystemLanguage ))
155 0 : rLangItems.insert( OUString( rLanguageTable.GetString( rSystemLanguage )) );
156 : }
157 :
158 : //3--UI
159 0 : LanguageType rUILanguage = rAllSettings.GetUILanguageTag().getLanguageType();
160 0 : if( rUILanguage != LANGUAGE_DONTKNOW )
161 : {
162 0 : if ( IsScriptTypeMatchingToLanguage( nScriptType, rUILanguage ))
163 0 : rLangItems.insert( OUString( rLanguageTable.GetString( rUILanguage )) );
164 : }
165 :
166 : //4--guessed language
167 0 : uno::Reference< linguistic2::XLanguageGuessing > xLangGuesser( rLangGuessHelper.GetGuesser() );
168 0 : if ( xLangGuesser.is() && !rGuessedTextLang.isEmpty())
169 : {
170 0 : ::com::sun::star::lang::Locale aLocale(xLangGuesser->guessPrimaryLanguage( rGuessedTextLang, 0, rGuessedTextLang.getLength()) );
171 0 : LanguageType nLang = LanguageTag( aLocale ).makeFallback().getLanguageType();
172 0 : if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_NONE && nLang != LANGUAGE_SYSTEM
173 0 : && IsScriptTypeMatchingToLanguage( nScriptType, nLang ))
174 0 : rLangItems.insert( rLanguageTable.GetString( nLang ));
175 : }
176 :
177 : //5--keyboard language
178 0 : if( !rKeyboardLang.isEmpty() )
179 : {
180 0 : if ( IsScriptTypeMatchingToLanguage( nScriptType, rLanguageTable.GetType( rKeyboardLang )))
181 0 : rLangItems.insert( rKeyboardLang );
182 : }
183 :
184 : //6--all languages used in current document
185 0 : Reference< com::sun::star::frame::XModel > xModel;
186 0 : if ( rxFrame.is() )
187 : {
188 0 : Reference< com::sun::star::frame::XController > xController( rxFrame->getController(), UNO_QUERY );
189 0 : if ( xController.is() )
190 0 : xModel = xController->getModel();
191 : }
192 0 : Reference< document::XDocumentLanguages > xDocumentLanguages( xModel, UNO_QUERY );
193 : /*the description of nScriptType
194 : LATIN : 0x001
195 : ASIAN : 0x002
196 : COMPLEX: 0x004
197 : */
198 0 : const sal_Int16 nMaxCount = 7;
199 0 : if ( xDocumentLanguages.is() )
200 : {
201 0 : Sequence< Locale > rLocales( xDocumentLanguages->getDocumentLanguages( nScriptType, nMaxCount ));
202 0 : if ( rLocales.getLength() > 0 )
203 : {
204 0 : for ( sal_uInt16 i = 0; i < rLocales.getLength(); ++i )
205 : {
206 0 : if ( rLangItems.size() == static_cast< size_t >(nMaxCount) )
207 0 : break;
208 0 : const Locale& rLocale=rLocales[i];
209 0 : if( IsScriptTypeMatchingToLanguage( nScriptType, rLanguageTable.GetType( rLocale.Language )))
210 0 : rLangItems.insert( OUString( rLocale.Language ) );
211 : }
212 0 : }
213 0 : }
214 0 : }
215 :
216 : } // namespace framework
217 :
218 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|