[haiku-commits] haiku: hrev44024 - src/kits/locale headers/os/locale src/kits/shared src/kits/interface

  • From: zooey@xxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sat, 14 Apr 2012 17:59:51 +0200 (CEST)

hrev44024 adds 4 changesets to branch 'master'
old head: 531ae0a6c0b38e8eb824fa3b50c2753f32fe32a5
new head: eaa5e0936d94e7d0bb2e6066a87d485e7613414c

----------------------------------------------------------------------------

ec48d27: Revert "Fix access to system catalog strings."
  
  This reverts commit 6d2186615f664194649a4129cf73d7f37696cd40.

250eca3: Start cleanup of Catalog.{h,cpp}
  
  * unify pointer style (to type* )
  * always use boolean expressions in if
  * introduce some spacing for better readability
  * make a couple inline methods non-inline

cc52f0d: Make BCatalog threadsafe.
  
  * use a locker to protect the CatalogAddOn-chain against parallel
    access
  * rename BCatalog::SetCatalog() to SetTo() and make it a proper
    initializing function
  * adjust implementation of BLocaleRoster accordingly

eaa5e09: Fix #8452 (app's crashing for non-existent system catalog)
  
  * make the system catalog a BCatalog instead of a BCatalogAddOn*,
    such that using a non-existing system catalog won't crash but
    simply return the untranslated string instead
  * rename MutableLocaleRoster::GetSystemCatalog() to LoadSystemCatalog()
    and adjust it to use BCatalog::SetTo() in order to replace the
    data used by the given catalog
  * adjust all users of gSystemCatalog accordingly

                                    [ Oliver Tappe <zooey@xxxxxxxxxxxxxxx> ]

----------------------------------------------------------------------------

17 files changed, 246 insertions(+), 179 deletions(-)
headers/os/locale/Catalog.h                  |  109 +++-------
headers/private/locale/MutableLocaleRoster.h |    4 +-
headers/private/locale/SystemCatalog.h       |    2 +-
src/kits/interface/ColorControl.cpp          |    6 +-
src/kits/interface/Dragger.cpp               |    2 +-
src/kits/interface/Menu.cpp                  |    2 +-
src/kits/interface/PrintJob.cpp              |    2 +-
src/kits/interface/TextView.cpp              |    2 +-
src/kits/interface/ZombieReplicantView.cpp   |    2 +-
src/kits/locale/Catalog.cpp                  |  243 ++++++++++++++++------
src/kits/locale/HashMapCatalog.cpp           |    5 +-
src/kits/locale/InitLocaleKit.cpp            |    5 +-
src/kits/locale/LocaleRoster.cpp             |    5 +-
src/kits/locale/MutableLocaleRoster.cpp      |   16 +-
src/kits/shared/AboutMenuItem.cpp            |    2 +-
src/kits/shared/AboutWindow.cpp              |    8 +-
src/kits/shared/StringForSize.cpp            |   10 +-

############################################################################

Commit:      ec48d27ca3717c4622f19e4446fe9bcca396dcd6
URL:         http://cgit.haiku-os.org/haiku/commit/?id=ec48d27
Author:      Oliver Tappe <zooey@xxxxxxxxxxxxxxx>
Date:        Sat Apr 14 12:05:00 2012 UTC

Revert "Fix access to system catalog strings."

This reverts commit 6d2186615f664194649a4129cf73d7f37696cd40.

----------------------------------------------------------------------------

diff --git a/src/kits/locale/HashMapCatalog.cpp 
b/src/kits/locale/HashMapCatalog.cpp
index cd77ad4..70f0fc0 100644
--- a/src/kits/locale/HashMapCatalog.cpp
+++ b/src/kits/locale/HashMapCatalog.cpp
@@ -124,10 +124,7 @@ BHashMapCatalog::GetString(const char *string, const char 
*context,
        const char *comment)
 {
        CatKey key(string, context, comment);
-       BString value = fCatMap.Get(key);
-       if (value.Length() != 0)
-               return value.String();
-       return string;
+       return GetString(key);
 }
 
 

############################################################################

Commit:      250eca3254c11a3f2762768287b83f39ff992855
URL:         http://cgit.haiku-os.org/haiku/commit/?id=250eca3
Author:      Oliver Tappe <zooey@xxxxxxxxxxxxxxx>
Date:        Sat Apr 14 15:24:20 2012 UTC

Start cleanup of Catalog.{h,cpp}

* unify pointer style (to type* )
* always use boolean expressions in if
* introduce some spacing for better readability
* make a couple inline methods non-inline

----------------------------------------------------------------------------

diff --git a/headers/os/locale/Catalog.h b/headers/os/locale/Catalog.h
index 48c1440..c147f0c 100644
--- a/headers/os/locale/Catalog.h
+++ b/headers/os/locale/Catalog.h
@@ -48,6 +48,8 @@ public:
                        status_t                        InitCheck() const;
                        int32                           CountItems() const;
 
+                       // TODO: drop this, as the lifetime of the returned 
object
+                       //       is indeterminate
                        BCatalogAddOn*          CatalogAddOn();
 
 protected:
@@ -58,8 +60,8 @@ protected:
                        BCatalogAddOn*          fCatalog;
 
 private:
-                       friend class BLocale;
-                       friend status_t get_add_on_catalog(BCatalog*, const 
char*);
+       friend  class BLocale;
+       friend  status_t                        get_add_on_catalog(BCatalog*, 
const char*);
 };
 
 
@@ -129,21 +131,21 @@ private:
 /* example:
                #define B_TRANSLATE_CONTEXT "MyDecentApp-Menu"
 
-               static const char *choices[] = {
+               static const char* choices[] = {
                        B_TRANSLATE_MARK("left"),
                        B_TRANSLATE_MARK("right"),
                        B_TRANSLATE_MARK("up"),
                        B_TRANSLATE_MARK("down")
                };
 
-               void MyClass::AddChoices(BMenu *menu) {
-                       for (char **ch = choices; *ch; ch++) {
+               void MyClass::AddChoices(BMenu* menu) {
+                       for (char** ch = choices; *ch != '\0'; ++ch) {
                                menu->AddItem(
                                        new BMenuItem(
                                                B_TRANSLATE(*ch),
                                                new BMessage(...)
                                        )
-                               )
+                               );
                        }
                }
 */
@@ -331,8 +333,8 @@ protected:
        virtual void                            UpdateFingerprint();
 
 protected:
-                       friend class BCatalog;
-                       friend status_t get_add_on_catalog(BCatalog*, const 
char*);
+       friend  class BCatalog;
+       friend  status_t                        get_add_on_catalog(BCatalog*, 
const char*);
 
                        status_t                        fInitCheck;
                        BString                         fSignature;
@@ -344,12 +346,13 @@ protected:
 // every catalog-add-on should export these symbols...
 // ...the function that instantiates a catalog for this add-on-type...
 extern "C"
-BCatalogAddOn *instantiate_catalog(const char *signature,
-       const char *language, uint32 fingerprint);
+BCatalogAddOn* instantiate_catalog(const char* signature, const char* language,
+       uint32 fingerprint);
+
 // ...the function that creates an empty catalog for this add-on-type...
 extern "C"
-BCatalogAddOn *create_catalog(const char *signature,
-       const char *language);
+BCatalogAddOn* create_catalog(const char* signature, const char* language);
+
 // ...and the priority which will be used to order the catalog-add-ons:
 extern uint8 gCatalogAddOnPriority;
 
@@ -357,76 +360,22 @@ extern uint8 gCatalogAddOnPriority;
 /*
  * BCatalog - inlines for trivial accessors:
  */
-inline status_t
-BCatalog::GetSignature(BString *sig)
-{
-       if (!sig)
-               return B_BAD_VALUE;
-       if (!fCatalog)
-               return B_NO_INIT;
-       *sig = fCatalog->fSignature;
-       return B_OK;
-}
-
-
-inline status_t
-BCatalog::GetLanguage(BString *lang)
-{
-       if (!lang)
-               return B_BAD_VALUE;
-       if (!fCatalog)
-               return B_NO_INIT;
-       *lang = fCatalog->fLanguageName;
-       return B_OK;
-}
-
-
-inline status_t
-BCatalog::GetFingerprint(uint32 *fp)
-{
-       if (!fp)
-               return B_BAD_VALUE;
-       if (!fCatalog)
-               return B_NO_INIT;
-       *fp = fCatalog->fFingerprint;
-       return B_OK;
-}
-
-
-inline const char *
-BCatalog::GetNoAutoCollectString(const char *string, const char *context,
-       const char *comment)
+inline const char*
+BCatalog::GetNoAutoCollectString(const char* string, const char* context,
+       const char* comment)
 {
        return GetString(string, context, comment);
 }
 
 
-inline const char *
+inline const char*
 BCatalog::GetNoAutoCollectString(uint32 id)
 {
        return GetString(id);
 }
 
 
-inline status_t
-BCatalog::InitCheck() const
-{
-       return fCatalog
-                               ? fCatalog->InitCheck()
-                               : B_NO_INIT;
-}
-
-
-inline int32
-BCatalog::CountItems() const
-{
-       if (!fCatalog)
-               return 0;
-       return fCatalog->CountItems();
-}
-
-
-inline BCatalogAddOn *
+inline BCatalogAddOn*
 BCatalog::CatalogAddOn()
 {
        return fCatalog;
@@ -436,16 +385,15 @@ BCatalog::CatalogAddOn()
 /*
  * BCatalogAddOn - inlines for trivial accessors:
  */
-inline BCatalogAddOn *
+inline BCatalogAddOn*
 BCatalogAddOn::Next()
 {
        return fNext;
 }
 
-inline const char *
-BCatalogAddOn::MarkForTranslation(const char *str,
-       const char * /*ctx __attribute__ ((unused))*/,
-       const char * /*cmt __attribute__ ((unused))*/)
+inline const char*
+BCatalogAddOn::MarkForTranslation(const char* str, const char* /* context */,
+       const char* /* comment */)
 {
        return str;
 }
diff --git a/src/kits/locale/Catalog.cpp b/src/kits/locale/Catalog.cpp
index d22f986..b7740d7 100644
--- a/src/kits/locale/Catalog.cpp
+++ b/src/kits/locale/Catalog.cpp
@@ -26,11 +26,11 @@ BCatalog::BCatalog()
 }
 
 
-BCatalog::BCatalog(const entry_ref &catalogOwner, const char *language,
+BCatalog::BCatalog(const entry_ref& catalogOwner, const char* language,
        uint32 fingerprint)
 {
-       fCatalog = MutableLocaleRoster::Default()->LoadCatalog(catalogOwner, 
language,
-               fingerprint);
+       fCatalog = MutableLocaleRoster::Default()->LoadCatalog(catalogOwner,
+               language, fingerprint);
 }
 
 
@@ -40,65 +40,116 @@ BCatalog::~BCatalog()
 }
 
 
-const char *
-BCatalog::GetString(const char *string, const char *context,
-       const char *comment)
+const char*
+BCatalog::GetString(const char* string, const char* context,
+       const char* comment)
 {
-       const char *translated;
+       const char* translated;
        for (BCatalogAddOn* cat = fCatalog; cat != NULL; cat = cat->fNext) {
                translated = cat->GetString(string, context, comment);
-               if (translated)
+               if (translated != NULL)
                        return translated;
        }
+
        return string;
 }
 
 
-const char *
+const char*
 BCatalog::GetString(uint32 id)
 {
-       const char *translated;
+       const char* translated;
        for (BCatalogAddOn* cat = fCatalog; cat != NULL; cat = cat->fNext) {
                translated = cat->GetString(id);
-               if (translated)
+               if (translated != NULL)
                        return translated;
        }
+
        return "";
 }
 
 
 status_t
-BCatalog::GetData(const char *name, BMessage *msg)
+BCatalog::GetData(const char* name, BMessage* msg)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        status_t res;
        for (BCatalogAddOn* cat = fCatalog; cat != NULL; cat = cat->fNext) {
                res = cat->GetData(name, msg);
                if (res != B_NAME_NOT_FOUND && res != EOPNOTSUPP)
                        return res;     // return B_OK if found, or specific 
error-code
        }
+
        return B_NAME_NOT_FOUND;
 }
 
 
 status_t
-BCatalog::GetData(uint32 id, BMessage *msg)
+BCatalog::GetData(uint32 id, BMessage* msg)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        status_t res;
        for (BCatalogAddOn* cat = fCatalog; cat != NULL; cat = cat->fNext) {
                res = cat->GetData(id, msg);
                if (res != B_NAME_NOT_FOUND && res != EOPNOTSUPP)
                        return res;     // return B_OK if found, or specific 
error-code
        }
+
        return B_NAME_NOT_FOUND;
 }
 
 
 status_t
-BCatalog::SetCatalog(const entry_ref &catalogOwner, uint32 fingerprint)
+BCatalog::GetSignature(BString* sig)
+{
+       if (sig == NULL)
+               return B_BAD_VALUE;
+
+       if (fCatalog == NULL)
+               return B_NO_INIT;
+
+       *sig = fCatalog->fSignature;
+
+       return B_OK;
+}
+
+
+status_t
+BCatalog::GetLanguage(BString* lang)
+{
+       if (lang == NULL)
+               return B_BAD_VALUE;
+
+       if (fCatalog == NULL)
+               return B_NO_INIT;
+
+       *lang = fCatalog->fLanguageName;
+
+       return B_OK;
+}
+
+
+status_t
+BCatalog::GetFingerprint(uint32* fp)
+{
+       if (fp == NULL)
+               return B_BAD_VALUE;
+
+       if (fCatalog == NULL)
+               return B_NO_INIT;
+
+       *fp = fCatalog->fFingerprint;
+
+       return B_OK;
+}
+
+
+status_t
+BCatalog::SetCatalog(const entry_ref& catalogOwner, uint32 fingerprint)
 {
        // This is not thread safe. It is used only in ReadOnlyBootPrompt and 
should
        // not do harm there, but not sure what to do about it…
@@ -110,8 +161,22 @@ BCatalog::SetCatalog(const entry_ref &catalogOwner, uint32 
fingerprint)
 }
 
 
+status_t
+BCatalog::InitCheck() const
+{
+       return fCatalog != NULL ? fCatalog->InitCheck() : B_NO_INIT;
+}
+
+
+int32
+BCatalog::CountItems() const
+{
+       return fCatalog != NULL ? fCatalog->CountItems() : 0;
+}
+
+
 //#pragma mark - BCatalogAddOn
-BCatalogAddOn::BCatalogAddOn(const char *signature, const char *language,
+BCatalogAddOn::BCatalogAddOn(const char* signature, const char* language,
        uint32 fingerprint)
        :
        fInitCheck(B_NO_INIT),
@@ -154,29 +219,29 @@ BCatalogAddOn::CanHaveData() const
 
 
 status_t
-BCatalogAddOn::GetData(const char *name, BMessage *msg)
+BCatalogAddOn::GetData(const char* name, BMessage* msg)
 {
        return EOPNOTSUPP;
 }
 
 
 status_t
-BCatalogAddOn::GetData(uint32 id, BMessage *msg)
+BCatalogAddOn::GetData(uint32 id, BMessage* msg)
 {
        return EOPNOTSUPP;
 }
 
 
 status_t
-BCatalogAddOn::SetString(const char *string, const char *translated,
-       const char *context, const char *comment)
+BCatalogAddOn::SetString(const char* string, const char* translated,
+       const char* context, const char* comment)
 {
        return EOPNOTSUPP;
 }
 
 
 status_t
-BCatalogAddOn::SetString(int32 id, const char *translated)
+BCatalogAddOn::SetString(int32 id, const char* translated)
 {
        return EOPNOTSUPP;
 }
@@ -190,56 +255,56 @@ BCatalogAddOn::CanWriteData() const
 
 
 status_t
-BCatalogAddOn::SetData(const char *name, BMessage *msg)
+BCatalogAddOn::SetData(const char* name, BMessage* msg)
 {
        return EOPNOTSUPP;
 }
 
 
 status_t
-BCatalogAddOn::SetData(uint32 id, BMessage *msg)
+BCatalogAddOn::SetData(uint32 id, BMessage* msg)
 {
        return EOPNOTSUPP;
 }
 
 
 status_t
-BCatalogAddOn::ReadFromFile(const char *path)
+BCatalogAddOn::ReadFromFile(const char* path)
 {
        return EOPNOTSUPP;
 }
 
 
 status_t
-BCatalogAddOn::ReadFromAttribute(const entry_ref &appOrAddOnRef)
+BCatalogAddOn::ReadFromAttribute(const entry_ref& appOrAddOnRef)
 {
        return EOPNOTSUPP;
 }
 
 
 status_t
-BCatalogAddOn::ReadFromResource(const entry_ref &appOrAddOnRef)
+BCatalogAddOn::ReadFromResource(const entry_ref& appOrAddOnRef)
 {
        return EOPNOTSUPP;
 }
 
 
 status_t
-BCatalogAddOn::WriteToFile(const char *path)
+BCatalogAddOn::WriteToFile(const char* path)
 {
        return EOPNOTSUPP;
 }
 
 
 status_t
-BCatalogAddOn::WriteToAttribute(const entry_ref &appOrAddOnRef)
+BCatalogAddOn::WriteToAttribute(const entry_ref& appOrAddOnRef)
 {
        return EOPNOTSUPP;
 }
 
 
 status_t
-BCatalogAddOn::WriteToResource(const entry_ref &appOrAddOnRef)
+BCatalogAddOn::WriteToResource(const entry_ref& appOrAddOnRef)
 {
        return EOPNOTSUPP;
 }
@@ -258,7 +323,7 @@ BCatalogAddOn::CountItems() const
 
 
 void
-BCatalogAddOn::SetNext(BCatalogAddOn *next)
+BCatalogAddOn::SetNext(BCatalogAddOn* next)
 {
        fNext = next;
 }
@@ -266,8 +331,8 @@ BCatalogAddOn::SetNext(BCatalogAddOn *next)
 
 //#pragma mark - EditableCatalog
 namespace BPrivate {
-EditableCatalog::EditableCatalog(const char *type, const char *signature,
-       const char *language)
+EditableCatalog::EditableCatalog(const char* type, const char* signature,
+       const char* language)
 {
        fCatalog = MutableLocaleRoster::Default()->CreateCatalog(type, 
signature,
                language);
@@ -280,20 +345,22 @@ EditableCatalog::~EditableCatalog()
 
 
 status_t
-EditableCatalog::SetString(const char *string, const char *translated,
-       const char *context, const char *comment)
+EditableCatalog::SetString(const char* string, const char* translated,
+       const char* context, const char* comment)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        return fCatalog->SetString(string, translated, context, comment);
 }
 
 
 status_t
-EditableCatalog::SetString(int32 id, const char *translated)
+EditableCatalog::SetString(int32 id, const char* translated)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        return fCatalog->SetString(id, translated);
 }
 
@@ -301,87 +368,96 @@ EditableCatalog::SetString(int32 id, const char 
*translated)
 bool
 EditableCatalog::CanWriteData() const
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return false;
+
        return fCatalog->CanWriteData();
 }
 
 
 status_t
-EditableCatalog::SetData(const char *name, BMessage *msg)
+EditableCatalog::SetData(const char* name, BMessage* msg)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        return fCatalog->SetData(name, msg);
 }
 
 
 status_t
-EditableCatalog::SetData(uint32 id, BMessage *msg)
+EditableCatalog::SetData(uint32 id, BMessage* msg)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        return fCatalog->SetData(id, msg);
 }
 
 
 status_t
-EditableCatalog::ReadFromFile(const char *path)
+EditableCatalog::ReadFromFile(const char* path)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        return fCatalog->ReadFromFile(path);
 }
 
 
 status_t
-EditableCatalog::ReadFromAttribute(const entry_ref &appOrAddOnRef)
+EditableCatalog::ReadFromAttribute(const entry_ref& appOrAddOnRef)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        return fCatalog->ReadFromAttribute(appOrAddOnRef);
 }
 
 
 status_t
-EditableCatalog::ReadFromResource(const entry_ref &appOrAddOnRef)
+EditableCatalog::ReadFromResource(const entry_ref& appOrAddOnRef)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        return fCatalog->ReadFromResource(appOrAddOnRef);
 }
 
 
 status_t
-EditableCatalog::WriteToFile(const char *path)
+EditableCatalog::WriteToFile(const char* path)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        return fCatalog->WriteToFile(path);
 }
 
 
 status_t
-EditableCatalog::WriteToAttribute(const entry_ref &appOrAddOnRef)
+EditableCatalog::WriteToAttribute(const entry_ref& appOrAddOnRef)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        return fCatalog->WriteToAttribute(appOrAddOnRef);
 }
 
 
 status_t
-EditableCatalog::WriteToResource(const entry_ref &appOrAddOnRef)
+EditableCatalog::WriteToResource(const entry_ref& appOrAddOnRef)
 {
-       if (!fCatalog)
+       if (fCatalog == NULL)
                return B_NO_INIT;
+
        return fCatalog->WriteToResource(appOrAddOnRef);
 }
 
 
 void EditableCatalog::MakeEmpty()
 {
-       if (fCatalog)
+       if (fCatalog == NULL)
                fCatalog->MakeEmpty();
 }
 

############################################################################

Commit:      cc52f0df3a040ee5056f9bf781d7d30a676767e5
URL:         http://cgit.haiku-os.org/haiku/commit/?id=cc52f0d
Author:      Oliver Tappe <zooey@xxxxxxxxxxxxxxx>
Date:        Sat Apr 14 15:38:58 2012 UTC

Make BCatalog threadsafe.

* use a locker to protect the CatalogAddOn-chain against parallel
  access
* rename BCatalog::SetCatalog() to SetTo() and make it a proper
  initializing function
* adjust implementation of BLocaleRoster accordingly

----------------------------------------------------------------------------

diff --git a/headers/os/locale/Catalog.h b/headers/os/locale/Catalog.h
index c147f0c..bdf178b 100644
--- a/headers/os/locale/Catalog.h
+++ b/headers/os/locale/Catalog.h
@@ -1,5 +1,5 @@
 /*
- * Copyright 2003-2010, Haiku, Inc.
+ * Copyright 2003-2012, Haiku, Inc.
  * Distributed under the terms of the MIT License.
  */
 #ifndef _CATALOG_H_
@@ -7,6 +7,7 @@
 
 
 #include <LocaleRoster.h>
+#include <Locker.h>
 #include <SupportDefs.h>
 #include <String.h>
 
@@ -42,8 +43,9 @@ public:
                        status_t                        GetLanguage(BString* 
language);
                        status_t                        GetFingerprint(uint32* 
fingerprint);
 
-                       status_t                        SetCatalog(const 
entry_ref& catalogOwner,
-                                                                       uint32 
fingerprint);
+                       status_t                        SetTo(const entry_ref& 
catalogOwner,
+                                                                       const 
char* language = NULL,
+                                                                       uint32 
fingerprint = 0);
 
                        status_t                        InitCheck() const;
                        int32                           CountItems() const;
@@ -58,6 +60,7 @@ protected:
                                                                        // hide 
assignment and copy-constructor
 
                        BCatalogAddOn*          fCatalog;
+       mutable BLocker                         fLock;
 
 private:
        friend  class BLocale;
diff --git a/src/kits/locale/Catalog.cpp b/src/kits/locale/Catalog.cpp
index b7740d7..2d51a5b 100644
--- a/src/kits/locale/Catalog.cpp
+++ b/src/kits/locale/Catalog.cpp
@@ -1,6 +1,6 @@
 /*
  * Copyright 2003-2004, Axel Dörfler, axeld@xxxxxxxxxxxxxxxx
- * Copyright 2003-2004, Oliver Tappe, zooey@xxxxxxxxxxxxxxx
+ * Copyright 2003-2004,2012, Oliver Tappe, zooey@xxxxxxxxxxxxxxx
  * Distributed under the terms of the MIT License.
  */
 
@@ -9,6 +9,7 @@
 #include <syslog.h>
 
 #include <Application.h>
+#include <Autolock.h>
 #include <Locale.h>
 #include <MutableLocaleRoster.h>
 #include <Node.h>
@@ -21,16 +22,19 @@ using BPrivate::MutableLocaleRoster;
 //#pragma mark - BCatalog
 BCatalog::BCatalog()
        :
-       fCatalog(NULL)
+       fCatalog(NULL),
+       fLock("Catalog")
 {
 }
 
 
 BCatalog::BCatalog(const entry_ref& catalogOwner, const char* language,
        uint32 fingerprint)
+       :
+       fCatalog(NULL),
+       fLock("Catalog")
 {
-       fCatalog = MutableLocaleRoster::Default()->LoadCatalog(catalogOwner,
-               language, fingerprint);
+       SetTo(catalogOwner, language, fingerprint);
 }
 
 
@@ -44,6 +48,10 @@ const char*
 BCatalog::GetString(const char* string, const char* context,
        const char* comment)
 {
+       BAutolock lock(&fLock);
+       if (!lock.IsLocked())
+               return string;
+
        const char* translated;
        for (BCatalogAddOn* cat = fCatalog; cat != NULL; cat = cat->fNext) {
                translated = cat->GetString(string, context, comment);
@@ -58,6 +66,10 @@ BCatalog::GetString(const char* string, const char* context,
 const char*
 BCatalog::GetString(uint32 id)
 {
+       BAutolock lock(&fLock);
+       if (!lock.IsLocked())
+               return "";
+
        const char* translated;
        for (BCatalogAddOn* cat = fCatalog; cat != NULL; cat = cat->fNext) {
                translated = cat->GetString(id);
@@ -72,6 +84,10 @@ BCatalog::GetString(uint32 id)
 status_t
 BCatalog::GetData(const char* name, BMessage* msg)
 {
+       BAutolock lock(&fLock);
+       if (!lock.IsLocked())
+               return B_ERROR;
+
        if (fCatalog == NULL)
                return B_NO_INIT;
 
@@ -89,6 +105,10 @@ BCatalog::GetData(const char* name, BMessage* msg)
 status_t
 BCatalog::GetData(uint32 id, BMessage* msg)
 {
+       BAutolock lock(&fLock);
+       if (!lock.IsLocked())
+               return B_ERROR;
+
        if (fCatalog == NULL)
                return B_NO_INIT;
 
@@ -106,6 +126,10 @@ BCatalog::GetData(uint32 id, BMessage* msg)
 status_t
 BCatalog::GetSignature(BString* sig)
 {
+       BAutolock lock(&fLock);
+       if (!lock.IsLocked())
+               return B_ERROR;
+
        if (sig == NULL)
                return B_BAD_VALUE;
 
@@ -121,6 +145,10 @@ BCatalog::GetSignature(BString* sig)
 status_t
 BCatalog::GetLanguage(BString* lang)
 {
+       BAutolock lock(&fLock);
+       if (!lock.IsLocked())
+               return B_ERROR;
+
        if (lang == NULL)
                return B_BAD_VALUE;
 
@@ -136,6 +164,10 @@ BCatalog::GetLanguage(BString* lang)
 status_t
 BCatalog::GetFingerprint(uint32* fp)
 {
+       BAutolock lock(&fLock);
+       if (!lock.IsLocked())
+               return B_ERROR;
+
        if (fp == NULL)
                return B_BAD_VALUE;
 
@@ -149,13 +181,16 @@ BCatalog::GetFingerprint(uint32* fp)
 
 
 status_t
-BCatalog::SetCatalog(const entry_ref& catalogOwner, uint32 fingerprint)
+BCatalog::SetTo(const entry_ref& catalogOwner, const char* language,
+       uint32 fingerprint)
 {
-       // This is not thread safe. It is used only in ReadOnlyBootPrompt and 
should
-       // not do harm there, but not sure what to do about it…
+       BAutolock lock(&fLock);
+       if (!lock.IsLocked())
+               return B_ERROR;
+
        MutableLocaleRoster::Default()->UnloadCatalog(fCatalog);
-       fCatalog = MutableLocaleRoster::Default()->LoadCatalog(catalogOwner, 
NULL,
-               fingerprint);
+       fCatalog = MutableLocaleRoster::Default()->LoadCatalog(catalogOwner,
+               language, fingerprint);
 
        return B_OK;
 }
@@ -164,6 +199,10 @@ BCatalog::SetCatalog(const entry_ref& catalogOwner, uint32 
fingerprint)
 status_t
 BCatalog::InitCheck() const
 {
+       BAutolock lock(&fLock);
+       if (!lock.IsLocked())
+               return B_ERROR;
+
        return fCatalog != NULL ? fCatalog->InitCheck() : B_NO_INIT;
 }
 
@@ -171,6 +210,10 @@ BCatalog::InitCheck() const
 int32
 BCatalog::CountItems() const
 {
+       BAutolock lock(&fLock);
+       if (!lock.IsLocked())
+               return 0;
+
        return fCatalog != NULL ? fCatalog->CountItems() : 0;
 }
 
diff --git a/src/kits/locale/LocaleRoster.cpp b/src/kits/locale/LocaleRoster.cpp
index 3874e6b..2631f93 100644
--- a/src/kits/locale/LocaleRoster.cpp
+++ b/src/kits/locale/LocaleRoster.cpp
@@ -548,9 +548,8 @@ BLocaleRoster::_GetCatalog(BCatalog* catalog, vint32* 
catalogInitStatus)
 
        // load the catalog for this mimetype and return it to the app
        entry_ref ref;
-       BEntry(info.name).GetRef(&ref);
-       catalog->SetCatalog(ref, 0);
-       *catalogInitStatus = true;
+       if (BEntry(info.name).GetRef(&ref) == B_OK && catalog->SetTo(ref) == 
B_OK)
+               *catalogInitStatus = true;
 
        return catalog;
 }

############################################################################

Revision:    hrev44024
Commit:      eaa5e0936d94e7d0bb2e6066a87d485e7613414c
URL:         http://cgit.haiku-os.org/haiku/commit/?id=eaa5e09
Author:      Oliver Tappe <zooey@xxxxxxxxxxxxxxx>
Date:        Sat Apr 14 15:58:58 2012 UTC

Ticket:      https://dev.haiku-os.org/ticket/8452

Fix #8452 (app's crashing for non-existent system catalog)

* make the system catalog a BCatalog instead of a BCatalogAddOn*,
  such that using a non-existing system catalog won't crash but
  simply return the untranslated string instead
* rename MutableLocaleRoster::GetSystemCatalog() to LoadSystemCatalog()
  and adjust it to use BCatalog::SetTo() in order to replace the
  data used by the given catalog
* adjust all users of gSystemCatalog accordingly

----------------------------------------------------------------------------

diff --git a/headers/private/locale/MutableLocaleRoster.h 
b/headers/private/locale/MutableLocaleRoster.h
index 1027fce..44dec12 100644
--- a/headers/private/locale/MutableLocaleRoster.h
+++ b/headers/private/locale/MutableLocaleRoster.h
@@ -46,7 +46,7 @@ public:
                                                                        // 
contain the language-name(s)
                        status_t                        
SetFilesystemTranslationPreferred(bool preferred);
 
-                       status_t                        
GetSystemCatalog(BCatalogAddOn** catalog) const;
+                       status_t                        
LoadSystemCatalog(BCatalog* catalog) const;
 
                        BCatalogAddOn*          LoadCatalog(const entry_ref& 
catalogOwner,
                                                                        const 
char* language = NULL,
@@ -109,7 +109,7 @@ struct RosterData {
 
                        BLocale                         fDefaultLocale;
                        BTimeZone                       fDefaultTimeZone;
-                       
+
                        bool                            
fIsFilesystemTranslationPreferred;
 
                        bool                            fAreResourcesLoaded;
diff --git a/headers/private/locale/SystemCatalog.h 
b/headers/private/locale/SystemCatalog.h
index ef0708e..b70fd4d 100644
--- a/headers/private/locale/SystemCatalog.h
+++ b/headers/private/locale/SystemCatalog.h
@@ -10,7 +10,7 @@
 
 namespace BPrivate {
 
-extern BCatalogAddOn* gSystemCatalog;
+extern BCatalog gSystemCatalog;
 
 }      // namespace BPrivate
 
diff --git a/src/kits/interface/ColorControl.cpp 
b/src/kits/interface/ColorControl.cpp
index 47bf460..919c5be 100644
--- a/src/kits/interface/ColorControl.cpp
+++ b/src/kits/interface/ColorControl.cpp
@@ -90,9 +90,9 @@ BColorControl::_InitData(color_control_layout layout, float 
size,
        const char* red = B_TRANSLATE_MARK("Red:");
        const char* green = B_TRANSLATE_MARK("Green:");
        const char* blue = B_TRANSLATE_MARK("Blue:");
-       red = gSystemCatalog->GetString(red, "ColorControl");
-       green = gSystemCatalog->GetString(green, "ColorControl");
-       blue = gSystemCatalog->GetString(blue, "ColorControl");
+       red = gSystemCatalog.GetString(red, "ColorControl");
+       green = gSystemCatalog.GetString(green, "ColorControl");
+       blue = gSystemCatalog.GetString(blue, "ColorControl");
 
        if (archive) {
                fRedText = (BTextControl*)FindView("_red");
diff --git a/src/kits/interface/Dragger.cpp b/src/kits/interface/Dragger.cpp
index 374b312..fa50202 100644
--- a/src/kits/interface/Dragger.cpp
+++ b/src/kits/interface/Dragger.cpp
@@ -43,7 +43,7 @@ using BPrivate::gSystemCatalog;
 
 #undef B_TRANSLATE
 #define B_TRANSLATE(str) \
-       gSystemCatalog->GetString(B_TRANSLATE_MARK(str), "Dragger")
+       gSystemCatalog.GetString(B_TRANSLATE_MARK(str), "Dragger")
 
 const uint32 kMsgDragStarted = 'Drgs';
 
diff --git a/src/kits/interface/Menu.cpp b/src/kits/interface/Menu.cpp
index 9055ce7..d5760de 100644
--- a/src/kits/interface/Menu.cpp
+++ b/src/kits/interface/Menu.cpp
@@ -52,7 +52,7 @@ using BPrivate::gSystemCatalog;
 
 #undef B_TRANSLATE
 #define B_TRANSLATE(str) \
-       gSystemCatalog->GetString(B_TRANSLATE_MARK(str), "Menu")
+       gSystemCatalog.GetString(B_TRANSLATE_MARK(str), "Menu")
 
 
 using std::nothrow;
diff --git a/src/kits/interface/PrintJob.cpp b/src/kits/interface/PrintJob.cpp
index 6a884f9..59c9543 100644
--- a/src/kits/interface/PrintJob.cpp
+++ b/src/kits/interface/PrintJob.cpp
@@ -42,7 +42,7 @@ using BPrivate::gSystemCatalog;
 
 #undef B_TRANSLATE
 #define B_TRANSLATE(str) \
-       gSystemCatalog->GetString(B_TRANSLATE_MARK(str), "PrintJob")
+       gSystemCatalog.GetString(B_TRANSLATE_MARK(str), "PrintJob")
 
 
 /*!    Summary of spool file:
diff --git a/src/kits/interface/TextView.cpp b/src/kits/interface/TextView.cpp
index b933443..9d14a84 100644
--- a/src/kits/interface/TextView.cpp
+++ b/src/kits/interface/TextView.cpp
@@ -65,7 +65,7 @@ using BPrivate::gSystemCatalog;
 
 
 #define TRANSLATE(str) \
-       gSystemCatalog->GetString(B_TRANSLATE_MARK(str), "TextView")
+       gSystemCatalog.GetString(B_TRANSLATE_MARK(str), "TextView")
 
 #undef TRACE
 #undef CALLED
diff --git a/src/kits/interface/ZombieReplicantView.cpp 
b/src/kits/interface/ZombieReplicantView.cpp
index de90844..4269009 100644
--- a/src/kits/interface/ZombieReplicantView.cpp
+++ b/src/kits/interface/ZombieReplicantView.cpp
@@ -26,7 +26,7 @@ using BPrivate::gSystemCatalog;
 
 #undef B_TRANSLATE
 #define B_TRANSLATE(str) \
-       gSystemCatalog->GetString(B_TRANSLATE_MARK(str), "ZombieReplicantView")
+       gSystemCatalog.GetString(B_TRANSLATE_MARK(str), "ZombieReplicantView")
 
 
 _BZombieReplicantView_::_BZombieReplicantView_(BRect frame, status_t error)
diff --git a/src/kits/locale/InitLocaleKit.cpp 
b/src/kits/locale/InitLocaleKit.cpp
index ac24684..3092092 100644
--- a/src/kits/locale/InitLocaleKit.cpp
+++ b/src/kits/locale/InitLocaleKit.cpp
@@ -17,7 +17,7 @@
 
 namespace BPrivate {
 
-BCatalogAddOn* gSystemCatalog;
+BCatalog gSystemCatalog;
 
 }
 
@@ -129,5 +129,6 @@ __initialize_locale_kit()
 {
        SetupCatalogBasics();
 
-       
MutableLocaleRoster::Default()->GetSystemCatalog(&BPrivate::gSystemCatalog);
+       MutableLocaleRoster::Default()->LoadSystemCatalog(
+               &BPrivate::gSystemCatalog);
 }
diff --git a/src/kits/locale/MutableLocaleRoster.cpp 
b/src/kits/locale/MutableLocaleRoster.cpp
index aa189b6..e2b3ca0 100644
--- a/src/kits/locale/MutableLocaleRoster.cpp
+++ b/src/kits/locale/MutableLocaleRoster.cpp
@@ -775,7 +775,7 @@ MutableLocaleRoster::SetFilesystemTranslationPreferred(bool 
preferred)
 
 
 status_t
-MutableLocaleRoster::GetSystemCatalog(BCatalogAddOn** catalog) const
+MutableLocaleRoster::LoadSystemCatalog(BCatalog* catalog) const
 {
        if (!catalog)
                return B_BAD_VALUE;
@@ -799,13 +799,13 @@ MutableLocaleRoster::GetSystemCatalog(BCatalogAddOn** 
catalog) const
                return B_ERROR;
        }
 
-       // load the catalog for libbe and return it to the app
+       // load the catalog for libbe into the given catalog
        entry_ref ref;
-       BEntry(info.name).GetRef(&ref);
+       status_t status = BEntry(info.name).GetRef(&ref);
+       if (status != B_OK)
+               return status;
 
-       *catalog = LoadCatalog(ref);
-
-       return B_OK;
+       return catalog->SetTo(ref);
 }
 
 
@@ -859,8 +859,8 @@ MutableLocaleRoster::CreateCatalog(const char* type, const 
char* signature,
  * NULL is returned if no matching catalog could be found.
  */
 BCatalogAddOn*
-MutableLocaleRoster::LoadCatalog(const entry_ref& catalogOwner, const char* 
language,
-       int32 fingerprint) const
+MutableLocaleRoster::LoadCatalog(const entry_ref& catalogOwner,
+       const char* language, int32 fingerprint) const
 {
        BAutolock lock(RosterData::Default()->fLock);
        if (!lock.IsLocked())
diff --git a/src/kits/shared/AboutMenuItem.cpp 
b/src/kits/shared/AboutMenuItem.cpp
index 3b4f45f..bfda1b9 100644
--- a/src/kits/shared/AboutMenuItem.cpp
+++ b/src/kits/shared/AboutMenuItem.cpp
@@ -31,7 +31,7 @@ BAboutMenuItem::BAboutMenuItem()
                name = B_TRANSLATE_NOCOLLECT_SYSTEM_NAME(info.ref.name);
 
        const char* string = B_TRANSLATE_MARK("About %app%");
-       string = gSystemCatalog->GetString(string, "AboutMenuItem");
+       string = gSystemCatalog.GetString(string, "AboutMenuItem");
 
        BString label = string;
        if (name != NULL)
diff --git a/src/kits/shared/AboutWindow.cpp b/src/kits/shared/AboutWindow.cpp
index 6c84a0f..ed0c53f 100644
--- a/src/kits/shared/AboutWindow.cpp
+++ b/src/kits/shared/AboutWindow.cpp
@@ -32,8 +32,8 @@ BAboutWindow::BAboutWindow(const char *appName, int32 
firstCopyrightYear,
        const char* copyright = B_TRANSLATE_MARK("Copyright " B_UTF8_COPYRIGHT
                " %years% Haiku, Inc.");
        const char* writtenBy = B_TRANSLATE_MARK("Written by:");
-       copyright = gSystemCatalog->GetString(copyright, "AboutWindow");
-       writtenBy = gSystemCatalog->GetString(writtenBy, "AboutWindow");
+       copyright = gSystemCatalog.GetString(copyright, "AboutWindow");
+       writtenBy = gSystemCatalog.GetString(writtenBy, "AboutWindow");
 
        // Get current year
        time_t tp;
@@ -77,8 +77,8 @@ BAboutWindow::Show()
 {
        const char* aboutTitle = B_TRANSLATE_MARK("About" B_UTF8_ELLIPSIS);
        const char* closeLabel = B_TRANSLATE_MARK("Close");
-       aboutTitle = gSystemCatalog->GetString(aboutTitle, "AboutWindow");
-       closeLabel = gSystemCatalog->GetString(closeLabel, "AboutWindow");
+       aboutTitle = gSystemCatalog.GetString(aboutTitle, "AboutWindow");
+       closeLabel = gSystemCatalog.GetString(closeLabel, "AboutWindow");
 
        BAlert *alert = new BAlert(aboutTitle, fText->String(), closeLabel);
        BTextView *view = alert->TextView();
diff --git a/src/kits/shared/StringForSize.cpp 
b/src/kits/shared/StringForSize.cpp
index 2517203..40a89a7 100644
--- a/src/kits/shared/StringForSize.cpp
+++ b/src/kits/shared/StringForSize.cpp
@@ -25,33 +25,33 @@ string_for_size(double size, char* string, size_t 
stringSize)
        double kib = size / 1024.0;
        if (kib < 1.0) {
                const char* trKey = B_TRANSLATE_MARK("%d bytes");
-               snprintf(string, stringSize, gSystemCatalog->GetString(trKey,
+               snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
                        B_TRANSLATE_CONTEXT), (int)size);
                return string;
        }
        double mib = kib / 1024.0;
        if (mib < 1.0) {
                const char* trKey = B_TRANSLATE_MARK("%3.2f KiB");
-               snprintf(string, stringSize, gSystemCatalog->GetString(trKey,
+               snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
                        B_TRANSLATE_CONTEXT), kib);
                return string;
        }
        double gib = mib / 1024.0;
        if (gib < 1.0) {
                const char* trKey = B_TRANSLATE_MARK("%3.2f MiB");
-               snprintf(string, stringSize, gSystemCatalog->GetString(trKey,
+               snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
                        B_TRANSLATE_CONTEXT), mib);
                return string;
        }
        double tib = gib / 1024.0;
        if (tib < 1.0) {
                const char* trKey = B_TRANSLATE_MARK("%3.2f GiB");
-               snprintf(string, stringSize, gSystemCatalog->GetString(trKey,
+               snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
                        B_TRANSLATE_CONTEXT), gib);
                return string;
        }
        const char* trKey = B_TRANSLATE_MARK("%.2f TiB");
-       snprintf(string, stringSize, gSystemCatalog->GetString(trKey,
+       snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
                B_TRANSLATE_CONTEXT), tib);
        return string;
 }


Other related posts:

  • » [haiku-commits] haiku: hrev44024 - src/kits/locale headers/os/locale src/kits/shared src/kits/interface - zooey