| 1 |
/* AbiSource Application Framework |
| 2 |
* Copyright (C) 1998-2000 AbiSource, Inc. |
| 3 |
* BIDI Copyright (C) 2001,2002 Tomas Frydrych |
| 4 |
* Copyright (C) 2002 Dom Lachowicz |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
* 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <stdlib.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "ut_assert.h" |
| 27 |
#include "ut_debugmsg.h" |
| 28 |
#include "ut_string.h" |
| 29 |
#include "ut_growbuf.h" |
| 30 |
#include "ut_bytebuf.h" |
| 31 |
#include "ut_wctomb.h" |
| 32 |
#include "ut_iconv.h" |
| 33 |
#include "ut_Language.h" |
| 34 |
|
| 35 |
#include "xap_App.h" |
| 36 |
#include "xap_Strings.h" |
| 37 |
#include "xap_EncodingManager.h" |
| 38 |
|
| 39 |
////////////////////////////////////////////////////////////////// |
| 40 |
// base class provides interface regardless of how we got the strings |
| 41 |
////////////////////////////////////////////////////////////////// |
| 42 |
|
| 43 |
XAP_StringSet::XAP_StringSet(XAP_App * pApp, const gchar * szLanguageName) |
| 44 |
: m_encoding("UTF-8") |
| 45 |
{ |
| 46 |
m_pApp = pApp; |
| 47 |
|
| 48 |
m_szLanguageName = NULL; |
| 49 |
if (szLanguageName && *szLanguageName) |
| 50 |
m_szLanguageName = g_strdup(szLanguageName); |
| 51 |
} |
| 52 |
|
| 53 |
XAP_StringSet::~XAP_StringSet(void) |
| 54 |
{ |
| 55 |
if (m_szLanguageName) |
| 56 |
g_free(const_cast<gchar *>(m_szLanguageName)); |
| 57 |
} |
| 58 |
|
| 59 |
const gchar * XAP_StringSet::getLanguageName(void) const |
| 60 |
{ |
| 61 |
return m_szLanguageName; |
| 62 |
} |
| 63 |
|
| 64 |
bool XAP_StringSet::getValue(XAP_String_Id id, const char * inEncoding, std::string &s) const |
| 65 |
{ |
| 66 |
const char * toTranslate = getValue(id); |
| 67 |
|
| 68 |
UT_return_val_if_fail(toTranslate != NULL, false); |
| 69 |
|
| 70 |
if(!strcmp(m_encoding.c_str(),inEncoding)) |
| 71 |
{ |
| 72 |
s = toTranslate; |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
UT_iconv_t conv = UT_iconv_open(inEncoding, m_encoding.c_str()); |
| 77 |
UT_return_val_if_fail(UT_iconv_isValid(conv), false); |
| 78 |
|
| 79 |
char * translated = UT_convert_cd(toTranslate, strlen (toTranslate)+1, conv, NULL, NULL); |
| 80 |
|
| 81 |
UT_iconv_close(conv); |
| 82 |
|
| 83 |
UT_return_val_if_fail(translated, false); |
| 84 |
s = translated; |
| 85 |
|
| 86 |
g_free(translated); |
| 87 |
} |
| 88 |
|
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
bool XAP_StringSet::getValueUTF8(XAP_String_Id id, std::string & s) const |
| 93 |
{ |
| 94 |
return getValue(id, "UTF-8", s); |
| 95 |
} |
| 96 |
|
| 97 |
void XAP_StringSet::setEncoding(const gchar * inEncoding) |
| 98 |
{ |
| 99 |
UT_return_if_fail(inEncoding != 0); |
| 100 |
m_encoding = inEncoding; |
| 101 |
} |
| 102 |
|
| 103 |
const char * XAP_StringSet::getEncoding() const |
| 104 |
{ |
| 105 |
return m_encoding.c_str(); |
| 106 |
} |
| 107 |
|
| 108 |
////////////////////////////////////////////////////////////////// |
| 109 |
// a sub-class to wrap the compiled-in (english) strings |
| 110 |
// (there will only be one instance of this sub-class) |
| 111 |
// (since these strings are English, we will not bother with any |
| 112 |
// (bidi processing, we only need this in the disk stringset) |
| 113 |
////////////////////////////////////////////////////////////////// |
| 114 |
|
| 115 |
XAP_BuiltinStringSet::XAP_BuiltinStringSet(XAP_App * pApp, const gchar * szLanguageName) |
| 116 |
: XAP_StringSet(pApp,szLanguageName) |
| 117 |
{ |
| 118 |
#define dcl(id,s) static_cast<const gchar *>(s), |
| 119 |
|
| 120 |
static const gchar * s_a[] = |
| 121 |
{ |
| 122 |
dcl(__FIRST__,0) // bogus entry for zero |
| 123 |
#include "xap_String_Id.h" |
| 124 |
dcl(__LAST__,0) // bogus entry for end |
| 125 |
}; |
| 126 |
|
| 127 |
m_arrayXAP = s_a; |
| 128 |
|
| 129 |
#undef dcl |
| 130 |
} |
| 131 |
|
| 132 |
XAP_BuiltinStringSet::~XAP_BuiltinStringSet(void) |
| 133 |
{ |
| 134 |
} |
| 135 |
|
| 136 |
const gchar * XAP_BuiltinStringSet::getValue(XAP_String_Id id) const |
| 137 |
{ |
| 138 |
if ( (id > XAP_STRING_ID__FIRST__) && (id < XAP_STRING_ID__LAST__) ) |
| 139 |
return m_arrayXAP[id]; |
| 140 |
|
| 141 |
return NULL; |
| 142 |
} |
| 143 |
|
| 144 |
////////////////////////////////////////////////////////////////// |
| 145 |
// a sub-class to deal with disk-based string sets (translations) |
| 146 |
// (a unique one of these will be instantiated for each language |
| 147 |
// that we load -- or rather one for each time the user switches |
| 148 |
// languages and we load another one from disk) |
| 149 |
////////////////////////////////////////////////////////////////// |
| 150 |
|
| 151 |
XAP_DiskStringSet::XAP_DiskStringSet(XAP_App * pApp) |
| 152 |
: XAP_StringSet(pApp,NULL), |
| 153 |
m_vecStringsXAP(XAP_STRING_ID__LAST__ - XAP_STRING_ID__FIRST__ + 1, 4, true) |
| 154 |
{ |
| 155 |
m_pFallbackStringSet = NULL; |
| 156 |
|
| 157 |
XAP_DiskStringSet::setValue(XAP_STRING_ID__FIRST__,0); // bogus zero element |
| 158 |
} |
| 159 |
|
| 160 |
XAP_DiskStringSet::~XAP_DiskStringSet(void) |
| 161 |
{ |
| 162 |
UT_sint32 kLimit = m_vecStringsXAP.getItemCount(); |
| 163 |
UT_sint32 k; |
| 164 |
|
| 165 |
for (k=kLimit-1; k>=0; k--) |
| 166 |
{ |
| 167 |
gchar * sz = m_vecStringsXAP.getNthItem(k); |
| 168 |
if (sz) |
| 169 |
g_free(sz); |
| 170 |
} |
| 171 |
|
| 172 |
// we didn't create the fallback set, but we inherit ownership of it. |
| 173 |
DELETEP(m_pFallbackStringSet); |
| 174 |
} |
| 175 |
|
| 176 |
bool XAP_DiskStringSet::setLanguage(const gchar * szLanguageName) |
| 177 |
{ |
| 178 |
if (m_szLanguageName) |
| 179 |
g_free(const_cast<gchar *>(m_szLanguageName)); |
| 180 |
m_szLanguageName = NULL; |
| 181 |
if (szLanguageName && *szLanguageName) |
| 182 |
m_szLanguageName = g_strdup(szLanguageName); |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
void XAP_DiskStringSet::setFallbackStringSet(XAP_StringSet * pFallback) |
| 187 |
{ |
| 188 |
m_pFallbackStringSet = pFallback; |
| 189 |
} |
| 190 |
|
| 191 |
bool XAP_DiskStringSet::setValue(XAP_String_Id id, const gchar * szString) |
| 192 |
{ |
| 193 |
bool bFoundMultiByte = false; |
| 194 |
gchar * szDup = NULL; |
| 195 |
int length; |
| 196 |
const void* ptr; |
| 197 |
|
| 198 |
if (szString && *szString) |
| 199 |
{ |
| 200 |
UT_GrowBuf gb; |
| 201 |
UT_decodeUTF8string(szString,strlen(szString),&gb); |
| 202 |
|
| 203 |
// TODO The strings that we use (for dialogs and etc) are currently |
| 204 |
// TODO limited to single-byte encodings by the code below. |
| 205 |
|
| 206 |
int kLimit=gb.getLength(); |
| 207 |
UT_UCS4Char * p = reinterpret_cast<UT_UCS4Char*>(gb.getPointer(0)); |
| 208 |
UT_ByteBuf str; |
| 209 |
|
| 210 |
// now we run this string through fribidi |
| 211 |
if(XAP_App::getApp()->theOSHasBidiSupport() == XAP_App::BIDI_SUPPORT_NONE) |
| 212 |
{ |
| 213 |
if (p && *p) |
| 214 |
{ |
| 215 |
UT_UCS4Char * fbdStr2 = new UT_UCS4Char [kLimit + 1]; |
| 216 |
UT_ASSERT(fbdStr2); |
| 217 |
|
| 218 |
UT_sint32 i; |
| 219 |
|
| 220 |
// Testing the first char is not good enough; we really need to get this from the |
| 221 |
// language |
| 222 |
// FriBidiCharType fbdDomDir = fribidi_get_type(fbdStr[0]); |
| 223 |
UT_BidiCharType iDomDir = UT_BIDI_LTR; |
| 224 |
UT_Language l; |
| 225 |
if(UTLANG_RTL == l.getDirFromCode(getLanguageName())) |
| 226 |
iDomDir = UT_BIDI_RTL; |
| 227 |
|
| 228 |
UT_bidiReorderString(p, kLimit, iDomDir, fbdStr2); |
| 229 |
|
| 230 |
for(i = 0; i < kLimit; i++) |
| 231 |
{ |
| 232 |
p[i] = fbdStr2[i]; |
| 233 |
} |
| 234 |
|
| 235 |
UT_ASSERT_HARMLESS(p[i] == 0); |
| 236 |
delete[] fbdStr2; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if(strcmp(getEncoding(), "UTF-8")) |
| 241 |
{ |
| 242 |
UT_Wctomb wctomb_conv(getEncoding()); |
| 243 |
|
| 244 |
char letter_buf[20]; |
| 245 |
for (int k=0; k<kLimit; k++) |
| 246 |
{ |
| 247 |
if (wctomb_conv.wctomb(letter_buf,length, p[k])) { |
| 248 |
str.append(reinterpret_cast<const UT_Byte*>(&letter_buf[0]),length); |
| 249 |
}; |
| 250 |
} |
| 251 |
length = str.getLength(); |
| 252 |
ptr = str.getPointer(0); |
| 253 |
} else |
| 254 |
{ |
| 255 |
length = strlen(szString); |
| 256 |
ptr = szString; |
| 257 |
} |
| 258 |
szDup = static_cast<gchar *>(g_try_malloc(length+1)); |
| 259 |
if (!szDup) |
| 260 |
return false; |
| 261 |
memcpy(szDup,ptr,length); |
| 262 |
szDup[length]='\0'; |
| 263 |
} |
| 264 |
|
| 265 |
gchar* pOldValue = NULL; |
| 266 |
bool bResult = (m_vecStringsXAP.setNthItem(id,szDup,&pOldValue) == 0); |
| 267 |
UT_ASSERT(pOldValue == NULL); // duplicate string for this id |
| 268 |
|
| 269 |
if (bFoundMultiByte) |
| 270 |
{ |
| 271 |
UT_DEBUGMSG(("WARNING: DiskStringSet: Found Multi-Byte char in String [%s][id %d] (we mapped it to [%s])\n",szString,id,szDup)); |
| 272 |
} |
| 273 |
|
| 274 |
return bResult; |
| 275 |
} |
| 276 |
|
| 277 |
const gchar * XAP_DiskStringSet::getValue(XAP_String_Id id) const |
| 278 |
{ |
| 279 |
UT_uint32 kLimit = m_vecStringsXAP.getItemCount(); |
| 280 |
|
| 281 |
if (id < kLimit) |
| 282 |
{ |
| 283 |
const gchar * szValue = m_vecStringsXAP.getNthItem(id); |
| 284 |
if (szValue) |
| 285 |
return szValue; |
| 286 |
} |
| 287 |
|
| 288 |
if (m_pFallbackStringSet) |
| 289 |
return m_pFallbackStringSet->getValue(id); |
| 290 |
|
| 291 |
return NULL; |
| 292 |
} |
| 293 |
|
| 294 |
////////////////////////////////////////////////////////////////// |
| 295 |
// build a static table to map id by names into numbers |
| 296 |
////////////////////////////////////////////////////////////////// |
| 297 |
|
| 298 |
#define dcl(id,s) { (const gchar *) #id, XAP_STRING_ID_##id }, |
| 299 |
|
| 300 |
static struct { const gchar * szName; XAP_String_Id id; } s_map[] = |
| 301 |
{ |
| 302 |
#include "xap_String_Id.h" |
| 303 |
}; |
| 304 |
|
| 305 |
#undef dcl |
| 306 |
|
| 307 |
////////////////////////////////////////////////////////////////// |
| 308 |
|
| 309 |
bool XAP_DiskStringSet::setValue(const gchar * szId, const gchar * szString) |
| 310 |
{ |
| 311 |
if (!szId || !*szId || !szString || !*szString) |
| 312 |
return true; |
| 313 |
|
| 314 |
|
| 315 |
gchar const* id; |
| 316 |
|
| 317 |
// Build a hash table the first time that the function is called |
| 318 |
if (m_hash.size() == 0) { |
| 319 |
UT_uint32 k, kLimit = G_N_ELEMENTS(s_map); |
| 320 |
|
| 321 |
for (k=0; k<kLimit; k++) { |
| 322 |
id = g_ascii_strdown (s_map[k].szName, -1); |
| 323 |
m_hash[ std::string(id) ] = k + 1; |
| 324 |
FREEP(id); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
id = g_ascii_strdown (szId, -1); |
| 329 |
std::map<std::string, UT_uint32>::iterator iter = m_hash.find( id ); |
| 330 |
FREEP(id); |
| 331 |
|
| 332 |
if (iter != m_hash.end()) |
| 333 |
return setValue(s_map[iter->second - 1].id, szString); |
| 334 |
|
| 335 |
// TODO should we promote this message to a message box ?? |
| 336 |
UT_DEBUGMSG(("Unknown ID in string file [%s=\"%s\"]\n",szId,szString)); |
| 337 |
return false; |
| 338 |
} |
| 339 |
|
| 340 |
/*****************************************************************/ |
| 341 |
|
| 342 |
void XAP_DiskStringSet::startElement(const gchar *name, const gchar **atts) |
| 343 |
{ |
| 344 |
if (!m_parserState.m_parserStatus) // eat if already had an error |
| 345 |
return; |
| 346 |
|
| 347 |
if (strcmp(name, "AbiStrings") == 0) |
| 348 |
{ |
| 349 |
// we expect something of the form: |
| 350 |
// <AbiStrings ver="1.0" language="en-US">...</AbiStrings> |
| 351 |
|
| 352 |
const gchar ** a = atts; |
| 353 |
while (*a) |
| 354 |
{ |
| 355 |
UT_ASSERT(a[1] && *a[1]); // require a value for each attribute keyword |
| 356 |
|
| 357 |
if (strcmp(a[0], "ver") == 0) |
| 358 |
{ |
| 359 |
// TODO test version number |
| 360 |
} |
| 361 |
else if (strcmp(a[0], "language") == 0) |
| 362 |
{ |
| 363 |
UT_DEBUGMSG(("Found strings for language [%s].\n",a[1])); |
| 364 |
if (!setLanguage(a[1])) |
| 365 |
goto MemoryError; |
| 366 |
} |
| 367 |
|
| 368 |
a += 2; |
| 369 |
} |
| 370 |
} |
| 371 |
else if (strcmp(name, "Strings") == 0) |
| 372 |
{ |
| 373 |
// we found a set of strings. we expect something of the form: |
| 374 |
// <Strings class="type" n0="v0" n1="v1" ... /> |
| 375 |
// where the [nk,vk] are arbitrary name/value pairs that mean |
| 376 |
// something to the application. each of the nk's corresponds |
| 377 |
// to an XAP_STRING_ID_nk or AP_STRING_ID_nk. |
| 378 |
// |
| 379 |
// class="type" is a way of indicating whether the string is an |
| 380 |
// XAP or AP string. this information is strictly for the |
| 381 |
// convenience of translators -- we don't care. on input, we |
| 382 |
// allow there to be as many <Strings.../> as the translator |
| 383 |
// wants. hopefully, this will make it easier to deal with |
| 384 |
// multiple applications and to deal with multiple language |
| 385 |
// dialects. |
| 386 |
// |
| 387 |
// if there are duplicated name/value pairs our behavior is |
| 388 |
// undefined -- we remember the last one that the XML parser |
| 389 |
// give us. |
| 390 |
|
| 391 |
const gchar ** a; |
| 392 |
for (a = atts; (*a); a += 2) |
| 393 |
{ |
| 394 |
if (strcmp(a[0], "class") == 0) |
| 395 |
continue; |
| 396 |
|
| 397 |
if (!setValue(a[0], a[1])) |
| 398 |
{ |
| 399 |
UT_DEBUGMSG(("UNKNOWN StringId [%s] value [%s]\n", a[0], a[1])); |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
// successful parse of tag... |
| 405 |
|
| 406 |
return; // success |
| 407 |
|
| 408 |
MemoryError: |
| 409 |
UT_DEBUGMSG(("Memory error parsing strings file.\n")); |
| 410 |
m_parserState.m_parserStatus = false; // cause parser driver to bail |
| 411 |
return; |
| 412 |
} |
| 413 |
|
| 414 |
void XAP_DiskStringSet::endElement(const gchar * /* name */) |
| 415 |
{ |
| 416 |
// everything in this file is contained in start-tags |
| 417 |
return; |
| 418 |
} |
| 419 |
|
| 420 |
void XAP_DiskStringSet::charData(const gchar * /* s */, int /* len */) |
| 421 |
{ |
| 422 |
// everything in this file is contained in start-tags |
| 423 |
return; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
bool XAP_DiskStringSet::loadStringsFromDisk(const char * szFilename) |
| 428 |
{ |
| 429 |
bool bResult = false; // assume failure |
| 430 |
|
| 431 |
m_parserState.m_parserStatus = true; |
| 432 |
|
| 433 |
UT_XML parser; |
| 434 |
|
| 435 |
if (!szFilename || !*szFilename) |
| 436 |
{ |
| 437 |
UT_DEBUGMSG(("Invalid pathname for Strings file.\n")); |
| 438 |
goto Cleanup; |
| 439 |
} |
| 440 |
|
| 441 |
parser.setListener (this); |
| 442 |
if ((parser.parse (szFilename) != UT_OK) || (!m_parserState.m_parserStatus)) |
| 443 |
{ |
| 444 |
UT_DEBUGMSG(("Problem reading document\n")); |
| 445 |
goto Cleanup; |
| 446 |
} |
| 447 |
|
| 448 |
bResult = true; |
| 449 |
|
| 450 |
Cleanup: |
| 451 |
|
| 452 |
return bResult; |
| 453 |
} |