1/*
2 * Copyright 2013-2014, Stephan A��mus <superstippi@gmx.de>.
3 * Copyright 2016-2019, 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 "AbstractProcess.h"
13#include "LanguageModel.h"
14#include "LocalIconStore.h"
15#include "PackageInfo.h"
16#include "WebAppInterface.h"
17
18
19class BFile;
20class BMessage;
21class BPath;
22
23
24class PackageFilter : public BReferenceable {
25public:
26	virtual						~PackageFilter();
27
28	virtual	bool				AcceptsPackage(
29									const PackageInfoRef& package) const = 0;
30};
31
32typedef BReference<PackageFilter> PackageFilterRef;
33
34
35class ModelListener : public BReferenceable {
36public:
37	virtual						~ModelListener();
38
39	virtual	void				AuthorizationChanged() = 0;
40	virtual void				CategoryListChanged() = 0;
41};
42
43
44class DepotMapper {
45public:
46	virtual DepotInfo			MapDepot(const DepotInfo& depot,
47									void* context) = 0;
48};
49
50
51class PackageConsumer {
52public:
53	virtual	bool				ConsumePackage(
54									const PackageInfoRef& packageInfoRef,
55									void* context) = 0;
56};
57
58
59typedef BReference<ModelListener> ModelListenerRef;
60typedef List<ModelListenerRef, false> ModelListenerList;
61
62
63class Model {
64public:
65								Model();
66	virtual						~Model();
67
68			LanguageModel&		Language();
69
70			BLocker*			Lock()
71									{ return &fLock; }
72
73			bool				AddListener(const ModelListenerRef& listener);
74
75			// !Returns new PackageInfoList from current parameters
76			PackageList			CreatePackageList() const;
77
78			bool				MatchesFilter(
79									const PackageInfoRef& package) const;
80
81			bool				AddDepot(const DepotInfo& depot);
82			bool				HasDepot(const BString& name) const;
83			const DepotList&	Depots() const
84									{ return fDepots; }
85			const DepotInfo*	DepotForName(const BString& name) const;
86			bool				SyncDepot(const DepotInfo& depot);
87
88			void				Clear();
89
90			void				AddCategories(const CategoryList& categories);
91			const CategoryList&	Categories() const
92									{ return fCategories; }
93
94			void				SetPackageState(
95									const PackageInfoRef& package,
96									PackageState state);
97
98			// Configure PackageFilters
99			void				SetCategory(const BString& category);
100			BString				Category() const;
101			void				SetDepot(const BString& depot);
102			BString				Depot() const;
103			void				SetSearchTerms(const BString& searchTerms);
104			BString				SearchTerms() const;
105
106			void				SetShowFeaturedPackages(bool show);
107			bool				ShowFeaturedPackages() const
108									{ return fShowFeaturedPackages; }
109			void				SetShowAvailablePackages(bool show);
110			bool				ShowAvailablePackages() const
111									{ return fShowAvailablePackages; }
112			void				SetShowInstalledPackages(bool show);
113			bool				ShowInstalledPackages() const
114									{ return fShowInstalledPackages; }
115			void				SetShowSourcePackages(bool show);
116			bool				ShowSourcePackages() const
117									{ return fShowSourcePackages; }
118			void				SetShowDevelopPackages(bool show);
119			bool				ShowDevelopPackages() const
120									{ return fShowDevelopPackages; }
121
122			// Retrieve package information
123	static	const uint32		POPULATE_CACHED_RATING	= 1 << 0;
124	static	const uint32		POPULATE_CACHED_ICON	= 1 << 1;
125	static	const uint32		POPULATE_USER_RATINGS	= 1 << 2;
126	static	const uint32		POPULATE_SCREEN_SHOTS	= 1 << 3;
127	static	const uint32		POPULATE_CHANGELOG		= 1 << 4;
128	static	const uint32		POPULATE_CATEGORIES		= 1 << 5;
129	static	const uint32		POPULATE_FORCE			= 1 << 6;
130
131			void				PopulatePackage(const PackageInfoRef& package,
132									uint32 flags);
133
134			void				SetNickname(BString nickname);
135			const BString&		Nickname() const;
136			void				SetAuthorization(const BString& nickname,
137									const BString& passwordClear,
138									bool storePassword);
139
140			const WebAppInterface& GetWebAppInterface() const
141									{ return fWebAppInterface; }
142
143			void				ReplaceDepotByUrl(
144									const BString& URL,
145									DepotMapper* depotMapper,
146									void* context);
147
148			status_t			IconStoragePath(BPath& path) const;
149			status_t			DumpExportReferenceDataPath(BPath& path) const;
150			status_t			DumpExportRepositoryDataPath(BPath& path) const;
151			status_t			DumpExportPkgDataPath(BPath& path,
152									const BString& repositorySourceCode) const;
153
154			void				LogDepotsWithNoWebAppRepositoryCode() const;
155
156private:
157			void				_AddCategory(const CategoryRef& category);
158			status_t			_LocalDataPath(const BString leaf,
159									BPath& path) const;
160
161			void				_MaybeLogJsonRpcError(
162									const BMessage &responsePayload,
163									const char *sourceDescription) const;
164
165			void				_UpdateIsFeaturedFilter();
166
167	static	int32				_PopulateAllPackagesEntry(void* cookie);
168
169			void				_PopulatePackageChangelog(
170									const PackageInfoRef& package);
171
172			void				_PopulatePackageScreenshot(
173									const PackageInfoRef& package,
174									const ScreenshotInfo& info,
175									int32 scaledWidth, bool fromCacheOnly);
176
177			bool				_GetCacheFile(BPath& path, BFile& file,
178									directory_which directory,
179									const char* relativeLocation,
180									const char* fileName,
181									uint32 openMode) const;
182			bool				_GetCacheFile(BPath& path, BFile& file,
183									directory_which directory,
184									const char* relativeLocation,
185									const char* fileName,
186									bool ignoreAge, time_t maxAge) const;
187
188			void				_NotifyAuthorizationChanged();
189			void				_NotifyCategoryListChanged();
190
191private:
192			BLocker				fLock;
193
194			DepotList			fDepots;
195
196			CategoryList		fCategories;
197
198			PackageList			fInstalledPackages;
199			PackageList			fActivatedPackages;
200			PackageList			fUninstalledPackages;
201			PackageList			fDownloadingPackages;
202			PackageList			fUpdateablePackages;
203			PackageList			fPopulatedPackages;
204
205			PackageFilterRef	fCategoryFilter;
206			BString				fDepotFilter;
207			PackageFilterRef	fSearchTermsFilter;
208			PackageFilterRef	fIsFeaturedFilter;
209
210			bool				fShowFeaturedPackages;
211			bool				fShowAvailablePackages;
212			bool				fShowInstalledPackages;
213			bool				fShowSourcePackages;
214			bool				fShowDevelopPackages;
215
216			LanguageModel		fLanguageModel;
217			WebAppInterface		fWebAppInterface;
218
219			ModelListenerList	fListeners;
220};
221
222
223#endif // PACKAGE_INFO_H
224