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