1 /* 2 * Copyright 2003-2004, Axel Dörfler, axeld@pinc-software.de 3 * Copyright 2003-2004, Oliver Tappe, zooey@hirschkaefer.de 4 * Distributed under the terms of the MIT License. 5 */ 6 7 #include <Catalog.h> 8 9 #include <syslog.h> 10 11 #include <Application.h> 12 #include <Locale.h> 13 #include <MutableLocaleRoster.h> 14 #include <Node.h> 15 #include <Roster.h> 16 17 18 using BPrivate::gMutableLocaleRoster; 19 20 21 //#pragma mark - BCatalog 22 BCatalog::BCatalog() 23 : 24 fCatalog(NULL) 25 { 26 } 27 28 29 BCatalog::BCatalog(const char *signature, const char *language, 30 uint32 fingerprint) 31 { 32 fCatalog 33 = gMutableLocaleRoster->LoadCatalog(signature, language, fingerprint); 34 } 35 36 37 BCatalog::~BCatalog() 38 { 39 gMutableLocaleRoster->UnloadCatalog(fCatalog); 40 } 41 42 43 const char * 44 BCatalog::GetString(const char *string, const char *context, 45 const char *comment) 46 { 47 const char *translated; 48 for (BCatalogAddOn* cat = fCatalog; cat != NULL; cat = cat->fNext) { 49 translated = cat->GetString(string, context, comment); 50 if (translated) 51 return translated; 52 } 53 return string; 54 } 55 56 57 const char * 58 BCatalog::GetString(uint32 id) 59 { 60 const char *translated; 61 for (BCatalogAddOn* cat = fCatalog; cat != NULL; cat = cat->fNext) { 62 translated = cat->GetString(id); 63 if (translated) 64 return translated; 65 } 66 return ""; 67 } 68 69 70 status_t 71 BCatalog::GetData(const char *name, BMessage *msg) 72 { 73 if (!fCatalog) 74 return B_NO_INIT; 75 status_t res; 76 for (BCatalogAddOn* cat = fCatalog; cat != NULL; cat = cat->fNext) { 77 res = cat->GetData(name, msg); 78 if (res != B_NAME_NOT_FOUND && res != EOPNOTSUPP) 79 return res; // return B_OK if found, or specific error-code 80 } 81 return B_NAME_NOT_FOUND; 82 } 83 84 85 status_t 86 BCatalog::GetData(uint32 id, BMessage *msg) 87 { 88 if (!fCatalog) 89 return B_NO_INIT; 90 status_t res; 91 for (BCatalogAddOn* cat = fCatalog; cat != NULL; cat = cat->fNext) { 92 res = cat->GetData(id, msg); 93 if (res != B_NAME_NOT_FOUND && res != EOPNOTSUPP) 94 return res; // return B_OK if found, or specific error-code 95 } 96 return B_NAME_NOT_FOUND; 97 } 98 99 100 status_t 101 BCatalog::SetCatalog(const char* signature, uint32 fingerprint) 102 { 103 // This is not thread safe. It is used only in ReadOnlyBootPrompt and should 104 // not do harm there, but not sure what to do about it… 105 gMutableLocaleRoster->UnloadCatalog(fCatalog); 106 fCatalog = gMutableLocaleRoster->LoadCatalog(signature, NULL, fingerprint); 107 108 return B_OK; 109 } 110 111 112 //#pragma mark - BCatalogAddOn 113 BCatalogAddOn::BCatalogAddOn(const char *signature, const char *language, 114 uint32 fingerprint) 115 : 116 fInitCheck(B_NO_INIT), 117 fSignature(signature), 118 fLanguageName(language), 119 fFingerprint(fingerprint), 120 fNext(NULL) 121 { 122 fLanguageName.ToLower(); 123 // canonicalize language-name to lowercase 124 } 125 126 127 BCatalogAddOn::~BCatalogAddOn() 128 { 129 } 130 131 132 void 133 BCatalogAddOn::UpdateFingerprint() 134 { 135 fFingerprint = 0; 136 // base implementation always yields the same fingerprint, 137 // which means that no version-mismatch detection is possible. 138 } 139 140 141 status_t 142 BCatalogAddOn::InitCheck() const 143 { 144 return fInitCheck; 145 } 146 147 148 bool 149 BCatalogAddOn::CanHaveData() const 150 { 151 return false; 152 } 153 154 155 status_t 156 BCatalogAddOn::GetData(const char *name, BMessage *msg) 157 { 158 return EOPNOTSUPP; 159 } 160 161 162 status_t 163 BCatalogAddOn::GetData(uint32 id, BMessage *msg) 164 { 165 return EOPNOTSUPP; 166 } 167 168 169 status_t 170 BCatalogAddOn::SetString(const char *string, const char *translated, 171 const char *context, const char *comment) 172 { 173 return EOPNOTSUPP; 174 } 175 176 177 status_t 178 BCatalogAddOn::SetString(int32 id, const char *translated) 179 { 180 return EOPNOTSUPP; 181 } 182 183 184 bool 185 BCatalogAddOn::CanWriteData() const 186 { 187 return false; 188 } 189 190 191 status_t 192 BCatalogAddOn::SetData(const char *name, BMessage *msg) 193 { 194 return EOPNOTSUPP; 195 } 196 197 198 status_t 199 BCatalogAddOn::SetData(uint32 id, BMessage *msg) 200 { 201 return EOPNOTSUPP; 202 } 203 204 205 status_t 206 BCatalogAddOn::ReadFromFile(const char *path) 207 { 208 return EOPNOTSUPP; 209 } 210 211 212 status_t 213 BCatalogAddOn::ReadFromAttribute(entry_ref *appOrAddOnRef) 214 { 215 return EOPNOTSUPP; 216 } 217 218 219 status_t 220 BCatalogAddOn::ReadFromResource(entry_ref *appOrAddOnRef) 221 { 222 return EOPNOTSUPP; 223 } 224 225 226 status_t 227 BCatalogAddOn::WriteToFile(const char *path) 228 { 229 return EOPNOTSUPP; 230 } 231 232 233 status_t 234 BCatalogAddOn::WriteToAttribute(entry_ref *appOrAddOnRef) 235 { 236 return EOPNOTSUPP; 237 } 238 239 240 status_t 241 BCatalogAddOn::WriteToResource(entry_ref *appOrAddOnRef) 242 { 243 return EOPNOTSUPP; 244 } 245 246 247 void BCatalogAddOn::MakeEmpty() 248 { 249 } 250 251 252 int32 253 BCatalogAddOn::CountItems() const 254 { 255 return 0; 256 } 257 258 259 void 260 BCatalogAddOn::SetNext(BCatalogAddOn *next) 261 { 262 fNext = next; 263 } 264 265 266 //#pragma mark - EditableCatalog 267 namespace BPrivate { 268 EditableCatalog::EditableCatalog(const char *type, const char *signature, 269 const char *language) 270 { 271 fCatalog = gMutableLocaleRoster->CreateCatalog(type, signature, language); 272 } 273 274 275 EditableCatalog::~EditableCatalog() 276 { 277 } 278 279 280 status_t 281 EditableCatalog::SetString(const char *string, const char *translated, 282 const char *context, const char *comment) 283 { 284 if (!fCatalog) 285 return B_NO_INIT; 286 return fCatalog->SetString(string, translated, context, comment); 287 } 288 289 290 status_t 291 EditableCatalog::SetString(int32 id, const char *translated) 292 { 293 if (!fCatalog) 294 return B_NO_INIT; 295 return fCatalog->SetString(id, translated); 296 } 297 298 299 bool 300 EditableCatalog::CanWriteData() const 301 { 302 if (!fCatalog) 303 return false; 304 return fCatalog->CanWriteData(); 305 } 306 307 308 status_t 309 EditableCatalog::SetData(const char *name, BMessage *msg) 310 { 311 if (!fCatalog) 312 return B_NO_INIT; 313 return fCatalog->SetData(name, msg); 314 } 315 316 317 status_t 318 EditableCatalog::SetData(uint32 id, BMessage *msg) 319 { 320 if (!fCatalog) 321 return B_NO_INIT; 322 return fCatalog->SetData(id, msg); 323 } 324 325 326 status_t 327 EditableCatalog::ReadFromFile(const char *path) 328 { 329 if (!fCatalog) 330 return B_NO_INIT; 331 return fCatalog->ReadFromFile(path); 332 } 333 334 335 status_t 336 EditableCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef) 337 { 338 if (!fCatalog) 339 return B_NO_INIT; 340 return fCatalog->ReadFromAttribute(appOrAddOnRef); 341 } 342 343 344 status_t 345 EditableCatalog::ReadFromResource(entry_ref *appOrAddOnRef) 346 { 347 if (!fCatalog) 348 return B_NO_INIT; 349 return fCatalog->ReadFromResource(appOrAddOnRef); 350 } 351 352 353 status_t 354 EditableCatalog::WriteToFile(const char *path) 355 { 356 if (!fCatalog) 357 return B_NO_INIT; 358 return fCatalog->WriteToFile(path); 359 } 360 361 362 status_t 363 EditableCatalog::WriteToAttribute(entry_ref *appOrAddOnRef) 364 { 365 if (!fCatalog) 366 return B_NO_INIT; 367 return fCatalog->WriteToAttribute(appOrAddOnRef); 368 } 369 370 371 status_t 372 EditableCatalog::WriteToResource(entry_ref *appOrAddOnRef) 373 { 374 if (!fCatalog) 375 return B_NO_INIT; 376 return fCatalog->WriteToResource(appOrAddOnRef); 377 } 378 379 380 void EditableCatalog::MakeEmpty() 381 { 382 if (fCatalog) 383 fCatalog->MakeEmpty(); 384 } 385 386 387 } // namespace BPrivate 388