nss: upgrade to release 3.73
[LibreOffice.git] / i18npool / source / indexentry / indexentrysupplier.cxx
blobf98856ee222700525905d121d37e2a6c90ef884a
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/ref.hxx>
21 #include <cppuhelper/supportsservice.hxx>
22 #include <indexentrysupplier.hxx>
23 #include <localedata.hxx>
25 #include <com/sun/star/uno/XComponentContext.hpp>
27 using namespace ::com::sun::star::uno;
28 using namespace ::com::sun::star::lang;
30 namespace i18npool {
32 IndexEntrySupplier::IndexEntrySupplier( const Reference < XComponentContext >& rxContext ) : m_xContext( rxContext )
36 Sequence < Locale > SAL_CALL IndexEntrySupplier::getLocaleList()
38 return LocaleDataImpl::get()->getAllInstalledLocaleNames();
41 Sequence < OUString > SAL_CALL IndexEntrySupplier::getAlgorithmList( const Locale& rLocale )
43 return LocaleDataImpl::get()->getIndexAlgorithm(rLocale);
46 sal_Bool SAL_CALL IndexEntrySupplier::loadAlgorithm( const Locale& rLocale, const OUString& SortAlgorithm,
47 sal_Int32 collatorOptions )
49 const Sequence < OUString > algorithmList = getAlgorithmList( rLocale );
50 if (std::any_of(algorithmList.begin(), algorithmList.end(),
51 [this, &SortAlgorithm, &rLocale](const OUString& rAlgorithm) {
52 return rAlgorithm == SortAlgorithm
53 && getLocaleSpecificIndexEntrySupplier(rLocale, SortAlgorithm).is(); }))
54 return xIES->loadAlgorithm(rLocale, SortAlgorithm, collatorOptions);
55 return false;
58 sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale )
60 return LocaleDataImpl::get()->hasPhonetic(rLocale);
63 OUString SAL_CALL IndexEntrySupplier::getPhoneticCandidate( const OUString& rIndexEntry,
64 const Locale& rLocale )
66 if (!getLocaleSpecificIndexEntrySupplier(rLocale, OUString()).is())
67 throw RuntimeException();
68 return xIES->getPhoneticCandidate(rIndexEntry, rLocale);
71 OUString SAL_CALL IndexEntrySupplier::getIndexKey( const OUString& rIndexEntry,
72 const OUString& rPhoneticEntry, const Locale& rLocale )
74 if (!xIES.is())
75 throw RuntimeException();
76 return xIES->getIndexKey(rIndexEntry, rPhoneticEntry, rLocale);
79 sal_Int16 SAL_CALL IndexEntrySupplier::compareIndexEntry(
80 const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
81 const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
83 if (!xIES.is())
84 throw RuntimeException();
85 return xIES->compareIndexEntry(rIndexEntry1, rPhoneticEntry1, rLocale1,
86 rIndexEntry2, rPhoneticEntry2, rLocale2);
89 OUString SAL_CALL IndexEntrySupplier::getIndexCharacter( const OUString& rIndexEntry,
90 const Locale& rLocale, const OUString& rSortAlgorithm )
92 return getLocaleSpecificIndexEntrySupplier(rLocale, rSortAlgorithm)->
93 getIndexCharacter( rIndexEntry, rLocale, rSortAlgorithm );
96 bool IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString& name)
98 Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
99 "com.sun.star.i18n.IndexEntrySupplier_" + name, m_xContext);
101 if ( xI.is() ) {
102 xIES.set( xI, UNO_QUERY );
103 return xIES.is();
105 return false;
108 Reference < css::i18n::XExtendedIndexEntrySupplier > const &
109 IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale& rLocale, const OUString& rSortAlgorithm)
111 if (xIES.is() && rSortAlgorithm == aSortAlgorithm && rLocale.Language == aLocale.Language &&
112 rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
113 return xIES;
114 else {
115 rtl::Reference<LocaleDataImpl> ld(new LocaleDataImpl);
116 aLocale = rLocale;
117 if (rSortAlgorithm.isEmpty())
118 aSortAlgorithm = ld->getDefaultIndexAlgorithm( rLocale );
119 else
120 aSortAlgorithm = rSortAlgorithm;
122 OUString module = ld->getIndexModuleByAlgorithm(rLocale, aSortAlgorithm);
123 if (!module.isEmpty() && createLocaleSpecificIndexEntrySupplier(module))
124 return xIES;
126 bool bLoaded = false;
127 if (!aSortAlgorithm.isEmpty())
129 // Load service with name <base>_<lang>_<country>_<algorithm>
130 // or <base>_<bcp47>_<algorithm> and fallbacks.
131 bLoaded = createLocaleSpecificIndexEntrySupplier(
132 LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_" + aSortAlgorithm);
133 if (!bLoaded)
135 ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
136 for (auto const& fallback : aFallbacks)
138 bLoaded = createLocaleSpecificIndexEntrySupplier(fallback + "_" + aSortAlgorithm);
139 if (bLoaded)
140 break;
142 if (!bLoaded)
144 // load service with name <base>_<algorithm>
145 bLoaded = createLocaleSpecificIndexEntrySupplier( aSortAlgorithm);
149 if (!bLoaded)
151 // load default service with name <base>_Unicode
152 bLoaded = createLocaleSpecificIndexEntrySupplier( "Unicode");
153 if (!bLoaded)
155 throw RuntimeException(); // could not load any service
158 return xIES;
162 OUString SAL_CALL IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages,
163 const Locale& rLocale )
165 Sequence< OUString > aFollowPageWords = LocaleDataImpl::get()->getFollowPageWords(rLocale);
167 return (bMorePages && aFollowPageWords.getLength() > 1) ?
168 aFollowPageWords[1] : (aFollowPageWords.hasElements() ?
169 aFollowPageWords[0] : OUString());
172 #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
174 OUString SAL_CALL
175 IndexEntrySupplier::getImplementationName()
177 return implementationName;
180 sal_Bool SAL_CALL
181 IndexEntrySupplier::supportsService(const OUString& rServiceName)
183 return cppu::supportsService(this, rServiceName);
186 Sequence< OUString > SAL_CALL
187 IndexEntrySupplier::getSupportedServiceNames()
189 Sequence< OUString > aRet { implementationName };
190 return aRet;
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */