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 :
21 : #include "dp_misc.h"
22 : #include "dp_resource.h"
23 : #include "osl/module.hxx"
24 : #include "osl/mutex.hxx"
25 : #include <tools/resmgr.hxx>
26 : #include "rtl/ustring.h"
27 : #include "cppuhelper/implbase1.hxx"
28 : #include "unotools/configmgr.hxx"
29 :
30 :
31 : using namespace ::com::sun::star;
32 : using namespace ::com::sun::star::uno;
33 : using ::rtl::OUString;
34 :
35 : namespace dp_misc {
36 : namespace {
37 :
38 : struct OfficeLocale :
39 : public rtl::StaticWithInit<OUString, OfficeLocale> {
40 0 : const OUString operator () () {
41 0 : OUString slang(utl::ConfigManager::getLocale());
42 : //fallback, the locale is currently only set when the user starts the
43 : //office for the first time.
44 0 : if (slang.isEmpty())
45 0 : slang = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en-US"));
46 0 : return slang;
47 : }
48 : };
49 :
50 : struct DeploymentResMgr : public rtl::StaticWithInit<
51 : ResMgr *, DeploymentResMgr> {
52 0 : ResMgr * operator () () {
53 0 : return ResMgr::CreateResMgr( "deployment", getOfficeLocale() );
54 : }
55 : };
56 :
57 : class theResourceMutex : public rtl::Static<osl::Mutex, theResourceMutex> {};
58 :
59 : } // anon namespace
60 :
61 : //==============================================================================
62 0 : ResId getResId( sal_uInt16 id )
63 : {
64 0 : const osl::MutexGuard guard( theResourceMutex::get() );
65 0 : return ResId( id, *DeploymentResMgr::get() );
66 : }
67 :
68 : //==============================================================================
69 0 : String getResourceString( sal_uInt16 id )
70 : {
71 0 : const osl::MutexGuard guard( theResourceMutex::get() );
72 0 : String ret( ResId( id, *DeploymentResMgr::get() ) );
73 : ret.SearchAndReplaceAllAscii(
74 0 : "%PRODUCTNAME", utl::ConfigManager::getProductName() );
75 0 : return ret;
76 : }
77 :
78 : //throws an Exception on failure
79 : //primary subtag 2 or three letters(A-Z, a-z), i or x
80 0 : void checkPrimarySubtag(::rtl::OUString const & tag)
81 : {
82 0 : sal_Int32 len = tag.getLength();
83 0 : sal_Unicode const * arLang = tag.getStr();
84 0 : if (len < 1 || len > 3)
85 0 : throw Exception(OUSTR("Invalid language string."), 0);
86 :
87 0 : if (len == 1
88 : && (arLang[0] != 'i' && arLang[0] != 'x'))
89 0 : throw Exception(OUSTR("Invalid language string."), 0);
90 :
91 0 : if (len == 2 || len == 3)
92 : {
93 0 : for (sal_Int32 i = 0; i < len; i++)
94 : {
95 0 : if ( !((arLang[i] >= 'A' && arLang[i] <= 'Z')
96 0 : || (arLang[i] >= 'a' && arLang[i] <= 'z')))
97 : {
98 0 : throw Exception(OUSTR("Invalid language string."), 0);
99 : }
100 : }
101 : }
102 0 : }
103 :
104 : //throws an Exception on failure
105 : //second subtag 2 letter country code or 3-8 letter other code(A-Z, a-z, 0-9)
106 0 : void checkSecondSubtag(::rtl::OUString const & tag, bool & bIsCountry)
107 : {
108 0 : sal_Int32 len = tag.getLength();
109 0 : sal_Unicode const * arLang = tag.getStr();
110 0 : if (len < 2 || len > 8)
111 0 : throw Exception(OUSTR("Invalid language string."), 0);
112 : //country code
113 0 : bIsCountry = false;
114 0 : if (len == 2)
115 : {
116 0 : for (sal_Int32 i = 0; i < 2; i++)
117 : {
118 0 : if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
119 0 : || (arLang[i] >= 'a' && arLang[i] <= 'z')))
120 : {
121 0 : throw Exception(OUSTR("Invalid language string."), 0);
122 : }
123 : }
124 0 : bIsCountry = true;
125 : }
126 :
127 0 : if (len > 2)
128 : {
129 0 : for (sal_Int32 i = 0; i < len; i++)
130 : {
131 0 : if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
132 0 : || (arLang[i] >= 'a' && arLang[i] <= 'z')
133 0 : || (arLang[i] >= '0' && arLang[i] <= '9') ))
134 : {
135 0 : throw Exception(OUSTR("Invalid language string."), 0);
136 : }
137 : }
138 : }
139 0 : }
140 :
141 0 : void checkThirdSubtag(::rtl::OUString const & tag)
142 : {
143 0 : sal_Int32 len = tag.getLength();
144 0 : sal_Unicode const * arLang = tag.getStr();
145 0 : if (len < 1 || len > 8)
146 0 : throw Exception(OUSTR("Invalid language string."), 0);
147 :
148 0 : for (sal_Int32 i = 0; i < len; i++)
149 : {
150 0 : if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
151 0 : || (arLang[i] >= 'a' && arLang[i] <= 'z')
152 0 : || (arLang[i] >= '0' && arLang[i] <= '9') ))
153 : {
154 0 : throw Exception(OUSTR("Invalid language string."), 0);
155 : }
156 : }
157 0 : }
158 :
159 : //=============================================================================
160 :
161 : //We parse the string acording to RFC 3066
162 : //We only use the primary sub-tag and two subtags. That is lang-country-variant
163 : //We do some simple tests if the string is correct. Actually this should do a
164 : //validating parser
165 : //We may have the case that there is no country tag, for example en-welsh
166 0 : ::com::sun::star::lang::Locale toLocale( ::rtl::OUString const & slang )
167 : {
168 0 : OUString _sLang = slang.trim();
169 0 : ::com::sun::star::lang::Locale locale;
170 0 : sal_Int32 nIndex = 0;
171 0 : OUString lang = _sLang.getToken( 0, '-', nIndex );
172 0 : checkPrimarySubtag(lang);
173 0 : locale.Language = lang;
174 0 : OUString country = _sLang.getToken( 0, '-', nIndex );
175 0 : if (!country.isEmpty())
176 : {
177 0 : bool bIsCountry = false;
178 0 : checkSecondSubtag(country, bIsCountry);
179 0 : if (bIsCountry)
180 : {
181 0 : locale.Country = country;
182 : }
183 : else
184 : {
185 0 : locale.Variant = country;
186 : }
187 : }
188 0 : if (locale.Variant.isEmpty())
189 : {
190 0 : OUString variant = _sLang.getToken( 0, '-', nIndex );
191 0 : if (!variant.isEmpty())
192 : {
193 0 : checkThirdSubtag(variant);
194 0 : locale.Variant = variant;
195 0 : }
196 : }
197 :
198 0 : return locale;
199 : }
200 :
201 : //==============================================================================
202 0 : lang::Locale getOfficeLocale()
203 : {
204 0 : return toLocale(OfficeLocale::get());
205 : }
206 :
207 0 : ::rtl::OUString getOfficeLocaleString()
208 : {
209 0 : return OfficeLocale::get();
210 : }
211 :
212 : }
213 :
214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|