look for java, javac and javadoc with the .exe suffix on windows
[LibreOffice.git] / linguistic / source / lngsvcmgr.cxx
blobbb503360fbdfb4f48be14665767f8d14d3e333df
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 bool LngSvcMgr::joinThreads()
457 if (mxGrammarDsp && !
458 mxGrammarDsp->joinThreads())
459 return false;
460 return true;
463 //run update, and inform everyone that dictionaries (may) have changed, this
464 //needs to be run in the main thread because
465 //utl::ConfigChangeListener_Impl::changesOccurred grabs the SolarMutex and we
466 //get notified that an extension was added from an extension manager thread
467 IMPL_LINK_NOARG(LngSvcMgr, updateAndBroadcast, Timer *, void)
469 osl::MutexGuard aGuard( GetLinguMutex() );
471 UpdateAll();
473 if (mxListenerHelper.is())
475 mxListenerHelper->AddLngSvcEvt(
476 linguistic2::LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN |
477 linguistic2::LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN |
478 linguistic2::LinguServiceEventFlags::PROOFREAD_AGAIN |
479 linguistic2::LinguServiceEventFlags::HYPHENATE_AGAIN );
483 void LngSvcMgr::stopListening()
485 osl::MutexGuard aGuard(GetLinguMutex());
487 if (!xMB.is())
488 return;
492 uno::Reference<util::XModifyListener> xListener(this);
493 xMB->removeModifyListener(xListener);
495 catch (const uno::Exception&)
499 xMB.clear();
502 void LngSvcMgr::disposing(const lang::EventObject&)
504 stopListening();
507 #if defined __GNUC__ && !defined __clang__ && __GNUC__ == 14
508 #pragma GCC diagnostic push
509 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
510 #endif
511 LngSvcMgr::~LngSvcMgr()
513 stopListening();
515 // memory for pSpellDsp, pHyphDsp, pThesDsp, pListenerHelper
516 // will be freed in the destructor of the respective Reference's
517 // xSpellDsp, xGrammarDsp, xHyphDsp, xThesDsp
519 pAvailSpellSvcs.reset();
520 pAvailGrammarSvcs.reset();
521 pAvailHyphSvcs.reset();
522 pAvailThesSvcs.reset();
524 #if defined __GNUC__ && !defined __clang__ && __GNUC__ == 14
525 #pragma GCC diagnostic pop
526 #endif
528 namespace
530 using lang::Locale;
531 using uno::Any;
532 using uno::Sequence;
534 bool lcl_FindEntry( const OUString &rEntry, const Sequence< OUString > &rCfgSvcs )
536 return comphelper::findValue(rCfgSvcs, rEntry) != -1;
539 bool lcl_FindEntry( const OUString &rEntry, const std::vector< OUString > &rCfgSvcs )
541 return std::find(rCfgSvcs.begin(), rCfgSvcs.end(), rEntry) != rCfgSvcs.end();
544 Sequence< OUString > lcl_GetLastFoundSvcs(
545 SvtLinguConfig const &rCfg,
546 const OUString &rLastFoundList ,
547 const OUString& rCfgLocaleStr )
549 Sequence< OUString > aRes;
551 Sequence< OUString > aNodeNames( rCfg.GetNodeNames(rLastFoundList) );
552 bool bFound = lcl_FindEntry( rCfgLocaleStr, aNodeNames);
554 if (bFound)
556 Sequence< OUString > aNames { rLastFoundList + "/" + rCfgLocaleStr };
557 Sequence< Any > aValues( rCfg.GetProperties( aNames ) );
558 if (aValues.hasElements())
560 SAL_WARN_IF( aValues.getLength() != 1, "linguistic", "unexpected length of sequence" );
561 Sequence< OUString > aSvcImplNames;
562 if (aValues.getConstArray()[0] >>= aSvcImplNames)
563 aRes = aSvcImplNames;
564 else
566 SAL_WARN( "linguistic", "type mismatch" );
571 return aRes;
574 Sequence< OUString > lcl_RemoveMissingEntries(
575 const Sequence< OUString > &rCfgSvcs,
576 const Sequence< OUString > &rAvailSvcs )
578 std::vector<OUString> aRes;
579 aRes.reserve(rCfgSvcs.getLength());
581 std::copy_if(rCfgSvcs.begin(), rCfgSvcs.end(), std::back_inserter(aRes),
582 [&rAvailSvcs](const OUString& entry) { return lcl_SeqHasString(rAvailSvcs, entry); });
584 return comphelper::containerToSequence(aRes);
587 Sequence< OUString > lcl_GetNewEntries(
588 const Sequence< OUString > &rLastFoundSvcs,
589 const Sequence< OUString > &rAvailSvcs )
591 std::vector<OUString> aRes;
592 aRes.reserve(rAvailSvcs.getLength());
594 std::copy_if(rAvailSvcs.begin(), rAvailSvcs.end(), std::back_inserter(aRes),
595 [&rLastFoundSvcs](const OUString& rEntry) {
596 return !rEntry.isEmpty() && !lcl_FindEntry( rEntry, rLastFoundSvcs ); });
598 return comphelper::containerToSequence(aRes);
601 Sequence< OUString > lcl_MergeSeq(
602 const Sequence< OUString > &rCfgSvcs,
603 const Sequence< OUString > &rNewSvcs )
605 std::vector<OUString> aRes;
606 aRes.reserve(rCfgSvcs.getLength() + rNewSvcs.getLength());
608 auto lVecNotHasString = [&aRes](const OUString& rEntry)
609 { return !rEntry.isEmpty() && !lcl_FindEntry(rEntry, aRes); };
611 // add previously configured service first and append
612 // new found services at the end
613 for (const Sequence< OUString > &rSeq : { rCfgSvcs, rNewSvcs })
615 std::copy_if(rSeq.begin(), rSeq.end(), std::back_inserter(aRes), lVecNotHasString);
618 return comphelper::containerToSequence(aRes);
622 void LngSvcMgr::UpdateAll()
624 using beans::PropertyValue;
625 using lang::Locale;
626 using uno::Sequence;
628 typedef std::map< OUString, Sequence< OUString > > list_entry_map_t;
630 SvtLinguConfig aCfg;
632 const int nNumServices = 4;
633 static constexpr OUString apServices[nNumServices] = { SN_SPELLCHECKER, SN_GRAMMARCHECKER, SN_HYPHENATOR, SN_THESAURUS };
634 const char * const apCurLists[nNumServices] = { "ServiceManager/SpellCheckerList", "ServiceManager/GrammarCheckerList", "ServiceManager/HyphenatorList", "ServiceManager/ThesaurusList" };
635 const char * const apLastFoundLists[nNumServices] = { "ServiceManager/LastFoundSpellCheckers", "ServiceManager/LastFoundGrammarCheckers", "ServiceManager/LastFoundHyphenators", "ServiceManager/LastFoundThesauri" };
637 // usage of indices as above: 0 = spell checker, 1 = grammar checker, 2 = hyphenator, 3 = thesaurus
638 std::vector< list_entry_map_t > aLastFoundSvcs(nNumServices);
639 std::vector< list_entry_map_t > aCurSvcs(nNumServices);
641 for (int k = 0; k < nNumServices; ++k)
643 OUString const & aService = apServices[k];
644 OUString aActiveList( OUString::createFromAscii( apCurLists[k] ) );
645 OUString aLastFoundList( OUString::createFromAscii( apLastFoundLists[k] ) );
648 // remove configured but not available language/services entries
650 const Sequence< OUString > aNodeNames( aCfg.GetNodeNames( aActiveList ) ); // list of configured locales
651 for (const OUString& rNodeName : aNodeNames)
653 Locale aLocale( LanguageTag::convertToLocale( rNodeName));
654 Sequence< OUString > aCfgSvcs( getConfiguredServices( aService, aLocale ));
655 Sequence< OUString > aAvailSvcs( getAvailableServices( aService, aLocale ));
657 aCfgSvcs = lcl_RemoveMissingEntries( aCfgSvcs, aAvailSvcs );
659 aCurSvcs[k][ rNodeName ] = aCfgSvcs;
663 // add new available language/service entries
664 // and
665 // set last found services to currently available ones
667 const Sequence< Locale > aAvailLocales( getAvailableLocales(aService) );
668 for (const Locale& rAvailLocale : aAvailLocales)
670 OUString aCfgLocaleStr( LanguageTag::convertToBcp47( rAvailLocale));
672 Sequence< OUString > aAvailSvcs( getAvailableServices( aService, rAvailLocale ));
674 aLastFoundSvcs[k][ aCfgLocaleStr ] = aAvailSvcs;
676 Sequence< OUString > aLastSvcs(
677 lcl_GetLastFoundSvcs( aCfg, aLastFoundList , aCfgLocaleStr ));
678 Sequence< OUString > aNewSvcs =
679 lcl_GetNewEntries( aLastSvcs, aAvailSvcs );
681 Sequence< OUString > aCfgSvcs( aCurSvcs[k][ aCfgLocaleStr ] );
683 // merge services list (previously configured to be listed first).
684 aCfgSvcs = lcl_MergeSeq( aCfgSvcs, aNewSvcs );
686 aCurSvcs[k][ aCfgLocaleStr ] = aCfgSvcs;
691 // write new data back to configuration
693 for (int k = 0; k < nNumServices; ++k)
695 for (int i = 0; i < 2; ++i)
697 const char *pSubNodeName = (i == 0) ? apCurLists[k] : apLastFoundLists[k];
698 OUString aSubNodeName( OUString::createFromAscii(pSubNodeName) );
700 list_entry_map_t &rCurMap = (i == 0) ? aCurSvcs[k] : aLastFoundSvcs[k];
701 sal_Int32 nVals = static_cast< sal_Int32 >( rCurMap.size() );
702 Sequence< PropertyValue > aNewValues( nVals );
703 PropertyValue *pNewValue = aNewValues.getArray();
704 for (auto const& elem : rCurMap)
706 pNewValue->Name = aSubNodeName + "/" + elem.first;
707 pNewValue->Value <<= elem.second;
708 ++pNewValue;
710 OSL_ENSURE( pNewValue - aNewValues.getConstArray() == nVals,
711 "possible mismatch of sequence size and property number" );
714 // add new or replace existing entries.
715 bool bRes = aCfg.ReplaceSetProperties( aSubNodeName, aNewValues );
716 SAL_WARN_IF(!bRes, "linguistic", "failed to set new configuration values");
721 //The new settings in the configuration get applied ! because we are
722 //listening to the configuration for changes of the relevant ! properties
723 //and Notify applies the new settings.
726 void LngSvcMgr::Notify( const uno::Sequence< OUString > &rPropertyNames )
728 static constexpr OUString aSpellCheckerList( u"ServiceManager/SpellCheckerList"_ustr );
729 static constexpr OUString aGrammarCheckerList( u"ServiceManager/GrammarCheckerList"_ustr );
730 static constexpr OUString aHyphenatorList( u"ServiceManager/HyphenatorList"_ustr );
731 static constexpr OUString aThesaurusList( u"ServiceManager/ThesaurusList"_ustr );
733 const uno::Sequence< OUString > aSpellCheckerListEntries( GetNodeNames( aSpellCheckerList ) );
734 const uno::Sequence< OUString > aGrammarCheckerListEntries( GetNodeNames( aGrammarCheckerList ) );
735 const uno::Sequence< OUString > aHyphenatorListEntries( GetNodeNames( aHyphenatorList ) );
736 const uno::Sequence< OUString > aThesaurusListEntries( GetNodeNames( aThesaurusList ) );
738 uno::Sequence< uno::Any > aValues;
739 uno::Sequence< OUString > aNames( 1 );
740 OUString *pNames = aNames.getArray();
742 for (const OUString& rName : rPropertyNames)
744 // property names look like
745 // "ServiceManager/ThesaurusList/de-CH"
747 sal_Int32 nKeyStart;
748 nKeyStart = rName.lastIndexOf( '/' );
749 OUString aKeyText;
750 if (nKeyStart != -1)
751 aKeyText = rName.copy( nKeyStart + 1 );
752 SAL_WARN_IF( aKeyText.isEmpty(), "linguistic", "unexpected key (lang::Locale) string" );
753 if (rName.startsWith( aSpellCheckerList ))
755 osl::MutexGuard aGuard(GetLinguMutex());
757 // delete old cached data, needs to be acquired new on demand
758 pAvailSpellSvcs.reset();
760 if (lcl_SeqHasString( aSpellCheckerListEntries, aKeyText ))
762 pNames[0] = aSpellCheckerList + "/" + aKeyText;
763 aValues = /*aCfg.*/GetProperties( aNames );
764 uno::Sequence< OUString > aSvcImplNames;
765 if (aValues.hasElements())
766 aSvcImplNames = GetLangSvcList( aValues.getConstArray()[0] );
768 LanguageType nLang = LANGUAGE_NONE;
769 if (!aKeyText.isEmpty())
770 nLang = LanguageTag::convertToLanguageType( aKeyText );
772 GetSpellCheckerDsp_Impl( false ); // don't set service list, it will be done below
773 mxSpellDsp->SetServiceList( LanguageTag::convertToLocale(nLang), aSvcImplNames );
776 else if (rName.startsWith( aGrammarCheckerList ))
778 osl::MutexGuard aGuard(GetLinguMutex());
780 // delete old cached data, needs to be acquired new on demand
781 pAvailGrammarSvcs.reset();
783 if (lcl_SeqHasString( aGrammarCheckerListEntries, aKeyText ))
785 pNames[0] = aGrammarCheckerList + "/" + aKeyText;
786 aValues = /*aCfg.*/GetProperties( aNames );
787 uno::Sequence< OUString > aSvcImplNames;
788 if (aValues.hasElements())
789 aSvcImplNames = GetLangSvc( aValues.getConstArray()[0] );
791 LanguageType nLang = LANGUAGE_NONE;
792 if (!aKeyText.isEmpty())
793 nLang = LanguageTag::convertToLanguageType( aKeyText );
795 if (SvtLinguConfig().HasGrammarChecker())
797 GetGrammarCheckerDsp_Impl( false ); // don't set service list, it will be done below
798 mxGrammarDsp->SetServiceList( LanguageTag::convertToLocale(nLang), aSvcImplNames );
802 else if (rName.startsWith( aHyphenatorList ))
804 osl::MutexGuard aGuard(GetLinguMutex());
806 // delete old cached data, needs to be acquired new on demand
807 pAvailHyphSvcs.reset();
809 if (lcl_SeqHasString( aHyphenatorListEntries, aKeyText ))
811 pNames[0] = aHyphenatorList + "/" + aKeyText;
812 aValues = /*aCfg.*/GetProperties( aNames );
813 uno::Sequence< OUString > aSvcImplNames;
814 if (aValues.hasElements())
815 aSvcImplNames = GetLangSvc( aValues.getConstArray()[0] );
817 LanguageType nLang = LANGUAGE_NONE;
818 if (!aKeyText.isEmpty())
819 nLang = LanguageTag::convertToLanguageType( aKeyText );
821 GetHyphenatorDsp_Impl( false ); // don't set service list, it will be done below
822 mxHyphDsp->SetServiceList( LanguageTag::convertToLocale(nLang), aSvcImplNames );
825 else if (rName.startsWith( aThesaurusList ))
827 osl::MutexGuard aGuard(GetLinguMutex());
829 // delete old cached data, needs to be acquired new on demand
830 pAvailThesSvcs.reset();
832 if (lcl_SeqHasString( aThesaurusListEntries, aKeyText ))
834 pNames[0] = aThesaurusList + "/" + aKeyText;
835 aValues = /*aCfg.*/GetProperties( aNames );
836 uno::Sequence< OUString > aSvcImplNames;
837 if (aValues.hasElements())
838 aSvcImplNames = GetLangSvcList( aValues.getConstArray()[0] );
840 LanguageType nLang = LANGUAGE_NONE;
841 if (!aKeyText.isEmpty())
842 nLang = LanguageTag::convertToLanguageType( aKeyText );
844 GetThesaurusDsp_Impl( false ); // don't set service list, it will be done below
845 mxThesDsp->SetServiceList( LanguageTag::convertToLocale(nLang), aSvcImplNames );
848 else
850 SAL_WARN( "linguistic", "notified for unexpected property" );
856 void LngSvcMgr::ImplCommit()
858 // everything necessary should have already been done by 'SaveCfgSvcs'
859 // called from within 'setConfiguredServices'.
860 // Also this class usually exits only when the Office is being shutdown.
864 void LngSvcMgr::GetListenerHelper_Impl()
866 if (!mxListenerHelper.is())
868 mxListenerHelper = new LngSvcMgrListenerHelper( *this, linguistic::GetDictionaryList() );
873 void LngSvcMgr::GetSpellCheckerDsp_Impl( bool bSetSvcList )
875 if (!mxSpellDsp.is())
877 mxSpellDsp = new SpellCheckerDispatcher( *this );
878 if (bSetSvcList)
879 SetCfgServiceLists( *mxSpellDsp );
884 void LngSvcMgr::GetGrammarCheckerDsp_Impl( bool bSetSvcList )
886 if (mxGrammarDsp.is() || !SvtLinguConfig().HasGrammarChecker())
887 return;
889 //! since the grammar checking iterator needs to be a one instance service
890 //! we need to create it the correct way!
891 uno::Reference< linguistic2::XProofreadingIterator > xGCI;
894 xGCI = linguistic2::ProofreadingIterator::create( comphelper::getProcessComponentContext() );
896 catch (const uno::Exception &)
899 SAL_WARN_IF( !xGCI.is(), "linguistic", "instantiating grammar checking iterator failed" );
901 if (xGCI.is())
903 mxGrammarDsp = dynamic_cast< GrammarCheckingIterator * >(xGCI.get());
904 SAL_WARN_IF( mxGrammarDsp == nullptr, "linguistic", "failed to get implementation" );
905 if (bSetSvcList && mxGrammarDsp.is())
906 SetCfgServiceLists( *mxGrammarDsp );
911 void LngSvcMgr::GetHyphenatorDsp_Impl( bool bSetSvcList )
913 if (!mxHyphDsp.is())
915 mxHyphDsp = new HyphenatorDispatcher( *this );
916 if (bSetSvcList)
917 SetCfgServiceLists( *mxHyphDsp );
922 void LngSvcMgr::GetThesaurusDsp_Impl( bool bSetSvcList )
924 if (!mxThesDsp.is())
926 mxThesDsp = new ThesaurusDispatcher;
927 if (bSetSvcList)
928 SetCfgServiceLists( *mxThesDsp );
933 void LngSvcMgr::GetAvailableSpellSvcs_Impl()
935 if (pAvailSpellSvcs)
936 return;
938 pAvailSpellSvcs.emplace();
940 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
942 uno::Reference< container::XContentEnumerationAccess > xEnumAccess( xContext->getServiceManager(), uno::UNO_QUERY );
943 uno::Reference< container::XEnumeration > xEnum;
944 if (xEnumAccess.is())
945 xEnum = xEnumAccess->createContentEnumeration( SN_SPELLCHECKER );
947 if (!xEnum.is())
948 return;
950 while (xEnum->hasMoreElements())
952 uno::Any aCurrent = xEnum->nextElement();
953 uno::Reference< lang::XSingleComponentFactory > xCompFactory;
954 uno::Reference< lang::XSingleServiceFactory > xFactory;
956 xCompFactory.set(aCurrent, css::uno::UNO_QUERY);
957 if (!xCompFactory.is())
959 xFactory.set(aCurrent, css::uno::UNO_QUERY);
961 if ( xCompFactory.is() || xFactory.is() )
965 uno::Reference< linguistic2::XSpellChecker > xSvc( ( xCompFactory.is() ? xCompFactory->createInstanceWithContext( xContext ) : xFactory->createInstance() ), uno::UNO_QUERY_THROW );
967 OUString aImplName;
968 std::vector< LanguageType > aLanguages;
969 uno::Reference< XServiceInfo > xInfo( xSvc, uno::UNO_QUERY );
970 if (xInfo.is())
971 aImplName = xInfo->getImplementationName();
972 SAL_WARN_IF( aImplName.isEmpty(), "linguistic", "empty implementation name" );
973 uno::Sequence<lang::Locale> aLocaleSequence(xSvc->getLocales());
974 aLanguages = LocaleSeqToLangVec( aLocaleSequence );
976 pAvailSpellSvcs->push_back( SvcInfo( aImplName, std::move(aLanguages) ) );
978 catch (const uno::Exception &)
980 SAL_WARN( "linguistic", "createInstance failed" );
987 void LngSvcMgr::GetAvailableGrammarSvcs_Impl()
989 if (pAvailGrammarSvcs)
990 return;
992 pAvailGrammarSvcs.emplace();
994 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
996 uno::Reference< container::XContentEnumerationAccess > xEnumAccess( xContext->getServiceManager(), uno::UNO_QUERY );
997 uno::Reference< container::XEnumeration > xEnum;
998 if (xEnumAccess.is())
999 xEnum = xEnumAccess->createContentEnumeration( SN_GRAMMARCHECKER );
1001 if (!xEnum.is())
1002 return;
1004 while (xEnum->hasMoreElements())
1006 uno::Any aCurrent = xEnum->nextElement();
1007 uno::Reference< lang::XSingleComponentFactory > xCompFactory;
1008 uno::Reference< lang::XSingleServiceFactory > xFactory;
1010 xCompFactory.set(aCurrent, css::uno::UNO_QUERY);
1011 if (!xCompFactory.is())
1013 xFactory.set(aCurrent, css::uno::UNO_QUERY);
1015 if ( xCompFactory.is() || xFactory.is() )
1019 uno::Reference< linguistic2::XProofreader > xSvc(
1020 xCompFactory.is()
1021 ? xCompFactory->createInstanceWithContext(xContext)
1022 : xFactory->createInstance(),
1023 uno::UNO_QUERY_THROW);
1025 OUString aImplName;
1026 std::vector< LanguageType > aLanguages;
1027 uno::Reference< XServiceInfo > xInfo( xSvc, uno::UNO_QUERY );
1028 if (xInfo.is())
1029 aImplName = xInfo->getImplementationName();
1030 SAL_WARN_IF( aImplName.isEmpty(), "linguistic", "empty implementation name" );
1031 uno::Sequence<lang::Locale> aLocaleSequence(xSvc->getLocales());
1032 aLanguages = LocaleSeqToLangVec( aLocaleSequence );
1034 pAvailGrammarSvcs->push_back( SvcInfo( aImplName, std::move(aLanguages) ) );
1036 catch (const uno::Exception &)
1038 SAL_WARN( "linguistic", "createInstance failed" );
1046 void LngSvcMgr::GetAvailableHyphSvcs_Impl()
1048 if (pAvailHyphSvcs)
1049 return;
1051 pAvailHyphSvcs.emplace();
1052 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
1054 uno::Reference< container::XContentEnumerationAccess > xEnumAccess( xContext->getServiceManager(), uno::UNO_QUERY );
1055 uno::Reference< container::XEnumeration > xEnum;
1056 if (xEnumAccess.is())
1057 xEnum = xEnumAccess->createContentEnumeration( SN_HYPHENATOR );
1059 if (!xEnum.is())
1060 return;
1062 while (xEnum->hasMoreElements())
1064 uno::Any aCurrent = xEnum->nextElement();
1065 uno::Reference< lang::XSingleComponentFactory > xCompFactory;
1066 uno::Reference< lang::XSingleServiceFactory > xFactory;
1068 xCompFactory.set(aCurrent, css::uno::UNO_QUERY);
1069 if (!xCompFactory.is())
1071 xFactory.set(aCurrent, css::uno::UNO_QUERY);
1073 if ( xCompFactory.is() || xFactory.is() )
1077 uno::Reference< linguistic2::XHyphenator > xSvc( ( xCompFactory.is() ? xCompFactory->createInstanceWithContext( xContext ) : xFactory->createInstance() ), uno::UNO_QUERY_THROW );
1078 OUString aImplName;
1079 std::vector< LanguageType > aLanguages;
1080 uno::Reference< XServiceInfo > xInfo( xSvc, uno::UNO_QUERY );
1081 if (xInfo.is())
1082 aImplName = xInfo->getImplementationName();
1083 SAL_WARN_IF( aImplName.isEmpty(), "linguistic", "empty implementation name" );
1084 uno::Sequence<lang::Locale> aLocaleSequence(xSvc->getLocales());
1085 aLanguages = LocaleSeqToLangVec( aLocaleSequence );
1086 pAvailHyphSvcs->push_back( SvcInfo( aImplName, std::move(aLanguages) ) );
1088 catch (const uno::Exception &)
1090 SAL_WARN( "linguistic", "createInstance failed" );
1097 void LngSvcMgr::GetAvailableThesSvcs_Impl()
1099 if (pAvailThesSvcs)
1100 return;
1102 pAvailThesSvcs.emplace();
1104 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
1106 uno::Reference< container::XContentEnumerationAccess > xEnumAccess( xContext->getServiceManager(), uno::UNO_QUERY );
1107 uno::Reference< container::XEnumeration > xEnum;
1108 if (xEnumAccess.is())
1109 xEnum = xEnumAccess->createContentEnumeration( SN_THESAURUS );
1111 if (!xEnum.is())
1112 return;
1114 while (xEnum->hasMoreElements())
1116 uno::Any aCurrent = xEnum->nextElement();
1117 uno::Reference< lang::XSingleComponentFactory > xCompFactory;
1118 uno::Reference< lang::XSingleServiceFactory > xFactory;
1120 xCompFactory.set(aCurrent, css::uno::UNO_QUERY);
1121 if (!xCompFactory.is())
1123 xFactory.set(aCurrent, css::uno::UNO_QUERY);
1125 if ( xCompFactory.is() || xFactory.is() )
1129 uno::Reference< linguistic2::XThesaurus > xSvc( ( xCompFactory.is() ? xCompFactory->createInstanceWithContext( xContext ) : xFactory->createInstance() ), uno::UNO_QUERY_THROW );
1131 OUString aImplName;
1132 std::vector< LanguageType > aLanguages;
1133 uno::Reference< XServiceInfo > xInfo( xSvc, uno::UNO_QUERY );
1134 if (xInfo.is())
1135 aImplName = xInfo->getImplementationName();
1136 SAL_WARN_IF( aImplName.isEmpty(), "linguistic", "empty implementation name" );
1137 uno::Sequence<lang::Locale> aLocaleSequence(xSvc->getLocales());
1138 aLanguages = LocaleSeqToLangVec( aLocaleSequence );
1140 pAvailThesSvcs->push_back( SvcInfo( aImplName, std::move(aLanguages) ) );
1142 catch (const uno::Exception &)
1144 SAL_WARN( "linguistic", "createInstance failed" );
1151 void LngSvcMgr::SetCfgServiceLists( SpellCheckerDispatcher &rSpellDsp )
1153 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SetCfgServiceLists - Spell" );
1155 OUString aNode("ServiceManager/SpellCheckerList");
1156 uno::Sequence< OUString > aNames( /*aCfg.*/GetNodeNames( aNode ) );
1158 // append path prefix need for 'GetProperties' call below
1159 OUString aPrefix = aNode + "/";
1160 for (OUString & name : asNonConstRange(aNames))
1162 name = aPrefix + name;
1165 const uno::Sequence< uno::Any > aValues( /*aCfg.*/GetProperties( aNames ) );
1166 if (!(aNames.hasElements() && aNames.getLength() == aValues.getLength()))
1167 return;
1169 const OUString *pNames = aNames.getConstArray();
1170 for (const uno::Any& rValue : aValues)
1172 uno::Sequence< OUString > aSvcImplNames;
1173 if (rValue >>= aSvcImplNames)
1175 OUString aLocaleStr( *pNames++ );
1176 sal_Int32 nSeparatorPos = aLocaleStr.lastIndexOf( '/' );
1177 aLocaleStr = aLocaleStr.copy( nSeparatorPos + 1 );
1178 rSpellDsp.SetServiceList( LanguageTag::convertToLocale(aLocaleStr), aSvcImplNames );
1184 void LngSvcMgr::SetCfgServiceLists( GrammarCheckingIterator &rGrammarDsp )
1186 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SetCfgServiceLists - Grammar" );
1188 OUString aNode("ServiceManager/GrammarCheckerList");
1189 uno::Sequence< OUString > aNames( /*aCfg.*/GetNodeNames( aNode ) );
1191 // append path prefix need for 'GetProperties' call below
1192 OUString aPrefix = aNode + "/";
1193 for (OUString & name : asNonConstRange(aNames))
1195 name = aPrefix + name;
1198 const uno::Sequence< uno::Any > aValues( /*aCfg.*/GetProperties( aNames ) );
1199 if (!(aNames.hasElements() && aNames.getLength() == aValues.getLength()))
1200 return;
1202 const OUString *pNames = aNames.getConstArray();
1203 for (const uno::Any& rValue : aValues)
1205 uno::Sequence< OUString > aSvcImplNames;
1206 if (rValue >>= aSvcImplNames)
1208 // there should only be one grammar checker in use per language...
1209 if (aSvcImplNames.getLength() > 1)
1210 aSvcImplNames.realloc(1);
1212 OUString aLocaleStr( *pNames++ );
1213 sal_Int32 nSeparatorPos = aLocaleStr.lastIndexOf( '/' );
1214 aLocaleStr = aLocaleStr.copy( nSeparatorPos + 1 );
1215 rGrammarDsp.SetServiceList( LanguageTag::convertToLocale(aLocaleStr), aSvcImplNames );
1221 void LngSvcMgr::SetCfgServiceLists( HyphenatorDispatcher &rHyphDsp )
1223 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SetCfgServiceLists - Hyph" );
1225 OUString aNode("ServiceManager/HyphenatorList");
1226 uno::Sequence< OUString > aNames( /*aCfg.*/GetNodeNames( aNode ) );
1228 // append path prefix need for 'GetProperties' call below
1229 OUString aPrefix = aNode + "/";
1230 for (OUString & name : asNonConstRange(aNames))
1232 name = aPrefix + name;
1235 const uno::Sequence< uno::Any > aValues( /*aCfg.*/GetProperties( aNames ) );
1236 if (!(aNames.hasElements() && aNames.getLength() == aValues.getLength()))
1237 return;
1239 const OUString *pNames = aNames.getConstArray();
1240 for (const uno::Any& rValue : aValues)
1242 uno::Sequence< OUString > aSvcImplNames;
1243 if (rValue >>= aSvcImplNames)
1245 // there should only be one hyphenator in use per language...
1246 if (aSvcImplNames.getLength() > 1)
1247 aSvcImplNames.realloc(1);
1249 OUString aLocaleStr( *pNames++ );
1250 sal_Int32 nSeparatorPos = aLocaleStr.lastIndexOf( '/' );
1251 aLocaleStr = aLocaleStr.copy( nSeparatorPos + 1 );
1252 rHyphDsp.SetServiceList( LanguageTag::convertToLocale(aLocaleStr), aSvcImplNames );
1258 void LngSvcMgr::SetCfgServiceLists( ThesaurusDispatcher &rThesDsp )
1260 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SetCfgServiceLists - Thes" );
1262 OUString aNode("ServiceManager/ThesaurusList");
1263 uno::Sequence< OUString > aNames( /*aCfg.*/GetNodeNames( aNode ) );
1265 // append path prefix need for 'GetProperties' call below
1266 OUString aPrefix = aNode + "/";
1267 for (OUString & name : asNonConstRange(aNames))
1269 name = aPrefix + name;
1272 const uno::Sequence< uno::Any > aValues( /*aCfg.*/GetProperties( aNames ) );
1273 if (!(aNames.hasElements() && aNames.getLength() == aValues.getLength()))
1274 return;
1276 const OUString *pNames = aNames.getConstArray();
1277 for (const uno::Any& rValue : aValues)
1279 uno::Sequence< OUString > aSvcImplNames;
1280 if (rValue >>= aSvcImplNames)
1282 OUString aLocaleStr( *pNames++ );
1283 sal_Int32 nSeparatorPos = aLocaleStr.lastIndexOf( '/' );
1284 aLocaleStr = aLocaleStr.copy( nSeparatorPos + 1 );
1285 rThesDsp.SetServiceList( LanguageTag::convertToLocale(aLocaleStr), aSvcImplNames );
1291 uno::Reference< linguistic2::XSpellChecker > SAL_CALL
1292 LngSvcMgr::getSpellChecker()
1294 osl::MutexGuard aGuard( GetLinguMutex() );
1295 #if OSL_DEBUG_LEVEL > 0
1296 getAvailableLocales(SN_SPELLCHECKER);
1297 #endif
1299 uno::Reference< linguistic2::XSpellChecker > xRes;
1300 if (!bDisposing)
1302 if (!mxSpellDsp.is())
1303 GetSpellCheckerDsp_Impl();
1304 xRes = mxSpellDsp.get();
1306 return xRes;
1310 uno::Reference< linguistic2::XHyphenator > SAL_CALL
1311 LngSvcMgr::getHyphenator()
1313 osl::MutexGuard aGuard( GetLinguMutex() );
1314 #if OSL_DEBUG_LEVEL > 0
1315 getAvailableLocales(SN_HYPHENATOR);
1316 #endif
1317 uno::Reference< linguistic2::XHyphenator > xRes;
1318 if (!bDisposing)
1320 if (!mxHyphDsp.is())
1321 GetHyphenatorDsp_Impl();
1322 xRes = mxHyphDsp.get();
1324 return xRes;
1328 uno::Reference< linguistic2::XThesaurus > SAL_CALL
1329 LngSvcMgr::getThesaurus()
1331 osl::MutexGuard aGuard( GetLinguMutex() );
1332 #if OSL_DEBUG_LEVEL > 0
1333 getAvailableLocales(SN_THESAURUS);
1334 #endif
1335 uno::Reference< linguistic2::XThesaurus > xRes;
1336 if (!bDisposing)
1338 if (!mxThesDsp.is())
1339 GetThesaurusDsp_Impl();
1340 xRes = mxThesDsp.get();
1342 return xRes;
1346 sal_Bool SAL_CALL
1347 LngSvcMgr::addLinguServiceManagerListener(
1348 const uno::Reference< lang::XEventListener >& xListener )
1350 osl::MutexGuard aGuard( GetLinguMutex() );
1352 if (bDisposing || !xListener.is())
1353 return false;
1355 if (!mxListenerHelper.is())
1356 GetListenerHelper_Impl();
1357 mxListenerHelper->AddLngSvcMgrListener( xListener );
1358 return true;
1362 sal_Bool SAL_CALL
1363 LngSvcMgr::removeLinguServiceManagerListener(
1364 const uno::Reference< lang::XEventListener >& xListener )
1366 osl::MutexGuard aGuard( GetLinguMutex() );
1368 if (bDisposing || !xListener.is())
1369 return false;
1371 DBG_ASSERT( mxListenerHelper.is(), "listener removed without being added" );
1372 if (!mxListenerHelper.is())
1373 GetListenerHelper_Impl();
1374 mxListenerHelper->RemoveLngSvcMgrListener( xListener );
1375 return true;
1379 uno::Sequence< OUString > SAL_CALL
1380 LngSvcMgr::getAvailableServices(
1381 const OUString& rServiceName,
1382 const lang::Locale& rLocale )
1384 osl::MutexGuard aGuard( GetLinguMutex() );
1386 uno::Sequence< OUString > aRes;
1387 const SvcInfoArray *pInfoArray = nullptr;
1389 if (rServiceName == SN_SPELLCHECKER)
1391 GetAvailableSpellSvcs_Impl();
1392 pInfoArray = &*pAvailSpellSvcs;
1394 else if (rServiceName == SN_GRAMMARCHECKER)
1396 GetAvailableGrammarSvcs_Impl();
1397 pInfoArray = &*pAvailGrammarSvcs;
1399 else if (rServiceName == SN_HYPHENATOR)
1401 GetAvailableHyphSvcs_Impl();
1402 pInfoArray = &*pAvailHyphSvcs;
1404 else if (rServiceName == SN_THESAURUS)
1406 GetAvailableThesSvcs_Impl();
1407 pInfoArray = &*pAvailThesSvcs;
1410 if (pInfoArray)
1412 std::vector<OUString> aVec;
1413 aVec.reserve(pInfoArray->size());
1415 LanguageType nLanguage = LinguLocaleToLanguage( rLocale );
1416 for (const auto& rInfo : *pInfoArray)
1418 if (LinguIsUnspecified( nLanguage )
1419 || rInfo.HasLanguage( nLanguage ))
1421 aVec.push_back(rInfo.aSvcImplName);
1425 aRes = comphelper::containerToSequence(aVec);
1428 return aRes;
1432 uno::Sequence< lang::Locale > SAL_CALL
1433 LngSvcMgr::getAvailableLocales(
1434 const OUString& rServiceName )
1436 osl::MutexGuard aGuard( GetLinguMutex() );
1438 uno::Sequence< lang::Locale > aRes;
1440 uno::Sequence< lang::Locale > *pAvailLocales = nullptr;
1441 if (rServiceName == SN_SPELLCHECKER)
1442 pAvailLocales = &aAvailSpellLocales;
1443 else if (rServiceName == SN_GRAMMARCHECKER)
1444 pAvailLocales = &aAvailGrammarLocales;
1445 else if (rServiceName == SN_HYPHENATOR)
1446 pAvailLocales = &aAvailHyphLocales;
1447 else if (rServiceName == SN_THESAURUS)
1448 pAvailLocales = &aAvailThesLocales;
1450 // Nowadays (with OOo lingu in SO) we want to know immediately about
1451 // new downloaded dictionaries and have them ready right away if the Tools/Options...
1452 // is used to activate them. Thus we can not rely anymore on buffered data.
1453 if (pAvailLocales)
1455 *pAvailLocales = GetAvailLocales(getAvailableServices(rServiceName, lang::Locale()));
1456 aRes = *pAvailLocales;
1459 return aRes;
1462 static bool IsEqSvcList( const uno::Sequence< OUString > &rList1,
1463 const uno::Sequence< OUString > &rList2 )
1465 // returns true if both sequences are equal
1466 return rList1.getLength() == rList2.getLength()
1467 && std::equal(rList1.begin(), rList1.end(), rList2.begin(), rList2.end());
1471 void SAL_CALL
1472 LngSvcMgr::setConfiguredServices(
1473 const OUString& rServiceName,
1474 const lang::Locale& rLocale,
1475 const uno::Sequence< OUString >& rServiceImplNames )
1477 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::setConfiguredServices" );
1479 osl::MutexGuard aGuard( GetLinguMutex() );
1481 LanguageType nLanguage = LinguLocaleToLanguage( rLocale );
1482 if (LinguIsUnspecified( nLanguage))
1483 return;
1485 if (rServiceName == SN_SPELLCHECKER)
1487 if (!mxSpellDsp.is())
1488 GetSpellCheckerDsp_Impl();
1489 bool bChanged = !IsEqSvcList( rServiceImplNames,
1490 mxSpellDsp->GetServiceList( rLocale ) );
1491 if (bChanged)
1493 mxSpellDsp->SetServiceList( rLocale, rServiceImplNames );
1494 SaveCfgSvcs( SN_SPELLCHECKER );
1496 if (mxListenerHelper)
1497 mxListenerHelper->AddLngSvcEvt(
1498 linguistic2::LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN |
1499 linguistic2::LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN );
1502 else if (rServiceName == SN_GRAMMARCHECKER)
1504 if (!mxGrammarDsp.is())
1505 GetGrammarCheckerDsp_Impl();
1506 if (!mxGrammarDsp) // e.g., when !SvtLinguConfig().HasGrammarChecker()
1507 return;
1508 bool bChanged = !IsEqSvcList( rServiceImplNames,
1509 mxGrammarDsp->GetServiceList( rLocale ) );
1510 if (bChanged)
1512 mxGrammarDsp->SetServiceList( rLocale, rServiceImplNames );
1513 SaveCfgSvcs( SN_GRAMMARCHECKER );
1515 if (mxListenerHelper)
1516 mxListenerHelper->AddLngSvcEvt(
1517 linguistic2::LinguServiceEventFlags::PROOFREAD_AGAIN );
1520 else if (rServiceName == SN_HYPHENATOR)
1522 if (!mxHyphDsp.is())
1523 GetHyphenatorDsp_Impl();
1524 bool bChanged = !IsEqSvcList( rServiceImplNames,
1525 mxHyphDsp->GetServiceList( rLocale ) );
1526 if (bChanged)
1528 mxHyphDsp->SetServiceList( rLocale, rServiceImplNames );
1529 SaveCfgSvcs( SN_HYPHENATOR );
1531 if (mxListenerHelper)
1532 mxListenerHelper->AddLngSvcEvt(
1533 linguistic2::LinguServiceEventFlags::HYPHENATE_AGAIN );
1536 else if (rServiceName == SN_THESAURUS)
1538 if (!mxThesDsp.is())
1539 GetThesaurusDsp_Impl();
1540 bool bChanged = !IsEqSvcList( rServiceImplNames,
1541 mxThesDsp->GetServiceList( rLocale ) );
1542 if (bChanged)
1544 mxThesDsp->SetServiceList( rLocale, rServiceImplNames );
1545 SaveCfgSvcs( SN_THESAURUS );
1551 bool LngSvcMgr::SaveCfgSvcs( std::u16string_view rServiceName )
1553 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SaveCfgSvcs" );
1555 bool bRes = false;
1557 LinguDispatcher *pDsp = nullptr;
1558 uno::Sequence< lang::Locale > aLocales;
1560 if (rServiceName == SN_SPELLCHECKER)
1562 if (!mxSpellDsp)
1563 GetSpellCheckerDsp_Impl();
1564 pDsp = mxSpellDsp.get();
1565 aLocales = getAvailableLocales( SN_SPELLCHECKER );
1567 else if (rServiceName == SN_GRAMMARCHECKER)
1569 if (!mxGrammarDsp.is())
1570 GetGrammarCheckerDsp_Impl();
1571 pDsp = mxGrammarDsp.get();
1572 aLocales = getAvailableLocales( SN_GRAMMARCHECKER );
1574 else if (rServiceName == SN_HYPHENATOR)
1576 if (!mxHyphDsp.is())
1577 GetHyphenatorDsp_Impl();
1578 pDsp = mxHyphDsp.get();
1579 aLocales = getAvailableLocales( SN_HYPHENATOR );
1581 else if (rServiceName == SN_THESAURUS)
1583 if (!mxThesDsp.is())
1584 GetThesaurusDsp_Impl();
1585 pDsp = mxThesDsp.get();
1586 aLocales = getAvailableLocales( SN_THESAURUS );
1589 if (pDsp && aLocales.hasElements())
1591 uno::Sequence< beans::PropertyValue > aValues( aLocales.getLength() );
1592 beans::PropertyValue *pValue = aValues.getArray();
1594 // get node name to be used
1595 const char *pNodeName = nullptr;
1596 if (pDsp == mxSpellDsp.get())
1597 pNodeName = "ServiceManager/SpellCheckerList";
1598 else if (pDsp == mxGrammarDsp.get())
1599 pNodeName = "ServiceManager/GrammarCheckerList";
1600 else if (pDsp == mxHyphDsp.get())
1601 pNodeName = "ServiceManager/HyphenatorList";
1602 else if (pDsp == mxThesDsp.get())
1603 pNodeName = "ServiceManager/ThesaurusList";
1604 else
1606 SAL_WARN( "linguistic", "node name missing" );
1608 OUString aNodeName( OUString::createFromAscii(pNodeName) );
1610 for (const lang::Locale& rLocale : aLocales)
1612 uno::Sequence< OUString > aSvcImplNames = pDsp->GetServiceList( rLocale );
1614 // build value to be written back to configuration
1615 uno::Any aCfgAny;
1616 if ((pDsp == mxHyphDsp.get() || pDsp == mxGrammarDsp.get()) && aSvcImplNames.getLength() > 1)
1617 aSvcImplNames.realloc(1); // there should be only one entry for hyphenators or grammar checkers (because they are not chained)
1618 aCfgAny <<= aSvcImplNames;
1619 DBG_ASSERT( aCfgAny.hasValue(), "missing value for 'Any' type" );
1621 OUString aCfgLocaleStr( LanguageTag::convertToBcp47( rLocale));
1622 pValue->Value = aCfgAny;
1623 pValue->Name = aNodeName + "/" + aCfgLocaleStr;
1624 pValue++;
1627 SAL_INFO( "linguistic", "linguistic: LngSvcMgr::SaveCfgSvcs - ReplaceSetProperties" );
1628 // change, add new or replace existing entries.
1629 bRes |= /*aCfg.*/ReplaceSetProperties( aNodeName, aValues );
1633 return bRes;
1637 static uno::Sequence< OUString > GetLangSvcList( const uno::Any &rVal )
1639 uno::Sequence< OUString > aRes;
1641 if (rVal.hasValue())
1643 rVal >>= aRes;
1644 #if OSL_DEBUG_LEVEL > 0
1645 for (const OUString& rSvcName : aRes)
1647 SAL_WARN_IF( rSvcName.isEmpty(), "linguistic", "service impl-name missing" );
1649 #endif
1652 return aRes;
1656 static uno::Sequence< OUString > GetLangSvc( const uno::Any &rVal )
1658 uno::Sequence< OUString > aRes;
1659 if (!rVal.hasValue())
1660 return aRes;
1662 // allowing for a sequence here as well (even though it should only
1663 // be a string) makes coding easier in other places since one needs
1664 // not make a special case for writing a string only and not a
1665 // sequence of strings.
1666 if (rVal >>= aRes)
1668 // but only the first string should be used.
1669 if (aRes.getLength() > 1)
1670 aRes.realloc(1);
1672 else
1674 OUString aImplName;
1675 if ((rVal >>= aImplName) && !aImplName.isEmpty())
1677 aRes.realloc(1);
1678 aRes.getArray()[0] = aImplName;
1680 else
1682 SAL_WARN( "linguistic", "GetLangSvc: unexpected type encountered" );
1686 return aRes;
1690 uno::Sequence< OUString > SAL_CALL
1691 LngSvcMgr::getConfiguredServices(
1692 const OUString& rServiceName,
1693 const lang::Locale& rLocale )
1695 osl::MutexGuard aGuard( GetLinguMutex() );
1697 uno::Sequence< OUString > aSvcImplNames;
1699 OUString aCfgLocale( LanguageTag::convertToBcp47( rLocale) );
1701 uno::Sequence< uno::Any > aValues;
1702 uno::Sequence< OUString > aNames( 1 );
1703 OUString *pNames = aNames.getArray();
1704 if ( rServiceName == SN_SPELLCHECKER )
1706 OUString aNode( "ServiceManager/SpellCheckerList");
1707 const uno::Sequence< OUString > aNodeEntries( GetNodeNames( aNode ) );
1708 if (lcl_SeqHasString( aNodeEntries, aCfgLocale ))
1710 pNames[0] = aNode + "/" + aCfgLocale;
1711 aValues = /*aCfg.*/GetProperties( aNames );
1712 if (aValues.hasElements())
1713 aSvcImplNames = GetLangSvcList( aValues.getConstArray()[0] );
1716 else if ( rServiceName == SN_GRAMMARCHECKER )
1718 OUString aNode( "ServiceManager/GrammarCheckerList");
1719 const uno::Sequence< OUString > aNodeEntries( GetNodeNames( aNode ) );
1720 if (lcl_SeqHasString( aNodeEntries, aCfgLocale ))
1722 pNames[0] = aNode + "/" + aCfgLocale;
1723 aValues = /*aCfg.*/GetProperties( aNames );
1724 if (aValues.hasElements())
1725 aSvcImplNames = GetLangSvc( aValues.getConstArray()[0] );
1728 else if ( rServiceName == SN_HYPHENATOR )
1730 OUString aNode( "ServiceManager/HyphenatorList");
1731 const uno::Sequence< OUString > aNodeEntries( GetNodeNames( aNode ) );
1732 if (lcl_SeqHasString( aNodeEntries, aCfgLocale ))
1734 pNames[0] = aNode + "/" + aCfgLocale;
1735 aValues = /*aCfg.*/GetProperties( aNames );
1736 if (aValues.hasElements())
1737 aSvcImplNames = GetLangSvc( aValues.getConstArray()[0] );
1740 else if ( rServiceName == SN_THESAURUS )
1742 OUString aNode( "ServiceManager/ThesaurusList");
1743 const uno::Sequence< OUString > aNodeEntries( GetNodeNames( aNode ) );
1744 if (lcl_SeqHasString( aNodeEntries, aCfgLocale ))
1746 pNames[0] = aNode + "/" + aCfgLocale;
1747 aValues = /*aCfg.*/GetProperties( aNames );
1748 if (aValues.hasElements())
1749 aSvcImplNames = GetLangSvcList( aValues.getConstArray()[0] );
1753 return aSvcImplNames;
1757 void SAL_CALL
1758 LngSvcMgr::dispose()
1760 osl::MutexGuard aGuard( GetLinguMutex() );
1762 if (!bDisposing)
1764 bDisposing = true;
1766 // require listeners to release this object
1767 lang::EventObject aEvtObj( static_cast<XLinguServiceManager*>(this) );
1768 aEvtListeners.disposeAndClear( aEvtObj );
1770 if (mxListenerHelper.is())
1771 mxListenerHelper->DisposeAndClear( aEvtObj );
1776 void SAL_CALL
1777 LngSvcMgr::addEventListener(
1778 const uno::Reference< lang::XEventListener >& xListener )
1780 osl::MutexGuard aGuard( GetLinguMutex() );
1782 if (!bDisposing && xListener.is())
1784 aEvtListeners.addInterface( xListener );
1789 void SAL_CALL
1790 LngSvcMgr::removeEventListener(
1791 const uno::Reference< lang::XEventListener >& xListener )
1793 osl::MutexGuard aGuard( GetLinguMutex() );
1795 if (xListener.is())
1797 aEvtListeners.removeInterface( xListener );
1802 bool LngSvcMgr::AddLngSvcEvtBroadcaster(
1803 const uno::Reference< linguistic2::XLinguServiceEventBroadcaster > &rxBroadcaster )
1805 if (!rxBroadcaster.is())
1806 return false;
1807 if (!mxListenerHelper.is())
1808 GetListenerHelper_Impl();
1809 mxListenerHelper->AddLngSvcEvtBroadcaster( rxBroadcaster );
1810 return true;
1814 OUString SAL_CALL
1815 LngSvcMgr::getImplementationName()
1817 return "com.sun.star.lingu2.LngSvcMgr";
1821 sal_Bool SAL_CALL
1822 LngSvcMgr::supportsService( const OUString& ServiceName )
1824 return cppu::supportsService(this, ServiceName);
1828 uno::Sequence< OUString > SAL_CALL
1829 LngSvcMgr::getSupportedServiceNames()
1831 return { "com.sun.star.linguistic2.LinguServiceManager" };
1834 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
1835 linguistic_LngSvcMgr_get_implementation(
1836 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
1838 return cppu::acquire(new LngSvcMgr());
1842 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */