File: CloudLocationDialog.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 (293 lines) | stat: -rw-r--r-- 8,659 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*  SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************

  Audacity: A Digital Audio Editor

  CloudLocationDialog.cpp

  Dmitry Vedenko

**********************************************************************/

#include "CloudLocationDialog.h"

#include <wx/button.h>
#include <wx/checkbox.h>
#include <wx/sizer.h>
#include <wx/statbmp.h>
#include <wx/statline.h>
#include <wx/stattext.h>

#include "AccessibilityUtils.h"

#include "CloudModuleSettings.h"

#include "../images/CloudImages.hpp"

namespace audacity::cloud::audiocom::sync
{
namespace
{
enum class ChoiceMode
{
   Local,
   User
};
struct DialogDescription final
{
   TranslatableString DialogTitle;

   // This pointer to the image is initialized on module load, we have to use a
   // pointer pointer here
   wxBitmap** RemoteImage;
   TranslatableString RemoteTitle;
   TranslatableString RemoteDescription;
   TranslatableString RemoteButtonLabel;

   wxBitmap** LocalImage;
   TranslatableString LocalTitle;
   TranslatableString LocalDescription;
   TranslatableString LocalButtonTitle;

   TranslatableString DoNotShowLabel;

   EnumSetting<CloudLocationMode>& LocationMode;

   ChoiceMode RememberChoiceMode;
};

const DialogDescription SaveDialogDescription = {
   /* i18n-hint: A title that is shown on the first project save that allows the
      user to select Cloud or local save. */
   XO("How would you like to save?"),
   &bin2c_SaveRemote_png,
   XO("Save to the Cloud (free)"),
   XO("Your project is backed up privately on audio.com. You can access your work from any device and collaborate on your project with others. Cloud saving is free for a limited number of projects."),
   XXO("&Save to Cloud"),
   &bin2c_SaveLocally_png,
   XO("On your computer"),
   XO("Files are saved on your device.\nNote: To export MP3 and WAV files, use File > Export Audio instead."),
   XXO("Save to &computer"),
   XO("&Remember my choice and don't show again"),
   SaveLocationMode,
   ChoiceMode::User,
};

const DialogDescription ExportDialogDescription = {
   /* i18n-hint: A title that is shown on export that allows the user to select
      Cloud or local export. */
   XO("How would you like to export?"),
   &bin2c_ExportRemote_png,
   XO("Share to audio.com"),
   XO("Uploads an uncompressed audio file and generates a shareable link. This link allows others to download the file in either .wav or .mp3 format."),
   XXO("&Share to audio.com"),
   &bin2c_ExportLocally_png,
   XO("On your computer"),
   XO("Export MP3s, WAVs, FLACs and other formats to your computer."),
   XXO("Export to &computer"),
   XO("&Don't show again"),
   ExportLocationMode,
   ChoiceMode::Local,
};

constexpr auto leftPadding = 16;

auto GetWrapWidth()
{
   return bin2c_SaveLocally_png->GetWidth() - leftPadding * 2;
}

std::unique_ptr<wxBoxSizer> SetupVerticalSizer(
   wxStaticBitmap* image, wxStaticText* title, wxStaticText* description,
   wxButton* button)
{
   const auto leftPaddingFlags = wxSizerFlags {}.Border(wxLEFT, leftPadding);

   auto sizer = std::make_unique<wxBoxSizer>(wxVERTICAL);

   sizer->Add(image);
   sizer->AddSpacer(24);
   sizer->Add(title, leftPaddingFlags);
   sizer->AddSpacer(12);
   sizer->Add(description, leftPaddingFlags);
   sizer->AddSpacer(40);
   sizer->AddStretchSpacer(1);
   sizer->Add(button, leftPaddingFlags);

   return sizer;
}

wxButton* CreateButton(
   wxWindow* parent, const wxFont& font, const TranslatableString& label)
{
   const auto transalatedLabel = label.Translation();

   auto button = safenew wxButton(parent, wxID_ANY, transalatedLabel);

   button->SetFont(font);

   const auto textSize = button->GetTextExtent(transalatedLabel);

   button->SetMinSize({ textSize.x + 12 * 2, 32 });

   return button;
}

} // namespace

CloudLocationDialog::CloudLocationDialog(
   wxWindow* parent, LocationDialogType type)
    : wxDialogWrapper { parent,         wxID_ANY,
                        XO("Audacity"), wxDefaultPosition,
                        { 442, -1 },    wxDEFAULT_DIALOG_STYLE }
    , mType { type }
{
   auto& description = type == LocationDialogType::Save ?
                          SaveDialogDescription :
                          ExportDialogDescription;

   wxFont titleFont = GetFont();
   titleFont.SetWeight(wxFONTWEIGHT_BOLD);
   titleFont.SetPixelSize({ 0, 18 });

   wxFont descriptionFont = GetFont();
   descriptionFont.SetPixelSize({ 0, 14 });

   auto title = safenew wxStaticText(
      this, wxID_ANY, description.DialogTitle.Translation());
   title->SetFont(titleFont);

   auto saveToCloudImage =
      safenew wxStaticBitmap(this, wxID_ANY, **description.RemoteImage);

   auto saveToCloudTitle = safenew wxStaticText(
      this, wxID_ANY, description.RemoteTitle.Translation());
   saveToCloudTitle->SetFont(titleFont);

   auto saveToCloudDescription = safenew wxStaticText(
      this, wxID_ANY, description.RemoteDescription.Translation());
   saveToCloudDescription->SetFont(descriptionFont);
   saveToCloudDescription->Wrap(GetWrapWidth());

   auto saveToCloudButton =
      CreateButton(this, descriptionFont, description.RemoteButtonLabel);

   auto saveToComputerImage =
      safenew wxStaticBitmap(this, wxID_ANY, **description.LocalImage);

   auto saveToComputerTitle = safenew wxStaticText(
      this, wxID_ANY, description.LocalTitle.Translation());
   saveToComputerTitle->SetFont(titleFont);

   auto saveToComputerDescription = safenew wxStaticText(
      this, wxID_ANY, description.LocalDescription.Translation());
   saveToComputerDescription->SetFont(descriptionFont);
   saveToComputerDescription->Wrap(GetWrapWidth());

   auto saveToComputerButton =
      CreateButton(this, descriptionFont, description.LocalButtonTitle);

   auto rememberChoiceCheckbox = safenew wxCheckBox(
      this, wxID_ANY,
      description.DoNotShowLabel.Translation());

   mDoNotShow = description.LocationMode.ReadEnum() != CloudLocationMode::Ask;
   rememberChoiceCheckbox->SetValue(mDoNotShow);

   auto sizer = safenew wxBoxSizer(wxVERTICAL);

   sizer->Add(title, wxSizerFlags {}.CenterHorizontal().Border(wxTOP, 16));

   auto topSizer = safenew wxBoxSizer(wxHORIZONTAL);

   auto cloudSizer = SetupVerticalSizer(
      saveToCloudImage, saveToCloudTitle, saveToCloudDescription,
      saveToCloudButton);

   topSizer->Add(cloudSizer.release(), wxSizerFlags {}.Expand());

   auto computerSizer = SetupVerticalSizer(
      saveToComputerImage, saveToComputerTitle, saveToComputerDescription,
      saveToComputerButton);

   topSizer->Add(
      computerSizer.release(), wxSizerFlags {}.Expand().Border(wxLEFT, 8));

   sizer->Add(topSizer, wxSizerFlags {}.Expand().Border(wxALL, 16));

   sizer->Add(safenew wxStaticLine(this), wxSizerFlags {}.Expand());

   sizer->Add(rememberChoiceCheckbox, wxSizerFlags {}.Border(wxALL, 16));

   SetSizerAndFit(sizer);
   SetupAccessibility(this);

   Center();

   Bind(
      wxEVT_CHAR_HOOK,
      [this](auto& evt)
      {
         if (!IsEscapeKey(evt))
         {
            evt.Skip();
            return;
         }

         EndModal(wxID_CANCEL);
      });

   rememberChoiceCheckbox->Bind(
      wxEVT_CHECKBOX, [this, rememberChoiceCheckbox](auto)
      { mDoNotShow = rememberChoiceCheckbox->GetValue(); });

   saveToCloudButton->Bind(wxEVT_BUTTON, [this](auto) { EndModal(wxID_SAVE); });
   saveToComputerButton->Bind(
      wxEVT_BUTTON, [this](auto) { EndModal(wxID_OK); });
}

CloudLocationDialog::~CloudLocationDialog()
{
}

LocationDialogResult CloudLocationDialog::ShowDialog()
{
   auto& description = mType == LocationDialogType::Save ?
                          SaveDialogDescription :
                          ExportDialogDescription;

   if (description.LocationMode.ReadEnum() != CloudLocationMode::Ask)
      return description.LocationMode.ReadEnum() == CloudLocationMode::Cloud ?
                LocationDialogResult::Cloud :
                LocationDialogResult::Local;

   const auto result = ShowModal();

   if (result == wxID_OK)
   {
      if (mDoNotShow)
      {
         description.LocationMode.WriteEnum(CloudLocationMode::Local);
         gPrefs->Flush();
      }
      return LocationDialogResult::Local;
   }

   if (result == wxID_SAVE)
   {
      if (mDoNotShow)
      {
         description.LocationMode.WriteEnum(
            description.RememberChoiceMode == ChoiceMode::User ?
               CloudLocationMode::Cloud :
               CloudLocationMode::Local);
         gPrefs->Flush();
      }

      return LocationDialogResult::Cloud;
   }

   return LocationDialogResult::Cancel;
}

} // namespace audacity::cloud::audiocom::sync