xref: /haiku/src/apps/haikudepot/model/Model.h (revision 6c2abee2f5e73c3fc81c33da51ac610f8bf1117a)
1 /*
2  * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2016, 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 "PackageInfo.h"
13 #include "WebAppInterface.h"
14 
15 
16 class BFile;
17 class BMessage;
18 class BPath;
19 
20 
21 class PackageFilter : public BReferenceable {
22 public:
23 	virtual						~PackageFilter();
24 
25 	virtual	bool				AcceptsPackage(
26 									const PackageInfoRef& package) const = 0;
27 };
28 
29 typedef BReference<PackageFilter> PackageFilterRef;
30 
31 
32 class ModelListener : public BReferenceable {
33 public:
34 	virtual						~ModelListener();
35 
36 	virtual	void				AuthorizationChanged() = 0;
37 };
38 
39 typedef BReference<ModelListener> ModelListenerRef;
40 typedef List<ModelListenerRef, false> ModelListenerList;
41 
42 
43 class Model {
44 public:
45 								Model();
46 	virtual						~Model();
47 
48 			BLocker*			Lock()
49 									{ return &fLock; }
50 
51 			bool				AddListener(const ModelListenerRef& listener);
52 
53 			// !Returns new PackageInfoList from current parameters
54 			PackageList			CreatePackageList() const;
55 
56 			bool				AddDepot(const DepotInfo& depot);
57 			bool				HasDepot(const BString& name) const;
58 			const DepotList&	Depots() const
59 									{ return fDepots; }
60 			const DepotInfo*	DepotForName(const BString& name) const;
61 			bool				SyncDepot(const DepotInfo& depot);
62 
63 			void				Clear();
64 
65 			// Access to global categories
66 			const CategoryRef&	CategoryAudio() const
67 									{ return fCategoryAudio; }
68 			const CategoryRef&	CategoryBusiness() const
69 									{ return fCategoryBusiness; }
70 			const CategoryRef&	CategoryDevelopment() const
71 									{ return fCategoryDevelopment; }
72 			const CategoryRef&	CategoryEducation() const
73 									{ return fCategoryEducation; }
74 			const CategoryRef&	CategoryInternetAndNetwork() const
75 									{ return fCategoryInternetAndNetwork; }
76 			const CategoryRef&	CategoryGames() const
77 									{ return fCategoryGames; }
78 			const CategoryRef&	CategoryGraphics() const
79 									{ return fCategoryGraphics; }
80 			const CategoryRef&	CategoryProductivity() const
81 									{ return fCategoryProductivity; }
82 			const CategoryRef&	CategoryScienceAndMathematics() const
83 									{ return fCategoryScienceAndMathematics; }
84 			const CategoryRef&	CategorySystemAndUtilities() const
85 									{ return fCategorySystemAndUtilities; }
86 			const CategoryRef&	CategoryVideo() const
87 									{ return fCategoryVideo; }
88 
89 			const CategoryList&	Categories() const
90 									{ return fCategories; }
91 
92 			void				SetPackageState(
93 									const PackageInfoRef& package,
94 									PackageState state);
95 
96 			// Configure PackageFilters
97 			void				SetCategory(const BString& category);
98 			BString				Category() const;
99 			void				SetDepot(const BString& depot);
100 			BString				Depot() const;
101 			void				SetSearchTerms(const BString& searchTerms);
102 			BString				SearchTerms() const;
103 
104 			void				SetShowFeaturedPackages(bool show);
105 			bool				ShowFeaturedPackages() const
106 									{ return fShowFeaturedPackages; }
107 			void				SetShowAvailablePackages(bool show);
108 			bool				ShowAvailablePackages() const
109 									{ return fShowAvailablePackages; }
110 			void				SetShowInstalledPackages(bool show);
111 			bool				ShowInstalledPackages() const
112 									{ return fShowInstalledPackages; }
113 			void				SetShowSourcePackages(bool show);
114 			bool				ShowSourcePackages() const
115 									{ return fShowSourcePackages; }
116 			void				SetShowDevelopPackages(bool show);
117 			bool				ShowDevelopPackages() const
118 									{ return fShowDevelopPackages; }
119 
120 			void				PopulateWebAppRepositoryCode(
121 									DepotInfo& depotInfo);
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 			void				_UpdateIsFeaturedFilter();
155 
156 	static	int32				_PopulateAllPackagesEntry(void* cookie);
157 			void				_PopulateAllPackagesThread(bool fromCacheOnly);
158 
159 			bool				_GetCacheFile(BPath& path, BFile& file,
160 									directory_which directory,
161 									const char* relativeLocation,
162 									const char* fileName,
163 									uint32 openMode) const;
164 			bool				_GetCacheFile(BPath& path, BFile& file,
165 									directory_which directory,
166 									const char* relativeLocation,
167 									const char* fileName,
168 									bool ignoreAge, time_t maxAge) const;
169 
170 			void				_PopulatePackageInfos(
171 									PackageList& packages,
172 									bool fromCacheOnly,
173 									PackageList& packagesWithIcons);
174 			void				_PopulatePackageInfo(
175 									const PackageInfoRef& package,
176 									bool fromCacheOnly);
177 			void				_PopulatePackageInfo(
178 									const PackageInfoRef& package,
179 									const BMessage& data);
180 			void				_PopulatePackageIcon(
181 									const PackageInfoRef& package,
182 									bool fromCacheOnly);
183 			bool				_HasNativeIcon(const BMessage& message) const;
184 			void				_PopulatePackageScreenshot(
185 									const PackageInfoRef& package,
186 									const ScreenshotInfo& info,
187 									int32 scaledWidth,
188 									bool fromCacheOnly);
189 
190 			void				_NotifyAuthorizationChanged();
191 
192 private:
193 			BLocker				fLock;
194 
195 			DepotList			fDepots;
196 
197 			CategoryRef			fCategoryAudio;
198 			CategoryRef			fCategoryBusiness;
199 			CategoryRef			fCategoryDevelopment;
200 			CategoryRef			fCategoryEducation;
201 			CategoryRef			fCategoryGames;
202 			CategoryRef			fCategoryGraphics;
203 			CategoryRef			fCategoryInternetAndNetwork;
204 			CategoryRef			fCategoryProductivity;
205 			CategoryRef			fCategoryScienceAndMathematics;
206 			CategoryRef			fCategorySystemAndUtilities;
207 			CategoryRef			fCategoryVideo;
208 			// TODO: Dynamic categories retrieved from web-app
209 
210 			CategoryList		fCategories;
211 
212 			PackageList			fInstalledPackages;
213 			PackageList			fActivatedPackages;
214 			PackageList			fUninstalledPackages;
215 			PackageList			fDownloadingPackages;
216 			PackageList			fUpdateablePackages;
217 			PackageList			fPopulatedPackages;
218 
219 			PackageFilterRef	fCategoryFilter;
220 			BString				fDepotFilter;
221 			PackageFilterRef	fSearchTermsFilter;
222 			PackageFilterRef	fIsFeaturedFilter;
223 
224 			bool				fShowFeaturedPackages;
225 			bool				fShowAvailablePackages;
226 			bool				fShowInstalledPackages;
227 			bool				fShowSourcePackages;
228 			bool				fShowDevelopPackages;
229 
230 			thread_id			fPopulateAllPackagesThread;
231 	volatile bool				fStopPopulatingAllPackages;
232 
233 			StringList			fSupportedLanguages;
234 			BString				fPreferredLanguage;
235 
236 			WebAppInterface		fWebAppInterface;
237 
238 			ModelListenerList	fListeners;
239 };
240 
241 
242 #endif // PACKAGE_INFO_H
243