tdf#159797 replace dash also between sentences
[LibreOffice.git] / framework / source / uielement / thesaurusmenucontroller.cxx
blob6a834757c3fa8f629490a93d1c3c2115c422f773
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <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>
31 #include <com/sun/star/graphic/GraphicProvider.hpp>
32 #include <com/sun/star/linguistic2/LinguServiceManager.hpp>
34 namespace {
36 class ThesaurusMenuController : public svt::PopupMenuControllerBase
38 public:
39 explicit ThesaurusMenuController( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
41 // XStatusListener
42 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
44 // XServiceInfo
45 virtual OUString SAL_CALL getImplementationName() override;
46 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
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;
59 ThesaurusMenuController::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() )
66 void ThesaurusMenuController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
68 rEvent.State >>= m_aLastWord;
69 m_xPopupMenu->clear();
70 if ( rEvent.IsEnabled )
71 fillPopupMenu();
74 void ThesaurusMenuController::fillPopupMenu()
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;
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*/ );
86 m_xPopupMenu->enableAutoMnemonics(false);
87 if ( aSynonyms.empty() )
88 return;
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())
96 try
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) });
102 catch (const css::uno::Exception&)
104 DBG_UNHANDLED_EXCEPTION("fwk");
108 sal_uInt16 nId = 1;
109 for ( const auto& aSynonym : aSynonyms )
111 OUString aItemText( linguistic::GetThesaurusReplaceText( aSynonym ) );
112 m_xPopupMenu->insertItem(nId, aItemText, 0, -1);
113 m_xPopupMenu->setCommand(nId, ".uno:ThesaurusFromContext?WordReplace:string=" + aItemText);
115 if (xGraphic.is())
116 m_xPopupMenu->setItemImage(nId, xGraphic, false);
118 nId++;
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);
128 void ThesaurusMenuController::getMeanings( std::vector< OUString >& rSynonyms, const OUString& rWord,
129 const css::lang::Locale& rLocale, size_t nMaxSynonms )
131 rSynonyms.clear();
132 if ( !(m_xThesaurus.is() && m_xThesaurus->hasLocale( rLocale ) && !rWord.isEmpty() && nMaxSynonms > 0) )
133 return;
137 const css::uno::Sequence< css::uno::Reference< css::linguistic2::XMeaning > > aMeaningSeq(
138 m_xThesaurus->queryMeanings( rWord, rLocale, css::uno::Sequence< css::beans::PropertyValue >() ) );
140 for ( const auto& xMeaning : aMeaningSeq )
142 const css::uno::Sequence< OUString > aSynonymSeq( xMeaning->querySynonyms() );
143 for ( const auto& aSynonym : aSynonymSeq )
145 rSynonyms.push_back( aSynonym );
146 if ( rSynonyms.size() == nMaxSynonms )
147 return;
151 catch ( const css::uno::Exception& )
153 SAL_WARN( "fwk.uielement", "Failed to get synonyms" );
157 OUString ThesaurusMenuController::getThesImplName( const css::lang::Locale& rLocale ) const
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];
165 return OUString();
168 OUString ThesaurusMenuController::getImplementationName()
170 return "com.sun.star.comp.framework.ThesaurusMenuController";
173 css::uno::Sequence< OUString > ThesaurusMenuController::getSupportedServiceNames()
175 return { "com.sun.star.frame.PopupMenuController" };
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 & )
183 return cppu::acquire( new ThesaurusMenuController( xContext ) );
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */