From 1a5c1f9ed57fae37829befc007cd51c32205c1cd Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Tue, 1 Nov 2011 10:13:34 +0000 Subject: [PATCH] * Use const references instead of pointers for the read from/write to attribute/resource method in locale kit catalogs * Only load the embedded catalog if nothing else was found, so it can easily be overridden * Change the resource type to 'CADA' (CAtalog DAta) for embedded catalogs, and use a hash of the language code as the resource ID. This allows multiple languages to be stored in the same file and does not interfere with the user storing his own BMessages as resources. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@43057 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/os/locale/Catalog.h | 24 ++++--- headers/private/locale/DefaultCatalog.h | 8 +-- headers/private/locale/HashMapCatalog.h | 8 +-- src/bin/locale/linkcatkeys.cpp | 4 +- src/kits/locale/Catalog.cpp | 16 ++--- src/kits/locale/DefaultCatalog.cpp | 84 +++++++++++++------------ src/tools/locale/Catalog.cpp | 8 +-- src/tools/locale/DefaultCatalog.cpp | 10 +-- src/tools/locale/linkcatkeys.cpp | 4 +- 9 files changed, 88 insertions(+), 78 deletions(-) diff --git a/headers/os/locale/Catalog.h b/headers/os/locale/Catalog.h index 11cb6368fc..ce8324dc74 100644 --- a/headers/os/locale/Catalog.h +++ b/headers/os/locale/Catalog.h @@ -305,11 +305,15 @@ public: virtual status_t SetData(uint32 id, BMessage* msg); virtual status_t ReadFromFile(const char* path = NULL); - virtual status_t ReadFromAttribute(entry_ref* appOrAddOnRef); - virtual status_t ReadFromResource(entry_ref* appOrAddOnRef); + virtual status_t ReadFromAttribute( + const entry_ref& appOrAddOnRef); + virtual status_t ReadFromResource( + const entry_ref& appOrAddOnRef); virtual status_t WriteToFile(const char* path = NULL); - virtual status_t WriteToAttribute(entry_ref* appOrAddOnRef); - virtual status_t WriteToResource(entry_ref* appOrAddOnRef); + virtual status_t WriteToAttribute( + const entry_ref& appOrAddOnRef); + virtual status_t WriteToResource( + const entry_ref& appOrAddOnRef); virtual void MakeEmpty(); virtual int32 CountItems() const; @@ -475,11 +479,15 @@ public: status_t SetData(uint32 id, BMessage* msg); status_t ReadFromFile(const char* path = NULL); - status_t ReadFromAttribute(entry_ref* appOrAddOnRef); - status_t ReadFromResource(entry_ref* appOrAddOnRef); + status_t ReadFromAttribute( + const entry_ref& appOrAddOnRef); + status_t ReadFromResource( + const entry_ref& appOrAddOnRef); status_t WriteToFile(const char* path = NULL); - status_t WriteToAttribute(entry_ref* appOrAddOnRef); - status_t WriteToResource(entry_ref* appOrAddOnRef); + status_t WriteToAttribute( + const entry_ref& appOrAddOnRef); + status_t WriteToResource( + const entry_ref& appOrAddOnRef); void MakeEmpty(); diff --git a/headers/private/locale/DefaultCatalog.h b/headers/private/locale/DefaultCatalog.h index a79bd31321..be519d3780 100644 --- a/headers/private/locale/DefaultCatalog.h +++ b/headers/private/locale/DefaultCatalog.h @@ -38,11 +38,11 @@ class DefaultCatalog : public BHashMapCatalog { // implementation for editor-interface: status_t ReadFromFile(const char *path = NULL); - status_t ReadFromAttribute(entry_ref *appOrAddOnRef); - status_t ReadFromResource(entry_ref *appOrAddOnRef); + status_t ReadFromAttribute(const entry_ref &appOrAddOnRef); + status_t ReadFromResource(const entry_ref &appOrAddOnRef); status_t WriteToFile(const char *path = NULL); - status_t WriteToAttribute(entry_ref *appOrAddOnRef); - status_t WriteToResource(entry_ref *appOrAddOnRef); + status_t WriteToAttribute(const entry_ref &appOrAddOnRef); + status_t WriteToResource(const entry_ref &appOrAddOnRef); status_t SetRawString(const CatKey& key, const char *translated); void SetSignature(const entry_ref &catalogOwner); diff --git a/headers/private/locale/HashMapCatalog.h b/headers/private/locale/HashMapCatalog.h index 930ac646ba..fd5b27fdee 100644 --- a/headers/private/locale/HashMapCatalog.h +++ b/headers/private/locale/HashMapCatalog.h @@ -90,15 +90,15 @@ class BHashMapCatalog: public BCatalogAddOn { // implementation for editor-interface virtual status_t ReadFromFile(const char *path = NULL) {return B_NOT_SUPPORTED;} - virtual status_t ReadFromAttribute(entry_ref *appOrAddOnRef) + virtual status_t ReadFromAttribute(const entry_ref &appOrAddOnRef) {return B_NOT_SUPPORTED;} - virtual status_t ReadFromResource(entry_ref *appOrAddOnRef) + virtual status_t ReadFromResource(const entry_ref &appOrAddOnRef) {return B_NOT_SUPPORTED;} virtual status_t WriteToFile(const char *path = NULL) {return B_NOT_SUPPORTED;} - virtual status_t WriteToAttribute(entry_ref *appOrAddOnRef) + virtual status_t WriteToAttribute(const entry_ref &appOrAddOnRef) {return B_NOT_SUPPORTED;} - virtual status_t WriteToResource(entry_ref *appOrAddOnRef) + virtual status_t WriteToResource(const entry_ref &appOrAddOnRef) {return B_NOT_SUPPORTED;} void UpdateFingerprint(); diff --git a/src/bin/locale/linkcatkeys.cpp b/src/bin/locale/linkcatkeys.cpp index f9407ec003..c762362888 100644 --- a/src/bin/locale/linkcatkeys.cpp +++ b/src/bin/locale/linkcatkeys.cpp @@ -139,7 +139,7 @@ main(int argc, char **argv) BEntry entry(outputFile.String()); entry_ref eref; entry.GetRef(&eref); - res = targetCatalog.WriteToAttribute(&eref); + res = targetCatalog.WriteToAttribute(eref); if (res != B_OK) { fprintf(stderr, "couldn't write target-attribute to %s - error: %s\n", @@ -152,7 +152,7 @@ main(int argc, char **argv) BEntry entry(outputFile.String()); entry_ref eref; entry.GetRef(&eref); - res = targetCatalog.WriteToResource(&eref); + res = targetCatalog.WriteToResource(eref); if (res != B_OK) { fprintf(stderr, "couldn't write target-resource to %s - error: %s\n", diff --git a/src/kits/locale/Catalog.cpp b/src/kits/locale/Catalog.cpp index 7e4a0b2d1d..d22f98697f 100644 --- a/src/kits/locale/Catalog.cpp +++ b/src/kits/locale/Catalog.cpp @@ -211,14 +211,14 @@ BCatalogAddOn::ReadFromFile(const char *path) status_t -BCatalogAddOn::ReadFromAttribute(entry_ref *appOrAddOnRef) +BCatalogAddOn::ReadFromAttribute(const entry_ref &appOrAddOnRef) { return EOPNOTSUPP; } status_t -BCatalogAddOn::ReadFromResource(entry_ref *appOrAddOnRef) +BCatalogAddOn::ReadFromResource(const entry_ref &appOrAddOnRef) { return EOPNOTSUPP; } @@ -232,14 +232,14 @@ BCatalogAddOn::WriteToFile(const char *path) status_t -BCatalogAddOn::WriteToAttribute(entry_ref *appOrAddOnRef) +BCatalogAddOn::WriteToAttribute(const entry_ref &appOrAddOnRef) { return EOPNOTSUPP; } status_t -BCatalogAddOn::WriteToResource(entry_ref *appOrAddOnRef) +BCatalogAddOn::WriteToResource(const entry_ref &appOrAddOnRef) { return EOPNOTSUPP; } @@ -335,7 +335,7 @@ EditableCatalog::ReadFromFile(const char *path) status_t -EditableCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef) +EditableCatalog::ReadFromAttribute(const entry_ref &appOrAddOnRef) { if (!fCatalog) return B_NO_INIT; @@ -344,7 +344,7 @@ EditableCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef) status_t -EditableCatalog::ReadFromResource(entry_ref *appOrAddOnRef) +EditableCatalog::ReadFromResource(const entry_ref &appOrAddOnRef) { if (!fCatalog) return B_NO_INIT; @@ -362,7 +362,7 @@ EditableCatalog::WriteToFile(const char *path) status_t -EditableCatalog::WriteToAttribute(entry_ref *appOrAddOnRef) +EditableCatalog::WriteToAttribute(const entry_ref &appOrAddOnRef) { if (!fCatalog) return B_NO_INIT; @@ -371,7 +371,7 @@ EditableCatalog::WriteToAttribute(entry_ref *appOrAddOnRef) status_t -EditableCatalog::WriteToResource(entry_ref *appOrAddOnRef) +EditableCatalog::WriteToResource(const entry_ref &appOrAddOnRef) { if (!fCatalog) return B_NO_INIT; diff --git a/src/kits/locale/DefaultCatalog.cpp b/src/kits/locale/DefaultCatalog.cpp index 8594bc03f5..a7acc96aa0 100644 --- a/src/kits/locale/DefaultCatalog.cpp +++ b/src/kits/locale/DefaultCatalog.cpp @@ -70,27 +70,18 @@ DefaultCatalog::DefaultCatalog(const entry_ref &catalogOwner, const char *langua SetSignature(catalogOwner); status_t status; - app_info appInfo; - be_app->GetAppInfo(&appInfo); - - // give highest priority to catalog embedded as resource in application - // executable: - status = ReadFromResource(&appInfo.ref); - // search for catalog living in sub-folder of app's folder: - if (status != B_OK) { - node_ref nref; - nref.device = appInfo.ref.device; - nref.node = appInfo.ref.directory; - BDirectory appDir(&nref); - BString catalogName("locale/"); - catalogName << kCatFolder - << "/" << fSignature - << "/" << fLanguageName - << kCatExtension; - BPath catalogPath(&appDir, catalogName.String()); - status = ReadFromFile(catalogPath.Path()); - } + node_ref nref; + nref.device = catalogOwner.device; + nref.node = catalogOwner.directory; + BDirectory appDir(&nref); + BString catalogName("locale/"); + catalogName << kCatFolder + << "/" << fSignature + << "/" << fLanguageName + << kCatExtension; + BPath catalogPath(&appDir, catalogName.String()); + status = ReadFromFile(catalogPath.Path()); if (status != B_OK) { // search in data folders @@ -116,6 +107,12 @@ DefaultCatalog::DefaultCatalog(const entry_ref &catalogOwner, const char *langua } } + if (status != B_OK) { + // give lowest priority to catalog embedded as resource in application + // executable, so they can be overridden easily. + status = ReadFromResource(catalogOwner); + } + fInitCheck = status; log_team(LOG_DEBUG, "trying to load default-catalog(sig=%s, lang=%s) results in %s", @@ -132,7 +129,7 @@ DefaultCatalog::DefaultCatalog(entry_ref *appOrAddOnRef) : BHashMapCatalog("", "", 0) { - fInitCheck = ReadFromResource(appOrAddOnRef); + fInitCheck = ReadFromResource(*appOrAddOnRef); log_team(LOG_DEBUG, "trying to load embedded catalog from resources results in %s", strerror(fInitCheck)); @@ -255,22 +252,22 @@ DefaultCatalog::ReadFromFile(const char *path) * this method is not currently being used, but it may be useful in the future... */ status_t -DefaultCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef) +DefaultCatalog::ReadFromAttribute(const entry_ref &appOrAddOnRef) { BNode node; - status_t res = node.SetTo(appOrAddOnRef); + status_t res = node.SetTo(&appOrAddOnRef); if (res != B_OK) { log_team(LOG_ERR, "couldn't find app or add-on (dev=%lu, dir=%Lu, name=%s)", - appOrAddOnRef->device, appOrAddOnRef->directory, - appOrAddOnRef->name); + appOrAddOnRef.device, appOrAddOnRef.directory, + appOrAddOnRef.name); return B_ENTRY_NOT_FOUND; } log_team(LOG_DEBUG, "looking for embedded catalog-attribute in app/add-on" - "(dev=%lu, dir=%Lu, name=%s)", appOrAddOnRef->device, - appOrAddOnRef->directory, appOrAddOnRef->name); + "(dev=%lu, dir=%Lu, name=%s)", appOrAddOnRef.device, + appOrAddOnRef.directory, appOrAddOnRef.name); attr_info attrInfo; res = node.GetAttrInfo(BLocaleRoster::kEmbeddedCatAttr, &attrInfo); @@ -305,22 +302,22 @@ DefaultCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef) status_t -DefaultCatalog::ReadFromResource(entry_ref *appOrAddOnRef) +DefaultCatalog::ReadFromResource(const entry_ref &appOrAddOnRef) { BFile file; - status_t res = file.SetTo(appOrAddOnRef, B_READ_ONLY); + status_t res = file.SetTo(&appOrAddOnRef, B_READ_ONLY); if (res != B_OK) { log_team(LOG_ERR, "couldn't find app or add-on (dev=%lu, dir=%Lu, name=%s)", - appOrAddOnRef->device, appOrAddOnRef->directory, - appOrAddOnRef->name); + appOrAddOnRef.device, appOrAddOnRef.directory, + appOrAddOnRef.name); return B_ENTRY_NOT_FOUND; } log_team(LOG_DEBUG, "looking for embedded catalog-resource in app/add-on" - "(dev=%lu, dir=%Lu, name=%s)", appOrAddOnRef->device, - appOrAddOnRef->directory, appOrAddOnRef->name); + "(dev=%lu, dir=%Lu, name=%s)", appOrAddOnRef.device, + appOrAddOnRef.directory, appOrAddOnRef.name); BResources rsrc; res = rsrc.SetTo(&file); @@ -329,9 +326,11 @@ DefaultCatalog::ReadFromResource(entry_ref *appOrAddOnRef) return res; } + int mangledLanguage = CatKey::HashFun(fLanguageName.String(), 0); + size_t sz; - const void *buf = rsrc.LoadResource(B_MESSAGE_TYPE, - BLocaleRoster::kEmbeddedCatResId, &sz); + const void *buf = rsrc.LoadResource('CADA', + mangledLanguage, &sz); if (!buf) { log_team(LOG_DEBUG, "file has no catalog-resource"); return B_NAME_NOT_FOUND; @@ -379,10 +378,10 @@ DefaultCatalog::WriteToFile(const char *path) * future... */ status_t -DefaultCatalog::WriteToAttribute(entry_ref *appOrAddOnRef) +DefaultCatalog::WriteToAttribute(const entry_ref &appOrAddOnRef) { BNode node; - status_t res = node.SetTo(appOrAddOnRef); + status_t res = node.SetTo(&appOrAddOnRef); if (res != B_OK) return res; @@ -405,10 +404,10 @@ DefaultCatalog::WriteToAttribute(entry_ref *appOrAddOnRef) status_t -DefaultCatalog::WriteToResource(entry_ref *appOrAddOnRef) +DefaultCatalog::WriteToResource(const entry_ref &appOrAddOnRef) { BFile file; - status_t res = file.SetTo(appOrAddOnRef, B_READ_WRITE); + status_t res = file.SetTo(&appOrAddOnRef, B_READ_WRITE); if (res != B_OK) return res; @@ -422,9 +421,12 @@ DefaultCatalog::WriteToResource(entry_ref *appOrAddOnRef) // set a largish block-size in order to avoid reallocs res = Flatten(&mallocIO); + int mangledLanguage = CatKey::HashFun(fLanguageName.String(), 0); + if (res == B_OK) { - res = rsrc.AddResource(B_MESSAGE_TYPE, BLocaleRoster::kEmbeddedCatResId, - mallocIO.Buffer(), mallocIO.BufferLength(), "embedded catalog"); + res = rsrc.AddResource('CADA', mangledLanguage, + mallocIO.Buffer(), mallocIO.BufferLength(), + BString(fLanguageName) << " catalog"); } return res; diff --git a/src/tools/locale/Catalog.cpp b/src/tools/locale/Catalog.cpp index 3c6ff02031..f576b53e4b 100644 --- a/src/tools/locale/Catalog.cpp +++ b/src/tools/locale/Catalog.cpp @@ -206,14 +206,14 @@ BCatalogAddOn::ReadFromFile(const char *path) status_t -BCatalogAddOn::ReadFromAttribute(entry_ref *appOrAddOnRef) +BCatalogAddOn::ReadFromAttribute(const entry_ref &appOrAddOnRef) { return EOPNOTSUPP; } status_t -BCatalogAddOn::ReadFromResource(entry_ref *appOrAddOnRef) +BCatalogAddOn::ReadFromResource(const entry_ref &appOrAddOnRef) { return EOPNOTSUPP; } @@ -227,14 +227,14 @@ BCatalogAddOn::WriteToFile(const char *path) status_t -BCatalogAddOn::WriteToAttribute(entry_ref *appOrAddOnRef) +BCatalogAddOn::WriteToAttribute(const entry_ref &appOrAddOnRef) { return EOPNOTSUPP; } status_t -BCatalogAddOn::WriteToResource(entry_ref *appOrAddOnRef) +BCatalogAddOn::WriteToResource(const entry_ref &appOrAddOnRef) { return EOPNOTSUPP; } diff --git a/src/tools/locale/DefaultCatalog.cpp b/src/tools/locale/DefaultCatalog.cpp index 86acdc6c84..e8fc2e0b10 100644 --- a/src/tools/locale/DefaultCatalog.cpp +++ b/src/tools/locale/DefaultCatalog.cpp @@ -82,7 +82,7 @@ DefaultCatalog::DefaultCatalog(entry_ref *appOrAddOnRef) : BHashMapCatalog("", "", 0) { - fInitCheck = ReadFromResource(appOrAddOnRef); + fInitCheck = ReadFromResource(*appOrAddOnRef); // fprintf(stderr, // "trying to load embedded catalog from resources results in %s", // strerror(fInitCheck)); @@ -180,14 +180,14 @@ DefaultCatalog::ReadFromFile(const char *path) future... */ status_t -DefaultCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef) +DefaultCatalog::ReadFromAttribute(const entry_ref &appOrAddOnRef) { return B_NOT_SUPPORTED; } status_t -DefaultCatalog::ReadFromResource(entry_ref *appOrAddOnRef) +DefaultCatalog::ReadFromResource(const entry_ref &appOrAddOnRef) { return B_NOT_SUPPORTED; } @@ -229,14 +229,14 @@ DefaultCatalog::WriteToFile(const char *path) future... */ status_t -DefaultCatalog::WriteToAttribute(entry_ref *appOrAddOnRef) +DefaultCatalog::WriteToAttribute(const entry_ref &appOrAddOnRef) { return B_NOT_SUPPORTED; } status_t -DefaultCatalog::WriteToResource(entry_ref *appOrAddOnRef) +DefaultCatalog::WriteToResource(const entry_ref &appOrAddOnRef) { return B_NOT_SUPPORTED; } diff --git a/src/tools/locale/linkcatkeys.cpp b/src/tools/locale/linkcatkeys.cpp index 51f731a747..41aaff0837 100644 --- a/src/tools/locale/linkcatkeys.cpp +++ b/src/tools/locale/linkcatkeys.cpp @@ -123,7 +123,7 @@ main(int argc, char **argv) BEntry entry(outputFile.String()); entry_ref eref; entry.GetRef(&eref); - res = targetCatImpl.WriteToAttribute(&eref); + res = targetCatImpl.WriteToAttribute(eref); if (res != B_OK) { fprintf(stderr, "couldn't write target-attribute to %s - error: %s\n", @@ -136,7 +136,7 @@ main(int argc, char **argv) BEntry entry(outputFile.String()); entry_ref eref; entry.GetRef(&eref); - res = targetCatImpl.WriteToResource(&eref); + res = targetCatImpl.WriteToResource(eref); if (res != B_OK) { fprintf(stderr, "couldn't write target-resource to %s - error: %s\n",