2 * Copyright 2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Ingo Weinhold <ingo_weinhold@gmx.de>
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()
30 fPackages(kInitialPackageListSize
, true),
36 BSolverRepository::BSolverRepository(const BString
& name
)
41 fPackages(kInitialPackageListSize
, true),
48 BSolverRepository::BSolverRepository(BPackageInstallationLocation location
)
53 fPackages(kInitialPackageListSize
, true),
60 BSolverRepository::BSolverRepository(BAllInstallationLocations
)
65 fPackages(kInitialPackageListSize
, true),
68 SetTo(B_ALL_INSTALLATION_LOCATIONS
);
72 BSolverRepository::BSolverRepository(const BRepositoryConfig
& config
)
77 fPackages(kInitialPackageListSize
, true),
84 BSolverRepository::~BSolverRepository()
90 BSolverRepository::SetTo(const BString
& name
)
95 return fName
.IsEmpty() ? B_BAD_VALUE
: B_OK
;
100 BSolverRepository::SetTo(BPackageInstallationLocation location
)
106 status_t error
= AddPackages(location
);
118 BSolverRepository::SetTo(BAllInstallationLocations
)
120 status_t error
= SetTo(B_PACKAGE_INSTALLATION_LOCATION_SYSTEM
);
124 error
= AddPackages(B_PACKAGE_INSTALLATION_LOCATION_HOME
);
135 BSolverRepository::SetTo(const BRepositoryConfig
& config
)
139 if (config
.InitCheck() != B_OK
)
142 fName
= config
.Name();
143 fPriority
= config
.Priority();
145 BPackageRoster roster
;
146 BRepositoryCache cache
;
147 status_t error
= roster
.GetRepositoryCache(config
.Name(), &cache
);
153 BRepositoryCache::Iterator it
= cache
.GetIterator();
154 while (const BPackageInfo
* packageInfo
= it
.Next()) {
155 error
= AddPackage(*packageInfo
);
167 BSolverRepository::SetTo(const BRepositoryCache
& cache
)
171 const BRepositoryInfo
& info
= cache
.Info();
172 if (info
.InitCheck() != B_OK
)
176 fPriority
= info
.Priority();
178 BRepositoryCache::Iterator it
= cache
.GetIterator();
179 while (const BPackageInfo
* packageInfo
= it
.Next()) {
180 status_t error
= AddPackage(*packageInfo
);
192 BSolverRepository::Unset()
196 fIsInstalled
= false;
197 fPackages
.MakeEmpty();
203 BSolverRepository::InitCheck()
205 return fName
.IsEmpty() ? B_NO_INIT
: B_OK
;
210 BSolverRepository::IsInstalled() const
217 BSolverRepository::SetInstalled(bool isInstalled
)
219 fIsInstalled
= isInstalled
;
225 BSolverRepository::Name() const
232 BSolverRepository::Priority() const
239 BSolverRepository::SetPriority(int32 priority
)
241 fPriority
= priority
;
247 BSolverRepository::IsEmpty() const
249 return fPackages
.IsEmpty();
254 BSolverRepository::CountPackages() const
256 return fPackages
.CountItems();
261 BSolverRepository::PackageAt(int32 index
) const
263 return fPackages
.ItemAt(index
);
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
)) {
279 if (_package
!= NULL
)
287 BSolverRepository::AddPackages(BPackageInstallationLocation location
)
289 BPackageRoster roster
;
290 BPackageInfoSet packageInfos
;
291 status_t error
= roster
.GetActivePackages(location
, packageInfos
);
295 BRepositoryCache::Iterator it
= packageInfos
.GetIterator();
296 while (const BPackageInfo
* packageInfo
= it
.Next()) {
297 error
= AddPackage(*packageInfo
);
307 BSolverRepository::RemovePackage(BSolverPackage
* package
)
309 if (!fPackages
.RemoveItem(package
, false))
318 BSolverRepository::DeletePackage(BSolverPackage
* package
)
320 if (!RemovePackage(package
))
329 BSolverRepository::ChangeCount() const
335 } // namespace BPackageKit