1 /* 2 * Copyright 2024, Andrew Lindesay <apl@lindesay.co.nz>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "PackageClassificationInfo.h" 8 9 #include <algorithm> 10 11 #include "HaikuDepotConstants.h" 12 13 14 PackageClassificationInfo::PackageClassificationInfo() 15 : 16 fCategories(), 17 fProminence(-1L), 18 fIsNativeDesktop(false) 19 { 20 } 21 22 23 PackageClassificationInfo::PackageClassificationInfo(const PackageClassificationInfo& other) 24 : 25 fCategories(other.fCategories), 26 fProminence(other.Prominence()), 27 fIsNativeDesktop(other.IsNativeDesktop()) 28 { 29 } 30 31 32 int32 33 PackageClassificationInfo::CountCategories() const 34 { 35 return fCategories.size(); 36 } 37 38 39 CategoryRef 40 PackageClassificationInfo::CategoryAtIndex(int32 index) const 41 { 42 return fCategories[index]; 43 } 44 45 46 void 47 PackageClassificationInfo::ClearCategories() 48 { 49 if (!fCategories.empty()) 50 fCategories.clear(); 51 } 52 53 54 bool 55 PackageClassificationInfo::AddCategory(const CategoryRef& category) 56 { 57 std::vector<CategoryRef>::const_iterator itInsertionPt = std::lower_bound(fCategories.begin(), 58 fCategories.end(), category, &IsPackageCategoryBefore); 59 60 if (itInsertionPt == fCategories.end()) { 61 fCategories.push_back(category); 62 return true; 63 } 64 return false; 65 } 66 67 68 bool 69 PackageClassificationInfo::HasCategoryByCode(const BString& code) const 70 { 71 for (int32 i = CountCategories() - 1; i >= 0; i--) { 72 if (CategoryAtIndex(i)->Code() == code) 73 return true; 74 } 75 76 return false; 77 } 78 79 80 void 81 PackageClassificationInfo::SetProminence(uint32 prominence) 82 { 83 fProminence = prominence; 84 } 85 86 87 bool 88 PackageClassificationInfo::IsProminent() const 89 { 90 return HasProminence() && Prominence() <= PROMINANCE_ORDERING_PROMINENT_MAX; 91 } 92 93 94 bool 95 PackageClassificationInfo::IsNativeDesktop() const 96 { 97 return fIsNativeDesktop; 98 } 99 100 101 void 102 PackageClassificationInfo::SetIsNativeDesktop(bool value) 103 { 104 fIsNativeDesktop = value; 105 } 106 107 108 bool 109 PackageClassificationInfo::operator==(const PackageClassificationInfo& other) const 110 { 111 if (fIsNativeDesktop != other.IsNativeDesktop()) 112 return false; 113 114 if (fProminence != other.Prominence()) 115 return false; 116 117 if (CountCategories() != other.CountCategories()) 118 return false; 119 120 for (int32 i = CountCategories() - 1; i >= 0; i--) { 121 if (other.CategoryAtIndex(i) != CategoryAtIndex(i)) 122 return false; 123 } 124 125 return true; 126 } 127 128 129 bool 130 PackageClassificationInfo::operator!=(const PackageClassificationInfo& other) const 131 { 132 return !(*this == other); 133 } 134