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