xref: /haiku/src/apps/haikudepot/packagemodel/PackageFilterModel.cpp (revision 363fc60bf3eb6fd69f0bc6f8448855eecfedfa9d)
1 /*
2  * Copyright 2024, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #include "PackageFilterModel.h"
6 
7 
PackageFilterModel()8 PackageFilterModel::PackageFilterModel()
9 	:
10 	fSearchTerms(""),
11 	fDepotName(""),
12 	fCategory(""),
13 	fShowAvailablePackages(true),
14 	fShowInstalledPackages(true),
15 	fShowSourcePackages(false),
16 	fShowDevelopPackages(false)
17 {
18 	fFilter = _CreateFilter();
19 }
20 
21 
~PackageFilterModel()22 PackageFilterModel::~PackageFilterModel()
23 {
24 }
25 
26 
27 BString
SearchTerms() const28 PackageFilterModel::SearchTerms() const
29 {
30 	return fSearchTerms;
31 }
32 
33 
34 BString
DepotName() const35 PackageFilterModel::DepotName() const
36 {
37 	return fDepotName;
38 }
39 
40 
41 BString
Category() const42 PackageFilterModel::Category() const
43 {
44 	return fCategory;
45 }
46 
47 
48 bool
ShowAvailablePackages() const49 PackageFilterModel::ShowAvailablePackages() const
50 {
51 	return fShowAvailablePackages;
52 }
53 
54 
55 bool
ShowInstalledPackages() const56 PackageFilterModel::ShowInstalledPackages() const
57 {
58 	return fShowInstalledPackages;
59 }
60 
61 
62 bool
ShowSourcePackages() const63 PackageFilterModel::ShowSourcePackages() const
64 {
65 	return fShowSourcePackages;
66 }
67 
68 
69 bool
ShowDevelopPackages() const70 PackageFilterModel::ShowDevelopPackages() const
71 {
72 	return fShowDevelopPackages;
73 }
74 
75 
76 void
SetSearchTerms(BString value)77 PackageFilterModel::SetSearchTerms(BString value)
78 {
79 	if (fSearchTerms != value) {
80 		fSearchTerms = value;
81 		fFilter = _CreateFilter();
82 	}
83 }
84 
85 
86 void
SetDepotName(BString value)87 PackageFilterModel::SetDepotName(BString value)
88 {
89 	if (fDepotName != value) {
90 		fDepotName = value;
91 		fFilter = _CreateFilter();
92 	}
93 }
94 
95 
96 void
SetCategory(BString value)97 PackageFilterModel::SetCategory(BString value)
98 {
99 	if (fCategory != value) {
100 		fCategory = value;
101 		fFilter = _CreateFilter();
102 	}
103 }
104 
105 
106 void
SetShowAvailablePackages(bool value)107 PackageFilterModel::SetShowAvailablePackages(bool value)
108 {
109 	if (fShowAvailablePackages != value) {
110 		fShowAvailablePackages = value;
111 		fFilter = _CreateFilter();
112 	}
113 }
114 
115 
116 void
SetShowInstalledPackages(bool value)117 PackageFilterModel::SetShowInstalledPackages(bool value)
118 {
119 	if (fShowInstalledPackages != value) {
120 		fShowInstalledPackages = value;
121 		fFilter = _CreateFilter();
122 	}
123 }
124 
125 
126 void
SetShowSourcePackages(bool value)127 PackageFilterModel::SetShowSourcePackages(bool value)
128 {
129 	if (fShowSourcePackages != value) {
130 		fShowSourcePackages = value;
131 		fFilter = _CreateFilter();
132 	}
133 }
134 
135 
136 void
SetShowDevelopPackages(bool value)137 PackageFilterModel::SetShowDevelopPackages(bool value)
138 {
139 	if (fShowDevelopPackages != value) {
140 		fShowDevelopPackages = value;
141 		fFilter = _CreateFilter();
142 	}
143 }
144 
145 
146 PackageFilterRef
Filter()147 PackageFilterModel::Filter()
148 {
149 	return fFilter;
150 }
151 
152 
153 PackageFilterRef
_CreateFilter() const154 PackageFilterModel::_CreateFilter() const
155 {
156 	AndFilter* andFilter = new AndFilter();
157 	if (!fSearchTerms.IsEmpty())
158 		andFilter->AddFilter(PackageFilterFactory::CreateSearchTermsFilter(fSearchTerms));
159 	if (!fDepotName.IsEmpty())
160 		andFilter->AddFilter(PackageFilterFactory::CreateDepotFilter(fDepotName));
161 	if (!fCategory.IsEmpty())
162 		andFilter->AddFilter(PackageFilterFactory::CreateCategoryFilter(fCategory));
163 	if (!fShowAvailablePackages) {
164 		andFilter->AddFilter(
165 			PackageFilterRef(new NotFilter(PackageFilterFactory::CreateStateFilter(NONE)), true));
166 	}
167 	if (!fShowInstalledPackages) {
168 		andFilter->AddFilter(PackageFilterRef(
169 			new NotFilter(PackageFilterFactory::CreateStateFilter(ACTIVATED)), true));
170 	}
171 	if (!fShowSourcePackages) {
172 		andFilter->AddFilter(
173 			PackageFilterRef(new NotFilter(PackageFilterFactory::CreateSourceFilter()), true));
174 	}
175 	if (!fShowDevelopPackages) {
176 		andFilter->AddFilter(
177 			PackageFilterRef(new NotFilter(PackageFilterFactory::CreateDevelopmentFilter()), true));
178 	}
179 	return PackageFilterRef(andFilter, true);
180 }
181