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