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 #include <com/sun/star/uno/Reference.h>
27cdf0e10cSrcweir #include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <com/sun/star/linguistic2/SpellFailure.hpp>
30cdf0e10cSrcweir #include <cppuhelper/factory.hxx>	// helper for factories
31cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
32cdf0e10cSrcweir #include <tools/debug.hxx>
33cdf0e10cSrcweir #include <unotools/processfactory.hxx>
34cdf0e10cSrcweir #include <osl/mutex.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <macspellimp.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <linguistic/spelldta.hxx>
39cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
40cdf0e10cSrcweir #include <unotools/useroptions.hxx>
41cdf0e10cSrcweir #include <osl/file.hxx>
42cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using namespace utl;
46cdf0e10cSrcweir using namespace osl;
47cdf0e10cSrcweir using namespace rtl;
48cdf0e10cSrcweir using namespace com::sun::star;
49cdf0e10cSrcweir using namespace com::sun::star::beans;
50cdf0e10cSrcweir using namespace com::sun::star::lang;
51cdf0e10cSrcweir using namespace com::sun::star::uno;
52cdf0e10cSrcweir using namespace com::sun::star::linguistic2;
53cdf0e10cSrcweir using namespace linguistic;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir MacSpellChecker::MacSpellChecker() :
56cdf0e10cSrcweir 	aEvtListeners	( GetLinguMutex() )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	aDEncs = NULL;
59cdf0e10cSrcweir 	aDLocs = NULL;
60cdf0e10cSrcweir 	aDNames = NULL;
61cdf0e10cSrcweir 	bDisposing = sal_False;
62cdf0e10cSrcweir 	pPropHelper = NULL;
63cdf0e10cSrcweir     numdict = 0;
64cdf0e10cSrcweir     NSApplicationLoad();
65cdf0e10cSrcweir 	NSAutoreleasePool* pool	= [[NSAutoreleasePool alloc] init];
66cdf0e10cSrcweir     macSpell = [NSSpellChecker sharedSpellChecker];
67cdf0e10cSrcweir     macTag = [NSSpellChecker uniqueSpellDocumentTag];
68cdf0e10cSrcweir     [pool release];
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 
72cdf0e10cSrcweir MacSpellChecker::~MacSpellChecker()
73cdf0e10cSrcweir {
74cdf0e10cSrcweir   numdict = 0;
75cdf0e10cSrcweir   if (aDEncs) delete[] aDEncs;
76cdf0e10cSrcweir   aDEncs = NULL;
77cdf0e10cSrcweir   if (aDLocs) delete[] aDLocs;
78cdf0e10cSrcweir   aDLocs = NULL;
79cdf0e10cSrcweir   if (aDNames) delete[] aDNames;
80cdf0e10cSrcweir   aDNames = NULL;
81cdf0e10cSrcweir   if (pPropHelper)
82cdf0e10cSrcweir 	 pPropHelper->RemoveAsPropListener();
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
86cdf0e10cSrcweir PropertyHelper_Spell & MacSpellChecker::GetPropHelper_Impl()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	if (!pPropHelper)
89cdf0e10cSrcweir 	{
90cdf0e10cSrcweir 		Reference< XPropertySet	>	xPropSet( GetLinguProperties(), UNO_QUERY );
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 		pPropHelper	= new PropertyHelper_Spell( (XSpellChecker *) this, xPropSet );
93cdf0e10cSrcweir 		xPropHelper = pPropHelper;
94cdf0e10cSrcweir 		pPropHelper->AddAsPropListener();	//! after a reference is established
95cdf0e10cSrcweir 	}
96cdf0e10cSrcweir 	return *pPropHelper;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 
100cdf0e10cSrcweir Sequence< Locale > SAL_CALL MacSpellChecker::getLocales()
101cdf0e10cSrcweir 		throw(RuntimeException)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         // this routine should return the locales supported by the installed
106cdf0e10cSrcweir         // dictionaries.  So here we need to parse both the user edited
107cdf0e10cSrcweir         // dictionary list and the shared dictionary list
108cdf0e10cSrcweir         // to see what dictionaries the admin/user has installed
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         int numusr;          // number of user dictionary entries
111cdf0e10cSrcweir         int numshr;          // number of shared dictionary entries
112*c4436845SJürgen Schmidt         // dictentry * spdict;  // shared dict entry pointer
113*c4436845SJürgen Schmidt         // dictentry * updict;  // user dict entry pointer
114cdf0e10cSrcweir         SvtPathOptions aPathOpt;
115cdf0e10cSrcweir 		rtl_TextEncoding aEnc = RTL_TEXTENCODING_UTF8;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     	std::vector<objc_object *> postspdict;
118*c4436845SJürgen Schmidt         std::vector<objc_object *> postupdict;
119cdf0e10cSrcweir     	//std::vector<dictentry *> postspdict;
120*c4436845SJürgen Schmidt         //std::vector<dictentry *> postupdict;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	if (!numdict) {
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         // invoke a dictionary manager to get the user dictionary list
126cdf0e10cSrcweir         // TODO How on Mac OS X?
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         // invoke a second  dictionary manager to get the shared dictionary list
129cdf0e10cSrcweir         NSArray *aLocales = [NSLocale availableLocaleIdentifiers];
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         //Test for existence of the dictionaries
132cdf0e10cSrcweir         for (unsigned int i = 0; i < [aLocales count]; i++)
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             if( [macSpell setLanguage:[aLocales objectAtIndex:i] ] )
135cdf0e10cSrcweir             {
136cdf0e10cSrcweir                 postspdict.push_back( [ aLocales objectAtIndex:i ] );
137cdf0e10cSrcweir             }
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	    numusr = postupdict.size();
141cdf0e10cSrcweir         numshr = postspdict.size();
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         // we really should merge these and remove duplicates but since
144cdf0e10cSrcweir         // users can name their dictionaries anything they want it would
145cdf0e10cSrcweir         // be impossible to know if a real duplication exists unless we
146cdf0e10cSrcweir         // add some unique key to each myspell dictionary
147cdf0e10cSrcweir         numdict = numshr + numusr;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         if (numdict) {
150cdf0e10cSrcweir             aDLocs = new Locale [numdict];
151cdf0e10cSrcweir             aDEncs  = new rtl_TextEncoding [numdict];
152cdf0e10cSrcweir             aDNames = new OUString [numdict];
153cdf0e10cSrcweir             aSuppLocales.realloc(numdict);
154cdf0e10cSrcweir             Locale * pLocale = aSuppLocales.getArray();
155cdf0e10cSrcweir             int numlocs = 0;
156cdf0e10cSrcweir             int newloc;
157cdf0e10cSrcweir             int i,j;
158cdf0e10cSrcweir             int k = 0;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir             //first add the user dictionaries
161cdf0e10cSrcweir             //TODO for MAC?
162cdf0e10cSrcweir 
163cdf0e10cSrcweir             // now add the shared dictionaries
164cdf0e10cSrcweir             for (i = 0; i < numshr; i++) {
165cdf0e10cSrcweir                 NSDictionary *aLocDict = [ NSLocale componentsFromLocaleIdentifier:postspdict[i] ];
166cdf0e10cSrcweir                 NSString* aLang = [ aLocDict objectForKey:NSLocaleLanguageCode ];
167cdf0e10cSrcweir                 NSString* aCountry = [ aLocDict objectForKey:NSLocaleCountryCode ];
168cdf0e10cSrcweir                 OUString lang([aLang cStringUsingEncoding: NSUTF8StringEncoding], [aLang length], aEnc);
169cdf0e10cSrcweir                 OUString country([ aCountry cStringUsingEncoding: NSUTF8StringEncoding], [aCountry length], aEnc);
170cdf0e10cSrcweir                 Locale nLoc( lang, country, OUString() );
171cdf0e10cSrcweir                 newloc = 1;
172cdf0e10cSrcweir                 //eliminate duplicates (is this needed for MacOS?)
173cdf0e10cSrcweir                 for (j = 0; j < numlocs; j++) {
174cdf0e10cSrcweir                     if (nLoc == pLocale[j]) newloc = 0;
175cdf0e10cSrcweir                 }
176cdf0e10cSrcweir                 if (newloc) {
177cdf0e10cSrcweir                     pLocale[numlocs] = nLoc;
178cdf0e10cSrcweir                     numlocs++;
179cdf0e10cSrcweir                 }
180cdf0e10cSrcweir                 aDLocs[k] = nLoc;
181cdf0e10cSrcweir                 //pointer to Hunspell dictionary - not needed for MAC
182cdf0e10cSrcweir                 //aDicts[k] = NULL;
183cdf0e10cSrcweir                 aDEncs[k] = 0;
184cdf0e10cSrcweir                 // Dictionary file names not valid for Mac Spell
185cdf0e10cSrcweir                 //aDNames[k] = aPathOpt.GetLinguisticPath() + A2OU("/ooo/") + A2OU(postspdict[i]->filename);
186cdf0e10cSrcweir                 k++;
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir             aSuppLocales.realloc(numlocs);
190cdf0e10cSrcweir 
191cdf0e10cSrcweir         } else {
192cdf0e10cSrcweir             /* no dictionary.lst found so register no dictionaries */
193cdf0e10cSrcweir             numdict = 0;
194cdf0e10cSrcweir             //aDicts = NULL;
195cdf0e10cSrcweir                 aDEncs  = NULL;
196cdf0e10cSrcweir                 aDLocs = NULL;
197cdf0e10cSrcweir                 aDNames = NULL;
198cdf0e10cSrcweir 	            aSuppLocales.realloc(0);
199cdf0e10cSrcweir             }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir             /* de-allocation of memory is handled inside the DictMgr */
202*c4436845SJürgen Schmidt             // updict = NULL;
203*c4436845SJürgen Schmidt             // spdict = NULL;
204cdf0e10cSrcweir 
205cdf0e10cSrcweir         }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 	return aSuppLocales;
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 
212cdf0e10cSrcweir sal_Bool SAL_CALL MacSpellChecker::hasLocale(const Locale& rLocale)
213cdf0e10cSrcweir 		throw(RuntimeException)
214cdf0e10cSrcweir {
215cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 	sal_Bool bRes = sal_False;
218cdf0e10cSrcweir 	if (!aSuppLocales.getLength())
219cdf0e10cSrcweir 		getLocales();
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	sal_Int32 nLen = aSuppLocales.getLength();
222cdf0e10cSrcweir 	for (sal_Int32 i = 0;  i < nLen;  ++i)
223cdf0e10cSrcweir 	{
224cdf0e10cSrcweir 		const Locale *pLocale = aSuppLocales.getConstArray();
225cdf0e10cSrcweir 		if (rLocale == pLocale[i])
226cdf0e10cSrcweir 		{
227cdf0e10cSrcweir 			bRes = sal_True;
228cdf0e10cSrcweir 			break;
229cdf0e10cSrcweir 		}
230cdf0e10cSrcweir 	}
231cdf0e10cSrcweir 	return bRes;
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 
235cdf0e10cSrcweir sal_Int16 MacSpellChecker::GetSpellFailure( const OUString &rWord, const Locale &rLocale )
236cdf0e10cSrcweir {
237cdf0e10cSrcweir     rtl_TextEncoding aEnc;
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 	// initialize a myspell object for each dictionary once
240cdf0e10cSrcweir         // (note: mutex is held higher up in isValid)
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 	sal_Int16 nRes = -1;
244cdf0e10cSrcweir 
245cdf0e10cSrcweir         // first handle smart quotes both single and double
246cdf0e10cSrcweir 	OUStringBuffer rBuf(rWord);
247cdf0e10cSrcweir         sal_Int32 n = rBuf.getLength();
248cdf0e10cSrcweir         sal_Unicode c;
249cdf0e10cSrcweir 	for (sal_Int32 ix=0; ix < n; ix++) {
250cdf0e10cSrcweir 	    c = rBuf.charAt(ix);
251cdf0e10cSrcweir             if ((c == 0x201C) || (c == 0x201D)) rBuf.setCharAt(ix,(sal_Unicode)0x0022);
252cdf0e10cSrcweir             if ((c == 0x2018) || (c == 0x2019)) rBuf.setCharAt(ix,(sal_Unicode)0x0027);
253cdf0e10cSrcweir         }
254cdf0e10cSrcweir         OUString nWord(rBuf.makeStringAndClear());
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 	if (n)
257cdf0e10cSrcweir 	{
258cdf0e10cSrcweir         aEnc = 0;
259cdf0e10cSrcweir 		NSAutoreleasePool* pool	= [[NSAutoreleasePool alloc] init];
260cdf0e10cSrcweir         NSString* aNSStr = [[NSString alloc] initWithCharacters: nWord.getStr() length: nWord.getLength()];
261cdf0e10cSrcweir         NSString* aLang = [[NSString alloc] initWithCharacters: rLocale.Language.getStr() length: rLocale.Language.getLength()];
262cdf0e10cSrcweir         if(rLocale.Country.getLength()>0)
263cdf0e10cSrcweir         {
264cdf0e10cSrcweir             NSString* aCountry = [[NSString alloc] initWithCharacters: rLocale.Country.getStr() length: rLocale.Country.getLength()];
265cdf0e10cSrcweir             NSString* aTag = @"_";
266cdf0e10cSrcweir             NSString* aTaggedCountry = [aTag stringByAppendingString:aCountry];
267cdf0e10cSrcweir             [aLang autorelease];
268cdf0e10cSrcweir             aLang = [aLang  stringByAppendingString:aTaggedCountry];
269cdf0e10cSrcweir         }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir         int aCount;
272cdf0e10cSrcweir         NSRange range = [macSpell checkSpellingOfString:aNSStr startingAt:0 language:aLang wrap:sal_False inSpellDocumentWithTag:macTag wordCount:&aCount];
273cdf0e10cSrcweir 		int rVal = 0;
274cdf0e10cSrcweir 		if(range.length>0)
275cdf0e10cSrcweir 		{
276cdf0e10cSrcweir 			rVal = -1;
277cdf0e10cSrcweir 		}
278cdf0e10cSrcweir 		else
279cdf0e10cSrcweir 		{
280cdf0e10cSrcweir 			rVal = 1;
281cdf0e10cSrcweir 		}
282cdf0e10cSrcweir         [pool release];
283cdf0e10cSrcweir         if (rVal != 1)
284cdf0e10cSrcweir         {
285cdf0e10cSrcweir             nRes = SpellFailure::SPELLING_ERROR;
286cdf0e10cSrcweir         } else {
287cdf0e10cSrcweir             return -1;
288cdf0e10cSrcweir         }
289cdf0e10cSrcweir     }
290cdf0e10cSrcweir 	return nRes;
291cdf0e10cSrcweir }
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 
294cdf0e10cSrcweir 
295cdf0e10cSrcweir sal_Bool SAL_CALL
296cdf0e10cSrcweir 	MacSpellChecker::isValid( const OUString& rWord, const Locale& rLocale,
297cdf0e10cSrcweir 			const PropertyValues& rProperties )
298cdf0e10cSrcweir 		throw(IllegalArgumentException, RuntimeException)
299cdf0e10cSrcweir {
300cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
301cdf0e10cSrcweir 
302cdf0e10cSrcweir  	if (rLocale == Locale()  ||  !rWord.getLength())
303cdf0e10cSrcweir 		return sal_True;
304cdf0e10cSrcweir 
305cdf0e10cSrcweir 	if (!hasLocale( rLocale ))
306cdf0e10cSrcweir #ifdef LINGU_EXCEPTIONS
307cdf0e10cSrcweir 		throw( IllegalArgumentException() );
308cdf0e10cSrcweir #else
309cdf0e10cSrcweir 		return sal_True;
310cdf0e10cSrcweir #endif
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 	// Get property values to be used.
313cdf0e10cSrcweir 	// These are be the default values set in the SN_LINGU_PROPERTIES
314cdf0e10cSrcweir 	// PropertySet which are overridden by the supplied ones from the
315cdf0e10cSrcweir 	// last argument.
316cdf0e10cSrcweir 	// You'll probably like to use a simplier solution than the provided
317cdf0e10cSrcweir 	// one using the PropertyHelper_Spell.
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 	PropertyHelper_Spell &rHelper = GetPropHelper();
320cdf0e10cSrcweir 	rHelper.SetTmpPropVals( rProperties );
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 	sal_Int16 nFailure = GetSpellFailure( rWord, rLocale );
323cdf0e10cSrcweir 	if (nFailure != -1)
324cdf0e10cSrcweir 	{
325cdf0e10cSrcweir 		sal_Int16 nLang = LocaleToLanguage( rLocale );
326cdf0e10cSrcweir 		// postprocess result for errors that should be ignored
327cdf0e10cSrcweir 		if (   (!rHelper.IsSpellUpperCase()  && IsUpper( rWord, nLang ))
328cdf0e10cSrcweir 			|| (!rHelper.IsSpellWithDigits() && HasDigits( rWord ))
329cdf0e10cSrcweir 			|| (!rHelper.IsSpellCapitalization()
330cdf0e10cSrcweir 				&&  nFailure == SpellFailure::CAPTION_ERROR)
331cdf0e10cSrcweir 		)
332cdf0e10cSrcweir 			nFailure = -1;
333cdf0e10cSrcweir 	}
334cdf0e10cSrcweir 
335cdf0e10cSrcweir 	return (nFailure == -1);
336cdf0e10cSrcweir }
337cdf0e10cSrcweir 
338cdf0e10cSrcweir 
339cdf0e10cSrcweir Reference< XSpellAlternatives >
340cdf0e10cSrcweir 	MacSpellChecker::GetProposals( const OUString &rWord, const Locale &rLocale )
341cdf0e10cSrcweir {
342cdf0e10cSrcweir 	// Retrieves the return values for the 'spell' function call in case
343cdf0e10cSrcweir 	// of a misspelled word.
344cdf0e10cSrcweir 	// Especially it may give a list of suggested (correct) words:
345cdf0e10cSrcweir 
346cdf0e10cSrcweir 	Reference< XSpellAlternatives > xRes;
347cdf0e10cSrcweir         // note: mutex is held by higher up by spell which covers both
348cdf0e10cSrcweir 
349cdf0e10cSrcweir     sal_Int16 nLang = LocaleToLanguage( rLocale );
350cdf0e10cSrcweir 	int count;
351cdf0e10cSrcweir 	Sequence< OUString > aStr( 0 );
352cdf0e10cSrcweir 
353cdf0e10cSrcweir         // first handle smart quotes (single and double)
354cdf0e10cSrcweir 	OUStringBuffer rBuf(rWord);
355cdf0e10cSrcweir         sal_Int32 n = rBuf.getLength();
356cdf0e10cSrcweir         sal_Unicode c;
357cdf0e10cSrcweir 	for (sal_Int32 ix=0; ix < n; ix++) {
358cdf0e10cSrcweir 	     c = rBuf.charAt(ix);
359cdf0e10cSrcweir              if ((c == 0x201C) || (c == 0x201D)) rBuf.setCharAt(ix,(sal_Unicode)0x0022);
360cdf0e10cSrcweir              if ((c == 0x2018) || (c == 0x2019)) rBuf.setCharAt(ix,(sal_Unicode)0x0027);
361cdf0e10cSrcweir         }
362cdf0e10cSrcweir         OUString nWord(rBuf.makeStringAndClear());
363cdf0e10cSrcweir 
364cdf0e10cSrcweir 	if (n)
365cdf0e10cSrcweir 	{
366cdf0e10cSrcweir 		NSAutoreleasePool* pool	= [[NSAutoreleasePool alloc] init];
367cdf0e10cSrcweir         NSString* aNSStr = [[NSString alloc] initWithCharacters: nWord.getStr() length: nWord.getLength()];
368cdf0e10cSrcweir         NSString* aLang = [[NSString alloc] initWithCharacters: rLocale.Language.getStr() length: rLocale.Language.getLength() ];
369cdf0e10cSrcweir         if(rLocale.Country.getLength()>0)
370cdf0e10cSrcweir         {
371cdf0e10cSrcweir             NSString* aCountry = [[NSString alloc] initWithCharacters: rLocale.Country.getStr() length: rLocale.Country.getLength() ];
372cdf0e10cSrcweir             NSString* aTag = @"_";
373cdf0e10cSrcweir             NSString* aTaggedCountry = [aTag stringByAppendingString:aCountry];
374cdf0e10cSrcweir             [aLang autorelease];
375cdf0e10cSrcweir             aLang = [aLang  stringByAppendingString:aTaggedCountry];
376cdf0e10cSrcweir         }
377cdf0e10cSrcweir         [macSpell setLanguage:aLang];
378cdf0e10cSrcweir         NSArray *guesses = [macSpell guessesForWord:aNSStr];
379cdf0e10cSrcweir         count = [guesses count];
380cdf0e10cSrcweir         if (count)
381cdf0e10cSrcweir         {
382cdf0e10cSrcweir            aStr.realloc( count );
383cdf0e10cSrcweir            OUString *pStr = aStr.getArray();
384cdf0e10cSrcweir                for (int ii=0; ii < count; ii++)
385cdf0e10cSrcweir                {
386cdf0e10cSrcweir                   // if needed add: if (suglst[ii] == NULL) continue;
387cdf0e10cSrcweir                   NSString* guess = [guesses objectAtIndex:ii];
388cdf0e10cSrcweir                   OUString cvtwrd((const sal_Unicode*)[guess cStringUsingEncoding:NSUnicodeStringEncoding], (sal_Int32)[guess length]);
389cdf0e10cSrcweir                   pStr[ii] = cvtwrd;
390cdf0e10cSrcweir                }
391cdf0e10cSrcweir         }
392cdf0e10cSrcweir        [pool release];
393cdf0e10cSrcweir 	}
394cdf0e10cSrcweir 
395cdf0e10cSrcweir             // now return an empty alternative for no suggestions or the list of alternatives if some found
396cdf0e10cSrcweir 	    SpellAlternatives *pAlt = new SpellAlternatives;
397cdf0e10cSrcweir             String aTmp(rWord);
398cdf0e10cSrcweir 	    pAlt->SetWordLanguage( aTmp, nLang );
399cdf0e10cSrcweir 	    pAlt->SetFailureType( SpellFailure::SPELLING_ERROR );
400cdf0e10cSrcweir 	    pAlt->SetAlternatives( aStr );
401cdf0e10cSrcweir 	    xRes = pAlt;
402cdf0e10cSrcweir         return xRes;
403cdf0e10cSrcweir 
404cdf0e10cSrcweir }
405cdf0e10cSrcweir 
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 
409cdf0e10cSrcweir Reference< XSpellAlternatives > SAL_CALL
410cdf0e10cSrcweir 	MacSpellChecker::spell( const OUString& rWord, const Locale& rLocale,
411cdf0e10cSrcweir 			const PropertyValues& rProperties )
412cdf0e10cSrcweir 		throw(IllegalArgumentException, RuntimeException)
413cdf0e10cSrcweir {
414cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
415cdf0e10cSrcweir 
416cdf0e10cSrcweir  	if (rLocale == Locale()  ||  !rWord.getLength())
417cdf0e10cSrcweir 		return NULL;
418cdf0e10cSrcweir 
419cdf0e10cSrcweir 	if (!hasLocale( rLocale ))
420cdf0e10cSrcweir #ifdef LINGU_EXCEPTIONS
421cdf0e10cSrcweir 		throw( IllegalArgumentException() );
422cdf0e10cSrcweir #else
423cdf0e10cSrcweir 		return NULL;
424cdf0e10cSrcweir #endif
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 	Reference< XSpellAlternatives > xAlt;
427cdf0e10cSrcweir 	if (!isValid( rWord, rLocale, rProperties ))
428cdf0e10cSrcweir 	{
429cdf0e10cSrcweir 		xAlt =  GetProposals( rWord, rLocale );
430cdf0e10cSrcweir 	}
431cdf0e10cSrcweir 	return xAlt;
432cdf0e10cSrcweir }
433cdf0e10cSrcweir 
434cdf0e10cSrcweir 
435cdf0e10cSrcweir Reference< XInterface > SAL_CALL MacSpellChecker_CreateInstance(
436cdf0e10cSrcweir             const Reference< XMultiServiceFactory > & /*rSMgr*/ )
437cdf0e10cSrcweir 		throw(Exception)
438cdf0e10cSrcweir {
439cdf0e10cSrcweir 
440cdf0e10cSrcweir 	Reference< XInterface > xService = (cppu::OWeakObject*) new MacSpellChecker;
441cdf0e10cSrcweir 	return xService;
442cdf0e10cSrcweir }
443cdf0e10cSrcweir 
444cdf0e10cSrcweir 
445cdf0e10cSrcweir sal_Bool SAL_CALL
446cdf0e10cSrcweir 	MacSpellChecker::addLinguServiceEventListener(
447cdf0e10cSrcweir 			const Reference< XLinguServiceEventListener >& rxLstnr )
448cdf0e10cSrcweir 		throw(RuntimeException)
449cdf0e10cSrcweir {
450cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
451cdf0e10cSrcweir 
452cdf0e10cSrcweir 	sal_Bool bRes = sal_False;
453cdf0e10cSrcweir 	if (!bDisposing && rxLstnr.is())
454cdf0e10cSrcweir 	{
455cdf0e10cSrcweir 		bRes = GetPropHelper().addLinguServiceEventListener( rxLstnr );
456cdf0e10cSrcweir 	}
457cdf0e10cSrcweir 	return bRes;
458cdf0e10cSrcweir }
459cdf0e10cSrcweir 
460cdf0e10cSrcweir 
461cdf0e10cSrcweir sal_Bool SAL_CALL
462cdf0e10cSrcweir 	MacSpellChecker::removeLinguServiceEventListener(
463cdf0e10cSrcweir 			const Reference< XLinguServiceEventListener >& rxLstnr )
464cdf0e10cSrcweir 		throw(RuntimeException)
465cdf0e10cSrcweir {
466cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
467cdf0e10cSrcweir 
468cdf0e10cSrcweir 	sal_Bool bRes = sal_False;
469cdf0e10cSrcweir 	if (!bDisposing && rxLstnr.is())
470cdf0e10cSrcweir 	{
471cdf0e10cSrcweir 		DBG_ASSERT( xPropHelper.is(), "xPropHelper non existent" );
472cdf0e10cSrcweir 		bRes = GetPropHelper().removeLinguServiceEventListener( rxLstnr );
473cdf0e10cSrcweir 	}
474cdf0e10cSrcweir 	return bRes;
475cdf0e10cSrcweir }
476cdf0e10cSrcweir 
477cdf0e10cSrcweir 
478cdf0e10cSrcweir OUString SAL_CALL
479cdf0e10cSrcweir     MacSpellChecker::getServiceDisplayName( const Locale& /*rLocale*/ )
480cdf0e10cSrcweir 		throw(RuntimeException)
481cdf0e10cSrcweir {
482cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
483cdf0e10cSrcweir 	return A2OU( "Mac OS X Spell Checker" );
484cdf0e10cSrcweir }
485cdf0e10cSrcweir 
486cdf0e10cSrcweir 
487cdf0e10cSrcweir void SAL_CALL
488cdf0e10cSrcweir 	MacSpellChecker::initialize( const Sequence< Any >& rArguments )
489cdf0e10cSrcweir 		throw(Exception, RuntimeException)
490cdf0e10cSrcweir {
491cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
492cdf0e10cSrcweir 
493cdf0e10cSrcweir 	if (!pPropHelper)
494cdf0e10cSrcweir 	{
495cdf0e10cSrcweir 		sal_Int32 nLen = rArguments.getLength();
496cdf0e10cSrcweir 		if (2 == nLen)
497cdf0e10cSrcweir 		{
498cdf0e10cSrcweir 			Reference< XPropertySet	>	xPropSet;
499cdf0e10cSrcweir 			rArguments.getConstArray()[0] >>= xPropSet;
500cdf0e10cSrcweir 			//rArguments.getConstArray()[1] >>= xDicList;
501cdf0e10cSrcweir 
502cdf0e10cSrcweir 			//! Pointer allows for access of the non-UNO functions.
503cdf0e10cSrcweir 			//! And the reference to the UNO-functions while increasing
504cdf0e10cSrcweir 			//! the ref-count and will implicitly free the memory
505cdf0e10cSrcweir 			//! when the object is not longer used.
506cdf0e10cSrcweir 			pPropHelper = new PropertyHelper_Spell( (XSpellChecker *) this, xPropSet );
507cdf0e10cSrcweir 			xPropHelper = pPropHelper;
508cdf0e10cSrcweir 			pPropHelper->AddAsPropListener();	//! after a reference is established
509cdf0e10cSrcweir 		}
510cdf0e10cSrcweir 		else
511cdf0e10cSrcweir 			DBG_ERROR( "wrong number of arguments in sequence" );
512cdf0e10cSrcweir 
513cdf0e10cSrcweir 	}
514cdf0e10cSrcweir }
515cdf0e10cSrcweir 
516cdf0e10cSrcweir 
517cdf0e10cSrcweir void SAL_CALL
518cdf0e10cSrcweir 	MacSpellChecker::dispose()
519cdf0e10cSrcweir 		throw(RuntimeException)
520cdf0e10cSrcweir {
521cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
522cdf0e10cSrcweir 
523cdf0e10cSrcweir 	if (!bDisposing)
524cdf0e10cSrcweir 	{
525cdf0e10cSrcweir 		bDisposing = sal_True;
526cdf0e10cSrcweir 		EventObject	aEvtObj( (XSpellChecker *) this );
527cdf0e10cSrcweir 		aEvtListeners.disposeAndClear( aEvtObj );
528cdf0e10cSrcweir 	}
529cdf0e10cSrcweir }
530cdf0e10cSrcweir 
531cdf0e10cSrcweir 
532cdf0e10cSrcweir void SAL_CALL
533cdf0e10cSrcweir 	MacSpellChecker::addEventListener( const Reference< XEventListener >& rxListener )
534cdf0e10cSrcweir 		throw(RuntimeException)
535cdf0e10cSrcweir {
536cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
537cdf0e10cSrcweir 
538cdf0e10cSrcweir 	if (!bDisposing && rxListener.is())
539cdf0e10cSrcweir 		aEvtListeners.addInterface( rxListener );
540cdf0e10cSrcweir }
541cdf0e10cSrcweir 
542cdf0e10cSrcweir 
543cdf0e10cSrcweir void SAL_CALL
544cdf0e10cSrcweir 	MacSpellChecker::removeEventListener( const Reference< XEventListener >& rxListener )
545cdf0e10cSrcweir 		throw(RuntimeException)
546cdf0e10cSrcweir {
547cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 	if (!bDisposing && rxListener.is())
550cdf0e10cSrcweir 		aEvtListeners.removeInterface( rxListener );
551cdf0e10cSrcweir }
552cdf0e10cSrcweir 
553cdf0e10cSrcweir 
554cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
555cdf0e10cSrcweir // Service specific part
556cdf0e10cSrcweir //
557cdf0e10cSrcweir 
558cdf0e10cSrcweir OUString SAL_CALL MacSpellChecker::getImplementationName()
559cdf0e10cSrcweir 		throw(RuntimeException)
560cdf0e10cSrcweir {
561cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
562cdf0e10cSrcweir 
563cdf0e10cSrcweir 	return getImplementationName_Static();
564cdf0e10cSrcweir }
565cdf0e10cSrcweir 
566cdf0e10cSrcweir 
567cdf0e10cSrcweir sal_Bool SAL_CALL MacSpellChecker::supportsService( const OUString& ServiceName )
568cdf0e10cSrcweir 		throw(RuntimeException)
569cdf0e10cSrcweir {
570cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
571cdf0e10cSrcweir 
572cdf0e10cSrcweir 	Sequence< OUString > aSNL = getSupportedServiceNames();
573cdf0e10cSrcweir 	const OUString * pArray = aSNL.getConstArray();
574cdf0e10cSrcweir 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
575cdf0e10cSrcweir 		if( pArray[i] == ServiceName )
576cdf0e10cSrcweir 			return sal_True;
577cdf0e10cSrcweir 	return sal_False;
578cdf0e10cSrcweir }
579cdf0e10cSrcweir 
580cdf0e10cSrcweir 
581cdf0e10cSrcweir Sequence< OUString > SAL_CALL MacSpellChecker::getSupportedServiceNames()
582cdf0e10cSrcweir 		throw(RuntimeException)
583cdf0e10cSrcweir {
584cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
585cdf0e10cSrcweir 
586cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
587cdf0e10cSrcweir }
588cdf0e10cSrcweir 
589cdf0e10cSrcweir 
590cdf0e10cSrcweir Sequence< OUString > MacSpellChecker::getSupportedServiceNames_Static()
591cdf0e10cSrcweir 		throw()
592cdf0e10cSrcweir {
593cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
594cdf0e10cSrcweir 
595cdf0e10cSrcweir 	Sequence< OUString > aSNS( 1 );	// auch mehr als 1 Service moeglich
596cdf0e10cSrcweir 	aSNS.getArray()[0] = A2OU( SN_SPELLCHECKER );
597cdf0e10cSrcweir 	return aSNS;
598cdf0e10cSrcweir }
599cdf0e10cSrcweir 
600cdf0e10cSrcweir void * SAL_CALL MacSpellChecker_getFactory( const sal_Char * pImplName,
601cdf0e10cSrcweir 			XMultiServiceFactory * pServiceManager, void *  )
602cdf0e10cSrcweir {
603cdf0e10cSrcweir 	void * pRet = 0;
604cdf0e10cSrcweir 	if ( !MacSpellChecker::getImplementationName_Static().compareToAscii( pImplName ) )
605cdf0e10cSrcweir 	{
606cdf0e10cSrcweir 		Reference< XSingleServiceFactory > xFactory =
607cdf0e10cSrcweir 			cppu::createOneInstanceFactory(
608cdf0e10cSrcweir 				pServiceManager,
609cdf0e10cSrcweir 				MacSpellChecker::getImplementationName_Static(),
610cdf0e10cSrcweir 				MacSpellChecker_CreateInstance,
611cdf0e10cSrcweir 				MacSpellChecker::getSupportedServiceNames_Static());
612cdf0e10cSrcweir 		// acquire, because we return an interface pointer instead of a reference
613cdf0e10cSrcweir 		xFactory->acquire();
614cdf0e10cSrcweir 		pRet = xFactory.get();
615cdf0e10cSrcweir 	}
616cdf0e10cSrcweir 	return pRet;
617cdf0e10cSrcweir }
618cdf0e10cSrcweir 
619cdf0e10cSrcweir 
620cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
621