xref: /haiku/src/apps/haikudepot/model/Model.h (revision 909af08f4328301fbdef1ffb41f566c3b5bec0c7)
1 /*
2  * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2016-2024, 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 "LanguageRepository.h"
16 #include "PackageFilterModel.h"
17 #include "PackageIconTarRepository.h"
18 #include "PackageInfo.h"
19 #include "PackageScreenshotRepository.h"
20 #include "RatingStability.h"
21 #include "ScreenshotCoordinate.h"
22 #include "WebAppInterface.h"
23 
24 
25 class BFile;
26 class BMessage;
27 class BPath;
28 
29 
30 typedef enum package_list_view_mode {
31 	PROMINENT,
32 	ALL
33 } package_list_view_mode;
34 
35 
36 class ModelListener : public BReferenceable {
37 public:
38 	virtual						~ModelListener();
39 
40 	virtual	void				AuthorizationChanged() = 0;
41 	virtual void				CategoryListChanged() = 0;
42 	virtual void				ScreenshotCached(const ScreenshotCoordinate& coordinate) = 0;
43 };
44 
45 
46 typedef BReference<ModelListener> ModelListenerRef;
47 
48 
49 class PackageConsumer {
50 public:
51 	virtual	bool				ConsumePackage(
52 									const PackageInfoRef& packageInfoRef,
53 									void* context) = 0;
54 };
55 
56 
57 class Model : public PackageScreenshotRepositoryListener {
58 public:
59 								Model();
60 	virtual						~Model();
61 
62 			PackageFilterModel*	PackageFilter();
63 			PackageIconRepository&
64 								GetPackageIconRepository();
65 			status_t			InitPackageIconRepository();
66 			PackageScreenshotRepository*
67 								GetPackageScreenshotRepository();
68 
69 			BLocker*			Lock()
70 									{ return &fLock; }
71 
72 			void				AddListener(const ModelListenerRef& listener);
73 
74 			LanguageRef			PreferredLanguage() const;
75 			void				SetPreferredLanguage(LanguageRef value);
76 			LanguageRepository*	Languages();
77 
78 			PackageInfoRef		PackageForName(const BString& name);
79 
80 			void				MergeOrAddDepot(const DepotInfoRef& depot);
81 			bool				HasDepot(const BString& name) const;
82 			int32				CountDepots() const;
83 			DepotInfoRef		DepotAtIndex(int32 index) const;
84 			const DepotInfoRef	DepotForName(const BString& name) const;
85 			bool				HasAnyProminentPackages();
86 
87 			void				Clear();
88 
89 			int32				CountCategories() const;
90 			CategoryRef			CategoryByCode(BString& code) const;
91 			CategoryRef			CategoryAtIndex(int32 index) const;
92 			void				AddCategories(
93 									std::vector<CategoryRef>& values);
94 
95 			int32				CountRatingStabilities() const;
96 			RatingStabilityRef	RatingStabilityByCode(BString& code) const;
97 			RatingStabilityRef	RatingStabilityAtIndex(int32 index) const;
98 			void				AddRatingStabilities(
99 									std::vector<RatingStabilityRef>& values);
100 
101 			void				SetStateForPackagesByName(
102 									BStringList& packageNames,
103 									PackageState state);
104 
105 
106 			void				SetPackageListViewMode(
107 									package_list_view_mode mode);
108 			package_list_view_mode
109 								PackageListViewMode() const
110 									{ return fPackageListViewMode; }
111 
112 			void				SetCanShareAnonymousUsageData(bool value);
113 			bool				CanShareAnonymousUsageData() const
114 									{ return fCanShareAnonymousUsageData; }
115 
116 			bool				CanPopulatePackage(
117 									const PackageInfoRef& package);
118 
119 			void				SetNickname(BString nickname);
120 			const BString&		Nickname();
121 			void				SetCredentials(const BString& nickname,
122 									const BString& passwordClear,
123 									bool storePassword);
124 
125 			WebAppInterface*    GetWebAppInterface()
126 									{ return &fWebAppInterface; }
127 
128 			// PackageScreenshotRepositoryListener
129     virtual	void				ScreenshotCached(const ScreenshotCoordinate& coord);
130 
131 private:
132 			void				_AddCategory(const CategoryRef& category);
133 
134 			void				_AddRatingStability(
135 									const RatingStabilityRef& value);
136 
137 			void				_MaybeLogJsonRpcError(
138 									const BMessage &responsePayload,
139 									const char *sourceDescription) const;
140 
141 			void				_NotifyAuthorizationChanged();
142 			void				_NotifyCategoryListChanged();
143 
144 private:
145 			BLocker				fLock;
146 
147 			LanguageRef			fPreferredLanguage;
148 
149 			std::vector<DepotInfoRef>
150 								fDepots;
151 			std::vector<CategoryRef>
152 								fCategories;
153 			std::vector<RatingStabilityRef>
154 								fRatingStabilities;
155 
156 			BStringList			fPopulatedPackageNames;
157 
158 			package_list_view_mode
159 								fPackageListViewMode;
160 
161 			bool				fCanShareAnonymousUsageData;
162 
163 			WebAppInterface		fWebAppInterface;
164 
165 			PackageFilterModel*	fPackageFilterModel;
166 
167 			LanguageRepository*	fLanguageRepository;
168 			PackageIconTarRepository
169 								fPackageIconRepository;
170 			PackageScreenshotRepository*
171 								fPackageScreenshotRepository;
172 
173 			std::vector<ModelListenerRef>
174 								fListeners;
175 };
176 
177 
178 #endif // PACKAGE_INFO_H
179