BPicture: Fix archive constructor.
[haiku.git] / src / kits / package / solver / SolverRepository.cpp
blobd88d7639150a0786291579beaae163e0ac159847
1 /*
2 * Copyright 2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Ingo Weinhold <ingo_weinhold@gmx.de>
7 */
10 #include <package/solver/SolverRepository.h>
12 #include <package/PackageDefs.h>
13 #include <package/PackageRoster.h>
14 #include <package/RepositoryCache.h>
15 #include <package/RepositoryConfig.h>
16 #include <package/solver/SolverPackage.h>
19 static const int32 kInitialPackageListSize = 40;
22 namespace BPackageKit {
25 BSolverRepository::BSolverRepository()
27 fName(),
28 fPriority(0),
29 fIsInstalled(false),
30 fPackages(kInitialPackageListSize, true),
31 fChangeCount(0)
36 BSolverRepository::BSolverRepository(const BString& name)
38 fName(),
39 fPriority(0),
40 fIsInstalled(false),
41 fPackages(kInitialPackageListSize, true),
42 fChangeCount(0)
44 SetTo(name);
48 BSolverRepository::BSolverRepository(BPackageInstallationLocation location)
50 fName(),
51 fPriority(0),
52 fIsInstalled(false),
53 fPackages(kInitialPackageListSize, true),
54 fChangeCount(0)
56 SetTo(location);
60 BSolverRepository::BSolverRepository(BAllInstallationLocations)
62 fName(),
63 fPriority(0),
64 fIsInstalled(false),
65 fPackages(kInitialPackageListSize, true),
66 fChangeCount(0)
68 SetTo(B_ALL_INSTALLATION_LOCATIONS);
72 BSolverRepository::BSolverRepository(const BRepositoryConfig& config)
74 fName(),
75 fPriority(0),
76 fIsInstalled(false),
77 fPackages(kInitialPackageListSize, true),
78 fChangeCount(0)
80 SetTo(config);
84 BSolverRepository::~BSolverRepository()
89 status_t
90 BSolverRepository::SetTo(const BString& name)
92 Unset();
94 fName = name;
95 return fName.IsEmpty() ? B_BAD_VALUE : B_OK;
99 status_t
100 BSolverRepository::SetTo(BPackageInstallationLocation location)
102 Unset();
104 fName = "Installed";
106 status_t error = AddPackages(location);
107 if (error != B_OK) {
108 Unset();
109 return error;
112 fIsInstalled = true;
113 return B_OK;
117 status_t
118 BSolverRepository::SetTo(BAllInstallationLocations)
120 status_t error = SetTo(B_PACKAGE_INSTALLATION_LOCATION_SYSTEM);
121 if (error != B_OK)
122 return error;
124 error = AddPackages(B_PACKAGE_INSTALLATION_LOCATION_HOME);
125 if (error != B_OK) {
126 Unset();
127 return error;
130 return B_OK;
134 status_t
135 BSolverRepository::SetTo(const BRepositoryConfig& config)
137 Unset();
139 if (config.InitCheck() != B_OK)
140 return B_BAD_VALUE;
142 fName = config.Name();
143 fPriority = config.Priority();
145 BPackageRoster roster;
146 BRepositoryCache cache;
147 status_t error = roster.GetRepositoryCache(config.Name(), &cache);
148 if (error != B_OK) {
149 Unset();
150 return error;
153 BRepositoryCache::Iterator it = cache.GetIterator();
154 while (const BPackageInfo* packageInfo = it.Next()) {
155 error = AddPackage(*packageInfo);
156 if (error != B_OK) {
157 Unset();
158 return error;
162 return B_OK;
166 status_t
167 BSolverRepository::SetTo(const BRepositoryCache& cache)
169 Unset();
171 const BRepositoryInfo& info = cache.Info();
172 if (info.InitCheck() != B_OK)
173 return B_BAD_VALUE;
175 fName = info.Name();
176 fPriority = info.Priority();
178 BRepositoryCache::Iterator it = cache.GetIterator();
179 while (const BPackageInfo* packageInfo = it.Next()) {
180 status_t error = AddPackage(*packageInfo);
181 if (error != B_OK) {
182 Unset();
183 return error;
187 return B_OK;
191 void
192 BSolverRepository::Unset()
194 fName = BString();
195 fPriority = 0;
196 fIsInstalled = false;
197 fPackages.MakeEmpty();
198 fChangeCount++;
202 status_t
203 BSolverRepository::InitCheck()
205 return fName.IsEmpty() ? B_NO_INIT : B_OK;
209 bool
210 BSolverRepository::IsInstalled() const
212 return fIsInstalled;
216 void
217 BSolverRepository::SetInstalled(bool isInstalled)
219 fIsInstalled = isInstalled;
220 fChangeCount++;
224 BString
225 BSolverRepository::Name() const
227 return fName;
231 int32
232 BSolverRepository::Priority() const
234 return fPriority;
238 void
239 BSolverRepository::SetPriority(int32 priority)
241 fPriority = priority;
242 fChangeCount++;
246 bool
247 BSolverRepository::IsEmpty() const
249 return fPackages.IsEmpty();
253 int32
254 BSolverRepository::CountPackages() const
256 return fPackages.CountItems();
260 BSolverPackage*
261 BSolverRepository::PackageAt(int32 index) const
263 return fPackages.ItemAt(index);
267 status_t
268 BSolverRepository::AddPackage(const BPackageInfo& info,
269 BSolverPackage** _package)
271 BSolverPackage* package = new(std::nothrow) BSolverPackage(this, info);
272 if (package == NULL || !fPackages.AddItem(package)) {
273 delete package;
274 return B_NO_MEMORY;
277 fChangeCount++;
279 if (_package != NULL)
280 *_package = package;
282 return B_OK;
286 status_t
287 BSolverRepository::AddPackages(BPackageInstallationLocation location)
289 BPackageRoster roster;
290 BPackageInfoSet packageInfos;
291 status_t error = roster.GetActivePackages(location, packageInfos);
292 if (error != B_OK)
293 return error;
295 BRepositoryCache::Iterator it = packageInfos.GetIterator();
296 while (const BPackageInfo* packageInfo = it.Next()) {
297 error = AddPackage(*packageInfo);
298 if (error != B_OK)
299 return error;
302 return B_OK;
306 bool
307 BSolverRepository::RemovePackage(BSolverPackage* package)
309 if (!fPackages.RemoveItem(package, false))
310 return false;
312 fChangeCount++;
313 return true;
317 bool
318 BSolverRepository::DeletePackage(BSolverPackage* package)
320 if (!RemovePackage(package))
321 return false;
323 delete package;
324 return true;
328 uint64
329 BSolverRepository::ChangeCount() const
331 return fChangeCount;
335 } // namespace BPackageKit