BPicture: Fix archive constructor.
[haiku.git] / src / kits / package / RepositoryInfo.cpp
blobaaff367a6e37a91439a74a36e5ae70a0cbddfea5
1 /*
2 * Copyright 2011, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Oliver Tappe <zooey@hirschkaefer.de>
7 */
10 #include <package/RepositoryInfo.h>
12 #include <stdlib.h>
14 #include <new>
16 #include <driver_settings.h>
17 #include <File.h>
18 #include <Message.h>
20 #include <AutoDeleter.h>
21 #include <package/PackageInfo.h>
24 namespace BPackageKit {
27 const uint8 BRepositoryInfo::kDefaultPriority = 50;
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";
39 BRepositoryInfo::BRepositoryInfo()
41 fInitStatus(B_NO_INIT),
42 fPriority(kDefaultPriority),
43 fArchitecture(B_PACKAGE_ARCHITECTURE_ENUM_COUNT)
48 BRepositoryInfo::BRepositoryInfo(BMessage* data)
50 inherited(data),
51 fLicenseTexts(5)
53 fInitStatus = _SetTo(data);
57 BRepositoryInfo::BRepositoryInfo(const BEntry& entry)
59 fInitStatus = _SetTo(entry);
63 BRepositoryInfo::~BRepositoryInfo()
68 /*static*/ BRepositoryInfo*
69 BRepositoryInfo::Instantiate(BMessage* data)
71 if (validate_instantiation(data, "BPackageKit::BRepositoryInfo"))
72 return new (std::nothrow) BRepositoryInfo(data);
74 return NULL;
78 status_t
79 BRepositoryInfo::Archive(BMessage* data, bool deep) const
81 status_t result = inherited::Archive(data, deep);
82 if (result != B_OK)
83 return result;
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;
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;
109 return B_OK;
113 status_t
114 BRepositoryInfo::InitCheck() const
116 return fInitStatus;
120 status_t
121 BRepositoryInfo::SetTo(const BMessage* data)
123 return fInitStatus = _SetTo(data);
127 status_t
128 BRepositoryInfo::SetTo(const BEntry& entry)
130 return fInitStatus = _SetTo(entry);
134 const BString&
135 BRepositoryInfo::Name() const
137 return fName;
141 const BString&
142 BRepositoryInfo::OriginalBaseURL() const
144 return fOriginalBaseURL;
148 const BString&
149 BRepositoryInfo::Vendor() const
151 return fVendor;
155 const BString&
156 BRepositoryInfo::Summary() const
158 return fSummary;
162 uint8
163 BRepositoryInfo::Priority() const
165 return fPriority;
169 BPackageArchitecture
170 BRepositoryInfo::Architecture() const
172 return fArchitecture;
176 const BStringList&
177 BRepositoryInfo::LicenseNames() const
179 return fLicenseNames;
183 const BStringList&
184 BRepositoryInfo::LicenseTexts() const
186 return fLicenseTexts;
190 void
191 BRepositoryInfo::SetName(const BString& name)
193 fName = name;
197 void
198 BRepositoryInfo::SetOriginalBaseURL(const BString& url)
200 fOriginalBaseURL = url;
204 void
205 BRepositoryInfo::SetVendor(const BString& vendor)
207 fVendor = vendor;
211 void
212 BRepositoryInfo::SetSummary(const BString& summary)
214 fSummary = summary;
218 void
219 BRepositoryInfo::SetPriority(uint8 priority)
221 fPriority = priority;
225 void
226 BRepositoryInfo::SetArchitecture(BPackageArchitecture architecture)
228 fArchitecture = architecture;
232 status_t
233 BRepositoryInfo::AddLicense(const BString& licenseName,
234 const BString& licenseText)
236 if (!fLicenseNames.Add(licenseName) || !fLicenseTexts.Add(licenseText))
237 return B_NO_MEMORY;
239 return B_OK;
243 void
244 BRepositoryInfo::ClearLicenses()
246 fLicenseNames.MakeEmpty();
247 fLicenseTexts.MakeEmpty();
251 status_t
252 BRepositoryInfo::_SetTo(const BMessage* data)
254 if (data == NULL)
255 return B_BAD_VALUE;
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;
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;
285 return B_OK;
289 status_t
290 BRepositoryInfo::_SetTo(const BEntry& entry)
292 BFile file(&entry, B_READ_ONLY);
293 status_t result = file.InitCheck();
294 if (result != B_OK)
295 return result;
297 off_t size;
298 if ((result = file.GetSize(&size)) != B_OK)
299 return result;
301 BString configString;
302 char* buffer = configString.LockBuffer(size);
303 if (buffer == NULL)
304 return B_NO_MEMORY;
306 if ((result = file.Read(buffer, size)) < size) {
307 configString.UnlockBuffer(0);
308 return (result >= 0) ? B_IO_ERROR : result;
311 buffer[size] = '\0';
312 configString.UnlockBuffer(size);
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);
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);
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;
339 BPackageArchitecture architecture;
340 if (BPackageInfo::GetArchitectureByName(architectureString, architecture)
341 != B_OK || architecture == B_PACKAGE_ARCHITECTURE_ANY) {
342 return B_BAD_DATA;
345 fName = name;
346 fOriginalBaseURL = url;
347 fVendor = vendor;
348 fSummary = summary;
349 fPriority = atoi(priorityString);
350 fArchitecture = architecture;
352 return B_OK;
356 } // namespace BPackageKit