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 .
21 #include <cppuhelper/factory.hxx>
23 #include <i18nlangtag/lang.h>
24 #include <i18nlangtag/languagetag.hxx>
25 #include <linguistic/misc.hxx>
26 #include <osl/mutex.hxx>
27 #include <osl/thread.h>
28 #include <sal/log.hxx>
29 #include <tools/debug.hxx>
30 #include <tools/stream.hxx>
31 #include <tools/urlobj.hxx>
32 #include <comphelper/processfactory.hxx>
33 #include <comphelper/string.hxx>
34 #include <comphelper/sequence.hxx>
35 #include <unotools/ucbstreamhelper.hxx>
37 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
38 #include <com/sun/star/linguistic2/DictionaryEventFlags.hpp>
39 #include <com/sun/star/io/TempFile.hpp>
40 #include <com/sun/star/io/XInputStream.hpp>
42 #include <com/sun/star/linguistic2/LinguServiceManager.hpp>
43 #include <com/sun/star/linguistic2/XSpellChecker1.hpp>
50 using namespace com::sun::star
;
51 using namespace com::sun::star::lang
;
52 using namespace com::sun::star::uno
;
53 using namespace com::sun::star::linguistic2
;
54 using namespace linguistic
;
58 #define VERS2_NOLANGUAGE 1024
60 #define MAX_HEADER_LENGTH 16
62 // XML-header to query SPELLML support
63 // to handle user words with "Grammar By" model words
64 #define SPELLML_SUPPORT "<?xml?>"
66 // User dictionaries can contain optional "title:" tags
67 // to support custom titles with space and other characters.
68 // (old mechanism stores the title of the user dictionary
69 // only in its file name, but special characters are
70 // problem for user dictionaries shipped with LibreOffice).
72 // The following fake file name extension will be
73 // added to the text of the title: field for correct
74 // text stripping and dictionary saving.
75 #define EXTENSION_FOR_TITLE_TEXT "."
77 const char* const pVerStr2
= "WBSWG2";
78 const char* const pVerStr5
= "WBSWG5";
79 const char* const pVerStr6
= "WBSWG6";
80 const char* const pVerOOo7
= "OOoUserDict1";
82 const sal_Int16 DIC_VERSION_DONTKNOW
= -1;
83 const sal_Int16 DIC_VERSION_2
= 2;
84 const sal_Int16 DIC_VERSION_5
= 5;
85 const sal_Int16 DIC_VERSION_6
= 6;
86 const sal_Int16 DIC_VERSION_7
= 7;
88 static uno::Reference
< XLinguServiceManager2
> GetLngSvcMgr_Impl()
90 uno::Reference
< XComponentContext
> xContext( comphelper::getProcessComponentContext() );
91 uno::Reference
< XLinguServiceManager2
> xRes
= LinguServiceManager::create( xContext
) ;
95 static bool getTag(const OString
&rLine
, const char *pTagName
,
98 sal_Int32 nPos
= rLine
.indexOf(pTagName
);
102 rTagValue
= comphelper::string::strip(rLine
.subView(nPos
+ strlen(pTagName
)),
108 sal_Int16
ReadDicVersion( SvStream
& rStream
, LanguageType
&nLng
, bool &bNeg
, OUString
&aDicName
)
111 sal_Int16 nDicVersion
= DIC_VERSION_DONTKNOW
;
112 char pMagicHeader
[MAX_HEADER_LENGTH
];
114 nLng
= LANGUAGE_NONE
;
117 if (rStream
.GetError())
120 sal_uInt64
const nSniffPos
= rStream
.Tell();
121 static std::size_t nVerOOo7Len
= sal::static_int_cast
< std::size_t >(strlen( pVerOOo7
));
122 pMagicHeader
[ nVerOOo7Len
] = '\0';
123 if ((rStream
.ReadBytes(static_cast<void *>(pMagicHeader
), nVerOOo7Len
) == nVerOOo7Len
) &&
124 !strcmp(pMagicHeader
, pVerOOo7
))
129 nDicVersion
= DIC_VERSION_7
;
131 // 1st skip magic / header line
132 rStream
.ReadLine(aLine
);
134 // 2nd line: language all | en-US | pt-BR ...
135 while ((bSuccess
= rStream
.ReadLine(aLine
)))
139 if (aLine
[0] == '#') // skip comments
143 if (getTag(aLine
, "lang: ", aTagValue
))
145 if (aTagValue
== "<none>")
146 nLng
= LANGUAGE_NONE
;
148 nLng
= LanguageTag::convertToLanguageType(
149 OStringToOUString( aTagValue
, RTL_TEXTENCODING_ASCII_US
));
152 // type: negative / positive
153 if (getTag(aLine
, "type: ", aTagValue
))
155 bNeg
= aTagValue
== "negative";
159 if (getTag(aLine
, "title: ", aTagValue
))
161 aDicName
= OStringToOUString( aTagValue
, RTL_TEXTENCODING_UTF8
) +
162 // recent title text preparation in GetDicInfoStr() waits for an
163 // extension, so we add it to avoid bad stripping at final dot
165 EXTENSION_FOR_TITLE_TEXT
;
168 if (aLine
.indexOf("---") != -1) // end of header
178 rStream
.Seek (nSniffPos
);
180 rStream
.ReadUInt16( nLen
);
181 if (nLen
>= MAX_HEADER_LENGTH
)
184 rStream
.ReadBytes(pMagicHeader
, nLen
);
185 pMagicHeader
[nLen
] = '\0';
187 // Check version magic
188 if (0 == strcmp( pMagicHeader
, pVerStr6
))
189 nDicVersion
= DIC_VERSION_6
;
190 else if (0 == strcmp( pMagicHeader
, pVerStr5
))
191 nDicVersion
= DIC_VERSION_5
;
192 else if (0 == strcmp( pMagicHeader
, pVerStr2
))
193 nDicVersion
= DIC_VERSION_2
;
195 nDicVersion
= DIC_VERSION_DONTKNOW
;
197 if (DIC_VERSION_2
== nDicVersion
||
198 DIC_VERSION_5
== nDicVersion
||
199 DIC_VERSION_6
== nDicVersion
)
201 // The language of the dictionary
203 rStream
.ReadUInt16( nTmp
);
204 nLng
= LanguageType(nTmp
);
205 if (VERS2_NOLANGUAGE
== static_cast<sal_uInt16
>(nLng
))
206 nLng
= LANGUAGE_NONE
;
209 rStream
.ReadCharAsBool( bNeg
);
216 DictionaryNeo::DictionaryNeo(const OUString
&rName
,
217 LanguageType nLang
, DictionaryType eType
,
218 const OUString
&rMainURL
,
220 aDicEvtListeners( GetLinguMutex() ),
226 nDicVersion
= DIC_VERSION_DONTKNOW
;
228 bIsModified
= bIsActive
= false;
229 bIsReadonly
= !bWriteable
;
231 if( !rMainURL
.isEmpty())
233 bool bExists
= FileExists( rMainURL
);
236 // save new dictionaries with in Format 7 (UTF8 plain text)
237 nDicVersion
= DIC_VERSION_7
;
239 //! create physical representation of an **empty** dictionary
240 //! that could be found by the dictionary-list implementation
241 // (Note: empty dictionaries are not just empty files!)
242 DBG_ASSERT( !bIsReadonly
,
243 "DictionaryNeo: dictionaries should be writeable if they are to be saved" );
245 saveEntries( rMainURL
);
246 bNeedEntries
= false;
251 // non persistent dictionaries (like IgnoreAllList) should always be writable
253 bNeedEntries
= false;
257 DictionaryNeo::~DictionaryNeo()
261 ErrCode
DictionaryNeo::loadEntries(const OUString
&rMainURL
)
263 MutexGuard
aGuard( GetLinguMutex() );
265 // counter check that it is safe to set bIsModified to sal_False at
266 // the end of the function
267 DBG_ASSERT(!bIsModified
, "lng : dictionary already modified!");
269 // function should only be called once in order to load entries from file
270 bNeedEntries
= false;
272 if (rMainURL
.isEmpty())
275 uno::Reference
< uno::XComponentContext
> xContext( comphelper::getProcessComponentContext() );
277 // get XInputStream stream
278 uno::Reference
< io::XInputStream
> xStream
;
281 uno::Reference
< ucb::XSimpleFileAccess3
> xAccess( ucb::SimpleFileAccess::create(xContext
) );
282 xStream
= xAccess
->openFileRead( rMainURL
);
284 catch (const uno::Exception
&)
286 SAL_WARN( "linguistic", "failed to get input stream" );
289 return ErrCode(sal_uInt32(-1));
291 std::unique_ptr
<SvStream
> pStream( utl::UcbStreamHelper::CreateStream( xStream
) );
296 nDicVersion
= ReadDicVersion(*pStream
, nLang
, bNegativ
, aDicName
);
297 ErrCode nErr
= pStream
->GetError();
298 if (nErr
!= ERRCODE_NONE
)
303 eDicType
= bNegativ
? DictionaryType_NEGATIVE
: DictionaryType_POSITIVE
;
305 rtl_TextEncoding eEnc
= osl_getThreadTextEncoding();
306 if (nDicVersion
>= DIC_VERSION_6
)
307 eEnc
= RTL_TEXTENCODING_UTF8
;
310 if (DIC_VERSION_6
== nDicVersion
||
311 DIC_VERSION_5
== nDicVersion
||
312 DIC_VERSION_2
== nDicVersion
)
315 char aWordBuf
[ BUFSIZE
];
317 // Read the first word
320 pStream
->ReadUInt16( nLen
);
321 if (ERRCODE_NONE
!= (nErr
= pStream
->GetError()))
323 if ( nLen
< BUFSIZE
)
325 pStream
->ReadBytes(aWordBuf
, nLen
);
326 if (ERRCODE_NONE
!= (nErr
= pStream
->GetError()))
328 *(aWordBuf
+ nLen
) = 0;
331 return SVSTREAM_READ_ERROR
;
334 while(!pStream
->eof())
337 // Paste in dictionary without converting
340 OUString
aText(aWordBuf
, rtl_str_getLength(aWordBuf
), eEnc
);
341 uno::Reference
< XDictionaryEntry
> xEntry
=
342 new DicEntry( aText
, bNegativ
);
343 addEntry_Impl( xEntry
, true ); //! don't launch events here
346 pStream
->ReadUInt16( nLen
);
349 if (ERRCODE_NONE
!= (nErr
= pStream
->GetError()))
354 pStream
->ReadBytes(aWordBuf
, nLen
);
355 if (ERRCODE_NONE
!= (nErr
= pStream
->GetError()))
359 return SVSTREAM_READ_ERROR
;
360 *(aWordBuf
+ nLen
) = 0;
363 else if (DIC_VERSION_7
== nDicVersion
)
367 // remaining lines - stock strings (a [==] b)
368 while (pStream
->ReadLine(aLine
))
370 if (aLine
.isEmpty() || aLine
[0] == '#') // skip comments
372 OUString aText
= OStringToOUString(aLine
, RTL_TEXTENCODING_UTF8
);
373 uno::Reference
< XDictionaryEntry
> xEntry
=
374 new DicEntry( aText
, eDicType
== DictionaryType_NEGATIVE
);
375 addEntry_Impl( xEntry
, true ); //! don't launch events here
379 SAL_WARN_IF(!isSorted(), "linguistic", "dictionary is not sorted");
381 // since this routine should be called only initially (prior to any
382 // modification to be saved) we reset the bIsModified flag here that
383 // was implicitly set by addEntry_Impl
386 return pStream
->GetError();
389 static OString
formatForSave(const uno::Reference
< XDictionaryEntry
> &xEntry
,
390 rtl_TextEncoding eEnc
)
392 OUStringBuffer
aStr(xEntry
->getDictionaryWord());
394 if (xEntry
->isNegative() || !xEntry
->getReplacementText().isEmpty())
396 aStr
.append("==" + xEntry
->getReplacementText());
398 return OUStringToOString(aStr
, eEnc
);
401 ErrCode
DictionaryNeo::saveEntries(const OUString
&rURL
)
403 MutexGuard
aGuard( GetLinguMutex() );
407 DBG_ASSERT(!INetURLObject( rURL
).HasError(), "lng : invalid URL");
409 uno::Reference
< uno::XComponentContext
> xContext( comphelper::getProcessComponentContext() );
411 // get XOutputStream stream
412 uno::Reference
<io::XStream
> xStream
;
415 xStream
= io::TempFile::create(xContext
);
417 catch (const uno::Exception
&)
419 DBG_ASSERT( false, "failed to get input stream" );
422 return ErrCode(sal_uInt32(-1));
424 std::unique_ptr
<SvStream
> pStream( utl::UcbStreamHelper::CreateStream( xStream
) );
426 // Always write as the latest version, i.e. DIC_VERSION_7
428 rtl_TextEncoding eEnc
= RTL_TEXTENCODING_UTF8
;
429 pStream
->WriteLine(pVerOOo7
);
430 ErrCode nErr
= pStream
->GetError();
431 if (nErr
!= ERRCODE_NONE
)
433 /* XXX: the <none> case could be differentiated, is it absence or
434 * undetermined or multiple? Earlier versions did not know about 'und' and
435 * 'mul' and 'zxx' codes. Sync with ReadDicVersion() */
436 if (LinguIsUnspecified(nLanguage
))
437 pStream
->WriteLine("lang: <none>");
440 OString aLine
= "lang: " + OUStringToOString(LanguageTag::convertToBcp47(nLanguage
), eEnc
);
441 pStream
->WriteLine(aLine
);
443 if (ERRCODE_NONE
!= (nErr
= pStream
->GetError()))
445 if (eDicType
== DictionaryType_POSITIVE
)
446 pStream
->WriteLine("type: positive");
448 pStream
->WriteLine("type: negative");
449 if (aDicName
.endsWith(EXTENSION_FOR_TITLE_TEXT
))
451 pStream
->WriteLine(OString("title: " + OUStringToOString(
452 // strip EXTENSION_FOR_TITLE_TEXT
453 aDicName
.subView(0, aDicName
.lastIndexOf(EXTENSION_FOR_TITLE_TEXT
)), eEnc
)));
455 if (ERRCODE_NONE
!= (nErr
= pStream
->GetError()))
457 pStream
->WriteLine("---");
458 if (ERRCODE_NONE
!= (nErr
= pStream
->GetError()))
460 for (const Reference
<XDictionaryEntry
> & aEntrie
: aEntries
)
462 OString aOutStr
= formatForSave(aEntrie
, eEnc
);
463 pStream
->WriteLine (aOutStr
);
464 if (ERRCODE_NONE
!= (nErr
= pStream
->GetError()))
471 uno::Reference
< ucb::XSimpleFileAccess3
> xAccess(ucb::SimpleFileAccess::create(xContext
));
472 Reference
<io::XInputStream
> xInputStream(xStream
, UNO_QUERY_THROW
);
473 uno::Reference
<io::XSeekable
> xSeek(xInputStream
, UNO_QUERY_THROW
);
475 xAccess
->writeFile(rURL
, xInputStream
);
476 //If we are migrating from an older version, then on first successful
477 //write, we're now converted to the latest version, i.e. DIC_VERSION_7
478 nDicVersion
= DIC_VERSION_7
;
480 catch (const uno::Exception
&)
482 DBG_ASSERT( false, "failed to write stream" );
483 return ErrCode(sal_uInt32(-1));
489 void DictionaryNeo::launchEvent(sal_Int16 nEvent
,
490 const uno::Reference
< XDictionaryEntry
>& xEntry
)
492 MutexGuard
aGuard( GetLinguMutex() );
494 DictionaryEvent aEvt
;
495 aEvt
.Source
= uno::Reference
< XDictionary
>( this );
496 aEvt
.nEvent
= nEvent
;
497 aEvt
.xDictionaryEntry
= xEntry
;
499 aDicEvtListeners
.notifyEach( &XDictionaryEventListener::processDictionaryEvent
, aEvt
);
502 int DictionaryNeo::cmpDicEntry(const OUString
& rWord1
,
503 const OUString
&rWord2
,
506 MutexGuard
aGuard( GetLinguMutex() );
508 // returns 0 if rWord1 is equal to rWord2
509 // " a value < 0 if rWord1 is less than rWord2
510 // " a value > 0 if rWord1 is greater than rWord2
514 sal_Int32 nLen1
= rWord1
.getLength(),
515 nLen2
= rWord2
.getLength();
518 const sal_Unicode cChar
= '.';
519 if (nLen1
&& cChar
== rWord1
[ nLen1
- 1 ])
521 if (nLen2
&& cChar
== rWord2
[ nLen2
- 1 ])
525 const sal_Unicode cIgnChar
= '=';
526 const sal_Unicode cIgnBeg
= '['; // for alternative hyphenation, eg. Schif[f]fahrt, Zuc[1k]ker
527 const sal_Unicode cIgnEnd
= ']'; // planned: gee"[1-/e]rfde or ge[-/1e]e"rfde (gee"rfde -> ge=erfde)
535 sal_Unicode cChar1
= '\0';
536 sal_Unicode cChar2
= '\0';
539 // skip chars to be ignored
541 while (nIdx1
< nLen1
)
543 cChar1
= rWord1
[ nIdx1
];
544 if (cChar1
!= cIgnChar
&& cChar1
!= cIgnBeg
&& !IgnState
)
546 if ( cChar1
== cIgnBeg
)
548 else if (cChar1
== cIgnEnd
)
554 while (nIdx2
< nLen2
)
556 cChar2
= rWord2
[ nIdx2
];
557 if (cChar2
!= cIgnChar
&& cChar2
!= cIgnBeg
&& !IgnState
)
559 if ( cChar2
== cIgnBeg
)
561 else if (cChar2
== cIgnEnd
)
567 if (nIdx1
< nLen1
&& nIdx2
< nLen2
)
569 nDiff
= cChar1
- cChar2
;
575 } while (nIdx1
< nLen1
&& nIdx2
< nLen2
);
581 { // the string with the smallest count of not ignored chars is the
584 // count remaining IgnChars
586 while (nIdx1
< nLen1
)
588 if (rWord1
[ nIdx1
] == cIgnBeg
)
590 if (IgnState
|| rWord1
[ nIdx1
] == cIgnChar
)
592 if (rWord1
[ nIdx1
] == cIgnEnd
)
597 while (nIdx2
< nLen2
)
599 if (rWord2
[ nIdx2
] == cIgnBeg
)
601 if (IgnState
|| rWord2
[ nIdx2
] == cIgnChar
)
603 if (rWord2
[ nIdx2
] == cIgnEnd
)
608 nRes
= (nLen1
- nNumIgnChar1
) - (nLen2
- nNumIgnChar2
);
614 bool DictionaryNeo::seekEntry(const OUString
&rWord
,
615 sal_Int32
*pPos
, bool bSimilarOnly
)
617 // look for entry with binary search.
618 // return sal_True if found sal_False else.
619 // if pPos != NULL it will become the position of the found entry, or
620 // if that was not found the position where it has to be inserted
621 // to keep the entries sorted
623 MutexGuard
aGuard( GetLinguMutex() );
625 sal_Int32 nUpperIdx
= getCount(),
631 while( nLowerIdx
<= nUpperIdx
)
633 nMidIdx
= (nLowerIdx
+ nUpperIdx
) / 2;
634 DBG_ASSERT(aEntries
[nMidIdx
].is(), "lng : empty entry encountered");
636 int nCmp
= - cmpDicEntry( aEntries
[nMidIdx
]->getDictionaryWord(),
637 rWord
, bSimilarOnly
);
640 if( pPos
) *pPos
= nMidIdx
;
644 nLowerIdx
= nMidIdx
+ 1;
645 else if( nMidIdx
== 0 )
647 if( pPos
) *pPos
= nLowerIdx
;
651 nUpperIdx
= nMidIdx
- 1;
654 if( pPos
) *pPos
= nLowerIdx
;
658 bool DictionaryNeo::isSorted()
662 sal_Int32 nEntries
= getCount();
664 for (i
= 1; i
< nEntries
; i
++)
666 if (cmpDicEntry( aEntries
[i
-1]->getDictionaryWord(),
667 aEntries
[i
]->getDictionaryWord() ) > 0)
676 bool DictionaryNeo::addEntry_Impl(const uno::Reference
< XDictionaryEntry
>& xDicEntry
,
679 MutexGuard
aGuard( GetLinguMutex() );
683 if ( bIsLoadEntries
|| (!bIsReadonly
&& xDicEntry
.is()) )
685 bool bIsNegEntry
= xDicEntry
->isNegative();
686 bool bAddEntry
= !isFull() &&
687 ( ( eDicType
== DictionaryType_POSITIVE
&& !bIsNegEntry
)
688 || ( eDicType
== DictionaryType_NEGATIVE
&& bIsNegEntry
)
689 || ( eDicType
== DictionaryType_MIXED
) );
691 // look for position to insert entry at
692 // if there is already an entry do not insert the new one
696 const bool bFound
= seekEntry( xDicEntry
->getDictionaryWord(), &nPos
);
703 DBG_ASSERT(!bNeedEntries
, "lng : entries still not loaded");
705 // insert new entry at specified position
706 aEntries
.insert(aEntries
.begin() + nPos
, xDicEntry
);
707 SAL_WARN_IF(!isSorted(), "linguistic", "dictionary entries unsorted");
713 launchEvent( DictionaryEventFlags::ADD_ENTRY
, xDicEntry
);
717 // add word to the Hunspell dictionary using a sample word for affixation/compounding
718 if (xDicEntry
.is() && !xDicEntry
->isNegative() && !xDicEntry
->getReplacementText().isEmpty()) {
719 uno::Reference
< XLinguServiceManager2
> xLngSvcMgr( GetLngSvcMgr_Impl() );
720 uno::Reference
< XSpellChecker1
> xSpell
;
721 Reference
< XSpellAlternatives
> xTmpRes
;
722 xSpell
.set( xLngSvcMgr
->getSpellChecker(), UNO_QUERY
);
723 Sequence
< css::beans::PropertyValue
> aEmptySeq
;
724 if (xSpell
.is() && (xSpell
->isValid( SPELLML_SUPPORT
, static_cast<sal_uInt16
>(nLanguage
), aEmptySeq
)))
726 // "Grammar By" sample word is a Hunspell dictionary word?
727 if (xSpell
->isValid( xDicEntry
->getReplacementText(), static_cast<sal_uInt16
>(nLanguage
), aEmptySeq
))
729 xTmpRes
= xSpell
->spell( "<?xml?><query type='add'><word>" +
730 xDicEntry
->getDictionaryWord() + "</word><word>" + xDicEntry
->getReplacementText() +
731 "</word></query>", static_cast<sal_uInt16
>(nLanguage
), aEmptySeq
);
741 OUString SAL_CALL
DictionaryNeo::getName( )
743 MutexGuard
aGuard( GetLinguMutex() );
747 void SAL_CALL
DictionaryNeo::setName( const OUString
& aName
)
749 MutexGuard
aGuard( GetLinguMutex() );
751 if (aDicName
!= aName
)
754 launchEvent(DictionaryEventFlags::CHG_NAME
, nullptr);
758 DictionaryType SAL_CALL
DictionaryNeo::getDictionaryType( )
760 MutexGuard
aGuard( GetLinguMutex() );
765 void SAL_CALL
DictionaryNeo::setActive( sal_Bool bActivate
)
767 MutexGuard
aGuard( GetLinguMutex() );
769 if (bIsActive
== bool(bActivate
))
772 bIsActive
= bActivate
;
773 sal_Int16 nEvent
= bIsActive
?
774 DictionaryEventFlags::ACTIVATE_DIC
: DictionaryEventFlags::DEACTIVATE_DIC
;
776 // remove entries from memory if dictionary is deactivated
779 bool bIsEmpty
= aEntries
.empty();
781 // save entries first if necessary
782 if (bIsModified
&& hasLocation() && !isReadonly())
787 bNeedEntries
= !bIsEmpty
;
789 DBG_ASSERT( !bIsModified
|| !hasLocation() || isReadonly(),
790 "lng : dictionary is still modified" );
793 launchEvent(nEvent
, nullptr);
796 sal_Bool SAL_CALL
DictionaryNeo::isActive( )
798 MutexGuard
aGuard( GetLinguMutex() );
802 sal_Int32 SAL_CALL
DictionaryNeo::getCount( )
804 MutexGuard
aGuard( GetLinguMutex() );
807 loadEntries( aMainURL
);
808 return static_cast<sal_Int32
>(aEntries
.size());
811 Locale SAL_CALL
DictionaryNeo::getLocale( )
813 MutexGuard
aGuard( GetLinguMutex() );
814 return LanguageTag::convertToLocale( nLanguage
);
817 void SAL_CALL
DictionaryNeo::setLocale( const Locale
& aLocale
)
819 MutexGuard
aGuard( GetLinguMutex() );
820 LanguageType nLanguageP
= LinguLocaleToLanguage( aLocale
);
821 if (!bIsReadonly
&& nLanguage
!= nLanguageP
)
823 nLanguage
= nLanguageP
;
824 bIsModified
= true; // new language needs to be saved with dictionary
826 launchEvent( DictionaryEventFlags::CHG_LANGUAGE
, nullptr );
830 uno::Reference
< XDictionaryEntry
> SAL_CALL
DictionaryNeo::getEntry(
831 const OUString
& aWord
)
833 MutexGuard
aGuard( GetLinguMutex() );
836 loadEntries( aMainURL
);
839 bool bFound
= seekEntry( aWord
, &nPos
, true );
840 DBG_ASSERT(!bFound
|| nPos
< static_cast<sal_Int32
>(aEntries
.size()), "lng : index out of range");
842 return bFound
? aEntries
[ nPos
]
843 : uno::Reference
< XDictionaryEntry
>();
846 sal_Bool SAL_CALL
DictionaryNeo::addEntry(
847 const uno::Reference
< XDictionaryEntry
>& xDicEntry
)
849 MutexGuard
aGuard( GetLinguMutex() );
856 loadEntries( aMainURL
);
857 bRes
= addEntry_Impl( xDicEntry
);
864 DictionaryNeo::add( const OUString
& rWord
, sal_Bool bIsNegative
,
865 const OUString
& rRplcText
)
867 MutexGuard
aGuard( GetLinguMutex() );
873 uno::Reference
< XDictionaryEntry
> xEntry
=
874 new DicEntry( rWord
, bIsNegative
, rRplcText
);
875 bRes
= addEntry_Impl( xEntry
);
881 sal_Bool SAL_CALL
DictionaryNeo::remove( const OUString
& aWord
)
883 MutexGuard
aGuard( GetLinguMutex() );
885 bool bRemoved
= false;
890 loadEntries( aMainURL
);
893 bool bFound
= seekEntry( aWord
, &nPos
);
894 DBG_ASSERT(!bFound
|| nPos
< static_cast<sal_Int32
>(aEntries
.size()), "lng : index out of range");
896 // remove element if found
899 // entry to be removed
900 uno::Reference
< XDictionaryEntry
>
901 xDicEntry( aEntries
[ nPos
] );
902 DBG_ASSERT(xDicEntry
.is(), "lng : dictionary entry is NULL");
904 aEntries
.erase(aEntries
.begin() + nPos
);
906 bRemoved
= bIsModified
= true;
908 launchEvent( DictionaryEventFlags::DEL_ENTRY
, xDicEntry
);
915 sal_Bool SAL_CALL
DictionaryNeo::isFull( )
917 MutexGuard
aGuard( GetLinguMutex() );
920 loadEntries( aMainURL
);
921 return aEntries
.size() >= DIC_MAX_ENTRIES
;
924 uno::Sequence
< uno::Reference
< XDictionaryEntry
> >
925 SAL_CALL
DictionaryNeo::getEntries( )
927 MutexGuard
aGuard( GetLinguMutex() );
930 loadEntries( aMainURL
);
931 return comphelper::containerToSequence(aEntries
);
935 void SAL_CALL
DictionaryNeo::clear( )
937 MutexGuard
aGuard( GetLinguMutex() );
939 if (!bIsReadonly
&& !aEntries
.empty())
941 // release all references to old entries
944 bNeedEntries
= false;
947 launchEvent( DictionaryEventFlags::ENTRIES_CLEARED
, nullptr );
951 sal_Bool SAL_CALL
DictionaryNeo::addDictionaryEventListener(
952 const uno::Reference
< XDictionaryEventListener
>& xListener
)
954 MutexGuard
aGuard( GetLinguMutex() );
959 sal_Int32 nLen
= aDicEvtListeners
.getLength();
960 bRes
= aDicEvtListeners
.addInterface( xListener
) != nLen
;
965 sal_Bool SAL_CALL
DictionaryNeo::removeDictionaryEventListener(
966 const uno::Reference
< XDictionaryEventListener
>& xListener
)
968 MutexGuard
aGuard( GetLinguMutex() );
973 sal_Int32 nLen
= aDicEvtListeners
.getLength();
974 bRes
= aDicEvtListeners
.removeInterface( xListener
) != nLen
;
980 sal_Bool SAL_CALL
DictionaryNeo::hasLocation()
982 MutexGuard
aGuard( GetLinguMutex() );
983 return !aMainURL
.isEmpty();
986 OUString SAL_CALL
DictionaryNeo::getLocation()
988 MutexGuard
aGuard( GetLinguMutex() );
992 sal_Bool SAL_CALL
DictionaryNeo::isReadonly()
994 MutexGuard
aGuard( GetLinguMutex() );
999 void SAL_CALL
DictionaryNeo::store()
1001 MutexGuard
aGuard( GetLinguMutex() );
1003 if (bIsModified
&& hasLocation() && !isReadonly())
1005 if (!saveEntries( aMainURL
))
1006 bIsModified
= false;
1010 void SAL_CALL
DictionaryNeo::storeAsURL(
1011 const OUString
& aURL
,
1012 const uno::Sequence
< beans::PropertyValue
>& /*rArgs*/ )
1014 MutexGuard
aGuard( GetLinguMutex() );
1016 if (!saveEntries( aURL
))
1019 bIsModified
= false;
1020 bIsReadonly
= IsReadOnly( getLocation() );
1024 void SAL_CALL
DictionaryNeo::storeToURL(
1025 const OUString
& aURL
,
1026 const uno::Sequence
< beans::PropertyValue
>& /*rArgs*/ )
1028 MutexGuard
aGuard( GetLinguMutex() );
1033 DicEntry::DicEntry(const OUString
&rDicFileWord
,
1034 bool bIsNegativWord
)
1036 if (!rDicFileWord
.isEmpty())
1037 splitDicFileWord( rDicFileWord
, aDicWord
, aReplacement
);
1038 bIsNegativ
= bIsNegativWord
;
1041 DicEntry::DicEntry(const OUString
&rDicWord
, bool bNegativ
,
1042 const OUString
&rRplcText
) :
1043 aDicWord (rDicWord
),
1044 aReplacement (rRplcText
),
1045 bIsNegativ (bNegativ
)
1049 DicEntry::~DicEntry()
1053 void DicEntry::splitDicFileWord(const OUString
&rDicFileWord
,
1055 OUString
&rReplacement
)
1057 MutexGuard
aGuard( GetLinguMutex() );
1059 sal_Int32 nDelimPos
= rDicFileWord
.indexOf( "==" );
1060 if (-1 != nDelimPos
)
1062 sal_Int32 nTriplePos
= nDelimPos
+ 2;
1063 if ( nTriplePos
< rDicFileWord
.getLength()
1064 && rDicFileWord
[ nTriplePos
] == '=' )
1066 rDicWord
= rDicFileWord
.copy( 0, nDelimPos
);
1067 rReplacement
= rDicFileWord
.copy( nDelimPos
+ 2 );
1071 rDicWord
= rDicFileWord
;
1072 rReplacement
.clear();
1076 OUString SAL_CALL
DicEntry::getDictionaryWord( )
1078 MutexGuard
aGuard( GetLinguMutex() );
1082 sal_Bool SAL_CALL
DicEntry::isNegative( )
1084 MutexGuard
aGuard( GetLinguMutex() );
1088 OUString SAL_CALL
DicEntry::getReplacementText( )
1090 MutexGuard
aGuard( GetLinguMutex() );
1091 return aReplacement
;
1095 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */