LibreOffice Module framework (master) 1
mischelper.cxx
Go to the documentation of this file.
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/linguistic2/LanguageGuessing.hpp>
23
24#include <sal/log.hxx>
25#include <vcl/settings.hxx>
26#include <vcl/svapp.hxx>
28#include <svtools/langtab.hxx>
29#include <helper/mischelper.hxx>
30
31using namespace ::com::sun::star;
32using namespace ::com::sun::star::uno;
33using namespace ::com::sun::star::frame;
34using namespace ::com::sun::star::beans;
35using namespace ::com::sun::star::container;
36using namespace ::com::sun::star::lang;
37
38namespace framework
39{
40
41uno::Reference< linguistic2::XLanguageGuessing > const & LanguageGuessingHelper::GetGuesser() const
42{
43 if (!m_xLanguageGuesser.is())
44 {
45 try
46 {
47 m_xLanguageGuesser = linguistic2::LanguageGuessing::create( m_xContext );
48 }
49 catch (const uno::Exception &)
50 {
51 SAL_WARN( "fwk", "failed to get language guessing component" );
52 }
53 }
54 return m_xLanguageGuesser;
55}
56
57void 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 )
64{
65 rLangItems.clear();
66
67 //1--add current language
68 if( !rCurLang.isEmpty() &&
70 rLangItems.insert( rCurLang );
71
72 //2--System
73 const AllSettings& rAllSettings = Application::GetSettings();
74 LanguageType rSystemLanguage = rAllSettings.GetLanguageTag().getLanguageType();
75 if( rSystemLanguage != LANGUAGE_DONTKNOW )
76 {
77 if ( IsScriptTypeMatchingToLanguage( nScriptType, rSystemLanguage ))
78 rLangItems.insert( SvtLanguageTable::GetLanguageString( rSystemLanguage ) );
79 }
80
81 //3--UI
82 LanguageType rUILanguage = rAllSettings.GetUILanguageTag().getLanguageType();
83 if( rUILanguage != LANGUAGE_DONTKNOW )
84 {
85 if ( IsScriptTypeMatchingToLanguage( nScriptType, rUILanguage ))
86 rLangItems.insert( SvtLanguageTable::GetLanguageString( rUILanguage ) );
87 }
88
89 //4--guessed language
90 const uno::Reference< linguistic2::XLanguageGuessing >& xLangGuesser( rLangGuessHelper.GetGuesser() );
91 if ( xLangGuesser.is() && !rGuessedTextLang.isEmpty())
92 {
93 css::lang::Locale aLocale(xLangGuesser->guessPrimaryLanguage( rGuessedTextLang, 0, rGuessedTextLang.getLength()) );
95 if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_NONE && nLang != LANGUAGE_SYSTEM
96 && IsScriptTypeMatchingToLanguage( nScriptType, nLang ))
97 rLangItems.insert( SvtLanguageTable::GetLanguageString( nLang ));
98 }
99
100 //5--keyboard language
101 if( !rKeyboardLang.isEmpty() )
102 {
103 if ( IsScriptTypeMatchingToLanguage( nScriptType, SvtLanguageTable::GetLanguageType( rKeyboardLang )))
104 rLangItems.insert( rKeyboardLang );
105 }
106
107 //6--all languages used in current document
108 Reference< css::frame::XModel > xModel;
109 if ( rxFrame.is() )
110 {
111 Reference< css::frame::XController > xController = rxFrame->getController();
112 if ( xController.is() )
113 xModel = xController->getModel();
114 }
115 Reference< document::XDocumentLanguages > xDocumentLanguages( xModel, UNO_QUERY );
116 /*the description of nScriptType
117 LATIN : 0x001
118 ASIAN : 0x002
119 COMPLEX: 0x004
120 */
121 const sal_Int16 nMaxCount = 7;
122 if ( !xDocumentLanguages.is() )
123 return;
124
125 const Sequence< Locale > rLocales( xDocumentLanguages->getDocumentLanguages( static_cast<sal_Int16>(nScriptType), nMaxCount ));
126 for ( const Locale& rLocale : rLocales )
127 {
128 if ( rLangItems.size() == static_cast< size_t >(nMaxCount) )
129 break;
130 if( IsScriptTypeMatchingToLanguage( nScriptType, SvtLanguageTable::GetLanguageType( rLocale.Language )))
131 rLangItems.insert( rLocale.Language );
132 }
133}
134
135auto (*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;
140
141uno::Reference<ui::XContextChangeEventListener>
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)
146{
147 assert(g_pGetMultiplexerListener != nullptr); // should not be called too early, nor too late
148 return g_pGetMultiplexerListener(xComponentContext, xEventFocus, rPredicate);
149}
150
151
152} // namespace framework
153
154/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const LanguageTag & GetLanguageTag() const
const LanguageTag & GetUILanguageTag() const
static const AllSettings & GetSettings()
LanguageType getLanguageType(bool bResolveSystem=true) const
LanguageTag & makeFallback()
static OUString GetLanguageString(const LanguageType eType)
static LanguageType GetLanguageType(std::u16string_view rStr)
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: mischelper.hxx:111
css::uno::Reference< css::linguistic2::XLanguageGuessing > const & GetGuesser() const
Definition: mischelper.cxx:41
css::uno::Reference< css::linguistic2::XLanguageGuessing > m_xLanguageGuesser
Definition: mischelper.hxx:110
#define LANGUAGE_SYSTEM
#define LANGUAGE_NONE
#define LANGUAGE_DONTKNOW
SvtScriptType
#define SAL_WARN(area, stream)
auto(* g_pGetMultiplexerListener)(css::uno::Reference< css::uno::XComponentContext > const &xComponentContext, uno::Reference< uno::XInterface > const &, std::function< bool(uno::Reference< ui::XContextChangeEventListener > const &)> const &) -> uno::Reference< ui::XContextChangeEventListener >=nullptr
Definition: mischelper.cxx:135
bool IsScriptTypeMatchingToLanguage(SvtScriptType nScriptType, LanguageType nLang)
Definition: mischelper.hxx:81
void FillLangItems(std::set< OUString > &rLangItems, const uno::Reference< frame::XFrame > &rxFrame, const LanguageGuessingHelper &rLangGuessHelper, SvtScriptType nScriptType, const OUString &rCurLang, const OUString &rKeyboardLang, const OUString &rGuessedTextLang)
Definition: mischelper.cxx:57
uno::Reference< ui::XContextChangeEventListener > GetFirstListenerWith_Impl(css::uno::Reference< css::uno::XComponentContext > const &xComponentContext, uno::Reference< uno::XInterface > const &xEventFocus, std::function< bool(uno::Reference< ui::XContextChangeEventListener > const &)> const &rPredicate)
Definition: mischelper.cxx:142
Reference< XController > xController
Reference< XModel > xModel