update emoji autocorrect entries from po-files
[LibreOffice.git] / i18npool / source / indexentry / indexentrysupplier.cxx
blobfc109658fb407057badcd090f4f43056174e52de
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;
28 namespace com { namespace sun { namespace star { namespace i18n {
30 IndexEntrySupplier::IndexEntrySupplier( const Reference < XComponentContext >& rxContext ) : m_xContext( rxContext )
34 Sequence < Locale > SAL_CALL IndexEntrySupplier::getLocaleList() throw (RuntimeException, std::exception)
36 return LocaleDataImpl().getAllInstalledLocaleNames();
39 Sequence < OUString > SAL_CALL IndexEntrySupplier::getAlgorithmList( const Locale& rLocale ) throw (RuntimeException, std::exception)
41 return LocaleDataImpl().getIndexAlgorithm(rLocale);
44 sal_Bool SAL_CALL IndexEntrySupplier::loadAlgorithm( const Locale& rLocale, const OUString& SortAlgorithm,
45 sal_Int32 collatorOptions ) throw (RuntimeException, std::exception)
47 Sequence < OUString > algorithmList = getAlgorithmList( rLocale );
48 for (sal_Int32 i = 0; i < algorithmList.getLength(); i++) {
49 if (algorithmList[i] == SortAlgorithm) {
50 if (getLocaleSpecificIndexEntrySupplier(rLocale, SortAlgorithm).is())
51 return xIES->loadAlgorithm(rLocale, SortAlgorithm, collatorOptions);
54 return sal_False;
57 sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale ) throw (RuntimeException, std::exception)
59 return LocaleDataImpl().hasPhonetic(rLocale);
62 OUString SAL_CALL IndexEntrySupplier::getPhoneticCandidate( const OUString& rIndexEntry,
63 const Locale& rLocale ) throw (RuntimeException, std::exception)
65 if (getLocaleSpecificIndexEntrySupplier(rLocale, OUString()).is())
66 return xIES->getPhoneticCandidate(rIndexEntry, rLocale);
67 else
68 throw RuntimeException();
71 OUString SAL_CALL IndexEntrySupplier::getIndexKey( const OUString& rIndexEntry,
72 const OUString& rPhoneticEntry, const Locale& rLocale ) throw (RuntimeException, std::exception)
74 if (xIES.is())
75 return xIES->getIndexKey(rIndexEntry, rPhoneticEntry, rLocale);
76 else
77 throw RuntimeException();
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 )
83 throw (com::sun::star::uno::RuntimeException, std::exception)
85 if (xIES.is())
86 return xIES->compareIndexEntry(rIndexEntry1, rPhoneticEntry1, rLocale1,
87 rIndexEntry2, rPhoneticEntry2, rLocale2);
88 else
89 throw RuntimeException();
92 OUString SAL_CALL IndexEntrySupplier::getIndexCharacter( const OUString& rIndexEntry,
93 const Locale& rLocale, const OUString& rSortAlgorithm )
94 throw (RuntimeException, std::exception)
96 return getLocaleSpecificIndexEntrySupplier(rLocale, rSortAlgorithm)->
97 getIndexCharacter( rIndexEntry, rLocale, rSortAlgorithm );
100 bool SAL_CALL IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString& name) throw( RuntimeException )
102 Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
103 "com.sun.star.i18n.IndexEntrySupplier_" + name, m_xContext);
105 if ( xI.is() ) {
106 xIES.set( xI, UNO_QUERY );
107 return xIES.is();
109 return false;
112 Reference < com::sun::star::i18n::XExtendedIndexEntrySupplier > SAL_CALL
113 IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale& rLocale, const OUString& rSortAlgorithm) throw (RuntimeException)
115 if (xIES.is() && rSortAlgorithm == aSortAlgorithm && rLocale.Language == aLocale.Language &&
116 rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
117 return xIES;
118 else {
119 LocaleDataImpl ld;
120 aLocale = rLocale;
121 if (rSortAlgorithm.isEmpty())
122 aSortAlgorithm = ld.getDefaultIndexAlgorithm( rLocale );
123 else
124 aSortAlgorithm = rSortAlgorithm;
126 OUString module = ld.getIndexModuleByAlgorithm(rLocale, aSortAlgorithm);
127 if (!module.isEmpty() && createLocaleSpecificIndexEntrySupplier(module))
128 return xIES;
130 bool bLoaded = false;
131 if (!aSortAlgorithm.isEmpty())
133 // Load service with name <base>_<lang>_<country>_<algorithm>
134 // or <base>_<bcp47>_<algorithm> and fallbacks.
135 bLoaded = createLocaleSpecificIndexEntrySupplier(
136 LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_" + aSortAlgorithm);
137 if (!bLoaded)
139 ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
140 for (::std::vector< OUString >::const_iterator it( aFallbacks.begin()); it != aFallbacks.end(); ++it)
142 bLoaded = createLocaleSpecificIndexEntrySupplier( *it + "_" + aSortAlgorithm);
143 if (bLoaded)
144 break;
146 if (!bLoaded)
148 // load service with name <base>_<algorithm>
149 bLoaded = createLocaleSpecificIndexEntrySupplier( aSortAlgorithm);
153 if (!bLoaded)
155 // load default service with name <base>_Unicode
156 bLoaded = createLocaleSpecificIndexEntrySupplier( "Unicode");
157 if (!bLoaded)
159 throw RuntimeException(); // could not load any service
162 return xIES;
166 OUString SAL_CALL IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages,
167 const Locale& rLocale ) throw (RuntimeException, std::exception)
169 Sequence< OUString > aFollowPageWords = LocaleDataImpl().getFollowPageWords(rLocale);
171 return (bMorePages && aFollowPageWords.getLength() > 1) ?
172 aFollowPageWords[1] : (aFollowPageWords.getLength() > 0 ?
173 aFollowPageWords[0] : OUString());
176 #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
178 OUString SAL_CALL
179 IndexEntrySupplier::getImplementationName() throw( RuntimeException, std::exception )
181 return OUString( implementationName );
184 sal_Bool SAL_CALL
185 IndexEntrySupplier::supportsService(const OUString& rServiceName) throw( RuntimeException, std::exception )
187 return cppu::supportsService(this, rServiceName);
190 Sequence< OUString > SAL_CALL
191 IndexEntrySupplier::getSupportedServiceNames() throw( RuntimeException, std::exception )
193 Sequence< OUString > aRet(1);
194 aRet[0] = implementationName;
195 return aRet;
198 } } } }
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */