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