1 /* 2 * Copyright 2003-2004, Axel Dörfler, axeld@pinc-software.de 3 * Copyright 2003-2004,2012, Oliver Tappe, zooey@hirschkaefer.de 4 * Distributed under the terms of the MIT License. 5 */ 6 7 #include <Catalog.h> 8 9 #include <Application.h> 10 #include <Autolock.h> 11 #include <CatalogData.h> 12 #include <Locale.h> 13 #include <MutableLocaleRoster.h> 14 #include <Node.h> 15 #include <Roster.h> 16 17 18 using BPrivate::MutableLocaleRoster; 19 20 21 //#pragma mark - BCatalog 22 BCatalog::BCatalog() 23 : 24 fCatalogData(NULL), 25 fLock("Catalog") 26 { 27 } 28 29 30 BCatalog::BCatalog(const entry_ref& catalogOwner, const char* language, 31 uint32 fingerprint) 32 : 33 fCatalogData(NULL), 34 fLock("Catalog") 35 { 36 SetTo(catalogOwner, language, fingerprint); 37 } 38 39 40 BCatalog::~BCatalog() 41 { 42 MutableLocaleRoster::Default()->UnloadCatalog(fCatalogData); 43 } 44 45 46 const char* 47 BCatalog::GetString(const char* string, const char* context, 48 const char* comment) 49 { 50 BAutolock lock(&fLock); 51 if (!lock.IsLocked()) 52 return string; 53 54 const char* translated; 55 for (BCatalogData* cat = fCatalogData; cat != NULL; cat = cat->fNext) { 56 translated = cat->GetString(string, context, comment); 57 if (translated != NULL) 58 return translated; 59 } 60 61 return string; 62 } 63 64 65 const char* 66 BCatalog::GetString(uint32 id) 67 { 68 BAutolock lock(&fLock); 69 if (!lock.IsLocked()) 70 return ""; 71 72 const char* translated; 73 for (BCatalogData* cat = fCatalogData; cat != NULL; cat = cat->fNext) { 74 translated = cat->GetString(id); 75 if (translated != NULL) 76 return translated; 77 } 78 79 return ""; 80 } 81 82 83 status_t 84 BCatalog::GetData(const char* name, BMessage* msg) 85 { 86 BAutolock lock(&fLock); 87 if (!lock.IsLocked()) 88 return B_ERROR; 89 90 if (fCatalogData == NULL) 91 return B_NO_INIT; 92 93 status_t res; 94 for (BCatalogData* cat = fCatalogData; cat != NULL; cat = cat->fNext) { 95 res = cat->GetData(name, msg); 96 if (res != B_NAME_NOT_FOUND && res != EOPNOTSUPP) 97 return res; // return B_OK if found, or specific error-code 98 } 99 100 return B_NAME_NOT_FOUND; 101 } 102 103 104 status_t 105 BCatalog::GetData(uint32 id, BMessage* msg) 106 { 107 BAutolock lock(&fLock); 108 if (!lock.IsLocked()) 109 return B_ERROR; 110 111 if (fCatalogData == NULL) 112 return B_NO_INIT; 113 114 status_t res; 115 for (BCatalogData* cat = fCatalogData; cat != NULL; cat = cat->fNext) { 116 res = cat->GetData(id, msg); 117 if (res != B_NAME_NOT_FOUND && res != EOPNOTSUPP) 118 return res; // return B_OK if found, or specific error-code 119 } 120 121 return B_NAME_NOT_FOUND; 122 } 123 124 125 status_t 126 BCatalog::GetSignature(BString* sig) 127 { 128 BAutolock lock(&fLock); 129 if (!lock.IsLocked()) 130 return B_ERROR; 131 132 if (sig == NULL) 133 return B_BAD_VALUE; 134 135 if (fCatalogData == NULL) 136 return B_NO_INIT; 137 138 *sig = fCatalogData->fSignature; 139 140 return B_OK; 141 } 142 143 144 status_t 145 BCatalog::GetLanguage(BString* lang) 146 { 147 BAutolock lock(&fLock); 148 if (!lock.IsLocked()) 149 return B_ERROR; 150 151 if (lang == NULL) 152 return B_BAD_VALUE; 153 154 if (fCatalogData == NULL) 155 return B_NO_INIT; 156 157 *lang = fCatalogData->fLanguageName; 158 159 return B_OK; 160 } 161 162 163 status_t 164 BCatalog::GetFingerprint(uint32* fp) 165 { 166 BAutolock lock(&fLock); 167 if (!lock.IsLocked()) 168 return B_ERROR; 169 170 if (fp == NULL) 171 return B_BAD_VALUE; 172 173 if (fCatalogData == NULL) 174 return B_NO_INIT; 175 176 *fp = fCatalogData->fFingerprint; 177 178 return B_OK; 179 } 180 181 182 status_t 183 BCatalog::SetTo(const entry_ref& catalogOwner, const char* language, 184 uint32 fingerprint) 185 { 186 BAutolock lock(&fLock); 187 if (!lock.IsLocked()) 188 return B_ERROR; 189 190 MutableLocaleRoster::Default()->UnloadCatalog(fCatalogData); 191 fCatalogData = MutableLocaleRoster::Default()->LoadCatalog(catalogOwner, 192 language, fingerprint); 193 194 return B_OK; 195 } 196 197 198 status_t 199 BCatalog::InitCheck() const 200 { 201 BAutolock lock(&fLock); 202 if (!lock.IsLocked()) 203 return B_ERROR; 204 205 return fCatalogData != NULL ? fCatalogData->InitCheck() : B_NO_INIT; 206 } 207 208 209 int32 210 BCatalog::CountItems() const 211 { 212 BAutolock lock(&fLock); 213 if (!lock.IsLocked()) 214 return 0; 215 216 return fCatalogData != NULL ? fCatalogData->CountItems() : 0; 217 } 218