Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / i18npool / source / indexentry / indexentrysupplier.cxx
blob53fc96b9b180d2d98f8b5549d41fc5d07ad78b3c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/ustrbuf.hxx>
21 #include <indexentrysupplier.hxx>
22 #include <localedata.hxx>
24 using namespace ::com::sun::star::uno;
25 using namespace ::com::sun::star::lang;
26 using namespace ::rtl;
28 static const sal_Unicode under = sal_Unicode('_');
30 namespace com { namespace sun { namespace star { namespace i18n {
32 IndexEntrySupplier::IndexEntrySupplier( const Reference < XMultiServiceFactory >& rxMSF ) : xMSF( rxMSF )
36 Sequence < Locale > SAL_CALL IndexEntrySupplier::getLocaleList() throw (RuntimeException)
38 return LocaleData().getAllInstalledLocaleNames();
41 Sequence < OUString > SAL_CALL IndexEntrySupplier::getAlgorithmList( const Locale& rLocale ) throw (RuntimeException)
43 return LocaleData().getIndexAlgorithm(rLocale);
46 sal_Bool SAL_CALL IndexEntrySupplier::loadAlgorithm( const Locale& rLocale, const OUString& SortAlgorithm,
47 sal_Int32 collatorOptions ) throw (RuntimeException)
49 Sequence < OUString > algorithmList = getAlgorithmList( rLocale );
50 for (sal_Int32 i = 0; i < algorithmList.getLength(); i++) {
51 if (algorithmList[i] == SortAlgorithm) {
52 if (getLocaleSpecificIndexEntrySupplier(rLocale, SortAlgorithm).is())
53 return xIES->loadAlgorithm(rLocale, SortAlgorithm, collatorOptions);
56 return sal_False;
59 sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale ) throw (RuntimeException)
61 return LocaleData().hasPhonetic(rLocale);
64 OUString SAL_CALL IndexEntrySupplier::getPhoneticCandidate( const OUString& rIndexEntry,
65 const Locale& rLocale ) throw (RuntimeException)
67 if (getLocaleSpecificIndexEntrySupplier(rLocale, OUString()).is())
68 return xIES->getPhoneticCandidate(rIndexEntry, rLocale);
69 else
70 throw RuntimeException();
73 OUString SAL_CALL IndexEntrySupplier::getIndexKey( const OUString& rIndexEntry,
74 const OUString& rPhoneticEntry, const Locale& rLocale ) throw (RuntimeException)
76 if (xIES.is())
77 return xIES->getIndexKey(rIndexEntry, rPhoneticEntry, rLocale);
78 else
79 throw RuntimeException();
82 sal_Int16 SAL_CALL IndexEntrySupplier::compareIndexEntry(
83 const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
84 const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
85 throw (com::sun::star::uno::RuntimeException)
87 if (xIES.is())
88 return xIES->compareIndexEntry(rIndexEntry1, rPhoneticEntry1, rLocale1,
89 rIndexEntry2, rPhoneticEntry2, rLocale2);
90 else
91 throw RuntimeException();
94 OUString SAL_CALL IndexEntrySupplier::getIndexCharacter( const OUString& rIndexEntry,
95 const Locale& rLocale, const OUString& rSortAlgorithm )
96 throw (RuntimeException)
98 return getLocaleSpecificIndexEntrySupplier(rLocale, rSortAlgorithm)->
99 getIndexCharacter( rIndexEntry, rLocale, rSortAlgorithm );
102 sal_Bool SAL_CALL IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString& name) throw( RuntimeException )
104 Reference < XInterface > xI = xMSF->createInstance(
105 OUString("com.sun.star.i18n.IndexEntrySupplier_") + name);
107 if ( xI.is() ) {
108 xI->queryInterface( ::getCppuType((const Reference< com::sun::star::i18n::XExtendedIndexEntrySupplier>*)0) ) >>= xIES;
109 return xIES.is();
111 return sal_False;
114 Reference < com::sun::star::i18n::XExtendedIndexEntrySupplier > SAL_CALL
115 IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale& rLocale, const OUString& rSortAlgorithm) throw (RuntimeException)
117 if (xIES.is() && rSortAlgorithm == aSortAlgorithm && rLocale.Language == aLocale.Language &&
118 rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
119 return xIES;
120 else if (xMSF.is()) {
121 LocaleData ld;
122 aLocale = rLocale;
123 if (rSortAlgorithm.isEmpty())
124 aSortAlgorithm = ld.getDefaultIndexAlgorithm( rLocale );
125 else
126 aSortAlgorithm = rSortAlgorithm;
128 OUString module = ld.getIndexModuleByAlgorithm(rLocale, aSortAlgorithm);
129 if (!module.isEmpty() && createLocaleSpecificIndexEntrySupplier(module))
130 return xIES;
132 sal_Int32 l = rLocale.Language.getLength();
133 sal_Int32 c = rLocale.Country.getLength();
134 sal_Int32 v = rLocale.Variant.getLength();
135 sal_Int32 a = aSortAlgorithm.getLength();
136 OUStringBuffer aBuf(l+c+v+a+4);
138 if ((l > 0 && c > 0 && v > 0 && a > 0 &&
139 // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
140 createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
141 rLocale.Country).append(under).append(rLocale.Variant).append(under).append(
142 aSortAlgorithm).makeStringAndClear())) ||
143 (l > 0 && c > 0 && a > 0 &&
144 // load service with name <base>_<lang>_<country>_<algorithm>
145 createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
146 rLocale.Country).append(under).append(aSortAlgorithm).makeStringAndClear())) ||
147 (l > 0 && c > 0 && a > 0 && rLocale.Language.compareToAscii("zh") == 0 &&
148 (rLocale.Country.compareToAscii("HK") == 0 ||
149 rLocale.Country.compareToAscii("MO") == 0) &&
150 // if the country code is HK or MO, one more step to try TW.
151 createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).appendAscii(
152 "TW").append(under).append(aSortAlgorithm).makeStringAndClear())) ||
153 (l > 0 && a > 0 &&
154 // load service with name <base>_<lang>_<algorithm>
155 createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
156 aSortAlgorithm).makeStringAndClear())) ||
157 // load service with name <base>_<algorithm>
158 (a > 0 && createLocaleSpecificIndexEntrySupplier(aSortAlgorithm)) ||
159 // load default service with name <base>_Unicode
160 createLocaleSpecificIndexEntrySupplier(OUString("Unicode"))) {
161 return xIES;
164 throw RuntimeException();
167 OUString SAL_CALL IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages,
168 const Locale& rLocale ) throw (RuntimeException)
170 Sequence< OUString > aFollowPageWords = LocaleData().getFollowPageWords(rLocale);
172 return (bMorePages && aFollowPageWords.getLength() > 1) ?
173 aFollowPageWords[1] : (aFollowPageWords.getLength() > 0 ?
174 aFollowPageWords[0] : OUString());
177 #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
179 OUString SAL_CALL
180 IndexEntrySupplier::getImplementationName() throw( RuntimeException )
182 return OUString::createFromAscii( implementationName );
185 sal_Bool SAL_CALL
186 IndexEntrySupplier::supportsService(const OUString& rServiceName) throw( RuntimeException )
188 return rServiceName.compareToAscii(implementationName) == 0;
191 Sequence< OUString > SAL_CALL
192 IndexEntrySupplier::getSupportedServiceNames() throw( RuntimeException )
194 Sequence< OUString > aRet(1);
195 aRet[0] = OUString::createFromAscii( implementationName );
196 return aRet;
199 } } } }
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */