1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
30 
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>
35 
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>
44 
45 
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;
52 
53 using ::rtl::OUString;
54 
55 
56 namespace framework
57 {
58 
59 uno::Reference< linguistic2::XLanguageGuessing > LanguageGuessingHelper::GetGuesser() const
60 {
61     if (!m_xLanguageGuesser.is())
62     {
63         try
64         {
65             m_xLanguageGuesser = uno::Reference< linguistic2::XLanguageGuessing >(
66                     m_xServiceManager->createInstance(
67                         rtl::OUString::createFromAscii( "com.sun.star.linguistic2.LanguageGuessing" ) ),
68                         uno::UNO_QUERY );
69         }
70         catch (uno::Exception &r)
71         {
72             (void) r;
73             DBG_ASSERT( 0, "failed to get language guessing component" );
74         }
75     }
76     return m_xLanguageGuesser;
77 }
78 
79 ////////////////////////////////////////////////////////////
80 
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)
89 {
90     ::rtl::OUString aLabel;
91 
92     // Retrieve popup menu labels
93     if ( !_xUICommandLabels.is() )
94     {
95       try
96         {
97             if ( !_rIni )
98             {
99                 _rIni = sal_True;
100                 Reference< XModuleManager > xModuleManager( _xServiceFactory->createInstance( SERVICENAME_MODULEMANAGER ), UNO_QUERY_THROW );
101 
102                 try
103                 {
104                     _rModuleIdentifier = xModuleManager->identify( _xFrame );
105                 }
106                 catch( Exception& )
107                 {
108                 }
109             }
110 
111             Reference< XNameAccess > xNameAccess( _xServiceFactory->createInstance( SERVICENAME_UICOMMANDDESCRIPTION ), UNO_QUERY );
112             if ( xNameAccess.is() )
113             {
114                 xNameAccess->getByName( _rModuleIdentifier ) >>= _xUICommandLabels;
115             }
116         }
117         catch ( Exception& )
118         {
119         }
120     }
121 
122     if ( _xUICommandLabels.is() )
123     {
124         try
125         {
126             if ( aCmdURL.getLength() > 0 )
127             {
128                 rtl::OUString aStr;
129                 Sequence< PropertyValue > aPropSeq;
130 				if( _xUICommandLabels->hasByName( aCmdURL ) )
131 				{
132 					if ( _xUICommandLabels->getByName( aCmdURL ) >>= aPropSeq )
133 					{
134 						for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
135 						{
136 							if ( aPropSeq[i].Name.equalsAscii( _pName/*"Label"*/ ))
137 							{
138 								aPropSeq[i].Value >>= aStr;
139 								break;
140 							}
141 						}
142 					}
143 				}
144                 aLabel = aStr;
145             }
146         }
147         catch ( com::sun::star::uno::Exception& )
148         {
149         }
150     }
151 
152     return aLabel;
153 }
154 
155 ////////////////////////////////////////////////////////////
156 
157 void FillLangItems( std::set< OUString > &rLangItems,
158         const SvtLanguageTable &    rLanguageTable,
159         const uno::Reference< frame::XFrame > & rxFrame,
160         const LanguageGuessingHelper & rLangGuessHelper,
161         sal_Int16        nScriptType,
162         const OUString & rCurLang,
163         const OUString & rKeyboardLang,
164         const OUString & rGuessedTextLang )
165 {
166     rLangItems.clear();
167 
168     //1--add current language
169     if( rCurLang != OUString() &&
170         LANGUAGE_DONTKNOW != rLanguageTable.GetType( rCurLang ))
171         rLangItems.insert( rCurLang );
172 
173     //2--System
174     const AllSettings& rAllSettings = Application::GetSettings();
175     LanguageType rSystemLanguage = rAllSettings.GetLanguage();
176     if( rSystemLanguage != LANGUAGE_DONTKNOW )
177     {
178         if ( IsScriptTypeMatchingToLanguage( nScriptType, rSystemLanguage ))
179             rLangItems.insert( OUString( rLanguageTable.GetString( rSystemLanguage )) );
180     }
181 
182     //3--UI
183     LanguageType rUILanguage = rAllSettings.GetUILanguage();
184     if( rUILanguage != LANGUAGE_DONTKNOW )
185     {
186         if ( IsScriptTypeMatchingToLanguage( nScriptType, rUILanguage ))
187             rLangItems.insert( OUString( rLanguageTable.GetString( rUILanguage )) );
188     }
189 
190     //4--guessed language
191     uno::Reference< linguistic2::XLanguageGuessing > xLangGuesser( rLangGuessHelper.GetGuesser() );
192     if ( xLangGuesser.is() && rGuessedTextLang.getLength() > 0)
193     {
194         ::com::sun::star::lang::Locale aLocale(xLangGuesser->guessPrimaryLanguage( rGuessedTextLang, 0, rGuessedTextLang.getLength()) );
195         LanguageType nLang = MsLangId::convertLocaleToLanguageWithFallback( aLocale );
196         if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_NONE && nLang != LANGUAGE_SYSTEM
197             && IsScriptTypeMatchingToLanguage( nScriptType, nLang ))
198             rLangItems.insert( rLanguageTable.GetString( nLang ));
199     }
200 
201     //5--keyboard language
202     if( rKeyboardLang != OUString())
203     {
204         if ( IsScriptTypeMatchingToLanguage( nScriptType, rLanguageTable.GetType( rKeyboardLang )))
205             rLangItems.insert( rKeyboardLang );
206     }
207 
208     //6--all languages used in current document
209     Reference< com::sun::star::frame::XModel > xModel;
210     if ( rxFrame.is() )
211     {
212        Reference< com::sun::star::frame::XController > xController( rxFrame->getController(), UNO_QUERY );
213        if ( xController.is() )
214            xModel = xController->getModel();
215     }
216     Reference< document::XDocumentLanguages > xDocumentLanguages( xModel, UNO_QUERY );
217     /*the description of nScriptType
218       LATIN :   0x001
219       ASIAN :   0x002
220       COMPLEX:  0x004
221     */
222     const sal_Int16 nMaxCount = 7;
223     if ( xDocumentLanguages.is() )
224     {
225         Sequence< Locale > rLocales( xDocumentLanguages->getDocumentLanguages( nScriptType, nMaxCount ));
226         if ( rLocales.getLength() > 0 )
227         {
228             for ( sal_uInt16 i = 0; i < rLocales.getLength(); ++i )
229             {
230                 if ( rLangItems.size() == static_cast< size_t >(nMaxCount) )
231                     break;
232                 const Locale& rLocale=rLocales[i];
233                 if( IsScriptTypeMatchingToLanguage( nScriptType, rLanguageTable.GetType( rLocale.Language )))
234                     rLangItems.insert( OUString( rLocale.Language ) );
235             }
236         }
237     }
238 }
239 
240 } // namespace framework
241 
242 
243