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