1 /* 2 * Copyright 2003-2012, Haiku. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Axel Dörfler, axeld@pinc-software.de 7 * Oliver Tappe, zooey@hirschkaefer.de 8 */ 9 10 11 #include <unicode/uversion.h> 12 #include <LocaleRoster.h> 13 14 #include <assert.h> 15 #include <ctype.h> 16 17 #include <new> 18 19 #include <Autolock.h> 20 #include <Bitmap.h> 21 #include <Catalog.h> 22 #include <Entry.h> 23 #include <FormattingConventions.h> 24 #include <fs_attr.h> 25 #include <IconUtils.h> 26 #include <Language.h> 27 #include <Locale.h> 28 #include <LocaleRosterData.h> 29 #include <MutableLocaleRoster.h> 30 #include <Node.h> 31 #include <Roster.h> 32 #include <String.h> 33 #include <TimeZone.h> 34 35 #include <ICUWrapper.h> 36 #include <locks.h> 37 38 // ICU includes 39 #include <unicode/locdspnm.h> 40 #include <unicode/locid.h> 41 #include <unicode/timezone.h> 42 43 44 using BPrivate::CatalogAddOnInfo; 45 using BPrivate::MutableLocaleRoster; 46 U_NAMESPACE_USE 47 48 49 /* 50 * several attributes/resource-IDs used within the Locale Kit: 51 */ 52 const char* BLocaleRoster::kCatLangAttr = "BEOS:LOCALE_LANGUAGE"; 53 // name of catalog language, lives in every catalog file 54 const char* BLocaleRoster::kCatSigAttr = "BEOS:LOCALE_SIGNATURE"; 55 // catalog signature, lives in every catalog file 56 const char* BLocaleRoster::kCatFingerprintAttr = "BEOS:LOCALE_FINGERPRINT"; 57 // catalog fingerprint, may live in catalog file 58 59 const char* BLocaleRoster::kEmbeddedCatAttr = "BEOS:LOCALE_EMBEDDED_CATALOG"; 60 // attribute which contains flattened data of embedded catalog 61 // this may live in an app- or add-on-file 62 int32 BLocaleRoster::kEmbeddedCatResId = 0xCADA; 63 // a unique value used to identify the resource (=> embedded CAtalog DAta) 64 // which contains flattened data of embedded catalog. 65 // this may live in an app- or add-on-file 66 67 68 static const char* 69 country_code_for_language(const BLanguage& language) 70 { 71 if (language.IsCountrySpecific()) 72 return language.CountryCode(); 73 74 // TODO: implement for real! For now, we just map some well known 75 // languages to countries to make FirstBootPrompt happy. 76 switch ((tolower(language.Code()[0]) << 8) | tolower(language.Code()[1])) { 77 case 'be': // Belarus 78 return "BY"; 79 case 'cs': // Czech Republic 80 return "CZ"; 81 case 'da': // Denmark 82 return "DK"; 83 case 'el': // Greece 84 return "GR"; 85 case 'en': // United Kingdom 86 return "GB"; 87 case 'hi': // India 88 return "IN"; 89 case 'ja': // Japan 90 return "JP"; 91 case 'ko': // South Korea 92 return "KR"; 93 case 'nb': // Norway 94 return "NO"; 95 case 'pa': // Pakistan 96 return "PK"; 97 case 'sv': // Sweden 98 return "SE"; 99 case 'uk': // Ukraine 100 return "UA"; 101 case 'zh': // China 102 return "CN"; 103 104 // Languages with a matching country name 105 case 'de': // Germany 106 case 'es': // Spain 107 case 'fi': // Finland 108 case 'fr': // France 109 case 'hr': // Croatia 110 case 'hu': // Hungary 111 case 'it': // Italy 112 case 'lt': // Lithuania 113 case 'nl': // Netherlands 114 case 'pl': // Poland 115 case 'pt': // Portugal 116 case 'ro': // Romania 117 case 'ru': // Russia 118 case 'sk': // Slovakia 119 return language.Code(); 120 } 121 122 return NULL; 123 } 124 125 126 // #pragma mark - 127 128 129 BLocaleRoster::BLocaleRoster() 130 : 131 fData(new(std::nothrow) BPrivate::LocaleRosterData(BLanguage("en_US"), 132 BFormattingConventions("en_US"))) 133 { 134 } 135 136 137 BLocaleRoster::~BLocaleRoster() 138 { 139 delete fData; 140 } 141 142 143 /*static*/ BLocaleRoster* 144 BLocaleRoster::Default() 145 { 146 return MutableLocaleRoster::Default(); 147 } 148 149 150 status_t 151 BLocaleRoster::Refresh() 152 { 153 return fData->Refresh(); 154 } 155 156 157 status_t 158 BLocaleRoster::GetDefaultTimeZone(BTimeZone* timezone) const 159 { 160 if (!timezone) 161 return B_BAD_VALUE; 162 163 BAutolock lock(fData->fLock); 164 if (!lock.IsLocked()) 165 return B_ERROR; 166 167 *timezone = fData->fDefaultTimeZone; 168 169 return B_OK; 170 } 171 172 173 const BLocale* 174 BLocaleRoster::GetDefaultLocale() const 175 { 176 return &fData->fDefaultLocale; 177 } 178 179 180 status_t 181 BLocaleRoster::GetLanguage(const char* languageCode, 182 BLanguage** _language) const 183 { 184 if (_language == NULL || languageCode == NULL || languageCode[0] == '\0') 185 return B_BAD_VALUE; 186 187 BLanguage* language = new(std::nothrow) BLanguage(languageCode); 188 if (language == NULL) 189 return B_NO_MEMORY; 190 191 *_language = language; 192 return B_OK; 193 } 194 195 196 status_t 197 BLocaleRoster::GetPreferredLanguages(BMessage* languages) const 198 { 199 if (!languages) 200 return B_BAD_VALUE; 201 202 BAutolock lock(fData->fLock); 203 if (!lock.IsLocked()) 204 return B_ERROR; 205 206 *languages = fData->fPreferredLanguages; 207 208 return B_OK; 209 } 210 211 212 /** 213 * \brief Fills \c message with 'language'-fields containing the language- 214 * ID(s) of all available languages. 215 */ 216 status_t 217 BLocaleRoster::GetAvailableLanguages(BMessage* languages) const 218 { 219 if (!languages) 220 return B_BAD_VALUE; 221 222 int32_t localeCount; 223 const Locale* icuLocaleList = Locale::getAvailableLocales(localeCount); 224 225 for (int i = 0; i < localeCount; i++) 226 languages->AddString("language", icuLocaleList[i].getName()); 227 228 return B_OK; 229 } 230 231 232 status_t 233 BLocaleRoster::GetAvailableCountries(BMessage* countries) const 234 { 235 if (!countries) 236 return B_BAD_VALUE; 237 238 int32 i; 239 const char* const* countryList = uloc_getISOCountries(); 240 241 for (i = 0; countryList[i] != NULL; i++) 242 countries->AddString("country", countryList[i]); 243 244 return B_OK; 245 } 246 247 248 status_t 249 BLocaleRoster::GetAvailableTimeZones(BMessage* timeZones) const 250 { 251 if (!timeZones) 252 return B_BAD_VALUE; 253 254 status_t status = B_OK; 255 256 StringEnumeration* zoneList = TimeZone::createEnumeration(); 257 258 UErrorCode icuStatus = U_ZERO_ERROR; 259 int32 count = zoneList->count(icuStatus); 260 if (U_SUCCESS(icuStatus)) { 261 for (int i = 0; i < count; ++i) { 262 const char* zoneID = zoneList->next(NULL, icuStatus); 263 if (zoneID == NULL || !U_SUCCESS(icuStatus)) { 264 status = B_ERROR; 265 break; 266 } 267 timeZones->AddString("timeZone", zoneID); 268 } 269 } else 270 status = B_ERROR; 271 272 delete zoneList; 273 274 return status; 275 } 276 277 278 status_t 279 BLocaleRoster::GetAvailableTimeZonesWithRegionInfo(BMessage* timeZones) const 280 { 281 if (!timeZones) 282 return B_BAD_VALUE; 283 284 status_t status = B_OK; 285 286 UErrorCode icuStatus = U_ZERO_ERROR; 287 288 StringEnumeration* zoneList = TimeZone::createTimeZoneIDEnumeration( 289 UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, icuStatus); 290 291 int32 count = zoneList->count(icuStatus); 292 if (U_SUCCESS(icuStatus)) { 293 for (int i = 0; i < count; ++i) { 294 const char* zoneID = zoneList->next(NULL, icuStatus); 295 if (zoneID == NULL || !U_SUCCESS(icuStatus)) { 296 status = B_ERROR; 297 break; 298 } 299 timeZones->AddString("timeZone", zoneID); 300 301 char region[5]; 302 icuStatus = U_ZERO_ERROR; 303 TimeZone::getRegion(zoneID, region, 5, icuStatus); 304 if (!U_SUCCESS(icuStatus)) { 305 status = B_ERROR; 306 break; 307 } 308 timeZones->AddString("region", region); 309 } 310 } else 311 status = B_ERROR; 312 313 delete zoneList; 314 315 return status; 316 } 317 318 319 status_t 320 BLocaleRoster::GetAvailableTimeZonesForCountry(BMessage* timeZones, 321 const char* countryCode) const 322 { 323 if (!timeZones) 324 return B_BAD_VALUE; 325 326 status_t status = B_OK; 327 328 StringEnumeration* zoneList = TimeZone::createEnumeration(countryCode); 329 // countryCode == NULL will yield all timezones not bound to a country 330 331 UErrorCode icuStatus = U_ZERO_ERROR; 332 int32 count = zoneList->count(icuStatus); 333 if (U_SUCCESS(icuStatus)) { 334 for (int i = 0; i < count; ++i) { 335 const char* zoneID = zoneList->next(NULL, icuStatus); 336 if (zoneID == NULL || !U_SUCCESS(icuStatus)) { 337 status = B_ERROR; 338 break; 339 } 340 timeZones->AddString("timeZone", zoneID); 341 } 342 } else 343 status = B_ERROR; 344 345 delete zoneList; 346 347 return status; 348 } 349 350 351 status_t 352 BLocaleRoster::GetFlagIconForCountry(BBitmap* flagIcon, const char* countryCode) 353 { 354 if (countryCode == NULL) 355 return B_BAD_VALUE; 356 357 BAutolock lock(fData->fLock); 358 if (!lock.IsLocked()) 359 return B_ERROR; 360 361 BResources* resources; 362 status_t status = fData->GetResources(&resources); 363 if (status != B_OK) 364 return status; 365 366 // Normalize the country code: 2 letters uppercase 367 // filter things out so that "pt_BR" gives the flag for brazil 368 369 int codeLength = strlen(countryCode); 370 if (codeLength < 2) 371 return B_BAD_VALUE; 372 373 char normalizedCode[3]; 374 normalizedCode[0] = toupper(countryCode[codeLength - 2]); 375 normalizedCode[1] = toupper(countryCode[codeLength - 1]); 376 normalizedCode[2] = '\0'; 377 378 size_t size; 379 const void* buffer = resources->LoadResource(B_VECTOR_ICON_TYPE, 380 normalizedCode, &size); 381 if (buffer == NULL || size == 0) 382 return B_NAME_NOT_FOUND; 383 384 return BIconUtils::GetVectorIcon(static_cast<const uint8*>(buffer), size, 385 flagIcon); 386 } 387 388 389 status_t 390 BLocaleRoster::GetFlagIconForLanguage(BBitmap* flagIcon, 391 const char* languageCode) 392 { 393 if (languageCode == NULL || languageCode[0] == '\0' 394 || languageCode[1] == '\0') 395 return B_BAD_VALUE; 396 397 BAutolock lock(fData->fLock); 398 if (!lock.IsLocked()) 399 return B_ERROR; 400 401 BResources* resources; 402 status_t status = fData->GetResources(&resources); 403 if (status != B_OK) 404 return status; 405 406 // Normalize the language code: first two letters, lowercase 407 408 char normalizedCode[3]; 409 normalizedCode[0] = tolower(languageCode[0]); 410 normalizedCode[1] = tolower(languageCode[1]); 411 normalizedCode[2] = '\0'; 412 413 size_t size; 414 const void* buffer = resources->LoadResource(B_VECTOR_ICON_TYPE, 415 normalizedCode, &size); 416 if (buffer != NULL && size != 0) { 417 return BIconUtils::GetVectorIcon(static_cast<const uint8*>(buffer), 418 size, flagIcon); 419 } 420 421 // There is no language flag, try to get the default country's flag for 422 // the language instead. 423 424 BLanguage language(languageCode); 425 const char* countryCode = country_code_for_language(language); 426 if (countryCode == NULL) 427 return B_NAME_NOT_FOUND; 428 429 return GetFlagIconForCountry(flagIcon, countryCode); 430 } 431 432 433 status_t 434 BLocaleRoster::GetAvailableCatalogs(BMessage* languageList, 435 const char* sigPattern, const char* langPattern, int32 fingerprint) const 436 { 437 if (languageList == NULL) 438 return B_BAD_VALUE; 439 440 BAutolock lock(fData->fLock); 441 if (!lock.IsLocked()) 442 return B_ERROR; 443 444 int32 count = fData->fCatalogAddOnInfos.CountItems(); 445 for (int32 i = 0; i < count; ++i) { 446 CatalogAddOnInfo* info 447 = (CatalogAddOnInfo*)fData->fCatalogAddOnInfos.ItemAt(i); 448 449 if (!info->fLanguagesFunc) 450 continue; 451 452 info->fLanguagesFunc(languageList, sigPattern, langPattern, 453 fingerprint); 454 } 455 456 return B_OK; 457 } 458 459 460 bool 461 BLocaleRoster::IsFilesystemTranslationPreferred() const 462 { 463 BAutolock lock(fData->fLock); 464 if (!lock.IsLocked()) 465 return B_ERROR; 466 467 return fData->fIsFilesystemTranslationPreferred; 468 } 469 470 471 /*! \brief Looks up a localized filename from a catalog. 472 \param localizedFileName A pre-allocated BString object for the result 473 of the lookup. 474 \param ref An entry_ref with an attribute holding data for catalog lookup. 475 \param traverse A boolean to decide if symlinks are to be traversed. 476 \return 477 - \c B_OK: success 478 - \c B_ENTRY_NOT_FOUND: failure. Attribute not found, entry not found 479 in catalog, etc 480 - other error codes: failure 481 482 Attribute format: "signature:context:string" 483 (no colon in any of signature, context and string) 484 485 Lookup is done for the top preferred language, only. 486 Lookup fails if a comment is present in the catalog entry. 487 */ 488 status_t 489 BLocaleRoster::GetLocalizedFileName(BString& localizedFileName, 490 const entry_ref& ref, bool traverse) 491 { 492 BString signature; 493 BString context; 494 BString string; 495 496 status_t status = _PrepareCatalogEntry(ref, signature, context, string, 497 traverse); 498 499 if (status != B_OK) 500 return status; 501 502 // Try to get entry_ref for signature from above 503 BRoster roster; 504 entry_ref catalogRef; 505 // The signature is missing application/ 506 signature.Prepend("application/"); 507 status = roster.FindApp(signature, &catalogRef); 508 if (status != B_OK) 509 return status; 510 511 BCatalog catalog(catalogRef); 512 const char* temp = catalog.GetString(string, context); 513 514 if (temp == NULL) 515 return B_ENTRY_NOT_FOUND; 516 517 localizedFileName = temp; 518 return B_OK; 519 } 520 521 522 static status_t 523 _InitializeCatalog(void* param) 524 { 525 BCatalog* catalog = (BCatalog*)param; 526 527 // figure out image (shared object) from catalog address 528 image_info info; 529 int32 cookie = 0; 530 bool found = false; 531 532 while (get_next_image_info(0, &cookie, &info) == B_OK) { 533 if ((char*)info.data < (char*)catalog && (char*)info.data 534 + info.data_size > (char*)catalog) { 535 found = true; 536 break; 537 } 538 } 539 540 if (!found) 541 return B_NAME_NOT_FOUND; 542 543 // load the catalog for this mimetype 544 entry_ref ref; 545 if (BEntry(info.name).GetRef(&ref) == B_OK && catalog->SetTo(ref) == B_OK) 546 return B_OK; 547 548 return B_ERROR; 549 } 550 551 552 BCatalog* 553 BLocaleRoster::_GetCatalog(BCatalog* catalog, int32* catalogInitStatus) 554 { 555 // This function is used in the translation macros, so it can't return a 556 // status_t. Maybe it could throw exceptions ? 557 558 __init_once(catalogInitStatus, _InitializeCatalog, catalog); 559 return catalog; 560 } 561 562 563 status_t 564 BLocaleRoster::_PrepareCatalogEntry(const entry_ref& ref, BString& signature, 565 BString& context, BString& string, bool traverse) 566 { 567 BEntry entry(&ref, traverse); 568 if (!entry.Exists()) 569 return B_ENTRY_NOT_FOUND; 570 571 BNode node(&entry); 572 status_t status = node.InitCheck(); 573 if (status != B_OK) 574 return status; 575 576 status = node.ReadAttrString("SYS:NAME", &signature); 577 if (status != B_OK) 578 return status; 579 580 int32 first = signature.FindFirst(':'); 581 int32 last = signature.FindLast(':'); 582 if (first == last) 583 return B_ENTRY_NOT_FOUND; 584 585 context = signature; 586 string = signature; 587 588 signature.Truncate(first); 589 context.Truncate(last); 590 context.Remove(0, first + 1); 591 string.Remove(0, last + 1); 592 593 if (signature.Length() == 0 || context.Length() == 0 594 || string.Length() == 0) 595 return B_ENTRY_NOT_FOUND; 596 597 return B_OK; 598 } 599