xref: /haiku/src/apps/haikudepot/model/Model.h (revision 4c07199d8201fcf267e90be0d24b76799d03cea6)
1 /*
2  * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2016-2023, 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 <vector>
10 
11 #include <Locker.h>
12 
13 #include "AbstractProcess.h"
14 #include "DepotInfo.h"
15 #include "PackageIconTarRepository.h"
16 #include "LanguageModel.h"
17 #include "PackageInfo.h"
18 #include "RatingStability.h"
19 #include "WebAppInterface.h"
20 
21 
22 class BFile;
23 class BMessage;
24 class BPath;
25 
26 
27 typedef enum package_list_view_mode {
28 	PROMINENT,
29 	ALL
30 } package_list_view_mode;
31 
32 
33 class PackageFilter : public BReferenceable {
34 public:
35 	virtual						~PackageFilter();
36 
37 	virtual	bool				AcceptsPackage(
38 									const PackageInfoRef& package) const = 0;
39 };
40 
41 typedef BReference<PackageFilter> PackageFilterRef;
42 
43 
44 class ModelListener : public BReferenceable {
45 public:
46 	virtual						~ModelListener();
47 
48 	virtual	void				AuthorizationChanged() = 0;
49 	virtual void				CategoryListChanged() = 0;
50 };
51 
52 
53 class PackageConsumer {
54 public:
55 	virtual	bool				ConsumePackage(
56 									const PackageInfoRef& packageInfoRef,
57 									void* context) = 0;
58 };
59 
60 
61 typedef BReference<ModelListener> ModelListenerRef;
62 
63 
64 class Model {
65 public:
66 								Model();
67 	virtual						~Model();
68 
69 			LanguageModel*		Language();
70 			PackageIconRepository&
71 								GetPackageIconRepository();
72 			status_t			InitPackageIconRepository();
73 
74 			BLocker*			Lock()
75 									{ return &fLock; }
76 
77 			void				AddListener(const ModelListenerRef& listener);
78 
79 			PackageInfoRef		PackageForName(const BString& name);
80 			bool				MatchesFilter(
81 									const PackageInfoRef& package) const;
82 
83 			void				MergeOrAddDepot(const DepotInfoRef& depot);
84 			bool				HasDepot(const BString& name) const;
85 			int32				CountDepots() const;
86 			DepotInfoRef		DepotAtIndex(int32 index) const;
87 			const DepotInfoRef	DepotForName(const BString& name) const;
88 			bool				HasAnyProminentPackages();
89 
90 			void				Clear();
91 
92 			int32				CountCategories() const;
93 			CategoryRef			CategoryByCode(BString& code) const;
94 			CategoryRef			CategoryAtIndex(int32 index) const;
95 			void				AddCategories(
96 									std::vector<CategoryRef>& values);
97 
98 			int32				CountRatingStabilities() const;
99 			RatingStabilityRef	RatingStabilityByCode(BString& code) const;
100 			RatingStabilityRef	RatingStabilityAtIndex(int32 index) const;
101 			void				AddRatingStabilities(
102 									std::vector<RatingStabilityRef>& values);
103 
104 			void				SetStateForPackagesByName(
105 									BStringList& packageNames,
106 									PackageState state);
107 
108 			// Configure PackageFilters
109 			void				SetCategory(const BString& category);
110 			BString				Category() const;
111 			void				SetDepot(const BString& depot);
112 			BString				Depot() const;
113 			void				SetSearchTerms(const BString& searchTerms);
114 			BString				SearchTerms() const;
115 
116 			void				SetPackageListViewMode(
117 									package_list_view_mode mode);
118 			package_list_view_mode
119 								PackageListViewMode() const
120 									{ return fPackageListViewMode; }
121 			void				SetShowAvailablePackages(bool show);
122 			bool				ShowAvailablePackages() const
123 									{ return fShowAvailablePackages; }
124 			void				SetShowInstalledPackages(bool show);
125 			bool				ShowInstalledPackages() const
126 									{ return fShowInstalledPackages; }
127 			void				SetShowSourcePackages(bool show);
128 			bool				ShowSourcePackages() const
129 									{ return fShowSourcePackages; }
130 			void				SetShowDevelopPackages(bool show);
131 			bool				ShowDevelopPackages() const
132 									{ return fShowDevelopPackages; }
133 			void				SetCanShareAnonymousUsageData(bool value);
134 			bool				CanShareAnonymousUsageData() const
135 									{ return fCanShareAnonymousUsageData; }
136 
137 			// Retrieve package information
138 	static	const uint32		POPULATE_CACHED_RATING	= 1 << 0;
139 	static	const uint32		POPULATE_CACHED_ICON	= 1 << 1;
140 	static	const uint32		POPULATE_USER_RATINGS	= 1 << 2;
141 	static	const uint32		POPULATE_SCREEN_SHOTS	= 1 << 3;
142 	static	const uint32		POPULATE_CHANGELOG		= 1 << 4;
143 	static	const uint32		POPULATE_CATEGORIES		= 1 << 5;
144 	static	const uint32		POPULATE_FORCE			= 1 << 6;
145 
146 			bool				CanPopulatePackage(
147 									const PackageInfoRef& package);
148 			void				PopulatePackage(const PackageInfoRef& package,
149 									uint32 flags);
150 
151 			void				SetNickname(BString nickname);
152 			const BString&		Nickname();
153 			void				SetCredentials(const BString& nickname,
154 									const BString& passwordClear,
155 									bool storePassword);
156 
157 			WebAppInterface*    GetWebAppInterface()
158 									{ return &fWebAppInterface; }
159 
160 			status_t			IconTarPath(BPath& path) const;
161 			status_t			DumpExportReferenceDataPath(BPath& path);
162 			status_t			DumpExportRepositoryDataPath(BPath& path);
163 			status_t			DumpExportPkgDataPath(BPath& path,
164 									const BString& repositorySourceCode);
165 
166 private:
167 			void				_AddCategory(const CategoryRef& category);
168 
169 			void				_AddRatingStability(
170 									const RatingStabilityRef& value);
171 
172 			void				_MaybeLogJsonRpcError(
173 									const BMessage &responsePayload,
174 									const char *sourceDescription) const;
175 
176 	static	int32				_PopulateAllPackagesEntry(void* cookie);
177 
178 			void				_PopulatePackageChangelog(
179 									const PackageInfoRef& package);
180 
181 			void				_PopulatePackageScreenshot(
182 									const PackageInfoRef& package,
183 									const ScreenshotInfoRef& info,
184 									int32 scaledWidth, bool fromCacheOnly);
185 
186 			void				_NotifyAuthorizationChanged();
187 			void				_NotifyCategoryListChanged();
188 
189 private:
190 			BLocker				fLock;
191 
192 			std::vector<DepotInfoRef>
193 								fDepots;
194 			std::vector<CategoryRef>
195 								fCategories;
196 			std::vector<RatingStabilityRef>
197 								fRatingStabilities;
198 
199 			BStringList			fPopulatedPackageNames;
200 
201 			PackageFilterRef	fCategoryFilter;
202 			BString				fDepotFilter;
203 			PackageFilterRef	fSearchTermsFilter;
204 
205 			package_list_view_mode
206 								fPackageListViewMode;
207 			bool				fShowAvailablePackages;
208 			bool				fShowInstalledPackages;
209 			bool				fShowSourcePackages;
210 			bool				fShowDevelopPackages;
211 			bool				fCanShareAnonymousUsageData;
212 
213 			LanguageModel		fLanguageModel;
214 			PackageIconTarRepository
215 								fPackageIconRepository;
216 			WebAppInterface		fWebAppInterface;
217 
218 			std::vector<ModelListenerRef>
219 								fListeners;
220 };
221 
222 
223 #endif // PACKAGE_INFO_H
224