xref: /trunk/main/editeng/source/misc/unolingu.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_editeng.hxx"
26 
27 #include <map>
28 #include <set>
29 #include <vector>
30 #include <slist>
31 #include <memory>
32 #include <editeng/unolingu.hxx>
33 #include <tools/debug.hxx>
34 #include <tools/urlobj.hxx>
35 #include <rtl/logfile.hxx>
36 #include <unotools/pathoptions.hxx>
37 #include <com/sun/star/frame/XModel.hpp>
38 #include <com/sun/star/frame/XStorable.hpp>
39 #include <com/sun/star/lang/XEventListener.hpp>
40 #include <com/sun/star/linguistic2/XAvailableLocales.hpp>
41 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
42 #include <com/sun/star/ucb/XContentAccess.hpp>
43 #include <com/sun/star/ucb/XSortedDynamicResultSetFactory.hpp>
44 #include <com/sun/star/ucb/NumberedSortingInfo.hpp>
45 #include <com/sun/star/ucb/XContentAccess.hpp>
46 #include <com/sun/star/sdbc/XResultSet.hpp>
47 #include <com/sun/star/sdbc/XRow.hpp>
48 #include <com/sun/star/util/DateTime.hpp>
49 
50 #include <comphelper/processfactory.hxx>
51 #include <cppuhelper/implbase1.hxx> // helper for implementations
52 #include <i18npool/mslangid.hxx>
53 #include <unotools/lingucfg.hxx>
54 #include <unotools/ucbhelper.hxx>
55 #include <unotools/localfilehelper.hxx>
56 #include <ucbhelper/commandenvironment.hxx>
57 #include <ucbhelper/content.hxx>
58 #include <comphelper/processfactory.hxx>
59 #include <vcl/msgbox.hxx>
60 #include <tools/shl.hxx>
61 #include <linguistic/misc.hxx>
62 #include <editeng/eerdll.hxx>
63 #include <editeng/editrids.hrc>
64 
65 using namespace ::rtl;
66 using namespace ::comphelper;
67 using namespace ::linguistic;
68 using namespace ::com::sun::star;
69 using namespace ::com::sun::star::util;
70 using namespace ::com::sun::star::uno;
71 using namespace ::com::sun::star::lang;
72 using namespace ::com::sun::star::beans;
73 using namespace ::com::sun::star::frame;
74 using namespace ::com::sun::star::linguistic2;
75 
76 #define CSS com::sun::star
77 
78 ///////////////////////////////////////////////////////////////////////////
79 
80 
GetLngSvcMgr_Impl()81 static uno::Reference< XLinguServiceManager > GetLngSvcMgr_Impl()
82 {
83     uno::Reference< XLinguServiceManager > xRes;
84     uno::Reference< XMultiServiceFactory >  xMgr = getProcessServiceFactory();
85     if (xMgr.is())
86     {
87         xRes = uno::Reference< XLinguServiceManager > ( xMgr->createInstance(
88                 OUString( RTL_CONSTASCII_USTRINGPARAM(
89                     "com.sun.star.linguistic2.LinguServiceManager" ) ) ), UNO_QUERY ) ;
90     }
91     return xRes;
92 }
93 
94 ///////////////////////////////////////////////////////////////////////////
95 
lcl_FindEntry(const OUString & rEntry,const Sequence<OUString> & rCfgSvcs)96 sal_Bool lcl_FindEntry( const OUString &rEntry, const Sequence< OUString > &rCfgSvcs )
97 {
98     sal_Int32 nRes = -1;
99     sal_Int32 nEntries = rCfgSvcs.getLength();
100     const OUString *pEntry = rCfgSvcs.getConstArray();
101     for (sal_Int32 i = 0;  i < nEntries && nRes == -1;  ++i)
102     {
103         if (rEntry == pEntry[i])
104             nRes = i;
105     }
106     return nRes != -1;
107 }
108 
109 
lcl_RemoveMissingEntries(const Sequence<OUString> & rCfgSvcs,const Sequence<OUString> & rAvailSvcs)110 Sequence< OUString > lcl_RemoveMissingEntries(
111         const Sequence< OUString > &rCfgSvcs,
112         const Sequence< OUString > &rAvailSvcs )
113 {
114     Sequence< OUString > aRes( rCfgSvcs.getLength() );
115     OUString *pRes = aRes.getArray();
116     sal_Int32 nCnt = 0;
117 
118     sal_Int32 nEntries = rCfgSvcs.getLength();
119     const OUString *pEntry = rCfgSvcs.getConstArray();
120     for (sal_Int32 i = 0;  i < nEntries;  ++i)
121     {
122         if (pEntry[i].getLength() && lcl_FindEntry( pEntry[i], rAvailSvcs ))
123             pRes[ nCnt++ ] = pEntry[i];
124     }
125 
126     aRes.realloc( nCnt );
127     return aRes;
128 }
129 
130 
lcl_GetLastFoundSvcs(SvtLinguConfig & rCfg,const OUString & rLastFoundList,const Locale & rAvailLocale)131 Sequence< OUString > lcl_GetLastFoundSvcs(
132         SvtLinguConfig &rCfg,
133         const OUString &rLastFoundList ,
134         const Locale &rAvailLocale )
135 {
136     Sequence< OUString > aRes;
137 
138     OUString aCfgLocaleStr( MsLangId::convertLanguageToIsoString(
139                                 SvxLocaleToLanguage( rAvailLocale ) ) );
140 
141     Sequence< OUString > aNodeNames( rCfg.GetNodeNames(rLastFoundList) );
142     sal_Bool bFound = lcl_FindEntry( aCfgLocaleStr, aNodeNames);
143 
144     if (bFound)
145     {
146         Sequence< OUString > aNames(1);
147         OUString &rNodeName = aNames.getArray()[0];
148         rNodeName = rLastFoundList;
149         rNodeName += OUString::valueOf( (sal_Unicode)'/' );
150         rNodeName += aCfgLocaleStr;
151         Sequence< Any > aValues( rCfg.GetProperties( aNames ) );
152 #if OSL_DEBUG_LEVEL > 1
153         const Any *pValue;
154         pValue = aValues.getConstArray();
155 #endif
156         if (aValues.getLength())
157         {
158             DBG_ASSERT( aValues.getLength() == 1, "unexpected length of sequence" );
159             Sequence< OUString > aSvcImplNames;
160             if (aValues.getConstArray()[0] >>= aSvcImplNames)
161                 aRes = aSvcImplNames;
162             else
163             {
164                 DBG_ERROR( "type mismatch" );
165             }
166         }
167     }
168 
169     return aRes;
170 }
171 
172 
lcl_GetNewEntries(const Sequence<OUString> & rLastFoundSvcs,const Sequence<OUString> & rAvailSvcs)173 Sequence< OUString > lcl_GetNewEntries(
174         const Sequence< OUString > &rLastFoundSvcs,
175         const Sequence< OUString > &rAvailSvcs )
176 {
177     sal_Int32 nLen = rAvailSvcs.getLength();
178     Sequence< OUString > aRes( nLen );
179     OUString *pRes = aRes.getArray();
180     sal_Int32 nCnt = 0;
181 
182     const OUString *pEntry = rAvailSvcs.getConstArray();
183     for (sal_Int32 i = 0;  i < nLen;  ++i)
184     {
185         if (pEntry[i].getLength() && !lcl_FindEntry( pEntry[i], rLastFoundSvcs ))
186             pRes[ nCnt++ ] = pEntry[i];
187     }
188 
189     aRes.realloc( nCnt );
190     return aRes;
191 }
192 
193 
lcl_MergeSeq(const Sequence<OUString> & rCfgSvcs,const Sequence<OUString> & rNewSvcs)194 Sequence< OUString > lcl_MergeSeq(
195         const Sequence< OUString > &rCfgSvcs,
196         const Sequence< OUString > &rNewSvcs )
197 {
198     Sequence< OUString > aRes( rCfgSvcs.getLength() + rNewSvcs.getLength() );
199     OUString *pRes = aRes.getArray();
200     sal_Int32 nCnt = 0;
201 
202     for (sal_Int32 k = 0;  k < 2;  ++k)
203     {
204         // add previously configured service first and append
205         // new found services at the end
206         const Sequence< OUString > &rSeq = k == 0 ? rCfgSvcs : rNewSvcs;
207 
208         sal_Int32 nLen = rSeq.getLength();
209         const OUString *pEntry = rSeq.getConstArray();
210         for (sal_Int32 i = 0;  i < nLen;  ++i)
211         {
212             if (pEntry[i].getLength() && !lcl_FindEntry( pEntry[i], aRes ))
213                 pRes[ nCnt++ ] = pEntry[i];
214         }
215     }
216 
217     aRes.realloc( nCnt );
218     return aRes;
219 }
220 
221 ///////////////////////////////////////////////////////////////////////////
222 
223 // static member initialization
224 sal_Int16 SvxLinguConfigUpdate::nNeedUpdating = -1;
225 sal_Int32 SvxLinguConfigUpdate::nCurrentDataFilesChangedCheckValue = -1;
226 
UpdateAll(sal_Bool bForceCheck)227 void SvxLinguConfigUpdate::UpdateAll( sal_Bool bForceCheck )
228 {
229     RTL_LOGFILE_CONTEXT( aLog, "svx: SvxLinguConfigUpdate::UpdateAll" );
230 
231     if (IsNeedUpdateAll( bForceCheck ))
232     {
233         typedef OUString OUstring_t;
234         typedef Sequence< OUString > Sequence_OUString_t;
235         typedef std::vector< OUstring_t > OUString_vector_t;
236         typedef std::set< OUstring_t > OUString_set_t;
237         std::vector< OUString_vector_t > aVector;
238         typedef std::map< OUstring_t, Sequence_OUString_t > list_entry_map_t;
239 
240         RTL_LOGFILE_CONTEXT( aLog, "svx: SvxLinguConfigUpdate::UpdateAll - updating..." );
241 
242         DBG_ASSERT( nNeedUpdating == 1, "SvxLinguConfigUpdate::UpdateAll already updated!" );
243 
244         uno::Reference< XLinguServiceManager > xLngSvcMgr( GetLngSvcMgr_Impl() );
245         DBG_ASSERT( xLngSvcMgr.is(), "service manager missing");
246         if (!xLngSvcMgr.is())
247             return;
248 
249         SvtLinguConfig aCfg;
250 
251         const int nNumServices = 4;
252         const sal_Char * apServices[nNumServices]       =  { SN_SPELLCHECKER, SN_GRAMMARCHECKER, SN_HYPHENATOR, SN_THESAURUS };
253         const sal_Char * apCurLists[nNumServices]       =  { "ServiceManager/SpellCheckerList",       "ServiceManager/GrammarCheckerList",       "ServiceManager/HyphenatorList",       "ServiceManager/ThesaurusList" };
254         const sal_Char * apLastFoundLists[nNumServices] =  { "ServiceManager/LastFoundSpellCheckers", "ServiceManager/LastFoundGrammarCheckers", "ServiceManager/LastFoundHyphenators", "ServiceManager/LastFoundThesauri" };
255 
256         // usage of indices as above: 0 = spell checker, 1 = grammar checker, 2 = hyphenator, 3 = thesaurus
257         std::vector< list_entry_map_t > aLastFoundSvcs(nNumServices);
258         std::vector< list_entry_map_t > aCurSvcs(nNumServices);
259 
260         for (int k = 0;  k < nNumServices;  ++k)
261         {
262             OUString aService( A2OU( apServices[k] ) );
263             OUString aActiveList( A2OU( apCurLists[k] ) );
264             OUString aLastFoundList( A2OU( apLastFoundLists[k] ) );
265             sal_Int32 i;
266 
267             //
268             // remove configured but not available language/services entries
269             //
270             Sequence< OUString > aNodeNames( aCfg.GetNodeNames( aActiveList ) );   // list of configured locales
271             sal_Int32 nNodeNames = aNodeNames.getLength();
272             const OUString *pNodeName = aNodeNames.getConstArray();
273             for (i = 0;  i < nNodeNames;  ++i)
274             {
275                 Locale aLocale( SvxCreateLocale( MsLangId::convertIsoStringToLanguage(pNodeName[i]) ) );
276                 Sequence< OUString > aCfgSvcs(
277                         xLngSvcMgr->getConfiguredServices( aService, aLocale ));
278                 Sequence< OUString > aAvailSvcs(
279                         xLngSvcMgr->getAvailableServices( aService, aLocale ));
280 #if OSL_DEBUG_LEVEL > 1
281                 const OUString * pCfgSvcs   = aCfgSvcs.getConstArray();
282                 const OUString * pAvailSvcs = aAvailSvcs.getConstArray();
283                 (void) pCfgSvcs;
284                 (void) pAvailSvcs;
285 #endif
286                 aCfgSvcs = lcl_RemoveMissingEntries( aCfgSvcs, aAvailSvcs );
287 
288                 aCurSvcs[k][ pNodeName[i] ] = aCfgSvcs;
289             }
290 
291             //
292             // add new available language/service entries
293             //
294             uno::Reference< XAvailableLocales > xAvail( xLngSvcMgr, UNO_QUERY );
295             Sequence< Locale > aAvailLocales( xAvail->getAvailableLocales(aService) );
296             sal_Int32 nAvailLocales = aAvailLocales.getLength();
297             const Locale *pAvailLocale = aAvailLocales.getConstArray();
298             for (i = 0;  i < nAvailLocales;  ++i)
299             {
300                 Sequence< OUString > aAvailSvcs(
301                         xLngSvcMgr->getAvailableServices( aService, pAvailLocale[i] ));
302                 Sequence< OUString > aLastSvcs(
303                         lcl_GetLastFoundSvcs( aCfg, aLastFoundList , pAvailLocale[i] ));
304                 Sequence< OUString > aNewSvcs =
305                         lcl_GetNewEntries( aLastSvcs, aAvailSvcs );
306 #if OSL_DEBUG_LEVEL > 1
307                 const OUString * pAvailSvcs = aAvailSvcs.getConstArray();
308                 const OUString * pLastSvcs  = aLastSvcs.getConstArray();
309                 const OUString * pNewSvcs   = aNewSvcs.getConstArray();
310                 (void) pAvailSvcs;
311                 (void) pLastSvcs;
312                 (void) pNewSvcs;
313 #endif
314 
315                 OUString aCfgLocaleStr( MsLangId::convertLanguageToIsoString(
316                                             SvxLocaleToLanguage( pAvailLocale[i] ) ) );
317                 Sequence< OUString > aCfgSvcs( aCurSvcs[k][ aCfgLocaleStr ] );
318 
319                 // merge services list (previously configured to be listed first).
320                 aCfgSvcs = lcl_MergeSeq( aCfgSvcs, aNewSvcs );
321 
322 /*
323                 // there is at most one Hyphenator per language allowed
324                 // to be configured, thus we only use the first one found.
325                 if (k == 2 && aCfgSvcs.getLength() > 1)
326                     aCfgSvcs.realloc(1);
327 */
328                 aCurSvcs[k][ aCfgLocaleStr ] = aCfgSvcs;
329             }
330 
331             //
332             // set last found services to currently available ones
333             //
334             for (i = 0;  i < nAvailLocales;  ++i)
335             {
336                 Sequence< OUString > aSvcImplNames(
337                         xLngSvcMgr->getAvailableServices( aService, pAvailLocale[i] ) );
338 
339 #if OSL_DEBUG_LEVEL > 1
340                 sal_Int32 nSvcs = aSvcImplNames.getLength();
341                 const OUString *pSvcImplName = aSvcImplNames.getConstArray();
342                 for (sal_Int32 j = 0;  j < nSvcs;  ++j)
343                 {
344                     OUString aImplName( pSvcImplName[j] );
345                 }
346 #endif
347 
348                 OUString aCfgLocaleStr( MsLangId::convertLanguageToIsoString(
349                                             SvxLocaleToLanguage( pAvailLocale[i] ) ) );
350                 aLastFoundSvcs[k][ aCfgLocaleStr ] = aSvcImplNames;
351             }
352         }
353 
354         //
355         // write new data back to configuration
356         //
357         for (int k = 0;  k < nNumServices;  ++k)
358         {
359             for (int i = 0;  i < 2;  ++i)
360             {
361                 const sal_Char *pSubNodeName = (i == 0) ? apCurLists[k] : apLastFoundLists[k];
362                 OUString aSubNodeName( A2OU(pSubNodeName) );
363 
364                 list_entry_map_t &rCurMap = (i == 0) ? aCurSvcs[k] : aLastFoundSvcs[k];
365                 list_entry_map_t::const_iterator aIt( rCurMap.begin() );
366                 sal_Int32 nVals = static_cast< sal_Int32 >( rCurMap.size() );
367                 Sequence< PropertyValue > aNewValues( nVals );
368                 PropertyValue *pNewValue = aNewValues.getArray();
369                 while (aIt != rCurMap.end())
370                 {
371                     OUString aCfgEntryName( aSubNodeName );
372                     aCfgEntryName += OUString::valueOf( (sal_Unicode) '/' );
373                     aCfgEntryName += (*aIt).first;
374 
375 #if OSL_DEBUG_LEVEL > 1
376                     Sequence< OUString > aSvcImplNames( (*aIt).second );
377                     sal_Int32 nSvcs = aSvcImplNames.getLength();
378                     const OUString *pSvcImplName = aSvcImplNames.getConstArray();
379                     for (sal_Int32 j = 0;  j < nSvcs;  ++j)
380                     {
381                         OUString aImplName( pSvcImplName[j] );
382                     }
383 #endif
384                     pNewValue->Name  = aCfgEntryName;
385                     pNewValue->Value <<= (*aIt).second;
386                     ++pNewValue;
387                     ++aIt;
388                 }
389                 DBG_ASSERT( pNewValue - aNewValues.getArray() == nVals,
390                         "possible mismatch of sequence size and property number" );
391 
392                 {
393                     RTL_LOGFILE_CONTEXT( aLog, "svx: SvxLinguConfigUpdate::UpdateAll - ReplaceSetProperties" );
394                     // add new or replace existing entries.
395                     sal_Bool bRes = aCfg.ReplaceSetProperties( aSubNodeName, aNewValues );
396                     if (!bRes)
397                     {
398 #if OSL_DEBUG_LEVEL > 1
399                         DBG_ERROR( "failed to set new configuration values" );
400 #endif
401                     }
402                 }
403             }
404         }
405         DBG_ASSERT( nCurrentDataFilesChangedCheckValue != -1, "SvxLinguConfigUpdate::UpdateAll DataFilesChangedCheckValue not yet calculated!" );
406         Any aAny;
407 
408         // for the time being (developer builds until OOo 3.0)
409         // we should always check for everything available
410         // otherwise we may miss a new installed extension dictionary
411         // just because e.g. the spellchecker is not asked what
412         // languages it does support currently...
413         // Since the check is on-demand occurring and executed once it should
414         // not be too troublesome.
415         // In OOo 3.0 we will not need the respective code anymore at all.
416 //      aAny <<= nCurrentDataFilesChangedCheckValue;
417         aAny <<= (sal_Int32) -1;    // keep the value set to 'need to check'
418 
419         aCfg.SetProperty( A2OU( "DataFilesChangedCheckValue" ), aAny );
420 
421         //! Note 1: the new values are committed when the 'aCfg' object
422         //!     gets destroyed.
423         //! Note 2: the new settings in the configuration get applied
424         //!     because the 'LngSvcMgr' (in linguistic/source/lngsvcmgr.hxx)
425         //!     listens to the configuration for changes of the relevant
426         //!     properties and then applies the new settings.
427 
428         // nothing needs to be done anymore
429         nNeedUpdating = 0;
430     }
431 }
432 
433 
CalcDataFilesChangedCheckValue()434 sal_Int32 SvxLinguConfigUpdate::CalcDataFilesChangedCheckValue()
435 {
436     RTL_LOGFILE_CONTEXT( aLog, "svx: SvxLinguConfigUpdate::CalcDataFilesChangedCheckValue" );
437 
438     sal_Int32 nHashVal = 0;
439     // nothing to be checked anymore since those old directory paths are gone by now
440     return nHashVal;
441 }
442 
443 
IsNeedUpdateAll(sal_Bool bForceCheck)444 sal_Bool SvxLinguConfigUpdate::IsNeedUpdateAll( sal_Bool bForceCheck )
445 {
446     RTL_LOGFILE_CONTEXT( aLog, "svx: SvxLinguConfigUpdate::IsNeedUpdateAll" );
447     if (nNeedUpdating == -1 || bForceCheck )    // need to check if updating is necessary
448     {
449         // calculate hash value for current data files
450         nCurrentDataFilesChangedCheckValue = CalcDataFilesChangedCheckValue();
451 
452         // compare hash value and check value to see if anything has changed
453         // and thus the configuration needs to be updated
454         SvtLinguOptions aLinguOpt;
455         SvtLinguConfig aCfg;
456         aCfg.GetOptions( aLinguOpt );
457         nNeedUpdating = (nCurrentDataFilesChangedCheckValue == aLinguOpt.nDataFilesChangedCheckValue) ? 0 : 1;
458     }
459     DBG_ASSERT( nNeedUpdating != -1,
460             "need for linguistic configuration update should have been already checked." );
461 
462     return nNeedUpdating == 1;
463 }
464 
465 ///////////////////////////////////////////////////////////////////////////
466 
467 
468 //! Dummy implementation in order to avoid loading of lingu DLL
469 //! when only the XSupportedLocales interface is used.
470 //! The dummy accesses the real implementation (and thus loading the DLL)
471 //! when "real" work needs to be done only.
472 class ThesDummy_Impl :
473     public cppu::WeakImplHelper1< XThesaurus >
474 {
475     uno::Reference< XThesaurus >     xThes;      // the real one...
476     Sequence< Locale >         *pLocaleSeq;
477 
478     void GetCfgLocales();
479 
480     void GetThes_Impl();
481 
482 public:
ThesDummy_Impl()483     ThesDummy_Impl() : pLocaleSeq(0)  {}
484     ~ThesDummy_Impl();
485 
486     // XSupportedLocales
487     virtual ::com::sun::star::uno::Sequence<
488             ::com::sun::star::lang::Locale > SAL_CALL
489         getLocales();
490     virtual sal_Bool SAL_CALL
491         hasLocale( const ::com::sun::star::lang::Locale& rLocale );
492 
493     // XThesaurus
494     virtual ::com::sun::star::uno::Sequence<
495             ::com::sun::star::uno::Reference<
496                 ::com::sun::star::linguistic2::XMeaning > > SAL_CALL
497         queryMeanings( const ::rtl::OUString& rTerm,
498                 const ::com::sun::star::lang::Locale& rLocale,
499                 const ::com::sun::star::beans::PropertyValues& rProperties );
500 };
501 
502 
~ThesDummy_Impl()503 ThesDummy_Impl::~ThesDummy_Impl()
504 {
505     delete pLocaleSeq;
506 }
507 
508 
GetCfgLocales()509 void ThesDummy_Impl::GetCfgLocales()
510 {
511     if (!pLocaleSeq)
512     {
513         SvtLinguConfig aCfg;
514         String  aNode( A2OU( "ServiceManager/ThesaurusList" ) );
515         Sequence < OUString > aNodeNames( aCfg.GetNodeNames( aNode ) );
516         const OUString *pNodeNames = aNodeNames.getConstArray();
517         sal_Int32 nLen = aNodeNames.getLength();
518         pLocaleSeq = new Sequence< Locale >( nLen );
519         Locale *pLocale = pLocaleSeq->getArray();
520         for (sal_Int32 i = 0;  i < nLen;  ++i)
521         {
522             pLocale[i] = SvxCreateLocale(
523                             MsLangId::convertIsoStringToLanguage( pNodeNames[i] ) );
524         }
525     }
526 }
527 
528 
GetThes_Impl()529 void ThesDummy_Impl::GetThes_Impl()
530 {
531     // update configuration before accessing the service
532     if (SvxLinguConfigUpdate::IsNeedUpdateAll())
533         SvxLinguConfigUpdate::UpdateAll();
534 
535     if (!xThes.is())
536     {
537         uno::Reference< XLinguServiceManager > xLngSvcMgr( GetLngSvcMgr_Impl() );
538         if (xLngSvcMgr.is())
539             xThes = xLngSvcMgr->getThesaurus();
540 
541         if (xThes.is())
542         {
543             // no longer needed...
544             delete pLocaleSeq;    pLocaleSeq = 0;
545         }
546     }
547 }
548 
549 
550 uno::Sequence< lang::Locale > SAL_CALL
getLocales()551         ThesDummy_Impl::getLocales()
552 {
553     if (!SvxLinguConfigUpdate::IsNeedUpdateAll())   // configuration already update and thus lingu DLL's already loaded ?
554         GetThes_Impl();
555     if (xThes.is())
556         return xThes->getLocales();
557     else if (!pLocaleSeq)       // if not already loaded save startup time by avoiding loading them now
558         GetCfgLocales();
559     return *pLocaleSeq;
560 }
561 
562 
563 sal_Bool SAL_CALL
hasLocale(const lang::Locale & rLocale)564         ThesDummy_Impl::hasLocale( const lang::Locale& rLocale )
565 {
566     if (!SvxLinguConfigUpdate::IsNeedUpdateAll())   // configuration already update and thus lingu DLL's already loaded ?
567         GetThes_Impl();
568     if (xThes.is())
569         return xThes->hasLocale( rLocale );
570     else if (!pLocaleSeq)       // if not already loaded save startup time by avoiding loading them now
571         GetCfgLocales();
572         GetCfgLocales();
573     sal_Bool bFound = sal_False;
574     sal_Int32 nLen = pLocaleSeq->getLength();
575     const Locale *pLocale = pLocaleSeq->getConstArray();
576     const Locale *pEnd = pLocale + nLen;
577     for ( ;  pLocale < pEnd  &&  !bFound;  ++pLocale)
578     {
579         bFound = pLocale->Language == rLocale.Language  &&
580                  pLocale->Country  == rLocale.Country   &&
581                  pLocale->Variant  == rLocale.Variant;
582     }
583     return bFound;
584 }
585 
586 
587 uno::Sequence< uno::Reference< linguistic2::XMeaning > > SAL_CALL
queryMeanings(const rtl::OUString & rTerm,const lang::Locale & rLocale,const beans::PropertyValues & rProperties)588         ThesDummy_Impl::queryMeanings(
589                 const rtl::OUString& rTerm,
590                 const lang::Locale& rLocale,
591                 const beans::PropertyValues& rProperties )
592 {
593     GetThes_Impl();
594     uno::Sequence< uno::Reference< linguistic2::XMeaning > > aRes;
595     DBG_ASSERT( xThes.is(), "Thesaurus missing" );
596     if (xThes.is())
597         aRes = xThes->queryMeanings( rTerm, rLocale, rProperties );
598     return aRes;
599 }
600 
601 
602 ///////////////////////////////////////////////////////////////////////////
603 
604 
605 //! Dummy implementation in order to avoid loading of lingu DLL.
606 //! The dummy accesses the real implementation (and thus loading the DLL)
607 //! when it needs to be done only.
608 class SpellDummy_Impl :
609     public cppu::WeakImplHelper1< XSpellChecker1 >
610 {
611     uno::Reference< XSpellChecker1 >     xSpell;      // the real one...
612 
613     void    GetSpell_Impl();
614 
615 public:
616 
617     // XSupportedLanguages (for XSpellChecker1)
618     virtual ::com::sun::star::uno::Sequence< sal_Int16 > SAL_CALL
619         getLanguages();
620     virtual sal_Bool SAL_CALL
621         hasLanguage( sal_Int16 nLanguage );
622 
623     // XSpellChecker1 (same as XSpellChecker but sal_Int16 for language)
624     virtual sal_Bool SAL_CALL
625         isValid( const ::rtl::OUString& rWord, sal_Int16 nLanguage,
626                 const ::com::sun::star::beans::PropertyValues& rProperties );
627     virtual ::com::sun::star::uno::Reference<
628             ::com::sun::star::linguistic2::XSpellAlternatives > SAL_CALL
629         spell( const ::rtl::OUString& rWord, sal_Int16 nLanguage,
630                 const ::com::sun::star::beans::PropertyValues& rProperties );
631 };
632 
633 
GetSpell_Impl()634 void SpellDummy_Impl::GetSpell_Impl()
635 {
636     // update configuration before accessing the service
637     if (SvxLinguConfigUpdate::IsNeedUpdateAll())
638         SvxLinguConfigUpdate::UpdateAll();
639 
640     if (!xSpell.is())
641     {
642         uno::Reference< XLinguServiceManager > xLngSvcMgr( GetLngSvcMgr_Impl() );
643         if (xLngSvcMgr.is())
644             xSpell = uno::Reference< XSpellChecker1 >( xLngSvcMgr->getSpellChecker(), UNO_QUERY );
645     }
646 }
647 
648 
649 uno::Sequence< sal_Int16 > SAL_CALL
getLanguages()650     SpellDummy_Impl::getLanguages()
651 {
652     GetSpell_Impl();
653     if (xSpell.is())
654         return xSpell->getLanguages();
655     else
656         return uno::Sequence< sal_Int16 >();
657 }
658 
659 
660 sal_Bool SAL_CALL
hasLanguage(sal_Int16 nLanguage)661     SpellDummy_Impl::hasLanguage( sal_Int16 nLanguage )
662 {
663     GetSpell_Impl();
664     sal_Bool bRes = sal_False;
665     if (xSpell.is())
666         bRes = xSpell->hasLanguage( nLanguage );
667     return bRes;
668 }
669 
670 
671 sal_Bool SAL_CALL
isValid(const rtl::OUString & rWord,sal_Int16 nLanguage,const beans::PropertyValues & rProperties)672     SpellDummy_Impl::isValid( const rtl::OUString& rWord, sal_Int16 nLanguage,
673             const beans::PropertyValues& rProperties )
674 {
675     GetSpell_Impl();
676     sal_Bool bRes = sal_True;
677     if (xSpell.is())
678         bRes = xSpell->isValid( rWord, nLanguage, rProperties );
679     return bRes;
680 }
681 
682 
683 uno::Reference< linguistic2::XSpellAlternatives > SAL_CALL
spell(const rtl::OUString & rWord,sal_Int16 nLanguage,const beans::PropertyValues & rProperties)684     SpellDummy_Impl::spell( const rtl::OUString& rWord, sal_Int16 nLanguage,
685             const beans::PropertyValues& rProperties )
686 {
687     GetSpell_Impl();
688     uno::Reference< linguistic2::XSpellAlternatives > xRes;
689     if (xSpell.is())
690         xRes = xSpell->spell( rWord, nLanguage, rProperties );
691     return xRes;
692 }
693 
694 
695 ///////////////////////////////////////////////////////////////////////////
696 
697 
698 //! Dummy implementation in order to avoid loading of lingu DLL.
699 //! The dummy accesses the real implementation (and thus loading the DLL)
700 //! when it needs to be done only.
701 class HyphDummy_Impl :
702     public cppu::WeakImplHelper1< XHyphenator >
703 {
704     uno::Reference< XHyphenator >     xHyph;      // the real one...
705 
706     void    GetHyph_Impl();
707 
708 public:
709 
710     // XSupportedLocales
711     virtual ::com::sun::star::uno::Sequence<
712             ::com::sun::star::lang::Locale > SAL_CALL
713         getLocales();
714     virtual sal_Bool SAL_CALL
715         hasLocale( const ::com::sun::star::lang::Locale& rLocale );
716 
717     // XHyphenator
718     virtual ::com::sun::star::uno::Reference<
719             ::com::sun::star::linguistic2::XHyphenatedWord > SAL_CALL
720         hyphenate( const ::rtl::OUString& rWord,
721                 const ::com::sun::star::lang::Locale& rLocale,
722                 sal_Int16 nMaxLeading,
723                 const ::com::sun::star::beans::PropertyValues& rProperties );
724     virtual ::com::sun::star::uno::Reference<
725             ::com::sun::star::linguistic2::XHyphenatedWord > SAL_CALL
726         queryAlternativeSpelling( const ::rtl::OUString& rWord,
727                 const ::com::sun::star::lang::Locale& rLocale,
728                 sal_Int16 nIndex,
729                 const ::com::sun::star::beans::PropertyValues& rProperties );
730     virtual ::com::sun::star::uno::Reference<
731             ::com::sun::star::linguistic2::XPossibleHyphens > SAL_CALL
732         createPossibleHyphens(
733                 const ::rtl::OUString& rWord,
734                 const ::com::sun::star::lang::Locale& rLocale,
735                 const ::com::sun::star::beans::PropertyValues& rProperties );
736 };
737 
738 
GetHyph_Impl()739 void HyphDummy_Impl::GetHyph_Impl()
740 {
741     // update configuration before accessing the service
742     if (SvxLinguConfigUpdate::IsNeedUpdateAll())
743         SvxLinguConfigUpdate::UpdateAll();
744 
745     if (!xHyph.is())
746     {
747         uno::Reference< XLinguServiceManager > xLngSvcMgr( GetLngSvcMgr_Impl() );
748         if (xLngSvcMgr.is())
749             xHyph = xLngSvcMgr->getHyphenator();
750     }
751 }
752 
753 
754 uno::Sequence< lang::Locale > SAL_CALL
getLocales()755     HyphDummy_Impl::getLocales()
756 {
757     GetHyph_Impl();
758     if (xHyph.is())
759         return xHyph->getLocales();
760     else
761         return uno::Sequence< lang::Locale >();
762 }
763 
764 
765 sal_Bool SAL_CALL
hasLocale(const lang::Locale & rLocale)766     HyphDummy_Impl::hasLocale( const lang::Locale& rLocale )
767 {
768     GetHyph_Impl();
769     sal_Bool bRes = sal_False;
770     if (xHyph.is())
771         bRes = xHyph->hasLocale( rLocale );
772     return bRes;
773 }
774 
775 
776 uno::Reference< linguistic2::XHyphenatedWord > SAL_CALL
hyphenate(const rtl::OUString & rWord,const lang::Locale & rLocale,sal_Int16 nMaxLeading,const beans::PropertyValues & rProperties)777     HyphDummy_Impl::hyphenate(
778             const rtl::OUString& rWord,
779             const lang::Locale& rLocale,
780             sal_Int16 nMaxLeading,
781             const beans::PropertyValues& rProperties )
782 {
783     GetHyph_Impl();
784     uno::Reference< linguistic2::XHyphenatedWord > xRes;
785     if (xHyph.is())
786         xRes = xHyph->hyphenate( rWord, rLocale, nMaxLeading, rProperties );
787     return xRes;
788 }
789 
790 
791 uno::Reference< linguistic2::XHyphenatedWord > SAL_CALL
queryAlternativeSpelling(const rtl::OUString & rWord,const lang::Locale & rLocale,sal_Int16 nIndex,const PropertyValues & rProperties)792     HyphDummy_Impl::queryAlternativeSpelling(
793             const rtl::OUString& rWord,
794             const lang::Locale& rLocale,
795             sal_Int16 nIndex,
796             const PropertyValues& rProperties )
797 {
798     GetHyph_Impl();
799     uno::Reference< linguistic2::XHyphenatedWord > xRes;
800     if (xHyph.is())
801         xRes = xHyph->queryAlternativeSpelling( rWord, rLocale, nIndex, rProperties );
802     return xRes;
803 }
804 
805 
806 uno::Reference< linguistic2::XPossibleHyphens > SAL_CALL
createPossibleHyphens(const rtl::OUString & rWord,const lang::Locale & rLocale,const beans::PropertyValues & rProperties)807     HyphDummy_Impl::createPossibleHyphens(
808             const rtl::OUString& rWord,
809             const lang::Locale& rLocale,
810             const beans::PropertyValues& rProperties )
811 {
812     GetHyph_Impl();
813     uno::Reference< linguistic2::XPossibleHyphens > xRes;
814     if (xHyph.is())
815         xRes = xHyph->createPossibleHyphens( rWord, rLocale, rProperties );
816     return xRes;
817 }
818 
819 
820 ///////////////////////////////////////////////////////////////////////////
821 
822 
823 typedef cppu::WeakImplHelper1 < XEventListener > LinguMgrAppExitLstnrBaseClass;
824 
825 class LinguMgrAppExitLstnr : public LinguMgrAppExitLstnrBaseClass
826 {
827     uno::Reference< XComponent >        xDesktop;
828 
829 public:
830     LinguMgrAppExitLstnr();
831     virtual ~LinguMgrAppExitLstnr();
832 
833     virtual void    AtExit() = 0;
834 
835 
836     // lang::XEventListener
837     virtual void    SAL_CALL disposing(const EventObject& rSource);
838 };
839 
LinguMgrAppExitLstnr()840 LinguMgrAppExitLstnr::LinguMgrAppExitLstnr()
841 {
842     // add object to frame::Desktop EventListeners in order to properly call
843     // the AtExit function at application exit.
844 
845     uno::Reference< XMultiServiceFactory >  xMgr = getProcessServiceFactory();
846     if ( xMgr.is() )
847     {
848         xDesktop = uno::Reference< XComponent > ( xMgr->createInstance(
849                 OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.frame.Desktop" ) ) ), UNO_QUERY ) ;
850         if (xDesktop.is())
851             xDesktop->addEventListener( this );
852     }
853 }
854 
~LinguMgrAppExitLstnr()855 LinguMgrAppExitLstnr::~LinguMgrAppExitLstnr()
856 {
857     if (xDesktop.is())
858     {
859         xDesktop->removeEventListener( this );
860         xDesktop = NULL;    //! release reference to desktop
861     }
862     DBG_ASSERT(!xDesktop.is(), "reference to desktop should be released");
863 }
864 
disposing(const EventObject & rSource)865 void LinguMgrAppExitLstnr::disposing(const EventObject& rSource)
866 {
867     if (xDesktop.is()  &&  rSource.Source == xDesktop)
868     {
869         xDesktop->removeEventListener( this );
870         xDesktop = NULL;    //! release reference to desktop
871 
872         AtExit();
873     }
874 }
875 
876 ///////////////////////////////////////////////////////////////////////////
877 
878 class LinguMgrExitLstnr : public LinguMgrAppExitLstnr
879 {
880 public:
881     virtual void    AtExit();
882 };
883 
AtExit()884 void LinguMgrExitLstnr::AtExit()
885 {
886     // release references
887     LinguMgr::xLngSvcMgr    = 0;
888     LinguMgr::xSpell        = 0;
889     LinguMgr::xHyph         = 0;
890     LinguMgr::xThes         = 0;
891     LinguMgr::xDicList      = 0;
892     LinguMgr::xProp         = 0;
893     LinguMgr::xIgnoreAll    = 0;
894     LinguMgr::xChangeAll    = 0;
895 
896     LinguMgr::bExiting      = sal_True;
897 
898     //TL:TODO: MBA fragen wie ich ohne Absturz hier meinen Speicher
899     //  wieder freibekomme...
900     //delete LinguMgr::pExitLstnr;
901     LinguMgr::pExitLstnr    = 0;
902 }
903 
904 ///////////////////////////////////////////////////////////////////////////
905 
906 
907 // static member initialization
908 LinguMgrExitLstnr *             LinguMgr::pExitLstnr    = 0;
909 sal_Bool                        LinguMgr::bExiting      = sal_False;
910 uno::Reference< XLinguServiceManager >  LinguMgr::xLngSvcMgr    = 0;
911 uno::Reference< XSpellChecker1 >    LinguMgr::xSpell        = 0;
912 uno::Reference< XHyphenator >       LinguMgr::xHyph         = 0;
913 uno::Reference< XThesaurus >        LinguMgr::xThes         = 0;
914 uno::Reference< XDictionaryList >   LinguMgr::xDicList      = 0;
915 uno::Reference< XPropertySet >      LinguMgr::xProp         = 0;
916 uno::Reference< XDictionary >       LinguMgr::xIgnoreAll    = 0;
917 uno::Reference< XDictionary >       LinguMgr::xChangeAll    = 0;
918 
919 
GetLngSvcMgr()920 uno::Reference< XLinguServiceManager > LinguMgr::GetLngSvcMgr()
921 {
922     if (bExiting)
923         return 0;
924 
925     if (!pExitLstnr)
926         pExitLstnr = new LinguMgrExitLstnr;
927 
928     if (!xLngSvcMgr.is())
929         xLngSvcMgr = GetLngSvcMgr_Impl();
930 
931     return xLngSvcMgr;
932 }
933 
934 
GetSpellChecker()935 uno::Reference< XSpellChecker1 > LinguMgr::GetSpellChecker()
936 {
937     return xSpell.is() ? xSpell : GetSpell();
938 }
939 
GetHyphenator()940 uno::Reference< XHyphenator > LinguMgr::GetHyphenator()
941 {
942     return xHyph.is() ? xHyph : GetHyph();
943 }
944 
GetThesaurus()945 uno::Reference< XThesaurus > LinguMgr::GetThesaurus()
946 {
947     return xThes.is() ? xThes : GetThes();
948 }
949 
GetDictionaryList()950 uno::Reference< XDictionaryList > LinguMgr::GetDictionaryList()
951 {
952     return xDicList.is() ? xDicList : GetDicList();
953 }
954 
GetLinguPropertySet()955 uno::Reference< XPropertySet > LinguMgr::GetLinguPropertySet()
956 {
957     return xProp.is() ? xProp : GetProp();
958 }
959 
GetStandardDic()960 uno::Reference< XDictionary > LinguMgr::GetStandardDic()
961 {
962     //! don't hold reference to this
963     //! (it may be removed from dictionary list and needs to be
964     //! created empty if accessed again)
965     return GetStandard();
966 }
967 
GetIgnoreAllList()968 uno::Reference< XDictionary > LinguMgr::GetIgnoreAllList()
969 {
970     return xIgnoreAll.is() ? xIgnoreAll : GetIgnoreAll();
971 }
972 
GetChangeAllList()973 uno::Reference< XDictionary > LinguMgr::GetChangeAllList()
974 {
975     return xChangeAll.is() ? xChangeAll : GetChangeAll();
976 }
977 
GetSpell()978 uno::Reference< XSpellChecker1 > LinguMgr::GetSpell()
979 {
980     if (bExiting)
981         return 0;
982 
983     if (!pExitLstnr)
984         pExitLstnr = new LinguMgrExitLstnr;
985 
986     //! use dummy implementation in order to avoid loading of lingu DLL
987     xSpell = new SpellDummy_Impl;
988 
989 /*    if (!xLngSvcMgr.is())
990         xLngSvcMgr = GetLngSvcMgr_Impl();
991 
992     if (xLngSvcMgr.is())
993     {
994         xSpell = uno::Reference< XSpellChecker1 > (
995                         xLngSvcMgr->getSpellChecker(), UNO_QUERY );
996     }
997 */
998     return xSpell;
999 }
1000 
GetHyph()1001 uno::Reference< XHyphenator > LinguMgr::GetHyph()
1002 {
1003     if (bExiting)
1004         return 0;
1005 
1006     if (!pExitLstnr)
1007         pExitLstnr = new LinguMgrExitLstnr;
1008 
1009     //! use dummy implementation in order to avoid loading of lingu DLL
1010     xHyph = new HyphDummy_Impl;
1011 
1012 /*
1013     if (!xLngSvcMgr.is())
1014         xLngSvcMgr = GetLngSvcMgr_Impl();
1015 
1016     if (xLngSvcMgr.is())
1017     {
1018         xHyph = xLngSvcMgr->getHyphenator();
1019     }
1020 */
1021     return xHyph;
1022 }
1023 
GetThes()1024 uno::Reference< XThesaurus > LinguMgr::GetThes()
1025 {
1026     if (bExiting)
1027         return 0;
1028 
1029     if (!pExitLstnr)
1030         pExitLstnr = new LinguMgrExitLstnr;
1031 
1032     //! use dummy implementation in order to avoid loading of lingu DLL
1033     //! when only the XSupportedLocales interface is used.
1034     //! The dummy accesses the real implementation (and thus loading the DLL)
1035     //! when "real" work needs to be done only.
1036     xThes = new ThesDummy_Impl;
1037 /*
1038     if (!xLngSvcMgr.is())
1039         xLngSvcMgr = GetLngSvcMgr_Impl();
1040 
1041     if (xLngSvcMgr.is())
1042     {
1043         xThes = xLngSvcMgr->getThesaurus();
1044     }
1045 */
1046     return xThes;
1047 }
1048 
1049 
UpdateAll()1050 void LinguMgr::UpdateAll()
1051 {
1052 }
1053 
1054 
GetDicList()1055 uno::Reference< XDictionaryList > LinguMgr::GetDicList()
1056 {
1057     if (bExiting)
1058         return 0;
1059 
1060     if (!pExitLstnr)
1061         pExitLstnr = new LinguMgrExitLstnr;
1062 
1063     uno::Reference< XMultiServiceFactory >  xMgr( getProcessServiceFactory() );
1064     if (xMgr.is())
1065     {
1066         xDicList = uno::Reference< XDictionaryList > ( xMgr->createInstance(
1067                     A2OU("com.sun.star.linguistic2.DictionaryList") ), UNO_QUERY );
1068     }
1069     return xDicList;
1070 }
1071 
GetProp()1072 uno::Reference< XPropertySet > LinguMgr::GetProp()
1073 {
1074     if (bExiting)
1075         return 0;
1076 
1077     if (!pExitLstnr)
1078         pExitLstnr = new LinguMgrExitLstnr;
1079 
1080     uno::Reference< XMultiServiceFactory >  xMgr( getProcessServiceFactory() );
1081     if (xMgr.is())
1082     {
1083         xProp = uno::Reference< XPropertySet > ( xMgr->createInstance(
1084                     A2OU("com.sun.star.linguistic2.LinguProperties") ), UNO_QUERY );
1085     }
1086     return xProp;
1087 }
1088 
GetIgnoreAll()1089 uno::Reference< XDictionary > LinguMgr::GetIgnoreAll()
1090 {
1091     if (bExiting)
1092         return 0;
1093 
1094     if (!pExitLstnr)
1095         pExitLstnr = new LinguMgrExitLstnr;
1096 
1097     uno::Reference< XDictionaryList >  xTmpDicList( GetDictionaryList() );
1098     if (xTmpDicList.is())
1099     {
1100         xIgnoreAll = uno::Reference< XDictionary > ( xTmpDicList->getDictionaryByName(
1101                     A2OU("IgnoreAllList") ), UNO_QUERY );
1102     }
1103     return xIgnoreAll;
1104 }
1105 
GetChangeAll()1106 uno::Reference< XDictionary > LinguMgr::GetChangeAll()
1107 {
1108     if (bExiting)
1109         return 0;
1110 
1111     if (!pExitLstnr)
1112         pExitLstnr = new LinguMgrExitLstnr;
1113 
1114     uno::Reference< XDictionaryList > _xDicList( GetDictionaryList() , UNO_QUERY );
1115     if (_xDicList.is())
1116     {
1117         xChangeAll = uno::Reference< XDictionary > (
1118                         _xDicList->createDictionary(
1119                             A2OU("ChangeAllList"),
1120                             SvxCreateLocale( LANGUAGE_NONE ),
1121                             DictionaryType_NEGATIVE, String() ), UNO_QUERY );
1122     }
1123     return xChangeAll;
1124 }
1125 
GetStandard()1126 uno::Reference< XDictionary > LinguMgr::GetStandard()
1127 {
1128     // Tries to return a dictionary which may hold positive entries is
1129     // persistent and not read-only.
1130 
1131     if (bExiting)
1132         return 0;
1133 
1134     uno::Reference< XDictionaryList >  xTmpDicList( GetDictionaryList() );
1135     if (!xTmpDicList.is())
1136         return NULL;
1137 
1138     const OUString aDicName( RTL_CONSTASCII_USTRINGPARAM( "standard.dic" ) );
1139     uno::Reference< XDictionary >   xDic( xTmpDicList->getDictionaryByName( aDicName ),
1140                                       UNO_QUERY );
1141     if (!xDic.is())
1142     {
1143         // try to create standard dictionary
1144         uno::Reference< XDictionary >    xTmp;
1145         try
1146         {
1147             xTmp =  xTmpDicList->createDictionary( aDicName,
1148                         SvxCreateLocale( LANGUAGE_NONE ),
1149                         DictionaryType_POSITIVE,
1150                         linguistic::GetWritableDictionaryURL( aDicName ) );
1151         }
1152         catch(com::sun::star::uno::Exception &)
1153         {
1154         }
1155 
1156         // add new dictionary to list
1157         if (xTmp.is())
1158         {
1159             xTmpDicList->addDictionary( xTmp );
1160             xTmp->setActive( sal_True );
1161         }
1162         xDic = uno::Reference< XDictionary > ( xTmp, UNO_QUERY );
1163     }
1164 #if OSL_DEBUG_LEVEL > 1
1165     uno::Reference< XStorable >      xStor( xDic, UNO_QUERY );
1166     DBG_ASSERT( xDic.is() && xDic->getDictionaryType() == DictionaryType_POSITIVE,
1167             "wrong dictionary type");
1168     DBG_ASSERT( xDic.is() && SvxLocaleToLanguage( xDic->getLocale() ) == LANGUAGE_NONE,
1169             "wrong dictionary language");
1170     DBG_ASSERT( !xStor.is() || (xStor->hasLocation() && !xStor->isReadonly()),
1171             "dictionary not editable" );
1172 #endif
1173 
1174     return xDic;
1175 }
1176 
1177 ///////////////////////////////////////////////////////////////////////////
1178 
SvxGetSpellChecker()1179 uno::Reference< XSpellChecker1 >  SvxGetSpellChecker()
1180 {
1181     return LinguMgr::GetSpellChecker();
1182 }
1183 
SvxGetHyphenator()1184 uno::Reference< XHyphenator >  SvxGetHyphenator()
1185 {
1186     return LinguMgr::GetHyphenator();
1187 }
1188 
SvxGetThesaurus()1189 uno::Reference< XThesaurus >  SvxGetThesaurus()
1190 {
1191     return LinguMgr::GetThesaurus();
1192 }
1193 
SvxGetDictionaryList()1194 uno::Reference< XDictionaryList >  SvxGetDictionaryList()
1195 {
1196     return LinguMgr::GetDictionaryList();
1197 }
1198 
SvxGetLinguPropertySet()1199 uno::Reference< XPropertySet >  SvxGetLinguPropertySet()
1200 {
1201     return LinguMgr::GetLinguPropertySet();
1202 }
1203 
1204 //TL:TODO: remove argument or provide SvxGetIgnoreAllList with the same one
SvxGetOrCreatePosDic(uno::Reference<XDictionaryList>)1205 uno::Reference< XDictionary >  SvxGetOrCreatePosDic(
1206         uno::Reference< XDictionaryList >  /* xDicList */ )
1207 {
1208     return LinguMgr::GetStandardDic();
1209 }
1210 
SvxGetIgnoreAllList()1211 uno::Reference< XDictionary >  SvxGetIgnoreAllList()
1212 {
1213     return LinguMgr::GetIgnoreAllList();
1214 }
1215 
SvxGetChangeAllList()1216 uno::Reference< XDictionary >  SvxGetChangeAllList()
1217 {
1218     return LinguMgr::GetChangeAllList();
1219 }
1220 
1221 ///////////////////////////////////////////////////////////////////////////
1222 
1223 
1224 #include <com/sun/star/linguistic2/XHyphenatedWord.hpp>
1225 
SvxGetAltSpelling(const::com::sun::star::uno::Reference<::com::sun::star::linguistic2::XHyphenatedWord> & rHyphWord)1226 SvxAlternativeSpelling SvxGetAltSpelling(
1227         const ::com::sun::star::uno::Reference<
1228             ::com::sun::star::linguistic2::XHyphenatedWord > & rHyphWord )
1229 {
1230     SvxAlternativeSpelling aRes;
1231     if (rHyphWord.is() && rHyphWord->isAlternativeSpelling())
1232     {
1233         OUString aWord( rHyphWord->getWord() ),
1234                  aAltWord( rHyphWord->getHyphenatedWord() );
1235         sal_Int16   nHyphenationPos     = rHyphWord->getHyphenationPos(),
1236                 nHyphenPos          = rHyphWord->getHyphenPos();
1237         sal_Int16   nLen    = (sal_Int16)aWord.getLength();
1238         sal_Int16   nAltLen = (sal_Int16)aAltWord.getLength();
1239         const sal_Unicode *pWord    = aWord.getStr(),
1240                           *pAltWord = aAltWord.getStr();
1241 
1242         // count number of chars from the left to the
1243         // hyphenation pos / hyphen pos that are equal
1244         sal_Int16 nL = 0;
1245         while (nL <= nHyphenationPos && nL <= nHyphenPos
1246                && pWord[ nL ] == pAltWord[ nL ])
1247             ++nL;
1248         // count number of chars from the right to the
1249         // hyphenation pos / hyphen pos that are equal
1250         sal_Int16 nR = 0;
1251         sal_Int32 nIdx    = nLen - 1;
1252         sal_Int32 nAltIdx = nAltLen - 1;
1253         while (nIdx > nHyphenationPos && nAltIdx > nHyphenPos
1254                && pWord[ nIdx-- ] == pAltWord[ nAltIdx-- ])
1255             ++nR;
1256 
1257         aRes.aReplacement       = OUString( aAltWord.copy( nL, nAltLen - nL - nR ) );
1258         aRes.nChangedPos        = (sal_Int16) nL;
1259         aRes.nChangedLength     = nLen - nL - nR;
1260         aRes.bIsAltSpelling     = sal_True;
1261         aRes.xHyphWord          = rHyphWord;
1262     }
1263     return aRes;
1264 }
1265 
1266 
1267 ///////////////////////////////////////////////////////////////////////////
1268 
SvxDicListChgClamp(uno::Reference<XDictionaryList> & rxDicList)1269 SvxDicListChgClamp::SvxDicListChgClamp( uno::Reference< XDictionaryList >  &rxDicList ) :
1270     xDicList    ( rxDicList )
1271 {
1272     if (xDicList.is())
1273     {
1274         xDicList->beginCollectEvents();
1275     }
1276 }
1277 
~SvxDicListChgClamp()1278 SvxDicListChgClamp::~SvxDicListChgClamp()
1279 {
1280     if (xDicList.is())
1281     {
1282         xDicList->endCollectEvents();
1283     }
1284 }
1285 
1286 ///////////////////////////////////////////////////////////////////////////
1287 
SvxDicError(Window * pParent,sal_Int16 nError)1288 short SvxDicError( Window *pParent, sal_Int16 nError )
1289 {
1290     short nRes = 0;
1291     if (DIC_ERR_NONE != nError)
1292     {
1293         int nRid;
1294         switch (nError)
1295         {
1296             case DIC_ERR_FULL     : nRid = RID_SVXSTR_DIC_ERR_FULL;  break;
1297             case DIC_ERR_READONLY : nRid = RID_SVXSTR_DIC_ERR_READONLY;  break;
1298             default:
1299                 nRid = RID_SVXSTR_DIC_ERR_UNKNOWN;
1300                 DBG_ASSERT(0, "unexpected case");
1301         }
1302         nRes = InfoBox( pParent, EE_RESSTR( nRid ) ).Execute();
1303     }
1304     return nRes;
1305 }
1306 
SvxLocaleToLanguage(const Locale & rLocale)1307 LanguageType SvxLocaleToLanguage( const Locale& rLocale )
1308 {
1309     //  empty Locale -> LANGUAGE_NONE
1310     if ( rLocale.Language.getLength() == 0 )
1311         return LANGUAGE_NONE;
1312 
1313     return MsLangId::convertLocaleToLanguage( rLocale );
1314 }
1315 
SvxLanguageToLocale(Locale & rLocale,LanguageType eLang)1316 Locale& SvxLanguageToLocale( Locale& rLocale, LanguageType eLang )
1317 {
1318     if ( eLang != LANGUAGE_NONE /* &&  eLang != LANGUAGE_SYSTEM */)
1319         MsLangId::convertLanguageToLocale( eLang, rLocale );
1320     else
1321         rLocale = Locale();
1322 
1323     return rLocale;
1324 }
1325 
SvxCreateLocale(LanguageType eLang)1326 Locale SvxCreateLocale( LanguageType eLang )
1327 {
1328     Locale aLocale;
1329     if ( eLang != LANGUAGE_NONE /* &&  eLang != LANGUAGE_SYSTEM */)
1330         MsLangId::convertLanguageToLocale( eLang, aLocale );
1331 
1332     return aLocale;
1333 }
1334