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