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