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