tdf#147298: Add a simple test case for formula cell tracking by column.
[LibreOffice.git] / linguistic / source / dicimp.cxx
blob20fe8bbadac18b4e3ff5154132b5ab89c94de216
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 .
21 #include <cppuhelper/factory.hxx>
22 #include "dicimp.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>
45 #include <algorithm>
48 using namespace utl;
49 using namespace osl;
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;
57 #define BUFSIZE 4096
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 constexpr OUStringLiteral SPELLML_SUPPORT = u"<?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 constexpr OUStringLiteral EXTENSION_FOR_TITLE_TEXT = u".";
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 ) ;
92 return xRes;
95 static bool getTag(const OString &rLine, const char *pTagName,
96 OString &rTagValue)
98 sal_Int32 nPos = rLine.indexOf(pTagName);
99 if (nPos == -1)
100 return false;
102 rTagValue = comphelper::string::strip(rLine.subView(nPos + strlen(pTagName)),
103 ' ');
104 return true;
108 sal_Int16 ReadDicVersion( SvStream& rStream, LanguageType &nLng, bool &bNeg, OUString &aDicName )
110 // Sniff the header
111 sal_Int16 nDicVersion = DIC_VERSION_DONTKNOW;
112 char pMagicHeader[MAX_HEADER_LENGTH];
114 nLng = LANGUAGE_NONE;
115 bNeg = false;
117 if (rStream.GetError())
118 return -1;
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))
126 bool bSuccess;
127 OString aLine;
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)))
137 OString aTagValue;
139 if (aLine[0] == '#') // skip comments
140 continue;
142 // lang: field
143 if (getTag(aLine, "lang: ", aTagValue))
145 if (aTagValue == "<none>")
146 nLng = LANGUAGE_NONE;
147 else
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";
158 // lang: title
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
164 // of the title text
165 EXTENSION_FOR_TITLE_TEXT;
168 if (aLine.indexOf("---") != -1) // end of header
169 break;
171 if (!bSuccess)
172 return -2;
174 else
176 sal_uInt16 nLen;
178 rStream.Seek (nSniffPos );
180 rStream.ReadUInt16( nLen );
181 if (nLen >= MAX_HEADER_LENGTH)
182 return -1;
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;
194 else
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
202 sal_uInt16 nTmp = 0;
203 rStream.ReadUInt16( nTmp );
204 nLng = LanguageType(nTmp);
205 if (VERS2_NOLANGUAGE == static_cast<sal_uInt16>(nLng))
206 nLng = LANGUAGE_NONE;
208 // Negative Flag
209 rStream.ReadCharAsBool( bNeg );
213 return nDicVersion;
216 DictionaryNeo::DictionaryNeo(const OUString &rName,
217 LanguageType nLang, DictionaryType eType,
218 const OUString &rMainURL,
219 bool bWriteable) :
220 aDicEvtListeners( GetLinguMutex() ),
221 aDicName (rName),
222 aMainURL (rMainURL),
223 eDicType (eType),
224 nLanguage (nLang)
226 nDicVersion = DIC_VERSION_DONTKNOW;
227 bNeedEntries = true;
228 bIsModified = bIsActive = false;
229 bIsReadonly = !bWriteable;
231 if( !rMainURL.isEmpty())
233 bool bExists = FileExists( rMainURL );
234 if( !bExists )
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" );
244 if (!bIsReadonly)
245 saveEntries( rMainURL );
246 bNeedEntries = false;
249 else
251 // non persistent dictionaries (like IgnoreAllList) should always be writable
252 bIsReadonly = false;
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())
273 return ERRCODE_NONE;
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" );
288 if (!xStream.is())
289 return ErrCode(sal_uInt32(-1));
291 std::unique_ptr<SvStream> pStream( utl::UcbStreamHelper::CreateStream( xStream ) );
293 // read header
294 bool bNegativ;
295 LanguageType nLang;
296 nDicVersion = ReadDicVersion(*pStream, nLang, bNegativ, aDicName);
297 ErrCode nErr = pStream->GetError();
298 if (nErr != ERRCODE_NONE)
299 return nErr;
301 nLanguage = nLang;
303 eDicType = bNegativ ? DictionaryType_NEGATIVE : DictionaryType_POSITIVE;
305 rtl_TextEncoding eEnc = osl_getThreadTextEncoding();
306 if (nDicVersion >= DIC_VERSION_6)
307 eEnc = RTL_TEXTENCODING_UTF8;
308 aEntries.clear();
310 if (DIC_VERSION_6 == nDicVersion ||
311 DIC_VERSION_5 == nDicVersion ||
312 DIC_VERSION_2 == nDicVersion)
314 sal_uInt16 nLen = 0;
315 char aWordBuf[ BUFSIZE ];
317 // Read the first word
318 if (!pStream->eof())
320 pStream->ReadUInt16( nLen );
321 if (ERRCODE_NONE != (nErr = pStream->GetError()))
322 return nErr;
323 if ( nLen < BUFSIZE )
325 pStream->ReadBytes(aWordBuf, nLen);
326 if (ERRCODE_NONE != (nErr = pStream->GetError()))
327 return nErr;
328 *(aWordBuf + nLen) = 0;
330 else
331 return SVSTREAM_READ_ERROR;
334 while(!pStream->eof())
336 // Read from file
337 // Paste in dictionary without converting
338 if(*aWordBuf)
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 );
347 if (pStream->eof())
348 break;
349 if (ERRCODE_NONE != (nErr = pStream->GetError()))
350 return nErr;
352 if (nLen < BUFSIZE)
354 pStream->ReadBytes(aWordBuf, nLen);
355 if (ERRCODE_NONE != (nErr = pStream->GetError()))
356 return nErr;
358 else
359 return SVSTREAM_READ_ERROR;
360 *(aWordBuf + nLen) = 0;
363 else if (DIC_VERSION_7 == nDicVersion)
365 OString aLine;
367 // remaining lines - stock strings (a [==] b)
368 while (pStream->ReadLine(aLine))
370 if (aLine.isEmpty() || aLine[0] == '#') // skip comments
371 continue;
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
384 bIsModified = false;
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() );
405 if (rURL.isEmpty())
406 return ERRCODE_NONE;
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" );
421 if (!xStream.is())
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)
432 return nErr;
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>");
438 else
440 OString aLine = "lang: " + OUStringToOString(LanguageTag::convertToBcp47(nLanguage), eEnc);
441 pStream->WriteLine(aLine);
443 if (ERRCODE_NONE != (nErr = pStream->GetError()))
444 return nErr;
445 if (eDicType == DictionaryType_POSITIVE)
446 pStream->WriteLine("type: positive");
447 else
448 pStream->WriteLine("type: negative");
449 if (aDicName.endsWith(EXTENSION_FOR_TITLE_TEXT))
451 pStream->WriteLine(OStringConcatenation("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()))
456 return nErr;
457 pStream->WriteLine("---");
458 if (ERRCODE_NONE != (nErr = pStream->GetError()))
459 return nErr;
460 for (const Reference<XDictionaryEntry> & aEntrie : aEntries)
462 OString aOutStr = formatForSave(aEntrie, eEnc);
463 pStream->WriteLine (aOutStr);
464 if (ERRCODE_NONE != (nErr = pStream->GetError()))
465 return nErr;
470 pStream.reset();
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);
474 xSeek->seek(0);
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));
486 return nErr;
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,
504 bool bSimilarOnly)
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
512 int nRes = 0;
514 sal_Int32 nLen1 = rWord1.getLength(),
515 nLen2 = rWord2.getLength();
516 if (bSimilarOnly)
518 const sal_Unicode cChar = '.';
519 if (nLen1 && cChar == rWord1[ nLen1 - 1 ])
520 nLen1--;
521 if (nLen2 && cChar == rWord2[ nLen2 - 1 ])
522 nLen2--;
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)
528 sal_Int32 nIdx1 = 0,
529 nIdx2 = 0,
530 nNumIgnChar1 = 0,
531 nNumIgnChar2 = 0;
533 bool IgnState;
534 sal_Int32 nDiff = 0;
535 sal_Unicode cChar1 = '\0';
536 sal_Unicode cChar2 = '\0';
539 // skip chars to be ignored
540 IgnState = false;
541 while (nIdx1 < nLen1)
543 cChar1 = rWord1[ nIdx1 ];
544 if (cChar1 != cIgnChar && cChar1 != cIgnBeg && !IgnState )
545 break;
546 if ( cChar1 == cIgnBeg )
547 IgnState = true;
548 else if (cChar1 == cIgnEnd)
549 IgnState = false;
550 nIdx1++;
551 nNumIgnChar1++;
553 IgnState = false;
554 while (nIdx2 < nLen2)
556 cChar2 = rWord2[ nIdx2 ];
557 if (cChar2 != cIgnChar && cChar2 != cIgnBeg && !IgnState )
558 break;
559 if ( cChar2 == cIgnBeg )
560 IgnState = true;
561 else if (cChar2 == cIgnEnd)
562 IgnState = false;
563 nIdx2++;
564 nNumIgnChar2++;
567 if (nIdx1 < nLen1 && nIdx2 < nLen2)
569 nDiff = cChar1 - cChar2;
570 if (nDiff)
571 break;
572 nIdx1++;
573 nIdx2++;
575 } while (nIdx1 < nLen1 && nIdx2 < nLen2);
578 if (nDiff)
579 nRes = nDiff;
580 else
581 { // the string with the smallest count of not ignored chars is the
582 // shorter one
584 // count remaining IgnChars
585 IgnState = false;
586 while (nIdx1 < nLen1 )
588 if (rWord1[ nIdx1 ] == cIgnBeg)
589 IgnState = true;
590 if (IgnState || rWord1[ nIdx1 ] == cIgnChar)
591 nNumIgnChar1++;
592 if (rWord1[ nIdx1] == cIgnEnd)
593 IgnState = false;
594 nIdx1++;
596 IgnState = false;
597 while (nIdx2 < nLen2 )
599 if (rWord2[ nIdx2 ] == cIgnBeg)
600 IgnState = true;
601 if (IgnState || rWord2[ nIdx2 ] == cIgnChar)
602 nNumIgnChar2++;
603 if (rWord2[ nIdx2 ] == cIgnEnd)
604 IgnState = false;
605 nIdx2++;
608 nRes = (nLen1 - nNumIgnChar1) - (nLen2 - nNumIgnChar2);
611 return nRes;
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(),
626 nMidIdx,
627 nLowerIdx = 0;
628 if( nUpperIdx > 0 )
630 nUpperIdx--;
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 );
638 if(nCmp == 0)
640 if( pPos ) *pPos = nMidIdx;
641 return true;
643 else if(nCmp > 0)
644 nLowerIdx = nMidIdx + 1;
645 else if( nMidIdx == 0 )
647 if( pPos ) *pPos = nLowerIdx;
648 return false;
650 else
651 nUpperIdx = nMidIdx - 1;
654 if( pPos ) *pPos = nLowerIdx;
655 return false;
658 bool DictionaryNeo::isSorted()
660 bool bRes = true;
662 sal_Int32 nEntries = getCount();
663 sal_Int32 i;
664 for (i = 1; i < nEntries; i++)
666 if (cmpDicEntry( aEntries[i-1]->getDictionaryWord(),
667 aEntries[i]->getDictionaryWord() ) > 0)
669 bRes = false;
670 break;
673 return bRes;
676 bool DictionaryNeo::addEntry_Impl(const uno::Reference< XDictionaryEntry >& xDicEntry,
677 bool bIsLoadEntries)
679 MutexGuard aGuard( GetLinguMutex() );
681 bool bRes = false;
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
693 sal_Int32 nPos = 0;
694 if (bAddEntry)
696 const bool bFound = seekEntry( xDicEntry->getDictionaryWord(), &nPos );
697 if (bFound)
698 bAddEntry = false;
701 if (bAddEntry)
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");
709 bIsModified = true;
710 bRes = true;
712 if (!bIsLoadEntries)
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 );
732 bRes = true;
733 } else
734 bRes = false;
738 return bRes;
741 OUString SAL_CALL DictionaryNeo::getName( )
743 MutexGuard aGuard( GetLinguMutex() );
744 return aDicName;
747 void SAL_CALL DictionaryNeo::setName( const OUString& aName )
749 MutexGuard aGuard( GetLinguMutex() );
751 if (aDicName != aName)
753 aDicName = aName;
754 launchEvent(DictionaryEventFlags::CHG_NAME, nullptr);
758 DictionaryType SAL_CALL DictionaryNeo::getDictionaryType( )
760 MutexGuard aGuard( GetLinguMutex() );
762 return eDicType;
765 void SAL_CALL DictionaryNeo::setActive( sal_Bool bActivate )
767 MutexGuard aGuard( GetLinguMutex() );
769 if (bIsActive == bool(bActivate))
770 return;
772 bIsActive = bActivate;
773 sal_Int16 nEvent = bIsActive ?
774 DictionaryEventFlags::ACTIVATE_DIC : DictionaryEventFlags::DEACTIVATE_DIC;
776 // remove entries from memory if dictionary is deactivated
777 if (!bIsActive)
779 bool bIsEmpty = aEntries.empty();
781 // save entries first if necessary
782 if (bIsModified && hasLocation() && !isReadonly())
784 store();
786 aEntries.clear();
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() );
799 return bIsActive;
802 sal_Int32 SAL_CALL DictionaryNeo::getCount( )
804 MutexGuard aGuard( GetLinguMutex() );
806 if (bNeedEntries)
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() );
835 if (bNeedEntries)
836 loadEntries( aMainURL );
838 sal_Int32 nPos;
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() );
851 bool bRes = false;
853 if (!bIsReadonly)
855 if (bNeedEntries)
856 loadEntries( aMainURL );
857 bRes = addEntry_Impl( xDicEntry );
860 return bRes;
863 sal_Bool SAL_CALL
864 DictionaryNeo::add( const OUString& rWord, sal_Bool bIsNegative,
865 const OUString& rRplcText )
867 MutexGuard aGuard( GetLinguMutex() );
869 bool bRes = false;
871 if (!bIsReadonly)
873 uno::Reference< XDictionaryEntry > xEntry =
874 new DicEntry( rWord, bIsNegative, rRplcText );
875 bRes = addEntry_Impl( xEntry );
878 return bRes;
881 sal_Bool SAL_CALL DictionaryNeo::remove( const OUString& aWord )
883 MutexGuard aGuard( GetLinguMutex() );
885 bool bRemoved = false;
887 if (!bIsReadonly)
889 if (bNeedEntries)
890 loadEntries( aMainURL );
892 sal_Int32 nPos;
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
897 if (bFound)
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 );
912 return bRemoved;
915 sal_Bool SAL_CALL DictionaryNeo::isFull( )
917 MutexGuard aGuard( GetLinguMutex() );
919 if (bNeedEntries)
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() );
929 if (bNeedEntries)
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
942 aEntries.clear();
944 bNeedEntries = false;
945 bIsModified = true;
947 launchEvent( DictionaryEventFlags::ENTRIES_CLEARED , nullptr );
951 sal_Bool SAL_CALL DictionaryNeo::addDictionaryEventListener(
952 const uno::Reference< XDictionaryEventListener >& xListener )
954 MutexGuard aGuard( GetLinguMutex() );
956 bool bRes = false;
957 if (xListener.is())
959 sal_Int32 nLen = aDicEvtListeners.getLength();
960 bRes = aDicEvtListeners.addInterface( xListener ) != nLen;
962 return bRes;
965 sal_Bool SAL_CALL DictionaryNeo::removeDictionaryEventListener(
966 const uno::Reference< XDictionaryEventListener >& xListener )
968 MutexGuard aGuard( GetLinguMutex() );
970 bool bRes = false;
971 if (xListener.is())
973 sal_Int32 nLen = aDicEvtListeners.getLength();
974 bRes = aDicEvtListeners.removeInterface( xListener ) != nLen;
976 return bRes;
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() );
989 return aMainURL;
992 sal_Bool SAL_CALL DictionaryNeo::isReadonly()
994 MutexGuard aGuard( GetLinguMutex() );
996 return bIsReadonly;
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 ))
1018 aMainURL = 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() );
1029 saveEntries(aURL);
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,
1054 OUString &rDicWord,
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 ] == '=' )
1065 ++nDelimPos;
1066 rDicWord = rDicFileWord.copy( 0, nDelimPos );
1067 rReplacement = rDicFileWord.copy( nDelimPos + 2 );
1069 else
1071 rDicWord = rDicFileWord;
1072 rReplacement.clear();
1076 OUString SAL_CALL DicEntry::getDictionaryWord( )
1078 MutexGuard aGuard( GetLinguMutex() );
1079 return aDicWord;
1082 sal_Bool SAL_CALL DicEntry::isNegative( )
1084 MutexGuard aGuard( GetLinguMutex() );
1085 return bIsNegativ;
1088 OUString SAL_CALL DicEntry::getReplacementText( )
1090 MutexGuard aGuard( GetLinguMutex() );
1091 return aReplacement;
1095 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */