Update ooo320-m1
[ooovba.git] / desktop / source / deployment / misc / dp_resource.cxx
blob54aff70ee14ceb741dc99040b280b67f3006fc8a
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: dp_resource.cxx,v $
10 * $Revision: 1.19 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
34 #include "dp_misc.h"
35 #include "dp_resource.h"
36 #include "osl/module.hxx"
37 #include "osl/mutex.hxx"
38 #include "rtl/ustring.h"
39 #include "cppuhelper/implbase1.hxx"
40 #include "unotools/configmgr.hxx"
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
45 using ::rtl::OUString;
47 namespace dp_misc {
48 namespace {
50 struct OfficeLocale :
51 public rtl::StaticWithInit<const OUString, OfficeLocale> {
52 const OUString operator () () {
53 OUString slang;
54 if (! (::utl::ConfigManager::GetDirectConfigProperty(
55 ::utl::ConfigManager::LOCALE ) >>= slang))
56 throw RuntimeException( OUSTR("Cannot determine language!"), 0 );
57 //fallback, the locale is currently only set when the user starts the
58 //office for the first time.
59 if (slang.getLength() == 0)
60 slang = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en-US"));
61 return slang;
65 struct DeploymentResMgr : public rtl::StaticWithInit<
66 ResMgr *, DeploymentResMgr> {
67 ResMgr * operator () () {
68 return ResMgr::CreateResMgr( "deployment", getOfficeLocale() );
72 osl::Mutex s_mutex;
74 } // anon namespace
76 //==============================================================================
77 ResId getResId( USHORT id )
79 const osl::MutexGuard guard( s_mutex );
80 return ResId( id, *DeploymentResMgr::get() );
83 //==============================================================================
84 String getResourceString( USHORT id )
86 const osl::MutexGuard guard( s_mutex );
87 String ret( ResId( id, *DeploymentResMgr::get() ) );
88 if (ret.SearchAscii( "%PRODUCTNAME" ) != STRING_NOTFOUND) {
89 static String s_brandName;
90 if (s_brandName.Len() == 0) {
91 OUString brandName(
92 ::utl::ConfigManager::GetDirectConfigProperty(
93 ::utl::ConfigManager::PRODUCTNAME ).get<OUString>() );
94 s_brandName = brandName;
96 ret.SearchAndReplaceAllAscii( "%PRODUCTNAME", s_brandName );
98 return ret;
101 //throws an Exception on failure
102 //primary subtag 2 or three letters(A-Z, a-z), i or x
103 void checkPrimarySubtag(::rtl::OUString const & tag)
105 sal_Int32 len = tag.getLength();
106 sal_Unicode const * arLang = tag.getStr();
107 if (len < 1 || len > 3)
108 throw Exception(OUSTR("Invalid language string."), 0);
110 if (len == 1
111 && (arLang[0] != 'i' && arLang[0] != 'x'))
112 throw Exception(OUSTR("Invalid language string."), 0);
114 if (len == 2 || len == 3)
116 for (sal_Int32 i = 0; i < len; i++)
118 if ( !((arLang[i] >= 'A' && arLang[i] <= 'Z')
119 || (arLang[i] >= 'a' && arLang[i] <= 'z')))
121 throw Exception(OUSTR("Invalid language string."), 0);
127 //throws an Exception on failure
128 //second subtag 2 letter country code or 3-8 letter other code(A-Z, a-z, 0-9)
129 void checkSecondSubtag(::rtl::OUString const & tag, bool & bIsCountry)
131 sal_Int32 len = tag.getLength();
132 sal_Unicode const * arLang = tag.getStr();
133 if (len < 2 || len > 8)
134 throw Exception(OUSTR("Invalid language string."), 0);
135 //country code
136 bIsCountry = false;
137 if (len == 2)
139 for (sal_Int32 i = 0; i < 2; i++)
141 if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
142 || (arLang[i] >= 'a' && arLang[i] <= 'z')))
144 throw Exception(OUSTR("Invalid language string."), 0);
147 bIsCountry = true;
150 if (len > 2)
152 for (sal_Int32 i = 0; i < len; i++)
154 if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
155 || (arLang[i] >= 'a' && arLang[i] <= 'z')
156 || (arLang[i] >= '0' && arLang[i] <= '9') ))
158 throw Exception(OUSTR("Invalid language string."), 0);
164 void checkThirdSubtag(::rtl::OUString const & tag)
166 sal_Int32 len = tag.getLength();
167 sal_Unicode const * arLang = tag.getStr();
168 if (len < 1 || len > 8)
169 throw Exception(OUSTR("Invalid language string."), 0);
171 for (sal_Int32 i = 0; i < len; i++)
173 if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
174 || (arLang[i] >= 'a' && arLang[i] <= 'z')
175 || (arLang[i] >= '0' && arLang[i] <= '9') ))
177 throw Exception(OUSTR("Invalid language string."), 0);
182 //=============================================================================
184 //We parse the string acording to RFC 3066
185 //We only use the primary sub-tag and two subtags. That is lang-country-variant
186 //We do some simple tests if the string is correct. Actually this should do a
187 //validating parser
188 //We may have the case that there is no country tag, for example en-welsh
189 ::com::sun::star::lang::Locale toLocale( ::rtl::OUString const & slang )
191 OUString _sLang = slang.trim();
192 ::com::sun::star::lang::Locale locale;
193 sal_Int32 nIndex = 0;
194 OUString lang = _sLang.getToken( 0, '-', nIndex );
195 checkPrimarySubtag(lang);
196 locale.Language = lang;
197 OUString country = _sLang.getToken( 0, '-', nIndex );
198 if (country.getLength() > 0)
200 bool bIsCountry = false;
201 checkSecondSubtag(country, bIsCountry);
202 if (bIsCountry)
204 locale.Country = country;
206 else
208 locale.Variant = country;
211 if (locale.Variant.getLength() == 0)
213 OUString variant = _sLang.getToken( 0, '-', nIndex );
214 if (variant.getLength() > 0)
216 checkThirdSubtag(variant);
217 locale.Variant = variant;
221 return locale;
224 //==============================================================================
225 lang::Locale getOfficeLocale()
227 return toLocale(OfficeLocale::get());
230 ::rtl::OUString getOfficeLocaleString()
232 return OfficeLocale::get();