1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
21 #include <com/sun/star/document/XDocumentLanguages.hpp>
22 #include <com/sun/star/linguistic2/LanguageGuessing.hpp>
24 #include <sal/log.hxx>
25 #include <vcl/settings.hxx>
26 #include <vcl/svapp.hxx>
27 #include <i18nlangtag/languagetag.hxx>
28 #include <svtools/langtab.hxx>
29 #include <helper/mischelper.hxx>
31 using namespace ::com::sun::star
;
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::frame
;
34 using namespace ::com::sun::star::beans
;
35 using namespace ::com::sun::star::container
;
36 using namespace ::com::sun::star::lang
;
41 uno::Reference
< linguistic2::XLanguageGuessing
> const & LanguageGuessingHelper::GetGuesser() const
43 if (!m_xLanguageGuesser
.is())
47 m_xLanguageGuesser
= linguistic2::LanguageGuessing::create( m_xContext
);
49 catch (const uno::Exception
&)
51 SAL_WARN( "fwk", "failed to get language guessing component" );
54 return m_xLanguageGuesser
;
57 void FillLangItems( std::set
< OUString
> &rLangItems
,
58 const uno::Reference
< frame::XFrame
> & rxFrame
,
59 const LanguageGuessingHelper
& rLangGuessHelper
,
60 SvtScriptType nScriptType
,
61 const OUString
& rCurLang
,
62 const OUString
& rKeyboardLang
,
63 const OUString
& rGuessedTextLang
)
67 //1--add current language
68 if( !rCurLang
.isEmpty() &&
69 LANGUAGE_DONTKNOW
!= SvtLanguageTable::GetLanguageType( rCurLang
))
70 rLangItems
.insert( rCurLang
);
73 const AllSettings
& rAllSettings
= Application::GetSettings();
74 LanguageType rSystemLanguage
= rAllSettings
.GetLanguageTag().getLanguageType();
75 if( rSystemLanguage
!= LANGUAGE_DONTKNOW
)
77 if ( IsScriptTypeMatchingToLanguage( nScriptType
, rSystemLanguage
))
78 rLangItems
.insert( SvtLanguageTable::GetLanguageString( rSystemLanguage
) );
82 LanguageType rUILanguage
= rAllSettings
.GetUILanguageTag().getLanguageType();
83 if( rUILanguage
!= LANGUAGE_DONTKNOW
)
85 if ( IsScriptTypeMatchingToLanguage( nScriptType
, rUILanguage
))
86 rLangItems
.insert( SvtLanguageTable::GetLanguageString( rUILanguage
) );
90 const uno::Reference
< linguistic2::XLanguageGuessing
>& xLangGuesser( rLangGuessHelper
.GetGuesser() );
91 if ( xLangGuesser
.is() && !rGuessedTextLang
.isEmpty())
93 css::lang::Locale
aLocale(xLangGuesser
->guessPrimaryLanguage( rGuessedTextLang
, 0, rGuessedTextLang
.getLength()) );
94 LanguageType nLang
= LanguageTag( aLocale
).makeFallback().getLanguageType();
95 if (nLang
!= LANGUAGE_DONTKNOW
&& nLang
!= LANGUAGE_NONE
&& nLang
!= LANGUAGE_SYSTEM
96 && IsScriptTypeMatchingToLanguage( nScriptType
, nLang
))
97 rLangItems
.insert( SvtLanguageTable::GetLanguageString( nLang
));
100 //5--keyboard language
101 if( !rKeyboardLang
.isEmpty() )
103 if ( IsScriptTypeMatchingToLanguage( nScriptType
, SvtLanguageTable::GetLanguageType( rKeyboardLang
)))
104 rLangItems
.insert( rKeyboardLang
);
107 //6--all languages used in current document
108 Reference
< css::frame::XModel
> xModel
;
111 Reference
< css::frame::XController
> xController
= rxFrame
->getController();
112 if ( xController
.is() )
113 xModel
= xController
->getModel();
115 Reference
< document::XDocumentLanguages
> xDocumentLanguages( xModel
, UNO_QUERY
);
116 /*the description of nScriptType
121 const sal_Int16 nMaxCount
= 7;
122 if ( !xDocumentLanguages
.is() )
125 const Sequence
< Locale
> rLocales( xDocumentLanguages
->getDocumentLanguages( static_cast<sal_Int16
>(nScriptType
), nMaxCount
));
126 for ( const Locale
& rLocale
: rLocales
)
128 if ( rLangItems
.size() == static_cast< size_t >(nMaxCount
) )
130 if( IsScriptTypeMatchingToLanguage( nScriptType
, SvtLanguageTable::GetLanguageType( rLocale
.Language
)))
131 rLangItems
.insert( rLocale
.Language
);
135 auto (*g_pGetMultiplexerListener
)(
136 css::uno::Reference
<css::uno::XComponentContext
> const & xComponentContext
,
137 uno::Reference
<uno::XInterface
> const&,
138 std::function
<bool (uno::Reference
<ui::XContextChangeEventListener
> const&)> const&)
139 -> uno::Reference
<ui::XContextChangeEventListener
> = nullptr;
141 uno::Reference
<ui::XContextChangeEventListener
>
142 GetFirstListenerWith_Impl(
143 css::uno::Reference
<css::uno::XComponentContext
> const & xComponentContext
,
144 uno::Reference
<uno::XInterface
> const& xEventFocus
,
145 std::function
<bool (uno::Reference
<ui::XContextChangeEventListener
> const&)> const& rPredicate
)
147 assert(g_pGetMultiplexerListener
!= nullptr); // should not be called too early, nor too late
148 return g_pGetMultiplexerListener(xComponentContext
, xEventFocus
, rPredicate
);
152 } // namespace framework
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */