Line data Source code
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/ustrbuf.hxx>
21 : #include <cppuhelper/supportsservice.hxx>
22 : #include <indexentrysupplier.hxx>
23 : #include <localedata.hxx>
24 :
25 : using namespace ::com::sun::star::uno;
26 : using namespace ::com::sun::star::lang;
27 : using namespace ::rtl;
28 :
29 : namespace com { namespace sun { namespace star { namespace i18n {
30 :
31 0 : IndexEntrySupplier::IndexEntrySupplier( const Reference < XComponentContext >& rxContext ) : m_xContext( rxContext )
32 : {
33 0 : }
34 :
35 0 : Sequence < Locale > SAL_CALL IndexEntrySupplier::getLocaleList() throw (RuntimeException, std::exception)
36 : {
37 0 : return LocaleDataImpl().getAllInstalledLocaleNames();
38 : }
39 :
40 0 : Sequence < OUString > SAL_CALL IndexEntrySupplier::getAlgorithmList( const Locale& rLocale ) throw (RuntimeException, std::exception)
41 : {
42 0 : return LocaleDataImpl().getIndexAlgorithm(rLocale);
43 : }
44 :
45 0 : sal_Bool SAL_CALL IndexEntrySupplier::loadAlgorithm( const Locale& rLocale, const OUString& SortAlgorithm,
46 : sal_Int32 collatorOptions ) throw (RuntimeException, std::exception)
47 : {
48 0 : Sequence < OUString > algorithmList = getAlgorithmList( rLocale );
49 0 : for (sal_Int32 i = 0; i < algorithmList.getLength(); i++) {
50 0 : if (algorithmList[i] == SortAlgorithm) {
51 0 : if (getLocaleSpecificIndexEntrySupplier(rLocale, SortAlgorithm).is())
52 0 : return xIES->loadAlgorithm(rLocale, SortAlgorithm, collatorOptions);
53 : }
54 : }
55 0 : return sal_False;
56 : }
57 :
58 0 : sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale ) throw (RuntimeException, std::exception)
59 : {
60 0 : return LocaleDataImpl().hasPhonetic(rLocale);
61 : }
62 :
63 0 : OUString SAL_CALL IndexEntrySupplier::getPhoneticCandidate( const OUString& rIndexEntry,
64 : const Locale& rLocale ) throw (RuntimeException, std::exception)
65 : {
66 0 : if (getLocaleSpecificIndexEntrySupplier(rLocale, OUString()).is())
67 0 : return xIES->getPhoneticCandidate(rIndexEntry, rLocale);
68 : else
69 0 : throw RuntimeException();
70 : }
71 :
72 0 : OUString SAL_CALL IndexEntrySupplier::getIndexKey( const OUString& rIndexEntry,
73 : const OUString& rPhoneticEntry, const Locale& rLocale ) throw (RuntimeException, std::exception)
74 : {
75 0 : if (xIES.is())
76 0 : return xIES->getIndexKey(rIndexEntry, rPhoneticEntry, rLocale);
77 : else
78 0 : throw RuntimeException();
79 : }
80 :
81 0 : sal_Int16 SAL_CALL IndexEntrySupplier::compareIndexEntry(
82 : const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
83 : const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
84 : throw (com::sun::star::uno::RuntimeException, std::exception)
85 : {
86 0 : if (xIES.is())
87 0 : return xIES->compareIndexEntry(rIndexEntry1, rPhoneticEntry1, rLocale1,
88 0 : rIndexEntry2, rPhoneticEntry2, rLocale2);
89 : else
90 0 : throw RuntimeException();
91 : }
92 :
93 0 : OUString SAL_CALL IndexEntrySupplier::getIndexCharacter( const OUString& rIndexEntry,
94 : const Locale& rLocale, const OUString& rSortAlgorithm )
95 : throw (RuntimeException, std::exception)
96 : {
97 0 : return getLocaleSpecificIndexEntrySupplier(rLocale, rSortAlgorithm)->
98 0 : getIndexCharacter( rIndexEntry, rLocale, rSortAlgorithm );
99 : }
100 :
101 0 : sal_Bool SAL_CALL IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString& name) throw( RuntimeException )
102 : {
103 0 : Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
104 0 : OUString("com.sun.star.i18n.IndexEntrySupplier_") + name, m_xContext);
105 :
106 0 : if ( xI.is() ) {
107 0 : xIES.set( xI, UNO_QUERY );
108 0 : return xIES.is();
109 : }
110 0 : return sal_False;
111 : }
112 :
113 : Reference < com::sun::star::i18n::XExtendedIndexEntrySupplier > SAL_CALL
114 0 : IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale& rLocale, const OUString& rSortAlgorithm) throw (RuntimeException)
115 : {
116 0 : if (xIES.is() && rSortAlgorithm == aSortAlgorithm && rLocale.Language == aLocale.Language &&
117 0 : rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
118 0 : return xIES;
119 : else {
120 0 : LocaleDataImpl ld;
121 0 : aLocale = rLocale;
122 0 : if (rSortAlgorithm.isEmpty())
123 0 : aSortAlgorithm = ld.getDefaultIndexAlgorithm( rLocale );
124 : else
125 0 : aSortAlgorithm = rSortAlgorithm;
126 :
127 0 : OUString module = ld.getIndexModuleByAlgorithm(rLocale, aSortAlgorithm);
128 0 : if (!module.isEmpty() && createLocaleSpecificIndexEntrySupplier(module))
129 0 : return xIES;
130 :
131 0 : bool bLoaded = false;
132 0 : if (!aSortAlgorithm.isEmpty())
133 : {
134 : // Load service with name <base>_<lang>_<country>_<algorithm>
135 : // or <base>_<bcp47>_<algorithm> and fallbacks.
136 : bLoaded = createLocaleSpecificIndexEntrySupplier(
137 0 : LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_" + aSortAlgorithm);
138 0 : if (!bLoaded)
139 : {
140 0 : ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
141 0 : for (::std::vector< OUString >::const_iterator it( aFallbacks.begin()); it != aFallbacks.end(); ++it)
142 : {
143 0 : bLoaded = createLocaleSpecificIndexEntrySupplier( *it + "_" + aSortAlgorithm);
144 0 : if (bLoaded)
145 0 : break;
146 : }
147 0 : if (!bLoaded)
148 : {
149 : // load service with name <base>_<algorithm>
150 0 : bLoaded = createLocaleSpecificIndexEntrySupplier( aSortAlgorithm);
151 0 : }
152 : }
153 : }
154 0 : if (!bLoaded)
155 : {
156 : // load default service with name <base>_Unicode
157 0 : bLoaded = createLocaleSpecificIndexEntrySupplier( "Unicode");
158 0 : if (!bLoaded)
159 : {
160 0 : throw RuntimeException(); // could not load any service
161 : }
162 : }
163 0 : return xIES;
164 : }
165 : }
166 :
167 0 : OUString SAL_CALL IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages,
168 : const Locale& rLocale ) throw (RuntimeException, std::exception)
169 : {
170 0 : Sequence< OUString > aFollowPageWords = LocaleDataImpl().getFollowPageWords(rLocale);
171 :
172 0 : return (bMorePages && aFollowPageWords.getLength() > 1) ?
173 0 : aFollowPageWords[1] : (aFollowPageWords.getLength() > 0 ?
174 0 : aFollowPageWords[0] : OUString());
175 : }
176 :
177 : #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
178 :
179 : OUString SAL_CALL
180 0 : IndexEntrySupplier::getImplementationName() throw( RuntimeException, std::exception )
181 : {
182 0 : return OUString::createFromAscii( implementationName );
183 : }
184 :
185 : sal_Bool SAL_CALL
186 0 : IndexEntrySupplier::supportsService(const OUString& rServiceName) throw( RuntimeException, std::exception )
187 : {
188 0 : return cppu::supportsService(this, rServiceName);
189 : }
190 :
191 : Sequence< OUString > SAL_CALL
192 0 : IndexEntrySupplier::getSupportedServiceNames() throw( RuntimeException, std::exception )
193 : {
194 0 : Sequence< OUString > aRet(1);
195 0 : aRet[0] = OUString::createFromAscii( implementationName );
196 0 : return aRet;
197 : }
198 :
199 : } } } }
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|