* 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
This commit is contained in:
Adrien Destugues
2011-11-01 10:13:34 +00:00
parent c87c47960c
commit 1a5c1f9ed5
9 changed files with 88 additions and 78 deletions
+16 -8
View File
@@ -305,11 +305,15 @@ public:
virtual status_t SetData(uint32 id, BMessage* msg); virtual status_t SetData(uint32 id, BMessage* msg);
virtual status_t ReadFromFile(const char* path = NULL); virtual status_t ReadFromFile(const char* path = NULL);
virtual status_t ReadFromAttribute(entry_ref* appOrAddOnRef); virtual status_t ReadFromAttribute(
virtual status_t ReadFromResource(entry_ref* appOrAddOnRef); const entry_ref& appOrAddOnRef);
virtual status_t ReadFromResource(
const entry_ref& appOrAddOnRef);
virtual status_t WriteToFile(const char* path = NULL); virtual status_t WriteToFile(const char* path = NULL);
virtual status_t WriteToAttribute(entry_ref* appOrAddOnRef); virtual status_t WriteToAttribute(
virtual status_t WriteToResource(entry_ref* appOrAddOnRef); const entry_ref& appOrAddOnRef);
virtual status_t WriteToResource(
const entry_ref& appOrAddOnRef);
virtual void MakeEmpty(); virtual void MakeEmpty();
virtual int32 CountItems() const; virtual int32 CountItems() const;
@@ -475,11 +479,15 @@ public:
status_t SetData(uint32 id, BMessage* msg); status_t SetData(uint32 id, BMessage* msg);
status_t ReadFromFile(const char* path = NULL); status_t ReadFromFile(const char* path = NULL);
status_t ReadFromAttribute(entry_ref* appOrAddOnRef); status_t ReadFromAttribute(
status_t ReadFromResource(entry_ref* appOrAddOnRef); const entry_ref& appOrAddOnRef);
status_t ReadFromResource(
const entry_ref& appOrAddOnRef);
status_t WriteToFile(const char* path = NULL); status_t WriteToFile(const char* path = NULL);
status_t WriteToAttribute(entry_ref* appOrAddOnRef); status_t WriteToAttribute(
status_t WriteToResource(entry_ref* appOrAddOnRef); const entry_ref& appOrAddOnRef);
status_t WriteToResource(
const entry_ref& appOrAddOnRef);
void MakeEmpty(); void MakeEmpty();
+4 -4
View File
@@ -38,11 +38,11 @@ class DefaultCatalog : public BHashMapCatalog {
// implementation for editor-interface: // implementation for editor-interface:
status_t ReadFromFile(const char *path = NULL); status_t ReadFromFile(const char *path = NULL);
status_t ReadFromAttribute(entry_ref *appOrAddOnRef); status_t ReadFromAttribute(const entry_ref &appOrAddOnRef);
status_t ReadFromResource(entry_ref *appOrAddOnRef); status_t ReadFromResource(const entry_ref &appOrAddOnRef);
status_t WriteToFile(const char *path = NULL); status_t WriteToFile(const char *path = NULL);
status_t WriteToAttribute(entry_ref *appOrAddOnRef); status_t WriteToAttribute(const entry_ref &appOrAddOnRef);
status_t WriteToResource(entry_ref *appOrAddOnRef); status_t WriteToResource(const entry_ref &appOrAddOnRef);
status_t SetRawString(const CatKey& key, const char *translated); status_t SetRawString(const CatKey& key, const char *translated);
void SetSignature(const entry_ref &catalogOwner); void SetSignature(const entry_ref &catalogOwner);
+4 -4
View File
@@ -90,15 +90,15 @@ class BHashMapCatalog: public BCatalogAddOn {
// implementation for editor-interface // implementation for editor-interface
virtual status_t ReadFromFile(const char *path = NULL) virtual status_t ReadFromFile(const char *path = NULL)
{return B_NOT_SUPPORTED;} {return B_NOT_SUPPORTED;}
virtual status_t ReadFromAttribute(entry_ref *appOrAddOnRef) virtual status_t ReadFromAttribute(const entry_ref &appOrAddOnRef)
{return B_NOT_SUPPORTED;} {return B_NOT_SUPPORTED;}
virtual status_t ReadFromResource(entry_ref *appOrAddOnRef) virtual status_t ReadFromResource(const entry_ref &appOrAddOnRef)
{return B_NOT_SUPPORTED;} {return B_NOT_SUPPORTED;}
virtual status_t WriteToFile(const char *path = NULL) virtual status_t WriteToFile(const char *path = NULL)
{return B_NOT_SUPPORTED;} {return B_NOT_SUPPORTED;}
virtual status_t WriteToAttribute(entry_ref *appOrAddOnRef) virtual status_t WriteToAttribute(const entry_ref &appOrAddOnRef)
{return B_NOT_SUPPORTED;} {return B_NOT_SUPPORTED;}
virtual status_t WriteToResource(entry_ref *appOrAddOnRef) virtual status_t WriteToResource(const entry_ref &appOrAddOnRef)
{return B_NOT_SUPPORTED;} {return B_NOT_SUPPORTED;}
void UpdateFingerprint(); void UpdateFingerprint();
+2 -2
View File
@@ -139,7 +139,7 @@ main(int argc, char **argv)
BEntry entry(outputFile.String()); BEntry entry(outputFile.String());
entry_ref eref; entry_ref eref;
entry.GetRef(&eref); entry.GetRef(&eref);
res = targetCatalog.WriteToAttribute(&eref); res = targetCatalog.WriteToAttribute(eref);
if (res != B_OK) { if (res != B_OK) {
fprintf(stderr, fprintf(stderr,
"couldn't write target-attribute to %s - error: %s\n", "couldn't write target-attribute to %s - error: %s\n",
@@ -152,7 +152,7 @@ main(int argc, char **argv)
BEntry entry(outputFile.String()); BEntry entry(outputFile.String());
entry_ref eref; entry_ref eref;
entry.GetRef(&eref); entry.GetRef(&eref);
res = targetCatalog.WriteToResource(&eref); res = targetCatalog.WriteToResource(eref);
if (res != B_OK) { if (res != B_OK) {
fprintf(stderr, fprintf(stderr,
"couldn't write target-resource to %s - error: %s\n", "couldn't write target-resource to %s - error: %s\n",
+8 -8
View File
@@ -211,14 +211,14 @@ BCatalogAddOn::ReadFromFile(const char *path)
status_t status_t
BCatalogAddOn::ReadFromAttribute(entry_ref *appOrAddOnRef) BCatalogAddOn::ReadFromAttribute(const entry_ref &appOrAddOnRef)
{ {
return EOPNOTSUPP; return EOPNOTSUPP;
} }
status_t status_t
BCatalogAddOn::ReadFromResource(entry_ref *appOrAddOnRef) BCatalogAddOn::ReadFromResource(const entry_ref &appOrAddOnRef)
{ {
return EOPNOTSUPP; return EOPNOTSUPP;
} }
@@ -232,14 +232,14 @@ BCatalogAddOn::WriteToFile(const char *path)
status_t status_t
BCatalogAddOn::WriteToAttribute(entry_ref *appOrAddOnRef) BCatalogAddOn::WriteToAttribute(const entry_ref &appOrAddOnRef)
{ {
return EOPNOTSUPP; return EOPNOTSUPP;
} }
status_t status_t
BCatalogAddOn::WriteToResource(entry_ref *appOrAddOnRef) BCatalogAddOn::WriteToResource(const entry_ref &appOrAddOnRef)
{ {
return EOPNOTSUPP; return EOPNOTSUPP;
} }
@@ -335,7 +335,7 @@ EditableCatalog::ReadFromFile(const char *path)
status_t status_t
EditableCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef) EditableCatalog::ReadFromAttribute(const entry_ref &appOrAddOnRef)
{ {
if (!fCatalog) if (!fCatalog)
return B_NO_INIT; return B_NO_INIT;
@@ -344,7 +344,7 @@ EditableCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef)
status_t status_t
EditableCatalog::ReadFromResource(entry_ref *appOrAddOnRef) EditableCatalog::ReadFromResource(const entry_ref &appOrAddOnRef)
{ {
if (!fCatalog) if (!fCatalog)
return B_NO_INIT; return B_NO_INIT;
@@ -362,7 +362,7 @@ EditableCatalog::WriteToFile(const char *path)
status_t status_t
EditableCatalog::WriteToAttribute(entry_ref *appOrAddOnRef) EditableCatalog::WriteToAttribute(const entry_ref &appOrAddOnRef)
{ {
if (!fCatalog) if (!fCatalog)
return B_NO_INIT; return B_NO_INIT;
@@ -371,7 +371,7 @@ EditableCatalog::WriteToAttribute(entry_ref *appOrAddOnRef)
status_t status_t
EditableCatalog::WriteToResource(entry_ref *appOrAddOnRef) EditableCatalog::WriteToResource(const entry_ref &appOrAddOnRef)
{ {
if (!fCatalog) if (!fCatalog)
return B_NO_INIT; return B_NO_INIT;
+43 -41
View File
@@ -70,27 +70,18 @@ DefaultCatalog::DefaultCatalog(const entry_ref &catalogOwner, const char *langua
SetSignature(catalogOwner); SetSignature(catalogOwner);
status_t status; 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: // search for catalog living in sub-folder of app's folder:
if (status != B_OK) { node_ref nref;
node_ref nref; nref.device = catalogOwner.device;
nref.device = appInfo.ref.device; nref.node = catalogOwner.directory;
nref.node = appInfo.ref.directory; BDirectory appDir(&nref);
BDirectory appDir(&nref); BString catalogName("locale/");
BString catalogName("locale/"); catalogName << kCatFolder
catalogName << kCatFolder << "/" << fSignature
<< "/" << fSignature << "/" << fLanguageName
<< "/" << fLanguageName << kCatExtension;
<< kCatExtension; BPath catalogPath(&appDir, catalogName.String());
BPath catalogPath(&appDir, catalogName.String()); status = ReadFromFile(catalogPath.Path());
status = ReadFromFile(catalogPath.Path());
}
if (status != B_OK) { if (status != B_OK) {
// search in data folders // 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; fInitCheck = status;
log_team(LOG_DEBUG, log_team(LOG_DEBUG,
"trying to load default-catalog(sig=%s, lang=%s) results in %s", "trying to load default-catalog(sig=%s, lang=%s) results in %s",
@@ -132,7 +129,7 @@ DefaultCatalog::DefaultCatalog(entry_ref *appOrAddOnRef)
: :
BHashMapCatalog("", "", 0) BHashMapCatalog("", "", 0)
{ {
fInitCheck = ReadFromResource(appOrAddOnRef); fInitCheck = ReadFromResource(*appOrAddOnRef);
log_team(LOG_DEBUG, log_team(LOG_DEBUG,
"trying to load embedded catalog from resources results in %s", "trying to load embedded catalog from resources results in %s",
strerror(fInitCheck)); 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... * this method is not currently being used, but it may be useful in the future...
*/ */
status_t status_t
DefaultCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef) DefaultCatalog::ReadFromAttribute(const entry_ref &appOrAddOnRef)
{ {
BNode node; BNode node;
status_t res = node.SetTo(appOrAddOnRef); status_t res = node.SetTo(&appOrAddOnRef);
if (res != B_OK) { if (res != B_OK) {
log_team(LOG_ERR, log_team(LOG_ERR,
"couldn't find app or add-on (dev=%lu, dir=%Lu, name=%s)", "couldn't find app or add-on (dev=%lu, dir=%Lu, name=%s)",
appOrAddOnRef->device, appOrAddOnRef->directory, appOrAddOnRef.device, appOrAddOnRef.directory,
appOrAddOnRef->name); appOrAddOnRef.name);
return B_ENTRY_NOT_FOUND; return B_ENTRY_NOT_FOUND;
} }
log_team(LOG_DEBUG, log_team(LOG_DEBUG,
"looking for embedded catalog-attribute in app/add-on" "looking for embedded catalog-attribute in app/add-on"
"(dev=%lu, dir=%Lu, name=%s)", appOrAddOnRef->device, "(dev=%lu, dir=%Lu, name=%s)", appOrAddOnRef.device,
appOrAddOnRef->directory, appOrAddOnRef->name); appOrAddOnRef.directory, appOrAddOnRef.name);
attr_info attrInfo; attr_info attrInfo;
res = node.GetAttrInfo(BLocaleRoster::kEmbeddedCatAttr, &attrInfo); res = node.GetAttrInfo(BLocaleRoster::kEmbeddedCatAttr, &attrInfo);
@@ -305,22 +302,22 @@ DefaultCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef)
status_t status_t
DefaultCatalog::ReadFromResource(entry_ref *appOrAddOnRef) DefaultCatalog::ReadFromResource(const entry_ref &appOrAddOnRef)
{ {
BFile file; BFile file;
status_t res = file.SetTo(appOrAddOnRef, B_READ_ONLY); status_t res = file.SetTo(&appOrAddOnRef, B_READ_ONLY);
if (res != B_OK) { if (res != B_OK) {
log_team(LOG_ERR, log_team(LOG_ERR,
"couldn't find app or add-on (dev=%lu, dir=%Lu, name=%s)", "couldn't find app or add-on (dev=%lu, dir=%Lu, name=%s)",
appOrAddOnRef->device, appOrAddOnRef->directory, appOrAddOnRef.device, appOrAddOnRef.directory,
appOrAddOnRef->name); appOrAddOnRef.name);
return B_ENTRY_NOT_FOUND; return B_ENTRY_NOT_FOUND;
} }
log_team(LOG_DEBUG, log_team(LOG_DEBUG,
"looking for embedded catalog-resource in app/add-on" "looking for embedded catalog-resource in app/add-on"
"(dev=%lu, dir=%Lu, name=%s)", appOrAddOnRef->device, "(dev=%lu, dir=%Lu, name=%s)", appOrAddOnRef.device,
appOrAddOnRef->directory, appOrAddOnRef->name); appOrAddOnRef.directory, appOrAddOnRef.name);
BResources rsrc; BResources rsrc;
res = rsrc.SetTo(&file); res = rsrc.SetTo(&file);
@@ -329,9 +326,11 @@ DefaultCatalog::ReadFromResource(entry_ref *appOrAddOnRef)
return res; return res;
} }
int mangledLanguage = CatKey::HashFun(fLanguageName.String(), 0);
size_t sz; size_t sz;
const void *buf = rsrc.LoadResource(B_MESSAGE_TYPE, const void *buf = rsrc.LoadResource('CADA',
BLocaleRoster::kEmbeddedCatResId, &sz); mangledLanguage, &sz);
if (!buf) { if (!buf) {
log_team(LOG_DEBUG, "file has no catalog-resource"); log_team(LOG_DEBUG, "file has no catalog-resource");
return B_NAME_NOT_FOUND; return B_NAME_NOT_FOUND;
@@ -379,10 +378,10 @@ DefaultCatalog::WriteToFile(const char *path)
* future... * future...
*/ */
status_t status_t
DefaultCatalog::WriteToAttribute(entry_ref *appOrAddOnRef) DefaultCatalog::WriteToAttribute(const entry_ref &appOrAddOnRef)
{ {
BNode node; BNode node;
status_t res = node.SetTo(appOrAddOnRef); status_t res = node.SetTo(&appOrAddOnRef);
if (res != B_OK) if (res != B_OK)
return res; return res;
@@ -405,10 +404,10 @@ DefaultCatalog::WriteToAttribute(entry_ref *appOrAddOnRef)
status_t status_t
DefaultCatalog::WriteToResource(entry_ref *appOrAddOnRef) DefaultCatalog::WriteToResource(const entry_ref &appOrAddOnRef)
{ {
BFile file; BFile file;
status_t res = file.SetTo(appOrAddOnRef, B_READ_WRITE); status_t res = file.SetTo(&appOrAddOnRef, B_READ_WRITE);
if (res != B_OK) if (res != B_OK)
return res; return res;
@@ -422,9 +421,12 @@ DefaultCatalog::WriteToResource(entry_ref *appOrAddOnRef)
// set a largish block-size in order to avoid reallocs // set a largish block-size in order to avoid reallocs
res = Flatten(&mallocIO); res = Flatten(&mallocIO);
int mangledLanguage = CatKey::HashFun(fLanguageName.String(), 0);
if (res == B_OK) { if (res == B_OK) {
res = rsrc.AddResource(B_MESSAGE_TYPE, BLocaleRoster::kEmbeddedCatResId, res = rsrc.AddResource('CADA', mangledLanguage,
mallocIO.Buffer(), mallocIO.BufferLength(), "embedded catalog"); mallocIO.Buffer(), mallocIO.BufferLength(),
BString(fLanguageName) << " catalog");
} }
return res; return res;
+4 -4
View File
@@ -206,14 +206,14 @@ BCatalogAddOn::ReadFromFile(const char *path)
status_t status_t
BCatalogAddOn::ReadFromAttribute(entry_ref *appOrAddOnRef) BCatalogAddOn::ReadFromAttribute(const entry_ref &appOrAddOnRef)
{ {
return EOPNOTSUPP; return EOPNOTSUPP;
} }
status_t status_t
BCatalogAddOn::ReadFromResource(entry_ref *appOrAddOnRef) BCatalogAddOn::ReadFromResource(const entry_ref &appOrAddOnRef)
{ {
return EOPNOTSUPP; return EOPNOTSUPP;
} }
@@ -227,14 +227,14 @@ BCatalogAddOn::WriteToFile(const char *path)
status_t status_t
BCatalogAddOn::WriteToAttribute(entry_ref *appOrAddOnRef) BCatalogAddOn::WriteToAttribute(const entry_ref &appOrAddOnRef)
{ {
return EOPNOTSUPP; return EOPNOTSUPP;
} }
status_t status_t
BCatalogAddOn::WriteToResource(entry_ref *appOrAddOnRef) BCatalogAddOn::WriteToResource(const entry_ref &appOrAddOnRef)
{ {
return EOPNOTSUPP; return EOPNOTSUPP;
} }
+5 -5
View File
@@ -82,7 +82,7 @@ DefaultCatalog::DefaultCatalog(entry_ref *appOrAddOnRef)
: :
BHashMapCatalog("", "", 0) BHashMapCatalog("", "", 0)
{ {
fInitCheck = ReadFromResource(appOrAddOnRef); fInitCheck = ReadFromResource(*appOrAddOnRef);
// fprintf(stderr, // fprintf(stderr,
// "trying to load embedded catalog from resources results in %s", // "trying to load embedded catalog from resources results in %s",
// strerror(fInitCheck)); // strerror(fInitCheck));
@@ -180,14 +180,14 @@ DefaultCatalog::ReadFromFile(const char *path)
future... future...
*/ */
status_t status_t
DefaultCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef) DefaultCatalog::ReadFromAttribute(const entry_ref &appOrAddOnRef)
{ {
return B_NOT_SUPPORTED; return B_NOT_SUPPORTED;
} }
status_t status_t
DefaultCatalog::ReadFromResource(entry_ref *appOrAddOnRef) DefaultCatalog::ReadFromResource(const entry_ref &appOrAddOnRef)
{ {
return B_NOT_SUPPORTED; return B_NOT_SUPPORTED;
} }
@@ -229,14 +229,14 @@ DefaultCatalog::WriteToFile(const char *path)
future... future...
*/ */
status_t status_t
DefaultCatalog::WriteToAttribute(entry_ref *appOrAddOnRef) DefaultCatalog::WriteToAttribute(const entry_ref &appOrAddOnRef)
{ {
return B_NOT_SUPPORTED; return B_NOT_SUPPORTED;
} }
status_t status_t
DefaultCatalog::WriteToResource(entry_ref *appOrAddOnRef) DefaultCatalog::WriteToResource(const entry_ref &appOrAddOnRef)
{ {
return B_NOT_SUPPORTED; return B_NOT_SUPPORTED;
} }
+2 -2
View File
@@ -123,7 +123,7 @@ main(int argc, char **argv)
BEntry entry(outputFile.String()); BEntry entry(outputFile.String());
entry_ref eref; entry_ref eref;
entry.GetRef(&eref); entry.GetRef(&eref);
res = targetCatImpl.WriteToAttribute(&eref); res = targetCatImpl.WriteToAttribute(eref);
if (res != B_OK) { if (res != B_OK) {
fprintf(stderr, fprintf(stderr,
"couldn't write target-attribute to %s - error: %s\n", "couldn't write target-attribute to %s - error: %s\n",
@@ -136,7 +136,7 @@ main(int argc, char **argv)
BEntry entry(outputFile.String()); BEntry entry(outputFile.String());
entry_ref eref; entry_ref eref;
entry.GetRef(&eref); entry.GetRef(&eref);
res = targetCatImpl.WriteToResource(&eref); res = targetCatImpl.WriteToResource(eref);
if (res != B_OK) { if (res != B_OK) {
fprintf(stderr, fprintf(stderr,
"couldn't write target-resource to %s - error: %s\n", "couldn't write target-resource to %s - error: %s\n",