Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / linguistic / source / lngsvcmgr.cxx
blob69e884257e33572af9741a8f2ee04fee1230f2a9
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 <sal/config.h>
21 #include <sal/log.hxx>
23 #include <com/sun/star/deployment/DeploymentException.hpp>
24 #include <com/sun/star/deployment/ExtensionManager.hpp>
25 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
26 #include <com/sun/star/container/XEnumeration.hpp>
27 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
28 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
29 #include <com/sun/star/linguistic2/XSupportedLocales.hpp>
30 #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
31 #include <com/sun/star/linguistic2/LinguServiceEventFlags.hpp>
32 #include <com/sun/star/linguistic2/ProofreadingIterator.hpp>
34 #include <tools/debug.hxx>
35 #include <unotools/lingucfg.hxx>
36 #include <utility>
37 #include <vcl/svapp.hxx>
38 #include <comphelper/interfacecontainer2.hxx>
39 #include <comphelper/processfactory.hxx>
40 #include <comphelper/sequence.hxx>
41 #include <i18nlangtag/lang.h>
42 #include <i18nlangtag/languagetag.hxx>
43 #include <cppuhelper/factory.hxx>
44 #include <cppuhelper/implbase.hxx>
45 #include <cppuhelper/supportsservice.hxx>
46 #include <cppuhelper/weak.hxx>
48 #include "lngsvcmgr.hxx"
49 #include <linguistic/misc.hxx>
50 #include "spelldsp.hxx"
51 #include "hyphdsp.hxx"
52 #include "thesdsp.hxx"
53 #include "gciterator.hxx"
55 using namespace com::sun::star;
56 using namespace linguistic;
58 uno::Sequence< OUString > static GetLangSvcList( const uno::Any &rVal );
59 uno::Sequence< OUString > static GetLangSvc( const uno::Any &rVal );
61 static bool lcl_SeqHasString( const uno::Sequence< OUString > &rSeq, const OUString &rText )
63 return !rText.isEmpty()
64 && comphelper::findValue(rSeq, rText) != -1;
68 static uno::Sequence< lang::Locale > GetAvailLocales(
69 const uno::Sequence< OUString > &rSvcImplNames )
71 uno::Sequence< lang::Locale > aRes;
73 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
74 if( rSvcImplNames.hasElements() )
76 std::set< LanguageType > aLanguages;
78 // All of these services only use one arg, but need two args for compat reasons
79 uno::Sequence< uno::Any > aArgs(2);
80 aArgs.getArray()[0] <<= GetLinguProperties();
82 // check all services for the supported languages and new
83 // languages to the result
85 for (const OUString& rImplName : rSvcImplNames)
87 uno::Reference< linguistic2::XSupportedLocales > xSuppLoc;
88 try
90 xSuppLoc.set( xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
91 rImplName, aArgs, xContext ),
92 uno::UNO_QUERY );
94 catch (uno::Exception &)
96 SAL_WARN( "linguistic", "createInstanceWithArguments failed" );
99 if (xSuppLoc.is())
101 const uno::Sequence< lang::Locale > aLoc( xSuppLoc->getLocales() );
102 for (const lang::Locale& rLoc : aLoc)
104 LanguageType nLang = LinguLocaleToLanguage( rLoc );
106 // It's a set, so insertion fails if language was already added.
107 aLanguages.insert( nLang );
110 else
112 SAL_WARN( "linguistic", "interface not supported by service" );
116 // build return sequence
117 std::vector<lang::Locale> aVec;
118 aVec.reserve(aLanguages.size());
120 std::transform(aLanguages.begin(), aLanguages.end(), std::back_inserter(aVec),
121 [](const LanguageType& rLang) -> lang::Locale { return LanguageTag::convertToLocale(rLang); });
123 aRes = comphelper::containerToSequence(aVec);
126 return aRes;
130 struct SvcInfo
132 const OUString aSvcImplName;
133 const std::vector< LanguageType > aSuppLanguages;
135 SvcInfo( OUString aSvcImplName_,
136 std::vector< LanguageType >&& rSuppLanguages ) :
137 aSvcImplName (std::move(aSvcImplName_)),
138 aSuppLanguages (std::move(rSuppLanguages))
142 bool HasLanguage( LanguageType nLanguage ) const;
146 bool SvcInfo::HasLanguage( LanguageType nLanguage ) const
148 for ( auto const & i : aSuppLanguages)
150 if (nLanguage == i)
151 return true;
153 return false;
156 class LngSvcMgrListenerHelper :
157 public cppu::WeakImplHelper
159 linguistic2::XLinguServiceEventListener,
160 linguistic2::XDictionaryListEventListener
163 LngSvcMgr &rMyManager;
165 ::comphelper::OInterfaceContainerHelper2 aLngSvcMgrListeners;
166 ::comphelper::OInterfaceContainerHelper2 aLngSvcEvtBroadcasters;
167 uno::Reference< linguistic2::XSearchableDictionaryList > xDicList;
169 sal_Int16 nCombinedLngSvcEvt;
171 void LaunchEvent( sal_Int16 nLngSvcEvtFlags );
173 void Timeout();
175 public:
176 LngSvcMgrListenerHelper( LngSvcMgr &rLngSvcMgr,
177 uno::Reference< linguistic2::XSearchableDictionaryList > xDicList );
179 LngSvcMgrListenerHelper(const LngSvcMgrListenerHelper&) = delete;
180 LngSvcMgrListenerHelper& operator=(const LngSvcMgrListenerHelper&) = delete;
182 // lang::XEventListener
183 virtual void SAL_CALL
184 disposing( const lang::EventObject& rSource ) override;
186 // linguistic2::XLinguServiceEventListener
187 virtual void SAL_CALL
188 processLinguServiceEvent( const linguistic2::LinguServiceEvent& aLngSvcEvent ) override;
190 // linguistic2::XDictionaryListEventListener
191 virtual void SAL_CALL
192 processDictionaryListEvent(
193 const linguistic2::DictionaryListEvent& rDicListEvent ) override;
195 inline void AddLngSvcMgrListener(
196 const uno::Reference< lang::XEventListener >& rxListener );
197 inline void RemoveLngSvcMgrListener(
198 const uno::Reference< lang::XEventListener >& rxListener );
199 void DisposeAndClear( const lang::EventObject &rEvtObj );
200 void AddLngSvcEvtBroadcaster(
201 const uno::Reference< linguistic2::XLinguServiceEventBroadcaster > &rxBroadcaster );
202 void RemoveLngSvcEvtBroadcaster(
203 const uno::Reference< linguistic2::XLinguServiceEventBroadcaster > &rxBroadcaster );
205 void AddLngSvcEvt( sal_Int16 nLngSvcEvt );
209 LngSvcMgrListenerHelper::LngSvcMgrListenerHelper(
210 LngSvcMgr &rLngSvcMgr,
211 uno::Reference< linguistic2::XSearchableDictionaryList > xDicList_ ) :
212 rMyManager ( rLngSvcMgr ),
213 aLngSvcMgrListeners ( GetLinguMutex() ),
214 aLngSvcEvtBroadcasters ( GetLinguMutex() ),
215 xDicList (std::move( xDicList_ ))
217 if (xDicList.is())
219 xDicList->addDictionaryListEventListener(
220 static_cast<linguistic2::XDictionaryListEventListener *>(this), false );
223 nCombinedLngSvcEvt = 0;
227 void SAL_CALL LngSvcMgrListenerHelper::disposing( const lang::EventObject& rSource )
229 osl::MutexGuard aGuard( GetLinguMutex() );
231 uno::Reference< uno::XInterface > xRef( rSource.Source );
232 if ( xRef.is() )
234 aLngSvcMgrListeners .removeInterface( xRef );
235 aLngSvcEvtBroadcasters.removeInterface( xRef );
236 if (xDicList == xRef)
237 xDicList = nullptr;
241 void LngSvcMgrListenerHelper::Timeout()
243 osl::MutexGuard aGuard( GetLinguMutex() );
246 // change event source to LinguServiceManager since the listeners
247 // probably do not know (and need not to know) about the specific
248 // SpellChecker's or Hyphenator's.
249 linguistic2::LinguServiceEvent aEvtObj(
250 static_cast<css::linguistic2::XLinguServiceManager*>(&rMyManager), nCombinedLngSvcEvt );
251 nCombinedLngSvcEvt = 0;
253 if (rMyManager.mxSpellDsp.is())
254 rMyManager.mxSpellDsp->FlushSpellCache();
256 // pass event on to linguistic2::XLinguServiceEventListener's
257 aLngSvcMgrListeners.notifyEach( &linguistic2::XLinguServiceEventListener::processLinguServiceEvent, aEvtObj );
262 void LngSvcMgrListenerHelper::AddLngSvcEvt( sal_Int16 nLngSvcEvt )
264 nCombinedLngSvcEvt |= nLngSvcEvt;
265 Timeout();
269 void SAL_CALL
270 LngSvcMgrListenerHelper::processLinguServiceEvent(
271 const linguistic2::LinguServiceEvent& rLngSvcEvent )
273 osl::MutexGuard aGuard( GetLinguMutex() );
274 AddLngSvcEvt( rLngSvcEvent.nEvent );
278 void SAL_CALL
279 LngSvcMgrListenerHelper::processDictionaryListEvent(
280 const linguistic2::DictionaryListEvent& rDicListEvent )
282 osl::MutexGuard aGuard( GetLinguMutex() );
284 sal_Int16 nDlEvt = rDicListEvent.nCondensedEvent;
285 if (0 == nDlEvt)
286 return;
288 // we do keep the original event source here though...
290 // pass event on to linguistic2::XDictionaryListEventListener's
291 aLngSvcMgrListeners.notifyEach( &linguistic2::XDictionaryListEventListener::processDictionaryListEvent, rDicListEvent );
293 // "translate" DictionaryList event into linguistic2::LinguServiceEvent
294 sal_Int16 nLngSvcEvt = 0;
295 sal_Int16 const nSpellCorrectFlags =
296 linguistic2::DictionaryListEventFlags::ADD_NEG_ENTRY |
297 linguistic2::DictionaryListEventFlags::DEL_POS_ENTRY |
298 linguistic2::DictionaryListEventFlags::ACTIVATE_NEG_DIC |
299 linguistic2::DictionaryListEventFlags::DEACTIVATE_POS_DIC;
300 if (0 != (nDlEvt & nSpellCorrectFlags))
301 nLngSvcEvt |= linguistic2::LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN;
303 sal_Int16 const nSpellWrongFlags =
304 linguistic2::DictionaryListEventFlags::ADD_POS_ENTRY |
305 linguistic2::DictionaryListEventFlags::DEL_NEG_ENTRY |
306 linguistic2::DictionaryListEventFlags::ACTIVATE_POS_DIC |
307 linguistic2::DictionaryListEventFlags::DEACTIVATE_NEG_DIC;
308 if (0 != (nDlEvt & nSpellWrongFlags))
309 nLngSvcEvt |= linguistic2::LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN;
311 sal_Int16 const nHyphenateFlags =
312 linguistic2::DictionaryListEventFlags::ADD_POS_ENTRY |
313 linguistic2::DictionaryListEventFlags::DEL_POS_ENTRY |
314 linguistic2::DictionaryListEventFlags::ACTIVATE_POS_DIC |
315 linguistic2::DictionaryListEventFlags::ACTIVATE_NEG_DIC;
316 if (0 != (nDlEvt & nHyphenateFlags))
317 nLngSvcEvt |= linguistic2::LinguServiceEventFlags::HYPHENATE_AGAIN;
319 if (rMyManager.mxSpellDsp.is())
320 rMyManager.mxSpellDsp->FlushSpellCache();
321 if (nLngSvcEvt)
322 LaunchEvent( nLngSvcEvt );
326 void LngSvcMgrListenerHelper::LaunchEvent( sal_Int16 nLngSvcEvtFlags )
328 linguistic2::LinguServiceEvent aEvt(
329 static_cast<css::linguistic2::XLinguServiceManager*>(&rMyManager), nLngSvcEvtFlags );
331 // pass event on to linguistic2::XLinguServiceEventListener's
332 aLngSvcMgrListeners.notifyEach( &linguistic2::XLinguServiceEventListener::processLinguServiceEvent, aEvt );
336 inline void LngSvcMgrListenerHelper::AddLngSvcMgrListener(
337 const uno::Reference< lang::XEventListener >& rxListener )
339 aLngSvcMgrListeners.addInterface( rxListener );
343 inline void LngSvcMgrListenerHelper::RemoveLngSvcMgrListener(
344 const uno::Reference< lang::XEventListener >& rxListener )
346 aLngSvcMgrListeners.removeInterface( rxListener );
350 void LngSvcMgrListenerHelper::DisposeAndClear( const lang::EventObject &rEvtObj )
352 // call "disposing" for all listeners and clear list
353 aLngSvcMgrListeners .disposeAndClear( rEvtObj );
355 // remove references to this object hold by the broadcasters
356 comphelper::OInterfaceIteratorHelper2 aIt( aLngSvcEvtBroadcasters );
357 while (aIt.hasMoreElements())
359 uno::Reference< linguistic2::XLinguServiceEventBroadcaster > xRef( aIt.next(), uno::UNO_QUERY );
360 if (xRef.is())
361 RemoveLngSvcEvtBroadcaster( xRef );
364 // remove reference to this object hold by the dictionary-list
365 if (xDicList.is())
367 xDicList->removeDictionaryListEventListener(
368 static_cast<linguistic2::XDictionaryListEventListener *>(this) );
369 xDicList = nullptr;
374 void LngSvcMgrListenerHelper::AddLngSvcEvtBroadcaster(
375 const uno::Reference< linguistic2::XLinguServiceEventBroadcaster > &rxBroadcaster )
377 if (rxBroadcaster.is())
379 aLngSvcEvtBroadcasters.addInterface( rxBroadcaster );
380 rxBroadcaster->addLinguServiceEventListener(
381 static_cast<linguistic2::XLinguServiceEventListener *>(this) );
386 void LngSvcMgrListenerHelper::RemoveLngSvcEvtBroadcaster(
387 const uno::Reference< linguistic2::XLinguServiceEventBroadcaster > &rxBroadcaster )
389 if (rxBroadcaster.is())
391 aLngSvcEvtBroadcasters.removeInterface( rxBroadcaster );
392 rxBroadcaster->removeLinguServiceEventListener(
393 static_cast<linguistic2::XLinguServiceEventListener *>(this) );
398 LngSvcMgr::LngSvcMgr()
399 : utl::ConfigItem("Office.Linguistic")
400 , aEvtListeners(GetLinguMutex())
401 , aUpdateIdle("LngSvcMgr aUpdateIdle")
403 bDisposing = false;
405 // request notify events when properties (i.e. something in the subtree) changes
406 uno::Sequence< OUString > aNames{
407 "ServiceManager/SpellCheckerList",
408 "ServiceManager/GrammarCheckerList",
409 "ServiceManager/HyphenatorList",
410 "ServiceManager/ThesaurusList"
412 EnableNotification( aNames );
414 UpdateAll();
416 aUpdateIdle.SetPriority(TaskPriority::LOWEST);
417 aUpdateIdle.SetInvokeHandler(LINK(this, LngSvcMgr, updateAndBroadcast));
419 // request to be notified if an extension has been added/removed
420 uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
422 uno::Reference<deployment::XExtensionManager> xExtensionManager;
423 try {
424 xExtensionManager = deployment::ExtensionManager::get(xContext);
425 } catch ( const uno::DeploymentException & ) {
426 SAL_WARN( "linguistic", "no extension manager - should fire on mobile only" );
427 } catch ( const deployment::DeploymentException & ) {
428 SAL_WARN( "linguistic", "no extension manager - should fire on mobile only" );
430 if (xExtensionManager.is())
432 xMB.set(xExtensionManager, uno::UNO_QUERY_THROW);
434 uno::Reference<util::XModifyListener> xListener(this);
435 xMB->addModifyListener( xListener );
439 // css::util::XModifyListener
440 void LngSvcMgr::modified(const lang::EventObject&)
442 osl::MutexGuard aGuard(GetLinguMutex());
443 //assume that if an extension has been added/removed that
444 //it might be a dictionary extension, so drop our cache
446 pAvailSpellSvcs.reset();
447 pAvailGrammarSvcs.reset();
448 pAvailHyphSvcs.reset();
449 pAvailThesSvcs.reset();
451 //schedule in an update to execute in the main thread
452 aUpdateIdle.Start();
455 //run update, and inform everyone that dictionaries (may) have changed, this
456 //needs to be run in the main thread because
457 //utl::ConfigChangeListener_Impl::changesOccurred grabs the SolarMutex and we
458 //get notified that an extension was added from an extension manager thread
459 IMPL_LINK_NOARG(LngSvcMgr, updateAndBroadcast, Timer *, void)
461 osl::MutexGuard aGuard( GetLinguMutex() );
463 UpdateAll();
465 if (mxListenerHelper.is())
467 mxListenerHelper->AddLngSvcEvt(
468 linguistic2::LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN |
469 linguistic2::LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN |
470 linguistic2::LinguServiceEventFlags::PROOFREAD_AGAIN |
471 linguistic2::LinguServiceEventFlags::HYPHENATE_AGAIN );
475 void LngSvcMgr::stopListening()
477 osl::MutexGuard aGuard(GetLinguMutex());
479 if (!xMB.is())
480 return;
484 uno::Reference<util::XModifyListener> xListener(this);
485 xMB->removeModifyListener(xListener);
487 catch (const uno::Exception&)
491 xMB.clear();
494 void LngSvcMgr::disposing(const lang::EventObject&)
496 stopListening();
499 LngSvcMgr::~LngSvcMgr()
501 stopListening();
503 // memory for pSpellDsp, pHyphDsp, pThesDsp, pListenerHelper
504 // will be freed in the destructor of the respective Reference's
505 // xSpellDsp, xGrammarDsp, xHyphDsp, xThesDsp
507 pAvailSpellSvcs.reset();
508 pAvailGrammarSvcs.reset();
509 pAvailHyphSvcs.reset();
510 pAvailThesSvcs.reset();
513 namespace
515 using lang::Locale;
516 using uno::Any;
517 using uno::Sequence;
519 bool lcl_FindEntry( const OUString &rEntry, const Sequence< OUString > &rCfgSvcs )
521 return comphelper::findValue(rCfgSvcs, rEntry) != -1;
524 bool lcl_FindEntry( const OUString &rEntry, const std::vector< OUString > &rCfgSvcs )
526 return std::find(rCfgSvcs.begin(), rCfgSvcs.end(), rEntry) != rCfgSvcs.end();
529 Sequence< OUString > lcl_GetLastFoundSvcs(
530 SvtLinguConfig const &rCfg,
531 const OUString &rLastFoundList ,
532 const OUString& rCfgLocaleStr )
534 Sequence< OUString > aRes;
536 Sequence< OUString > aNodeNames( rCfg.GetNodeNames(rLastFoundList) );
537 bool bFound = lcl_FindEntry( rCfgLocaleStr, aNodeNames);
539 if (bFound)
541 Sequence< OUString > aNames { rLastFoundList + "/" + rCfgLocaleStr };
542 Sequence< Any > aValues( rCfg.GetProperties( aNames ) );
543 if (aValues.hasElements())
545 SAL_WARN_IF( aValues.getLength() != 1, "linguistic", "unexpected length of sequence" );
546 Sequence< OUString > aSvcImplNames;
547 if (aValues.getConstArray()[0] >>= aSvcImplNames)
548 aRes = aSvcImplNames;
549 else
551 SAL_WARN( "linguistic", "type mismatch" );
556 return aRes;
559 Sequence< OUString > lcl_RemoveMissingEntries(
560 const Sequence< OUString > &rCfgSvcs,
561 const Sequence< OUString > &rAvailSvcs )
563 std::vector<OUString> aRes;
564 aRes.reserve(rCfgSvcs.getLength());
566 std::copy_if(rCfgSvcs.begin(), rCfgSvcs.end(), std::back_inserter(aRes),
567 [&rAvailSvcs](const OUString& entry) { return lcl_SeqHasString(rAvailSvcs, entry); });
569 return comphelper::containerToSequence(aRes);
572 Sequence< OUString > lcl_GetNewEntries(
573 const Sequence< OUString > &rLastFoundSvcs,
574 const Sequence< OUString > &rAvailSvcs )
576 std::vector<OUString> aRes;
577 aRes.reserve(rAvailSvcs.getLength());
579 std::copy_if(rAvailSvcs.begin(), rAvailSvcs.end(), std::back_inserter(aRes),
580 [&rLastFoundSvcs](const OUString& rEntry) {
581 return !rEntry.isEmpty() && !lcl_FindEntry( rEntry, rLastFoundSvcs ); });
583 return comphelper::containerToSequence(aRes);
586 Sequence< OUString > lcl_MergeSeq(
587 const Sequence< OUString > &rCfgSvcs,
588 const Sequence< OUString > &rNewSvcs )
590 std::vector<OUString> aRes;
591 aRes.reserve(rCfgSvcs.getLength() + rNewSvcs.getLength());
593 auto lVecNotHasString = [&aRes](const OUString& rEntry)
594 { return !rEntry.isEmpty() && !lcl_FindEntry(rEntry, aRes); };
596 // add previously configured service first and append
597 // new found services at the end
598 for (const Sequence< OUString > &rSeq : { rCfgSvcs, rNewSvcs })
600 std::copy_if(rSeq.begin(), rSeq.end(), std::back_inserter(aRes), lVecNotHasString);
603 return comphelper::containerToSequence(aRes);
607 void LngSvcMgr::UpdateAll()
609 using beans::PropertyValue;
610 using lang::Locale;
611 using uno::Sequence;
613 typedef std::map< OUString, Sequence< OUString > > list_entry_map_t;
615 SvtLinguConfig aCfg;
617 const int nNumServices = 4;
618 static constexpr rtl::OUStringConstExpr apServices[nNumServices] = { SN_SPELLCHECKER, SN_GRAMMARCHECKER, SN_HYPHENATOR, SN_THESAURUS };
619 const char * const apCurLists[nNumServices] = { "ServiceManager/SpellCheckerList", "ServiceManager/GrammarCheckerList", "ServiceManager/HyphenatorList", "ServiceManager/ThesaurusList" };
620 const char * const apLastFoundLists[nNumServices] = { "ServiceManager/LastFoundSpellCheckers", "ServiceManager/LastFoundGrammarCheckers", "ServiceManager/LastFoundHyphenators", "ServiceManager/LastFoundThesauri" };
622 // usage of indices as above: 0 = spell checker, 1 = grammar checker, 2 = hyphenator, 3 = thesaurus
623 std::vector< list_entry_map_t > aLastFoundSvcs(nNumServices);
624 std::vector< list_entry_map_t > aCurSvcs(nNumServices);
626 for (int k = 0; k < nNumServices; ++k)
628 OUString aService( apServices[k] );
629 OUString aActiveList( OUString::createFromAscii( apCurLists[k] ) );
630 OUString aLastFoundList( OUString::createFromAscii( apLastFoundLists[k] ) );
633 // remove configured but not available language/services entries
635 const Sequence< OUString > aNodeNames( aCfg.GetNodeNames( aActiveList ) ); // list of configured locales
636 for (const OUString& rNodeName : aNodeNames)
638 Locale aLocale( LanguageTag::convertToLocale( rNodeName));
639 Sequence< OUString > aCfgSvcs( getConfiguredServices( aService, aLocale ));
640 Sequence< OUString > aAvailSvcs( getAvailableServices( aService, aLocale ));
642 aCfgSvcs = lcl_RemoveMissingEntries( aCfgSvcs, aAvailSvcs );
644 aCurSvcs[k][ rNodeName ] = aCfgSvcs;
648 // add new available language/service entries
649 // and
650 // set last found services to currently available ones
652 const Sequence< Locale > aAvailLocales( getAvailableLocales(aService) );
653 for (const Locale& rAvailLocale : aAvailLocales)
655 OUString aCfgLocaleStr( LanguageTag::convertToBcp47( rAvailLocale));
657 Sequence< OUString > aAvailSvcs( getAvailableServices( aService, rAvailLocale ));
659 aLastFoundSvcs[k][ aCfgLocaleStr ] = aAvailSvcs;
661 Sequence< OUString > aLastSvcs(
662 lcl_GetLastFoundSvcs( aCfg, aLastFoundList , aCfgLocaleStr ));
663 Sequence< OUString > aNewSvcs =
664 lcl_GetNewEntries( aLastSvcs, aAvailSvcs );
666 Sequence< OUString > aCfgSvcs( aCurSvcs[k][ aCfgLocaleStr ] );
668 // merge services list (previously configured to be listed first).
669 aCfgSvcs = lcl_MergeSeq( aCfgSvcs, aNewSvcs );
671 aCurSvcs[k][ aCfgLocaleStr ] = aCfgSvcs;
676 // write new data back to configuration
678 for (int k = 0; k < nNumServices; ++k)
680 for (int i = 0; i < 2; ++i)
682 const char *pSubNodeName = (i == 0) ? apCurLists[k] : apLastFoundLists[k];
683 OUString aSubNodeName( OUString::createFromAscii(pSubNodeName) );
685 list_entry_map_t &rCurMap = (i == 0) ? aCurSvcs[k] : aLastFoundSvcs[k];
686 sal_Int32 nVals = static_cast< sal_Int32 >( rCurMap.size() );
687 Sequence< PropertyValue > aNewValues( nVals );
688 PropertyValue *pNewValue = aNewValues.getArray();
689 for (auto const& elem : rCurMap)
691 pNewValue->Name = aSubNodeName + "/" + elem.first;
692 pNewValue->Value <<= elem.second;
693 ++pNewValue;
695 OSL_ENSURE( pNewValue - aNewValues.getConstArray() == nVals,
696 "possible mismatch of sequence size and property number" );
699 // add new or replace existing entries.
700 bool bRes = aCfg.ReplaceSetProperties( aSubNodeName, aNewValues );
701 SAL_WARN_IF(!bRes, "linguistic", "failed to set new configuration values");
706 //The new settings in the configuration get applied ! because we are
707 //listening to the configuration for changes of the relevant ! properties
708 //and Notify applies the new settings.
711 void LngSvcMgr::Notify( const uno::Sequence< OUString > &rPropertyNames )
713 static const OUStringLiteral aSpellCheckerList( u"ServiceManager/SpellCheckerList" );
714 static const OUStringLiteral aGrammarCheckerList( u"ServiceManager/GrammarCheckerList" );
715 static const OUStringLiteral aHyphenatorList( u"ServiceManager/HyphenatorList" );
716 static const OUStringLiteral aThesaurusList( u"ServiceManager/ThesaurusList" );
718 const uno::Sequence< OUString > aSpellCheckerListEntries( GetNodeNames( aSpellCheckerList ) );
719 const uno::Sequence< OUString > aGrammarCheckerListEntries( GetNodeNames( aGrammarCheckerList ) );
720 const uno::Sequence< OUString > aHyphenatorListEntries( GetNodeNames( aHyphenatorList ) );
721 const uno::Sequence< OUString > aThesaurusListEntries( GetNodeNames( aThesaurusList ) );
723 uno::Sequence< uno::Any > aValues;
724 uno::Sequence< OUString > aNames( 1 );
725 OUString *pNames = aNames.getArray();
727 for (const OUString& rName : rPropertyNames)
729 // property names look like
730 // "ServiceManager/ThesaurusList/de-CH"
732 sal_Int32 nKeyStart;
733 nKeyStart = rName.lastIndexOf( '/' );
734 OUString aKeyText;
735 if (nKeyStart != -1)
736 aKeyText = rName.copy( nKeyStart + 1 );
737 SAL_WARN_IF( aKeyText.isEmpty(), "linguistic", "unexpected key (lang::Locale) string" );
738 if (rName.startsWith( aSpellCheckerList ))
740 osl::MutexGuard aGuard(GetLinguMutex());
742 // delete old cached data, needs to be acquired new on demand
743 pAvailSpellSvcs.reset();
745 if (lcl_SeqHasString( aSpellCheckerListEntries, aKeyText ))
747 pNames[0] = aSpellCheckerList + "/" + aKeyText;
748 aValues = /*aCfg.*/GetProperties( aNames );
749 uno::Sequence< OUString > aSvcImplNames;
750 if (aValues.hasElements())
751 aSvcImplNames = GetLangSvcList( aValues.getConstArray()[0] );
753 LanguageType nLang = LANGUAGE_NONE;
754 if (!aKeyText.isEmpty())
755 nLang = LanguageTag::convertToLanguageType( aKeyText );
757 GetSpellCheckerDsp_Impl( false ); // don't set service list, it will be done below
758 mxSpellDsp->SetServiceList( LanguageTag::convertToLocale(nLang), aSvcImplNames );
761 else if (rName.startsWith( aGrammarCheckerList ))
763 osl::MutexGuard aGuard(GetLinguMutex());
765 // delete old cached data, needs to be acquired new on demand
766 pAvailGrammarSvcs.reset();
768 if (lcl_SeqHasString( aGrammarCheckerListEntries, aKeyText ))
770 pNames[0] = aGrammarCheckerList + "/" + aKeyText;
771 aValues = /*aCfg.*/GetProperties( aNames );
772 uno::Sequence< OUString > aSvcImplNames;
773 if (aValues.hasElements())
774 aSvcImplNames = GetLangSvc( aValues.getConstArray()[0] );
776 LanguageType nLang = LANGUAGE_NONE;
777 if (!aKeyText.isEmpty())
778 nLang = LanguageTag::convertToLanguageType( aKeyText );
780 if (SvtLinguConfig().HasGrammarChecker())
782 GetGrammarCheckerDsp_Impl( false ); // don't set service list, it will be done below
783 mxGrammarDsp->SetServiceList( LanguageTag::convertToLocale(nLang), aSvcImplNames );
787 else if (rName.startsWith( aHyphenatorList ))
789 osl::MutexGuard aGuard(GetLinguMutex());
791 // delete old cached data, needs to be acquired new on demand
792 pAvailHyphSvcs.reset();
794 if (lcl_SeqHasString( aHyphenatorListEntries, aKeyText ))
796 pNames[0] = aHyphenatorList + "/" + aKeyText;
797 aValues = /*aCfg.*/GetProperties( aNames );
798 uno::Sequence< OUString > aSvcImplNames;
799 if (aValues.hasElements())
800 aSvcImplNames = GetLangSvc( aValues.getConstArray()[0] );
802 LanguageType nLang = LANGUAGE_NONE;
803 if (!aKeyText.isEmpty())
804 nLang = LanguageTag::convertToLanguageType( aKeyText );
806 GetHyphenatorDsp_Impl( false ); // don't set service list, it will be done below
807 mxHyphDsp->SetServiceList( LanguageTag::convertToLocale(nLang), aSvcImplNames );
810 else if (rName.startsWith( aThesaurusList ))
812 osl::MutexGuard aGuard(GetLinguMutex());
814 // delete old cached data, needs to be acquired new on demand
815 pAvailThesSvcs.reset();
817 if (lcl_SeqHasString( aThesaurusListEntries, aKeyText ))
819 pNames[0] = aThesaurusList + "/" + aKeyText;
820 aValues = /*aCfg.*/GetProperties( aNames );
821 uno::Sequence< OUString > aSvcImplNames;
822 if (aValues.hasElements())
823 aSvcImplNames = GetLangSvcList( aValues.getConstArray()[0] );
825 LanguageType nLang = LANGUAGE_NONE;
826 if (!aKeyText.isEmpty())
827 nLang = LanguageTag::convertToLanguageType( aKeyText );
829 GetThesaurusDsp_Impl( false ); // don't set service list, it will be done below
830 mxThesDsp->SetServiceList( LanguageTag::convertToLocale(nLang), aSvcImplNames );
833 else
835 SAL_WARN( "linguistic", "notified for unexpected property" );
841 void LngSvcMgr::ImplCommit()
843 // everything necessary should have already been done by 'SaveCfgSvcs'
844 // called from within 'setConfiguredServices'.
845 // Also this class usually exits only when the Office is being shutdown.
849 void LngSvcMgr::GetListenerHelper_Impl()
851 if (!mxListenerHelper.is())
853 mxListenerHelper = new LngSvcMgrListenerHelper( *this, linguistic::GetDictionaryList() );
858 void LngSvcMgr::GetSpellCheckerDsp_Impl( bool bSetSvcList )
860 if (!mxSpellDsp.is())
862 mxSpellDsp = new SpellCheckerDispatcher( *this );
863 if (bSetSvcList)
864 SetCfgServiceLists( *mxSpellDsp );
869 void LngSvcMgr::GetGrammarCheckerDsp_Impl( bool bSetSvcList )
871 if (mxGrammarDsp.is() || !SvtLinguConfig().HasGrammarChecker())
872 return;
874 //! since the grammar checking iterator needs to be a one instance service
875 //! we need to create it the correct way!
876 uno::Reference< linguistic2::XProofreadingIterator > xGCI;
879 xGCI = linguistic2::ProofreadingIterator::create( comphelper::getProcessComponentContext() );
881 catch (const uno::Exception &)
884 SAL_WARN_IF( !xGCI.is(), "linguistic", "instantiating grammar checking iterator failed" );
886 if (xGCI.is())
888 mxGrammarDsp = dynamic_cast< GrammarCheckingIterator * >(xGCI.get());
889 SAL_WARN_IF( mxGrammarDsp == nullptr, "linguistic", "failed to get implementation" );
890 if (bSetSvcList && mxGrammarDsp.is())
891 SetCfgServiceLists( *mxGrammarDsp );
896 void LngSvcMgr::GetHyphenatorDsp_Impl( bool bSetSvcList )
898 if (!mxHyphDsp.is())
900 mxHyphDsp = new HyphenatorDispatcher( *this );
901 if (bSetSvcList)
902 SetCfgServiceLists( *mxHyphDsp );
907 void LngSvcMgr::GetThesaurusDsp_Impl( bool bSetSvcList )
909 if (!mxThesDsp.is())
911 mxThesDsp = new ThesaurusDispatcher;
912 if (bSetSvcList)
913 SetCfgServiceLists( *mxThesDsp );
918 void LngSvcMgr::GetAvailableSpellSvcs_Impl()
920 if (pAvailSpellSvcs)
921 return;
923 pAvailSpellSvcs.emplace();
925 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
927 uno::Reference< container::XContentEnumerationAccess > xEnumAccess( xContext->getServiceManager(), uno::UNO_QUERY );
928 uno::Reference< container::XEnumeration > xEnum;
929 if (xEnumAccess.is())
930 xEnum = xEnumAccess->createContentEnumeration( SN_SPELLCHECKER );
932 if (!xEnum.is())
933 return;
935 while (xEnum->hasMoreElements())
937 uno::Any aCurrent = xEnum->nextElement();
938 uno::Reference< lang::XSingleComponentFactory > xCompFactory;
939 uno::Reference< lang::XSingleServiceFactory > xFactory;
941 xCompFactory.set(aCurrent, css::uno::UNO_QUERY);
942 if (!xCompFactory.is())
944 xFactory.set(aCurrent, css::uno::UNO_QUERY);
946 if ( xCompFactory.is() || xFactory.is() )
950 uno::Reference< linguistic2::XSpellChecker > xSvc( ( xCompFactory.is() ? xCompFactory->createInstanceWithContext( xContext ) : xFactory->createInstance() ), uno::UNO_QUERY_THROW );
952 OUString aImplName;
953 std::vector< LanguageType > aLanguages;
954 uno::Reference< XServiceInfo > xInfo( xSvc, uno::UNO_QUERY );
955 if (xInfo.is())
956 aImplName = xInfo->getImplementationName();
957 SAL_WARN_IF( aImplName.isEmpty(), "linguistic", "empty implementation name" );
958 uno::Sequence<lang::Locale> aLocaleSequence(xSvc->getLocales());
959 aLanguages = LocaleSeqToLangVec( aLocaleSequence );
961 pAvailSpellSvcs->push_back( SvcInfo( aImplName, std::move(aLanguages) ) );
963 catch (const uno::Exception &)
965 SAL_WARN( "linguistic", "createInstance failed" );
972 void LngSvcMgr::GetAvailableGrammarSvcs_Impl()
974 if (pAvailGrammarSvcs)
975 return;
977 pAvailGrammarSvcs.emplace();
979 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
981 uno::Reference< container::XContentEnumerationAccess > xEnumAccess( xContext->getServiceManager(), uno::UNO_QUERY );
982 uno::Reference< container::XEnumeration > xEnum;
983 if (xEnumAccess.is())
984 xEnum = xEnumAccess->createContentEnumeration( SN_GRAMMARCHECKER );
986 if (!xEnum.is())
987 return;
989 while (xEnum->hasMoreElements())
991 uno::Any aCurrent = xEnum->nextElement();
992 uno::Reference< lang::XSingleComponentFactory > xCompFactory;
993 uno::Reference< lang::XSingleServiceFactory > xFactory;
995 xCompFactory.set(aCurrent, css::uno::UNO_QUERY);
996 if (!xCompFactory.is())
998 xFactory.set(aCurrent, css::uno::UNO_QUERY);
1000 if ( xCompFactory.is() || xFactory.is() )
1004 uno::Reference< linguistic2::XProofreader > xSvc(
1005 xCompFactory.is()
1006 ? xCompFactory->createInstanceWithContext(xContext)
1007 : xFactory->createInstance(),
1008 uno::UNO_QUERY_THROW);
1010 OUString aImplName;
1011 std::vector< LanguageType > aLanguages;
1012 uno::Reference< XServiceInfo > xInfo( xSvc, uno::UNO_QUERY );
1013 if (xInfo.is())
1014 aImplName = xInfo->getImplementationName();
1015 SAL_WARN_IF( aImplName.isEmpty(), "linguistic", "empty implementation name" );
1016 uno::Sequence<lang::Locale> aLocaleSequence(xSvc->getLocales());
1017 aLanguages = LocaleSeqToLangVec( aLocaleSequence );
1019 pAvailGrammarSvcs->push_back( SvcInfo( aImplName, std::move(aLanguages) ) );
1021 catch (const uno::Exception &)
1023 SAL_WARN( "linguistic", "createInstance failed" );
1031 void LngSvcMgr::GetAvailableHyphSvcs_Impl()
1033 if (pAvailHyphSvcs)
1034 return;
1036 pAvailHyphSvcs.emplace();
1037 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
1039 uno::Reference< container::XContentEnumerationAccess > xEnumAccess( xContext->getServiceManager(), uno::UNO_QUERY );
1040 uno::Reference< container::XEnumeration > xEnum;
1041 if (xEnumAccess.is())
1042 xEnum = xEnumAccess->createContentEnumeration( SN_HYPHENATOR );
1044 if (!xEnum.is())
1045 return;
1047 while (xEnum->hasMoreElements())
1049 uno::Any aCurrent = xEnum->nextElement();
1050 uno::Reference< lang::XSingleComponentFactory > xCompFactory;
1051 uno::Reference< lang::XSingleServiceFactory > xFactory;
1053 xCompFactory.set(aCurrent, css::uno::UNO_QUERY);
1054 if (!xCompFactory.is())
1056 xFactory.set(aCurrent, css::uno::UNO_QUERY);
1058 if ( xCompFactory.is() || xFactory.is() )
1062 uno::Reference< linguistic2::XHyphenator > xSvc( ( xCompFactory.is() ? xCompFactory->createInstanceWithContext( xContext ) : xFactory->createInstance() ), uno::UNO_QUERY_THROW );
1063 OUString aImplName;
1064 std::vector< LanguageType > aLanguages;
1065 uno::Reference< XServiceInfo > xInfo( xSvc, uno::UNO_QUERY );
1066 if (xInfo.is())
1067 aImplName = xInfo->getImplementationName();
1068 SAL_WARN_IF( aImplName.isEmpty(), "linguistic", "empty implementation name" );
1069 uno::Sequence<lang::Locale> aLocaleSequence(xSvc->getLocales());
1070 aLanguages = LocaleSeqToLangVec( aLocaleSequence );
1071 pAvailHyphSvcs->push_back( SvcInfo( aImplName, std::move(aLanguages) ) );
1073 catch (const uno::Exception &)
1075 SAL_WARN( "linguistic", "createInstance failed" );
1082 void LngSvcMgr::GetAvailableThesSvcs_Impl()
1084 if (pAvailThesSvcs)
1085 return;
1087 pAvailThesSvcs.emplace();
1089 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
1091 uno::Reference< container::XContentEnumerationAccess > xEnumAccess( xContext->getServiceManager(), uno::UNO_QUERY );
1092 uno::Reference< container::XEnumeration > xEnum;
1093 if (xEnumAccess.is())
1094 xEnum = xEnumAccess->createContentEnumeration( SN_THESAURUS );
1096 if (!xEnum.is())
1097 return;
1099 while (xEnum->hasMoreElements())
1101 uno::Any aCurrent = xEnum->nextElement();
1102 uno::Reference< lang::XSingleComponentFactory > xCompFactory;
1103 uno::Reference< lang::XSingleServiceFactory > xFactory;
1105 xCompFactory.set(aCurrent, css::uno::UNO_QUERY);
1106 if (!xCompFactory.is())
1108 xFactory.set(aCurrent, css::uno::UNO_QUERY);
1110 if ( xCompFactory.is() || xFactory.is() )
1114 uno::Reference< linguistic2::XThesaurus > xSvc( ( xCompFactory.is() ? xCompFactory->createInstanceWithContext( xContext ) : xFactory->createInstance() ), uno::UNO_QUERY_THROW );
1116 OUString aImplName;
1117 std::vector< LanguageType > aLanguages;
1118 uno::Reference< XServiceInfo > xInfo( xSvc, uno::UNO_QUERY );
1119 if (xInfo.is())
1120 aImplName = xInfo->getImplementationName();
1121 SAL_WARN_IF( aImplName.isEmpty(), "linguistic", "empty implementation name" );
1122 uno::Sequence<lang::Locale> aLocaleSequence(xSvc->getLocales());
1123 aLanguages = LocaleSeqToLangVec( aLocaleSequence );
1125 pAvailThesSvcs->push_back( SvcInfo( aImplName, std::move(aLanguages) ) );
1127 catch (const uno::Exception &)
1129 SAL_WARN( "linguistic", "createInstance failed" );
1136 void LngSvcMgr::SetCfgServiceLists( SpellCheckerDispatcher &rSpellDsp )
1138 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SetCfgServiceLists - Spell" );
1140 OUString aNode("ServiceManager/SpellCheckerList");
1141 uno::Sequence< OUString > aNames( /*aCfg.*/GetNodeNames( aNode ) );
1143 // append path prefix need for 'GetProperties' call below
1144 OUString aPrefix = aNode + "/";
1145 for (OUString & name : asNonConstRange(aNames))
1147 name = aPrefix + name;
1150 const uno::Sequence< uno::Any > aValues( /*aCfg.*/GetProperties( aNames ) );
1151 if (!(aNames.hasElements() && aNames.getLength() == aValues.getLength()))
1152 return;
1154 const OUString *pNames = aNames.getConstArray();
1155 for (const uno::Any& rValue : aValues)
1157 uno::Sequence< OUString > aSvcImplNames;
1158 if (rValue >>= aSvcImplNames)
1160 OUString aLocaleStr( *pNames++ );
1161 sal_Int32 nSeparatorPos = aLocaleStr.lastIndexOf( '/' );
1162 aLocaleStr = aLocaleStr.copy( nSeparatorPos + 1 );
1163 rSpellDsp.SetServiceList( LanguageTag::convertToLocale(aLocaleStr), aSvcImplNames );
1169 void LngSvcMgr::SetCfgServiceLists( GrammarCheckingIterator &rGrammarDsp )
1171 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SetCfgServiceLists - Grammar" );
1173 OUString aNode("ServiceManager/GrammarCheckerList");
1174 uno::Sequence< OUString > aNames( /*aCfg.*/GetNodeNames( aNode ) );
1176 // append path prefix need for 'GetProperties' call below
1177 OUString aPrefix = aNode + "/";
1178 for (OUString & name : asNonConstRange(aNames))
1180 name = aPrefix + name;
1183 const uno::Sequence< uno::Any > aValues( /*aCfg.*/GetProperties( aNames ) );
1184 if (!(aNames.hasElements() && aNames.getLength() == aValues.getLength()))
1185 return;
1187 const OUString *pNames = aNames.getConstArray();
1188 for (const uno::Any& rValue : aValues)
1190 uno::Sequence< OUString > aSvcImplNames;
1191 if (rValue >>= aSvcImplNames)
1193 // there should only be one grammar checker in use per language...
1194 if (aSvcImplNames.getLength() > 1)
1195 aSvcImplNames.realloc(1);
1197 OUString aLocaleStr( *pNames++ );
1198 sal_Int32 nSeparatorPos = aLocaleStr.lastIndexOf( '/' );
1199 aLocaleStr = aLocaleStr.copy( nSeparatorPos + 1 );
1200 rGrammarDsp.SetServiceList( LanguageTag::convertToLocale(aLocaleStr), aSvcImplNames );
1206 void LngSvcMgr::SetCfgServiceLists( HyphenatorDispatcher &rHyphDsp )
1208 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SetCfgServiceLists - Hyph" );
1210 OUString aNode("ServiceManager/HyphenatorList");
1211 uno::Sequence< OUString > aNames( /*aCfg.*/GetNodeNames( aNode ) );
1213 // append path prefix need for 'GetProperties' call below
1214 OUString aPrefix = aNode + "/";
1215 for (OUString & name : asNonConstRange(aNames))
1217 name = aPrefix + name;
1220 const uno::Sequence< uno::Any > aValues( /*aCfg.*/GetProperties( aNames ) );
1221 if (!(aNames.hasElements() && aNames.getLength() == aValues.getLength()))
1222 return;
1224 const OUString *pNames = aNames.getConstArray();
1225 for (const uno::Any& rValue : aValues)
1227 uno::Sequence< OUString > aSvcImplNames;
1228 if (rValue >>= aSvcImplNames)
1230 // there should only be one hyphenator in use per language...
1231 if (aSvcImplNames.getLength() > 1)
1232 aSvcImplNames.realloc(1);
1234 OUString aLocaleStr( *pNames++ );
1235 sal_Int32 nSeparatorPos = aLocaleStr.lastIndexOf( '/' );
1236 aLocaleStr = aLocaleStr.copy( nSeparatorPos + 1 );
1237 rHyphDsp.SetServiceList( LanguageTag::convertToLocale(aLocaleStr), aSvcImplNames );
1243 void LngSvcMgr::SetCfgServiceLists( ThesaurusDispatcher &rThesDsp )
1245 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SetCfgServiceLists - Thes" );
1247 OUString aNode("ServiceManager/ThesaurusList");
1248 uno::Sequence< OUString > aNames( /*aCfg.*/GetNodeNames( aNode ) );
1250 // append path prefix need for 'GetProperties' call below
1251 OUString aPrefix = aNode + "/";
1252 for (OUString & name : asNonConstRange(aNames))
1254 name = aPrefix + name;
1257 const uno::Sequence< uno::Any > aValues( /*aCfg.*/GetProperties( aNames ) );
1258 if (!(aNames.hasElements() && aNames.getLength() == aValues.getLength()))
1259 return;
1261 const OUString *pNames = aNames.getConstArray();
1262 for (const uno::Any& rValue : aValues)
1264 uno::Sequence< OUString > aSvcImplNames;
1265 if (rValue >>= aSvcImplNames)
1267 OUString aLocaleStr( *pNames++ );
1268 sal_Int32 nSeparatorPos = aLocaleStr.lastIndexOf( '/' );
1269 aLocaleStr = aLocaleStr.copy( nSeparatorPos + 1 );
1270 rThesDsp.SetServiceList( LanguageTag::convertToLocale(aLocaleStr), aSvcImplNames );
1276 uno::Reference< linguistic2::XSpellChecker > SAL_CALL
1277 LngSvcMgr::getSpellChecker()
1279 osl::MutexGuard aGuard( GetLinguMutex() );
1280 #if OSL_DEBUG_LEVEL > 0
1281 getAvailableLocales(SN_SPELLCHECKER);
1282 #endif
1284 uno::Reference< linguistic2::XSpellChecker > xRes;
1285 if (!bDisposing)
1287 if (!mxSpellDsp.is())
1288 GetSpellCheckerDsp_Impl();
1289 xRes = mxSpellDsp.get();
1291 return xRes;
1295 uno::Reference< linguistic2::XHyphenator > SAL_CALL
1296 LngSvcMgr::getHyphenator()
1298 osl::MutexGuard aGuard( GetLinguMutex() );
1299 #if OSL_DEBUG_LEVEL > 0
1300 getAvailableLocales(SN_HYPHENATOR);
1301 #endif
1302 uno::Reference< linguistic2::XHyphenator > xRes;
1303 if (!bDisposing)
1305 if (!mxHyphDsp.is())
1306 GetHyphenatorDsp_Impl();
1307 xRes = mxHyphDsp.get();
1309 return xRes;
1313 uno::Reference< linguistic2::XThesaurus > SAL_CALL
1314 LngSvcMgr::getThesaurus()
1316 osl::MutexGuard aGuard( GetLinguMutex() );
1317 #if OSL_DEBUG_LEVEL > 0
1318 getAvailableLocales(SN_THESAURUS);
1319 #endif
1320 uno::Reference< linguistic2::XThesaurus > xRes;
1321 if (!bDisposing)
1323 if (!mxThesDsp.is())
1324 GetThesaurusDsp_Impl();
1325 xRes = mxThesDsp.get();
1327 return xRes;
1331 sal_Bool SAL_CALL
1332 LngSvcMgr::addLinguServiceManagerListener(
1333 const uno::Reference< lang::XEventListener >& xListener )
1335 osl::MutexGuard aGuard( GetLinguMutex() );
1337 if (bDisposing || !xListener.is())
1338 return false;
1340 if (!mxListenerHelper.is())
1341 GetListenerHelper_Impl();
1342 mxListenerHelper->AddLngSvcMgrListener( xListener );
1343 return true;
1347 sal_Bool SAL_CALL
1348 LngSvcMgr::removeLinguServiceManagerListener(
1349 const uno::Reference< lang::XEventListener >& xListener )
1351 osl::MutexGuard aGuard( GetLinguMutex() );
1353 if (bDisposing || !xListener.is())
1354 return false;
1356 DBG_ASSERT( mxListenerHelper.is(), "listener removed without being added" );
1357 if (!mxListenerHelper.is())
1358 GetListenerHelper_Impl();
1359 mxListenerHelper->RemoveLngSvcMgrListener( xListener );
1360 return true;
1364 uno::Sequence< OUString > SAL_CALL
1365 LngSvcMgr::getAvailableServices(
1366 const OUString& rServiceName,
1367 const lang::Locale& rLocale )
1369 osl::MutexGuard aGuard( GetLinguMutex() );
1371 uno::Sequence< OUString > aRes;
1372 const SvcInfoArray *pInfoArray = nullptr;
1374 if (rServiceName == SN_SPELLCHECKER)
1376 GetAvailableSpellSvcs_Impl();
1377 pInfoArray = &*pAvailSpellSvcs;
1379 else if (rServiceName == SN_GRAMMARCHECKER)
1381 GetAvailableGrammarSvcs_Impl();
1382 pInfoArray = &*pAvailGrammarSvcs;
1384 else if (rServiceName == SN_HYPHENATOR)
1386 GetAvailableHyphSvcs_Impl();
1387 pInfoArray = &*pAvailHyphSvcs;
1389 else if (rServiceName == SN_THESAURUS)
1391 GetAvailableThesSvcs_Impl();
1392 pInfoArray = &*pAvailThesSvcs;
1395 if (pInfoArray)
1397 std::vector<OUString> aVec;
1398 aVec.reserve(pInfoArray->size());
1400 LanguageType nLanguage = LinguLocaleToLanguage( rLocale );
1401 for (const auto& rInfo : *pInfoArray)
1403 if (LinguIsUnspecified( nLanguage )
1404 || rInfo.HasLanguage( nLanguage ))
1406 aVec.push_back(rInfo.aSvcImplName);
1410 aRes = comphelper::containerToSequence(aVec);
1413 return aRes;
1417 uno::Sequence< lang::Locale > SAL_CALL
1418 LngSvcMgr::getAvailableLocales(
1419 const OUString& rServiceName )
1421 osl::MutexGuard aGuard( GetLinguMutex() );
1423 uno::Sequence< lang::Locale > aRes;
1425 uno::Sequence< lang::Locale > *pAvailLocales = nullptr;
1426 if (rServiceName == SN_SPELLCHECKER)
1427 pAvailLocales = &aAvailSpellLocales;
1428 else if (rServiceName == SN_GRAMMARCHECKER)
1429 pAvailLocales = &aAvailGrammarLocales;
1430 else if (rServiceName == SN_HYPHENATOR)
1431 pAvailLocales = &aAvailHyphLocales;
1432 else if (rServiceName == SN_THESAURUS)
1433 pAvailLocales = &aAvailThesLocales;
1435 // Nowadays (with OOo lingu in SO) we want to know immediately about
1436 // new downloaded dictionaries and have them ready right away if the Tools/Options...
1437 // is used to activate them. Thus we can not rely anymore on buffered data.
1438 if (pAvailLocales)
1440 *pAvailLocales = GetAvailLocales(getAvailableServices(rServiceName, lang::Locale()));
1441 aRes = *pAvailLocales;
1444 return aRes;
1447 static bool IsEqSvcList( const uno::Sequence< OUString > &rList1,
1448 const uno::Sequence< OUString > &rList2 )
1450 // returns true if both sequences are equal
1451 return rList1.getLength() == rList2.getLength()
1452 && std::equal(rList1.begin(), rList1.end(), rList2.begin(), rList2.end());
1456 void SAL_CALL
1457 LngSvcMgr::setConfiguredServices(
1458 const OUString& rServiceName,
1459 const lang::Locale& rLocale,
1460 const uno::Sequence< OUString >& rServiceImplNames )
1462 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::setConfiguredServices" );
1464 osl::MutexGuard aGuard( GetLinguMutex() );
1466 LanguageType nLanguage = LinguLocaleToLanguage( rLocale );
1467 if (LinguIsUnspecified( nLanguage))
1468 return;
1470 if (rServiceName == SN_SPELLCHECKER)
1472 if (!mxSpellDsp.is())
1473 GetSpellCheckerDsp_Impl();
1474 bool bChanged = !IsEqSvcList( rServiceImplNames,
1475 mxSpellDsp->GetServiceList( rLocale ) );
1476 if (bChanged)
1478 mxSpellDsp->SetServiceList( rLocale, rServiceImplNames );
1479 SaveCfgSvcs( SN_SPELLCHECKER );
1481 if (mxListenerHelper)
1482 mxListenerHelper->AddLngSvcEvt(
1483 linguistic2::LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN |
1484 linguistic2::LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN );
1487 else if (rServiceName == SN_GRAMMARCHECKER)
1489 if (!mxGrammarDsp.is())
1490 GetGrammarCheckerDsp_Impl();
1491 if (!mxGrammarDsp) // e.g., when !SvtLinguConfig().HasGrammarChecker()
1492 return;
1493 bool bChanged = !IsEqSvcList( rServiceImplNames,
1494 mxGrammarDsp->GetServiceList( rLocale ) );
1495 if (bChanged)
1497 mxGrammarDsp->SetServiceList( rLocale, rServiceImplNames );
1498 SaveCfgSvcs( SN_GRAMMARCHECKER );
1500 if (mxListenerHelper)
1501 mxListenerHelper->AddLngSvcEvt(
1502 linguistic2::LinguServiceEventFlags::PROOFREAD_AGAIN );
1505 else if (rServiceName == SN_HYPHENATOR)
1507 if (!mxHyphDsp.is())
1508 GetHyphenatorDsp_Impl();
1509 bool bChanged = !IsEqSvcList( rServiceImplNames,
1510 mxHyphDsp->GetServiceList( rLocale ) );
1511 if (bChanged)
1513 mxHyphDsp->SetServiceList( rLocale, rServiceImplNames );
1514 SaveCfgSvcs( SN_HYPHENATOR );
1516 if (mxListenerHelper)
1517 mxListenerHelper->AddLngSvcEvt(
1518 linguistic2::LinguServiceEventFlags::HYPHENATE_AGAIN );
1521 else if (rServiceName == SN_THESAURUS)
1523 if (!mxThesDsp.is())
1524 GetThesaurusDsp_Impl();
1525 bool bChanged = !IsEqSvcList( rServiceImplNames,
1526 mxThesDsp->GetServiceList( rLocale ) );
1527 if (bChanged)
1529 mxThesDsp->SetServiceList( rLocale, rServiceImplNames );
1530 SaveCfgSvcs( SN_THESAURUS );
1536 bool LngSvcMgr::SaveCfgSvcs( std::u16string_view rServiceName )
1538 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SaveCfgSvcs" );
1540 bool bRes = false;
1542 LinguDispatcher *pDsp = nullptr;
1543 uno::Sequence< lang::Locale > aLocales;
1545 if (rServiceName == SN_SPELLCHECKER)
1547 if (!mxSpellDsp)
1548 GetSpellCheckerDsp_Impl();
1549 pDsp = mxSpellDsp.get();
1550 aLocales = getAvailableLocales( SN_SPELLCHECKER );
1552 else if (rServiceName == SN_GRAMMARCHECKER)
1554 if (!mxGrammarDsp.is())
1555 GetGrammarCheckerDsp_Impl();
1556 pDsp = mxGrammarDsp.get();
1557 aLocales = getAvailableLocales( SN_GRAMMARCHECKER );
1559 else if (rServiceName == SN_HYPHENATOR)
1561 if (!mxHyphDsp.is())
1562 GetHyphenatorDsp_Impl();
1563 pDsp = mxHyphDsp.get();
1564 aLocales = getAvailableLocales( SN_HYPHENATOR );
1566 else if (rServiceName == SN_THESAURUS)
1568 if (!mxThesDsp.is())
1569 GetThesaurusDsp_Impl();
1570 pDsp = mxThesDsp.get();
1571 aLocales = getAvailableLocales( SN_THESAURUS );
1574 if (pDsp && aLocales.hasElements())
1576 uno::Sequence< beans::PropertyValue > aValues( aLocales.getLength() );
1577 beans::PropertyValue *pValue = aValues.getArray();
1579 // get node name to be used
1580 const char *pNodeName = nullptr;
1581 if (pDsp == mxSpellDsp.get())
1582 pNodeName = "ServiceManager/SpellCheckerList";
1583 else if (pDsp == mxGrammarDsp.get())
1584 pNodeName = "ServiceManager/GrammarCheckerList";
1585 else if (pDsp == mxHyphDsp.get())
1586 pNodeName = "ServiceManager/HyphenatorList";
1587 else if (pDsp == mxThesDsp.get())
1588 pNodeName = "ServiceManager/ThesaurusList";
1589 else
1591 SAL_WARN( "linguistic", "node name missing" );
1593 OUString aNodeName( OUString::createFromAscii(pNodeName) );
1595 for (const lang::Locale& rLocale : std::as_const(aLocales))
1597 uno::Sequence< OUString > aSvcImplNames = pDsp->GetServiceList( rLocale );
1599 // build value to be written back to configuration
1600 uno::Any aCfgAny;
1601 if ((pDsp == mxHyphDsp.get() || pDsp == mxGrammarDsp.get()) && aSvcImplNames.getLength() > 1)
1602 aSvcImplNames.realloc(1); // there should be only one entry for hyphenators or grammar checkers (because they are not chained)
1603 aCfgAny <<= aSvcImplNames;
1604 DBG_ASSERT( aCfgAny.hasValue(), "missing value for 'Any' type" );
1606 OUString aCfgLocaleStr( LanguageTag::convertToBcp47( rLocale));
1607 pValue->Value = aCfgAny;
1608 pValue->Name = aNodeName + "/" + aCfgLocaleStr;
1609 pValue++;
1612 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SaveCfgSvcs - ReplaceSetProperties" );
1613 // change, add new or replace existing entries.
1614 bRes |= /*aCfg.*/ReplaceSetProperties( aNodeName, aValues );
1618 return bRes;
1622 static uno::Sequence< OUString > GetLangSvcList( const uno::Any &rVal )
1624 uno::Sequence< OUString > aRes;
1626 if (rVal.hasValue())
1628 rVal >>= aRes;
1629 #if OSL_DEBUG_LEVEL > 0
1630 for (const OUString& rSvcName : std::as_const(aRes))
1632 SAL_WARN_IF( rSvcName.isEmpty(), "linguistic", "service impl-name missing" );
1634 #endif
1637 return aRes;
1641 static uno::Sequence< OUString > GetLangSvc( const uno::Any &rVal )
1643 uno::Sequence< OUString > aRes;
1644 if (!rVal.hasValue())
1645 return aRes;
1647 // allowing for a sequence here as well (even though it should only
1648 // be a string) makes coding easier in other places since one needs
1649 // not make a special case for writing a string only and not a
1650 // sequence of strings.
1651 if (rVal >>= aRes)
1653 // but only the first string should be used.
1654 if (aRes.getLength() > 1)
1655 aRes.realloc(1);
1657 else
1659 OUString aImplName;
1660 if ((rVal >>= aImplName) && !aImplName.isEmpty())
1662 aRes.realloc(1);
1663 aRes.getArray()[0] = aImplName;
1665 else
1667 SAL_WARN( "linguistic", "GetLangSvc: unexpected type encountered" );
1671 return aRes;
1675 uno::Sequence< OUString > SAL_CALL
1676 LngSvcMgr::getConfiguredServices(
1677 const OUString& rServiceName,
1678 const lang::Locale& rLocale )
1680 osl::MutexGuard aGuard( GetLinguMutex() );
1682 uno::Sequence< OUString > aSvcImplNames;
1684 OUString aCfgLocale( LanguageTag::convertToBcp47( rLocale) );
1686 uno::Sequence< uno::Any > aValues;
1687 uno::Sequence< OUString > aNames( 1 );
1688 OUString *pNames = aNames.getArray();
1689 if ( rServiceName == SN_SPELLCHECKER )
1691 OUString aNode( "ServiceManager/SpellCheckerList");
1692 const uno::Sequence< OUString > aNodeEntries( GetNodeNames( aNode ) );
1693 if (lcl_SeqHasString( aNodeEntries, aCfgLocale ))
1695 pNames[0] = aNode + "/" + aCfgLocale;
1696 aValues = /*aCfg.*/GetProperties( aNames );
1697 if (aValues.hasElements())
1698 aSvcImplNames = GetLangSvcList( aValues.getConstArray()[0] );
1701 else if ( rServiceName == SN_GRAMMARCHECKER )
1703 OUString aNode( "ServiceManager/GrammarCheckerList");
1704 const uno::Sequence< OUString > aNodeEntries( GetNodeNames( aNode ) );
1705 if (lcl_SeqHasString( aNodeEntries, aCfgLocale ))
1707 pNames[0] = aNode + "/" + aCfgLocale;
1708 aValues = /*aCfg.*/GetProperties( aNames );
1709 if (aValues.hasElements())
1710 aSvcImplNames = GetLangSvc( aValues.getConstArray()[0] );
1713 else if ( rServiceName == SN_HYPHENATOR )
1715 OUString aNode( "ServiceManager/HyphenatorList");
1716 const uno::Sequence< OUString > aNodeEntries( GetNodeNames( aNode ) );
1717 if (lcl_SeqHasString( aNodeEntries, aCfgLocale ))
1719 pNames[0] = aNode + "/" + aCfgLocale;
1720 aValues = /*aCfg.*/GetProperties( aNames );
1721 if (aValues.hasElements())
1722 aSvcImplNames = GetLangSvc( aValues.getConstArray()[0] );
1725 else if ( rServiceName == SN_THESAURUS )
1727 OUString aNode( "ServiceManager/ThesaurusList");
1728 const uno::Sequence< OUString > aNodeEntries( GetNodeNames( aNode ) );
1729 if (lcl_SeqHasString( aNodeEntries, aCfgLocale ))
1731 pNames[0] = aNode + "/" + aCfgLocale;
1732 aValues = /*aCfg.*/GetProperties( aNames );
1733 if (aValues.hasElements())
1734 aSvcImplNames = GetLangSvcList( aValues.getConstArray()[0] );
1738 return aSvcImplNames;
1742 void SAL_CALL
1743 LngSvcMgr::dispose()
1745 osl::MutexGuard aGuard( GetLinguMutex() );
1747 if (!bDisposing)
1749 bDisposing = true;
1751 // require listeners to release this object
1752 lang::EventObject aEvtObj( static_cast<XLinguServiceManager*>(this) );
1753 aEvtListeners.disposeAndClear( aEvtObj );
1755 if (mxListenerHelper.is())
1756 mxListenerHelper->DisposeAndClear( aEvtObj );
1761 void SAL_CALL
1762 LngSvcMgr::addEventListener(
1763 const uno::Reference< lang::XEventListener >& xListener )
1765 osl::MutexGuard aGuard( GetLinguMutex() );
1767 if (!bDisposing && xListener.is())
1769 aEvtListeners.addInterface( xListener );
1774 void SAL_CALL
1775 LngSvcMgr::removeEventListener(
1776 const uno::Reference< lang::XEventListener >& xListener )
1778 osl::MutexGuard aGuard( GetLinguMutex() );
1780 if (xListener.is())
1782 aEvtListeners.removeInterface( xListener );
1787 bool LngSvcMgr::AddLngSvcEvtBroadcaster(
1788 const uno::Reference< linguistic2::XLinguServiceEventBroadcaster > &rxBroadcaster )
1790 if (!rxBroadcaster.is())
1791 return false;
1792 if (!mxListenerHelper.is())
1793 GetListenerHelper_Impl();
1794 mxListenerHelper->AddLngSvcEvtBroadcaster( rxBroadcaster );
1795 return true;
1799 OUString SAL_CALL
1800 LngSvcMgr::getImplementationName()
1802 return "com.sun.star.lingu2.LngSvcMgr";
1806 sal_Bool SAL_CALL
1807 LngSvcMgr::supportsService( const OUString& ServiceName )
1809 return cppu::supportsService(this, ServiceName);
1813 uno::Sequence< OUString > SAL_CALL
1814 LngSvcMgr::getSupportedServiceNames()
1816 return { "com.sun.star.linguistic2.LinguServiceManager" };
1819 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
1820 linguistic_LngSvcMgr_get_implementation(
1821 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
1823 return cppu::acquire(new LngSvcMgr());
1827 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */