xref: /haiku/src/apps/haikudepot/ui/UserLoginWindow.h (revision bf866d5ef7dead2119e2a229ea580e33c67f27a7)
1 /*
2  * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2019-2024, Andrew Lindesay <apl@lindesay.co.nz>.
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 #ifndef USER_LOGIN_WINDOW_H
7 #define USER_LOGIN_WINDOW_H
8 
9 #include <Locker.h>
10 #include <Messenger.h>
11 #include <Window.h>
12 
13 #include "CreateUserDetail.h"
14 #include "PackageInfo.h"
15 #include "PasswordRequirements.h"
16 #include "UserCredentials.h"
17 #include "ValidationFailure.h"
18 
19 
20 class BButton;
21 class BCheckBox;
22 class BMenuField;
23 class BTabView;
24 class BTextControl;
25 class BitmapView;
26 class Captcha;
27 class LinkView;
28 class Model;
29 class UserUsageConditions;
30 
31 
32 class UserLoginWindow : public BWindow {
33 public:
34 								UserLoginWindow(BWindow* parent, BRect frame,
35 									Model& model);
36 	virtual						~UserLoginWindow();
37 
38 	virtual	bool				QuitRequested();
39 	virtual	void				MessageReceived(BMessage* message);
40 
41 			void				SetOnSuccessMessage(
42 									const BMessenger& messenger,
43 									const BMessage& message);
44 
45 private:
46 
47 			enum Mode {
48 				NONE = 0,
49 				LOGIN,
50 				CREATE_ACCOUNT
51 			};
52 
53 			void				_SetMode(Mode mode);
54 			void				_SetWorkerThread(thread_id thread);
55 			void				_SetWorkerThreadLocked(thread_id thread);
56 
57 			void				_Authenticate();
58 			void				_Authenticate(
59 									const UserCredentials& credentials);
60 	static	int32				_AuthenticateThreadEntry(void* data);
61 			void				_AuthenticateThread(
62 									UserCredentials& credentials);
63 			void				_UnpackAuthenticationToken(
64 									BMessage& responsePayload, BString& token);
65 			void				_HandleAuthenticationFailed();
66 			void				_HandleAuthenticationSuccess(
67 									const UserCredentials & credentials);
68 			void				_HandleAuthenticationError();
69 
70 			void				_CreateAccount();
71 			void				_AssembleCreateUserDetail(
72 									CreateUserDetail& detail);
73 			void				_ValidateCreateUserDetail(
74 									CreateUserDetail& detail,
75 									ValidationFailures& failures);
76 			void				_AlertCreateUserValidationFailure(
77 									const ValidationFailures& failures);
78 	static	BString				_CreateAlertTextFromValidationFailure(
79 									const BString& property,
80 									const BString& message);
81 			void				_MarkCreateUserInvalidFields();
82 			void				_MarkCreateUserInvalidFields(
83 									const ValidationFailures& failures);
84 	static	int32				_CreateAccountThreadEntry(void* data);
85 			void				_CreateAccountThread(CreateUserDetail* detail);
86 			void				_HandleCreateAccountSuccess(
87 									const UserCredentials& credentials);
88 			void				_HandleCreateAccountFailure(
89 									const ValidationFailures& failures);
90 			void				_HandleCreateAccountError();
91 
92 			void				_CreateAccountSetup(uint32 mask);
93 			void				_CreateAccountSetupIfNecessary();
94 	static	int32				_CreateAccountSetupThreadEntry(void* data);
95 			status_t			_CreateAccountCaptchaSetupThread(
96 									Captcha& captcha);
97 			status_t			_CreateAccountUserUsageConditionsSetupThread(
98 									UserUsageConditions& userUsageConditions);
99 			status_t			_CreateAccountPasswordRequirementsSetupThread(
100 									PasswordRequirements& passwordRequirements);
101 			status_t			_UnpackCaptcha(BMessage& responsePayload,
102 									Captcha& captcha);
103 			void				_HandleCreateAccountSetupSuccess(
104 									BMessage* message);
105 
106 			void				_SetCaptcha(Captcha* captcha);
107 			void				_SetUserUsageConditions(
108 									UserUsageConditions* userUsageConditions);
109 			void				_SetPasswordRequirements(
110 									PasswordRequirements* passwordRequirements);
111 
112 			void				_CollectValidationFailures(
113 									const BMessage& result,
114 									BString& error) const;
115 
116 			void				_ViewUserUsageConditions();
117 			void				_ViewPasswordRequirements();
118 
119 			void				_TakeUpCredentialsAndQuit(
120 									const UserCredentials& credentials);
121 
122 			void				_EnableMutableControls(bool enabled);
123 
124 	static	void				_ValidationFailuresToString(
125 									const ValidationFailures& failures,
126 									BString& output);
127 
128 private:
129 			BMessenger			fOnSuccessTarget;
130 			BMessage			fOnSuccessMessage;
131 
132 			BTabView*			fTabView;
133 
134 			BTextControl*		fNicknameField;
135 			BTextControl*		fPasswordField;
136 
137 			BTextControl*		fNewNicknameField;
138 			BTextControl*		fNewPasswordField;
139 			BTextControl*		fRepeatPasswordField;
140 			LinkView*			fPasswordRequirementsLink;
141 			BTextControl*		fEmailField;
142 			BMenuField*			fLanguageIdField;
143 			BitmapView*			fCaptchaView;
144 			BTextControl*		fCaptchaResultField;
145 			BCheckBox*			fConfirmMinimumAgeCheckBox;
146 			BCheckBox*			fConfirmUserUsageConditionsCheckBox;
147 			LinkView*			fUserUsageConditionsLink;
148 
149 			BButton*			fSendButton;
150 			BButton*			fCancelButton;
151 
152 			PasswordRequirements*
153 								fPasswordRequirements;
154 			UserUsageConditions*
155 								fUserUsageConditions;
156 			Captcha*			fCaptcha;
157 			BString				fPreferredLanguageId;
158 
159 			Model&				fModel;
160 
161 			Mode				fMode;
162 
163 			BLocker				fLock;
164 			thread_id			fWorkerThread;
165 			bool				fQuitRequestedDuringWorkerThread;
166 };
167 
168 
169 #endif // USER_LOGIN_WINDOW_H
170