1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <rtl/ref.hxx>
21 #include <cppuhelper/supportsservice.hxx>
22 #include <indexentrysupplier.hxx>
23 #include <localedata.hxx>
24 
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 
27 using namespace ::com::sun::star::uno;
28 using namespace ::com::sun::star::lang;
29 
30 namespace i18npool {
31 
IndexEntrySupplier(const Reference<XComponentContext> & rxContext)32 IndexEntrySupplier::IndexEntrySupplier( const Reference < XComponentContext >& rxContext ) : m_xContext( rxContext )
33 {
34 }
35 
getLocaleList()36 Sequence < Locale > SAL_CALL IndexEntrySupplier::getLocaleList()
37 {
38     return LocaleDataImpl::get()->getAllInstalledLocaleNames();
39 }
40 
getAlgorithmList(const Locale & rLocale)41 Sequence < OUString > SAL_CALL IndexEntrySupplier::getAlgorithmList( const Locale& rLocale )
42 {
43     return LocaleDataImpl::get()->getIndexAlgorithm(rLocale);
44 }
45 
loadAlgorithm(const Locale & rLocale,const OUString & SortAlgorithm,sal_Int32 collatorOptions)46 sal_Bool SAL_CALL IndexEntrySupplier::loadAlgorithm( const Locale& rLocale, const OUString& SortAlgorithm,
47         sal_Int32 collatorOptions )
48 {
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;
56 }
57 
usePhoneticEntry(const Locale & rLocale)58 sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale )
59 {
60     return LocaleDataImpl::get()->hasPhonetic(rLocale);
61 }
62 
getPhoneticCandidate(const OUString & rIndexEntry,const Locale & rLocale)63 OUString SAL_CALL IndexEntrySupplier::getPhoneticCandidate( const OUString& rIndexEntry,
64         const Locale& rLocale )
65 {
66     if (!getLocaleSpecificIndexEntrySupplier(rLocale, OUString()).is())
67         throw RuntimeException();
68     return xIES->getPhoneticCandidate(rIndexEntry, rLocale);
69 }
70 
getIndexKey(const OUString & rIndexEntry,const OUString & rPhoneticEntry,const Locale & rLocale)71 OUString SAL_CALL IndexEntrySupplier::getIndexKey( const OUString& rIndexEntry,
72         const OUString& rPhoneticEntry, const Locale& rLocale )
73 {
74     if (!xIES.is())
75         throw RuntimeException();
76     return xIES->getIndexKey(rIndexEntry, rPhoneticEntry, rLocale);
77 }
78 
compareIndexEntry(const OUString & rIndexEntry1,const OUString & rPhoneticEntry1,const Locale & rLocale1,const OUString & rIndexEntry2,const OUString & rPhoneticEntry2,const Locale & rLocale2)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 )
82 {
83     if (!xIES.is())
84         throw RuntimeException();
85     return xIES->compareIndexEntry(rIndexEntry1, rPhoneticEntry1, rLocale1,
86             rIndexEntry2, rPhoneticEntry2, rLocale2);
87 }
88 
getIndexCharacter(const OUString & rIndexEntry,const Locale & rLocale,const OUString & rSortAlgorithm)89 OUString SAL_CALL IndexEntrySupplier::getIndexCharacter( const OUString& rIndexEntry,
90         const Locale& rLocale, const OUString& rSortAlgorithm )
91 {
92     return getLocaleSpecificIndexEntrySupplier(rLocale, rSortAlgorithm)->
93         getIndexCharacter( rIndexEntry, rLocale, rSortAlgorithm );
94 }
95 
createLocaleSpecificIndexEntrySupplier(std::u16string_view name)96 bool IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(std::u16string_view name)
97 {
98     Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
99             OUString::Concat("com.sun.star.i18n.IndexEntrySupplier_") + name, m_xContext);
100 
101     if ( xI.is() ) {
102         xIES.set( xI, UNO_QUERY );
103         return xIES.is();
104     }
105     return false;
106 }
107 
108 Reference < css::i18n::XExtendedIndexEntrySupplier > const &
getLocaleSpecificIndexEntrySupplier(const Locale & rLocale,const OUString & rSortAlgorithm)109 IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale& rLocale, const OUString& rSortAlgorithm)
110 {
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;
121 
122         OUString module = ld->getIndexModuleByAlgorithm(rLocale, aSortAlgorithm);
123         if (!module.isEmpty() && createLocaleSpecificIndexEntrySupplier(module))
124             return xIES;
125 
126         bool bLoaded = false;
127         if (!aSortAlgorithm.isEmpty())
128         {
129             // Load service with name <base>_<lang>_<country>_<algorithm>
130             // or <base>_<bcp47>_<algorithm> and fallbacks.
131             bLoaded = createLocaleSpecificIndexEntrySupplier(
132                     OUString(
133                         LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_"
134                         + aSortAlgorithm));
135             if (!bLoaded)
136             {
137                 ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
138                 for (auto const& fallback : aFallbacks)
139                 {
140                     bLoaded = createLocaleSpecificIndexEntrySupplier(OUString(fallback + "_" + aSortAlgorithm));
141                     if (bLoaded)
142                         break;
143                 }
144                 if (!bLoaded)
145                 {
146                     // load service with name <base>_<algorithm>
147                     bLoaded = createLocaleSpecificIndexEntrySupplier( aSortAlgorithm);
148                 }
149             }
150         }
151         if (!bLoaded)
152         {
153             // load default service with name <base>_Unicode
154             bLoaded = createLocaleSpecificIndexEntrySupplier( u"Unicode");
155             if (!bLoaded)
156             {
157                 throw RuntimeException();   // could not load any service
158             }
159         }
160         return xIES;
161     }
162 }
163 
getIndexFollowPageWord(sal_Bool bMorePages,const Locale & rLocale)164 OUString SAL_CALL IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages,
165         const Locale& rLocale )
166 {
167     Sequence< OUString > aFollowPageWords = LocaleDataImpl::get()->getFollowPageWords(rLocale);
168 
169     return (bMorePages && aFollowPageWords.getLength() > 1) ?
170         aFollowPageWords[1] : (aFollowPageWords.hasElements() ?
171                 aFollowPageWords[0] : OUString());
172 }
173 
174 #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
175 
176 OUString SAL_CALL
getImplementationName()177 IndexEntrySupplier::getImplementationName()
178 {
179     return implementationName;
180 }
181 
182 sal_Bool SAL_CALL
supportsService(const OUString & rServiceName)183 IndexEntrySupplier::supportsService(const OUString& rServiceName)
184 {
185     return cppu::supportsService(this, rServiceName);
186 }
187 
188 Sequence< OUString > SAL_CALL
getSupportedServiceNames()189 IndexEntrySupplier::getSupportedServiceNames()
190 {
191     Sequence< OUString > aRet { implementationName };
192     return aRet;
193 }
194 
195 }
196 
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
198