52 #include <nsIObserverService.h>
58 #include <nsAutoPtr.h>
60 #include <nsServiceManagerUtils.h>
61 #include <nsThreadUtils.h>
70 nsIStringBundleService,
92 NS_ENSURE_ARG_POINTER(_retval);
93 NS_ADDREF(*_retval = mBundle);
103 sbStringBundleService::GetBrandBundle(nsIStringBundle** _retval)
105 NS_ENSURE_ARG_POINTER(_retval);
106 NS_ADDREF(*_retval = mBrandBundle);
127 sbStringBundleService::CreateBundle(
const char* aURLSpec,
128 nsIStringBundle** _retval)
131 NS_ENSURE_ARG_POINTER(aURLSpec);
132 NS_ENSURE_ARG_POINTER(_retval);
138 nsCOMPtr<nsIStringBundle> baseStringBundle;
139 rv = mBaseStringBundleService->CreateBundle(aURLSpec,
140 getter_AddRefs(baseStringBundle));
141 NS_ENSURE_SUCCESS(rv, rv);
144 nsRefPtr<sbStringBundle> stringBundle;
146 NS_ENSURE_SUCCESS(rv, rv);
149 NS_ADDREF(*_retval = stringBundle);
164 sbStringBundleService::CreateExtensibleBundle(
const char *aRegistryKey,
165 nsIStringBundle **_retval)
168 NS_ENSURE_ARG_POINTER(aRegistryKey);
169 NS_ENSURE_ARG_POINTER(_retval);
175 nsCOMPtr<nsIStringBundle> baseStringBundle;
176 rv = mBaseStringBundleService->CreateExtensibleBundle
178 getter_AddRefs(baseStringBundle));
179 NS_ENSURE_SUCCESS(rv, rv);
182 nsRefPtr<sbStringBundle> stringBundle;
184 NS_ENSURE_SUCCESS(rv, rv);
187 NS_ADDREF(*_retval = stringBundle);
203 sbStringBundleService::FormatStatusMessage(nsresult aStatus,
204 const PRUnichar *aStatusArg,
207 return mBaseStringBundleService->FormatStatusMessage(aStatus,
224 sbStringBundleService::FlushBundles()
226 return mBaseStringBundleService->FlushBundles();
237 sbStringBundleService::Observe(
nsISupports *aSubject,
239 const PRUnichar *someData)
242 if (!strcmp(
"chrome-flush-caches", aTopic)) {
245 NS_ENSURE_SUCCESS(rv, rv);
247 else if (!strcmp(
"profile-change-teardown", aTopic)) {
248 nsCOMPtr<nsIObserverService>
obssvc =
249 do_GetService(
"@mozilla.org/observer-service;1", &rv);
250 NS_ENSURE_SUCCESS(rv, rv);
251 rv = obssvc->RemoveObserver(
this,
"chrome-flush-caches");
252 NS_ENSURE_SUCCESS(rv, rv);
253 rv = obssvc->RemoveObserver(
this, aTopic);
254 NS_ENSURE_SUCCESS(rv, rv);
293 nsCOMPtr<nsIObserverService> obssvc =
294 do_GetService(
"@mozilla.org/observer-service;1", &rv);
295 NS_ENSURE_SUCCESS(rv, rv);
296 rv = obssvc->AddObserver(
this,
"chrome-flush-caches", PR_FALSE);
297 NS_ENSURE_SUCCESS(rv, rv);
298 rv = obssvc->AddObserver(
this,
"profile-change-teardown", PR_FALSE);
299 NS_ENSURE_SUCCESS(rv, rv);
302 NS_ENSURE_SUCCESS(rv, rv);
317 mBaseStringBundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
318 NS_ENSURE_SUCCESS(rv, rv);
321 rv = CreateBundle(
"chrome://songbird/locale/songbird.properties",
322 getter_AddRefs(mBundle));
323 NS_ENSURE_SUCCESS(rv, rv);
326 rv = CreateBundle(
"chrome://branding/locale/brand.properties",
327 getter_AddRefs(mBrandBundle));
328 NS_ENSURE_SUCCESS(rv, rv);
367 NS_ENSURE_ARG_POINTER(_retval);
373 nsAutoString bundleString;
374 PRBool bundleAvailable = PR_FALSE;
375 PRUint32 bundleCount = mStringBundleList.Count();
376 for (PRUint32
i = 0;
i < bundleCount;
i++) {
377 nsCOMPtr<nsIStringBundle> stringBundle = mStringBundleList[
i];
378 rv = stringBundle->GetStringFromID(aID, getter_Copies(bundleString));
379 if (NS_SUCCEEDED(rv)) {
380 bundleAvailable = PR_TRUE;
384 if (!bundleAvailable)
385 return NS_ERROR_NOT_AVAILABLE;
388 rv = ApplySubstitutions(bundleString);
389 NS_ENSURE_SUCCESS(rv, rv);
392 PRUnichar* prBundleString = ToNewUnicode(bundleString);
393 NS_ENSURE_TRUE(prBundleString, NS_ERROR_OUT_OF_MEMORY);
394 *_retval = prBundleString;
405 sbStringBundle::GetStringFromName(
const PRUnichar *
aName,
409 NS_ENSURE_ARG_POINTER(aName);
410 NS_ENSURE_ARG_POINTER(_retval);
416 nsAutoString bundleString;
417 PRBool bundleAvailable = PR_FALSE;
418 PRUint32 bundleCount = mStringBundleList.Count();
419 for (PRUint32
i = 0;
i < bundleCount;
i++) {
420 nsCOMPtr<nsIStringBundle> stringBundle = mStringBundleList[
i];
421 rv = stringBundle->GetStringFromName(aName, getter_Copies(bundleString));
422 if (NS_SUCCEEDED(rv)) {
423 bundleAvailable = PR_TRUE;
427 if (!bundleAvailable)
428 return NS_ERROR_NOT_AVAILABLE;
431 rv = ApplySubstitutions(bundleString);
432 NS_ENSURE_SUCCESS(rv, rv);
435 PRUnichar* prBundleString = ToNewUnicode(bundleString);
436 NS_ENSURE_TRUE(prBundleString, NS_ERROR_OUT_OF_MEMORY);
437 *_retval = prBundleString;
450 sbStringBundle::FormatStringFromID(PRInt32 aID,
451 const PRUnichar **params,
456 NS_ENSURE_ARG_POINTER(_retval);
462 nsAutoString bundleString;
463 PRBool bundleAvailable = PR_FALSE;
464 PRUint32 bundleCount = mStringBundleList.Count();
465 for (PRUint32
i = 0;
i < bundleCount;
i++) {
466 nsCOMPtr<nsIStringBundle> stringBundle = mStringBundleList[
i];
467 rv = stringBundle->FormatStringFromID(aID,
470 getter_Copies(bundleString));
471 if (NS_SUCCEEDED(rv)) {
472 bundleAvailable = PR_TRUE;
476 if (!bundleAvailable)
477 return NS_ERROR_NOT_AVAILABLE;
480 rv = ApplySubstitutions(bundleString);
481 NS_ENSURE_SUCCESS(rv, rv);
484 PRUnichar* prBundleString = ToNewUnicode(bundleString);
485 NS_ENSURE_TRUE(prBundleString, NS_ERROR_OUT_OF_MEMORY);
486 *_retval = prBundleString;
499 sbStringBundle::FormatStringFromName(
const PRUnichar *aName,
500 const PRUnichar **params,
505 NS_ENSURE_ARG_POINTER(aName);
506 NS_ENSURE_ARG_POINTER(_retval);
512 nsAutoString bundleString;
513 PRBool bundleAvailable = PR_FALSE;
514 PRUint32 bundleCount = mStringBundleList.Count();
515 for (PRUint32
i = 0;
i < bundleCount;
i++) {
516 nsCOMPtr<nsIStringBundle> stringBundle = mStringBundleList[
i];
517 rv = stringBundle->FormatStringFromName(aName,
520 getter_Copies(bundleString));
521 if (NS_SUCCEEDED(rv)) {
522 bundleAvailable = PR_TRUE;
526 if (!bundleAvailable)
527 return NS_ERROR_NOT_AVAILABLE;
530 rv = ApplySubstitutions(bundleString);
531 NS_ENSURE_SUCCESS(rv, rv);
534 PRUnichar* prBundleString = ToNewUnicode(bundleString);
535 NS_ENSURE_TRUE(prBundleString, NS_ERROR_OUT_OF_MEMORY);
536 *_retval = prBundleString;
549 return mBaseStringBundle->GetSimpleEnumeration(_retval);
573 NS_ENSURE_ARG_POINTER(aBaseStringBundle);
574 NS_ENSURE_ARG_POINTER(aStringBundle);
580 nsRefPtr<sbStringBundle> stringBundle =
new sbStringBundle(aBaseStringBundle);
581 NS_ENSURE_TRUE(stringBundle, NS_ERROR_OUT_OF_MEMORY);
584 rv = stringBundle->Initialize();
585 NS_ENSURE_SUCCESS(rv, rv);
588 stringBundle.forget(aStringBundle);
616 sbStringBundle::sbStringBundle(nsIStringBundle* aBaseStringBundle) :
617 mBaseStringBundle(aBaseStringBundle)
627 sbStringBundle::Initialize()
632 mBaseStringBundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
633 NS_ENSURE_SUCCESS(rv, rv);
636 rv = LoadBundle(mBaseStringBundle);
637 NS_ENSURE_SUCCESS(rv, rv);
651 sbStringBundle::LoadBundle(nsAString& aBundleURLSpec)
656 nsCOMPtr<nsIStringBundle>
bundle;
657 rv = mBaseStringBundleService->CreateBundle
658 (NS_ConvertUTF16toUTF8(aBundleURLSpec).BeginReading(),
659 getter_AddRefs(bundle));
660 NS_ENSURE_SUCCESS(rv, rv);
663 rv = LoadBundle(bundle);
664 NS_ENSURE_SUCCESS(rv, rv);
677 sbStringBundle::LoadBundle(nsIStringBundle* aBundle)
680 NS_ENSURE_ARG_POINTER(aBundle);
686 mStringBundleList.AppendObject(aBundle);
689 nsTArray<nsString> includeBundleList;
690 nsAutoString includeBundleListString;
691 rv = aBundle->GetStringFromName
692 (NS_LITERAL_STRING(
"include_bundle_list").
get(),
693 getter_Copies(includeBundleListString));
694 if (NS_SUCCEEDED(rv)) {
696 NS_LITERAL_STRING(
","),
701 PRUint32 bundleCount = includeBundleList.Length();
702 for (PRUint32
i = 0;
i < bundleCount;
i++) {
703 rv = LoadBundle(includeBundleList[
i]);
704 NS_ENSURE_SUCCESS(rv, rv);
718 sbStringBundle::ApplySubstitutions(nsAString& aString)
723 nsAutoString processedString(aString);
724 PRInt32 subStartIndex = 0;
725 PRInt32 subEndIndex = -1;
729 subStartIndex = processedString.Find(
"&",
730 static_cast<PRUint32>(subStartIndex));
731 if (subStartIndex < 0)
733 subEndIndex = processedString.Find(
";",
734 static_cast<PRUint32>(subStartIndex));
737 subLength = subEndIndex + 1 - subStartIndex;
740 nsAutoString subName(Substring(processedString,
745 nsAutoString subString;
746 if (subName.Equals(NS_LITERAL_STRING(
"amp"))) {
747 subString.Assign(NS_LITERAL_STRING(
"&"));
749 rv = GetStringFromName(subName.get(), getter_Copies(subString));
751 subString.Truncate();
755 processedString.Replace(subStartIndex, subLength, subString);
758 subStartIndex += subString.Length();
762 aString.Assign(processedString);
NS_IMPL_THREADSAFE_ISUPPORTS1(sbStringBundle, nsIStringBundle) NS_IMETHODIMP sbStringBundle
NS_DECL_ISUPPORTS NS_DECL_NSISTRINGBUNDLESERVICE NS_DECL_NSIOBSERVER NS_DECL_SBISTRINGBUNDLESERVICE sbStringBundleService()
virtual ~sbStringBundleService()
void nsString_Split(const nsAString &aString, const nsAString &aDelimiter, nsTArray< nsString > &aSubStringArray)
NS_DECL_ISUPPORTS static NS_DECL_NSISTRINGBUNDLE nsresult New(nsIStringBundle *aBaseStringBundle, sbStringBundle **aStringBundle)
Songbird String Bundle Service Definitions.
Interface for the Songbird string bundle service. Instantiate as a component service.
_getSelectedPageStyle s i
NS_IMPL_THREADSAFE_ISUPPORTS3(sbStringBundleService, nsIStringBundleService, nsIObserver, sbIStringBundleService) NS_IMETHODIMP sbStringBundleService
Main Songbird string bundle. Thread-safe.
virtual ~sbStringBundle()