xref: /haiku/src/apps/haikudepot/model/Model.h (revision efafab643ce980e3f3c916795ed302599f6b4f66)
1 /*
2  * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2016-2018, 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 									const BString& baseURL,
172 									DepotMapper* depotMapper,
173 									void* context);
174 
175 			void				ForAllDepots(
176 									void (*func)(const DepotInfo& depot,
177 										void* context),
178 									void* context);
179 
180 			void				ForAllPackages(PackageConsumer* packageConsumer,
181 									void* context);
182 
183 			void				ForPackageByNameInDepot(
184 									const BString& depotName,
185 									const BString& packageName,
186 									PackageConsumer* packageConsumer,
187 									void* context);
188 
189 			status_t			IconStoragePath(BPath& path) const;
190 			status_t			DumpExportRepositoryDataPath(BPath& path) const;
191 			status_t			DumpExportPkgDataPath(BPath& path,
192 									const BString& repositorySourceCode) const;
193 
194 			void				LogDepotsWithNoWebAppRepositoryCode() const;
195 
196 private:
197 			void				_MaybeLogJsonRpcError(
198 									const BMessage &responsePayload,
199 									const char *sourceDescription) const;
200 
201 			void				_UpdateIsFeaturedFilter();
202 
203 	static	int32				_PopulateAllPackagesEntry(void* cookie);
204 
205 			void				_PopulatePackageChangelog(
206 									const PackageInfoRef& package);
207 
208 			void				_PopulatePackageScreenshot(
209 									const PackageInfoRef& package,
210 									const ScreenshotInfo& info,
211 									int32 scaledWidth, bool fromCacheOnly);
212 
213 			bool				_GetCacheFile(BPath& path, BFile& file,
214 									directory_which directory,
215 									const char* relativeLocation,
216 									const char* fileName,
217 									uint32 openMode) const;
218 			bool				_GetCacheFile(BPath& path, BFile& file,
219 									directory_which directory,
220 									const char* relativeLocation,
221 									const char* fileName,
222 									bool ignoreAge, time_t maxAge) const;
223 
224 			void				_NotifyAuthorizationChanged();
225 
226 private:
227 			BLocker				fLock;
228 
229 			DepotList			fDepots;
230 
231 			CategoryRef			fCategoryAudio;
232 			CategoryRef			fCategoryBusiness;
233 			CategoryRef			fCategoryDevelopment;
234 			CategoryRef			fCategoryEducation;
235 			CategoryRef			fCategoryGames;
236 			CategoryRef			fCategoryGraphics;
237 			CategoryRef			fCategoryInternetAndNetwork;
238 			CategoryRef			fCategoryProductivity;
239 			CategoryRef			fCategoryScienceAndMathematics;
240 			CategoryRef			fCategorySystemAndUtilities;
241 			CategoryRef			fCategoryVideo;
242 			// TODO: Dynamic categories retrieved from web-app
243 
244 			CategoryList		fCategories;
245 
246 			PackageList			fInstalledPackages;
247 			PackageList			fActivatedPackages;
248 			PackageList			fUninstalledPackages;
249 			PackageList			fDownloadingPackages;
250 			PackageList			fUpdateablePackages;
251 			PackageList			fPopulatedPackages;
252 
253 			PackageFilterRef	fCategoryFilter;
254 			BString				fDepotFilter;
255 			PackageFilterRef	fSearchTermsFilter;
256 			PackageFilterRef	fIsFeaturedFilter;
257 
258 			bool				fShowFeaturedPackages;
259 			bool				fShowAvailablePackages;
260 			bool				fShowInstalledPackages;
261 			bool				fShowSourcePackages;
262 			bool				fShowDevelopPackages;
263 
264 			StringList			fSupportedLanguages;
265 			BString				fPreferredLanguage;
266 
267 			WebAppInterface		fWebAppInterface;
268 
269 			ModelListenerList	fListeners;
270 };
271 
272 
273 #endif // PACKAGE_INFO_H
274