1b0844812SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3b0844812SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b0844812SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b0844812SAndrew Rist  * distributed with this work for additional information
6b0844812SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b0844812SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b0844812SAndrew Rist  * "License"); you may not use this file except in compliance
9b0844812SAndrew Rist  * with the License.  You may obtain a copy of the License at
10b0844812SAndrew Rist  *
11b0844812SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12b0844812SAndrew Rist  *
13b0844812SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b0844812SAndrew Rist  * software distributed under the License is distributed on an
15b0844812SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b0844812SAndrew Rist  * KIND, either express or implied.  See the License for the
17b0844812SAndrew Rist  * specific language governing permissions and limitations
18b0844812SAndrew Rist  * under the License.
19b0844812SAndrew Rist  *
20b0844812SAndrew Rist  *************************************************************/
21b0844812SAndrew Rist 
22b0844812SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_lingucomponent.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h>
28cdf0e10cSrcweir #include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <com/sun/star/linguistic2/SpellFailure.hpp>
31cdf0e10cSrcweir #include <cppuhelper/factory.hxx>	// helper for factories
32cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
33cdf0e10cSrcweir #include <tools/debug.hxx>
34cdf0e10cSrcweir #include <unotools/processfactory.hxx>
35cdf0e10cSrcweir #include <osl/mutex.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <lingutil.hxx>
38cdf0e10cSrcweir #include <hunspell.hxx>
39cdf0e10cSrcweir #include <sspellimp.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <linguistic/lngprops.hxx>
42cdf0e10cSrcweir #include <linguistic/spelldta.hxx>
43cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
44cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
45cdf0e10cSrcweir #include <unotools/lingucfg.hxx>
46cdf0e10cSrcweir #include <unotools/useroptions.hxx>
47cdf0e10cSrcweir #include <osl/file.hxx>
48cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
49cdf0e10cSrcweir #include <rtl/textenc.h>
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include <list>
52cdf0e10cSrcweir #include <set>
53cdf0e10cSrcweir #include <string.h>
54cdf0e10cSrcweir 
55cdf0e10cSrcweir using namespace utl;
56cdf0e10cSrcweir using namespace osl;
57cdf0e10cSrcweir using namespace rtl;
58cdf0e10cSrcweir using namespace com::sun::star;
59cdf0e10cSrcweir using namespace com::sun::star::beans;
60cdf0e10cSrcweir using namespace com::sun::star::lang;
61cdf0e10cSrcweir using namespace com::sun::star::uno;
62cdf0e10cSrcweir using namespace com::sun::star::linguistic2;
63cdf0e10cSrcweir using namespace linguistic;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir // XML-header of SPELLML queries
66cdf0e10cSrcweir #define SPELLML_HEADER "<?xml?>"
67cdf0e10cSrcweir 
68cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
69cdf0e10cSrcweir 
SpellChecker()70cdf0e10cSrcweir SpellChecker::SpellChecker() :
71cdf0e10cSrcweir 	aEvtListeners	( GetLinguMutex() )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     aDicts = NULL;
74cdf0e10cSrcweir 	aDEncs = NULL;
75cdf0e10cSrcweir 	aDLocs = NULL;
76cdf0e10cSrcweir 	aDNames = NULL;
77cdf0e10cSrcweir 	bDisposing = sal_False;
78cdf0e10cSrcweir 	pPropHelper = NULL;
79cdf0e10cSrcweir     numdict = 0;
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 
~SpellChecker()83cdf0e10cSrcweir SpellChecker::~SpellChecker()
84cdf0e10cSrcweir {
85cdf0e10cSrcweir     if (aDicts)
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir        for (int i = 0; i < numdict; i++)
88cdf0e10cSrcweir        {
89cdf0e10cSrcweir             if (aDicts[i]) delete aDicts[i];
90cdf0e10cSrcweir             aDicts[i] = NULL;
91cdf0e10cSrcweir        }
92cdf0e10cSrcweir        delete[] aDicts;
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir     aDicts = NULL;
95cdf0e10cSrcweir     numdict = 0;
96cdf0e10cSrcweir     if (aDEncs) delete[] aDEncs;
97cdf0e10cSrcweir     aDEncs = NULL;
98cdf0e10cSrcweir     if (aDLocs) delete[] aDLocs;
99cdf0e10cSrcweir     aDLocs = NULL;
100cdf0e10cSrcweir     if (aDNames) delete[] aDNames;
101cdf0e10cSrcweir     aDNames = NULL;
102cdf0e10cSrcweir     if (pPropHelper)
103cdf0e10cSrcweir         pPropHelper->RemoveAsPropListener();
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 
GetPropHelper_Impl()107cdf0e10cSrcweir PropertyHelper_Spell & SpellChecker::GetPropHelper_Impl()
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	if (!pPropHelper)
110cdf0e10cSrcweir 	{
111cdf0e10cSrcweir 		Reference< XPropertySet	>	xPropSet( GetLinguProperties(), UNO_QUERY );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 		pPropHelper	= new PropertyHelper_Spell( (XSpellChecker *) this, xPropSet );
114cdf0e10cSrcweir 		xPropHelper = pPropHelper;
115cdf0e10cSrcweir 		pPropHelper->AddAsPropListener();	//! after a reference is established
116cdf0e10cSrcweir 	}
117cdf0e10cSrcweir 	return *pPropHelper;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 
getLocales()121cdf0e10cSrcweir Sequence< Locale > SAL_CALL SpellChecker::getLocales()
122cdf0e10cSrcweir 		throw(RuntimeException)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir     MutexGuard  aGuard( GetLinguMutex() );
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     // this routine should return the locales supported by the installed
127cdf0e10cSrcweir     // dictionaries.
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     if (!numdict)
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir         SvtLinguConfig aLinguCfg;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         // get list of extension dictionaries-to-use
134cdf0e10cSrcweir 		// (or better speaking: the list of dictionaries using the
135cdf0e10cSrcweir 		// new configuration entries).
136cdf0e10cSrcweir         std::list< SvtLinguConfigDictionaryEntry > aDics;
137cdf0e10cSrcweir         uno::Sequence< rtl::OUString > aFormatList;
138cdf0e10cSrcweir         aLinguCfg.GetSupportedDictionaryFormatsFor( A2OU("SpellCheckers"),
139cdf0e10cSrcweir                 A2OU("org.openoffice.lingu.MySpellSpellChecker"), aFormatList );
140cdf0e10cSrcweir         sal_Int32 nLen = aFormatList.getLength();
141cdf0e10cSrcweir         for (sal_Int32 i = 0;  i < nLen;  ++i)
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             std::vector< SvtLinguConfigDictionaryEntry > aTmpDic(
144cdf0e10cSrcweir                     aLinguCfg.GetActiveDictionariesByFormat( aFormatList[i] ) );
145cdf0e10cSrcweir             aDics.insert( aDics.end(), aTmpDic.begin(), aTmpDic.end() );
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         //!! for compatibility with old dictionaries (the ones not using extensions
149cdf0e10cSrcweir         //!! or new configuration entries, but still using the dictionary.lst file)
150cdf0e10cSrcweir 		//!! Get the list of old style spell checking dictionaries to use...
151cdf0e10cSrcweir         std::vector< SvtLinguConfigDictionaryEntry > aOldStyleDics(
152cdf0e10cSrcweir 				GetOldStyleDics( "DICT" ) );
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 		// to prefer dictionaries with configuration entries we will only
155cdf0e10cSrcweir 		// use those old style dictionaries that add a language that
156cdf0e10cSrcweir 		// is not yet supported by the list od new style dictionaries
157cdf0e10cSrcweir 		MergeNewStyleDicsAndOldStyleDics( aDics, aOldStyleDics );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         numdict = aDics.size();
160cdf0e10cSrcweir         if (numdict)
161cdf0e10cSrcweir         {
162cdf0e10cSrcweir             // get supported locales from the dictionaries-to-use...
163cdf0e10cSrcweir             sal_Int32 k = 0;
164cdf0e10cSrcweir             std::set< rtl::OUString, lt_rtl_OUString > aLocaleNamesSet;
165cdf0e10cSrcweir             std::list< SvtLinguConfigDictionaryEntry >::const_iterator aDictIt;
166cdf0e10cSrcweir             for (aDictIt = aDics.begin();  aDictIt != aDics.end();  ++aDictIt)
167cdf0e10cSrcweir             {
168cdf0e10cSrcweir                 uno::Sequence< rtl::OUString > aLocaleNames( aDictIt->aLocaleNames );
169cdf0e10cSrcweir                 sal_Int32 nLen2 = aLocaleNames.getLength();
170cdf0e10cSrcweir                 for (k = 0;  k < nLen2;  ++k)
171cdf0e10cSrcweir                 {
172*2d4eee10Smseidel                     aLocaleNamesSet.insert( aLocaleNames[k] );
173cdf0e10cSrcweir                 }
174cdf0e10cSrcweir             }
175cdf0e10cSrcweir             // ... and add them to the resulting sequence
176cdf0e10cSrcweir             aSuppLocales.realloc( aLocaleNamesSet.size() );
177cdf0e10cSrcweir             std::set< rtl::OUString, lt_rtl_OUString >::const_iterator aItB;
178cdf0e10cSrcweir             k = 0;
179cdf0e10cSrcweir             for (aItB = aLocaleNamesSet.begin();  aItB != aLocaleNamesSet.end();  ++aItB)
180cdf0e10cSrcweir             {
181cdf0e10cSrcweir 				Locale aTmp( MsLangId::convertLanguageToLocale(
182cdf0e10cSrcweir 						MsLangId::convertIsoStringToLanguage( *aItB )));
183cdf0e10cSrcweir                 aSuppLocales[k++] = aTmp;
184cdf0e10cSrcweir             }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir             //! For each dictionary and each locale we need a seperate entry.
187cdf0e10cSrcweir             //! If this results in more than one dictionary per locale than (for now)
188cdf0e10cSrcweir 			//! it is undefined which dictionary gets used.
189cdf0e10cSrcweir 			//! In the future the implementation should support using several dictionaries
190cdf0e10cSrcweir 			//! for one locale.
191cdf0e10cSrcweir 			numdict = 0;
192cdf0e10cSrcweir             for (aDictIt = aDics.begin();  aDictIt != aDics.end();  ++aDictIt)
193cdf0e10cSrcweir 				numdict = numdict + aDictIt->aLocaleNames.getLength();
194cdf0e10cSrcweir 
195cdf0e10cSrcweir             // add dictionary information
196cdf0e10cSrcweir             aDicts  = new Hunspell* [numdict];
197cdf0e10cSrcweir             aDEncs  = new rtl_TextEncoding [numdict];
198cdf0e10cSrcweir             aDLocs  = new Locale [numdict];
199cdf0e10cSrcweir             aDNames = new OUString [numdict];
200cdf0e10cSrcweir             k = 0;
201cdf0e10cSrcweir             for (aDictIt = aDics.begin();  aDictIt != aDics.end();  ++aDictIt)
202cdf0e10cSrcweir             {
203cdf0e10cSrcweir                 if (aDictIt->aLocaleNames.getLength() > 0 &&
204cdf0e10cSrcweir                     aDictIt->aLocations.getLength() > 0)
205cdf0e10cSrcweir                 {
206cdf0e10cSrcweir                     uno::Sequence< rtl::OUString > aLocaleNames( aDictIt->aLocaleNames );
207cdf0e10cSrcweir                     sal_Int32 nLocales = aLocaleNames.getLength();
208cdf0e10cSrcweir 
209cdf0e10cSrcweir                     // currently only one language per dictionary is supported in the actual implementation...
210cdf0e10cSrcweir                     // Thus here we work-around this by adding the same dictionary several times.
211cdf0e10cSrcweir                     // Once for each of it's supported locales.
212cdf0e10cSrcweir                     for (sal_Int32 i = 0;  i < nLocales;  ++i)
213cdf0e10cSrcweir                     {
214cdf0e10cSrcweir                         aDicts[k]  = NULL;
215cdf0e10cSrcweir                         aDEncs[k]  = RTL_TEXTENCODING_DONTKNOW;
216cdf0e10cSrcweir                         aDLocs[k]  = MsLangId::convertLanguageToLocale(
217cdf0e10cSrcweir                                         MsLangId::convertIsoStringToLanguage( aLocaleNames[i] ));
218cdf0e10cSrcweir                         // also both files have to be in the same directory and the
219cdf0e10cSrcweir                         // file names must only differ in the extension (.aff/.dic).
220cdf0e10cSrcweir                         // Thus we use the first location only and strip the extension part.
221cdf0e10cSrcweir                         rtl::OUString aLocation = aDictIt->aLocations[0];
222cdf0e10cSrcweir                         sal_Int32 nPos = aLocation.lastIndexOf( '.' );
223cdf0e10cSrcweir                         aLocation = aLocation.copy( 0, nPos );
224cdf0e10cSrcweir                         aDNames[k] = aLocation;
225cdf0e10cSrcweir 
226cdf0e10cSrcweir                         ++k;
227cdf0e10cSrcweir                     }
228cdf0e10cSrcweir                 }
229cdf0e10cSrcweir             }
230cdf0e10cSrcweir             DBG_ASSERT( k == numdict, "index mismatch?" );
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir         else
233cdf0e10cSrcweir         {
234cdf0e10cSrcweir             /* no dictionary found so register no dictionaries */
235cdf0e10cSrcweir             numdict = 0;
236cdf0e10cSrcweir             aDicts  = NULL;
237cdf0e10cSrcweir             aDEncs  = NULL;
238cdf0e10cSrcweir             aDLocs  = NULL;
239cdf0e10cSrcweir             aDNames = NULL;
240cdf0e10cSrcweir             aSuppLocales.realloc(0);
241cdf0e10cSrcweir         }
242cdf0e10cSrcweir     }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 	return aSuppLocales;
245cdf0e10cSrcweir }
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 
hasLocale(const Locale & rLocale)248cdf0e10cSrcweir sal_Bool SAL_CALL SpellChecker::hasLocale(const Locale& rLocale)
249cdf0e10cSrcweir 		throw(RuntimeException)
250cdf0e10cSrcweir {
251cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 	sal_Bool bRes = sal_False;
254cdf0e10cSrcweir 	if (!aSuppLocales.getLength())
255cdf0e10cSrcweir 		getLocales();
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 	sal_Int32 nLen = aSuppLocales.getLength();
258cdf0e10cSrcweir 	for (sal_Int32 i = 0;  i < nLen;  ++i)
259cdf0e10cSrcweir 	{
260cdf0e10cSrcweir 		const Locale *pLocale = aSuppLocales.getConstArray();
261cdf0e10cSrcweir 		if (rLocale == pLocale[i])
262cdf0e10cSrcweir 		{
263cdf0e10cSrcweir 			bRes = sal_True;
264cdf0e10cSrcweir 			break;
265cdf0e10cSrcweir 		}
266cdf0e10cSrcweir 	}
267cdf0e10cSrcweir 	return bRes;
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 
GetSpellFailure(const OUString & rWord,const Locale & rLocale)271cdf0e10cSrcweir sal_Int16 SpellChecker::GetSpellFailure( const OUString &rWord, const Locale &rLocale )
272cdf0e10cSrcweir {
273cdf0e10cSrcweir     Hunspell * pMS = NULL;
274cdf0e10cSrcweir     rtl_TextEncoding eEnc = RTL_TEXTENCODING_DONTKNOW;
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 	// initialize a myspell object for each dictionary once
277cdf0e10cSrcweir     // (note: mutex is held higher up in isValid)
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 	sal_Int16 nRes = -1;
280cdf0e10cSrcweir 
281cdf0e10cSrcweir     // first handle smart quotes both single and double
282cdf0e10cSrcweir 	OUStringBuffer rBuf(rWord);
283cdf0e10cSrcweir     sal_Int32 n = rBuf.getLength();
284cdf0e10cSrcweir     sal_Unicode c;
285cdf0e10cSrcweir     for (sal_Int32 ix=0; ix < n; ix++)
286cdf0e10cSrcweir     {
287cdf0e10cSrcweir 	    c = rBuf.charAt(ix);
288cdf0e10cSrcweir         if ((c == 0x201C) || (c == 0x201D))
289cdf0e10cSrcweir             rBuf.setCharAt(ix,(sal_Unicode)0x0022);
290cdf0e10cSrcweir         if ((c == 0x2018) || (c == 0x2019))
291cdf0e10cSrcweir             rBuf.setCharAt(ix,(sal_Unicode)0x0027);
292cdf0e10cSrcweir     }
293cdf0e10cSrcweir     OUString nWord(rBuf.makeStringAndClear());
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 	if (n)
296cdf0e10cSrcweir 	{
297cdf0e10cSrcweir         for (sal_Int32 i = 0; i < numdict; ++i)
298cdf0e10cSrcweir         {
299cdf0e10cSrcweir             pMS = NULL;
300cdf0e10cSrcweir             eEnc = RTL_TEXTENCODING_DONTKNOW;
301cdf0e10cSrcweir 
302cdf0e10cSrcweir             if (rLocale == aDLocs[i])
303cdf0e10cSrcweir             {
304cdf0e10cSrcweir                 if (!aDicts[i])
305cdf0e10cSrcweir                 {
306cdf0e10cSrcweir                     OUString dicpath = aDNames[i] + A2OU(".dic");
307cdf0e10cSrcweir                     OUString affpath = aDNames[i] + A2OU(".aff");
308cdf0e10cSrcweir                     OUString dict;
309cdf0e10cSrcweir                     OUString aff;
310cdf0e10cSrcweir                     osl::FileBase::getSystemPathFromFileURL(dicpath,dict);
311cdf0e10cSrcweir                     osl::FileBase::getSystemPathFromFileURL(affpath,aff);
312cdf0e10cSrcweir                     OString aTmpaff(OU2ENC(aff,osl_getThreadTextEncoding()));
313cdf0e10cSrcweir                     OString aTmpdict(OU2ENC(dict,osl_getThreadTextEncoding()));
314cdf0e10cSrcweir 
315cdf0e10cSrcweir #if defined(WNT)
316cdf0e10cSrcweir                     // workaround for Windows specifc problem that the
317cdf0e10cSrcweir                     // path length in calls to 'fopen' is limted to somewhat
318cdf0e10cSrcweir                     // about 120+ characters which will usually be exceed when
319cdf0e10cSrcweir                     // using dictionaries as extensions.
320cdf0e10cSrcweir                     aTmpaff = Win_GetShortPathName( aff );
321cdf0e10cSrcweir                     aTmpdict = Win_GetShortPathName( dict );
322cdf0e10cSrcweir #endif
323cdf0e10cSrcweir 
324cdf0e10cSrcweir                     aDicts[i] = new Hunspell(aTmpaff.getStr(),aTmpdict.getStr());
325cdf0e10cSrcweir                     aDEncs[i] = RTL_TEXTENCODING_DONTKNOW;
326cdf0e10cSrcweir                     if (aDicts[i])
327cdf0e10cSrcweir                         aDEncs[i] = getTextEncodingFromCharset(aDicts[i]->get_dic_encoding());
328cdf0e10cSrcweir                 }
329cdf0e10cSrcweir                 pMS = aDicts[i];
330cdf0e10cSrcweir                 eEnc = aDEncs[i];
331cdf0e10cSrcweir             }
332cdf0e10cSrcweir 
333cdf0e10cSrcweir             if (pMS)
334cdf0e10cSrcweir             {
335cdf0e10cSrcweir                 // we don't want to work with a default text encoding since following incorrect
336cdf0e10cSrcweir                 // results may occur only for specific text and thus may be hard to notice.
337cdf0e10cSrcweir                 // Thus better always make a clean exit here if the text encoding is in question.
338cdf0e10cSrcweir                 // Hopefully something not working at all will raise proper attention quickly. ;-)
339cdf0e10cSrcweir                 DBG_ASSERT( eEnc != RTL_TEXTENCODING_DONTKNOW, "failed to get text encoding! (maybe incorrect encoding string in file)" );
340cdf0e10cSrcweir                 if (eEnc == RTL_TEXTENCODING_DONTKNOW)
341cdf0e10cSrcweir                     return -1;
342cdf0e10cSrcweir 
343cdf0e10cSrcweir                 OString aWrd(OU2ENC(nWord,eEnc));
344cdf0e10cSrcweir                 int rVal = pMS->spell((char*)aWrd.getStr());
345cdf0e10cSrcweir                 if (rVal != 1)
346cdf0e10cSrcweir                     nRes = SpellFailure::SPELLING_ERROR;
347cdf0e10cSrcweir                 else
348cdf0e10cSrcweir                     return -1;
349cdf0e10cSrcweir                 pMS = NULL;
350cdf0e10cSrcweir             }
351cdf0e10cSrcweir         }
352cdf0e10cSrcweir 	}
353cdf0e10cSrcweir 
354cdf0e10cSrcweir 	return nRes;
355cdf0e10cSrcweir }
356cdf0e10cSrcweir 
357cdf0e10cSrcweir 
isValid(const OUString & rWord,const Locale & rLocale,const PropertyValues & rProperties)358cdf0e10cSrcweir sal_Bool SAL_CALL SpellChecker::isValid( const OUString& rWord, const Locale& rLocale,
359cdf0e10cSrcweir 			const PropertyValues& rProperties )
360cdf0e10cSrcweir 		throw(IllegalArgumentException, RuntimeException)
361cdf0e10cSrcweir {
362cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
363cdf0e10cSrcweir 
364cdf0e10cSrcweir  	if (rLocale == Locale()  ||  !rWord.getLength())
365cdf0e10cSrcweir 		return sal_True;
366cdf0e10cSrcweir 
367cdf0e10cSrcweir 	if (!hasLocale( rLocale ))
368cdf0e10cSrcweir #ifdef LINGU_EXCEPTIONS
369cdf0e10cSrcweir 		throw( IllegalArgumentException() );
370cdf0e10cSrcweir #else
371cdf0e10cSrcweir 		return sal_True;
372cdf0e10cSrcweir #endif
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 	// return sal_False to process SPELLML requests (they are longer than the header)
375cdf0e10cSrcweir 	if (rWord.match(A2OU(SPELLML_HEADER), 0) && (rWord.getLength() > 10)) return sal_False;
376cdf0e10cSrcweir 
377cdf0e10cSrcweir 	// Get property values to be used.
378cdf0e10cSrcweir 	// These are be the default values set in the SN_LINGU_PROPERTIES
379cdf0e10cSrcweir 	// PropertySet which are overridden by the supplied ones from the
380cdf0e10cSrcweir 	// last argument.
381cdf0e10cSrcweir 	// You'll probably like to use a simplier solution than the provided
382cdf0e10cSrcweir 	// one using the PropertyHelper_Spell.
383cdf0e10cSrcweir 
384cdf0e10cSrcweir 	PropertyHelper_Spell &rHelper = GetPropHelper();
385cdf0e10cSrcweir 	rHelper.SetTmpPropVals( rProperties );
386cdf0e10cSrcweir 
387cdf0e10cSrcweir 	sal_Int16 nFailure = GetSpellFailure( rWord, rLocale );
388cdf0e10cSrcweir 	if (nFailure != -1 && !rWord.match(A2OU(SPELLML_HEADER), 0))
389cdf0e10cSrcweir 	{
390cdf0e10cSrcweir 		sal_Int16 nLang = LocaleToLanguage( rLocale );
391cdf0e10cSrcweir 		// postprocess result for errors that should be ignored
392cdf0e10cSrcweir         const bool bIgnoreError =
393cdf0e10cSrcweir                 (!rHelper.IsSpellUpperCase()  && IsUpper( rWord, nLang )) ||
394cdf0e10cSrcweir                 (!rHelper.IsSpellWithDigits() && HasDigits( rWord )) ||
395cdf0e10cSrcweir                 (!rHelper.IsSpellCapitalization()  &&  nFailure == SpellFailure::CAPTION_ERROR);
396cdf0e10cSrcweir         if (bIgnoreError)
397cdf0e10cSrcweir             nFailure = -1;
398cdf0e10cSrcweir 	}
399cdf0e10cSrcweir 
400cdf0e10cSrcweir 	return (nFailure == -1);
401cdf0e10cSrcweir }
402cdf0e10cSrcweir 
403cdf0e10cSrcweir 
404cdf0e10cSrcweir Reference< XSpellAlternatives >
GetProposals(const OUString & rWord,const Locale & rLocale)405cdf0e10cSrcweir 	SpellChecker::GetProposals( const OUString &rWord, const Locale &rLocale )
406cdf0e10cSrcweir {
407cdf0e10cSrcweir 	// Retrieves the return values for the 'spell' function call in case
408cdf0e10cSrcweir 	// of a misspelled word.
409cdf0e10cSrcweir 	// Especially it may give a list of suggested (correct) words:
410cdf0e10cSrcweir 
411cdf0e10cSrcweir 	Reference< XSpellAlternatives > xRes;
412cdf0e10cSrcweir     // note: mutex is held by higher up by spell which covers both
413cdf0e10cSrcweir 
414cdf0e10cSrcweir     Hunspell* pMS = NULL;
415cdf0e10cSrcweir     rtl_TextEncoding eEnc = RTL_TEXTENCODING_DONTKNOW;
416cdf0e10cSrcweir     int count = 0;
417cdf0e10cSrcweir     int numsug = 0;
418cdf0e10cSrcweir 
419cdf0e10cSrcweir     // first handle smart quotes (single and double)
420cdf0e10cSrcweir 	OUStringBuffer rBuf(rWord);
421cdf0e10cSrcweir     sal_Int32 n = rBuf.getLength();
422cdf0e10cSrcweir     sal_Unicode c;
423cdf0e10cSrcweir     for (sal_Int32 ix=0; ix < n; ix++)
424cdf0e10cSrcweir     {
425cdf0e10cSrcweir         c = rBuf.charAt(ix);
426cdf0e10cSrcweir         if ((c == 0x201C) || (c == 0x201D))
427cdf0e10cSrcweir             rBuf.setCharAt(ix,(sal_Unicode)0x0022);
428cdf0e10cSrcweir         if ((c == 0x2018) || (c == 0x2019))
429cdf0e10cSrcweir             rBuf.setCharAt(ix,(sal_Unicode)0x0027);
430cdf0e10cSrcweir     }
431cdf0e10cSrcweir     OUString nWord(rBuf.makeStringAndClear());
432cdf0e10cSrcweir 
433cdf0e10cSrcweir 	if (n)
434cdf0e10cSrcweir 	{
435cdf0e10cSrcweir         sal_Int16 nLang = LocaleToLanguage( rLocale );
436cdf0e10cSrcweir 
437cdf0e10cSrcweir 	    Sequence< OUString > aStr( 0 );
438cdf0e10cSrcweir 
439cdf0e10cSrcweir         for (int i =0; i < numdict; i++)
440cdf0e10cSrcweir         {
441cdf0e10cSrcweir             pMS = NULL;
442cdf0e10cSrcweir             eEnc = RTL_TEXTENCODING_DONTKNOW;
443cdf0e10cSrcweir             count = 0;
444cdf0e10cSrcweir 
445cdf0e10cSrcweir             if (rLocale == aDLocs[i])
446cdf0e10cSrcweir             {
447cdf0e10cSrcweir                 pMS = aDicts[i];
448cdf0e10cSrcweir                 eEnc = aDEncs[i];
449cdf0e10cSrcweir             }
450cdf0e10cSrcweir 
451cdf0e10cSrcweir 	        if (pMS)
452cdf0e10cSrcweir 	        {
453cdf0e10cSrcweir                 char ** suglst = NULL;
454cdf0e10cSrcweir                 OString aWrd(OU2ENC(nWord,eEnc));
455cdf0e10cSrcweir                 count = pMS->suggest(&suglst, (const char *) aWrd.getStr());
456cdf0e10cSrcweir 
457cdf0e10cSrcweir                 if (count)
458cdf0e10cSrcweir                 {
459cdf0e10cSrcweir                     aStr.realloc( numsug + count );
460cdf0e10cSrcweir                     OUString *pStr = aStr.getArray();
461cdf0e10cSrcweir                     for (int ii=0; ii < count; ++ii)
462cdf0e10cSrcweir                     {
463cdf0e10cSrcweir                         OUString cvtwrd(suglst[ii],strlen(suglst[ii]),eEnc);
464cdf0e10cSrcweir                         pStr[numsug + ii] = cvtwrd;
465cdf0e10cSrcweir                     }
466cdf0e10cSrcweir                     pMS->free_list(&suglst, count);
467cdf0e10cSrcweir                     numsug += count;
468cdf0e10cSrcweir                 }
469cdf0e10cSrcweir             }
470cdf0e10cSrcweir 	    }
471cdf0e10cSrcweir 
472cdf0e10cSrcweir         // now return an empty alternative for no suggestions or the list of alternatives if some found
473cdf0e10cSrcweir 	    SpellAlternatives *pAlt = new SpellAlternatives;
474cdf0e10cSrcweir         String aTmp(rWord);
475cdf0e10cSrcweir 	    pAlt->SetWordLanguage( aTmp, nLang );
476cdf0e10cSrcweir 	    pAlt->SetFailureType( SpellFailure::SPELLING_ERROR );
477cdf0e10cSrcweir 	    pAlt->SetAlternatives( aStr );
478cdf0e10cSrcweir 	    xRes = pAlt;
479cdf0e10cSrcweir         return xRes;
480cdf0e10cSrcweir 	}
481cdf0e10cSrcweir     return xRes;
482cdf0e10cSrcweir }
483cdf0e10cSrcweir 
484cdf0e10cSrcweir 
spell(const OUString & rWord,const Locale & rLocale,const PropertyValues & rProperties)485cdf0e10cSrcweir Reference< XSpellAlternatives > SAL_CALL SpellChecker::spell(
486cdf0e10cSrcweir         const OUString& rWord, const Locale& rLocale,
487cdf0e10cSrcweir         const PropertyValues& rProperties )
488cdf0e10cSrcweir 		throw(IllegalArgumentException, RuntimeException)
489cdf0e10cSrcweir {
490cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
491cdf0e10cSrcweir 
492cdf0e10cSrcweir  	if (rLocale == Locale()  ||  !rWord.getLength())
493cdf0e10cSrcweir 		return NULL;
494cdf0e10cSrcweir 
495cdf0e10cSrcweir 	if (!hasLocale( rLocale ))
496cdf0e10cSrcweir #ifdef LINGU_EXCEPTIONS
497cdf0e10cSrcweir 		throw( IllegalArgumentException() );
498cdf0e10cSrcweir #else
499cdf0e10cSrcweir 		return NULL;
500cdf0e10cSrcweir #endif
501cdf0e10cSrcweir 
502cdf0e10cSrcweir 	Reference< XSpellAlternatives > xAlt;
503cdf0e10cSrcweir 	if (!isValid( rWord, rLocale, rProperties ))
504cdf0e10cSrcweir 	{
505cdf0e10cSrcweir 		xAlt =  GetProposals( rWord, rLocale );
506cdf0e10cSrcweir 	}
507cdf0e10cSrcweir 	return xAlt;
508cdf0e10cSrcweir }
509cdf0e10cSrcweir 
510cdf0e10cSrcweir 
SpellChecker_CreateInstance(const Reference<XMultiServiceFactory> &)511cdf0e10cSrcweir Reference< XInterface > SAL_CALL SpellChecker_CreateInstance(
512cdf0e10cSrcweir         const Reference< XMultiServiceFactory > & /*rSMgr*/ )
513cdf0e10cSrcweir         throw(Exception)
514cdf0e10cSrcweir {
515cdf0e10cSrcweir 
516cdf0e10cSrcweir 	Reference< XInterface > xService = (cppu::OWeakObject*) new SpellChecker;
517cdf0e10cSrcweir 	return xService;
518cdf0e10cSrcweir }
519cdf0e10cSrcweir 
520cdf0e10cSrcweir 
addLinguServiceEventListener(const Reference<XLinguServiceEventListener> & rxLstnr)521cdf0e10cSrcweir sal_Bool SAL_CALL SpellChecker::addLinguServiceEventListener(
522cdf0e10cSrcweir         const Reference< XLinguServiceEventListener >& rxLstnr )
523cdf0e10cSrcweir         throw(RuntimeException)
524cdf0e10cSrcweir {
525cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
526cdf0e10cSrcweir 
527cdf0e10cSrcweir 	sal_Bool bRes = sal_False;
528cdf0e10cSrcweir 	if (!bDisposing && rxLstnr.is())
529cdf0e10cSrcweir 	{
530cdf0e10cSrcweir 		bRes = GetPropHelper().addLinguServiceEventListener( rxLstnr );
531cdf0e10cSrcweir 	}
532cdf0e10cSrcweir 	return bRes;
533cdf0e10cSrcweir }
534cdf0e10cSrcweir 
535cdf0e10cSrcweir 
removeLinguServiceEventListener(const Reference<XLinguServiceEventListener> & rxLstnr)536cdf0e10cSrcweir sal_Bool SAL_CALL SpellChecker::removeLinguServiceEventListener(
537cdf0e10cSrcweir         const Reference< XLinguServiceEventListener >& rxLstnr )
538cdf0e10cSrcweir         throw(RuntimeException)
539cdf0e10cSrcweir {
540cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
541cdf0e10cSrcweir 
542cdf0e10cSrcweir 	sal_Bool bRes = sal_False;
543cdf0e10cSrcweir 	if (!bDisposing && rxLstnr.is())
544cdf0e10cSrcweir 	{
545cdf0e10cSrcweir 		DBG_ASSERT( xPropHelper.is(), "xPropHelper non existent" );
546cdf0e10cSrcweir 		bRes = GetPropHelper().removeLinguServiceEventListener( rxLstnr );
547cdf0e10cSrcweir 	}
548cdf0e10cSrcweir 	return bRes;
549cdf0e10cSrcweir }
550cdf0e10cSrcweir 
551cdf0e10cSrcweir 
getServiceDisplayName(const Locale &)552cdf0e10cSrcweir OUString SAL_CALL SpellChecker::getServiceDisplayName( const Locale& /*rLocale*/ )
553cdf0e10cSrcweir 		throw(RuntimeException)
554cdf0e10cSrcweir {
555cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
556cdf0e10cSrcweir 	return A2OU( "Hunspell SpellChecker" );
557cdf0e10cSrcweir }
558cdf0e10cSrcweir 
559cdf0e10cSrcweir 
initialize(const Sequence<Any> & rArguments)560cdf0e10cSrcweir void SAL_CALL SpellChecker::initialize( const Sequence< Any >& rArguments )
561cdf0e10cSrcweir 		throw(Exception, RuntimeException)
562cdf0e10cSrcweir {
563cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
564cdf0e10cSrcweir 
565cdf0e10cSrcweir 	if (!pPropHelper)
566cdf0e10cSrcweir 	{
567cdf0e10cSrcweir 		sal_Int32 nLen = rArguments.getLength();
568cdf0e10cSrcweir 		if (2 == nLen)
569cdf0e10cSrcweir 		{
570cdf0e10cSrcweir 			Reference< XPropertySet	>	xPropSet;
571cdf0e10cSrcweir 			rArguments.getConstArray()[0] >>= xPropSet;
572cdf0e10cSrcweir 			//rArguments.getConstArray()[1] >>= xDicList;
573cdf0e10cSrcweir 
574cdf0e10cSrcweir 			//! Pointer allows for access of the non-UNO functions.
575cdf0e10cSrcweir 			//! And the reference to the UNO-functions while increasing
576cdf0e10cSrcweir 			//! the ref-count and will implicitly free the memory
577cdf0e10cSrcweir 			//! when the object is not longer used.
578cdf0e10cSrcweir 			pPropHelper = new PropertyHelper_Spell( (XSpellChecker *) this, xPropSet );
579cdf0e10cSrcweir 			xPropHelper = pPropHelper;
580cdf0e10cSrcweir 			pPropHelper->AddAsPropListener();	//! after a reference is established
581cdf0e10cSrcweir 		}
582cdf0e10cSrcweir         else
583cdf0e10cSrcweir         {
584cdf0e10cSrcweir 			DBG_ERROR( "wrong number of arguments in sequence" );
585cdf0e10cSrcweir         }
586cdf0e10cSrcweir 	}
587cdf0e10cSrcweir }
588cdf0e10cSrcweir 
589cdf0e10cSrcweir 
dispose()590cdf0e10cSrcweir void SAL_CALL SpellChecker::dispose()
591cdf0e10cSrcweir 		throw(RuntimeException)
592cdf0e10cSrcweir {
593cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
594cdf0e10cSrcweir 
595cdf0e10cSrcweir 	if (!bDisposing)
596cdf0e10cSrcweir 	{
597cdf0e10cSrcweir 		bDisposing = sal_True;
598cdf0e10cSrcweir 		EventObject	aEvtObj( (XSpellChecker *) this );
599cdf0e10cSrcweir 		aEvtListeners.disposeAndClear( aEvtObj );
600cdf0e10cSrcweir 	}
601cdf0e10cSrcweir }
602cdf0e10cSrcweir 
603cdf0e10cSrcweir 
addEventListener(const Reference<XEventListener> & rxListener)604cdf0e10cSrcweir void SAL_CALL SpellChecker::addEventListener( const Reference< XEventListener >& rxListener )
605cdf0e10cSrcweir 		throw(RuntimeException)
606cdf0e10cSrcweir {
607cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
608cdf0e10cSrcweir 
609cdf0e10cSrcweir 	if (!bDisposing && rxListener.is())
610cdf0e10cSrcweir 		aEvtListeners.addInterface( rxListener );
611cdf0e10cSrcweir }
612cdf0e10cSrcweir 
613cdf0e10cSrcweir 
removeEventListener(const Reference<XEventListener> & rxListener)614cdf0e10cSrcweir void SAL_CALL SpellChecker::removeEventListener( const Reference< XEventListener >& rxListener )
615cdf0e10cSrcweir 		throw(RuntimeException)
616cdf0e10cSrcweir {
617cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
618cdf0e10cSrcweir 
619cdf0e10cSrcweir 	if (!bDisposing && rxListener.is())
620cdf0e10cSrcweir 		aEvtListeners.removeInterface( rxListener );
621cdf0e10cSrcweir }
622cdf0e10cSrcweir 
623cdf0e10cSrcweir 
624cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
625cdf0e10cSrcweir // Service specific part
626cdf0e10cSrcweir //
627cdf0e10cSrcweir 
getImplementationName()628cdf0e10cSrcweir OUString SAL_CALL SpellChecker::getImplementationName()
629cdf0e10cSrcweir 		throw(RuntimeException)
630cdf0e10cSrcweir {
631cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
632cdf0e10cSrcweir 
633cdf0e10cSrcweir 	return getImplementationName_Static();
634cdf0e10cSrcweir }
635cdf0e10cSrcweir 
636cdf0e10cSrcweir 
supportsService(const OUString & ServiceName)637cdf0e10cSrcweir sal_Bool SAL_CALL SpellChecker::supportsService( const OUString& ServiceName )
638cdf0e10cSrcweir 		throw(RuntimeException)
639cdf0e10cSrcweir {
640cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
641cdf0e10cSrcweir 
642cdf0e10cSrcweir 	Sequence< OUString > aSNL = getSupportedServiceNames();
643cdf0e10cSrcweir 	const OUString * pArray = aSNL.getConstArray();
644cdf0e10cSrcweir 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
645cdf0e10cSrcweir 		if( pArray[i] == ServiceName )
646cdf0e10cSrcweir 			return sal_True;
647cdf0e10cSrcweir 	return sal_False;
648cdf0e10cSrcweir }
649cdf0e10cSrcweir 
650cdf0e10cSrcweir 
getSupportedServiceNames()651cdf0e10cSrcweir Sequence< OUString > SAL_CALL SpellChecker::getSupportedServiceNames()
652cdf0e10cSrcweir 		throw(RuntimeException)
653cdf0e10cSrcweir {
654cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
655cdf0e10cSrcweir 
656cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
657cdf0e10cSrcweir }
658cdf0e10cSrcweir 
659cdf0e10cSrcweir 
getSupportedServiceNames_Static()660cdf0e10cSrcweir Sequence< OUString > SpellChecker::getSupportedServiceNames_Static()
661cdf0e10cSrcweir 		throw()
662cdf0e10cSrcweir {
663cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
664cdf0e10cSrcweir 
665cdf0e10cSrcweir 	Sequence< OUString > aSNS( 1 );	// auch mehr als 1 Service moeglich
666cdf0e10cSrcweir 	aSNS.getArray()[0] = A2OU( SN_SPELLCHECKER );
667cdf0e10cSrcweir 	return aSNS;
668cdf0e10cSrcweir }
669cdf0e10cSrcweir 
SpellChecker_getFactory(const sal_Char * pImplName,XMultiServiceFactory * pServiceManager,void *)670cdf0e10cSrcweir void * SAL_CALL SpellChecker_getFactory( const sal_Char * pImplName,
671cdf0e10cSrcweir 			XMultiServiceFactory * pServiceManager, void *  )
672cdf0e10cSrcweir {
673cdf0e10cSrcweir 	void * pRet = 0;
674cdf0e10cSrcweir 	if ( !SpellChecker::getImplementationName_Static().compareToAscii( pImplName ) )
675cdf0e10cSrcweir 	{
676cdf0e10cSrcweir 		Reference< XSingleServiceFactory > xFactory =
677cdf0e10cSrcweir 			cppu::createOneInstanceFactory(
678cdf0e10cSrcweir 				pServiceManager,
679cdf0e10cSrcweir 				SpellChecker::getImplementationName_Static(),
680cdf0e10cSrcweir 				SpellChecker_CreateInstance,
681cdf0e10cSrcweir 				SpellChecker::getSupportedServiceNames_Static());
682cdf0e10cSrcweir 		// acquire, because we return an interface pointer instead of a reference
683cdf0e10cSrcweir 		xFactory->acquire();
684cdf0e10cSrcweir 		pRet = xFactory.get();
685cdf0e10cSrcweir 	}
686cdf0e10cSrcweir 	return pRet;
687cdf0e10cSrcweir }
688cdf0e10cSrcweir 
689cdf0e10cSrcweir 
690cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
691