1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <cppuhelper/factory.hxx>
#include <i18nlangtag/mslangid.hxx>
#include <osl/file.hxx>
#include <tools/debug.hxx>
#include <tools/stream.hxx>
#include <tools/urlobj.hxx>
#include <unotools/pathoptions.hxx>
#include <unotools/useroptions.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <unotools/localfilehelper.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/sequence.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/lang/Locale.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <com/sun/star/uno/Reference.h>
#include <com/sun/star/linguistic2/DictionaryEventFlags.hpp>
#include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
#include <com/sun/star/registry/XRegistryKey.hpp>
#include <com/sun/star/ucb/SimpleFileAccess.hpp>
#include <svtools/strings.hrc>
#include <unotools/resmgr.hxx>
#include <sal/log.hxx>
#include "defs.hxx"
#include "dlistimp.hxx"
#include "dicimp.hxx"
#include "lngopt.hxx"
#include "lngreg.hxx"
using namespace osl;
using namespace com::sun::star;
using namespace com::sun::star::lang;
using namespace com::sun::star::uno;
using namespace com::sun::star::linguistic2;
using namespace linguistic;
static bool IsVers2OrNewer( const OUString& rFileURL, LanguageType& nLng, bool& bNeg, OUString& aDicName );
static void AddInternal( const uno::Reference< XDictionary > &rDic,
const OUString& rNew );
static void AddUserData( const uno::Reference< XDictionary > &rDic );
class DicEvtListenerHelper :
public cppu::WeakImplHelper
<
XDictionaryEventListener
>
{
comphelper::OInterfaceContainerHelper2 aDicListEvtListeners;
uno::Reference< XDictionaryList > xMyDicList;
sal_Int16 nCondensedEvt;
sal_Int16 nNumCollectEvtListeners;
public:
explicit DicEvtListenerHelper( const uno::Reference< XDictionaryList > &rxDicList );
virtual ~DicEvtListenerHelper() override;
// XEventListener
virtual void SAL_CALL
disposing( const EventObject& rSource ) override;
// XDictionaryEventListener
virtual void SAL_CALL
processDictionaryEvent( const DictionaryEvent& rDicEvent ) override;
// non-UNO functions
void DisposeAndClear( const EventObject &rEvtObj );
bool AddDicListEvtListener(
const uno::Reference< XDictionaryListEventListener >& rxListener );
bool RemoveDicListEvtListener(
const uno::Reference< XDictionaryListEventListener >& rxListener );
sal_Int16 BeginCollectEvents() { return ++nNumCollectEvtListeners;}
sal_Int16 EndCollectEvents();
sal_Int16 FlushEvents();
void ClearEvents() { nCondensedEvt = 0; }
};
DicEvtListenerHelper::DicEvtListenerHelper(
const uno::Reference< XDictionaryList > &rxDicList ) :
aDicListEvtListeners ( GetLinguMutex() ),
xMyDicList ( rxDicList )
{
nCondensedEvt = 0;
nNumCollectEvtListeners = 0;
}
DicEvtListenerHelper::~DicEvtListenerHelper()
{
DBG_ASSERT(aDicListEvtListeners.getLength() == 0,
"lng : event listeners are still existing");
}
void DicEvtListenerHelper::DisposeAndClear( const EventObject &rEvtObj )
{
aDicListEvtListeners.disposeAndClear( rEvtObj );
}
void SAL_CALL DicEvtListenerHelper::disposing( const EventObject& rSource )
{
osl::MutexGuard aGuard( GetLinguMutex() );
uno::Reference< XInterface > xSrc( rSource.Source );
// remove event object from EventListener list
if (xSrc.is())
aDicListEvtListeners.removeInterface( xSrc );
// if object is a dictionary then remove it from the dictionary list
// Note: this will probably happen only if someone makes a XDictionary
// implementation of his own that is also a XComponent.
uno::Reference< XDictionary > xDic( xSrc, UNO_QUERY );
if (xDic.is())
{
xMyDicList->removeDictionary( xDic );
}
}
void SAL_CALL DicEvtListenerHelper::processDictionaryEvent(
const DictionaryEvent& rDicEvent )
{
osl::MutexGuard aGuard( GetLinguMutex() );
uno::Reference< XDictionary > xDic( rDicEvent.Source, UNO_QUERY );
DBG_ASSERT(xDic.is(), "lng : missing event source");
// assert that there is a corresponding dictionary entry if one was
// added or deleted
uno::Reference< XDictionaryEntry > xDicEntry( rDicEvent.xDictionaryEntry, UNO_QUERY );
DBG_ASSERT( !(rDicEvent.nEvent &
(DictionaryEventFlags::ADD_ENTRY | DictionaryEventFlags::DEL_ENTRY))
|| xDicEntry.is(),
"lng : missing dictionary entry" );
// evaluate DictionaryEvents and update data for next DictionaryListEvent
DictionaryType eDicType = xDic->getDictionaryType();
DBG_ASSERT(eDicType != DictionaryType_MIXED,
"lng : unexpected dictionary type");
if ((rDicEvent.nEvent & DictionaryEventFlags::ADD_ENTRY) && xDic->isActive())
nCondensedEvt |= xDicEntry->isNegative() ?
DictionaryListEventFlags::ADD_NEG_ENTRY :
DictionaryListEventFlags::ADD_POS_ENTRY;
if ((rDicEvent.nEvent & DictionaryEventFlags::DEL_ENTRY) && xDic->isActive())
nCondensedEvt |= xDicEntry->isNegative() ?
DictionaryListEventFlags::DEL_NEG_ENTRY :
DictionaryListEventFlags::DEL_POS_ENTRY;
if ((rDicEvent.nEvent & DictionaryEventFlags::ENTRIES_CLEARED) && xDic->isActive())
nCondensedEvt |= eDicType == DictionaryType_NEGATIVE ?
DictionaryListEventFlags::DEL_NEG_ENTRY :
DictionaryListEventFlags::DEL_POS_ENTRY;
if ((rDicEvent.nEvent & DictionaryEventFlags::CHG_LANGUAGE) && xDic->isActive())
nCondensedEvt |= eDicType == DictionaryType_NEGATIVE ?
DictionaryListEventFlags::DEACTIVATE_NEG_DIC
| DictionaryListEventFlags::ACTIVATE_NEG_DIC :
DictionaryListEventFlags::DEACTIVATE_POS_DIC
| DictionaryListEventFlags::ACTIVATE_POS_DIC;
if (rDicEvent.nEvent & DictionaryEventFlags::ACTIVATE_DIC)
nCondensedEvt |= eDicType == DictionaryType_NEGATIVE ?
DictionaryListEventFlags::ACTIVATE_NEG_DIC :
DictionaryListEventFlags::ACTIVATE_POS_DIC;
if (rDicEvent.nEvent & DictionaryEventFlags::DEACTIVATE_DIC)
nCondensedEvt |= eDicType == DictionaryType_NEGATIVE ?
DictionaryListEventFlags::DEACTIVATE_NEG_DIC :
DictionaryListEventFlags::DEACTIVATE_POS_DIC;
if (nNumCollectEvtListeners == 0 && nCondensedEvt != 0)
FlushEvents();
}
bool DicEvtListenerHelper::AddDicListEvtListener(
const uno::Reference< XDictionaryListEventListener >& xListener )
{
DBG_ASSERT( xListener.is(), "empty reference" );
sal_Int32 nCount = aDicListEvtListeners.getLength();
return aDicListEvtListeners.addInterface( xListener ) != nCount;
}
bool DicEvtListenerHelper::RemoveDicListEvtListener(
const uno::Reference< XDictionaryListEventListener >& xListener )
{
DBG_ASSERT( xListener.is(), "empty reference" );
sal_Int32 nCount = aDicListEvtListeners.getLength();
return aDicListEvtListeners.removeInterface( xListener ) != nCount;
}
sal_Int16 DicEvtListenerHelper::EndCollectEvents()
{
DBG_ASSERT(nNumCollectEvtListeners > 0, "lng: mismatched function call");
if (nNumCollectEvtListeners > 0)
{
FlushEvents();
nNumCollectEvtListeners--;
}
return nNumCollectEvtListeners;
}
sal_Int16 DicEvtListenerHelper::FlushEvents()
{
if (0 != nCondensedEvt)
{
// build DictionaryListEvent to pass on to listeners
uno::Sequence< DictionaryEvent > aDicEvents;
DictionaryListEvent aEvent( xMyDicList, nCondensedEvt, aDicEvents );
// pass on event
aDicListEvtListeners.notifyEach( &XDictionaryListEventListener::processDictionaryListEvent, aEvent );
// clear "list" of events
nCondensedEvt = 0;
}
return nNumCollectEvtListeners;
}
void DicList::MyAppExitListener::AtExit()
{
rMyDicList.SaveDics();
}
DicList::DicList() :
aEvtListeners ( GetLinguMutex() )
{
mxDicEvtLstnrHelper = new DicEvtListenerHelper( this );
bDisposing = false;
bInCreation = false;
mxExitListener = new MyAppExitListener( *this );
mxExitListener->Activate();
}
DicList::~DicList()
{
mxExitListener->Deactivate();
}
void DicList::SearchForDictionaries(
DictionaryVec_t&rDicList,
const OUString &rDicDirURL,
bool bIsWriteablePath )
{
osl::MutexGuard aGuard( GetLinguMutex() );
const uno::Sequence< OUString > aDirCnt( utl::LocalFileHelper::
GetFolderContents( rDicDirURL, false ) );
const OUString *pDirCnt = aDirCnt.getConstArray();
sal_Int32 nEntries = aDirCnt.getLength();
for (sal_Int32 i = 0; i < nEntries; ++i)
{
OUString aURL( pDirCnt[i] );
LanguageType nLang = LANGUAGE_NONE;
bool bNeg = false;
OUString aDicTitle = "";
if(!::IsVers2OrNewer( aURL, nLang, bNeg, aDicTitle ))
{
// When not
sal_Int32 nPos = aURL.indexOf('.');
OUString aExt( aURL.copy(nPos + 1).toAsciiLowerCase() );
if ("dcn" == aExt) // negative
bNeg = true;
else if ("dcp" == aExt) // positive
bNeg = false;
else
continue; // other files
}
// Record in the list of Dictionaries
// When it already exists don't record
LanguageType nSystemLanguage = MsLangId::getSystemLanguage();
OUString aTmp1 = ToLower( aURL, nSystemLanguage );
sal_Int32 nPos = aTmp1.lastIndexOf( '/' );
if (-1 != nPos)
aTmp1 = aTmp1.copy( nPos + 1 );
OUString aTmp2;
size_t j;
size_t nCount = rDicList.size();
for(j = 0; j < nCount; j++)
{
aTmp2 = rDicList[j]->getName();
aTmp2 = ToLower( aTmp2, nSystemLanguage );
if(aTmp1 == aTmp2)
break;
}
if(j >= nCount) // dictionary not yet in DicList
{
// get decoded dictionary file name
INetURLObject aURLObj( aURL );
OUString aDicName = aURLObj.getName( INetURLObject::LAST_SEGMENT,
true, INetURLObject::DecodeMechanism::WithCharset );
DictionaryType eType = bNeg ? DictionaryType_NEGATIVE : DictionaryType_POSITIVE;
uno::Reference< XDictionary > xDic =
new DictionaryNeo( aDicTitle.isEmpty() ? aDicName : aDicTitle, nLang, eType, aURL, bIsWriteablePath );
addDictionary( xDic );
nCount++;
}
}
}
sal_Int32 DicList::GetDicPos(const uno::Reference< XDictionary > &xDic)
{
osl::MutexGuard aGuard( GetLinguMutex() );
DictionaryVec_t& rDicList = GetOrCreateDicList();
size_t n = rDicList.size();
for (size_t i = 0; i < n; i++)
{
if ( rDicList[i] == xDic )
return i;
}
return -1;
}
/// @throws Exception
static uno::Reference< XInterface >
DicList_CreateInstance( const uno::Reference< XMultiServiceFactory > & /*rSMgr*/ )
{
uno::Reference< XInterface > xService = static_cast<cppu::OWeakObject *>(new DicList);
return xService;
}
sal_Int16 SAL_CALL DicList::getCount()
{
osl::MutexGuard aGuard( GetLinguMutex() );
return static_cast< sal_Int16 >(GetOrCreateDicList().size());
}
uno::Sequence< uno::Reference< XDictionary > > SAL_CALL
DicList::getDictionaries()
{
osl::MutexGuard aGuard( GetLinguMutex() );
DictionaryVec_t& rDicList = GetOrCreateDicList();
return comphelper::containerToSequence(rDicList);
}
uno::Reference< XDictionary > SAL_CALL
DicList::getDictionaryByName( const OUString& aDictionaryName )
{
osl::MutexGuard aGuard( GetLinguMutex() );
uno::Reference< XDictionary > xDic;
DictionaryVec_t& rDicList = GetOrCreateDicList();
size_t nCount = rDicList.size();
for (size_t i = 0; i < nCount; i++)
{
const uno::Reference< XDictionary > &rDic = rDicList[i];
if (rDic.is() && rDic->getName() == aDictionaryName)
{
xDic = rDic;
break;
}
}
return xDic;
}
sal_Bool SAL_CALL DicList::addDictionary(
const uno::Reference< XDictionary >& xDictionary )
{
osl::MutexGuard aGuard( GetLinguMutex() );
if (bDisposing)
return false;
bool bRes = false;
if (xDictionary.is())
{
DictionaryVec_t& rDicList = GetOrCreateDicList();
rDicList.push_back( xDictionary );
bRes = true;
// add listener helper to the dictionaries listener lists
xDictionary->addDictionaryEventListener( mxDicEvtLstnrHelper.get() );
}
return bRes;
}
sal_Bool SAL_CALL
DicList::removeDictionary( const uno::Reference< XDictionary >& xDictionary )
{
osl::MutexGuard aGuard( GetLinguMutex() );
if (bDisposing)
return false;
bool bRes = false;
sal_Int32 nPos = GetDicPos( xDictionary );
if (nPos >= 0)
{
// remove dictionary list from the dictionaries listener lists
DictionaryVec_t& rDicList = GetOrCreateDicList();
uno::Reference< XDictionary > xDic( rDicList[ nPos ] );
DBG_ASSERT(xDic.is(), "lng : empty reference");
if (xDic.is())
{
// deactivate dictionary if not already done
xDic->setActive( false );
xDic->removeDictionaryEventListener( mxDicEvtLstnrHelper.get() );
}
// remove element at nPos
rDicList.erase( rDicList.begin() + nPos );
bRes = true;
}
return bRes;
}
sal_Bool SAL_CALL DicList::addDictionaryListEventListener(
const uno::Reference< XDictionaryListEventListener >& xListener,
sal_Bool bReceiveVerbose )
{
osl::MutexGuard aGuard( GetLinguMutex() );
if (bDisposing)
return false;
DBG_ASSERT(!bReceiveVerbose, "lng : not yet supported");
bool bRes = false;
if (xListener.is()) //! don't add empty references
{
bRes = mxDicEvtLstnrHelper->AddDicListEvtListener( xListener );
}
return bRes;
}
sal_Bool SAL_CALL DicList::removeDictionaryListEventListener(
const uno::Reference< XDictionaryListEventListener >& xListener )
{
osl::MutexGuard aGuard( GetLinguMutex() );
if (bDisposing)
return false;
bool bRes = false;
if(xListener.is())
{
bRes = mxDicEvtLstnrHelper->RemoveDicListEvtListener( xListener );
}
return bRes;
}
sal_Int16 SAL_CALL DicList::beginCollectEvents()
{
osl::MutexGuard aGuard( GetLinguMutex() );
return mxDicEvtLstnrHelper->BeginCollectEvents();
}
sal_Int16 SAL_CALL DicList::endCollectEvents()
{
osl::MutexGuard aGuard( GetLinguMutex() );
return mxDicEvtLstnrHelper->EndCollectEvents();
}
sal_Int16 SAL_CALL DicList::flushEvents()
{
osl::MutexGuard aGuard( GetLinguMutex() );
return mxDicEvtLstnrHelper->FlushEvents();
}
uno::Reference< XDictionary > SAL_CALL
DicList::createDictionary( const OUString& rName, const Locale& rLocale,
DictionaryType eDicType, const OUString& rURL )
{
osl::MutexGuard aGuard( GetLinguMutex() );
LanguageType nLanguage = LinguLocaleToLanguage( rLocale );
bool bIsWriteablePath = rURL.match( GetDictionaryWriteablePath() );
return new DictionaryNeo( rName, nLanguage, eDicType, rURL, bIsWriteablePath );
}
uno::Reference< XDictionaryEntry > SAL_CALL
DicList::queryDictionaryEntry( const OUString& rWord, const Locale& rLocale,
sal_Bool bSearchPosDics, sal_Bool bSearchSpellEntry )
{
osl::MutexGuard aGuard( GetLinguMutex() );
return SearchDicList( this, rWord, LinguLocaleToLanguage( rLocale ),
bSearchPosDics, bSearchSpellEntry );
}
void SAL_CALL
DicList::dispose()
{
osl::MutexGuard aGuard( GetLinguMutex() );
if (!bDisposing)
{
bDisposing = true;
EventObject aEvtObj( static_cast<XDictionaryList *>(this) );
aEvtListeners.disposeAndClear( aEvtObj );
if (mxDicEvtLstnrHelper.is())
mxDicEvtLstnrHelper->DisposeAndClear( aEvtObj );
//! avoid creation of dictionaries if not already done
if ( !aDicList.empty() )
{
DictionaryVec_t& rDicList = GetOrCreateDicList();
size_t nCount = rDicList.size();
for (size_t i = 0; i < nCount; i++)
{
uno::Reference< XDictionary > xDic( rDicList[i], UNO_QUERY );
// save (modified) dictionaries
uno::Reference< frame::XStorable > xStor( xDic , UNO_QUERY );
if (xStor.is())
{
try
{
if (!xStor->isReadonly() && xStor->hasLocation())
xStor->store();
}
catch(Exception &)
{
}
}
// release references to (members of) this object hold by
// dictionaries
if (xDic.is())
xDic->removeDictionaryEventListener( mxDicEvtLstnrHelper.get() );
}
}
mxDicEvtLstnrHelper.clear();
}
}
void SAL_CALL
DicList::addEventListener( const uno::Reference< XEventListener >& rxListener )
{
osl::MutexGuard aGuard( GetLinguMutex() );
if (!bDisposing && rxListener.is())
aEvtListeners.addInterface( rxListener );
}
void SAL_CALL
DicList::removeEventListener( const uno::Reference< XEventListener >& rxListener )
{
osl::MutexGuard aGuard( GetLinguMutex() );
if (!bDisposing && rxListener.is())
aEvtListeners.removeInterface( rxListener );
}
void DicList::CreateDicList()
{
bInCreation = true;
// look for dictionaries
const OUString aWriteablePath( GetDictionaryWriteablePath() );
std::vector< OUString > aPaths( GetDictionaryPaths() );
for (OUString & aPath : aPaths)
{
const bool bIsWriteablePath = (aPath == aWriteablePath);
SearchForDictionaries( aDicList, aPath, bIsWriteablePath );
}
// create IgnoreAllList dictionary with empty URL (non persistent)
// and add it to list
std::locale loc(Translate::Create("svt"));
uno::Reference< XDictionary > xIgnAll(
createDictionary( Translate::get(STR_DESCRIPTION_IGNOREALLLIST, loc), LinguLanguageToLocale( LANGUAGE_NONE ),
DictionaryType_POSITIVE, OUString() ) );
if (xIgnAll.is())
{
AddUserData( xIgnAll );
xIgnAll->setActive( true );
addDictionary( xIgnAll );
}
// evaluate list of dictionaries to be activated from configuration
//! to suppress overwriting the list of active dictionaries in the
//! configuration with incorrect arguments during the following
//! activation of the dictionaries
mxDicEvtLstnrHelper->BeginCollectEvents();
const uno::Sequence< OUString > aActiveDics( aOpt.GetActiveDics() );
const OUString *pActiveDic = aActiveDics.getConstArray();
sal_Int32 nLen = aActiveDics.getLength();
for (sal_Int32 i = 0; i < nLen; ++i)
{
if (!pActiveDic[i].isEmpty())
{
uno::Reference< XDictionary > xDic( getDictionaryByName( pActiveDic[i] ) );
if (xDic.is())
xDic->setActive( true );
}
}
// suppress collected events during creation of the dictionary list.
// there should be no events during creation.
mxDicEvtLstnrHelper->ClearEvents();
mxDicEvtLstnrHelper->EndCollectEvents();
bInCreation = false;
}
void DicList::SaveDics()
{
// save dics only if they have already been used/created.
//! don't create them just for the purpose of saving them !
if ( !aDicList.empty() )
{
// save (modified) dictionaries
DictionaryVec_t& rDicList = GetOrCreateDicList();
size_t nCount = rDicList.size();
for (size_t i = 0; i < nCount; i++)
{
// save (modified) dictionaries
uno::Reference< frame::XStorable > xStor( rDicList[i], UNO_QUERY );
if (xStor.is())
{
try
{
if (!xStor->isReadonly() && xStor->hasLocation())
xStor->store();
}
catch(Exception &)
{
}
}
}
}
}
// Service specific part
OUString SAL_CALL DicList::getImplementationName( )
{
return getImplementationName_Static();
}
sal_Bool SAL_CALL DicList::supportsService( const OUString& ServiceName )
{
return cppu::supportsService(this, ServiceName);
}
uno::Sequence< OUString > SAL_CALL DicList::getSupportedServiceNames( )
{
return getSupportedServiceNames_Static();
}
uno::Sequence< OUString > DicList::getSupportedServiceNames_Static() throw()
{
uno::Sequence< OUString > aSNS { "com.sun.star.linguistic2.DictionaryList" };
return aSNS;
}
void * DicList_getFactory( const sal_Char * pImplName,
XMultiServiceFactory * pServiceManager )
{
void * pRet = nullptr;
if ( DicList::getImplementationName_Static().equalsAscii( pImplName ) )
{
uno::Reference< XSingleServiceFactory > xFactory =
cppu::createOneInstanceFactory(
pServiceManager,
DicList::getImplementationName_Static(),
DicList_CreateInstance,
DicList::getSupportedServiceNames_Static());
// acquire, because we return an interface pointer instead of a reference
xFactory->acquire();
pRet = xFactory.get();
}
return pRet;
}
static sal_Int32 lcl_GetToken( OUString &rToken,
const OUString &rText, sal_Int32 nPos, const OUString &rDelim )
{
sal_Int32 nRes = -1;
if (rText.isEmpty() || nPos >= rText.getLength())
rToken.clear();
else if (rDelim.isEmpty())
{
rToken = rText;
if (!rToken.isEmpty())
nRes = rText.getLength();
}
else
{
sal_Int32 i;
for (i = nPos; i < rText.getLength(); ++i)
{
if (-1 != rDelim.indexOf( rText[i] ))
break;
}
if (i >= rText.getLength()) // delimiter not found
rToken = rText.copy( nPos );
else
rToken = rText.copy( nPos, i - nPos );
nRes = i + 1; // continue after found delimiter
}
return nRes;
}
static void AddInternal(
const uno::Reference<XDictionary> &rDic,
const OUString& rNew )
{
if (rDic.is())
{
//! TL TODO: word iterator should be used to break up the text
OUString aDelim("!\"#$%&'()*+,-/:;<=>?[]\\_^`{|}~\t \n");
OSL_ENSURE(aDelim.indexOf(u'.') == -1,
"ensure no '.'");
OUString aToken;
sal_Int32 nPos = 0;
while (-1 != (nPos = lcl_GetToken( aToken, rNew, nPos, aDelim )))
{
if( !aToken.isEmpty() && !IsNumeric( aToken ) )
{
rDic->add( aToken, false, OUString() );
}
}
}
}
static void AddUserData( const uno::Reference< XDictionary > &rDic )
{
if (rDic.is())
{
SvtUserOptions aUserOpt;
AddInternal( rDic, aUserOpt.GetFullName() );
AddInternal( rDic, aUserOpt.GetCompany() );
AddInternal( rDic, aUserOpt.GetStreet() );
AddInternal( rDic, aUserOpt.GetCity() );
AddInternal( rDic, aUserOpt.GetTitle() );
AddInternal( rDic, aUserOpt.GetPosition() );
AddInternal( rDic, aUserOpt.GetEmail() );
}
}
static bool IsVers2OrNewer( const OUString& rFileURL, LanguageType& nLng, bool& bNeg, OUString& aDicName )
{
if (rFileURL.isEmpty())
return false;
OUString aExt;
sal_Int32 nPos = rFileURL.lastIndexOf( '.' );
if (-1 != nPos)
aExt = rFileURL.copy( nPos + 1 ).toAsciiLowerCase();
if (aExt != "dic")
return false;
// get stream to be used
uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
// get XInputStream stream
uno::Reference< io::XInputStream > xStream;
try
{
uno::Reference< ucb::XSimpleFileAccess3 > xAccess( ucb::SimpleFileAccess::create(xContext) );
xStream = xAccess->openFileRead( rFileURL );
}
catch (const uno::Exception &)
{
SAL_WARN( "linguistic", "failed to get input stream" );
}
DBG_ASSERT( xStream.is(), "failed to get stream for read" );
if (!xStream.is())
return false;
SvStreamPtr pStream = SvStreamPtr( utl::UcbStreamHelper::CreateStream( xStream ) );
int nDicVersion = ReadDicVersion(pStream, nLng, bNeg, aDicName);
return 2 == nDicVersion || nDicVersion >= 5;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|