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 "LocalIconStore.h" 13 #include "PackageInfo.h" 14 #include "WebAppInterface.h" 15 16 17 class BFile; 18 class BMessage; 19 class BPath; 20 21 22 class PackageFilter : public BReferenceable { 23 public: 24 virtual ~PackageFilter(); 25 26 virtual bool AcceptsPackage( 27 const PackageInfoRef& package) const = 0; 28 }; 29 30 typedef BReference<PackageFilter> PackageFilterRef; 31 32 33 class ModelListener : public BReferenceable { 34 public: 35 virtual ~ModelListener(); 36 37 virtual void AuthorizationChanged() = 0; 38 }; 39 40 typedef BReference<ModelListener> ModelListenerRef; 41 typedef List<ModelListenerRef, false> ModelListenerList; 42 43 44 class Model { 45 public: 46 Model(); 47 virtual ~Model(); 48 49 BLocker* Lock() 50 { return &fLock; } 51 52 bool AddListener(const ModelListenerRef& listener); 53 54 // !Returns new PackageInfoList from current parameters 55 PackageList CreatePackageList() const; 56 57 bool AddDepot(const DepotInfo& depot); 58 bool HasDepot(const BString& name) const; 59 const DepotList& Depots() const 60 { return fDepots; } 61 const DepotInfo* DepotForName(const BString& name) const; 62 bool SyncDepot(const DepotInfo& depot); 63 64 void Clear(); 65 66 // Access to global categories 67 const CategoryRef& CategoryAudio() const 68 { return fCategoryAudio; } 69 const CategoryRef& CategoryBusiness() const 70 { return fCategoryBusiness; } 71 const CategoryRef& CategoryDevelopment() const 72 { return fCategoryDevelopment; } 73 const CategoryRef& CategoryEducation() const 74 { return fCategoryEducation; } 75 const CategoryRef& CategoryInternetAndNetwork() const 76 { return fCategoryInternetAndNetwork; } 77 const CategoryRef& CategoryGames() const 78 { return fCategoryGames; } 79 const CategoryRef& CategoryGraphics() const 80 { return fCategoryGraphics; } 81 const CategoryRef& CategoryProductivity() const 82 { return fCategoryProductivity; } 83 const CategoryRef& CategoryScienceAndMathematics() const 84 { return fCategoryScienceAndMathematics; } 85 const CategoryRef& CategorySystemAndUtilities() const 86 { return fCategorySystemAndUtilities; } 87 const CategoryRef& CategoryVideo() const 88 { return fCategoryVideo; } 89 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 void PopulateWebAppRepositoryCode( 122 DepotInfo& depotInfo); 123 124 // Retrieve package information 125 static const uint32 POPULATE_CACHED_RATING = 1 << 0; 126 static const uint32 POPULATE_CACHED_ICON = 1 << 1; 127 static const uint32 POPULATE_USER_RATINGS = 1 << 2; 128 static const uint32 POPULATE_SCREEN_SHOTS = 1 << 3; 129 static const uint32 POPULATE_CHANGELOG = 1 << 4; 130 static const uint32 POPULATE_CATEGORIES = 1 << 5; 131 static const uint32 POPULATE_FORCE = 1 << 6; 132 133 void PopulatePackage(const PackageInfoRef& package, 134 uint32 flags); 135 void PopulateAllPackages(); 136 void StopPopulatingAllPackages(); 137 138 const StringList& SupportedLanguages() const 139 { return fSupportedLanguages; } 140 141 const BString& PreferredLanguage() const 142 { return fPreferredLanguage; } 143 144 void SetUsername(BString username); 145 const BString& Username() const; 146 void SetAuthorization(const BString& username, 147 const BString& password, 148 bool storePassword); 149 150 const WebAppInterface& GetWebAppInterface() const 151 { return fWebAppInterface; } 152 153 154 private: 155 void _UpdateIsFeaturedFilter(); 156 157 static int32 _PopulateAllPackagesEntry(void* cookie); 158 void _PopulateAllPackagesThread(bool fromCacheOnly); 159 160 void _PopulateAllPackagesIcons(); 161 162 bool _GetCacheFile(BPath& path, BFile& file, 163 directory_which directory, 164 const char* relativeLocation, 165 const char* fileName, 166 uint32 openMode) const; 167 bool _GetCacheFile(BPath& path, BFile& file, 168 directory_which directory, 169 const char* relativeLocation, 170 const char* fileName, 171 bool ignoreAge, time_t maxAge) const; 172 173 void _PopulatePackageInfos( 174 PackageList& packages, 175 bool fromCacheOnly); 176 void _PopulatePackageInfo( 177 const PackageInfoRef& package, 178 bool fromCacheOnly); 179 void _PopulatePackageInfo( 180 const PackageInfoRef& package, 181 const BMessage& data); 182 status_t _PopulatePackageIcon( 183 const PackageInfoRef& package); 184 void _PopulatePackageScreenshot( 185 const PackageInfoRef& package, 186 const ScreenshotInfo& info, 187 int32 scaledWidth, 188 bool fromCacheOnly); 189 190 void _NotifyAuthorizationChanged(); 191 192 private: 193 BLocker fLock; 194 195 DepotList fDepots; 196 197 LocalIconStore fLocalIconStore; 198 199 CategoryRef fCategoryAudio; 200 CategoryRef fCategoryBusiness; 201 CategoryRef fCategoryDevelopment; 202 CategoryRef fCategoryEducation; 203 CategoryRef fCategoryGames; 204 CategoryRef fCategoryGraphics; 205 CategoryRef fCategoryInternetAndNetwork; 206 CategoryRef fCategoryProductivity; 207 CategoryRef fCategoryScienceAndMathematics; 208 CategoryRef fCategorySystemAndUtilities; 209 CategoryRef fCategoryVideo; 210 // TODO: Dynamic categories retrieved from web-app 211 212 CategoryList fCategories; 213 214 PackageList fInstalledPackages; 215 PackageList fActivatedPackages; 216 PackageList fUninstalledPackages; 217 PackageList fDownloadingPackages; 218 PackageList fUpdateablePackages; 219 PackageList fPopulatedPackages; 220 221 PackageFilterRef fCategoryFilter; 222 BString fDepotFilter; 223 PackageFilterRef fSearchTermsFilter; 224 PackageFilterRef fIsFeaturedFilter; 225 226 bool fShowFeaturedPackages; 227 bool fShowAvailablePackages; 228 bool fShowInstalledPackages; 229 bool fShowSourcePackages; 230 bool fShowDevelopPackages; 231 232 thread_id fPopulateAllPackagesThread; 233 volatile bool fStopPopulatingAllPackages; 234 235 StringList fSupportedLanguages; 236 BString fPreferredLanguage; 237 238 WebAppInterface fWebAppInterface; 239 240 ModelListenerList fListeners; 241 }; 242 243 244 #endif // PACKAGE_INFO_H 245