1 /* 2 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>. 3 * Copyright 2016-2017, 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 <FindDirectory.h> 10 #include <Locker.h> 11 12 #include "AbstractServerProcess.h" 13 #include "LocalIconStore.h" 14 #include "BulkLoadContext.h" 15 #include "PackageInfo.h" 16 #include "WebAppInterface.h" 17 18 19 class BFile; 20 class BMessage; 21 class BPath; 22 23 24 class PackageFilter : public BReferenceable { 25 public: 26 virtual ~PackageFilter(); 27 28 virtual bool AcceptsPackage( 29 const PackageInfoRef& package) const = 0; 30 }; 31 32 typedef BReference<PackageFilter> PackageFilterRef; 33 34 35 class ModelListener : public BReferenceable { 36 public: 37 virtual ~ModelListener(); 38 39 virtual void AuthorizationChanged() = 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 BLocker* Lock() 68 { return &fLock; } 69 70 bool AddListener(const ModelListenerRef& listener); 71 72 // !Returns new PackageInfoList from current parameters 73 PackageList CreatePackageList() const; 74 75 bool MatchesFilter( 76 const PackageInfoRef& package) const; 77 78 bool AddDepot(const DepotInfo& depot); 79 bool HasDepot(const BString& name) const; 80 const DepotList& Depots() const 81 { return fDepots; } 82 const DepotInfo* DepotForName(const BString& name) const; 83 bool SyncDepot(const DepotInfo& depot); 84 85 void Clear(); 86 87 // Access to global categories 88 const CategoryRef& CategoryAudio() const 89 { return fCategoryAudio; } 90 const CategoryRef& CategoryBusiness() const 91 { return fCategoryBusiness; } 92 const CategoryRef& CategoryDevelopment() const 93 { return fCategoryDevelopment; } 94 const CategoryRef& CategoryEducation() const 95 { return fCategoryEducation; } 96 const CategoryRef& CategoryInternetAndNetwork() const 97 { return fCategoryInternetAndNetwork; } 98 const CategoryRef& CategoryGames() const 99 { return fCategoryGames; } 100 const CategoryRef& CategoryGraphics() const 101 { return fCategoryGraphics; } 102 const CategoryRef& CategoryProductivity() const 103 { return fCategoryProductivity; } 104 const CategoryRef& CategoryScienceAndMathematics() const 105 { return fCategoryScienceAndMathematics; } 106 const CategoryRef& CategorySystemAndUtilities() const 107 { return fCategorySystemAndUtilities; } 108 const CategoryRef& CategoryVideo() const 109 { return fCategoryVideo; } 110 111 const CategoryList& Categories() const 112 { return fCategories; } 113 114 void SetPackageState( 115 const PackageInfoRef& package, 116 PackageState state); 117 118 // Configure PackageFilters 119 void SetCategory(const BString& category); 120 BString Category() const; 121 void SetDepot(const BString& depot); 122 BString Depot() const; 123 void SetSearchTerms(const BString& searchTerms); 124 BString SearchTerms() const; 125 126 void SetShowFeaturedPackages(bool show); 127 bool ShowFeaturedPackages() const 128 { return fShowFeaturedPackages; } 129 void SetShowAvailablePackages(bool show); 130 bool ShowAvailablePackages() const 131 { return fShowAvailablePackages; } 132 void SetShowInstalledPackages(bool show); 133 bool ShowInstalledPackages() const 134 { return fShowInstalledPackages; } 135 void SetShowSourcePackages(bool show); 136 bool ShowSourcePackages() const 137 { return fShowSourcePackages; } 138 void SetShowDevelopPackages(bool show); 139 bool ShowDevelopPackages() const 140 { return fShowDevelopPackages; } 141 142 // Retrieve package information 143 static const uint32 POPULATE_CACHED_RATING = 1 << 0; 144 static const uint32 POPULATE_CACHED_ICON = 1 << 1; 145 static const uint32 POPULATE_USER_RATINGS = 1 << 2; 146 static const uint32 POPULATE_SCREEN_SHOTS = 1 << 3; 147 static const uint32 POPULATE_CHANGELOG = 1 << 4; 148 static const uint32 POPULATE_CATEGORIES = 1 << 5; 149 static const uint32 POPULATE_FORCE = 1 << 6; 150 151 void PopulatePackage(const PackageInfoRef& package, 152 uint32 flags); 153 154 const StringList& SupportedLanguages() const 155 { return fSupportedLanguages; } 156 157 const BString& PreferredLanguage() const 158 { return fPreferredLanguage; } 159 160 void SetUsername(BString username); 161 const BString& Username() const; 162 void SetAuthorization(const BString& username, 163 const BString& password, 164 bool storePassword); 165 166 const WebAppInterface& GetWebAppInterface() const 167 { return fWebAppInterface; } 168 169 void ReplaceDepotByUrl( 170 const BString& url, 171 DepotMapper* depotMapper, 172 void* context); 173 174 void ForAllDepots( 175 void (*func)(const DepotInfo& depot, 176 void* context), 177 void* context); 178 179 void ForAllPackages(PackageConsumer* packageConsumer, 180 void* context); 181 182 void ForPackageByNameInDepot( 183 const BString& depotName, 184 const BString& packageName, 185 PackageConsumer* packageConsumer, 186 void* context); 187 188 status_t IconStoragePath(BPath& path) const; 189 status_t DumpExportRepositoryDataPath(BPath& path) const; 190 status_t DumpExportPkgDataPath(BPath& path, 191 const BString& repositorySourceCode) const; 192 193 void LogDepotsWithNoWebAppRepositoryCode() const; 194 195 private: 196 void _UpdateIsFeaturedFilter(); 197 198 static int32 _PopulateAllPackagesEntry(void* cookie); 199 200 void _PopulatePackageScreenshot( 201 const PackageInfoRef& package, 202 const ScreenshotInfo& info, 203 int32 scaledWidth, bool fromCacheOnly); 204 205 bool _GetCacheFile(BPath& path, BFile& file, 206 directory_which directory, 207 const char* relativeLocation, 208 const char* fileName, 209 uint32 openMode) const; 210 bool _GetCacheFile(BPath& path, BFile& file, 211 directory_which directory, 212 const char* relativeLocation, 213 const char* fileName, 214 bool ignoreAge, time_t maxAge) const; 215 216 void _NotifyAuthorizationChanged(); 217 218 private: 219 BLocker fLock; 220 221 DepotList fDepots; 222 223 CategoryRef fCategoryAudio; 224 CategoryRef fCategoryBusiness; 225 CategoryRef fCategoryDevelopment; 226 CategoryRef fCategoryEducation; 227 CategoryRef fCategoryGames; 228 CategoryRef fCategoryGraphics; 229 CategoryRef fCategoryInternetAndNetwork; 230 CategoryRef fCategoryProductivity; 231 CategoryRef fCategoryScienceAndMathematics; 232 CategoryRef fCategorySystemAndUtilities; 233 CategoryRef fCategoryVideo; 234 // TODO: Dynamic categories retrieved from web-app 235 236 CategoryList fCategories; 237 238 PackageList fInstalledPackages; 239 PackageList fActivatedPackages; 240 PackageList fUninstalledPackages; 241 PackageList fDownloadingPackages; 242 PackageList fUpdateablePackages; 243 PackageList fPopulatedPackages; 244 245 PackageFilterRef fCategoryFilter; 246 BString fDepotFilter; 247 PackageFilterRef fSearchTermsFilter; 248 PackageFilterRef fIsFeaturedFilter; 249 250 bool fShowFeaturedPackages; 251 bool fShowAvailablePackages; 252 bool fShowInstalledPackages; 253 bool fShowSourcePackages; 254 bool fShowDevelopPackages; 255 256 StringList fSupportedLanguages; 257 BString fPreferredLanguage; 258 259 WebAppInterface fWebAppInterface; 260 261 ModelListenerList fListeners; 262 }; 263 264 265 #endif // PACKAGE_INFO_H 266