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::lang
;
39 uno::Reference
< linguistic2::XLanguageGuessing
> const & LanguageGuessingHelper::GetGuesser() const
41 if (!m_xLanguageGuesser
.is())
45 m_xLanguageGuesser
= linguistic2::LanguageGuessing::create( m_xContext
);
47 catch (const uno::Exception
&)
49 SAL_WARN( "fwk", "failed to get language guessing component" );
52 return m_xLanguageGuesser
;
55 void FillLangItems( std::set
< OUString
> &rLangItems
,
56 const uno::Reference
< frame::XFrame
> & rxFrame
,
57 const LanguageGuessingHelper
& rLangGuessHelper
,
58 SvtScriptType nScriptType
,
59 const OUString
& rCurLang
,
60 const OUString
& rKeyboardLang
,
61 const OUString
& rGuessedTextLang
)
65 //1--add current language
66 if( !rCurLang
.isEmpty() &&
67 LANGUAGE_DONTKNOW
!= SvtLanguageTable::GetLanguageType( rCurLang
))
68 rLangItems
.insert( rCurLang
);
71 const AllSettings
& rAllSettings
= Application::GetSettings();
72 LanguageType rSystemLanguage
= rAllSettings
.GetLanguageTag().getLanguageType();
73 if( rSystemLanguage
!= LANGUAGE_DONTKNOW
)
75 if ( IsScriptTypeMatchingToLanguage( nScriptType
, rSystemLanguage
))
76 rLangItems
.insert( SvtLanguageTable::GetLanguageString( rSystemLanguage
) );
80 LanguageType rUILanguage
= rAllSettings
.GetUILanguageTag().getLanguageType();
81 if( rUILanguage
!= LANGUAGE_DONTKNOW
)
83 if ( IsScriptTypeMatchingToLanguage( nScriptType
, rUILanguage
))
84 rLangItems
.insert( SvtLanguageTable::GetLanguageString( rUILanguage
) );
88 const uno::Reference
< linguistic2::XLanguageGuessing
>& xLangGuesser( rLangGuessHelper
.GetGuesser() );
89 if ( xLangGuesser
.is() && !rGuessedTextLang
.isEmpty())
91 css::lang::Locale
aLocale(xLangGuesser
->guessPrimaryLanguage( rGuessedTextLang
, 0, rGuessedTextLang
.getLength()) );
92 LanguageType nLang
= LanguageTag( aLocale
).makeFallback().getLanguageType();
93 if (nLang
!= LANGUAGE_DONTKNOW
&& nLang
!= LANGUAGE_NONE
&& nLang
!= LANGUAGE_SYSTEM
94 && IsScriptTypeMatchingToLanguage( nScriptType
, nLang
))
95 rLangItems
.insert( SvtLanguageTable::GetLanguageString( nLang
));
98 //5--keyboard language
99 if( !rKeyboardLang
.isEmpty() )
101 if ( IsScriptTypeMatchingToLanguage( nScriptType
, SvtLanguageTable::GetLanguageType( rKeyboardLang
)))
102 rLangItems
.insert( rKeyboardLang
);
105 //6--all languages used in current document
106 Reference
< css::frame::XModel
> xModel
;
109 Reference
< css::frame::XController
> xController
= rxFrame
->getController();
110 if ( xController
.is() )
111 xModel
= xController
->getModel();
113 Reference
< document::XDocumentLanguages
> xDocumentLanguages( xModel
, UNO_QUERY
);
114 /*the description of nScriptType
119 const sal_Int16 nMaxCount
= 7;
120 if ( !xDocumentLanguages
.is() )
123 const Sequence
< Locale
> rLocales( xDocumentLanguages
->getDocumentLanguages( static_cast<sal_Int16
>(nScriptType
), nMaxCount
));
124 for ( const Locale
& rLocale
: rLocales
)
126 if ( rLangItems
.size() == static_cast< size_t >(nMaxCount
) )
128 if( IsScriptTypeMatchingToLanguage( nScriptType
, SvtLanguageTable::GetLanguageType( rLocale
.Language
)))
129 rLangItems
.insert( rLocale
.Language
);
133 auto (*g_pGetMultiplexerListener
)(
134 css::uno::Reference
<css::uno::XComponentContext
> const & xComponentContext
,
135 uno::Reference
<uno::XInterface
> const&,
136 std::function
<bool (uno::Reference
<ui::XContextChangeEventListener
> const&)> const&)
137 -> uno::Reference
<ui::XContextChangeEventListener
> = nullptr;
139 uno::Reference
<ui::XContextChangeEventListener
>
140 GetFirstListenerWith_Impl(
141 css::uno::Reference
<css::uno::XComponentContext
> const & xComponentContext
,
142 uno::Reference
<uno::XInterface
> const& xEventFocus
,
143 std::function
<bool (uno::Reference
<ui::XContextChangeEventListener
> const&)> const& rPredicate
)
145 assert(g_pGetMultiplexerListener
!= nullptr); // should not be called too early, nor too late
146 return g_pGetMultiplexerListener(xComponentContext
, xEventFocus
, rPredicate
);
150 } // namespace framework
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */