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 PackageClassificationInfo()14PackageClassificationInfo::PackageClassificationInfo() 15 : 16 fCategories(), 17 fProminence(-1L), 18 fIsNativeDesktop(false) 19 { 20 } 21 22 PackageClassificationInfo(const PackageClassificationInfo & other)23PackageClassificationInfo::PackageClassificationInfo(const PackageClassificationInfo& other) 24 : 25 fCategories(other.fCategories), 26 fProminence(other.Prominence()), 27 fIsNativeDesktop(other.IsNativeDesktop()) 28 { 29 } 30 31 32 int32 CountCategories() const33PackageClassificationInfo::CountCategories() const 34 { 35 return fCategories.size(); 36 } 37 38 39 CategoryRef CategoryAtIndex(int32 index) const40PackageClassificationInfo::CategoryAtIndex(int32 index) const 41 { 42 return fCategories[index]; 43 } 44 45 46 void ClearCategories()47PackageClassificationInfo::ClearCategories() 48 { 49 if (!fCategories.empty()) 50 fCategories.clear(); 51 } 52 53 54 bool AddCategory(const CategoryRef & category)55PackageClassificationInfo::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 HasCategoryByCode(const BString & code) const69PackageClassificationInfo::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 SetProminence(uint32 prominence)81PackageClassificationInfo::SetProminence(uint32 prominence) 82 { 83 fProminence = prominence; 84 } 85 86 87 bool IsProminent() const88PackageClassificationInfo::IsProminent() const 89 { 90 return HasProminence() && Prominence() <= PROMINANCE_ORDERING_PROMINENT_MAX; 91 } 92 93 94 bool IsNativeDesktop() const95PackageClassificationInfo::IsNativeDesktop() const 96 { 97 return fIsNativeDesktop; 98 } 99 100 101 void SetIsNativeDesktop(bool value)102PackageClassificationInfo::SetIsNativeDesktop(bool value) 103 { 104 fIsNativeDesktop = value; 105 } 106 107 108 bool operator ==(const PackageClassificationInfo & other) const109PackageClassificationInfo::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 operator !=(const PackageClassificationInfo & other) const130PackageClassificationInfo::operator!=(const PackageClassificationInfo& other) const 131 { 132 return !(*this == other); 133 } 134