xref: /haiku/src/apps/haikudepot/ui/SettingsWindow.cpp (revision bb83316a5811a550c4f850d07fa8e328e7ac0a94)
1 /*
2  * Copyright 2021, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #include "SettingsWindow.h"
6 
7 #include <Button.h>
8 #include <Catalog.h>
9 #include <CheckBox.h>
10 #include <LayoutBuilder.h>
11 #include <Locker.h>
12 #include <SeparatorView.h>
13 
14 #include "Logger.h"
15 #include "Model.h"
16 #include "UserUsageConditionsWindow.h"
17 #include "ServerHelper.h"
18 #include "WebAppInterface.h"
19 
20 
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "SettingsWindow"
23 
24 #define WINDOW_FRAME BRect(0, 0, 500, 280)
25 
26 
27 enum {
28 	MSG_APPLY =									'aply',
29 };
30 
31 
32 SettingsWindow::SettingsWindow(BWindow* parent, Model* model)
33 	:
34 	BWindow(WINDOW_FRAME, B_TRANSLATE("Settings"),
35 		B_FLOATING_WINDOW_LOOK, B_MODAL_SUBSET_WINDOW_FEEL,
36 		B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS
37 			| B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_NOT_CLOSABLE ),
38 	fModel(model)
39 {
40 	AddToSubset(parent);
41 	_InitUiControls();
42 	_UpdateUiFromModel();
43 
44 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
45 		.AddGroup(B_VERTICAL, 0)
46 			.SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING,
47 				B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING)
48 			.Add(fCanShareAnonymousUsageDataCheckBox)
49 		.End()
50 		.Add(new BSeparatorView(B_HORIZONTAL))
51 			// rule off
52 		.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
53 			.SetInsets(0, B_USE_DEFAULT_SPACING,
54 				B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING)
55 			.AddGlue()
56 			.Add(fCancelButton)
57 			.Add(fApplyButton)
58 		.End();
59 
60 	CenterOnScreen();
61 }
62 
63 
64 SettingsWindow::~SettingsWindow()
65 {
66 }
67 
68 
69 void
70 SettingsWindow::_InitUiControls()
71 {
72 	fCanShareAnonymousUsageDataCheckBox = new BCheckBox(
73 		"share anonymous usage data",
74 		B_TRANSLATE("Share anonymous usage data with HaikuDepotServer"), NULL);
75 
76 	fApplyButton = new BButton("apply", B_TRANSLATE("Apply"),
77 		new BMessage(MSG_APPLY));
78 	fCancelButton = new BButton("cancel", B_TRANSLATE("Cancel"),
79 		new BMessage(B_QUIT_REQUESTED));
80 }
81 
82 
83 void
84 SettingsWindow::_UpdateUiFromModel()
85 {
86 	fCanShareAnonymousUsageDataCheckBox->SetValue(
87 		fModel->CanShareAnonymousUsageData() ? 1 : 0);
88 }
89 
90 
91 void
92 SettingsWindow::_UpdateModelFromUi()
93 {
94 	fModel->SetCanShareAnonymousUsageData(
95 		0 != fCanShareAnonymousUsageDataCheckBox->Value());
96 }
97 
98 
99 void
100 SettingsWindow::MessageReceived(BMessage* message)
101 {
102 	switch (message->what) {
103 		case MSG_APPLY:
104 			_UpdateModelFromUi();
105 			BMessenger(this).SendMessage(B_QUIT_REQUESTED);
106 			break;
107 		default:
108 			BWindow::MessageReceived(message);
109 			break;
110 	}
111 }
112