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