1 /* 2 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>. 3 * Copyright 2016-2021, 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 <vector> 10 11 #include <Locker.h> 12 13 #include "AbstractProcess.h" 14 #include "PackageIconTarRepository.h" 15 #include "LanguageModel.h" 16 #include "PackageInfo.h" 17 #include "RatingStability.h" 18 #include "WebAppInterface.h" 19 20 21 class BFile; 22 class BMessage; 23 class BPath; 24 25 26 typedef enum package_list_view_mode { 27 PROMINENT, 28 ALL 29 } package_list_view_mode; 30 31 32 class PackageFilter : public BReferenceable { 33 public: 34 virtual ~PackageFilter(); 35 36 virtual bool AcceptsPackage( 37 const PackageInfoRef& package) const = 0; 38 }; 39 40 typedef BReference<PackageFilter> PackageFilterRef; 41 42 43 class ModelListener : public BReferenceable { 44 public: 45 virtual ~ModelListener(); 46 47 virtual void AuthorizationChanged() = 0; 48 virtual void CategoryListChanged() = 0; 49 }; 50 51 52 class PackageConsumer { 53 public: 54 virtual bool ConsumePackage( 55 const PackageInfoRef& packageInfoRef, 56 void* context) = 0; 57 }; 58 59 60 typedef BReference<ModelListener> ModelListenerRef; 61 62 63 class Model { 64 public: 65 Model(); 66 virtual ~Model(); 67 68 LanguageModel* Language(); 69 PackageIconRepository& 70 GetPackageIconRepository(); 71 status_t InitPackageIconRepository(); 72 73 BLocker* Lock() 74 { return &fLock; } 75 76 void AddListener(const ModelListenerRef& listener); 77 78 PackageInfoRef PackageForName(const BString& name); 79 bool MatchesFilter( 80 const PackageInfoRef& package) const; 81 82 void MergeOrAddDepot(const DepotInfoRef& depot); 83 bool HasDepot(const BString& name) const; 84 int32 CountDepots() const; 85 DepotInfoRef DepotAtIndex(int32 index) const; 86 const DepotInfoRef DepotForName(const BString& name) const; 87 bool HasAnyProminentPackages(); 88 89 void Clear(); 90 91 int32 CountCategories() const; 92 CategoryRef CategoryByCode(BString& code) const; 93 CategoryRef CategoryAtIndex(int32 index) const; 94 void AddCategories( 95 std::vector<CategoryRef>& values); 96 97 int32 CountRatingStabilities() const; 98 RatingStabilityRef RatingStabilityByCode(BString& code) const; 99 RatingStabilityRef RatingStabilityAtIndex(int32 index) const; 100 void AddRatingStabilities( 101 std::vector<RatingStabilityRef>& values); 102 103 void SetStateForPackagesByName( 104 BStringList& packageNames, 105 PackageState state); 106 107 // Configure PackageFilters 108 void SetCategory(const BString& category); 109 BString Category() const; 110 void SetDepot(const BString& depot); 111 BString Depot() const; 112 void SetSearchTerms(const BString& searchTerms); 113 BString SearchTerms() const; 114 115 void SetPackageListViewMode( 116 package_list_view_mode mode); 117 package_list_view_mode 118 PackageListViewMode() const 119 { return fPackageListViewMode; } 120 void SetShowAvailablePackages(bool show); 121 bool ShowAvailablePackages() const 122 { return fShowAvailablePackages; } 123 void SetShowInstalledPackages(bool show); 124 bool ShowInstalledPackages() const 125 { return fShowInstalledPackages; } 126 void SetShowSourcePackages(bool show); 127 bool ShowSourcePackages() const 128 { return fShowSourcePackages; } 129 void SetShowDevelopPackages(bool show); 130 bool ShowDevelopPackages() const 131 { return fShowDevelopPackages; } 132 void SetCanShareAnonymousUsageData(bool value); 133 bool CanShareAnonymousUsageData() const 134 { return fCanShareAnonymousUsageData; } 135 136 // Retrieve package information 137 static const uint32 POPULATE_CACHED_RATING = 1 << 0; 138 static const uint32 POPULATE_CACHED_ICON = 1 << 1; 139 static const uint32 POPULATE_USER_RATINGS = 1 << 2; 140 static const uint32 POPULATE_SCREEN_SHOTS = 1 << 3; 141 static const uint32 POPULATE_CHANGELOG = 1 << 4; 142 static const uint32 POPULATE_CATEGORIES = 1 << 5; 143 static const uint32 POPULATE_FORCE = 1 << 6; 144 145 bool CanPopulatePackage( 146 const PackageInfoRef& package); 147 void PopulatePackage(const PackageInfoRef& package, 148 uint32 flags); 149 150 void SetNickname(BString nickname); 151 const BString& Nickname() const; 152 void SetAuthorization(const BString& nickname, 153 const BString& passwordClear, 154 bool storePassword); 155 156 WebAppInterface& 157 GetWebAppInterface() 158 { return fWebAppInterface; } 159 160 status_t IconTarPath(BPath& path) const; 161 status_t DumpExportReferenceDataPath(BPath& path); 162 status_t DumpExportRepositoryDataPath(BPath& path); 163 status_t DumpExportPkgDataPath(BPath& path, 164 const BString& repositorySourceCode); 165 166 private: 167 void _AddCategory(const CategoryRef& category); 168 169 void _AddRatingStability( 170 const RatingStabilityRef& value); 171 172 void _MaybeLogJsonRpcError( 173 const BMessage &responsePayload, 174 const char *sourceDescription) const; 175 176 static int32 _PopulateAllPackagesEntry(void* cookie); 177 178 void _PopulatePackageChangelog( 179 const PackageInfoRef& package); 180 181 void _PopulatePackageScreenshot( 182 const PackageInfoRef& package, 183 const ScreenshotInfoRef& info, 184 int32 scaledWidth, bool fromCacheOnly); 185 186 void _NotifyAuthorizationChanged(); 187 void _NotifyCategoryListChanged(); 188 189 private: 190 BLocker fLock; 191 192 std::vector<DepotInfoRef> 193 fDepots; 194 std::vector<CategoryRef> 195 fCategories; 196 std::vector<RatingStabilityRef> 197 fRatingStabilities; 198 199 BStringList fPopulatedPackageNames; 200 201 PackageFilterRef fCategoryFilter; 202 BString fDepotFilter; 203 PackageFilterRef fSearchTermsFilter; 204 205 package_list_view_mode 206 fPackageListViewMode; 207 bool fShowAvailablePackages; 208 bool fShowInstalledPackages; 209 bool fShowSourcePackages; 210 bool fShowDevelopPackages; 211 bool fCanShareAnonymousUsageData; 212 213 LanguageModel fLanguageModel; 214 PackageIconTarRepository 215 fPackageIconRepository; 216 WebAppInterface fWebAppInterface; 217 218 std::vector<ModelListenerRef> 219 fListeners; 220 }; 221 222 223 #endif // PACKAGE_INFO_H 224