1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <editeng/unolingu.hxx>
23 #include <com/sun/star/frame/Desktop.hpp>
24 #include <com/sun/star/frame/XStorable.hpp>
25 #include <com/sun/star/lang/XEventListener.hpp>
26 #include <com/sun/star/linguistic2/XHyphenatedWord.hpp>
27 #include <com/sun/star/linguistic2/DictionaryList.hpp>
28 #include <com/sun/star/linguistic2/LinguServiceManager.hpp>
29 #include <com/sun/star/linguistic2/LinguProperties.hpp>
30 #include <com/sun/star/linguistic2/XSpellChecker1.hpp>
32 #include <comphelper/processfactory.hxx>
33 #include <cppuhelper/implbase.hxx>
34 #include <i18nlangtag/languagetag.hxx>
35 #include <unotools/lingucfg.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/weld.hxx>
38 #include <linguistic/misc.hxx>
39 #include <editeng/eerdll.hxx>
40 #include <editeng/editrids.hrc>
41 #include <svtools/strings.hrc>
42 #include <unotools/resmgr.hxx>
43 #include <sal/log.hxx>
44 #include <osl/diagnose.h>
46 using namespace ::comphelper
;
47 using namespace ::linguistic
;
48 using namespace ::com::sun::star
;
49 using namespace ::com::sun::star::util
;
50 using namespace ::com::sun::star::uno
;
51 using namespace ::com::sun::star::lang
;
52 using namespace ::com::sun::star::beans
;
53 using namespace ::com::sun::star::frame
;
54 using namespace ::com::sun::star::linguistic2
;
56 static uno::Reference
< XLinguServiceManager2
> GetLngSvcMgr_Impl()
58 uno::Reference
< XComponentContext
> xContext
= comphelper::getProcessComponentContext();
59 uno::Reference
< XLinguServiceManager2
> xRes
= LinguServiceManager::create(xContext
);
65 //! Dummy implementation in order to avoid loading of lingu DLL
66 //! when only the XSupportedLocales interface is used.
67 //! The dummy accesses the real implementation (and thus loading the DLL)
68 //! when "real" work needs to be done only.
69 class ThesDummy_Impl
:
70 public cppu::WeakImplHelper
< XThesaurus
>
72 uno::Reference
< XThesaurus
> xThes
; // the real one...
73 std::unique_ptr
<Sequence
< lang::Locale
>> pLocaleSeq
;
83 virtual css::uno::Sequence
< css::lang::Locale
> SAL_CALL
84 getLocales() override
;
85 virtual sal_Bool SAL_CALL
86 hasLocale( const css::lang::Locale
& rLocale
) override
;
89 virtual css::uno::Sequence
<
90 css::uno::Reference
< css::linguistic2::XMeaning
> > SAL_CALL
91 queryMeanings( const OUString
& rTerm
,
92 const css::lang::Locale
& rLocale
,
93 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
) override
;
98 void ThesDummy_Impl::GetCfgLocales()
104 Sequence
< OUString
> aNodeNames( aCfg
.GetNodeNames( "ServiceManager/ThesaurusList" ) );
105 const OUString
*pNodeNames
= aNodeNames
.getConstArray();
106 sal_Int32 nLen
= aNodeNames
.getLength();
107 pLocaleSeq
.reset( new Sequence
< lang::Locale
>( nLen
) );
108 lang::Locale
*pLocale
= pLocaleSeq
->getArray();
109 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
111 pLocale
[i
] = LanguageTag::convertToLocaleWithFallback( pNodeNames
[i
] );
116 void ThesDummy_Impl::GetThes_Impl()
120 uno::Reference
< XLinguServiceManager2
> xLngSvcMgr( GetLngSvcMgr_Impl() );
121 xThes
= xLngSvcMgr
->getThesaurus();
125 // no longer needed...
132 uno::Sequence
< lang::Locale
> SAL_CALL
133 ThesDummy_Impl::getLocales()
137 return xThes
->getLocales();
138 else if (!pLocaleSeq
) // if not already loaded save startup time by avoiding loading them now
145 ThesDummy_Impl::hasLocale( const lang::Locale
& rLocale
)
149 return xThes
->hasLocale( rLocale
);
150 else if (!pLocaleSeq
) // if not already loaded save startup time by avoiding loading them now
153 sal_Int32 nLen
= pLocaleSeq
->getLength();
154 const lang::Locale
*pLocale
= pLocaleSeq
->getConstArray();
155 const lang::Locale
*pEnd
= pLocale
+ nLen
;
156 for ( ; pLocale
< pEnd
&& !bFound
; ++pLocale
)
158 bFound
= pLocale
->Language
== rLocale
.Language
&&
159 pLocale
->Country
== rLocale
.Country
&&
160 pLocale
->Variant
== rLocale
.Variant
;
166 uno::Sequence
< uno::Reference
< linguistic2::XMeaning
> > SAL_CALL
167 ThesDummy_Impl::queryMeanings(
168 const OUString
& rTerm
,
169 const lang::Locale
& rLocale
,
170 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
)
173 uno::Sequence
< uno::Reference
< linguistic2::XMeaning
> > aRes
;
174 OSL_ENSURE( xThes
.is(), "Thesaurus missing" );
176 aRes
= xThes
->queryMeanings( rTerm
, rLocale
, rProperties
);
182 //! Dummy implementation in order to avoid loading of lingu DLL.
183 //! The dummy accesses the real implementation (and thus loading the DLL)
184 //! when it needs to be done only.
185 class SpellDummy_Impl
:
186 public cppu::WeakImplHelper
< XSpellChecker1
>
188 uno::Reference
< XSpellChecker1
> xSpell
; // the real one...
190 void GetSpell_Impl();
194 // XSupportedLanguages (for XSpellChecker1)
195 virtual css::uno::Sequence
< sal_Int16
> SAL_CALL
196 getLanguages() override
;
197 virtual sal_Bool SAL_CALL
198 hasLanguage( sal_Int16 nLanguage
) override
;
200 // XSpellChecker1 (same as XSpellChecker but sal_Int16 for language)
201 virtual sal_Bool SAL_CALL
202 isValid( const OUString
& rWord
, sal_Int16 nLanguage
,
203 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
) override
;
204 virtual css::uno::Reference
< css::linguistic2::XSpellAlternatives
> SAL_CALL
205 spell( const OUString
& rWord
, sal_Int16 nLanguage
,
206 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
) override
;
211 void SpellDummy_Impl::GetSpell_Impl()
215 uno::Reference
< XLinguServiceManager2
> xLngSvcMgr( GetLngSvcMgr_Impl() );
216 xSpell
.set( xLngSvcMgr
->getSpellChecker(), UNO_QUERY
);
221 uno::Sequence
< sal_Int16
> SAL_CALL
222 SpellDummy_Impl::getLanguages()
226 return xSpell
->getLanguages();
228 return uno::Sequence
< sal_Int16
>();
233 SpellDummy_Impl::hasLanguage( sal_Int16 nLanguage
)
238 bRes
= xSpell
->hasLanguage( nLanguage
);
244 SpellDummy_Impl::isValid( const OUString
& rWord
, sal_Int16 nLanguage
,
245 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
)
250 bRes
= xSpell
->isValid( rWord
, nLanguage
, rProperties
);
255 uno::Reference
< linguistic2::XSpellAlternatives
> SAL_CALL
256 SpellDummy_Impl::spell( const OUString
& rWord
, sal_Int16 nLanguage
,
257 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
)
260 uno::Reference
< linguistic2::XSpellAlternatives
> xRes
;
262 xRes
= xSpell
->spell( rWord
, nLanguage
, rProperties
);
268 //! Dummy implementation in order to avoid loading of lingu DLL.
269 //! The dummy accesses the real implementation (and thus loading the DLL)
270 //! when it needs to be done only.
271 class HyphDummy_Impl
:
272 public cppu::WeakImplHelper
< XHyphenator
>
274 uno::Reference
< XHyphenator
> xHyph
; // the real one...
281 virtual css::uno::Sequence
<
282 css::lang::Locale
> SAL_CALL
283 getLocales() override
;
284 virtual sal_Bool SAL_CALL
285 hasLocale( const css::lang::Locale
& rLocale
) override
;
288 virtual css::uno::Reference
<
289 css::linguistic2::XHyphenatedWord
> SAL_CALL
290 hyphenate( const OUString
& rWord
,
291 const css::lang::Locale
& rLocale
,
292 sal_Int16 nMaxLeading
,
293 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
) override
;
294 virtual css::uno::Reference
<
295 css::linguistic2::XHyphenatedWord
> SAL_CALL
296 queryAlternativeSpelling( const OUString
& rWord
,
297 const css::lang::Locale
& rLocale
,
299 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
) override
;
300 virtual css::uno::Reference
<
301 css::linguistic2::XPossibleHyphens
> SAL_CALL
302 createPossibleHyphens(
303 const OUString
& rWord
,
304 const css::lang::Locale
& rLocale
,
305 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
) override
;
310 void HyphDummy_Impl::GetHyph_Impl()
314 uno::Reference
< XLinguServiceManager2
> xLngSvcMgr( GetLngSvcMgr_Impl() );
315 xHyph
= xLngSvcMgr
->getHyphenator();
320 uno::Sequence
< lang::Locale
> SAL_CALL
321 HyphDummy_Impl::getLocales()
325 return xHyph
->getLocales();
327 return uno::Sequence
< lang::Locale
>();
332 HyphDummy_Impl::hasLocale( const lang::Locale
& rLocale
)
337 bRes
= xHyph
->hasLocale( rLocale
);
342 uno::Reference
< linguistic2::XHyphenatedWord
> SAL_CALL
343 HyphDummy_Impl::hyphenate(
344 const OUString
& rWord
,
345 const lang::Locale
& rLocale
,
346 sal_Int16 nMaxLeading
,
347 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
)
350 uno::Reference
< linguistic2::XHyphenatedWord
> xRes
;
352 xRes
= xHyph
->hyphenate( rWord
, rLocale
, nMaxLeading
, rProperties
);
357 uno::Reference
< linguistic2::XHyphenatedWord
> SAL_CALL
358 HyphDummy_Impl::queryAlternativeSpelling(
359 const OUString
& rWord
,
360 const lang::Locale
& rLocale
,
362 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
)
365 uno::Reference
< linguistic2::XHyphenatedWord
> xRes
;
367 xRes
= xHyph
->queryAlternativeSpelling( rWord
, rLocale
, nIndex
, rProperties
);
372 uno::Reference
< linguistic2::XPossibleHyphens
> SAL_CALL
373 HyphDummy_Impl::createPossibleHyphens(
374 const OUString
& rWord
,
375 const lang::Locale
& rLocale
,
376 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
)
379 uno::Reference
< linguistic2::XPossibleHyphens
> xRes
;
381 xRes
= xHyph
->createPossibleHyphens( rWord
, rLocale
, rProperties
);
385 class LinguMgrExitLstnr
: public cppu::WeakImplHelper
<XEventListener
>
387 uno::Reference
< XDesktop2
> xDesktop
;
389 static void AtExit();
393 virtual ~LinguMgrExitLstnr() override
;
395 // lang::XEventListener
396 virtual void SAL_CALL
disposing(const EventObject
& rSource
) override
;
399 LinguMgrExitLstnr::LinguMgrExitLstnr()
401 // add object to frame::Desktop EventListeners in order to properly call
402 // the AtExit function at application exit.
404 uno::Reference
< XComponentContext
> xContext
= getProcessComponentContext();
405 xDesktop
= Desktop::create( xContext
);
406 xDesktop
->addEventListener( this );
409 LinguMgrExitLstnr::~LinguMgrExitLstnr()
413 xDesktop
->removeEventListener( this );
414 xDesktop
= nullptr; //! release reference to desktop
416 OSL_ENSURE(!xDesktop
.is(), "reference to desktop should be released");
419 void LinguMgrExitLstnr::disposing(const EventObject
& rSource
)
421 if (xDesktop
.is() && rSource
.Source
== xDesktop
)
423 xDesktop
->removeEventListener( this );
424 xDesktop
= nullptr; //! release reference to desktop
430 void LinguMgrExitLstnr::AtExit()
434 // release references
435 LinguMgr::xLngSvcMgr
= nullptr;
436 LinguMgr::xSpell
= nullptr;
437 LinguMgr::xHyph
= nullptr;
438 LinguMgr::xThes
= nullptr;
439 LinguMgr::xDicList
= nullptr;
440 LinguMgr::xProp
= nullptr;
441 LinguMgr::xIgnoreAll
= nullptr;
442 LinguMgr::xChangeAll
= nullptr;
444 LinguMgr::bExiting
= true;
446 LinguMgr::pExitLstnr
= nullptr;
450 LinguMgrExitLstnr
* LinguMgr::pExitLstnr
= nullptr;
451 bool LinguMgr::bExiting
= false;
452 uno::Reference
< XLinguServiceManager2
> LinguMgr::xLngSvcMgr
;
453 uno::Reference
< XSpellChecker1
> LinguMgr::xSpell
;
454 uno::Reference
< XHyphenator
> LinguMgr::xHyph
;
455 uno::Reference
< XThesaurus
> LinguMgr::xThes
;
456 uno::Reference
< XSearchableDictionaryList
> LinguMgr::xDicList
;
457 uno::Reference
< XLinguProperties
> LinguMgr::xProp
;
458 uno::Reference
< XDictionary
> LinguMgr::xIgnoreAll
;
459 uno::Reference
< XDictionary
> LinguMgr::xChangeAll
;
462 uno::Reference
< XLinguServiceManager2
> LinguMgr::GetLngSvcMgr()
468 pExitLstnr
= new LinguMgrExitLstnr
;
470 if (!xLngSvcMgr
.is())
471 xLngSvcMgr
= GetLngSvcMgr_Impl();
477 uno::Reference
< XSpellChecker1
> LinguMgr::GetSpellChecker()
479 return xSpell
.is() ? xSpell
: GetSpell();
482 uno::Reference
< XHyphenator
> LinguMgr::GetHyphenator()
484 return xHyph
.is() ? xHyph
: GetHyph();
487 uno::Reference
< XThesaurus
> LinguMgr::GetThesaurus()
489 return xThes
.is() ? xThes
: GetThes();
492 uno::Reference
< XSearchableDictionaryList
> LinguMgr::GetDictionaryList()
494 return xDicList
.is() ? xDicList
: GetDicList();
497 uno::Reference
< linguistic2::XLinguProperties
> LinguMgr::GetLinguPropertySet()
499 return xProp
.is() ? xProp
: GetProp();
502 uno::Reference
< XDictionary
> LinguMgr::GetStandardDic()
504 //! don't hold reference to this
505 //! (it may be removed from dictionary list and needs to be
506 //! created empty if accessed again)
507 return GetStandard();
510 uno::Reference
< XDictionary
> LinguMgr::GetIgnoreAllList()
512 return xIgnoreAll
.is() ? xIgnoreAll
: GetIgnoreAll();
515 uno::Reference
< XDictionary
> LinguMgr::GetChangeAllList()
517 return xChangeAll
.is() ? xChangeAll
: GetChangeAll();
520 uno::Reference
< XSpellChecker1
> LinguMgr::GetSpell()
526 pExitLstnr
= new LinguMgrExitLstnr
;
528 //! use dummy implementation in order to avoid loading of lingu DLL
529 xSpell
= new SpellDummy_Impl
;
533 uno::Reference
< XHyphenator
> LinguMgr::GetHyph()
539 pExitLstnr
= new LinguMgrExitLstnr
;
541 //! use dummy implementation in order to avoid loading of lingu DLL
542 xHyph
= new HyphDummy_Impl
;
546 uno::Reference
< XThesaurus
> LinguMgr::GetThes()
552 pExitLstnr
= new LinguMgrExitLstnr
;
554 //! use dummy implementation in order to avoid loading of lingu DLL
555 //! when only the XSupportedLocales interface is used.
556 //! The dummy accesses the real implementation (and thus loading the DLL)
557 //! when "real" work needs to be done only.
558 xThes
= new ThesDummy_Impl
;
562 uno::Reference
< XSearchableDictionaryList
> LinguMgr::GetDicList()
568 pExitLstnr
= new LinguMgrExitLstnr
;
570 xDicList
= linguistic2::DictionaryList::create( getProcessComponentContext() );
574 uno::Reference
< linguistic2::XLinguProperties
> LinguMgr::GetProp()
580 pExitLstnr
= new LinguMgrExitLstnr
;
582 xProp
= linguistic2::LinguProperties::create( getProcessComponentContext() );
586 uno::Reference
< XDictionary
> LinguMgr::GetIgnoreAll()
592 pExitLstnr
= new LinguMgrExitLstnr
;
594 uno::Reference
< XSearchableDictionaryList
> xTmpDicList( GetDictionaryList() );
595 if (xTmpDicList
.is())
597 std::locale
loc(Translate::Create("svt"));
598 xIgnoreAll
= xTmpDicList
->getDictionaryByName(
599 Translate::get(STR_DESCRIPTION_IGNOREALLLIST
, loc
) );
604 uno::Reference
< XDictionary
> LinguMgr::GetChangeAll()
610 pExitLstnr
= new LinguMgrExitLstnr
;
612 uno::Reference
< XSearchableDictionaryList
> _xDicList
= GetDictionaryList();
615 xChangeAll
= _xDicList
->createDictionary(
617 LanguageTag::convertToLocale( LANGUAGE_NONE
),
618 DictionaryType_NEGATIVE
, OUString() );
623 uno::Reference
< XDictionary
> LinguMgr::GetStandard()
625 // Tries to return a dictionary which may hold positive entries is
626 // persistent and not read-only.
631 uno::Reference
< XSearchableDictionaryList
> xTmpDicList( GetDictionaryList() );
632 if (!xTmpDicList
.is())
635 const OUString
aDicName( "standard.dic" );
636 uno::Reference
< XDictionary
> xDic
= xTmpDicList
->getDictionaryByName( aDicName
);
639 // try to create standard dictionary
640 uno::Reference
< XDictionary
> xTmp
;
643 xTmp
= xTmpDicList
->createDictionary( aDicName
,
644 LanguageTag::convertToLocale( LANGUAGE_NONE
),
645 DictionaryType_POSITIVE
,
646 linguistic::GetWritableDictionaryURL( aDicName
) );
648 catch(const css::uno::Exception
&)
652 // add new dictionary to list
655 xTmpDicList
->addDictionary( xTmp
);
656 xTmp
->setActive( true );
660 #if OSL_DEBUG_LEVEL > 1
661 uno::Reference
< XStorable
> xStor( xDic
, UNO_QUERY
);
662 OSL_ENSURE( xDic
.is() && xDic
->getDictionaryType() == DictionaryType_POSITIVE
,
663 "wrong dictionary type");
664 OSL_ENSURE( xDic
.is() && LanguageTag( xDic
->getLocale() ).getLanguageType() == LANGUAGE_NONE
,
665 "wrong dictionary language");
666 OSL_ENSURE( !xStor
.is() || (xStor
->hasLocation() && !xStor
->isReadonly()),
667 "dictionary not editable" );
673 SvxAlternativeSpelling
SvxGetAltSpelling(
674 const css::uno::Reference
< css::linguistic2::XHyphenatedWord
> & rHyphWord
)
676 SvxAlternativeSpelling aRes
;
677 if (rHyphWord
.is() && rHyphWord
->isAlternativeSpelling())
679 OUString
aWord( rHyphWord
->getWord() ),
680 aAltWord( rHyphWord
->getHyphenatedWord() );
681 sal_Int16 nHyphenationPos
= rHyphWord
->getHyphenationPos(),
682 nHyphenPos
= rHyphWord
->getHyphenPos();
683 sal_Int16 nLen
= static_cast<sal_Int16
>(aWord
.getLength());
684 sal_Int16 nAltLen
= static_cast<sal_Int16
>(aAltWord
.getLength());
685 const sal_Unicode
*pWord
= aWord
.getStr(),
686 *pAltWord
= aAltWord
.getStr();
688 // count number of chars from the left to the
689 // hyphenation pos / hyphen pos that are equal
691 while (nL
<= nHyphenationPos
&& nL
<= nHyphenPos
692 && pWord
[ nL
] == pAltWord
[ nL
])
694 // count number of chars from the right to the
695 // hyphenation pos / hyphen pos that are equal
697 sal_Int32 nIdx
= nLen
- 1;
698 sal_Int32 nAltIdx
= nAltLen
- 1;
699 while (nIdx
> nHyphenationPos
&& nAltIdx
> nHyphenPos
700 && pWord
[ nIdx
-- ] == pAltWord
[ nAltIdx
-- ])
703 aRes
.aReplacement
= aAltWord
.copy( nL
, nAltLen
- nL
- nR
);
704 aRes
.nChangedPos
= nL
;
705 aRes
.nChangedLength
= nLen
- nL
- nR
;
706 aRes
.bIsAltSpelling
= true;
712 SvxDicListChgClamp::SvxDicListChgClamp( uno::Reference
< XSearchableDictionaryList
> const &rxDicList
) :
713 xDicList ( rxDicList
)
717 xDicList
->beginCollectEvents();
721 SvxDicListChgClamp::~SvxDicListChgClamp()
725 xDicList
->endCollectEvents();
729 short SvxDicError(weld::Window
*pParent
, linguistic::DictionaryError nError
)
732 if (linguistic::DictionaryError::NONE
!= nError
)
737 case linguistic::DictionaryError::FULL
: pRid
= RID_SVXSTR_DIC_ERR_FULL
; break;
738 case linguistic::DictionaryError::READONLY
: pRid
= RID_SVXSTR_DIC_ERR_READONLY
; break;
740 pRid
= RID_SVXSTR_DIC_ERR_UNKNOWN
;
741 SAL_WARN("editeng", "unexpected case");
743 std::unique_ptr
<weld::MessageDialog
> xInfoBox(Application::CreateMessageDialog(pParent
,
744 VclMessageType::Info
, VclButtonsType::Ok
,
746 nRes
= xInfoBox
->run();
753 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */