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
BCatalog()22 BCatalog::BCatalog()
23 :
24 fCatalogData(NULL),
25 fLock("Catalog")
26 {
27 }
28
29
BCatalog(const entry_ref & catalogOwner,const char * language,uint32 fingerprint)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
BCatalog(const char * signature,const char * language)40 BCatalog::BCatalog(const char* signature, const char* language)
41 :
42 fCatalogData(NULL),
43 fLock("Catalog")
44 {
45 SetTo(signature, language);
46 }
47
48
~BCatalog()49 BCatalog::~BCatalog()
50 {
51 MutableLocaleRoster::Default()->UnloadCatalog(fCatalogData);
52 }
53
54
55 const char*
GetString(const char * string,const char * context,const char * comment)56 BCatalog::GetString(const char* string, const char* context,
57 const char* comment)
58 {
59 BAutolock lock(&fLock);
60 if (!lock.IsLocked())
61 return string;
62
63 const char* translated;
64 for (BCatalogData* cat = fCatalogData; cat != NULL; cat = cat->fNext) {
65 translated = cat->GetString(string, context, comment);
66 if (translated != NULL)
67 return translated;
68 }
69
70 return string;
71 }
72
73
74 const char*
GetString(uint32 id)75 BCatalog::GetString(uint32 id)
76 {
77 BAutolock lock(&fLock);
78 if (!lock.IsLocked())
79 return "";
80
81 const char* translated;
82 for (BCatalogData* cat = fCatalogData; cat != NULL; cat = cat->fNext) {
83 translated = cat->GetString(id);
84 if (translated != NULL)
85 return translated;
86 }
87
88 return "";
89 }
90
91
92 status_t
GetData(const char * name,BMessage * msg)93 BCatalog::GetData(const char* name, BMessage* msg)
94 {
95 BAutolock lock(&fLock);
96 if (!lock.IsLocked())
97 return B_ERROR;
98
99 if (fCatalogData == NULL)
100 return B_NO_INIT;
101
102 status_t res;
103 for (BCatalogData* cat = fCatalogData; cat != NULL; cat = cat->fNext) {
104 res = cat->GetData(name, msg);
105 if (res != B_NAME_NOT_FOUND && res != EOPNOTSUPP)
106 return res; // return B_OK if found, or specific error-code
107 }
108
109 return B_NAME_NOT_FOUND;
110 }
111
112
113 status_t
GetData(uint32 id,BMessage * msg)114 BCatalog::GetData(uint32 id, BMessage* msg)
115 {
116 BAutolock lock(&fLock);
117 if (!lock.IsLocked())
118 return B_ERROR;
119
120 if (fCatalogData == NULL)
121 return B_NO_INIT;
122
123 status_t res;
124 for (BCatalogData* cat = fCatalogData; cat != NULL; cat = cat->fNext) {
125 res = cat->GetData(id, msg);
126 if (res != B_NAME_NOT_FOUND && res != EOPNOTSUPP)
127 return res; // return B_OK if found, or specific error-code
128 }
129
130 return B_NAME_NOT_FOUND;
131 }
132
133
134 status_t
GetSignature(BString * sig)135 BCatalog::GetSignature(BString* sig)
136 {
137 BAutolock lock(&fLock);
138 if (!lock.IsLocked())
139 return B_ERROR;
140
141 if (sig == NULL)
142 return B_BAD_VALUE;
143
144 if (fCatalogData == NULL)
145 return B_NO_INIT;
146
147 *sig = fCatalogData->fSignature;
148
149 return B_OK;
150 }
151
152
153 status_t
GetLanguage(BString * lang)154 BCatalog::GetLanguage(BString* lang)
155 {
156 BAutolock lock(&fLock);
157 if (!lock.IsLocked())
158 return B_ERROR;
159
160 if (lang == NULL)
161 return B_BAD_VALUE;
162
163 if (fCatalogData == NULL)
164 return B_NO_INIT;
165
166 *lang = fCatalogData->fLanguageName;
167
168 return B_OK;
169 }
170
171
172 status_t
GetFingerprint(uint32 * fp)173 BCatalog::GetFingerprint(uint32* fp)
174 {
175 BAutolock lock(&fLock);
176 if (!lock.IsLocked())
177 return B_ERROR;
178
179 if (fp == NULL)
180 return B_BAD_VALUE;
181
182 if (fCatalogData == NULL)
183 return B_NO_INIT;
184
185 *fp = fCatalogData->fFingerprint;
186
187 return B_OK;
188 }
189
190
191 status_t
SetTo(const entry_ref & catalogOwner,const char * language,uint32 fingerprint)192 BCatalog::SetTo(const entry_ref& catalogOwner, const char* language,
193 uint32 fingerprint)
194 {
195 BAutolock lock(&fLock);
196 if (!lock.IsLocked())
197 return B_ERROR;
198
199 MutableLocaleRoster::Default()->UnloadCatalog(fCatalogData);
200 fCatalogData = MutableLocaleRoster::Default()->LoadCatalog(catalogOwner,
201 language, fingerprint);
202
203 return B_OK;
204 }
205
206
207 status_t
SetTo(const char * signature,const char * language)208 BCatalog::SetTo(const char* signature, const char* language)
209 {
210 BAutolock lock(&fLock);
211 if (!lock.IsLocked())
212 return B_ERROR;
213
214 MutableLocaleRoster::Default()->UnloadCatalog(fCatalogData);
215 fCatalogData = MutableLocaleRoster::Default()->LoadCatalog(signature,
216 language);
217
218 return B_OK;
219 }
220
221
222 status_t
InitCheck() const223 BCatalog::InitCheck() const
224 {
225 BAutolock lock(&fLock);
226 if (!lock.IsLocked())
227 return B_ERROR;
228
229 return fCatalogData != NULL ? fCatalogData->InitCheck() : B_NO_INIT;
230 }
231
232
233 int32
CountItems() const234 BCatalog::CountItems() const
235 {
236 BAutolock lock(&fLock);
237 if (!lock.IsLocked())
238 return 0;
239
240 return fCatalogData != NULL ? fCatalogData->CountItems() : 0;
241 }
242