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