PackageManager.h revision 0de3219e
1/*
2 * Copyright 2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Ingo Weinhold <ingo_weinhold@gmx.de>
7 *		Rene Gollent <rene@gollent.com>
8 */
9#ifndef _PACKAGE__MANAGER__PRIVATE__PACKAGE_MANAGER_H_
10#define _PACKAGE__MANAGER__PRIVATE__PACKAGE_MANAGER_H_
11
12
13#include <Directory.h>
14#include <ObjectList.h>
15#include <package/Context.h>
16#include <package/PackageDefs.h>
17#include <package/PackageRoster.h>
18#include <package/RepositoryConfig.h>
19#include <package/solver/Solver.h>
20#include <package/solver/SolverRepository.h>
21
22#include <package/ActivationTransaction.h>
23#include <package/DaemonClient.h>
24#include <package/Job.h>
25
26
27namespace BPackageKit {
28
29namespace BManager {
30
31namespace BPrivate {
32
33
34using BPackageKit::BPrivate::BActivationTransaction;
35using BPackageKit::BPrivate::BDaemonClient;
36
37
38class BPackageManager : protected BJobStateListener {
39public:
40			class RemoteRepository;
41			class InstalledRepository;
42			class Transaction;
43			class InstallationInterface;
44			class ClientInstallationInterface;
45			class UserInteractionHandler;
46
47			typedef BObjectList<RemoteRepository> RemoteRepositoryList;
48			typedef BObjectList<InstalledRepository> InstalledRepositoryList;
49			typedef BObjectList<BSolverPackage> PackageList;
50			typedef BObjectList<Transaction> TransactionList;
51
52			enum {
53				B_ADD_INSTALLED_REPOSITORIES	= 0x01,
54				B_ADD_REMOTE_REPOSITORIES		= 0x02,
55				B_REFRESH_REPOSITORIES			= 0x04,
56			};
57
58public:
59								BPackageManager(
60									BPackageInstallationLocation location,
61									InstallationInterface*
62										installationInterface,
63									UserInteractionHandler*
64										userInteractionHandler);
65	virtual						~BPackageManager();
66
67			void				Init(uint32 flags);
68
69			BSolver*			Solver() const
70									{ return fSolver; }
71
72			const InstalledRepository* SystemRepository() const
73									{ return fSystemRepository; }
74			const InstalledRepository* HomeRepository() const
75									{ return fHomeRepository; }
76			const InstalledRepositoryList& InstalledRepositories() const
77									{ return fInstalledRepositories; }
78			const RemoteRepositoryList& OtherRepositories() const
79									{ return fOtherRepositories; }
80
81			void				Install(const char* const* packages,
82									int packageCount);
83			void				Install(const BSolverPackageSpecifierList&
84									packages);
85			void				Uninstall(const char* const* packages,
86									int packageCount);
87			void				Uninstall(const BSolverPackageSpecifierList&
88									packages);
89			void				Update(const char* const* packages,
90									int packageCount);
91			void				Update(const BSolverPackageSpecifierList&
92									packages);
93			void				FullSync();
94
95			void				VerifyInstallation();
96
97
98	virtual	status_t			DownloadPackage(const BString& fileURL,
99									const BEntry& targetEntry,
100									const BString& checksum);
101	virtual	status_t			RefreshRepository(
102									const BRepositoryConfig& repoConfig);
103
104protected:
105			InstalledRepository& InstallationRepository();
106
107protected:
108			// BJobStateListener
109	virtual	void				JobStarted(BJob* job);
110	virtual	void				JobProgress(BJob* job);
111	virtual	void				JobSucceeded(BJob* job);
112
113private:
114			void				_HandleProblems();
115			void				_AnalyzeResult();
116			void				_ConfirmChanges(bool fromMostSpecific = false);
117			void				_ApplyPackageChanges(
118									bool fromMostSpecific = false);
119			void				_PreparePackageChanges(
120									InstalledRepository&
121										installationRepository);
122			void				_CommitPackageChanges(Transaction& transaction);
123
124			void				_ClonePackageFile(
125									InstalledRepository* repository,
126									const BString& fileName,
127							 		const BEntry& entry);
128			int32				_FindBasePackage(const PackageList& packages,
129									const BPackageInfo& info);
130
131			void				_AddInstalledRepository(
132									InstalledRepository* repository);
133			void				_AddRemoteRepository(BPackageRoster& roster,
134									const char* name, bool refresh);
135			status_t			_GetRepositoryCache(BPackageRoster& roster,
136									const BRepositoryConfig& config,
137									bool refresh, BRepositoryCache& _cache);
138
139			bool				_NextSpecificInstallationLocation();
140
141protected:
142			BPackageInstallationLocation fLocation;
143			BSolver*			fSolver;
144			InstalledRepository* fSystemRepository;
145			InstalledRepository* fHomeRepository;
146			InstalledRepositoryList fInstalledRepositories;
147			RemoteRepositoryList fOtherRepositories;
148			TransactionList		fTransactions;
149
150			// must be set by the derived class
151			InstallationInterface* fInstallationInterface;
152			UserInteractionHandler* fUserInteractionHandler;
153};
154
155
156class BPackageManager::RemoteRepository : public BSolverRepository {
157public:
158								RemoteRepository(
159									const BRepositoryConfig& config);
160
161			const BRepositoryConfig& Config() const;
162
163private:
164			BRepositoryConfig	fConfig;
165};
166
167
168class BPackageManager::InstalledRepository : public BSolverRepository {
169public:
170			typedef BObjectList<BSolverPackage> PackageList;
171
172public:
173								InstalledRepository(const char* name,
174									BPackageInstallationLocation location,
175									int32 priority);
176
177			BPackageInstallationLocation Location() const
178									{ return fLocation; }
179			const char*			InitialName() const
180									{ return fInitialName; }
181			int32				InitialPriority() const
182									{ return fInitialPriority; }
183
184			void				DisablePackage(BSolverPackage* package);
185									// throws, if already disabled
186			bool				EnablePackage(BSolverPackage* package);
187									// returns whether it was disabled
188
189			PackageList&		PackagesToActivate()
190									{ return fPackagesToActivate; }
191			PackageList&		PackagesToDeactivate()
192									{ return fPackagesToDeactivate; }
193
194			bool				HasChanges() const;
195			void				ApplyChanges();
196
197private:
198			PackageList			fDisabledPackages;
199			PackageList			fPackagesToActivate;
200			PackageList			fPackagesToDeactivate;
201			const char*			fInitialName;
202			BPackageInstallationLocation fLocation;
203			int32				fInitialPriority;
204};
205
206
207class BPackageManager::Transaction {
208public:
209								Transaction(InstalledRepository& repository);
210								~Transaction();
211
212			InstalledRepository& Repository()
213									{ return fRepository; }
214			BActivationTransaction& ActivationTransaction()
215									{ return fTransaction; }
216			BDirectory&			TransactionDirectory()
217									{ return fTransactionDirectory; }
218
219private:
220			InstalledRepository& fRepository;
221			BActivationTransaction fTransaction;
222			BDirectory			fTransactionDirectory;
223};
224
225
226class BPackageManager::InstallationInterface {
227public:
228	virtual						~InstallationInterface();
229
230	virtual	void				InitInstalledRepository(
231									InstalledRepository& repository) = 0;
232	virtual	void				ResultComputed(InstalledRepository& repository);
233
234	virtual	status_t			PrepareTransaction(Transaction& transaction)
235									= 0;
236	virtual	status_t			CommitTransaction(Transaction& transaction,
237									BCommitTransactionResult& _result) = 0;
238};
239
240
241class BPackageManager::ClientInstallationInterface
242	: public InstallationInterface {
243public:
244								ClientInstallationInterface();
245	virtual						~ClientInstallationInterface();
246
247	virtual	void				InitInstalledRepository(
248									InstalledRepository& repository);
249
250	virtual	status_t			PrepareTransaction(Transaction& transaction);
251	virtual	status_t			CommitTransaction(Transaction& transaction,
252									BCommitTransactionResult& _result);
253
254private:
255			BDaemonClient		fDaemonClient;
256};
257
258
259class BPackageManager::UserInteractionHandler {
260public:
261	virtual						~UserInteractionHandler();
262
263	virtual	void				HandleProblems();
264	virtual	void				ConfirmChanges(bool fromMostSpecific);
265
266	virtual	void				Warn(status_t error, const char* format, ...);
267
268	virtual	void				ProgressPackageDownloadStarted(
269									const char* packageName);
270	virtual	void				ProgressPackageDownloadActive(
271									const char* packageName,
272									float completionPercentage);
273	virtual	void				ProgressPackageDownloadComplete(
274									const char* packageName);
275	virtual	void				ProgressPackageChecksumStarted(
276									const char* title);
277	virtual	void				ProgressPackageChecksumComplete(
278									const char* title);
279
280	virtual	void				ProgressStartApplyingChanges(
281									InstalledRepository& repository);
282	virtual	void				ProgressTransactionCommitted(
283									InstalledRepository& repository,
284									const char* transactionDirectoryName);
285	virtual	void				ProgressApplyingChangesDone(
286									InstalledRepository& repository);
287};
288
289
290}	// namespace BPrivate
291
292}	// namespace BManager
293
294}	// namespace BPackageKit
295
296
297#endif	// _PACKAGE__MANAGER__PRIVATE__PACKAGE_MANAGER_H_
298