31#include <com/sun/star/graphic/GraphicProvider.hpp>
32#include <com/sun/star/linguistic2/LinguServiceManager.hpp>
36class ThesaurusMenuController :
public svt::PopupMenuControllerBase
39 explicit ThesaurusMenuController(
const css::uno::Reference< css::uno::XComponentContext >& rxContext );
42 virtual void SAL_CALL statusChanged(
const css::frame::FeatureStateEvent& rEvent )
override;
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;
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() )
66void ThesaurusMenuController::statusChanged(
const css::frame::FeatureStateEvent& rEvent )
68 rEvent.State >>= m_aLastWord;
69 m_xPopupMenu->clear();
70 if ( rEvent.IsEnabled )
74void ThesaurusMenuController::fillPopupMenu()
77 OUString aText = m_aLastWord.getToken(0,
'#', nIdx);
78 OUString aIsoLang = m_aLastWord.getToken(0,
'#', nIdx);
79 if ( aText.isEmpty() || aIsoLang.isEmpty() )
82 std::vector< OUString > aSynonyms;
84 getMeanings( aSynonyms, aText, aLocale, 7 );
86 m_xPopupMenu->enableAutoMnemonics(
false);
87 if ( aSynonyms.empty() )
91 css::uno::Reference<css::graphic::XGraphic> xGraphic;
92 OUString aThesImplName( getThesImplName( aLocale ) );
94 if (!aThesImplName.isEmpty() && !aSynonymsImageUrl.isEmpty())
98 css::uno::Reference<css::uno::XComponentContext> xContext(::comphelper::getProcessComponentContext());
99 css::uno::Reference<css::graphic::XGraphicProvider> xProvider(css::graphic::GraphicProvider::create(xContext));
102 catch (
const css::uno::Exception&)
109 for (
const auto& aSynonym : aSynonyms )
112 m_xPopupMenu->insertItem(nId, aItemText, 0, -1);
113 m_xPopupMenu->setCommand(nId,
".uno:ThesaurusFromContext?WordReplace:string=" + aItemText);
116 m_xPopupMenu->setItemImage(nId, xGraphic,
false);
121 m_xPopupMenu->insertSeparator(-1);
122 OUString aThesaurusDialogCmd(
".uno:ThesaurusDialog" );
125 m_xPopupMenu->setCommand(nId, aThesaurusDialogCmd);
128void ThesaurusMenuController::getMeanings( std::vector< OUString >& rSynonyms,
const OUString& rWord,
129 const css::lang::Locale& rLocale,
size_t nMaxSynonms )
132 if ( !(m_xThesaurus.is() && m_xThesaurus->hasLocale( rLocale ) && !rWord.isEmpty() && nMaxSynonms > 0) )
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 )
151 catch (
const css::uno::Exception& )
153 SAL_WARN(
"fwk.uielement",
"Failed to get synonyms" );
157OUString ThesaurusMenuController::getThesImplName(
const css::lang::Locale& rLocale )
const
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!" );
168OUString ThesaurusMenuController::getImplementationName()
170 return "com.sun.star.comp.framework.ThesaurusMenuController";
173css::uno::Sequence< OUString > ThesaurusMenuController::getSupportedServiceNames()
175 return {
"com.sun.star.frame.PopupMenuController" };
178extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
180 css::uno::XComponentContext* xContext,
181 css::uno::Sequence< css::uno::Any >
const & )
183 return cppu::acquire(
new ThesaurusMenuController( xContext ) );
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)