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