bump product version to 6.3.0.0.beta1
[LibreOffice.git] / i18npool / source / indexentry / indexentrysupplier.cxx
blob3660c89e6e4636e61234adb27ae4f75458221f3d
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 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 false;
59 sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale )
61 return LocaleDataImpl::get()->hasPhonetic(rLocale);
64 OUString SAL_CALL IndexEntrySupplier::getPhoneticCandidate( const OUString& rIndexEntry,
65 const Locale& rLocale )
67 if (!getLocaleSpecificIndexEntrySupplier(rLocale, OUString()).is())
68 throw RuntimeException();
69 return xIES->getPhoneticCandidate(rIndexEntry, rLocale);
72 OUString SAL_CALL IndexEntrySupplier::getIndexKey( const OUString& rIndexEntry,
73 const OUString& rPhoneticEntry, const Locale& rLocale )
75 if (!xIES.is())
76 throw RuntimeException();
77 return xIES->getIndexKey(rIndexEntry, rPhoneticEntry, rLocale);
80 sal_Int16 SAL_CALL IndexEntrySupplier::compareIndexEntry(
81 const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
82 const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
84 if (!xIES.is())
85 throw RuntimeException();
86 return xIES->compareIndexEntry(rIndexEntry1, rPhoneticEntry1, rLocale1,
87 rIndexEntry2, rPhoneticEntry2, rLocale2);
90 OUString SAL_CALL IndexEntrySupplier::getIndexCharacter( const OUString& rIndexEntry,
91 const Locale& rLocale, const OUString& rSortAlgorithm )
93 return getLocaleSpecificIndexEntrySupplier(rLocale, rSortAlgorithm)->
94 getIndexCharacter( rIndexEntry, rLocale, rSortAlgorithm );
97 bool IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString& name)
99 Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
100 "com.sun.star.i18n.IndexEntrySupplier_" + name, m_xContext);
102 if ( xI.is() ) {
103 xIES.set( xI, UNO_QUERY );
104 return xIES.is();
106 return false;
109 Reference < css::i18n::XExtendedIndexEntrySupplier > const &
110 IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale& rLocale, const OUString& rSortAlgorithm)
112 if (xIES.is() && rSortAlgorithm == aSortAlgorithm && rLocale.Language == aLocale.Language &&
113 rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
114 return xIES;
115 else {
116 rtl::Reference<LocaleDataImpl> ld(new LocaleDataImpl);
117 aLocale = rLocale;
118 if (rSortAlgorithm.isEmpty())
119 aSortAlgorithm = ld->getDefaultIndexAlgorithm( rLocale );
120 else
121 aSortAlgorithm = rSortAlgorithm;
123 OUString module = ld->getIndexModuleByAlgorithm(rLocale, aSortAlgorithm);
124 if (!module.isEmpty() && createLocaleSpecificIndexEntrySupplier(module))
125 return xIES;
127 bool bLoaded = false;
128 if (!aSortAlgorithm.isEmpty())
130 // Load service with name <base>_<lang>_<country>_<algorithm>
131 // or <base>_<bcp47>_<algorithm> and fallbacks.
132 bLoaded = createLocaleSpecificIndexEntrySupplier(
133 LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_" + aSortAlgorithm);
134 if (!bLoaded)
136 ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
137 for (auto const& fallback : aFallbacks)
139 bLoaded = createLocaleSpecificIndexEntrySupplier(fallback + "_" + aSortAlgorithm);
140 if (bLoaded)
141 break;
143 if (!bLoaded)
145 // load service with name <base>_<algorithm>
146 bLoaded = createLocaleSpecificIndexEntrySupplier( aSortAlgorithm);
150 if (!bLoaded)
152 // load default service with name <base>_Unicode
153 bLoaded = createLocaleSpecificIndexEntrySupplier( "Unicode");
154 if (!bLoaded)
156 throw RuntimeException(); // could not load any service
159 return xIES;
163 OUString SAL_CALL IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages,
164 const Locale& rLocale )
166 Sequence< OUString > aFollowPageWords = LocaleDataImpl::get()->getFollowPageWords(rLocale);
168 return (bMorePages && aFollowPageWords.getLength() > 1) ?
169 aFollowPageWords[1] : (aFollowPageWords.hasElements() ?
170 aFollowPageWords[0] : OUString());
173 #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
175 OUString SAL_CALL
176 IndexEntrySupplier::getImplementationName()
178 return OUString( implementationName );
181 sal_Bool SAL_CALL
182 IndexEntrySupplier::supportsService(const OUString& rServiceName)
184 return cppu::supportsService(this, rServiceName);
187 Sequence< OUString > SAL_CALL
188 IndexEntrySupplier::getSupportedServiceNames()
190 Sequence< OUString > aRet { implementationName };
191 return aRet;
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */