19#include <rapidjson/document.h>
42 return avatarFileName.GetFullPath();
65 if (!oauthService.HasAccessToken())
74 std::string(oauthService.GetAccessToken()));
81 response->setRequestFinishedCallback(
82 [response,
this](
auto)
84 const auto httpCode = response->getHTTPCode();
89 const auto body = response->readAll<std::string>();
91 using namespace rapidjson;
94 document.Parse(body.data(), body.size());
96 if (!document.IsObject())
99 const auto username = document[
"username"].GetString();
100 const auto avatar = document[
"avatar"].GetString();
101 const auto profileName = document[
"profile"][
"name"].GetString();
104 [
this, username = std::string(username),
105 profileName = std::string(profileName),
106 avatar = std::string(avatar)]()
144 const auto avatarTempPath = avatarPath +
".tmp";
148 if (wxFileExists(avatarPath))
149 wxRemoveFile(avatarPath);
154 std::shared_ptr<wxFile> avatarFile = std::make_shared<wxFile>();
156 if (!avatarFile->Create(avatarTempPath,
true))
161 auto request =
Request(std::string(url));
166 if (!etag.empty() && wxFileExists(avatarPath))
171 response->setOnDataReceivedCallback(
172 [response, avatarFile](
auto)
174 std::vector<char> buffer(response->getBytesAvailable());
176 size_t bytes = response->readData(buffer.data(), buffer.size());
178 avatarFile->Write(buffer.data(), buffer.size());
181 response->setRequestFinishedCallback(
182 [response, avatarFile, avatarPath, avatarTempPath,
this](
auto)
186 const auto httpCode = response->getHTTPCode();
191 wxRemoveFile(avatarTempPath);
195 const auto etag = response->getHeader(
"ETag");
196 const auto oldPath = avatarPath +
".old";
198 if (wxFileExists(avatarPath))
199 if (!wxRenameFile(avatarPath, oldPath))
202 if (!wxRenameFile(avatarTempPath, avatarPath))
205 wxRenameFile(oldPath, avatarPath);
209 if (wxFileExists(oldPath))
210 wxRemoveFile(oldPath);
237 if (!wxFileExists(path))
Toolkit-neutral facade for basic user interface services.
Declare functions to perform UTF-8 to std::wstring conversions.
Declare an interface for HTTP response.
Declare a class for performing HTTP requests.
Declare a class for constructing HTTP requests.
virtual bool Flush(bool bCurrentOnly=false) wxOVERRIDE
Subscription Subscribe(Callback callback)
Connect a callback to the Publisher; later-connected are called earlier.
CallbackReturn Publish(const UserDataChanged &message)
Send a message to connected callbacks.
A move-only handle representing a connection to a Publisher.
bool Write(const T &value)
Write value to config and return true if successful.
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined */
Specialization of Setting for strings.
Request & setHeader(const std::string &name, std::string value)
Service for providing information about the user profile.
void DownloadAvatar(std::string_view url)
wxString GetDisplayName() const
Get the user name to display in the dialog.
wxString GetAvatarPath() const
Gets a path to the avatar.
void UpdateUserData()
Request the service to update the data.
wxString GetUserSlug() const
"Slug" used to construct shareable URLs
void ClearUserData()
Reset the user profile data.
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
FILES_API FilePath ConfigDir()
Audacity user config directory.
FrameStatistics & GetInstance() noexcept
const std::string ApplicationJson
std::string ToUTF8(const std::wstring &wstr)
wxString ToWXString(const std::string &str)
wxString MakeAvatarPath()
Observer::Subscription authStateChangedSubscription
StringSetting displayName
const ServiceConfig & GetServiceConfig()
Returns the instance of the ServiceConfig.
OAuthService & GetOAuthService()
Returns the instance of the OAuthService.
UserService & GetUserService()