16d367ba4SStephan Aßmus/*
25bd5f161SStephan Aßmus * Copyright 2013-2014, Stephan A��mus <superstippi@gmx.de>.
3f85e0300SAndrew Lindesay * Copyright 2016-2019, Andrew Lindesay <apl@lindesay.co.nz>.
46d367ba4SStephan Aßmus * All rights reserved. Distributed under the terms of the MIT License.
56d367ba4SStephan Aßmus */
66d367ba4SStephan Aßmus#ifndef MODEL_H
76d367ba4SStephan Aßmus#define MODEL_H
86d367ba4SStephan Aßmus
9a3dfc57dSStephan Aßmus#include <FindDirectory.h>
107eec822dSStephan Aßmus#include <Locker.h>
116d367ba4SStephan Aßmus
123369e03dSAndrew Lindesay#include "AbstractProcess.h"
13f85e0300SAndrew Lindesay#include "LanguageModel.h"
1419c15fecSAndrew Lindesay#include "LocalIconStore.h"
156d367ba4SStephan Aßmus#include "PackageInfo.h"
1628075ee4SStephan Aßmus#include "WebAppInterface.h"
1728075ee4SStephan Aßmus
186d367ba4SStephan Aßmus
19a3dfc57dSStephan Aßmusclass BFile;
20e3fdc894SStephan Aßmusclass BMessage;
21a3dfc57dSStephan Aßmusclass BPath;
22e3fdc894SStephan Aßmus
236d367ba4SStephan Aßmus
244abd2b71SStephan Aßmusclass PackageFilter : public BReferenceable {
254abd2b71SStephan Aßmuspublic:
264abd2b71SStephan Aßmus	virtual						~PackageFilter();
274abd2b71SStephan Aßmus
284abd2b71SStephan Aßmus	virtual	bool				AcceptsPackage(
29779d8213SStephan Aßmus									const PackageInfoRef& package) const = 0;
304abd2b71SStephan Aßmus};
314abd2b71SStephan Aßmus
324abd2b71SStephan Aßmustypedef BReference<PackageFilter> PackageFilterRef;
334abd2b71SStephan Aßmus
344abd2b71SStephan Aßmus
358f03a0f9SStephan Aßmusclass ModelListener : public BReferenceable {
368f03a0f9SStephan Aßmuspublic:
378f03a0f9SStephan Aßmus	virtual						~ModelListener();
388f03a0f9SStephan Aßmus
398f03a0f9SStephan Aßmus	virtual	void				AuthorizationChanged() = 0;
4089b16bb4SAndrew Lindesay	virtual void				CategoryListChanged() = 0;
418f03a0f9SStephan Aßmus};
428f03a0f9SStephan Aßmus
433094fef3SAndrew Lindesay
443094fef3SAndrew Lindesayclass DepotMapper {
453094fef3SAndrew Lindesaypublic:
463094fef3SAndrew Lindesay	virtual DepotInfo			MapDepot(const DepotInfo& depot,
473094fef3SAndrew Lindesay									void* context) = 0;
483094fef3SAndrew Lindesay};
493094fef3SAndrew Lindesay
503094fef3SAndrew Lindesay
513094fef3SAndrew Lindesayclass PackageConsumer {
523094fef3SAndrew Lindesaypublic:
533094fef3SAndrew Lindesay	virtual	bool				ConsumePackage(
543094fef3SAndrew Lindesay									const PackageInfoRef& packageInfoRef,
553094fef3SAndrew Lindesay									void* context) = 0;
563094fef3SAndrew Lindesay};
573094fef3SAndrew Lindesay
583094fef3SAndrew Lindesay
598f03a0f9SStephan Aßmustypedef BReference<ModelListener> ModelListenerRef;
608f03a0f9SStephan Aßmustypedef List<ModelListenerRef, false> ModelListenerList;
618f03a0f9SStephan Aßmus
628f03a0f9SStephan Aßmus
636d367ba4SStephan Aßmusclass Model {
646d367ba4SStephan Aßmuspublic:
656d367ba4SStephan Aßmus								Model();
665bd5f161SStephan Aßmus	virtual						~Model();
676d367ba4SStephan Aßmus
68f85e0300SAndrew Lindesay			LanguageModel&		Language();
69f85e0300SAndrew Lindesay
707eec822dSStephan Aßmus			BLocker*			Lock()
717eec822dSStephan Aßmus									{ return &fLock; }
727eec822dSStephan Aßmus
738f03a0f9SStephan Aßmus			bool				AddListener(const ModelListenerRef& listener);
748f03a0f9SStephan Aßmus
756d367ba4SStephan Aßmus			// !Returns new PackageInfoList from current parameters
76706edd80SStephan Aßmus			PackageList			CreatePackageList() const;
776d367ba4SStephan Aßmus
7839f49f70SJulian Harnath			bool				MatchesFilter(
7939f49f70SJulian Harnath									const PackageInfoRef& package) const;
8039f49f70SJulian Harnath
816d367ba4SStephan Aßmus			bool				AddDepot(const DepotInfo& depot);
827b2c59b9SStephan Aßmus			bool				HasDepot(const BString& name) const;
8342479955SStephan Aßmus			const DepotList&	Depots() const
8442479955SStephan Aßmus									{ return fDepots; }
85ebb1a01cSAndrew Lindesay			const DepotInfo*	DepotForName(const BString& name) const;
867b2c59b9SStephan Aßmus			bool				SyncDepot(const DepotInfo& depot);
876d367ba4SStephan Aßmus
882c287f5bSRene Gollent			void				Clear();
892c287f5bSRene Gollent
9089b16bb4SAndrew Lindesay			void				AddCategories(const CategoryList& categories);
91633ed4c4SStephan Aßmus			const CategoryList&	Categories() const
92633ed4c4SStephan Aßmus									{ return fCategories; }
93633ed4c4SStephan Aßmus
94706edd80SStephan Aßmus			void				SetPackageState(
95779d8213SStephan Aßmus									const PackageInfoRef& package,
96706edd80SStephan Aßmus									PackageState state);
97706edd80SStephan Aßmus
9842479955SStephan Aßmus			// Configure PackageFilters
9942479955SStephan Aßmus			void				SetCategory(const BString& category);
1000c169aceSStephan Aßmus			BString				Category() const;
10109d44cecSStephan Aßmus			void				SetDepot(const BString& depot);
1020c169aceSStephan Aßmus			BString				Depot() const;
10309d44cecSStephan Aßmus			void				SetSearchTerms(const BString& searchTerms);
1040c169aceSStephan Aßmus			BString				SearchTerms() const;
105ab172803SStephan Aßmus
1064fade61fSStephan Aßmus			void				SetShowFeaturedPackages(bool show);
1074fade61fSStephan Aßmus			bool				ShowFeaturedPackages() const
1084fade61fSStephan Aßmus									{ return fShowFeaturedPackages; }
109ab172803SStephan Aßmus			void				SetShowAvailablePackages(bool show);
110ab172803SStephan Aßmus			bool				ShowAvailablePackages() const
111ab172803SStephan Aßmus									{ return fShowAvailablePackages; }
112ab172803SStephan Aßmus			void				SetShowInstalledPackages(bool show);
113ab172803SStephan Aßmus			bool				ShowInstalledPackages() const
114ab172803SStephan Aßmus									{ return fShowInstalledPackages; }
115870554acSStephan Aßmus			void				SetShowSourcePackages(bool show);
116870554acSStephan Aßmus			bool				ShowSourcePackages() const
117870554acSStephan Aßmus									{ return fShowSourcePackages; }
118870554acSStephan Aßmus			void				SetShowDevelopPackages(bool show);
119870554acSStephan Aßmus			bool				ShowDevelopPackages() const
120870554acSStephan Aßmus									{ return fShowDevelopPackages; }
12142479955SStephan Aßmus
1227eec822dSStephan Aßmus			// Retrieve package information
123a60a23f8SStephan Aßmus	static	const uint32		POPULATE_CACHED_RATING	= 1 << 0;
124a60a23f8SStephan Aßmus	static	const uint32		POPULATE_CACHED_ICON	= 1 << 1;
125a60a23f8SStephan Aßmus	static	const uint32		POPULATE_USER_RATINGS	= 1 << 2;
126a60a23f8SStephan Aßmus	static	const uint32		POPULATE_SCREEN_SHOTS	= 1 << 3;
127a60a23f8SStephan Aßmus	static	const uint32		POPULATE_CHANGELOG		= 1 << 4;
128a60a23f8SStephan Aßmus	static	const uint32		POPULATE_CATEGORIES		= 1 << 5;
12989e4ba17SStephan Aßmus	static	const uint32		POPULATE_FORCE			= 1 << 6;
130a60a23f8SStephan Aßmus
131a60a23f8SStephan Aßmus			void				PopulatePackage(const PackageInfoRef& package,
132a60a23f8SStephan Aßmus									uint32 flags);
1335bd5f161SStephan Aßmus
134d2d4866dSAndrew Lindesay			void				SetNickname(BString nickname);
135d2d4866dSAndrew Lindesay			const BString&		Nickname() const;
136d2d4866dSAndrew Lindesay			void				SetAuthorization(const BString& nickname,
137d2d4866dSAndrew Lindesay									const BString& passwordClear,
13889ec84a4SStephan Aßmus									bool storePassword);
13928075ee4SStephan Aßmus
14024cde1e4SStephan Aßmus			const WebAppInterface& GetWebAppInterface() const
14124cde1e4SStephan Aßmus									{ return fWebAppInterface; }
14224cde1e4SStephan Aßmus
1433094fef3SAndrew Lindesay			void				ReplaceDepotByUrl(
1443b17d8ddSAndrew Lindesay									const BString& URL,
1453094fef3SAndrew Lindesay									DepotMapper* depotMapper,
1463094fef3SAndrew Lindesay									void* context);
14724cde1e4SStephan Aßmus
1483094fef3SAndrew Lindesay			status_t			IconStoragePath(BPath& path) const;
149f85e0300SAndrew Lindesay			status_t			DumpExportReferenceDataPath(BPath& path) const;
1503094fef3SAndrew Lindesay			status_t			DumpExportRepositoryDataPath(BPath& path) const;
1513094fef3SAndrew Lindesay			status_t			DumpExportPkgDataPath(BPath& path,
152c23b641cSAndrew Lindesay									const BString& repositorySourceCode) const;
1533094fef3SAndrew Lindesay
1543094fef3SAndrew Lindesay			void				LogDepotsWithNoWebAppRepositoryCode() const;
1553094fef3SAndrew Lindesay
1563094fef3SAndrew Lindesayprivate:
15789b16bb4SAndrew Lindesay			void				_AddCategory(const CategoryRef& category);
158f85e0300SAndrew Lindesay			status_t			_LocalDataPath(const BString leaf,
159f85e0300SAndrew Lindesay									BPath& path) const;
160f85e0300SAndrew Lindesay
16180a272eeSAndrew Lindesay			void				_MaybeLogJsonRpcError(
16280a272eeSAndrew Lindesay									const BMessage &responsePayload,
16380a272eeSAndrew Lindesay									const char *sourceDescription) const;
16480a272eeSAndrew Lindesay
16512107be2SStephan Aßmus			void				_UpdateIsFeaturedFilter();
16612107be2SStephan Aßmus
1675bd5f161SStephan Aßmus	static	int32				_PopulateAllPackagesEntry(void* cookie);
1685bd5f161SStephan Aßmus
16980a272eeSAndrew Lindesay			void				_PopulatePackageChangelog(
17080a272eeSAndrew Lindesay									const PackageInfoRef& package);
17180a272eeSAndrew Lindesay
1723094fef3SAndrew Lindesay			void				_PopulatePackageScreenshot(
1733094fef3SAndrew Lindesay									const PackageInfoRef& package,
1743094fef3SAndrew Lindesay									const ScreenshotInfo& info,
1753094fef3SAndrew Lindesay									int32 scaledWidth, bool fromCacheOnly);
17619c15fecSAndrew Lindesay
177a3dfc57dSStephan Aßmus			bool				_GetCacheFile(BPath& path, BFile& file,
178a3dfc57dSStephan Aßmus									directory_which directory,
179a3dfc57dSStephan Aßmus									const char* relativeLocation,
180a3dfc57dSStephan Aßmus									const char* fileName,
181a3dfc57dSStephan Aßmus									uint32 openMode) const;
182a3dfc57dSStephan Aßmus			bool				_GetCacheFile(BPath& path, BFile& file,
183a3dfc57dSStephan Aßmus									directory_which directory,
184a3dfc57dSStephan Aßmus									const char* relativeLocation,
185a3dfc57dSStephan Aßmus									const char* fileName,
186a3dfc57dSStephan Aßmus									bool ignoreAge, time_t maxAge) const;
187a3dfc57dSStephan Aßmus
1888f03a0f9SStephan Aßmus			void				_NotifyAuthorizationChanged();
18989b16bb4SAndrew Lindesay			void				_NotifyCategoryListChanged();
1908f03a0f9SStephan Aßmus
1916d367ba4SStephan Aßmusprivate:
1927eec822dSStephan Aßmus			BLocker				fLock;
1937eec822dSStephan Aßmus
19442479955SStephan Aßmus			DepotList			fDepots;
195f75ec10dSStephan Aßmus
196633ed4c4SStephan Aßmus			CategoryList		fCategories;
1974abd2b71SStephan Aßmus
198706edd80SStephan Aßmus			PackageList			fInstalledPackages;
199706edd80SStephan Aßmus			PackageList			fActivatedPackages;
200706edd80SStephan Aßmus			PackageList			fUninstalledPackages;
2013a6ccc85SStephan Aßmus			PackageList			fDownloadingPackages;
2023a6ccc85SStephan Aßmus			PackageList			fUpdateablePackages;
2037eec822dSStephan Aßmus			PackageList			fPopulatedPackages;
204706edd80SStephan Aßmus
2054abd2b71SStephan Aßmus			PackageFilterRef	fCategoryFilter;
20609d44cecSStephan Aßmus			BString				fDepotFilter;
2074abd2b71SStephan Aßmus			PackageFilterRef	fSearchTermsFilter;
20812107be2SStephan Aßmus			PackageFilterRef	fIsFeaturedFilter;
209870554acSStephan Aßmus
2104fade61fSStephan Aßmus			bool				fShowFeaturedPackages;
211ab172803SStephan Aßmus			bool				fShowAvailablePackages;
212ab172803SStephan Aßmus			bool				fShowInstalledPackages;
213870554acSStephan Aßmus			bool				fShowSourcePackages;
214870554acSStephan Aßmus			bool				fShowDevelopPackages;
2155bd5f161SStephan Aßmus
216f85e0300SAndrew Lindesay			LanguageModel		fLanguageModel;
21728075ee4SStephan Aßmus			WebAppInterface		fWebAppInterface;
2188f03a0f9SStephan Aßmus
2198f03a0f9SStephan Aßmus			ModelListenerList	fListeners;
2206d367ba4SStephan Aßmus};
2216d367ba4SStephan Aßmus
2226d367ba4SStephan Aßmus
2236d367ba4SStephan Aßmus#endif // PACKAGE_INFO_H
224