xref: /haiku/src/kits/package/RepositoryInfo.cpp (revision f73f5d4c42a01ece688cbb57b5d332cc0f68b2c6)
1 /*
2  * Copyright 2011, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Oliver Tappe <zooey@hirschkaefer.de>
7  */
8 
9 
10 #include <package/RepositoryInfo.h>
11 
12 #include <stdlib.h>
13 
14 #include <new>
15 
16 #include <driver_settings.h>
17 #include <File.h>
18 #include <Message.h>
19 
20 #include <AutoDeleter.h>
21 #include <package/PackageInfo.h>
22 
23 
24 namespace BPackageKit {
25 
26 
27 const uint8 BRepositoryInfo::kDefaultPriority	= 50;
28 
29 const char* const BRepositoryInfo::kNameField			= "name";
30 const char* const BRepositoryInfo::kURLField			= "url";
31 const char* const BRepositoryInfo::kVendorField			= "vendor";
32 const char* const BRepositoryInfo::kSummaryField		= "summary";
33 const char* const BRepositoryInfo::kPriorityField		= "priority";
34 const char* const BRepositoryInfo::kArchitectureField	= "architecture";
35 const char* const BRepositoryInfo::kLicenseNameField	= "licenseName";
36 const char* const BRepositoryInfo::kLicenseTextField	= "licenseText";
37 
38 
39 BRepositoryInfo::BRepositoryInfo()
40 	:
41 	fInitStatus(B_NO_INIT),
42 	fPriority(kDefaultPriority),
43 	fArchitecture(B_PACKAGE_ARCHITECTURE_ENUM_COUNT)
44 {
45 }
46 
47 
48 BRepositoryInfo::BRepositoryInfo(BMessage* data)
49 	:
50 	inherited(data),
51 	fLicenseTexts(5)
52 {
53 	fInitStatus = _SetTo(data);
54 }
55 
56 
57 BRepositoryInfo::BRepositoryInfo(const BEntry& entry)
58 {
59 	fInitStatus = _SetTo(entry);
60 }
61 
62 
63 BRepositoryInfo::~BRepositoryInfo()
64 {
65 }
66 
67 
68 /*static*/ BRepositoryInfo*
69 BRepositoryInfo::Instantiate(BMessage* data)
70 {
71 	if (validate_instantiation(data, "BPackageKit::BRepositoryInfo"))
72 		return new (std::nothrow) BRepositoryInfo(data);
73 
74 	return NULL;
75 }
76 
77 
78 status_t
79 BRepositoryInfo::Archive(BMessage* data, bool deep) const
80 {
81 	status_t result = inherited::Archive(data, deep);
82 	if (result != B_OK)
83 		return result;
84 
85 	if ((result = data->AddString(kNameField, fName)) != B_OK)
86 		return result;
87 	if ((result = data->AddString(kURLField, fOriginalBaseURL)) != B_OK)
88 		return result;
89 	if ((result = data->AddString(kVendorField, fVendor)) != B_OK)
90 		return result;
91 	result = data->AddString(kSummaryField, fSummary);
92 	if (result != B_OK)
93 		return result;
94 	if ((result = data->AddUInt8(kPriorityField, fPriority)) != B_OK)
95 		return result;
96 	if ((result = data->AddUInt8(kArchitectureField, fArchitecture)) != B_OK)
97 		return result;
98 	for (int i = 0; i < fLicenseNames.CountStrings(); ++i) {
99 		result = data->AddString(kLicenseNameField, fLicenseNames.StringAt(i));
100 		if (result != B_OK)
101 			return result;
102 	}
103 	for (int i = 0; i < fLicenseTexts.CountStrings(); ++i) {
104 		result = data->AddString(kLicenseTextField, fLicenseTexts.StringAt(i));
105 		if (result != B_OK)
106 			return result;
107 	}
108 
109 	return B_OK;
110 }
111 
112 
113 status_t
114 BRepositoryInfo::InitCheck() const
115 {
116 	return fInitStatus;
117 }
118 
119 
120 status_t
121 BRepositoryInfo::SetTo(const BMessage* data)
122 {
123 	return fInitStatus = _SetTo(data);
124 }
125 
126 
127 status_t
128 BRepositoryInfo::SetTo(const BEntry& entry)
129 {
130 	return fInitStatus = _SetTo(entry);
131 }
132 
133 
134 const BString&
135 BRepositoryInfo::Name() const
136 {
137 	return fName;
138 }
139 
140 
141 const BString&
142 BRepositoryInfo::OriginalBaseURL() const
143 {
144 	return fOriginalBaseURL;
145 }
146 
147 
148 const BString&
149 BRepositoryInfo::Vendor() const
150 {
151 	return fVendor;
152 }
153 
154 
155 const BString&
156 BRepositoryInfo::Summary() const
157 {
158 	return fSummary;
159 }
160 
161 
162 uint8
163 BRepositoryInfo::Priority() const
164 {
165 	return fPriority;
166 }
167 
168 
169 BPackageArchitecture
170 BRepositoryInfo::Architecture() const
171 {
172 	return fArchitecture;
173 }
174 
175 
176 const BStringList&
177 BRepositoryInfo::LicenseNames() const
178 {
179 	return fLicenseNames;
180 }
181 
182 
183 const BStringList&
184 BRepositoryInfo::LicenseTexts() const
185 {
186 	return fLicenseTexts;
187 }
188 
189 
190 void
191 BRepositoryInfo::SetName(const BString& name)
192 {
193 	fName = name;
194 }
195 
196 
197 void
198 BRepositoryInfo::SetOriginalBaseURL(const BString& url)
199 {
200 	fOriginalBaseURL = url;
201 }
202 
203 
204 void
205 BRepositoryInfo::SetVendor(const BString& vendor)
206 {
207 	fVendor = vendor;
208 }
209 
210 
211 void
212 BRepositoryInfo::SetSummary(const BString& summary)
213 {
214 	fSummary = summary;
215 }
216 
217 
218 void
219 BRepositoryInfo::SetPriority(uint8 priority)
220 {
221 	fPriority = priority;
222 }
223 
224 
225 void
226 BRepositoryInfo::SetArchitecture(BPackageArchitecture architecture)
227 {
228 	fArchitecture = architecture;
229 }
230 
231 
232 status_t
233 BRepositoryInfo::AddLicense(const BString& licenseName,
234 	const BString& licenseText)
235 {
236 	if (!fLicenseNames.Add(licenseName) || !fLicenseTexts.Add(licenseText))
237 		return B_NO_MEMORY;
238 
239 	return B_OK;
240 }
241 
242 
243 void
244 BRepositoryInfo::ClearLicenses()
245 {
246 	fLicenseNames.MakeEmpty();
247 	fLicenseTexts.MakeEmpty();
248 }
249 
250 
251 status_t
252 BRepositoryInfo::_SetTo(const BMessage* data)
253 {
254 	if (data == NULL)
255 		return B_BAD_VALUE;
256 
257 	status_t result;
258 	if ((result = data->FindString(kNameField, &fName)) != B_OK)
259 		return result;
260 	if ((result = data->FindString(kURLField, &fOriginalBaseURL)) != B_OK)
261 		return result;
262 	if ((result = data->FindString(kVendorField, &fVendor)) != B_OK)
263 		return result;
264 	result = data->FindString(kSummaryField, &fSummary);
265 	if (result != B_OK)
266 		return result;
267 	if ((result = data->FindUInt8(kPriorityField, &fPriority)) != B_OK)
268 		return result;
269 	result = data->FindUInt8(kArchitectureField, (uint8*)&fArchitecture);
270 	if (result != B_OK)
271 		return result;
272 	if (fArchitecture == B_PACKAGE_ARCHITECTURE_ANY)
273 		return B_BAD_DATA;
274 
275 	const char* licenseName;
276 	const char* licenseText;
277 	for (int i = 0;
278 		data->FindString(kLicenseNameField, i, &licenseName) == B_OK
279 			&& data->FindString(kLicenseTextField, i, &licenseText) == B_OK;
280 		++i) {
281 		if (!fLicenseNames.Add(licenseName) || !fLicenseTexts.Add(licenseText))
282 			return B_NO_MEMORY;
283 	}
284 
285 	return B_OK;
286 }
287 
288 
289 status_t
290 BRepositoryInfo::_SetTo(const BEntry& entry)
291 {
292 	BFile file(&entry, B_READ_ONLY);
293 	status_t result = file.InitCheck();
294 	if (result != B_OK)
295 		return result;
296 
297 	off_t size;
298 	if ((result = file.GetSize(&size)) != B_OK)
299 		return result;
300 
301 	BString configString;
302 	char* buffer = configString.LockBuffer(size);
303 	if (buffer == NULL)
304 		return B_NO_MEMORY;
305 
306 	if ((result = file.Read(buffer, size)) < size) {
307 		configString.UnlockBuffer(0);
308 		return (result >= 0) ? B_IO_ERROR : result;
309 	}
310 
311 	buffer[size] = '\0';
312 	configString.UnlockBuffer(size);
313 
314 	void* settingsHandle = parse_driver_settings_string(configString.String());
315 	if (settingsHandle == NULL)
316 		return B_BAD_DATA;
317 	CObjectDeleter<void, status_t> settingsHandleDeleter(settingsHandle,
318 		&unload_driver_settings);
319 
320 	const char* name = get_driver_parameter(settingsHandle, "name", NULL, NULL);
321 	const char* url = get_driver_parameter(settingsHandle, "url", NULL, NULL);
322 	const char* vendor
323 		= get_driver_parameter(settingsHandle, "vendor", NULL, NULL);
324 	const char* summary
325 		= get_driver_parameter(settingsHandle, "summary", NULL, NULL);
326 	const char* priorityString
327 		= get_driver_parameter(settingsHandle, "priority", NULL, NULL);
328 	const char* architectureString
329 		= get_driver_parameter(settingsHandle, "architecture", NULL, NULL);
330 
331 	if (name == NULL || *name == '\0' || url == NULL || *url == '\0'
332 		|| vendor == NULL || *vendor == '\0'
333 		|| summary == NULL || *summary == '\0'
334 		|| priorityString == NULL || *priorityString == '\0'
335 		|| architectureString == NULL || *architectureString == '\0') {
336 		return B_BAD_DATA;
337 	}
338 
339 	BPackageArchitecture architecture;
340 	if (BPackageInfo::GetArchitectureByName(architectureString, architecture)
341 			!= B_OK || architecture == B_PACKAGE_ARCHITECTURE_ANY) {
342 		return B_BAD_DATA;
343 	}
344 
345 	fName = name;
346 	fOriginalBaseURL = url;
347 	fVendor = vendor;
348 	fSummary = summary;
349 	fPriority = atoi(priorityString);
350 	fArchitecture = architecture;
351 
352 	return B_OK;
353 }
354 
355 
356 }	// namespace BPackageKit
357