2 * Copyright 2011, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Oliver Tappe <zooey@hirschkaefer.de>
10 #include <package/RepositoryInfo.h>
16 #include <driver_settings.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
)
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
);
79 BRepositoryInfo::Archive(BMessage
* data
, bool deep
) const
81 status_t result
= inherited::Archive(data
, deep
);
85 if ((result
= data
->AddString(kNameField
, fName
)) != B_OK
)
87 if ((result
= data
->AddString(kURLField
, fOriginalBaseURL
)) != B_OK
)
89 if ((result
= data
->AddString(kVendorField
, fVendor
)) != B_OK
)
91 result
= data
->AddString(kSummaryField
, fSummary
);
94 if ((result
= data
->AddUInt8(kPriorityField
, fPriority
)) != B_OK
)
96 if ((result
= data
->AddUInt8(kArchitectureField
, fArchitecture
)) != B_OK
)
98 for (int i
= 0; i
< fLicenseNames
.CountStrings(); ++i
) {
99 result
= data
->AddString(kLicenseNameField
, fLicenseNames
.StringAt(i
));
103 for (int i
= 0; i
< fLicenseTexts
.CountStrings(); ++i
) {
104 result
= data
->AddString(kLicenseTextField
, fLicenseTexts
.StringAt(i
));
114 BRepositoryInfo::InitCheck() const
121 BRepositoryInfo::SetTo(const BMessage
* data
)
123 return fInitStatus
= _SetTo(data
);
128 BRepositoryInfo::SetTo(const BEntry
& entry
)
130 return fInitStatus
= _SetTo(entry
);
135 BRepositoryInfo::Name() const
142 BRepositoryInfo::OriginalBaseURL() const
144 return fOriginalBaseURL
;
149 BRepositoryInfo::Vendor() const
156 BRepositoryInfo::Summary() const
163 BRepositoryInfo::Priority() const
170 BRepositoryInfo::Architecture() const
172 return fArchitecture
;
177 BRepositoryInfo::LicenseNames() const
179 return fLicenseNames
;
184 BRepositoryInfo::LicenseTexts() const
186 return fLicenseTexts
;
191 BRepositoryInfo::SetName(const BString
& name
)
198 BRepositoryInfo::SetOriginalBaseURL(const BString
& url
)
200 fOriginalBaseURL
= url
;
205 BRepositoryInfo::SetVendor(const BString
& vendor
)
212 BRepositoryInfo::SetSummary(const BString
& summary
)
219 BRepositoryInfo::SetPriority(uint8 priority
)
221 fPriority
= priority
;
226 BRepositoryInfo::SetArchitecture(BPackageArchitecture architecture
)
228 fArchitecture
= architecture
;
233 BRepositoryInfo::AddLicense(const BString
& licenseName
,
234 const BString
& licenseText
)
236 if (!fLicenseNames
.Add(licenseName
) || !fLicenseTexts
.Add(licenseText
))
244 BRepositoryInfo::ClearLicenses()
246 fLicenseNames
.MakeEmpty();
247 fLicenseTexts
.MakeEmpty();
252 BRepositoryInfo::_SetTo(const BMessage
* data
)
258 if ((result
= data
->FindString(kNameField
, &fName
)) != B_OK
)
260 if ((result
= data
->FindString(kURLField
, &fOriginalBaseURL
)) != B_OK
)
262 if ((result
= data
->FindString(kVendorField
, &fVendor
)) != B_OK
)
264 result
= data
->FindString(kSummaryField
, &fSummary
);
267 if ((result
= data
->FindUInt8(kPriorityField
, &fPriority
)) != B_OK
)
269 result
= data
->FindUInt8(kArchitectureField
, (uint8
*)&fArchitecture
);
272 if (fArchitecture
== B_PACKAGE_ARCHITECTURE_ANY
)
275 const char* licenseName
;
276 const char* licenseText
;
278 data
->FindString(kLicenseNameField
, i
, &licenseName
) == B_OK
279 && data
->FindString(kLicenseTextField
, i
, &licenseText
) == B_OK
;
281 if (!fLicenseNames
.Add(licenseName
) || !fLicenseTexts
.Add(licenseText
))
290 BRepositoryInfo::_SetTo(const BEntry
& entry
)
292 BFile
file(&entry
, B_READ_ONLY
);
293 status_t result
= file
.InitCheck();
298 if ((result
= file
.GetSize(&size
)) != B_OK
)
301 BString configString
;
302 char* buffer
= configString
.LockBuffer(size
);
306 if ((result
= file
.Read(buffer
, size
)) < size
) {
307 configString
.UnlockBuffer(0);
308 return (result
>= 0) ? B_IO_ERROR
: result
;
312 configString
.UnlockBuffer(size
);
314 void* settingsHandle
= parse_driver_settings_string(configString
.String());
315 if (settingsHandle
== NULL
)
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
);
323 = get_driver_parameter(settingsHandle
, "vendor", NULL
, NULL
);
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') {
339 BPackageArchitecture architecture
;
340 if (BPackageInfo::GetArchitectureByName(architectureString
, architecture
)
341 != B_OK
|| architecture
== B_PACKAGE_ARCHITECTURE_ANY
) {
346 fOriginalBaseURL
= url
;
349 fPriority
= atoi(priorityString
);
350 fArchitecture
= architecture
;
356 } // namespace BPackageKit