1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/document/XDocumentLanguages.hpp>
29 #include <com/sun/star/frame/XModuleManager.hpp>
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 
32 #include <tools/debug.hxx>
33 #include <vcl/settings.hxx>
34 #include <vcl/svapp.hxx>
35 #include <i18npool/mslangid.hxx>
36 #include <svtools/langtab.hxx>
37 #include <comphelper/processfactory.hxx>
38 #include <helper/mischelper.hxx>
39 #include <services.h>
40 
41 
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::frame;
45 using namespace ::com::sun::star::beans;
46 using namespace ::com::sun::star::container;
47 using namespace ::com::sun::star::lang;
48 
49 using ::rtl::OUString;
50 
51 
52 namespace framework
53 {
54 
GetGuesser() const55 uno::Reference< linguistic2::XLanguageGuessing > LanguageGuessingHelper::GetGuesser() const
56 {
57     if (!m_xLanguageGuesser.is())
58     {
59         try
60         {
61             m_xLanguageGuesser = uno::Reference< linguistic2::XLanguageGuessing >(
62                     m_xServiceManager->createInstance(
63                         rtl::OUString::createFromAscii( "com.sun.star.linguistic2.LanguageGuessing" ) ),
64                         uno::UNO_QUERY );
65         }
66         catch (uno::Exception &r)
67         {
68             (void) r;
69             DBG_ASSERT( 0, "failed to get language guessing component" );
70         }
71     }
72     return m_xLanguageGuesser;
73 }
74 
75 ////////////////////////////////////////////////////////////
76 
RetrieveLabelFromCommand(const::rtl::OUString & aCmdURL,const uno::Reference<lang::XMultiServiceFactory> & _xServiceFactory,uno::Reference<container::XNameAccess> & _xUICommandLabels,const uno::Reference<frame::XFrame> & _xFrame,::rtl::OUString & _rModuleIdentifier,sal_Bool & _rIni,const sal_Char * _pName)77 ::rtl::OUString RetrieveLabelFromCommand(
78     const ::rtl::OUString& aCmdURL,
79     const uno::Reference< lang::XMultiServiceFactory >& _xServiceFactory,
80     uno::Reference< container::XNameAccess >& _xUICommandLabels,
81     const uno::Reference< frame::XFrame >& _xFrame,
82     ::rtl::OUString& _rModuleIdentifier,
83     sal_Bool& _rIni,
84     const sal_Char* _pName)
85 {
86     ::rtl::OUString aLabel;
87 
88     // Retrieve popup menu labels
89     if ( !_xUICommandLabels.is() )
90     {
91       try
92         {
93             if ( !_rIni )
94             {
95                 _rIni = sal_True;
96                 Reference< XModuleManager > xModuleManager( _xServiceFactory->createInstance( SERVICENAME_MODULEMANAGER ), UNO_QUERY_THROW );
97 
98                 try
99                 {
100                     _rModuleIdentifier = xModuleManager->identify( _xFrame );
101                 }
102                 catch( Exception& )
103                 {
104                 }
105             }
106 
107             Reference< XNameAccess > xNameAccess( _xServiceFactory->createInstance( SERVICENAME_UICOMMANDDESCRIPTION ), UNO_QUERY );
108             if ( xNameAccess.is() )
109             {
110                 xNameAccess->getByName( _rModuleIdentifier ) >>= _xUICommandLabels;
111             }
112         }
113         catch ( Exception& )
114         {
115         }
116     }
117 
118     if ( _xUICommandLabels.is() )
119     {
120         try
121         {
122             if ( aCmdURL.getLength() > 0 )
123             {
124                 rtl::OUString aStr;
125                 Sequence< PropertyValue > aPropSeq;
126 				if( _xUICommandLabels->hasByName( aCmdURL ) )
127 				{
128 					if ( _xUICommandLabels->getByName( aCmdURL ) >>= aPropSeq )
129 					{
130 						for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
131 						{
132 							if ( aPropSeq[i].Name.equalsAscii( _pName/*"Label"*/ ))
133 							{
134 								aPropSeq[i].Value >>= aStr;
135 								break;
136 							}
137 						}
138 					}
139 				}
140                 aLabel = aStr;
141             }
142         }
143         catch ( com::sun::star::uno::Exception& )
144         {
145         }
146     }
147 
148     return aLabel;
149 }
150 
151 ////////////////////////////////////////////////////////////
152 
FillLangItems(std::set<OUString> & rLangItems,const SvtLanguageTable & rLanguageTable,const uno::Reference<frame::XFrame> & rxFrame,const LanguageGuessingHelper & rLangGuessHelper,sal_Int16 nScriptType,const OUString & rCurLang,const OUString & rKeyboardLang,const OUString & rGuessedTextLang)153 void FillLangItems( std::set< OUString > &rLangItems,
154         const SvtLanguageTable &    rLanguageTable,
155         const uno::Reference< frame::XFrame > & rxFrame,
156         const LanguageGuessingHelper & rLangGuessHelper,
157         sal_Int16        nScriptType,
158         const OUString & rCurLang,
159         const OUString & rKeyboardLang,
160         const OUString & rGuessedTextLang )
161 {
162     rLangItems.clear();
163 
164     //1--add current language
165     if( rCurLang != OUString() &&
166         LANGUAGE_DONTKNOW != rLanguageTable.GetType( rCurLang ))
167         rLangItems.insert( rCurLang );
168 
169     //2--System
170     const AllSettings& rAllSettings = Application::GetSettings();
171     LanguageType rSystemLanguage = rAllSettings.GetLanguage();
172     if( rSystemLanguage != LANGUAGE_DONTKNOW )
173     {
174         if ( IsScriptTypeMatchingToLanguage( nScriptType, rSystemLanguage ))
175             rLangItems.insert( OUString( rLanguageTable.GetString( rSystemLanguage )) );
176     }
177 
178     //3--UI
179     LanguageType rUILanguage = rAllSettings.GetUILanguage();
180     if( rUILanguage != LANGUAGE_DONTKNOW )
181     {
182         if ( IsScriptTypeMatchingToLanguage( nScriptType, rUILanguage ))
183             rLangItems.insert( OUString( rLanguageTable.GetString( rUILanguage )) );
184     }
185 
186     //4--guessed language
187     uno::Reference< linguistic2::XLanguageGuessing > xLangGuesser( rLangGuessHelper.GetGuesser() );
188     if ( xLangGuesser.is() && rGuessedTextLang.getLength() > 0)
189     {
190         ::com::sun::star::lang::Locale aLocale(xLangGuesser->guessPrimaryLanguage( rGuessedTextLang, 0, rGuessedTextLang.getLength()) );
191         LanguageType nLang = MsLangId::convertLocaleToLanguageWithFallback( aLocale );
192         if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_NONE && nLang != LANGUAGE_SYSTEM
193             && IsScriptTypeMatchingToLanguage( nScriptType, nLang ))
194             rLangItems.insert( rLanguageTable.GetString( nLang ));
195     }
196 
197     //5--keyboard language
198     if( rKeyboardLang != OUString())
199     {
200         if ( IsScriptTypeMatchingToLanguage( nScriptType, rLanguageTable.GetType( rKeyboardLang )))
201             rLangItems.insert( rKeyboardLang );
202     }
203 
204     //6--all languages used in current document
205     Reference< com::sun::star::frame::XModel > xModel;
206     if ( rxFrame.is() )
207     {
208        Reference< com::sun::star::frame::XController > xController( rxFrame->getController(), UNO_QUERY );
209        if ( xController.is() )
210            xModel = xController->getModel();
211     }
212     Reference< document::XDocumentLanguages > xDocumentLanguages( xModel, UNO_QUERY );
213     /*the description of nScriptType
214       LATIN :   0x001
215       ASIAN :   0x002
216       COMPLEX:  0x004
217     */
218     const sal_Int16 nMaxCount = 7;
219     if ( xDocumentLanguages.is() )
220     {
221         Sequence< Locale > rLocales( xDocumentLanguages->getDocumentLanguages( nScriptType, nMaxCount ));
222         if ( rLocales.getLength() > 0 )
223         {
224             for ( sal_uInt16 i = 0; i < rLocales.getLength(); ++i )
225             {
226                 if ( rLangItems.size() == static_cast< size_t >(nMaxCount) )
227                     break;
228                 const Locale& rLocale=rLocales[i];
229                 if( IsScriptTypeMatchingToLanguage( nScriptType, rLanguageTable.GetType( rLocale.Language )))
230                     rLangItems.insert( OUString( rLocale.Language ) );
231             }
232         }
233     }
234 }
235 
236 } // namespace framework
237 
238 
239