xref: /haiku/src/apps/haikudepot/model/Model.h (revision be622abddb00c474c53631429ad1102d78688d4f)
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 			status_t			PopulateWebAppRepositoryCodes();
122 
123 			// Retrieve package information
124 	static	const uint32		POPULATE_CACHED_RATING	= 1 << 0;
125 	static	const uint32		POPULATE_CACHED_ICON	= 1 << 1;
126 	static	const uint32		POPULATE_USER_RATINGS	= 1 << 2;
127 	static	const uint32		POPULATE_SCREEN_SHOTS	= 1 << 3;
128 	static	const uint32		POPULATE_CHANGELOG		= 1 << 4;
129 	static	const uint32		POPULATE_CATEGORIES		= 1 << 5;
130 	static	const uint32		POPULATE_FORCE			= 1 << 6;
131 
132 			void				PopulatePackage(const PackageInfoRef& package,
133 									uint32 flags);
134 			void				PopulateAllPackages();
135 			void				StopPopulatingAllPackages();
136 
137 			const StringList&	SupportedLanguages() const
138 									{ return fSupportedLanguages; }
139 
140 			const BString&		PreferredLanguage() const
141 									{ return fPreferredLanguage; }
142 
143 			void				SetUsername(BString username);
144 			const BString&		Username() const;
145 			void				SetAuthorization(const BString& username,
146 									const BString& password,
147 									bool storePassword);
148 
149 			const WebAppInterface& GetWebAppInterface() const
150 									{ return fWebAppInterface; }
151 
152 
153 private:
154 			status_t			_DumpExportRepositoryDataPath(
155 									BPath& path) const;
156 			status_t			_DumpExportPkgDataPath(BPath& path,
157 									const BString& repositorySourceCode) const;
158 			void				_UpdateIsFeaturedFilter();
159 
160 	static	int32				_PopulateAllPackagesEntry(void* cookie);
161 			void				_PopulateAllPackagesThread();
162 			void				_PopulatePackagesForDepot(
163 									const DepotInfo& depotInfo);
164 
165 			void				_PopulateAllPackagesIcons();
166 
167 			bool				_GetCacheFile(BPath& path, BFile& file,
168 									directory_which directory,
169 									const char* relativeLocation,
170 									const char* fileName,
171 									uint32 openMode) const;
172 			bool				_GetCacheFile(BPath& path, BFile& file,
173 									directory_which directory,
174 									const char* relativeLocation,
175 									const char* fileName,
176 									bool ignoreAge, time_t maxAge) const;
177 
178 			status_t			_PopulatePackageIcon(
179 									const PackageInfoRef& package);
180 			void				_PopulatePackageScreenshot(
181 									const PackageInfoRef& package,
182 									const ScreenshotInfo& info,
183 									int32 scaledWidth,
184 									bool fromCacheOnly);
185 
186 			void				_NotifyAuthorizationChanged();
187 
188 private:
189 			BLocker				fLock;
190 
191 			DepotList			fDepots;
192 
193 			LocalIconStore		fLocalIconStore;
194 
195 			CategoryRef			fCategoryAudio;
196 			CategoryRef			fCategoryBusiness;
197 			CategoryRef			fCategoryDevelopment;
198 			CategoryRef			fCategoryEducation;
199 			CategoryRef			fCategoryGames;
200 			CategoryRef			fCategoryGraphics;
201 			CategoryRef			fCategoryInternetAndNetwork;
202 			CategoryRef			fCategoryProductivity;
203 			CategoryRef			fCategoryScienceAndMathematics;
204 			CategoryRef			fCategorySystemAndUtilities;
205 			CategoryRef			fCategoryVideo;
206 			// TODO: Dynamic categories retrieved from web-app
207 
208 			CategoryList		fCategories;
209 
210 			PackageList			fInstalledPackages;
211 			PackageList			fActivatedPackages;
212 			PackageList			fUninstalledPackages;
213 			PackageList			fDownloadingPackages;
214 			PackageList			fUpdateablePackages;
215 			PackageList			fPopulatedPackages;
216 
217 			PackageFilterRef	fCategoryFilter;
218 			BString				fDepotFilter;
219 			PackageFilterRef	fSearchTermsFilter;
220 			PackageFilterRef	fIsFeaturedFilter;
221 
222 			bool				fShowFeaturedPackages;
223 			bool				fShowAvailablePackages;
224 			bool				fShowInstalledPackages;
225 			bool				fShowSourcePackages;
226 			bool				fShowDevelopPackages;
227 
228 			thread_id			fPopulateAllPackagesThread;
229 	volatile bool				fStopPopulatingAllPackages;
230 
231 			StringList			fSupportedLanguages;
232 			BString				fPreferredLanguage;
233 
234 			WebAppInterface		fWebAppInterface;
235 
236 			ModelListenerList	fListeners;
237 };
238 
239 
240 #endif // PACKAGE_INFO_H
241