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