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