/src/libreoffice/framework/source/uielement/thesaurusmenucontroller.cxx
Line | Count | Source |
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 <comphelper/processfactory.hxx> |
21 | | #include <comphelper/propertyvalue.hxx> |
22 | | #include <i18nlangtag/languagetag.hxx> |
23 | | #include <sal/log.hxx> |
24 | | #include <svl/lngmisc.hxx> |
25 | | #include <svtools/popupmenucontrollerbase.hxx> |
26 | | #include <comphelper/diagnose_ex.hxx> |
27 | | #include <toolkit/awt/vclxmenu.hxx> |
28 | | #include <unotools/lingucfg.hxx> |
29 | | #include <vcl/commandinfoprovider.hxx> |
30 | | |
31 | | #include <com/sun/star/graphic/GraphicProvider.hpp> |
32 | | #include <com/sun/star/linguistic2/LinguServiceManager.hpp> |
33 | | |
34 | | namespace { |
35 | | |
36 | | class ThesaurusMenuController : public svt::PopupMenuControllerBase |
37 | | { |
38 | | public: |
39 | | explicit ThesaurusMenuController( const css::uno::Reference< css::uno::XComponentContext >& rxContext ); |
40 | | |
41 | | // XStatusListener |
42 | | virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override; |
43 | | |
44 | | // XServiceInfo |
45 | | virtual OUString SAL_CALL getImplementationName() override; |
46 | | virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override; |
47 | | |
48 | | private: |
49 | | void fillPopupMenu(); |
50 | | void getMeanings( std::vector< OUString >& rSynonyms, const OUString& rWord, const css::lang::Locale& rLocale, size_t nMaxSynonms ); |
51 | | OUString getThesImplName( const css::lang::Locale& rLocale ) const; |
52 | | css::uno::Reference< css::linguistic2::XLinguServiceManager2 > m_xLinguServiceManager; |
53 | | css::uno::Reference< css::linguistic2::XThesaurus > m_xThesaurus; |
54 | | OUString m_aLastWord; |
55 | | }; |
56 | | |
57 | | } |
58 | | |
59 | | ThesaurusMenuController::ThesaurusMenuController( const css::uno::Reference< css::uno::XComponentContext >& rxContext ) : |
60 | 0 | svt::PopupMenuControllerBase( rxContext ), |
61 | 0 | m_xLinguServiceManager( css::linguistic2::LinguServiceManager::create( rxContext ) ), |
62 | 0 | m_xThesaurus( m_xLinguServiceManager->getThesaurus() ) |
63 | 0 | { |
64 | 0 | } |
65 | | |
66 | | void ThesaurusMenuController::statusChanged( const css::frame::FeatureStateEvent& rEvent ) |
67 | 0 | { |
68 | 0 | rEvent.State >>= m_aLastWord; |
69 | 0 | m_xPopupMenu->clear(); |
70 | 0 | if ( rEvent.IsEnabled ) |
71 | 0 | fillPopupMenu(); |
72 | 0 | } |
73 | | |
74 | | void ThesaurusMenuController::fillPopupMenu() |
75 | 0 | { |
76 | 0 | sal_Int32 nIdx{ 0 }; |
77 | 0 | OUString aText = m_aLastWord.getToken(0, '#', nIdx); |
78 | 0 | OUString aIsoLang = m_aLastWord.getToken(0, '#', nIdx); |
79 | 0 | if ( aText.isEmpty() || aIsoLang.isEmpty() ) |
80 | 0 | return; |
81 | | |
82 | 0 | std::vector< OUString > aSynonyms; |
83 | 0 | css::lang::Locale aLocale = LanguageTag::convertToLocale( aIsoLang ); |
84 | 0 | getMeanings( aSynonyms, aText, aLocale, 7 /*max number of synonyms to retrieve*/ ); |
85 | |
|
86 | 0 | m_xPopupMenu->enableAutoMnemonics(false); |
87 | 0 | if ( aSynonyms.empty() ) |
88 | 0 | return; |
89 | | |
90 | 0 | SvtLinguConfig aCfg; |
91 | 0 | css::uno::Reference<css::graphic::XGraphic> xGraphic; |
92 | 0 | OUString aThesImplName( getThesImplName( aLocale ) ); |
93 | 0 | OUString aSynonymsImageUrl( aCfg.GetSynonymsContextImage( aThesImplName ) ); |
94 | 0 | if (!aThesImplName.isEmpty() && !aSynonymsImageUrl.isEmpty()) |
95 | 0 | { |
96 | 0 | try |
97 | 0 | { |
98 | 0 | const css::uno::Reference<css::uno::XComponentContext>& xContext(::comphelper::getProcessComponentContext()); |
99 | 0 | css::uno::Reference<css::graphic::XGraphicProvider> xProvider(css::graphic::GraphicProvider::create(xContext)); |
100 | 0 | xGraphic = xProvider->queryGraphic({ comphelper::makePropertyValue(u"URL"_ustr, aSynonymsImageUrl) }); |
101 | 0 | } |
102 | 0 | catch (const css::uno::Exception&) |
103 | 0 | { |
104 | 0 | DBG_UNHANDLED_EXCEPTION("fwk"); |
105 | 0 | } |
106 | 0 | } |
107 | |
|
108 | 0 | sal_uInt16 nId = 1; |
109 | 0 | for ( const auto& aSynonym : aSynonyms ) |
110 | 0 | { |
111 | 0 | OUString aItemText( linguistic::GetThesaurusReplaceText( aSynonym ) ); |
112 | 0 | m_xPopupMenu->insertItem(nId, aItemText, 0, -1); |
113 | 0 | m_xPopupMenu->setCommand(nId, ".uno:ThesaurusFromContext?WordReplace:string=" + aItemText); |
114 | |
|
115 | 0 | if (xGraphic.is()) |
116 | 0 | m_xPopupMenu->setItemImage(nId, xGraphic, false); |
117 | |
|
118 | 0 | nId++; |
119 | 0 | } |
120 | |
|
121 | 0 | m_xPopupMenu->insertSeparator(-1); |
122 | 0 | OUString aThesaurusDialogCmd( u".uno:ThesaurusDialog"_ustr ); |
123 | 0 | auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(aThesaurusDialogCmd, m_aModuleName); |
124 | 0 | m_xPopupMenu->insertItem(nId, vcl::CommandInfoProvider::GetPopupLabelForCommand(aProperties), 0, -1); |
125 | 0 | m_xPopupMenu->setCommand(nId, aThesaurusDialogCmd); |
126 | 0 | } |
127 | | |
128 | | void ThesaurusMenuController::getMeanings( std::vector< OUString >& rSynonyms, const OUString& rWord, |
129 | | const css::lang::Locale& rLocale, size_t nMaxSynonms ) |
130 | 0 | { |
131 | 0 | rSynonyms.clear(); |
132 | 0 | if ( !(m_xThesaurus.is() && m_xThesaurus->hasLocale( rLocale ) && !rWord.isEmpty() && nMaxSynonms > 0) ) |
133 | 0 | return; |
134 | | |
135 | 0 | try |
136 | 0 | { |
137 | 0 | const css::uno::Sequence< css::uno::Reference< css::linguistic2::XMeaning > > aMeaningSeq( |
138 | 0 | m_xThesaurus->queryMeanings( rWord, rLocale, css::uno::Sequence< css::beans::PropertyValue >() ) ); |
139 | |
|
140 | 0 | for ( const auto& xMeaning : aMeaningSeq ) |
141 | 0 | { |
142 | 0 | const css::uno::Sequence< OUString > aSynonymSeq( xMeaning->querySynonyms() ); |
143 | 0 | for ( const auto& aSynonym : aSynonymSeq ) |
144 | 0 | { |
145 | 0 | rSynonyms.push_back( aSynonym ); |
146 | 0 | if ( rSynonyms.size() == nMaxSynonms ) |
147 | 0 | return; |
148 | 0 | } |
149 | 0 | } |
150 | 0 | } |
151 | 0 | catch ( const css::uno::Exception& ) |
152 | 0 | { |
153 | 0 | SAL_WARN( "fwk.uielement", "Failed to get synonyms" ); |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | | OUString ThesaurusMenuController::getThesImplName( const css::lang::Locale& rLocale ) const |
158 | 0 | { |
159 | 0 | css::uno::Sequence< OUString > aServiceNames = |
160 | 0 | m_xLinguServiceManager->getConfiguredServices( u"com.sun.star.linguistic2.Thesaurus"_ustr, rLocale ); |
161 | 0 | SAL_WARN_IF( aServiceNames.getLength() > 1, "fwk.uielement", "Only one thesaurus is allowed per locale, but found more!" ); |
162 | 0 | if ( aServiceNames.getLength() == 1 ) |
163 | 0 | return aServiceNames[0]; |
164 | | |
165 | 0 | return OUString(); |
166 | 0 | } |
167 | | |
168 | | OUString ThesaurusMenuController::getImplementationName() |
169 | 0 | { |
170 | 0 | return u"com.sun.star.comp.framework.ThesaurusMenuController"_ustr; |
171 | 0 | } |
172 | | |
173 | | css::uno::Sequence< OUString > ThesaurusMenuController::getSupportedServiceNames() |
174 | 0 | { |
175 | 0 | return { u"com.sun.star.frame.PopupMenuController"_ustr }; |
176 | 0 | } |
177 | | |
178 | | extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * |
179 | | com_sun_star_comp_framework_ThesaurusMenuController_get_implementation( |
180 | | css::uno::XComponentContext* xContext, |
181 | | css::uno::Sequence< css::uno::Any > const & ) |
182 | 0 | { |
183 | 0 | return cppu::acquire( new ThesaurusMenuController( xContext ) ); |
184 | 0 | } |
185 | | |
186 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |