xref: /haiku/headers/private/package/manager/PackageManager.h (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
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 
27 namespace BPackageKit {
28 
29 namespace BManager {
30 
31 namespace BPrivate {
32 
33 
34 using BPackageKit::BPrivate::BActivationTransaction;
35 using BPackageKit::BPrivate::BDaemonClient;
36 
37 
38 class BPackageManager : protected BJobStateListener {
39 public:
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 
58 public:
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 
103 protected:
104 			InstalledRepository& InstallationRepository();
105 
106 protected:
107 			// BJobStateListener
108 	virtual	void				JobStarted(BJob* job);
109 	virtual	void				JobProgress(BJob* job);
110 	virtual	void				JobSucceeded(BJob* job);
111 
112 private:
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 
140 protected:
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 
155 class BPackageManager::RemoteRepository : public BSolverRepository {
156 public:
157 								RemoteRepository(
158 									const BRepositoryConfig& config);
159 
160 			const BRepositoryConfig& Config() const;
161 
162 private:
163 			BRepositoryConfig	fConfig;
164 };
165 
166 
167 class BPackageManager::InstalledRepository : public BSolverRepository {
168 public:
169 			typedef BObjectList<BSolverPackage> PackageList;
170 
171 public:
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 
196 private:
197 			PackageList			fDisabledPackages;
198 			PackageList			fPackagesToActivate;
199 			PackageList			fPackagesToDeactivate;
200 			const char*			fInitialName;
201 			BPackageInstallationLocation fLocation;
202 			int32				fInitialPriority;
203 };
204 
205 
206 class BPackageManager::Transaction {
207 public:
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 
218 private:
219 			InstalledRepository& fRepository;
220 			BActivationTransaction fTransaction;
221 			BDirectory			fTransactionDirectory;
222 };
223 
224 
225 class BPackageManager::InstallationInterface {
226 public:
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 
241 class BPackageManager::ClientInstallationInterface
242 	: public InstallationInterface {
243 public:
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 
255 private:
256 			BDaemonClient		fDaemonClient;
257 };
258 
259 
260 class BPackageManager::UserInteractionHandler {
261 public:
262 	virtual						~UserInteractionHandler();
263 
264 	virtual	void				HandleProblems();
265 	virtual	void				ConfirmChanges(bool fromMostSpecific);
266 
267 	virtual	void				Warn(status_t error, const char* format, ...);
268 
269 	virtual	void				ProgressPackageDownloadStarted(
270 									const char* packageName);
271 	virtual	void				ProgressPackageDownloadActive(
272 									const char* packageName,
273 									float completionPercentage);
274 	virtual	void				ProgressPackageDownloadComplete(
275 									const char* packageName);
276 	virtual	void				ProgressPackageChecksumStarted(
277 									const char* title);
278 	virtual	void				ProgressPackageChecksumComplete(
279 									const char* title);
280 
281 	virtual	void				ProgressStartApplyingChanges(
282 									InstalledRepository& repository);
283 	virtual	void				ProgressTransactionCommitted(
284 									InstalledRepository& repository,
285 									const char* transactionDirectoryName);
286 	virtual	void				ProgressApplyingChangesDone(
287 									InstalledRepository& repository);
288 };
289 
290 
291 }	// namespace BPrivate
292 
293 }	// namespace BManager
294 
295 }	// namespace BPackageKit
296 
297 
298 #endif	// _PACKAGE__MANAGER__PRIVATE__PACKAGE_MANAGER_H_
299