1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************
Audacity: A Digital Audio Editor
CloudProjectPropertiesDialog.cpp
Dmitry Vedenko
**********************************************************************/
#include "CloudProjectPropertiesDialog.h"
#include <wx/button.h>
#include <wx/choice.h>
#include <wx/sizer.h>
#include <wx/statline.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include "../UserPanel.h"
#include "LoginDialog.h"
#include "AccessibilityUtils.h"
#include "AuthorizationHandler.h"
#include "CodeConversions.h"
#include "OAuthService.h"
namespace audacity::cloud::audiocom::sync
{
CloudProjectPropertiesDialog::CloudProjectPropertiesDialog(
const ServiceConfig& serviceConfig, OAuthService& authService,
UserService& userService, const wxString& projectName, wxWindow* parent,
AudiocomTrace trace)
: wxDialogWrapper { parent, wxID_ANY, XO("Save to audio.com") }
{
mUserPanel =
new UserPanel(serviceConfig, authService, userService,
false, trace, this);
mProjectName = new wxTextCtrl { this, wxID_ANY,
projectName, wxDefaultPosition,
wxDefaultSize, wxTE_PROCESS_ENTER };
mProjectName->SetName(XO("Project Name").Translation());
if (!projectName.empty())
{
mProjectName->SetValue(projectName);
// mProjectName->SetInsertionPoint(-1);
}
const wxString choices[] = { XO("Private").Translation(),
XO("Unlisted").Translation(),
XO("Public").Translation() };
mSaveToCloud = new wxButton { this, wxID_ANY, XO("Save").Translation() };
mSaveLocally =
new wxButton { this, wxID_ANY, XO("Save to computer...").Translation() };
mCancel = new wxButton { this, wxID_ANY, XO("Cancel").Translation() };
mProjectName->SetFocus();
LayoutControls();
OnUpdateCloudSaveState();
SetupEvents();
SetupAccessibility(this);
}
CloudProjectPropertiesDialog::~CloudProjectPropertiesDialog()
{
GetAuthorizationHandler().PopSuppressDialogs();
}
std::pair<CloudProjectPropertiesDialog::Action, wxString>
CloudProjectPropertiesDialog::Show(
const ServiceConfig& serviceConfig, OAuthService& authService,
UserService& userService, const wxString& projectName, wxWindow* parent,
bool allowLocalSave, AudiocomTrace trace)
{
CloudProjectPropertiesDialog dialog { serviceConfig, authService,
userService, projectName,
parent, trace };
dialog.mSaveLocally->Show(allowLocalSave);
const auto resultCode = dialog.ShowModal();
const auto action =
resultCode == wxID_OK ?
Action::SaveToCloud :
(resultCode == wxID_CANCEL ? Action::Cancel : Action::SaveLocally);
return { action, dialog.GetProjectName() };
}
bool CloudProjectPropertiesDialog::OnSubmit()
{
const bool canSubmit =
mUserPanel->IsAuthorized() && !GetProjectName().empty();
if (!canSubmit)
return false;
EndModal(wxID_OK);
return true;
}
void CloudProjectPropertiesDialog::LayoutControls()
{
auto* topSizer = new wxBoxSizer { wxVERTICAL };
constexpr auto spacerHeight = 8;
topSizer->AddSpacer(spacerHeight);
topSizer->Add(mUserPanel, 0, wxEXPAND | wxLEFT | wxRIGHT, 16);
topSizer->AddSpacer(spacerHeight);
topSizer->Add(new wxStaticLine { this }, 0, wxEXPAND | wxLEFT | wxRIGHT, 16);
topSizer->AddSpacer(spacerHeight);
topSizer->Add(
new wxStaticText { this, wxID_ANY, XO("Project Name").Translation() }, 0,
wxEXPAND | wxLEFT | wxRIGHT, 16);
topSizer->AddSpacer(spacerHeight);
topSizer->Add(mProjectName, 0, wxEXPAND | wxLEFT | wxRIGHT, 16);
topSizer->AddSpacer(spacerHeight);
topSizer->AddSpacer(2 * spacerHeight);
auto buttonSizer = new wxBoxSizer { wxHORIZONTAL };
buttonSizer->Add(mSaveLocally, 0, wxLEFT, 16);
buttonSizer->AddStretchSpacer();
buttonSizer->Add(mCancel, 0, wxRIGHT, 4);
buttonSizer->Add(mSaveToCloud, 0, wxRIGHT, 16);
topSizer->Add(buttonSizer, 0, wxEXPAND | wxALL, 0);
topSizer->AddSpacer(spacerHeight);
SetMinSize({ 450, -1 });
SetSizer(topSizer);
Fit();
Centre(wxBOTH);
}
void CloudProjectPropertiesDialog::SetupEvents()
{
mSaveToCloud->Bind(wxEVT_BUTTON, [this](auto&)
{
Disable();
if(!GetOAuthService().HasAccessToken())
{
LoginDialog::SignIn(this, LoginDialog::Mode::Create);
}
Enable();
if(GetOAuthService().HasAccessToken())
{
EndModal(wxID_OK);
}
});
mSaveLocally->Bind(wxEVT_BUTTON, [this](auto&) { EndModal(wxID_SAVE); });
mCancel->Bind(wxEVT_BUTTON, [this](auto&) { EndModal(wxID_CANCEL); });
Bind(
wxEVT_CHAR_HOOK,
[this](auto& evt)
{
if (!IsEscapeKey(evt))
{
evt.Skip();
return;
}
EndModal(wxID_CANCEL);
});
Bind(
wxEVT_KEY_UP,
[this](auto& evt)
{
const auto keyCode = evt.GetKeyCode();
if (keyCode != WXK_RETURN && keyCode != WXK_NUMPAD_ENTER)
{
evt.Skip();
return;
}
if (!OnSubmit())
evt.Skip();
});
mProjectName->Bind(wxEVT_TEXT, [this](auto&) { OnUpdateCloudSaveState(); });
mProjectName->Bind(wxEVT_TEXT_ENTER, [this](auto&) { OnSubmit(); });
}
wxString CloudProjectPropertiesDialog::GetProjectName() const
{
wxString result { mProjectName->GetValue() };
result.Trim(true).Trim(false);
return result;
}
void CloudProjectPropertiesDialog::OnUpdateCloudSaveState()
{
mSaveToCloud->Enable(!GetProjectName().empty());
}
} // namespace audacity::cloud::audiocom::sync
|