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_i18npool.hxx"
30*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
31*cdf0e10cSrcweir #include <indexentrysupplier.hxx>
32*cdf0e10cSrcweir #include <localedata.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
35*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
36*cdf0e10cSrcweir using namespace ::rtl;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir static const sal_Unicode under = sal_Unicode('_');
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir namespace com { namespace sun { namespace star { namespace i18n {
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir IndexEntrySupplier::IndexEntrySupplier( const Reference < XMultiServiceFactory >& rxMSF ) : xMSF( rxMSF )
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir }
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir Sequence < Locale > SAL_CALL IndexEntrySupplier::getLocaleList() throw (RuntimeException)
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir         return LocaleData().getAllInstalledLocaleNames();
49*cdf0e10cSrcweir }
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir Sequence < OUString > SAL_CALL IndexEntrySupplier::getAlgorithmList( const Locale& rLocale ) throw (RuntimeException)
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir         return LocaleData().getIndexAlgorithm(rLocale);
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir sal_Bool SAL_CALL IndexEntrySupplier::loadAlgorithm( const Locale& rLocale, const OUString& SortAlgorithm,
57*cdf0e10cSrcweir         sal_Int32 collatorOptions ) throw (RuntimeException)
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir         Sequence < OUString > algorithmList = getAlgorithmList( rLocale );
60*cdf0e10cSrcweir         for (sal_Int32 i = 0; i < algorithmList.getLength(); i++) {
61*cdf0e10cSrcweir             if (algorithmList[i] == SortAlgorithm) {
62*cdf0e10cSrcweir                 if (getLocaleSpecificIndexEntrySupplier(rLocale, SortAlgorithm).is())
63*cdf0e10cSrcweir                     return xIES->loadAlgorithm(rLocale, SortAlgorithm, collatorOptions);
64*cdf0e10cSrcweir             }
65*cdf0e10cSrcweir         }
66*cdf0e10cSrcweir         return sal_False;
67*cdf0e10cSrcweir }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale ) throw (RuntimeException)
70*cdf0e10cSrcweir {
71*cdf0e10cSrcweir         return LocaleData().hasPhonetic(rLocale);
72*cdf0e10cSrcweir }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir OUString SAL_CALL IndexEntrySupplier::getPhoneticCandidate( const OUString& rIndexEntry,
75*cdf0e10cSrcweir         const Locale& rLocale ) throw (RuntimeException)
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir         if (getLocaleSpecificIndexEntrySupplier(rLocale, OUString()).is())
78*cdf0e10cSrcweir             return xIES->getPhoneticCandidate(rIndexEntry, rLocale);
79*cdf0e10cSrcweir         else
80*cdf0e10cSrcweir             throw RuntimeException();
81*cdf0e10cSrcweir }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir OUString SAL_CALL IndexEntrySupplier::getIndexKey( const OUString& rIndexEntry,
84*cdf0e10cSrcweir         const OUString& rPhoneticEntry, const Locale& rLocale ) throw (RuntimeException)
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir         if (xIES.is())
87*cdf0e10cSrcweir             return xIES->getIndexKey(rIndexEntry, rPhoneticEntry, rLocale);
88*cdf0e10cSrcweir         else
89*cdf0e10cSrcweir             throw RuntimeException();
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir sal_Int16 SAL_CALL IndexEntrySupplier::compareIndexEntry(
93*cdf0e10cSrcweir         const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
94*cdf0e10cSrcweir         const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
95*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir         if (xIES.is())
98*cdf0e10cSrcweir             return xIES->compareIndexEntry(rIndexEntry1, rPhoneticEntry1, rLocale1,
99*cdf0e10cSrcweir                                         rIndexEntry2, rPhoneticEntry2, rLocale2);
100*cdf0e10cSrcweir         else
101*cdf0e10cSrcweir             throw RuntimeException();
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir OUString SAL_CALL IndexEntrySupplier::getIndexCharacter( const OUString& rIndexEntry,
105*cdf0e10cSrcweir         const Locale& rLocale, const OUString& rSortAlgorithm )
106*cdf0e10cSrcweir         throw (RuntimeException)
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir         return getLocaleSpecificIndexEntrySupplier(rLocale, rSortAlgorithm)->
109*cdf0e10cSrcweir                         getIndexCharacter( rIndexEntry, rLocale, rSortAlgorithm );
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir sal_Bool SAL_CALL IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString& name) throw( RuntimeException )
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir         Reference < XInterface > xI = xMSF->createInstance(
115*cdf0e10cSrcweir             OUString::createFromAscii("com.sun.star.i18n.IndexEntrySupplier_") + name);
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir         if ( xI.is() ) {
118*cdf0e10cSrcweir             xI->queryInterface( ::getCppuType((const Reference< com::sun::star::i18n::XExtendedIndexEntrySupplier>*)0) ) >>= xIES;
119*cdf0e10cSrcweir             return xIES.is();
120*cdf0e10cSrcweir         }
121*cdf0e10cSrcweir         return sal_False;
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir Reference < com::sun::star::i18n::XExtendedIndexEntrySupplier > SAL_CALL
125*cdf0e10cSrcweir IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale& rLocale, const OUString& rSortAlgorithm) throw (RuntimeException)
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir         if (xIES.is() && rSortAlgorithm == aSortAlgorithm && rLocale.Language == aLocale.Language &&
128*cdf0e10cSrcweir                 rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
129*cdf0e10cSrcweir             return xIES;
130*cdf0e10cSrcweir         else if (xMSF.is()) {
131*cdf0e10cSrcweir             LocaleData ld;
132*cdf0e10cSrcweir             aLocale = rLocale;
133*cdf0e10cSrcweir             if (rSortAlgorithm.getLength() == 0)
134*cdf0e10cSrcweir                 aSortAlgorithm = ld.getDefaultIndexAlgorithm( rLocale );
135*cdf0e10cSrcweir             else
136*cdf0e10cSrcweir                 aSortAlgorithm = rSortAlgorithm;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir             OUString module = ld.getIndexModuleByAlgorithm(rLocale, aSortAlgorithm);
139*cdf0e10cSrcweir             if (module.getLength() > 0 && createLocaleSpecificIndexEntrySupplier(module))
140*cdf0e10cSrcweir                 return xIES;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir             sal_Int32 l = rLocale.Language.getLength();
143*cdf0e10cSrcweir             sal_Int32 c = rLocale.Country.getLength();
144*cdf0e10cSrcweir             sal_Int32 v = rLocale.Variant.getLength();
145*cdf0e10cSrcweir             sal_Int32 a = aSortAlgorithm.getLength();
146*cdf0e10cSrcweir             OUStringBuffer aBuf(l+c+v+a+4);
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir             if ((l > 0 && c > 0 && v > 0 && a > 0 &&
149*cdf0e10cSrcweir                         // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
150*cdf0e10cSrcweir                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
151*cdf0e10cSrcweir                                     rLocale.Country).append(under).append(rLocale.Variant).append(under).append(
152*cdf0e10cSrcweir                                     aSortAlgorithm).makeStringAndClear())) ||
153*cdf0e10cSrcweir                 (l > 0 && c > 0 && a > 0 &&
154*cdf0e10cSrcweir                         // load service with name <base>_<lang>_<country>_<algorithm>
155*cdf0e10cSrcweir                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
156*cdf0e10cSrcweir                                     rLocale.Country).append(under).append(aSortAlgorithm).makeStringAndClear())) ||
157*cdf0e10cSrcweir                 (l > 0 && c > 0 && a > 0 && rLocale.Language.compareToAscii("zh") == 0 &&
158*cdf0e10cSrcweir                                             (rLocale.Country.compareToAscii("HK") == 0 ||
159*cdf0e10cSrcweir                                             rLocale.Country.compareToAscii("MO") == 0) &&
160*cdf0e10cSrcweir                         // if the country code is HK or MO, one more step to try TW.
161*cdf0e10cSrcweir                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).appendAscii(
162*cdf0e10cSrcweir                                     "TW").append(under).append(aSortAlgorithm).makeStringAndClear())) ||
163*cdf0e10cSrcweir                 (l > 0 && a > 0 &&
164*cdf0e10cSrcweir                         // load service with name <base>_<lang>_<algorithm>
165*cdf0e10cSrcweir                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
166*cdf0e10cSrcweir                                     aSortAlgorithm).makeStringAndClear())) ||
167*cdf0e10cSrcweir                         // load service with name <base>_<algorithm>
168*cdf0e10cSrcweir                 (a > 0 && createLocaleSpecificIndexEntrySupplier(aSortAlgorithm)) ||
169*cdf0e10cSrcweir                         // load default service with name <base>_Unicode
170*cdf0e10cSrcweir                         createLocaleSpecificIndexEntrySupplier(OUString::createFromAscii("Unicode"))) {
171*cdf0e10cSrcweir                 return xIES;
172*cdf0e10cSrcweir             }
173*cdf0e10cSrcweir         }
174*cdf0e10cSrcweir         throw RuntimeException();
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir OUString SAL_CALL IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages,
178*cdf0e10cSrcweir         const Locale& rLocale ) throw (RuntimeException)
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir         Sequence< OUString > aFollowPageWords = LocaleData().getFollowPageWords(rLocale);
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir         return (bMorePages && aFollowPageWords.getLength() > 1) ?
183*cdf0e10cSrcweir             aFollowPageWords[1] : (aFollowPageWords.getLength() > 0 ?
184*cdf0e10cSrcweir             aFollowPageWords[0] : OUString());
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir OUString SAL_CALL
190*cdf0e10cSrcweir IndexEntrySupplier::getImplementationName() throw( RuntimeException )
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir         return OUString::createFromAscii( implementationName );
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir sal_Bool SAL_CALL
196*cdf0e10cSrcweir IndexEntrySupplier::supportsService(const OUString& rServiceName) throw( RuntimeException )
197*cdf0e10cSrcweir {
198*cdf0e10cSrcweir         return rServiceName.compareToAscii(implementationName) == 0;
199*cdf0e10cSrcweir }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir Sequence< OUString > SAL_CALL
202*cdf0e10cSrcweir IndexEntrySupplier::getSupportedServiceNames() throw( RuntimeException )
203*cdf0e10cSrcweir {
204*cdf0e10cSrcweir         Sequence< OUString > aRet(1);
205*cdf0e10cSrcweir         aRet[0] = OUString::createFromAscii( implementationName );
206*cdf0e10cSrcweir         return aRet;
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir } } } }
210