Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <i18npool/lang.h>
21 : #include <tools/debug.hxx>
22 : #include <svl/lngmisc.hxx>
23 :
24 : #include <cppuhelper/factory.hxx> // helper for factories
25 : #include <com/sun/star/registry/XRegistryKey.hpp>
26 : #include <com/sun/star/beans/XPropertySet.hpp>
27 : #include <comphelper/processfactory.hxx>
28 : #include <osl/mutex.hxx>
29 :
30 : #include "thesdsp.hxx"
31 : #include "linguistic/lngprops.hxx"
32 :
33 : using namespace osl;
34 : using namespace com::sun::star;
35 : using namespace com::sun::star::beans;
36 : using namespace com::sun::star::lang;
37 : using namespace com::sun::star::uno;
38 : using namespace com::sun::star::linguistic2;
39 : using namespace linguistic;
40 :
41 : using ::rtl::OUString;
42 :
43 :
44 0 : static sal_Bool SvcListHasLanguage(
45 : const Sequence< Reference< XThesaurus > > &rRefs,
46 : const Locale &rLocale )
47 : {
48 0 : sal_Bool bHasLanguage = sal_False;
49 :
50 0 : const Reference< XThesaurus > *pRef = rRefs.getConstArray();
51 0 : sal_Int32 nLen = rRefs.getLength();
52 0 : for (sal_Int32 k = 0; k < nLen && !bHasLanguage; ++k)
53 : {
54 0 : if (pRef[k].is())
55 0 : bHasLanguage = pRef[k]->hasLocale( rLocale );
56 : }
57 :
58 0 : return bHasLanguage;
59 : }
60 :
61 :
62 :
63 0 : ThesaurusDispatcher::ThesaurusDispatcher()
64 : {
65 0 : }
66 :
67 :
68 0 : ThesaurusDispatcher::~ThesaurusDispatcher()
69 : {
70 0 : ClearSvcList();
71 0 : }
72 :
73 :
74 0 : void ThesaurusDispatcher::ClearSvcList()
75 : {
76 : // release memory for each table entry
77 0 : ThesSvcByLangMap_t aTmp;
78 0 : aSvcMap.swap( aTmp );
79 0 : }
80 :
81 :
82 : Sequence< Locale > SAL_CALL
83 0 : ThesaurusDispatcher::getLocales()
84 : throw(RuntimeException)
85 : {
86 0 : MutexGuard aGuard( GetLinguMutex() );
87 :
88 0 : Sequence< Locale > aLocales( static_cast< sal_Int32 >(aSvcMap.size()) );
89 0 : Locale *pLocales = aLocales.getArray();
90 0 : ThesSvcByLangMap_t::const_iterator aIt;
91 0 : for (aIt = aSvcMap.begin(); aIt != aSvcMap.end(); ++aIt)
92 : {
93 0 : *pLocales++ = LanguageTag( aIt->first ).getLocale();
94 : }
95 0 : return aLocales;
96 : }
97 :
98 :
99 : sal_Bool SAL_CALL
100 0 : ThesaurusDispatcher::hasLocale( const Locale& rLocale )
101 : throw(RuntimeException)
102 : {
103 0 : MutexGuard aGuard( GetLinguMutex() );
104 0 : ThesSvcByLangMap_t::const_iterator aIt( aSvcMap.find( LanguageTag( rLocale ).getLanguageType() ) );
105 0 : return aIt != aSvcMap.end();
106 : }
107 :
108 :
109 : Sequence< Reference< XMeaning > > SAL_CALL
110 0 : ThesaurusDispatcher::queryMeanings(
111 : const OUString& rTerm, const Locale& rLocale,
112 : const PropertyValues& rProperties )
113 : throw(IllegalArgumentException, RuntimeException)
114 : {
115 0 : MutexGuard aGuard( GetLinguMutex() );
116 :
117 0 : Sequence< Reference< XMeaning > > aMeanings;
118 :
119 0 : sal_Int16 nLanguage = LanguageTag( rLocale ).getLanguageType();
120 0 : if (nLanguage == LANGUAGE_NONE || rTerm.isEmpty())
121 : return aMeanings;
122 :
123 : // search for entry with that language
124 0 : ThesSvcByLangMap_t::iterator aIt( aSvcMap.find( nLanguage ) );
125 0 : LangSvcEntries_Thes *pEntry = aIt != aSvcMap.end() ? aIt->second.get() : NULL;
126 :
127 0 : if (pEntry)
128 : {
129 0 : OUString aChkWord( rTerm );
130 0 : aChkWord = aChkWord.replace( SVT_HARD_SPACE, ' ' );
131 0 : RemoveHyphens( aChkWord );
132 0 : if (IsIgnoreControlChars( rProperties, GetPropSet() ))
133 0 : RemoveControlChars( aChkWord );
134 :
135 0 : sal_Int32 nLen = pEntry->aSvcRefs.getLength();
136 : DBG_ASSERT( nLen == pEntry->aSvcImplNames.getLength(),
137 : "lng : sequence length mismatch");
138 : DBG_ASSERT( pEntry->nLastTriedSvcIndex < nLen,
139 : "lng : index out of range");
140 :
141 0 : sal_Int32 i = 0;
142 :
143 : // try already instantiated services first
144 : {
145 0 : const Reference< XThesaurus > *pRef = pEntry->aSvcRefs.getConstArray();
146 0 : while (i <= pEntry->nLastTriedSvcIndex
147 0 : && aMeanings.getLength() == 0)
148 : {
149 0 : if (pRef[i].is() && pRef[i]->hasLocale( rLocale ))
150 0 : aMeanings = pRef[i]->queryMeanings( aChkWord, rLocale, rProperties );
151 0 : ++i;
152 : }
153 : }
154 :
155 : // if still no result instantiate new services and try those
156 0 : if (aMeanings.getLength() == 0
157 : && pEntry->nLastTriedSvcIndex < nLen - 1)
158 : {
159 0 : const OUString *pImplNames = pEntry->aSvcImplNames.getConstArray();
160 0 : Reference< XThesaurus > *pRef = pEntry->aSvcRefs.getArray();
161 :
162 : Reference< XMultiServiceFactory > xMgr(
163 0 : comphelper::getProcessServiceFactory() );
164 0 : if (xMgr.is())
165 : {
166 : // build service initialization argument
167 0 : Sequence< Any > aArgs(1);
168 0 : aArgs.getArray()[0] <<= GetPropSet();
169 :
170 0 : while (i < nLen && aMeanings.getLength() == 0)
171 : {
172 : // create specific service via it's implementation name
173 0 : Reference< XThesaurus > xThes;
174 : try
175 : {
176 : xThes = Reference< XThesaurus >(
177 0 : xMgr->createInstanceWithArguments(
178 0 : pImplNames[i], aArgs ), UNO_QUERY );
179 : }
180 0 : catch (uno::Exception &)
181 : {
182 : DBG_ASSERT( 0, "createInstanceWithArguments failed" );
183 : }
184 0 : pRef[i] = xThes;
185 :
186 0 : if (xThes.is() && xThes->hasLocale( rLocale ))
187 0 : aMeanings = xThes->queryMeanings( aChkWord, rLocale, rProperties );
188 :
189 0 : pEntry->nLastTriedSvcIndex = (sal_Int16) i;
190 0 : ++i;
191 0 : }
192 :
193 : // if language is not supported by any of the services
194 : // remove it from the list.
195 0 : if (i == nLen && aMeanings.getLength() == 0)
196 : {
197 0 : if (!SvcListHasLanguage( pEntry->aSvcRefs, rLocale ))
198 0 : aSvcMap.erase( nLanguage );
199 0 : }
200 0 : }
201 0 : }
202 : }
203 :
204 0 : return aMeanings;
205 : }
206 :
207 :
208 0 : void ThesaurusDispatcher::SetServiceList( const Locale &rLocale,
209 : const Sequence< OUString > &rSvcImplNames )
210 : {
211 0 : MutexGuard aGuard( GetLinguMutex() );
212 :
213 0 : sal_Int16 nLanguage = LanguageTag( rLocale ).getLanguageType();
214 :
215 0 : sal_Int32 nLen = rSvcImplNames.getLength();
216 0 : if (0 == nLen)
217 : // remove entry
218 0 : aSvcMap.erase( nLanguage );
219 : else
220 : {
221 : // modify/add entry
222 0 : LangSvcEntries_Thes *pEntry = aSvcMap[ nLanguage ].get();
223 0 : if (pEntry)
224 : {
225 0 : pEntry->Clear();
226 0 : pEntry->aSvcImplNames = rSvcImplNames;
227 0 : pEntry->aSvcRefs = Sequence< Reference < XThesaurus > >( nLen );
228 : }
229 : else
230 : {
231 0 : boost::shared_ptr< LangSvcEntries_Thes > pTmpEntry( new LangSvcEntries_Thes( rSvcImplNames ) );
232 0 : pTmpEntry->aSvcRefs = Sequence< Reference < XThesaurus > >( nLen );
233 0 : aSvcMap[ nLanguage ] = pTmpEntry;
234 : }
235 0 : }
236 0 : }
237 :
238 :
239 : Sequence< OUString >
240 0 : ThesaurusDispatcher::GetServiceList( const Locale &rLocale ) const
241 : {
242 0 : MutexGuard aGuard( GetLinguMutex() );
243 :
244 0 : Sequence< OUString > aRes;
245 :
246 : // search for entry with that language and use data from that
247 0 : sal_Int16 nLanguage = LanguageTag( rLocale ).getLanguageType();
248 0 : ThesaurusDispatcher *pThis = (ThesaurusDispatcher *) this;
249 0 : const ThesSvcByLangMap_t::iterator aIt( pThis->aSvcMap.find( nLanguage ) );
250 0 : const LangSvcEntries_Thes *pEntry = aIt != aSvcMap.end() ? aIt->second.get() : NULL;
251 0 : if (pEntry)
252 0 : aRes = pEntry->aSvcImplNames;
253 :
254 0 : return aRes;
255 : }
256 :
257 :
258 0 : LinguDispatcher::DspType ThesaurusDispatcher::GetDspType() const
259 : {
260 0 : return DSP_THES;
261 : }
262 :
263 :
264 :
265 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|