1/*
2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * Copyright 2013, Rene Gollent, <rene@gollent.com>
4 *
5 * All rights reserved. Distributed under the terms of the MIT License.
6 */
7
8
9#include "PackageAction.h"
10
11#include "Model.h"
12#include "PackageManager.h"
13
14
15using namespace BPackageKit;
16
17// #pragma mark - PackageAction
18
19
20PackageAction::PackageAction(int32 type, PackageInfoRef package, Model* model)
21	:
22	fPackage(package),
23	fType(type),
24	fModel(model),
25	fInstallLocation(InstallLocation(package))
26{
27	// TODO: ideally if the package is installed at multiple locations,
28	// the user should be able to pick which one to remove.
29	// TODO: allow configuring the installation location
30	fPackageManager = new(std::nothrow) PackageManager(
31		(BPackageInstallationLocation)fInstallLocation);
32}
33
34
35PackageAction::~PackageAction()
36{
37	delete fPackageManager;
38}
39
40
41PackageInfoRef
42PackageAction::FindPackageByName(const BString& name)
43{
44	Model* model = GetModel();
45	const DepotList& depots = model->Depots();
46	// TODO: optimize!
47	for (int32 i = 0; i < depots.CountItems(); i++) {
48		const DepotInfo& depot = depots.ItemAtFast(i);
49		const PackageList& packages = depot.Packages();
50		for (int32 j = 0; j < packages.CountItems(); j++) {
51			PackageInfoRef info = packages.ItemAtFast(j);
52			if (info->Name() == name)
53				return info;
54		}
55	}
56
57	return PackageInfoRef();
58}
59
60
61int32
62PackageAction::InstallLocation(const PackageInfoRef& package)
63{
64	const PackageInstallationLocationSet& locations
65		= package->InstallationLocations();
66
67	// If the package is already installed, return its first installed location
68	if (locations.size() != 0)
69		return *locations.begin();
70
71	return B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
72}
73
74
75