xref: /aoo41x/main/linguistic/source/thesdsp.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_linguistic.hxx"
30*cdf0e10cSrcweir #include <i18npool/lang.h>
31*cdf0e10cSrcweir #include <tools/debug.hxx>
32*cdf0e10cSrcweir #include <svl/lngmisc.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>	// helper for factories
35*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
37*cdf0e10cSrcweir #include <unotools/processfactory.hxx>
38*cdf0e10cSrcweir #include <osl/mutex.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include "thesdsp.hxx"
41*cdf0e10cSrcweir #include "linguistic/lngprops.hxx"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir using namespace utl;
44*cdf0e10cSrcweir using namespace osl;
45*cdf0e10cSrcweir using namespace rtl;
46*cdf0e10cSrcweir using namespace com::sun::star;
47*cdf0e10cSrcweir using namespace com::sun::star::beans;
48*cdf0e10cSrcweir using namespace com::sun::star::lang;
49*cdf0e10cSrcweir using namespace com::sun::star::uno;
50*cdf0e10cSrcweir using namespace com::sun::star::linguistic2;
51*cdf0e10cSrcweir using namespace linguistic;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir static sal_Bool SvcListHasLanguage(
56*cdf0e10cSrcweir         const Sequence< Reference< XThesaurus > > &rRefs,
57*cdf0e10cSrcweir         const Locale &rLocale )
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir     sal_Bool bHasLanguage = sal_False;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     const Reference< XThesaurus > *pRef = rRefs.getConstArray();
62*cdf0e10cSrcweir     sal_Int32 nLen = rRefs.getLength();
63*cdf0e10cSrcweir     for (sal_Int32 k = 0;  k < nLen  &&  !bHasLanguage;  ++k)
64*cdf0e10cSrcweir     {
65*cdf0e10cSrcweir         if (pRef[k].is())
66*cdf0e10cSrcweir             bHasLanguage = pRef[k]->hasLocale( rLocale );
67*cdf0e10cSrcweir     }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     return bHasLanguage;
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir ThesaurusDispatcher::ThesaurusDispatcher()
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir ThesaurusDispatcher::~ThesaurusDispatcher()
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir 	ClearSvcList();
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir void ThesaurusDispatcher::ClearSvcList()
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir 	// release memory for each table entry
89*cdf0e10cSrcweir     ThesSvcByLangMap_t aTmp;
90*cdf0e10cSrcweir     aSvcMap.swap( aTmp );
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir Sequence< Locale > SAL_CALL
95*cdf0e10cSrcweir 	ThesaurusDispatcher::getLocales()
96*cdf0e10cSrcweir 		throw(RuntimeException)
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     Sequence< Locale > aLocales( static_cast< sal_Int32 >(aSvcMap.size()) );
101*cdf0e10cSrcweir     Locale *pLocales = aLocales.getArray();
102*cdf0e10cSrcweir     ThesSvcByLangMap_t::const_iterator aIt;
103*cdf0e10cSrcweir     for (aIt = aSvcMap.begin();  aIt != aSvcMap.end();  ++aIt)
104*cdf0e10cSrcweir     {
105*cdf0e10cSrcweir         *pLocales++ = CreateLocale( aIt->first );
106*cdf0e10cSrcweir     }
107*cdf0e10cSrcweir     return aLocales;
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir sal_Bool SAL_CALL
112*cdf0e10cSrcweir     ThesaurusDispatcher::hasLocale( const Locale& rLocale )
113*cdf0e10cSrcweir 		throw(RuntimeException)
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
116*cdf0e10cSrcweir     ThesSvcByLangMap_t::const_iterator aIt( aSvcMap.find( LocaleToLanguage( rLocale ) ) );
117*cdf0e10cSrcweir     return aIt != aSvcMap.end();
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir Sequence< Reference< XMeaning > > SAL_CALL
122*cdf0e10cSrcweir 	ThesaurusDispatcher::queryMeanings(
123*cdf0e10cSrcweir 			const OUString& rTerm, const Locale& rLocale,
124*cdf0e10cSrcweir 			const PropertyValues& rProperties )
125*cdf0e10cSrcweir 		throw(IllegalArgumentException, RuntimeException)
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 	Sequence< Reference< XMeaning > >	aMeanings;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	sal_Int16 nLanguage = LocaleToLanguage( rLocale );
132*cdf0e10cSrcweir 	if (nLanguage == LANGUAGE_NONE  || !rTerm.getLength())
133*cdf0e10cSrcweir 		return aMeanings;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 	// search for entry with that language
136*cdf0e10cSrcweir     ThesSvcByLangMap_t::iterator    aIt( aSvcMap.find( nLanguage ) );
137*cdf0e10cSrcweir     LangSvcEntries_Thes     *pEntry = aIt != aSvcMap.end() ? aIt->second.get() : NULL;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 	if (!pEntry)
140*cdf0e10cSrcweir 	{
141*cdf0e10cSrcweir #ifdef LINGU_EXCEPTIONS
142*cdf0e10cSrcweir 		throw IllegalArgumentException();
143*cdf0e10cSrcweir #endif
144*cdf0e10cSrcweir 	}
145*cdf0e10cSrcweir 	else
146*cdf0e10cSrcweir 	{
147*cdf0e10cSrcweir 		OUString aChkWord( rTerm );
148*cdf0e10cSrcweir         aChkWord = aChkWord.replace( SVT_HARD_SPACE, ' ' );
149*cdf0e10cSrcweir 		RemoveHyphens( aChkWord );
150*cdf0e10cSrcweir 		if (IsIgnoreControlChars( rProperties, GetPropSet() ))
151*cdf0e10cSrcweir 			RemoveControlChars( aChkWord );
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 		sal_Int32 nLen = pEntry->aSvcRefs.getLength();
154*cdf0e10cSrcweir 		DBG_ASSERT( nLen == pEntry->aSvcImplNames.getLength(),
155*cdf0e10cSrcweir 				"lng : sequence length mismatch");
156*cdf0e10cSrcweir         DBG_ASSERT( pEntry->nLastTriedSvcIndex < nLen,
157*cdf0e10cSrcweir 				"lng : index out of range");
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 		sal_Int32 i = 0;
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 		// try already instantiated services first
162*cdf0e10cSrcweir 		{
163*cdf0e10cSrcweir 			const Reference< XThesaurus > *pRef = pEntry->aSvcRefs.getConstArray();
164*cdf0e10cSrcweir             while (i <= pEntry->nLastTriedSvcIndex
165*cdf0e10cSrcweir 				   &&  aMeanings.getLength() == 0)
166*cdf0e10cSrcweir 			{
167*cdf0e10cSrcweir                 if (pRef[i].is()  &&  pRef[i]->hasLocale( rLocale ))
168*cdf0e10cSrcweir 					aMeanings = pRef[i]->queryMeanings( aChkWord, rLocale, rProperties );
169*cdf0e10cSrcweir 				++i;
170*cdf0e10cSrcweir 			}
171*cdf0e10cSrcweir 		}
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 		// if still no result instantiate new services and try those
174*cdf0e10cSrcweir 		if (aMeanings.getLength() == 0
175*cdf0e10cSrcweir             &&  pEntry->nLastTriedSvcIndex < nLen - 1)
176*cdf0e10cSrcweir 		{
177*cdf0e10cSrcweir 			const OUString *pImplNames = pEntry->aSvcImplNames.getConstArray();
178*cdf0e10cSrcweir 			Reference< XThesaurus > *pRef = pEntry->aSvcRefs.getArray();
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 			Reference< XMultiServiceFactory >  xMgr( getProcessServiceFactory() );
181*cdf0e10cSrcweir 			if (xMgr.is())
182*cdf0e10cSrcweir 			{
183*cdf0e10cSrcweir 				// build service initialization argument
184*cdf0e10cSrcweir 				Sequence< Any > aArgs(1);
185*cdf0e10cSrcweir 				aArgs.getArray()[0] <<= GetPropSet();
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 				while (i < nLen  &&  aMeanings.getLength() == 0)
188*cdf0e10cSrcweir 				{
189*cdf0e10cSrcweir 					// create specific service via it's implementation name
190*cdf0e10cSrcweir 					Reference< XThesaurus > xThes;
191*cdf0e10cSrcweir 					try
192*cdf0e10cSrcweir 					{
193*cdf0e10cSrcweir 						xThes = Reference< XThesaurus >(
194*cdf0e10cSrcweir 								xMgr->createInstanceWithArguments(
195*cdf0e10cSrcweir 								pImplNames[i], aArgs ), UNO_QUERY );
196*cdf0e10cSrcweir 					}
197*cdf0e10cSrcweir 					catch (uno::Exception &)
198*cdf0e10cSrcweir 					{
199*cdf0e10cSrcweir                         DBG_ASSERT( 0, "createInstanceWithArguments failed" );
200*cdf0e10cSrcweir 					}
201*cdf0e10cSrcweir 					pRef[i] = xThes;
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir                     if (xThes.is()  &&  xThes->hasLocale( rLocale ))
204*cdf0e10cSrcweir 						aMeanings = xThes->queryMeanings( aChkWord, rLocale, rProperties );
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir                     pEntry->nLastTriedSvcIndex = (sal_Int16) i;
207*cdf0e10cSrcweir 					++i;
208*cdf0e10cSrcweir 				}
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir                 // if language is not supported by any of the services
211*cdf0e10cSrcweir                 // remove it from the list.
212*cdf0e10cSrcweir                 if (i == nLen  &&  aMeanings.getLength() == 0)
213*cdf0e10cSrcweir                 {
214*cdf0e10cSrcweir                     if (!SvcListHasLanguage( pEntry->aSvcRefs, rLocale ))
215*cdf0e10cSrcweir                         aSvcMap.erase( nLanguage );
216*cdf0e10cSrcweir                 }
217*cdf0e10cSrcweir 			}
218*cdf0e10cSrcweir 		}
219*cdf0e10cSrcweir 	}
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 	return aMeanings;
222*cdf0e10cSrcweir }
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir void ThesaurusDispatcher::SetServiceList( const Locale &rLocale,
226*cdf0e10cSrcweir 		const Sequence< OUString > &rSvcImplNames )
227*cdf0e10cSrcweir {
228*cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 	sal_Int16 nLanguage = LocaleToLanguage( rLocale );
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir     sal_Int32 nLen = rSvcImplNames.getLength();
233*cdf0e10cSrcweir     if (0 == nLen)
234*cdf0e10cSrcweir         // remove entry
235*cdf0e10cSrcweir         aSvcMap.erase( nLanguage );
236*cdf0e10cSrcweir     else
237*cdf0e10cSrcweir     {
238*cdf0e10cSrcweir         // modify/add entry
239*cdf0e10cSrcweir         LangSvcEntries_Thes *pEntry = aSvcMap[ nLanguage ].get();
240*cdf0e10cSrcweir         if (pEntry)
241*cdf0e10cSrcweir         {
242*cdf0e10cSrcweir             pEntry->Clear();
243*cdf0e10cSrcweir             pEntry->aSvcImplNames = rSvcImplNames;
244*cdf0e10cSrcweir             pEntry->aSvcRefs = Sequence< Reference < XThesaurus > >( nLen );
245*cdf0e10cSrcweir         }
246*cdf0e10cSrcweir         else
247*cdf0e10cSrcweir         {
248*cdf0e10cSrcweir             boost::shared_ptr< LangSvcEntries_Thes > pTmpEntry( new LangSvcEntries_Thes( rSvcImplNames ) );
249*cdf0e10cSrcweir             pTmpEntry->aSvcRefs = Sequence< Reference < XThesaurus > >( nLen );
250*cdf0e10cSrcweir             aSvcMap[ nLanguage ] = pTmpEntry;
251*cdf0e10cSrcweir         }
252*cdf0e10cSrcweir     }
253*cdf0e10cSrcweir }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir Sequence< OUString >
257*cdf0e10cSrcweir 	ThesaurusDispatcher::GetServiceList( const Locale &rLocale ) const
258*cdf0e10cSrcweir {
259*cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 	Sequence< OUString > aRes;
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir 	// search for entry with that language and use data from that
264*cdf0e10cSrcweir 	sal_Int16 nLanguage = LocaleToLanguage( rLocale );
265*cdf0e10cSrcweir     ThesaurusDispatcher             *pThis = (ThesaurusDispatcher *) this;
266*cdf0e10cSrcweir     const ThesSvcByLangMap_t::iterator  aIt( pThis->aSvcMap.find( nLanguage ) );
267*cdf0e10cSrcweir     const LangSvcEntries_Thes       *pEntry = aIt != aSvcMap.end() ? aIt->second.get() : NULL;
268*cdf0e10cSrcweir 	if (pEntry)
269*cdf0e10cSrcweir 		aRes = pEntry->aSvcImplNames;
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir 	return aRes;
272*cdf0e10cSrcweir }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir LinguDispatcher::DspType ThesaurusDispatcher::GetDspType() const
276*cdf0e10cSrcweir {
277*cdf0e10cSrcweir 	return DSP_THES;
278*cdf0e10cSrcweir }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
282*cdf0e10cSrcweir 
283