Bump version to 4.3-4
[LibreOffice.git] / i18npool / source / indexentry / indexentrysupplier.cxx
blobb64899abcab870b97b081841d449d7c7798eea6a
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 <cppuhelper/supportsservice.hxx>
22 #include <indexentrysupplier.hxx>
23 #include <localedata.hxx>
25 using namespace ::com::sun::star::uno;
26 using namespace ::com::sun::star::lang;
27 using namespace ::rtl;
29 namespace com { namespace sun { namespace star { namespace i18n {
31 IndexEntrySupplier::IndexEntrySupplier( const Reference < XComponentContext >& rxContext ) : m_xContext( rxContext )
35 Sequence < Locale > SAL_CALL IndexEntrySupplier::getLocaleList() throw (RuntimeException, std::exception)
37 return LocaleDataImpl().getAllInstalledLocaleNames();
40 Sequence < OUString > SAL_CALL IndexEntrySupplier::getAlgorithmList( const Locale& rLocale ) throw (RuntimeException, std::exception)
42 return LocaleDataImpl().getIndexAlgorithm(rLocale);
45 sal_Bool SAL_CALL IndexEntrySupplier::loadAlgorithm( const Locale& rLocale, const OUString& SortAlgorithm,
46 sal_Int32 collatorOptions ) throw (RuntimeException, std::exception)
48 Sequence < OUString > algorithmList = getAlgorithmList( rLocale );
49 for (sal_Int32 i = 0; i < algorithmList.getLength(); i++) {
50 if (algorithmList[i] == SortAlgorithm) {
51 if (getLocaleSpecificIndexEntrySupplier(rLocale, SortAlgorithm).is())
52 return xIES->loadAlgorithm(rLocale, SortAlgorithm, collatorOptions);
55 return sal_False;
58 sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale ) throw (RuntimeException, std::exception)
60 return LocaleDataImpl().hasPhonetic(rLocale);
63 OUString SAL_CALL IndexEntrySupplier::getPhoneticCandidate( const OUString& rIndexEntry,
64 const Locale& rLocale ) throw (RuntimeException, std::exception)
66 if (getLocaleSpecificIndexEntrySupplier(rLocale, OUString()).is())
67 return xIES->getPhoneticCandidate(rIndexEntry, rLocale);
68 else
69 throw RuntimeException();
72 OUString SAL_CALL IndexEntrySupplier::getIndexKey( const OUString& rIndexEntry,
73 const OUString& rPhoneticEntry, const Locale& rLocale ) throw (RuntimeException, std::exception)
75 if (xIES.is())
76 return xIES->getIndexKey(rIndexEntry, rPhoneticEntry, rLocale);
77 else
78 throw RuntimeException();
81 sal_Int16 SAL_CALL IndexEntrySupplier::compareIndexEntry(
82 const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
83 const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
84 throw (com::sun::star::uno::RuntimeException, std::exception)
86 if (xIES.is())
87 return xIES->compareIndexEntry(rIndexEntry1, rPhoneticEntry1, rLocale1,
88 rIndexEntry2, rPhoneticEntry2, rLocale2);
89 else
90 throw RuntimeException();
93 OUString SAL_CALL IndexEntrySupplier::getIndexCharacter( const OUString& rIndexEntry,
94 const Locale& rLocale, const OUString& rSortAlgorithm )
95 throw (RuntimeException, std::exception)
97 return getLocaleSpecificIndexEntrySupplier(rLocale, rSortAlgorithm)->
98 getIndexCharacter( rIndexEntry, rLocale, rSortAlgorithm );
101 bool SAL_CALL IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString& name) throw( RuntimeException )
103 Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
104 OUString("com.sun.star.i18n.IndexEntrySupplier_") + name, m_xContext);
106 if ( xI.is() ) {
107 xIES.set( xI, UNO_QUERY );
108 return xIES.is();
110 return false;
113 Reference < com::sun::star::i18n::XExtendedIndexEntrySupplier > SAL_CALL
114 IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale& rLocale, const OUString& rSortAlgorithm) throw (RuntimeException)
116 if (xIES.is() && rSortAlgorithm == aSortAlgorithm && rLocale.Language == aLocale.Language &&
117 rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
118 return xIES;
119 else {
120 LocaleDataImpl ld;
121 aLocale = rLocale;
122 if (rSortAlgorithm.isEmpty())
123 aSortAlgorithm = ld.getDefaultIndexAlgorithm( rLocale );
124 else
125 aSortAlgorithm = rSortAlgorithm;
127 OUString module = ld.getIndexModuleByAlgorithm(rLocale, aSortAlgorithm);
128 if (!module.isEmpty() && createLocaleSpecificIndexEntrySupplier(module))
129 return xIES;
131 bool bLoaded = false;
132 if (!aSortAlgorithm.isEmpty())
134 // Load service with name <base>_<lang>_<country>_<algorithm>
135 // or <base>_<bcp47>_<algorithm> and fallbacks.
136 bLoaded = createLocaleSpecificIndexEntrySupplier(
137 LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_" + aSortAlgorithm);
138 if (!bLoaded)
140 ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
141 for (::std::vector< OUString >::const_iterator it( aFallbacks.begin()); it != aFallbacks.end(); ++it)
143 bLoaded = createLocaleSpecificIndexEntrySupplier( *it + "_" + aSortAlgorithm);
144 if (bLoaded)
145 break;
147 if (!bLoaded)
149 // load service with name <base>_<algorithm>
150 bLoaded = createLocaleSpecificIndexEntrySupplier( aSortAlgorithm);
154 if (!bLoaded)
156 // load default service with name <base>_Unicode
157 bLoaded = createLocaleSpecificIndexEntrySupplier( "Unicode");
158 if (!bLoaded)
160 throw RuntimeException(); // could not load any service
163 return xIES;
167 OUString SAL_CALL IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages,
168 const Locale& rLocale ) throw (RuntimeException, std::exception)
170 Sequence< OUString > aFollowPageWords = LocaleDataImpl().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, std::exception )
182 return OUString::createFromAscii( implementationName );
185 sal_Bool SAL_CALL
186 IndexEntrySupplier::supportsService(const OUString& rServiceName) throw( RuntimeException, std::exception )
188 return cppu::supportsService(this, rServiceName);
191 Sequence< OUString > SAL_CALL
192 IndexEntrySupplier::getSupportedServiceNames() throw( RuntimeException, std::exception )
194 Sequence< OUString > aRet(1);
195 aRet[0] = OUString::createFromAscii( implementationName );
196 return aRet;
199 } } } }
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */