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::MutableLocaleRoster; 19 20 21 //#pragma mark - BCatalog 22 BCatalog::BCatalog() 23 : 24 fCatalog(NULL) 25 { 26 } 27 28 29 BCatalog::BCatalog(const entry_ref &catalogOwner, const char *language, 30 uint32 fingerprint) 31 { 32 fCatalog = MutableLocaleRoster::Default()->LoadCatalog(catalogOwner, language, 33 fingerprint); 34 } 35 36 37 BCatalog::~BCatalog() 38 { 39 MutableLocaleRoster::Default()->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 entry_ref &catalogOwner, 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 MutableLocaleRoster::Default()->UnloadCatalog(fCatalog); 106 fCatalog = MutableLocaleRoster::Default()->LoadCatalog(catalogOwner, NULL, 107 fingerprint); 108 109 return B_OK; 110 } 111 112 113 //#pragma mark - BCatalogAddOn 114 BCatalogAddOn::BCatalogAddOn(const char *signature, const char *language, 115 uint32 fingerprint) 116 : 117 fInitCheck(B_NO_INIT), 118 fSignature(signature), 119 fLanguageName(language), 120 fFingerprint(fingerprint), 121 fNext(NULL) 122 { 123 fLanguageName.ToLower(); 124 // canonicalize language-name to lowercase 125 } 126 127 128 BCatalogAddOn::~BCatalogAddOn() 129 { 130 } 131 132 133 void 134 BCatalogAddOn::UpdateFingerprint() 135 { 136 fFingerprint = 0; 137 // base implementation always yields the same fingerprint, 138 // which means that no version-mismatch detection is possible. 139 } 140 141 142 status_t 143 BCatalogAddOn::InitCheck() const 144 { 145 return fInitCheck; 146 } 147 148 149 bool 150 BCatalogAddOn::CanHaveData() const 151 { 152 return false; 153 } 154 155 156 status_t 157 BCatalogAddOn::GetData(const char *name, BMessage *msg) 158 { 159 return EOPNOTSUPP; 160 } 161 162 163 status_t 164 BCatalogAddOn::GetData(uint32 id, BMessage *msg) 165 { 166 return EOPNOTSUPP; 167 } 168 169 170 status_t 171 BCatalogAddOn::SetString(const char *string, const char *translated, 172 const char *context, const char *comment) 173 { 174 return EOPNOTSUPP; 175 } 176 177 178 status_t 179 BCatalogAddOn::SetString(int32 id, const char *translated) 180 { 181 return EOPNOTSUPP; 182 } 183 184 185 bool 186 BCatalogAddOn::CanWriteData() const 187 { 188 return false; 189 } 190 191 192 status_t 193 BCatalogAddOn::SetData(const char *name, BMessage *msg) 194 { 195 return EOPNOTSUPP; 196 } 197 198 199 status_t 200 BCatalogAddOn::SetData(uint32 id, BMessage *msg) 201 { 202 return EOPNOTSUPP; 203 } 204 205 206 status_t 207 BCatalogAddOn::ReadFromFile(const char *path) 208 { 209 return EOPNOTSUPP; 210 } 211 212 213 status_t 214 BCatalogAddOn::ReadFromAttribute(entry_ref *appOrAddOnRef) 215 { 216 return EOPNOTSUPP; 217 } 218 219 220 status_t 221 BCatalogAddOn::ReadFromResource(entry_ref *appOrAddOnRef) 222 { 223 return EOPNOTSUPP; 224 } 225 226 227 status_t 228 BCatalogAddOn::WriteToFile(const char *path) 229 { 230 return EOPNOTSUPP; 231 } 232 233 234 status_t 235 BCatalogAddOn::WriteToAttribute(entry_ref *appOrAddOnRef) 236 { 237 return EOPNOTSUPP; 238 } 239 240 241 status_t 242 BCatalogAddOn::WriteToResource(entry_ref *appOrAddOnRef) 243 { 244 return EOPNOTSUPP; 245 } 246 247 248 void BCatalogAddOn::MakeEmpty() 249 { 250 } 251 252 253 int32 254 BCatalogAddOn::CountItems() const 255 { 256 return 0; 257 } 258 259 260 void 261 BCatalogAddOn::SetNext(BCatalogAddOn *next) 262 { 263 fNext = next; 264 } 265 266 267 //#pragma mark - EditableCatalog 268 namespace BPrivate { 269 EditableCatalog::EditableCatalog(const char *type, const char *signature, 270 const char *language) 271 { 272 fCatalog = MutableLocaleRoster::Default()->CreateCatalog(type, signature, 273 language); 274 } 275 276 277 EditableCatalog::~EditableCatalog() 278 { 279 } 280 281 282 status_t 283 EditableCatalog::SetString(const char *string, const char *translated, 284 const char *context, const char *comment) 285 { 286 if (!fCatalog) 287 return B_NO_INIT; 288 return fCatalog->SetString(string, translated, context, comment); 289 } 290 291 292 status_t 293 EditableCatalog::SetString(int32 id, const char *translated) 294 { 295 if (!fCatalog) 296 return B_NO_INIT; 297 return fCatalog->SetString(id, translated); 298 } 299 300 301 bool 302 EditableCatalog::CanWriteData() const 303 { 304 if (!fCatalog) 305 return false; 306 return fCatalog->CanWriteData(); 307 } 308 309 310 status_t 311 EditableCatalog::SetData(const char *name, BMessage *msg) 312 { 313 if (!fCatalog) 314 return B_NO_INIT; 315 return fCatalog->SetData(name, msg); 316 } 317 318 319 status_t 320 EditableCatalog::SetData(uint32 id, BMessage *msg) 321 { 322 if (!fCatalog) 323 return B_NO_INIT; 324 return fCatalog->SetData(id, msg); 325 } 326 327 328 status_t 329 EditableCatalog::ReadFromFile(const char *path) 330 { 331 if (!fCatalog) 332 return B_NO_INIT; 333 return fCatalog->ReadFromFile(path); 334 } 335 336 337 status_t 338 EditableCatalog::ReadFromAttribute(entry_ref *appOrAddOnRef) 339 { 340 if (!fCatalog) 341 return B_NO_INIT; 342 return fCatalog->ReadFromAttribute(appOrAddOnRef); 343 } 344 345 346 status_t 347 EditableCatalog::ReadFromResource(entry_ref *appOrAddOnRef) 348 { 349 if (!fCatalog) 350 return B_NO_INIT; 351 return fCatalog->ReadFromResource(appOrAddOnRef); 352 } 353 354 355 status_t 356 EditableCatalog::WriteToFile(const char *path) 357 { 358 if (!fCatalog) 359 return B_NO_INIT; 360 return fCatalog->WriteToFile(path); 361 } 362 363 364 status_t 365 EditableCatalog::WriteToAttribute(entry_ref *appOrAddOnRef) 366 { 367 if (!fCatalog) 368 return B_NO_INIT; 369 return fCatalog->WriteToAttribute(appOrAddOnRef); 370 } 371 372 373 status_t 374 EditableCatalog::WriteToResource(entry_ref *appOrAddOnRef) 375 { 376 if (!fCatalog) 377 return B_NO_INIT; 378 return fCatalog->WriteToResource(appOrAddOnRef); 379 } 380 381 382 void EditableCatalog::MakeEmpty() 383 { 384 if (fCatalog) 385 fCatalog->MakeEmpty(); 386 } 387 388 389 } // namespace BPrivate 390