1 /* 2 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>. 3 * Copyright 2016-2020, Andrew Lindesay <apl@lindesay.co.nz>. 4 * All rights reserved. Distributed under the terms of the MIT License. 5 */ 6 #ifndef MODEL_H 7 #define MODEL_H 8 9 #include <Locker.h> 10 11 #include "AbstractProcess.h" 12 #include "LanguageModel.h" 13 #include "LocalIconStore.h" 14 #include "PackageInfo.h" 15 #include "WebAppInterface.h" 16 17 18 class BFile; 19 class BMessage; 20 class BPath; 21 22 23 class PackageFilter : public BReferenceable { 24 public: 25 virtual ~PackageFilter(); 26 27 virtual bool AcceptsPackage( 28 const PackageInfoRef& package) const = 0; 29 }; 30 31 typedef BReference<PackageFilter> PackageFilterRef; 32 33 34 class ModelListener : public BReferenceable { 35 public: 36 virtual ~ModelListener(); 37 38 virtual void AuthorizationChanged() = 0; 39 virtual void CategoryListChanged() = 0; 40 }; 41 42 43 class DepotMapper { 44 public: 45 virtual DepotInfo MapDepot(const DepotInfo& depot, 46 void* context) = 0; 47 }; 48 49 50 class PackageConsumer { 51 public: 52 virtual bool ConsumePackage( 53 const PackageInfoRef& packageInfoRef, 54 void* context) = 0; 55 }; 56 57 58 typedef BReference<ModelListener> ModelListenerRef; 59 typedef List<ModelListenerRef, false> ModelListenerList; 60 61 62 class Model { 63 public: 64 Model(); 65 virtual ~Model(); 66 67 LanguageModel& Language(); 68 69 BLocker* Lock() 70 { return &fLock; } 71 72 bool AddListener(const ModelListenerRef& listener); 73 74 // !Returns new PackageInfoList from current parameters 75 PackageList CreatePackageList() const; 76 77 bool MatchesFilter( 78 const PackageInfoRef& package) const; 79 80 bool AddDepot(const DepotInfo& depot); 81 bool HasDepot(const BString& name) const; 82 const DepotList& Depots() const 83 { return fDepots; } 84 const DepotInfo* DepotForName(const BString& name) const; 85 bool SyncDepot(const DepotInfo& depot); 86 87 void Clear(); 88 89 void AddCategories(const CategoryList& categories); 90 const CategoryList& Categories() const 91 { return fCategories; } 92 93 void SetPackageState( 94 const PackageInfoRef& package, 95 PackageState state); 96 97 // Configure PackageFilters 98 void SetCategory(const BString& category); 99 BString Category() const; 100 void SetDepot(const BString& depot); 101 BString Depot() const; 102 void SetSearchTerms(const BString& searchTerms); 103 BString SearchTerms() const; 104 105 void SetShowFeaturedPackages(bool show); 106 bool ShowFeaturedPackages() const 107 { return fShowFeaturedPackages; } 108 void SetShowAvailablePackages(bool show); 109 bool ShowAvailablePackages() const 110 { return fShowAvailablePackages; } 111 void SetShowInstalledPackages(bool show); 112 bool ShowInstalledPackages() const 113 { return fShowInstalledPackages; } 114 void SetShowSourcePackages(bool show); 115 bool ShowSourcePackages() const 116 { return fShowSourcePackages; } 117 void SetShowDevelopPackages(bool show); 118 bool ShowDevelopPackages() const 119 { return fShowDevelopPackages; } 120 121 // Retrieve package information 122 static const uint32 POPULATE_CACHED_RATING = 1 << 0; 123 static const uint32 POPULATE_CACHED_ICON = 1 << 1; 124 static const uint32 POPULATE_USER_RATINGS = 1 << 2; 125 static const uint32 POPULATE_SCREEN_SHOTS = 1 << 3; 126 static const uint32 POPULATE_CHANGELOG = 1 << 4; 127 static const uint32 POPULATE_CATEGORIES = 1 << 5; 128 static const uint32 POPULATE_FORCE = 1 << 6; 129 130 void PopulatePackage(const PackageInfoRef& package, 131 uint32 flags); 132 133 void SetNickname(BString nickname); 134 const BString& Nickname() const; 135 void SetAuthorization(const BString& nickname, 136 const BString& passwordClear, 137 bool storePassword); 138 139 const WebAppInterface& GetWebAppInterface() const 140 { return fWebAppInterface; } 141 142 void ReplaceDepotByUrl( 143 const BString& URL, 144 DepotMapper* depotMapper, 145 void* context); 146 147 status_t IconStoragePath(BPath& path) const; 148 status_t DumpExportReferenceDataPath(BPath& path) const; 149 status_t DumpExportRepositoryDataPath(BPath& path) const; 150 status_t DumpExportPkgDataPath(BPath& path, 151 const BString& repositorySourceCode) const; 152 153 void LogDepotsWithNoWebAppRepositoryCode() const; 154 155 private: 156 void _AddCategory(const CategoryRef& category); 157 158 void _MaybeLogJsonRpcError( 159 const BMessage &responsePayload, 160 const char *sourceDescription) const; 161 162 void _UpdateIsFeaturedFilter(); 163 164 static int32 _PopulateAllPackagesEntry(void* cookie); 165 166 void _PopulatePackageChangelog( 167 const PackageInfoRef& package); 168 169 void _PopulatePackageScreenshot( 170 const PackageInfoRef& package, 171 const ScreenshotInfo& info, 172 int32 scaledWidth, bool fromCacheOnly); 173 174 void _NotifyAuthorizationChanged(); 175 void _NotifyCategoryListChanged(); 176 177 private: 178 BLocker fLock; 179 180 DepotList fDepots; 181 182 CategoryList fCategories; 183 184 PackageList fInstalledPackages; 185 PackageList fActivatedPackages; 186 PackageList fUninstalledPackages; 187 PackageList fDownloadingPackages; 188 PackageList fUpdateablePackages; 189 PackageList fPopulatedPackages; 190 191 PackageFilterRef fCategoryFilter; 192 BString fDepotFilter; 193 PackageFilterRef fSearchTermsFilter; 194 PackageFilterRef fIsFeaturedFilter; 195 196 bool fShowFeaturedPackages; 197 bool fShowAvailablePackages; 198 bool fShowInstalledPackages; 199 bool fShowSourcePackages; 200 bool fShowDevelopPackages; 201 202 LanguageModel fLanguageModel; 203 WebAppInterface fWebAppInterface; 204 205 ModelListenerList fListeners; 206 }; 207 208 209 #endif // PACKAGE_INFO_H 210