Audacity 3.2.0
UserPanel.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 UserPanel.cpp
7
8 Dmitry Vedenko
9
10**********************************************************************/
11
12#include "UserPanel.h"
13
14#include <cassert>
15
16#include <wx/button.h>
17#include <wx/sizer.h>
18#include <wx/stattext.h>
19
20#ifdef HAS_CUSTOM_URL_HANDLING
21# include "URLSchemesRegistry.h"
22#endif
23
25
26#include "OAuthService.h"
27#include "ServiceConfig.h"
28#include "UserService.h"
29
30#include "CodeConversions.h"
31
32#include "AllThemeResources.h"
33#include "Theme.h"
34
35#include "UserImage.h"
36
37#include "MemoryX.h"
38
39#include "HelpSystem.h"
40
42{
43
44namespace
45{
46const wxSize avatarSize = { 32, 32 };
47const auto anonymousText = XO("Account not linked");
48} // namespace
49
51 const ServiceConfig& serviceConfig, OAuthService& authService,
52 UserService& userService, bool hasLinkButton, wxWindow* parent,
53 const wxPoint& pos, const wxSize& size)
54 : wxPanelWrapper { parent, wxID_ANY, pos, size }
55 , mServiceConfig { serviceConfig }
56 , mAuthService { authService }
57 , mUserService { userService }
58 , mUserDataChangedSubscription { userService.Subscribe(
59 [this](const auto&) { UpdateUserData(); }) }
60{
61 mUserImage = safenew UserImage(this, avatarSize);
62 mUserImage->SetLabel(anonymousText); // for screen readers
63 mUserName =
64 safenew wxStaticText(this, wxID_ANY, anonymousText.Translation());
65 mLinkButton =
66 safenew wxButton(this, wxID_ANY, XXO("&Link Account").Translation());
67 mLinkButton->Bind(wxEVT_BUTTON, [this](auto) { OnLinkButtonPressed(); });
68 mLinkButton->Show(hasLinkButton);
69
70 auto sizer = safenew wxBoxSizer { wxHORIZONTAL };
71
72 sizer->Add(mUserImage, 0, wxALIGN_CENTER_VERTICAL);
73 sizer->AddSpacer(8);
74 sizer->Add(mUserName, 0, wxALIGN_CENTER_VERTICAL);
75 sizer->AddStretchSpacer();
76 sizer->Add(mLinkButton, 0, wxALIGN_CENTER_VERTICAL);
77
78 SetSizerAndFit(sizer);
79 UpdateUserData();
80}
81
82UserPanel::~UserPanel() = default;
83
84bool UserPanel::IsAuthorized() const
85{
86 return mIsAuthorized;
87}
88
89void UserPanel::OnStateChaged(bool isAuthorized)
90{
91 if (mIsAuthorized == isAuthorized)
92 return;
93
94 mIsAuthorized = isAuthorized;
95 Publish(UserPanelStateChangedMessage { isAuthorized });
96}
97
98void UserPanel::UpdateUserData()
99{
100 Freeze();
101
102 auto layoutUpdater = finally(
103 [this]()
104 {
105 mLinkButton->Fit();
106
107 Layout();
108 Thaw();
109
110 auto parent = GetParent();
111
112 if (parent != nullptr)
113 parent->Refresh();
114 else
115 Refresh();
116 });
117
118 auto& oauthService = GetOAuthService();
119
120 if (!oauthService.HasRefreshToken())
121 {
122 SetAnonymousState();
123 return;
124 }
125
126 if (!oauthService.HasAccessToken())
127 oauthService.ValidateAuth({}, true);
128
129 auto& userService = GetUserService();
130
131 if (userService.GetUserSlug().empty())
132 {
133 SetAnonymousState();
134 return;
135 }
136
137 const auto displayName = userService.GetDisplayName();
138
139 if (!displayName.empty()) {
140 mUserName->SetLabel(displayName);
141 mUserImage->wxPanel::SetLabel(displayName); // for screen readers
142 }
143
144 const auto avatarPath = userService.GetAvatarPath();
145
146 if (!avatarPath.empty())
147 mUserImage->SetBitmap(avatarPath);
148 else
149 mUserImage->SetBitmap(theTheme.Bitmap(bmpAnonymousUser));
150
151 mLinkButton->SetLabel(XXO("&Unlink Account").Translation());
152
153 OnStateChaged(true);
154}
155
156void UserPanel::OnLinkButtonPressed()
157{
158 auto& oauthService = GetOAuthService();
159
160 if (oauthService.HasAccessToken())
161 oauthService.UnlinkAccount();
162 else
163 {
165 { audacity::ToWXString(mServiceConfig.GetOAuthLoginPage()) });
166
167#ifdef HAS_CUSTOM_URL_HANDLING
169#endif
170 {
171 LinkWithTokenDialog dlg(this);
172 dlg.ShowModal();
173 }
174 }
175}
176
177void UserPanel::SetAnonymousState()
178{
179 mUserName->SetLabel(anonymousText.Translation());
180 mUserImage->SetBitmap(theTheme.Bitmap(bmpAnonymousUser));
181 mUserImage->SetLabel(anonymousText); // for screen readers
182 mLinkButton->SetLabel(XXO("&Link Account").Translation());
183
184 OnStateChaged(false);
185}
186
187} // namespace audacity::cloud::audiocom
Declare functions to perform UTF-8 to std::wstring conversions.
XO("Cut/Copy/Paste")
XXO("&Cut/Copy/Paste Toolbar")
#define safenew
Definition: MemoryX.h:10
THEME_API Theme theTheme
Definition: Theme.cpp:82
wxBitmap & Bitmap(int iIndex)
bool IsURLHandlingSupported() const noexcept
Returns true, if Audacity can handle custom URLs.
static URLSchemesRegistry & Get()
Retrieves the registry instance.
Service responsible for OAuth authentication against the audio.com service.
Definition: OAuthService.h:40
Configuration for the audio.com.
Definition: ServiceConfig.h:23
UserPanel(const ServiceConfig &serviceConfig, OAuthService &authService, UserService &userService, bool hasLinkButton, wxWindow *parent=nullptr, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize)
Definition: UserPanel.cpp:50
Service for providing information about the user profile.
Definition: UserService.h:28
void SetLabel(const TranslatableString &title)
bool OpenInDefaultBrowser(const wxString &url)
Open an URL in default browser.
Definition: BasicUI.cpp:246
UserService & GetUserService()
OAuthService & GetOAuthService()
Returns the instance of the OAuthService.
wxString ToWXString(const std::string &str)