LibreOffice Module i18npool (master)  1
indexentrysupplier.cxx
Go to the documentation of this file.
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>
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 
33 {
34 }
35 
36 Sequence < Locale > SAL_CALL IndexEntrySupplier::getLocaleList()
37 {
38  return LocaleDataImpl::get()->getAllInstalledLocaleNames();
39 }
40 
42 {
43  return LocaleDataImpl::get()->getIndexAlgorithm(rLocale);
44 }
45 
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 
58 sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale )
59 {
60  return LocaleDataImpl::get()->hasPhonetic(rLocale);
61 }
62 
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 
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 
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 
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 
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 &
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 {
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.
132  OUString(
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>
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 
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
178 {
179  return implementationName;
180 }
181 
182 sal_Bool SAL_CALL
183 IndexEntrySupplier::supportsService(const OUString& rServiceName)
184 {
185  return cppu::supportsService(this, rServiceName);
186 }
187 
188 Sequence< OUString > SAL_CALL
190 {
192  return aRet;
193 }
194 
195 }
196 
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Sequence< OUString > SAL_CALL getAlgorithmList(const css::lang::Locale &rLocale) override
virtual OUString SAL_CALL getImplementationName() override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual css::uno::Sequence< css::lang::Locale > SAL_CALL getLocaleList() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
bool createLocaleSpecificIndexEntrySupplier(std::u16string_view name)
static OUString getFirstLocaleServiceName(const css::lang::Locale &rLocale)
Generates a _ or (if Language=="qlt") string suitable as part of service...
virtual OUString SAL_CALL getPhoneticCandidate(const OUString &IndexEntry, const css::lang::Locale &rLocale) override
static rtl::Reference< LocaleDataImpl > get()
Definition: localedata.hxx:62
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual sal_Int16 SAL_CALL compareIndexEntry(const OUString &IndexEntry1, const OUString &PhoneticEntry1, const css::lang::Locale &rLocale1, const OUString &IndexEntry2, const OUString &PhoneticEntry2, const css::lang::Locale &rLocale2) override
float u
unsigned char sal_Bool
Constant values shared between i18npool and, for example, the number formatter.
IndexEntrySupplier(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
static::std::vector< OUString > getFallbackLocaleServiceNames(const css::lang::Locale &rLocale)
Generates fallback strings suitable as parts of service names, excluding the one obtained via getFirs...
virtual sal_Bool SAL_CALL usePhoneticEntry(const css::lang::Locale &rLocale) override
virtual sal_Bool SAL_CALL loadAlgorithm(const css::lang::Locale &rLocale, const OUString &SortAlgorithm, sal_Int32 collatorOptions) override
virtual OUString SAL_CALL getIndexFollowPageWord(sal_Bool MorePages, const css::lang::Locale &rLocale) override
css::uno::Reference< css::i18n::XExtendedIndexEntrySupplier > xIES
css::uno::Reference< css::i18n::XExtendedIndexEntrySupplier > const & getLocaleSpecificIndexEntrySupplier(const css::lang::Locale &rLocale, const OUString &rSortAlgorithm)
virtual OUString SAL_CALL getIndexKey(const OUString &IndexEntry, const OUString &PhoneticEntry, const css::lang::Locale &rLocale) override
Reference< XComponentContext > m_xContext
virtual OUString SAL_CALL getIndexCharacter(const OUString &IndexEntry, const css::lang::Locale &rLocale, const OUString &SortAlgorithm) override
constexpr OUStringLiteral implementationName