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