1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_i18npool.hxx"
30 
31 #include <indexentrysupplier_common.hxx>
32 #include <com/sun/star/i18n/CollatorOptions.hpp>
33 #include <localedata.hxx>
34 
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star;
37 using namespace ::rtl;
38 
39 namespace com { namespace sun { namespace star { namespace i18n {
40 
41 IndexEntrySupplier_Common::IndexEntrySupplier_Common(const Reference < lang::XMultiServiceFactory >& rxMSF)
42 {
43 	implementationName = "com.sun.star.i18n.IndexEntrySupplier_Common";
44 	collator = new CollatorImpl(rxMSF);
45 	usePhonetic = sal_False;
46 }
47 
48 IndexEntrySupplier_Common::~IndexEntrySupplier_Common()
49 {
50 	delete collator;
51 }
52 
53 Sequence < lang::Locale > SAL_CALL IndexEntrySupplier_Common::getLocaleList() throw (RuntimeException)
54 {
55     throw RuntimeException();
56 }
57 
58 Sequence < OUString > SAL_CALL IndexEntrySupplier_Common::getAlgorithmList( const lang::Locale& ) throw (RuntimeException)
59 {
60     throw RuntimeException();
61 }
62 
63 OUString SAL_CALL IndexEntrySupplier_Common::getPhoneticCandidate( const OUString&,
64 	const lang::Locale& ) throw (RuntimeException)
65 {
66     return OUString();
67 }
68 
69 sal_Bool SAL_CALL IndexEntrySupplier_Common::usePhoneticEntry( const lang::Locale& ) throw (RuntimeException)
70 {
71     throw RuntimeException();
72 }
73 
74 sal_Bool SAL_CALL IndexEntrySupplier_Common::loadAlgorithm( const lang::Locale& rLocale,
75 	const OUString& rAlgorithm, sal_Int32 collatorOptions ) throw (RuntimeException)
76 {
77     usePhonetic = LocaleData().isPhonetic(rLocale, rAlgorithm);
78     collator->loadCollatorAlgorithm(rAlgorithm, rLocale, collatorOptions);
79     aLocale = rLocale;
80     aAlgorithm = rAlgorithm;
81     return sal_True;
82 }
83 
84 OUString SAL_CALL IndexEntrySupplier_Common::getIndexKey( const OUString& rIndexEntry,
85 	const OUString&, const lang::Locale& ) throw (RuntimeException)
86 {
87     sal_Int32 nPos=0;
88     sal_uInt32 indexChar=rIndexEntry.iterateCodePoints(&nPos, 0);
89     return OUString(&indexChar, 1);
90 }
91 
92 sal_Int16 SAL_CALL IndexEntrySupplier_Common::compareIndexEntry(
93 	const OUString& rIndexEntry1, const OUString&, const lang::Locale&,
94 	const OUString& rIndexEntry2, const OUString&, const lang::Locale& )
95 	throw (RuntimeException)
96 {
97     return sal::static_int_cast< sal_Int16 >(
98         collator->compareString(rIndexEntry1, rIndexEntry2));
99         // return value of compareString in { -1, 0, 1 }
100 }
101 
102 OUString SAL_CALL IndexEntrySupplier_Common::getIndexCharacter( const OUString& rIndexEntry,
103 	const lang::Locale& rLocale, const OUString& ) throw (RuntimeException)
104 {
105     return getIndexKey(rIndexEntry, rIndexEntry, rLocale);
106 }
107 
108 OUString SAL_CALL IndexEntrySupplier_Common::getIndexFollowPageWord( sal_Bool,
109 	const lang::Locale& ) throw (RuntimeException)
110 {
111     throw RuntimeException();
112 }
113 
114 const OUString& SAL_CALL
115 IndexEntrySupplier_Common::getEntry( const OUString& IndexEntry,
116 	const OUString& PhoneticEntry, const lang::Locale& rLocale ) throw (RuntimeException)
117 {
118 	// The condition for using phonetic entry is:
119 	// usePhonetic is set for the algorithm;
120 	// rLocale for phonetic entry is same as aLocale for algorithm,
121 	// which means Chinese phonetic will not be used for Japanese algorithm;
122 	// phonetic entry is not blank.
123 	if (usePhonetic && PhoneticEntry.getLength() > 0 && rLocale.Language == aLocale.Language &&
124             rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
125 	    return PhoneticEntry;
126 	else
127 	    return IndexEntry;
128 }
129 
130 OUString SAL_CALL
131 IndexEntrySupplier_Common::getImplementationName() throw( RuntimeException )
132 {
133     return OUString::createFromAscii( implementationName );
134 }
135 
136 sal_Bool SAL_CALL
137 IndexEntrySupplier_Common::supportsService(const OUString& rServiceName) throw( RuntimeException )
138 {
139     return rServiceName.compareToAscii(implementationName) == 0;
140 }
141 
142 Sequence< OUString > SAL_CALL
143 IndexEntrySupplier_Common::getSupportedServiceNames() throw( RuntimeException )
144 {
145     Sequence< OUString > aRet(1);
146     aRet[0] = OUString::createFromAscii( implementationName );
147     return aRet;
148 }
149 
150 } } } }
151