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