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