1*449ab281SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*449ab281SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*449ab281SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*449ab281SAndrew Rist  * distributed with this work for additional information
6*449ab281SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*449ab281SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*449ab281SAndrew Rist  * "License"); you may not use this file except in compliance
9*449ab281SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*449ab281SAndrew Rist  *
11*449ab281SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*449ab281SAndrew Rist  *
13*449ab281SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*449ab281SAndrew Rist  * software distributed under the License is distributed on an
15*449ab281SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*449ab281SAndrew Rist  * KIND, either express or implied.  See the License for the
17*449ab281SAndrew Rist  * specific language governing permissions and limitations
18*449ab281SAndrew Rist  * under the License.
19*449ab281SAndrew Rist  *
20*449ab281SAndrew Rist  *************************************************************/
21*449ab281SAndrew Rist 
22*449ab281SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
26cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
27cdf0e10cSrcweir #include <indexentrysupplier.hxx>
28cdf0e10cSrcweir #include <localedata.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir using namespace ::com::sun::star::uno;
31cdf0e10cSrcweir using namespace ::com::sun::star::lang;
32cdf0e10cSrcweir using namespace ::rtl;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir static const sal_Unicode under = sal_Unicode('_');
35cdf0e10cSrcweir 
36cdf0e10cSrcweir namespace com { namespace sun { namespace star { namespace i18n {
37cdf0e10cSrcweir 
IndexEntrySupplier(const Reference<XMultiServiceFactory> & rxMSF)38cdf0e10cSrcweir IndexEntrySupplier::IndexEntrySupplier( const Reference < XMultiServiceFactory >& rxMSF ) : xMSF( rxMSF )
39cdf0e10cSrcweir {
40cdf0e10cSrcweir }
41cdf0e10cSrcweir 
getLocaleList()42cdf0e10cSrcweir Sequence < Locale > SAL_CALL IndexEntrySupplier::getLocaleList() throw (RuntimeException)
43cdf0e10cSrcweir {
44cdf0e10cSrcweir         return LocaleData().getAllInstalledLocaleNames();
45cdf0e10cSrcweir }
46cdf0e10cSrcweir 
getAlgorithmList(const Locale & rLocale)47cdf0e10cSrcweir Sequence < OUString > SAL_CALL IndexEntrySupplier::getAlgorithmList( const Locale& rLocale ) throw (RuntimeException)
48cdf0e10cSrcweir {
49cdf0e10cSrcweir         return LocaleData().getIndexAlgorithm(rLocale);
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
loadAlgorithm(const Locale & rLocale,const OUString & SortAlgorithm,sal_Int32 collatorOptions)52cdf0e10cSrcweir sal_Bool SAL_CALL IndexEntrySupplier::loadAlgorithm( const Locale& rLocale, const OUString& SortAlgorithm,
53cdf0e10cSrcweir         sal_Int32 collatorOptions ) throw (RuntimeException)
54cdf0e10cSrcweir {
55cdf0e10cSrcweir         Sequence < OUString > algorithmList = getAlgorithmList( rLocale );
56cdf0e10cSrcweir         for (sal_Int32 i = 0; i < algorithmList.getLength(); i++) {
57cdf0e10cSrcweir             if (algorithmList[i] == SortAlgorithm) {
58cdf0e10cSrcweir                 if (getLocaleSpecificIndexEntrySupplier(rLocale, SortAlgorithm).is())
59cdf0e10cSrcweir                     return xIES->loadAlgorithm(rLocale, SortAlgorithm, collatorOptions);
60cdf0e10cSrcweir             }
61cdf0e10cSrcweir         }
62cdf0e10cSrcweir         return sal_False;
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
usePhoneticEntry(const Locale & rLocale)65cdf0e10cSrcweir sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale ) throw (RuntimeException)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir         return LocaleData().hasPhonetic(rLocale);
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
getPhoneticCandidate(const OUString & rIndexEntry,const Locale & rLocale)70cdf0e10cSrcweir OUString SAL_CALL IndexEntrySupplier::getPhoneticCandidate( const OUString& rIndexEntry,
71cdf0e10cSrcweir         const Locale& rLocale ) throw (RuntimeException)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir         if (getLocaleSpecificIndexEntrySupplier(rLocale, OUString()).is())
74cdf0e10cSrcweir             return xIES->getPhoneticCandidate(rIndexEntry, rLocale);
75cdf0e10cSrcweir         else
76cdf0e10cSrcweir             throw RuntimeException();
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
getIndexKey(const OUString & rIndexEntry,const OUString & rPhoneticEntry,const Locale & rLocale)79cdf0e10cSrcweir OUString SAL_CALL IndexEntrySupplier::getIndexKey( const OUString& rIndexEntry,
80cdf0e10cSrcweir         const OUString& rPhoneticEntry, const Locale& rLocale ) throw (RuntimeException)
81cdf0e10cSrcweir {
82cdf0e10cSrcweir         if (xIES.is())
83cdf0e10cSrcweir             return xIES->getIndexKey(rIndexEntry, rPhoneticEntry, rLocale);
84cdf0e10cSrcweir         else
85cdf0e10cSrcweir             throw RuntimeException();
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
compareIndexEntry(const OUString & rIndexEntry1,const OUString & rPhoneticEntry1,const Locale & rLocale1,const OUString & rIndexEntry2,const OUString & rPhoneticEntry2,const Locale & rLocale2)88cdf0e10cSrcweir sal_Int16 SAL_CALL IndexEntrySupplier::compareIndexEntry(
89cdf0e10cSrcweir         const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
90cdf0e10cSrcweir         const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
91cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
92cdf0e10cSrcweir {
93cdf0e10cSrcweir         if (xIES.is())
94cdf0e10cSrcweir             return xIES->compareIndexEntry(rIndexEntry1, rPhoneticEntry1, rLocale1,
95cdf0e10cSrcweir                                         rIndexEntry2, rPhoneticEntry2, rLocale2);
96cdf0e10cSrcweir         else
97cdf0e10cSrcweir             throw RuntimeException();
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
getIndexCharacter(const OUString & rIndexEntry,const Locale & rLocale,const OUString & rSortAlgorithm)100cdf0e10cSrcweir OUString SAL_CALL IndexEntrySupplier::getIndexCharacter( const OUString& rIndexEntry,
101cdf0e10cSrcweir         const Locale& rLocale, const OUString& rSortAlgorithm )
102cdf0e10cSrcweir         throw (RuntimeException)
103cdf0e10cSrcweir {
104cdf0e10cSrcweir         return getLocaleSpecificIndexEntrySupplier(rLocale, rSortAlgorithm)->
105cdf0e10cSrcweir                         getIndexCharacter( rIndexEntry, rLocale, rSortAlgorithm );
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
createLocaleSpecificIndexEntrySupplier(const OUString & name)108cdf0e10cSrcweir sal_Bool SAL_CALL IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString& name) throw( RuntimeException )
109cdf0e10cSrcweir {
110cdf0e10cSrcweir         Reference < XInterface > xI = xMSF->createInstance(
111cdf0e10cSrcweir             OUString::createFromAscii("com.sun.star.i18n.IndexEntrySupplier_") + name);
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         if ( xI.is() ) {
114cdf0e10cSrcweir             xI->queryInterface( ::getCppuType((const Reference< com::sun::star::i18n::XExtendedIndexEntrySupplier>*)0) ) >>= xIES;
115cdf0e10cSrcweir             return xIES.is();
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir         return sal_False;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir Reference < com::sun::star::i18n::XExtendedIndexEntrySupplier > SAL_CALL
getLocaleSpecificIndexEntrySupplier(const Locale & rLocale,const OUString & rSortAlgorithm)121cdf0e10cSrcweir IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale& rLocale, const OUString& rSortAlgorithm) throw (RuntimeException)
122cdf0e10cSrcweir {
123cdf0e10cSrcweir         if (xIES.is() && rSortAlgorithm == aSortAlgorithm && rLocale.Language == aLocale.Language &&
124cdf0e10cSrcweir                 rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
125cdf0e10cSrcweir             return xIES;
126cdf0e10cSrcweir         else if (xMSF.is()) {
127cdf0e10cSrcweir             LocaleData ld;
128cdf0e10cSrcweir             aLocale = rLocale;
129cdf0e10cSrcweir             if (rSortAlgorithm.getLength() == 0)
130cdf0e10cSrcweir                 aSortAlgorithm = ld.getDefaultIndexAlgorithm( rLocale );
131cdf0e10cSrcweir             else
132cdf0e10cSrcweir                 aSortAlgorithm = rSortAlgorithm;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir             OUString module = ld.getIndexModuleByAlgorithm(rLocale, aSortAlgorithm);
135cdf0e10cSrcweir             if (module.getLength() > 0 && createLocaleSpecificIndexEntrySupplier(module))
136cdf0e10cSrcweir                 return xIES;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             sal_Int32 l = rLocale.Language.getLength();
139cdf0e10cSrcweir             sal_Int32 c = rLocale.Country.getLength();
140cdf0e10cSrcweir             sal_Int32 v = rLocale.Variant.getLength();
141cdf0e10cSrcweir             sal_Int32 a = aSortAlgorithm.getLength();
142cdf0e10cSrcweir             OUStringBuffer aBuf(l+c+v+a+4);
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             if ((l > 0 && c > 0 && v > 0 && a > 0 &&
145cdf0e10cSrcweir                         // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
146cdf0e10cSrcweir                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
147cdf0e10cSrcweir                                     rLocale.Country).append(under).append(rLocale.Variant).append(under).append(
148cdf0e10cSrcweir                                     aSortAlgorithm).makeStringAndClear())) ||
149cdf0e10cSrcweir                 (l > 0 && c > 0 && a > 0 &&
150cdf0e10cSrcweir                         // load service with name <base>_<lang>_<country>_<algorithm>
151cdf0e10cSrcweir                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
152cdf0e10cSrcweir                                     rLocale.Country).append(under).append(aSortAlgorithm).makeStringAndClear())) ||
153cdf0e10cSrcweir                 (l > 0 && c > 0 && a > 0 && rLocale.Language.compareToAscii("zh") == 0 &&
154cdf0e10cSrcweir                                             (rLocale.Country.compareToAscii("HK") == 0 ||
155cdf0e10cSrcweir                                             rLocale.Country.compareToAscii("MO") == 0) &&
156cdf0e10cSrcweir                         // if the country code is HK or MO, one more step to try TW.
157cdf0e10cSrcweir                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).appendAscii(
158cdf0e10cSrcweir                                     "TW").append(under).append(aSortAlgorithm).makeStringAndClear())) ||
159cdf0e10cSrcweir                 (l > 0 && a > 0 &&
160cdf0e10cSrcweir                         // load service with name <base>_<lang>_<algorithm>
161cdf0e10cSrcweir                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
162cdf0e10cSrcweir                                     aSortAlgorithm).makeStringAndClear())) ||
163cdf0e10cSrcweir                         // load service with name <base>_<algorithm>
164cdf0e10cSrcweir                 (a > 0 && createLocaleSpecificIndexEntrySupplier(aSortAlgorithm)) ||
165cdf0e10cSrcweir                         // load default service with name <base>_Unicode
166cdf0e10cSrcweir                         createLocaleSpecificIndexEntrySupplier(OUString::createFromAscii("Unicode"))) {
167cdf0e10cSrcweir                 return xIES;
168cdf0e10cSrcweir             }
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir         throw RuntimeException();
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
getIndexFollowPageWord(sal_Bool bMorePages,const Locale & rLocale)173cdf0e10cSrcweir OUString SAL_CALL IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages,
174cdf0e10cSrcweir         const Locale& rLocale ) throw (RuntimeException)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir         Sequence< OUString > aFollowPageWords = LocaleData().getFollowPageWords(rLocale);
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         return (bMorePages && aFollowPageWords.getLength() > 1) ?
179cdf0e10cSrcweir             aFollowPageWords[1] : (aFollowPageWords.getLength() > 0 ?
180cdf0e10cSrcweir             aFollowPageWords[0] : OUString());
181cdf0e10cSrcweir }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
184cdf0e10cSrcweir 
185cdf0e10cSrcweir OUString SAL_CALL
getImplementationName()186cdf0e10cSrcweir IndexEntrySupplier::getImplementationName() throw( RuntimeException )
187cdf0e10cSrcweir {
188cdf0e10cSrcweir         return OUString::createFromAscii( implementationName );
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir sal_Bool SAL_CALL
supportsService(const OUString & rServiceName)192cdf0e10cSrcweir IndexEntrySupplier::supportsService(const OUString& rServiceName) throw( RuntimeException )
193cdf0e10cSrcweir {
194cdf0e10cSrcweir         return rServiceName.compareToAscii(implementationName) == 0;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir Sequence< OUString > SAL_CALL
getSupportedServiceNames()198cdf0e10cSrcweir IndexEntrySupplier::getSupportedServiceNames() throw( RuntimeException )
199cdf0e10cSrcweir {
200cdf0e10cSrcweir         Sequence< OUString > aRet(1);
201cdf0e10cSrcweir         aRet[0] = OUString::createFromAscii( implementationName );
202cdf0e10cSrcweir         return aRet;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir } } } }
206