1 /* 2 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>. 3 * Copyright 2016-2023, 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(); 152 void SetCredentials(const BString& nickname, 153 const BString& passwordClear, 154 bool storePassword); 155 156 WebAppInterface* GetWebAppInterface() 157 { return &fWebAppInterface; } 158 159 status_t IconTarPath(BPath& path) const; 160 status_t DumpExportReferenceDataPath(BPath& path); 161 status_t DumpExportRepositoryDataPath(BPath& path); 162 status_t DumpExportPkgDataPath(BPath& path, 163 const BString& repositorySourceCode); 164 165 private: 166 void _AddCategory(const CategoryRef& category); 167 168 void _AddRatingStability( 169 const RatingStabilityRef& value); 170 171 void _MaybeLogJsonRpcError( 172 const BMessage &responsePayload, 173 const char *sourceDescription) const; 174 175 static int32 _PopulateAllPackagesEntry(void* cookie); 176 177 void _PopulatePackageChangelog( 178 const PackageInfoRef& package); 179 180 void _PopulatePackageScreenshot( 181 const PackageInfoRef& package, 182 const ScreenshotInfoRef& info, 183 int32 scaledWidth, bool fromCacheOnly); 184 185 void _NotifyAuthorizationChanged(); 186 void _NotifyCategoryListChanged(); 187 188 private: 189 BLocker fLock; 190 191 std::vector<DepotInfoRef> 192 fDepots; 193 std::vector<CategoryRef> 194 fCategories; 195 std::vector<RatingStabilityRef> 196 fRatingStabilities; 197 198 BStringList fPopulatedPackageNames; 199 200 PackageFilterRef fCategoryFilter; 201 BString fDepotFilter; 202 PackageFilterRef fSearchTermsFilter; 203 204 package_list_view_mode 205 fPackageListViewMode; 206 bool fShowAvailablePackages; 207 bool fShowInstalledPackages; 208 bool fShowSourcePackages; 209 bool fShowDevelopPackages; 210 bool fCanShareAnonymousUsageData; 211 212 LanguageModel fLanguageModel; 213 PackageIconTarRepository 214 fPackageIconRepository; 215 WebAppInterface fWebAppInterface; 216 217 std::vector<ModelListenerRef> 218 fListeners; 219 }; 220 221 222 #endif // PACKAGE_INFO_H 223