File: ProjectCloudUIExtension.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 (222 lines) | stat: -rw-r--r-- 6,405 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
/*  SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************

  Audacity: A Digital Audio Editor

  ProjectCloudUIExtension.cpp

  Dmitry Vedenko

**********************************************************************/
#include "ProjectCloudUIExtension.h"

#include <wx/log.h>

#include "sync/ProjectCloudExtension.h"
#include "sync/ResumedSnaphotUploadOperation.h"

#include "dialogs/ConnectionIssuesDialog.h"
#include "dialogs/NotCloudProjectDialog.h"
#include "dialogs/ProjectLimitDialog.h"
#include "dialogs/ProjectVersionConflictDialog.h"
#include "dialogs/SyncFailedDialog.h"
#include "dialogs/WaitForActionDialog.h"

#include "CloudProjectFileIOExtensions.h"
#include "CloudProjectOpenUtils.h"

#include "OAuthService.h"
#include "ServiceConfig.h"
#include "UserService.h"

#include "BasicUI.h"
#include "CodeConversions.h"
#include "Project.h"

namespace audacity::cloud::audiocom::sync
{
namespace
{
const AttachedProjectObjects::RegisteredFactory key {
   [](AudacityProject& project)
   { return std::make_shared<ProjectCloudUIExtension>(project); }
};

OnCloseHook::Scope onCloseHookScope { [](AudacityProject& project) {
   return ProjectCloudUIExtension::Get(project).AllowClosing();
} };
} // namespace

ProjectCloudUIExtension::ProjectCloudUIExtension(AudacityProject& project)
    : mProject { project }
    , mCloudStatusChangedSubscription {
       ProjectCloudExtension::Get(project).SubscribeStatusChanged(
          [this](const auto& status) { OnCloudStatusChanged(status); }, true)
    }
{
}

ProjectCloudUIExtension::~ProjectCloudUIExtension() = default;

ProjectCloudUIExtension& ProjectCloudUIExtension::Get(AudacityProject& project)
{
   return project.AttachedObjects::Get<ProjectCloudUIExtension&>(key);
}

const ProjectCloudUIExtension&
ProjectCloudUIExtension::Get(const AudacityProject& project)
{
   return Get(const_cast<AudacityProject&>(project));
}

void ProjectCloudUIExtension::SetUploadProgress(double progress)
{
   mProgress = progress;
}

bool ProjectCloudUIExtension::AllowClosing()
{
   while (mInSync.load(std::memory_order_acquire) && !mClosingCancelled)
   {
      if (mProgressDialog == nullptr)
      {
         mProgressDialog = BasicUI::MakeProgress(
            XO("Save to audio.com"),
            XO("Project is syncing with audio.com. Do you want to stop the sync process?"),
            BasicUI::ProgressShowCancel | BasicUI::ProgressShowStop);
      }

      const auto result = mProgressDialog->Poll(mProgress * 10000, 10000);

      if (result == BasicUI::ProgressResult::Cancelled)
      {
         mClosingCancelled = true;
         mProgressDialog.reset();
      }

      if (result == BasicUI::ProgressResult::Stopped)
         ProjectCloudExtension::Get(mProject).CancelSync();

      BasicUI::Yield();
   }

   bool closingCancelled = mClosingCancelled;
   mClosingCancelled     = false;

   mProgressDialog.reset();

   return !mInSync.load(std::memory_order_acquire) || !closingCancelled;
}

void ProjectCloudUIExtension::OnCloudStatusChanged(
   const CloudStatusChangedMessage& message)
{
   mInSync = message.IsSyncing();

   if (!mInSync)
      mProgressDialog.reset();
   else
      SetUploadProgress(message.Progress);

   // It the sync was successful - check for unsuccessful operations before
   if (message.Status == ProjectSyncStatus::Synced)
      ResumeProjectUpload(ProjectCloudExtension::Get(mProject), {});

   if (message.Status != ProjectSyncStatus::Failed || !message.Error)
      return;

   const auto error = *message.Error;

   switch (error.Type)
   {
   case CloudSyncError::Authorization:
      // How do we got here? Probable auth_token is invalid?
      GetOAuthService().UnlinkAccount(message.audiocomTrace);
      SaveToCloud(mProject, UploadMode::Normal);
      break;
   case CloudSyncError::ProjectLimitReached:
      [[fallthrough]];
   case CloudSyncError::ProjectStorageLimitReached:
   {
      auto result = ProjectLimitDialog { &mProject }.ShowDialog();

      if (result == ProjectLimitDialog::VisitAudioComIdentifier())
      {
         auto& userService = GetUserService();
         auto& oauthService = GetOAuthService();
         auto& serviceConfig = GetServiceConfig();

         const auto slug = audacity::ToUTF8(userService.GetUserSlug());
         const auto projectsPath = serviceConfig.GetProjectsPagePath(slug, message.audiocomTrace);
         const auto url = oauthService.MakeAudioComAuthorizeURL(slug, projectsPath);

         BasicUI::OpenInDefaultBrowser(url);

         WaitForActionDialog {
            &mProject,
            XO("Waiting for space to free up"),
            XO("Once you have made storage space available on audio.com, click Retry."),
         }
            .ShowDialog();
         SaveToCloud(mProject, UploadMode::Normal);
      }
      else if (result == ProjectLimitDialog::SaveLocallyButtonIdentifier())
      {
         if (!ResaveLocally(mProject))
            SaveToCloud(mProject, UploadMode::Normal);
      }
   }
   break;
   case CloudSyncError::ProjectVersionConflict:
   {
      if (
         ProjectVersionConflictDialog { &mProject,
                                        ProjectVersionConflictDialogMode::Save }
            .ShowDialog() == ProjectVersionConflictDialog::UseLocalIdentifier())
      {
         SaveToCloud(mProject, UploadMode::ForceOverwrite);
      }
      else
      {
         ReopenProject(mProject);
      }
   }
   break;
   case CloudSyncError::ProjectNotFound:
   {
      if (
         NotCloudProjectDialog { &mProject }.ShowDialog() ==
         NotCloudProjectDialog::SaveLocallyIdentifier())
      {
         if (!ResaveLocally(mProject))
            SaveToCloud(mProject, UploadMode::CreateNew);
      }
      else
      {
         SaveToCloud(mProject, UploadMode::CreateNew);
      }
   }
   break;
   case CloudSyncError::Network:
   {
      ConnectionIssuesDialog { &mProject }.ShowDialog();
   }
   break;
   case CloudSyncError::DataUploadFailed:
      [[fallthrough]];
   case CloudSyncError::Server:
      [[fallthrough]];
   case CloudSyncError::ClientFailure:
      SyncFailedDialog::OnSave(error);
      break;
   case CloudSyncError::Cancelled:
      [[fallthrough]];
   default:
      break;
   }

   wxLogError(
      "Cloud sync has failed: %s", audacity::ToWXString(error.ErrorMessage));
}

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