PackageManager.h revision 6722e1ae
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
94			void				VerifyInstallation();
95
96
97	virtual	status_t			DownloadPackage(const BString& fileURL,
98									const BEntry& targetEntry,
99									const BString& checksum);
100	virtual	status_t			RefreshRepository(
101									const BRepositoryConfig& repoConfig);
102
103protected:
104			InstalledRepository& InstallationRepository();
105
106protected:
107			// BJobStateListener
108	virtual	void				JobStarted(BJob* job);
109	virtual	void				JobProgress(BJob* job);
110	virtual	void				JobSucceeded(BJob* job);
111
112private:
113			void				_HandleProblems();
114			void				_AnalyzeResult();
115			void				_ConfirmChanges(bool fromMostSpecific = false);
116			void				_ApplyPackageChanges(
117									bool fromMostSpecific = false);
118			void				_PreparePackageChanges(
119									InstalledRepository&
120										installationRepository);
121			void				_CommitPackageChanges(Transaction& transaction);
122
123			void				_ClonePackageFile(
124									InstalledRepository* repository,
125									const BString& fileName,
126							 		const BEntry& entry);
127			int32				_FindBasePackage(const PackageList& packages,
128									const BPackageInfo& info);
129
130			void				_AddInstalledRepository(
131									InstalledRepository* repository);
132			void				_AddRemoteRepository(BPackageRoster& roster,
133									const char* name, bool refresh);
134			status_t			_GetRepositoryCache(BPackageRoster& roster,
135									const BRepositoryConfig& config,
136									bool refresh, BRepositoryCache& _cache);
137
138			bool				_NextSpecificInstallationLocation();
139
140protected:
141			BPackageInstallationLocation fLocation;
142			BSolver*			fSolver;
143			InstalledRepository* fSystemRepository;
144			InstalledRepository* fHomeRepository;
145			InstalledRepositoryList fInstalledRepositories;
146			RemoteRepositoryList fOtherRepositories;
147			TransactionList		fTransactions;
148
149			// must be set by the derived class
150			InstallationInterface* fInstallationInterface;
151			UserInteractionHandler* fUserInteractionHandler;
152};
153
154
155class BPackageManager::RemoteRepository : public BSolverRepository {
156public:
157								RemoteRepository(
158									const BRepositoryConfig& config);
159
160			const BRepositoryConfig& Config() const;
161
162private:
163			BRepositoryConfig	fConfig;
164};
165
166
167class BPackageManager::InstalledRepository : public BSolverRepository {
168public:
169			typedef BObjectList<BSolverPackage> PackageList;
170
171public:
172								InstalledRepository(const char* name,
173									BPackageInstallationLocation location,
174									int32 priority);
175
176			BPackageInstallationLocation Location() const
177									{ return fLocation; }
178			const char*			InitialName() const
179									{ return fInitialName; }
180			int32				InitialPriority() const
181									{ return fInitialPriority; }
182
183			void				DisablePackage(BSolverPackage* package);
184									// throws, if already disabled
185			bool				EnablePackage(BSolverPackage* package);
186									// returns whether it was disabled
187
188			PackageList&		PackagesToActivate()
189									{ return fPackagesToActivate; }
190			PackageList&		PackagesToDeactivate()
191									{ return fPackagesToDeactivate; }
192
193			bool				HasChanges() const;
194			void				ApplyChanges();
195
196private:
197			PackageList			fDisabledPackages;
198			PackageList			fPackagesToActivate;
199			PackageList			fPackagesToDeactivate;
200			const char*			fInitialName;
201			BPackageInstallationLocation fLocation;
202			int32				fInitialPriority;
203};
204
205
206class BPackageManager::Transaction {
207public:
208								Transaction(InstalledRepository& repository);
209								~Transaction();
210
211			InstalledRepository& Repository()
212									{ return fRepository; }
213			BActivationTransaction& ActivationTransaction()
214									{ return fTransaction; }
215			BDirectory&			TransactionDirectory()
216									{ return fTransactionDirectory; }
217
218private:
219			InstalledRepository& fRepository;
220			BActivationTransaction fTransaction;
221			BDirectory			fTransactionDirectory;
222};
223
224
225class BPackageManager::InstallationInterface {
226public:
227	virtual						~InstallationInterface();
228
229	virtual	void				InitInstalledRepository(
230									InstalledRepository& repository) = 0;
231	virtual	void				ResultComputed(InstalledRepository& repository);
232
233	virtual	status_t			PrepareTransaction(Transaction& transaction)
234									= 0;
235	virtual	status_t			CommitTransaction(Transaction& transaction,
236									BDaemonClient::BCommitTransactionResult&
237										_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									BDaemonClient::BCommitTransactionResult&
253										_result);
254
255private:
256			BDaemonClient		fDaemonClient;
257};
258
259
260class BPackageManager::UserInteractionHandler {
261public:
262	virtual						~UserInteractionHandler();
263
264	virtual	void				HandleProblems() = 0;
265	virtual	void				ConfirmChanges(bool fromMostSpecific) = 0;
266
267	virtual	void				Warn(status_t error, const char* format, ...)
268									= 0;
269
270	virtual	void				ProgressPackageDownloadStarted(
271									const char* packageName) = 0;
272	virtual	void				ProgressPackageDownloadActive(
273									const char* packageName,
274									float completionPercentage) = 0;
275	virtual	void				ProgressPackageDownloadComplete(
276									const char* packageName) = 0;
277	virtual	void				ProgressPackageChecksumStarted(
278									const char* title) = 0;
279	virtual	void				ProgressPackageChecksumComplete(
280									const char* title) = 0;
281
282	virtual	void				ProgressStartApplyingChanges(
283									InstalledRepository& repository) = 0;
284	virtual	void				ProgressTransactionCommitted(
285									InstalledRepository& repository,
286									const char* transactionDirectoryName) = 0;
287	virtual	void				ProgressApplyingChangesDone(
288									InstalledRepository& repository) = 0;
289};
290
291
292}	// namespace BPrivate
293
294}	// namespace BManager
295
296}	// namespace BPackageKit
297
298
299#endif	// _PACKAGE__MANAGER__PRIVATE__PACKAGE_MANAGER_H_
300