File: LinkWithTokenDialog.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (154 lines) | stat: -rw-r--r-- 3,930 bytes parent folder | download
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
/*  SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************

  Audacity: A Digital Audio Editor

  LinkAccountDialog.cpp

  Dmitry Vedenko

**********************************************************************/
#include "LinkWithTokenDialog.h"

#include <wx/button.h>
#include <wx/textctrl.h>
#include <wx/statline.h>
#include <wx/weakref.h>

#include "ShuttleGui.h"

#include "BasicUI.h"
#include "CodeConversions.h"
#include "ExportUtils.h"

#include "OAuthService.h"

#include "AuthorizationHandler.h"
#include "LinkFailedDialog.h"
#include "LinkSucceededDialog.h"

namespace audacity::cloud::audiocom
{
LinkWithTokenDialog::LinkWithTokenDialog(AudiocomTrace trace, wxWindow* parent)
    : wxDialogWrapper(
         parent, wxID_ANY, XO("Link account"), wxDefaultPosition, { 480, -1 },
         wxDEFAULT_DIALOG_STYLE)
    , mAudiocomTrace(trace)
{
   GetAuthorizationHandler().PushSuppressDialogs();

   ShuttleGui s(this, eIsCreating);

   s.StartVerticalLay();
   {
      s.StartInvisiblePanel(16);
      {
         s.SetBorder(0);
         s.AddFixedText(XO("Enter token to link your account"));

         s.AddSpace(0, 4, 0);

         mToken = s.AddTextBox(TranslatableString {}, {}, 60);
         mToken->SetName(XO("Token").Translation());
         mToken->Bind(wxEVT_TEXT, [this](auto) { OnTextChanged(); });

         s.AddSpace(0, 16, 0);

         s.AddWindow(safenew wxStaticLine { s.GetParent() }, wxEXPAND);

         s.AddSpace(0, 10, 0);

         s.StartHorizontalLay(wxEXPAND, 0);
         {
            s.AddSpace(0, 0, 1);

            s.AddButton(XXO("&Cancel"))
               ->Bind(wxEVT_BUTTON, [this](auto) { Close(); });

            mContinueButton = s.AddButton(XXO("C&ontinue"));
            mContinueButton->Disable();
            mContinueButton->Bind(wxEVT_BUTTON, [this](auto) { OnContinue(); });
         }
         s.EndHorizontalLay();
      }
      s.EndInvisiblePanel();
   }
   s.EndVerticalLay();

   Layout();
   Fit();
   Centre();

   mToken->SetFocus();
}

LinkWithTokenDialog::~LinkWithTokenDialog()
{
   GetAuthorizationHandler().PopSuppressDialogs();
}

void LinkWithTokenDialog::OnContinue()
{
   mContinueButton->Disable();

   wxWeakRef<LinkWithTokenDialog> weakDialog(this);

   GetOAuthService().HandleLinkURI(
      audacity::ToUTF8(mToken->GetValue()), mAudiocomTrace,
      [weakDialog, trace = mAudiocomTrace](auto accessToken) {
         BasicUI::CallAfter(
            [weakDialog, token = std::string(accessToken), trace]() {
               if (!token.empty())
               {
                  if (weakDialog)
                  {
                     auto parent = weakDialog->GetParent();
                     weakDialog->Close();

                     LinkSucceededDialog successDialog { parent };
                     successDialog.ShowModal();
                  }

                  return;
               }

               LinkFailedDialog errorDialog(weakDialog, trace);

               if (wxID_RETRY != errorDialog.ShowModal())
               {
                  if (weakDialog)
                     weakDialog->Close();
               }
            });
      });
}

void LinkWithTokenDialog::OnTextChanged()
{
   mContinueButton->Enable(!mToken->GetValue().empty());
}

} // namespace audacity::cloud::audiocom

// Remaining code hooks this add-on into the application
#include "CommandContext.h"
#include "MenuRegistry.h"

namespace {
// Define our extra menu item
void OnLinkAccount(const CommandContext&)
{
   audacity::cloud::audiocom::LinkWithTokenDialog dialog {
      AudiocomTrace::LinkAudiocomAccountHelpMenu
   };
   dialog.ShowModal();
}

using namespace MenuRegistry;
AttachedItem sAttachment{
      Command(
         wxT("LinkAccount"), XXO("L&ink audio.com account..."),
         OnLinkAccount, AlwaysEnabledFlag),
   Placement{ wxT("Help/Extra"), { OrderingHint::Begin } }
};
}