merge the formfield patch from ooo-build
[ooovba.git] / configmgr / source / inc / matchlocale.hxx
blobc6b87c5d3f5af261d410b9bc56ba5d143123591a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: matchlocale.hxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 /* PLEASE DON'T DELETE ANY COMMENT LINES, ALSO IT'S UNNECESSARY. */
33 #ifndef CONFIGMGR_MATCHLOCALE_HXX
34 #define CONFIGMGR_MATCHLOCALE_HXX
36 #include <com/sun/star/uno/Sequence.hxx>
37 #include <com/sun/star/lang/Locale.hpp>
39 #include <rtl/ustring.hxx>
40 #include <vector>
42 namespace configmgr
44 // -----------------------------------------------------------------------------
46 namespace localehelper
48 // -------------------------------------------------------------------------
49 namespace uno = ::com::sun::star::uno;
50 namespace lang = ::com::sun::star::lang;
52 // -------------------------------------------------------------------------
53 extern char const * const c_sAnyLanguage;
54 extern char const * const c_sDefLanguage;
56 extern bool isAnyLanguage(rtl::OUString const & _sLanguage);
57 extern bool isDefaultLanguage(rtl::OUString const & _sLanguage);
59 extern rtl::OUString getAnyLanguage();
60 extern rtl::OUString getDefaultLanguage();
62 extern com::sun::star::lang::Locale getAnyLocale();
63 extern com::sun::star::lang::Locale getDefaultLocale();
65 // -------------------------------------------------------------------------
66 // the max value of std::vector< com::sun::star::lang::Locale >::size_type - marks out-of-range (Value == -1 == ~0)
67 std::vector< com::sun::star::lang::Locale >::size_type const c_noPosition = std::vector< com::sun::star::lang::Locale >::size_type(0)-std::vector< com::sun::star::lang::Locale >::size_type(1);
69 // conversion helpers
70 com::sun::star::lang::Locale makeLocale(rtl::OUString const& sLocaleName_);
71 rtl::OUString makeIsoLocale(com::sun::star::lang::Locale const& aUnoLocale_);
73 std::vector< com::sun::star::lang::Locale > makeLocaleSequence(uno::Sequence<rtl::OUString> const& sLocaleNames_);
74 uno::Sequence<rtl::OUString> makeIsoSequence(std::vector< com::sun::star::lang::Locale > const& aLocales_);
76 inline
77 bool equalLocale(com::sun::star::lang::Locale const & lhs, com::sun::star::lang::Locale const & rhs)
78 { return lhs.Language == rhs.Language && lhs.Country == rhs.Country; }
80 inline
81 bool equalLanguage(com::sun::star::lang::Locale const & lhs, com::sun::star::lang::Locale const & rhs)
82 { return lhs.Language == rhs.Language; }
83 // -------------------------------------------------------------------------
84 bool designatesAllLocales(com::sun::star::lang::Locale const& aLocale_);
85 bool designatesAllLocales(std::vector< com::sun::star::lang::Locale > const& aLocales_);
86 // -------------------------------------------------------------------------
88 /// result of matching a locale against a target locale
89 enum MatchQuality
91 MISMATCH = 0, /// match: locales do not match (must be zero!)
92 MATCH_LANGUAGE, /// match: languages match - country mismatch
93 MATCH_LANGUAGE_PLAIN, /// match: languages match - no country to match
94 MATCH_LOCALE, /// match: full match
95 BEST_MATCH = MATCH_LOCALE
98 /// compare two locales for 'nearness'
99 MatchQuality match(com::sun::star::lang::Locale const& aLocale_, com::sun::star::lang::Locale const& aTarget_);
102 // -------------------------------------------------------------------------
103 /// result of matching a Locale against a target sequence of locales
104 class MatchResult
106 std::vector< com::sun::star::lang::Locale >::size_type m_nPos;
107 MatchQuality m_eQuality;
109 public:
110 /// construct a default (no match) result
111 MatchResult()
112 { reset(); }
114 /// construct a result from given parameters - use with care
115 MatchResult(std::vector< com::sun::star::lang::Locale >::size_type nPos_, MatchQuality eQuality_)
116 : m_nPos( nPos_ )
117 , m_eQuality(eQuality_)
120 /// construct an optimum result
121 static MatchResult best() { return MatchResult(0,MATCH_LOCALE); }
123 /// has there been a match
124 bool isMatch() const { return m_eQuality != MISMATCH; }
125 /// is this the best match possible ?
126 bool isBest() const { return m_nPos == 0 && m_eQuality == MATCH_LOCALE; }
128 /// retrieve the position that was matched
129 std::vector< com::sun::star::lang::Locale >::size_type position() const { return m_nPos; }
130 /// retrieve the quality of match
131 MatchQuality quality() const { return m_eQuality; }
133 /// assign the given position and quality, if they are an improvement
134 bool improve(std::vector< com::sun::star::lang::Locale >::size_type nPos, MatchQuality eQuality_);
136 /// reset to no match or best match state
137 void reset()
139 m_nPos = c_noPosition;
140 m_eQuality = MISMATCH;
143 // ---------------------------------------------------------------------
144 // comparing MatchResults
145 friend bool operator ==(MatchResult const& lhs, MatchResult const& rhs)
147 return lhs.m_nPos == rhs.m_nPos &&
148 lhs.m_eQuality == rhs.m_eQuality;
151 // ordering of MatchResults - greater is better
152 friend bool operator < (MatchResult const& lhs, MatchResult const& rhs)
154 if (lhs.m_nPos > rhs.m_nPos) return true; // greater position is worse
155 if (lhs.m_nPos < rhs.m_nPos) return false;
157 return (lhs.m_eQuality < rhs.m_eQuality); // least Quality is worse
162 // ---------------------------------------------------------------------
163 // derived relational operators
164 inline bool operator !=(MatchResult const& lhs, MatchResult const& rhs)
165 { return !(lhs == rhs); }
166 inline bool operator > (MatchResult const& lhs, MatchResult const& rhs)
167 { return rhs < lhs; }
168 inline bool operator <= (MatchResult const& lhs, MatchResult const& rhs)
169 { return !(rhs < lhs); }
170 inline bool operator >=(MatchResult const& lhs, MatchResult const& rhs)
171 { return !(lhs < rhs); }
173 /// improve an existing match of a locale against a sequence of locales
174 bool improveMatch(MatchResult& rMatch_, com::sun::star::lang::Locale const& aLocale_, std::vector< com::sun::star::lang::Locale > const& aTarget_);
176 /// match a locale against a sequence of locales for a given quality level
177 bool isMatch(com::sun::star::lang::Locale const& aLocales, std::vector< com::sun::star::lang::Locale > const& aTarget_, MatchQuality eRequiredQuality_);
179 // -------------------------------------------------------------------------
180 /// add defaults to a sequence of locales
181 void addFallbackLocales(std::vector< com::sun::star::lang::Locale >& aTargetList_);
183 // -------------------------------------------------------------------------
184 class FindBestLocale
186 public:
187 /// construct a MatchLocale with a single target locale
188 FindBestLocale(com::sun::star::lang::Locale const& aTarget_);
190 /// is there any match ?
191 bool isMatch() const { return m_aResult.isMatch(); }
193 /// is there an optimum match (so we are done) ?
194 bool isBestMatch() const { return m_aResult.isBest(); }
196 /// get the quality of the best match found
197 MatchQuality getMatchQuality() const { return m_aResult.quality(); }
199 /// check, if the given locale improves the quality. if it does, accept it
200 bool accept(com::sun::star::lang::Locale const& aLocale_);
202 /// reset the match result, indicating whether a match is needed at all
203 void reset(bool bNeedLocale_ = true);
205 private:
206 void implSetTarget(std::vector< com::sun::star::lang::Locale > const& aTarget_);
208 std::vector< com::sun::star::lang::Locale > m_aTarget;
209 MatchResult m_aResult;
212 } // namespace
213 // -----------------------------------------------------------------------------
215 } // namespace
217 #endif