Audacity 3.2.0
CloudProjectMixdownUtils.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 CloudProjectMixdownUtils.cpp
7
8 Dmitry Vedenko
9
10**********************************************************************/
12
13#include "BasicUI.h"
16#include "Project.h"
17#include "ProjectWindow.h"
18#include "ServiceConfig.h"
19#include "UriParser.h"
20
21#include "sync/CloudSyncDTO.h"
24
26{
27
28bool HandleMixdownLink(std::string_view uri)
29{
31
32 const auto parsedUri = ParseUri(uri);
33
34 if (parsedUri.Scheme != "audacity" || parsedUri.Host != "generate-audio")
35 return false;
36
37 const auto queryParameters = ParseUriQuery(parsedUri.Query);
38
39 if (queryParameters.empty())
40 return false;
41
42 const auto projectId = queryParameters.find("projectId");
43
44 if (projectId == queryParameters.end())
45 return false;
46
47 auto openedProject = GetOpenedProject(projectId->second);
48
49 const auto hasOpenProject = openedProject != nullptr;
50
51 const auto project = hasOpenProject ?
52 openedProject :
54 GetPotentialTarget(), projectId->second,
55 std::string_view {}, false);
56
57 if (project == nullptr)
58 return false;
59
61 *project,
62 [hasOpenProject](AudacityProject& project, MixdownState state)
63 {
64 if (!hasOpenProject)
65 ProjectWindow::Get(project).Close(true);
66 });
67
68 return true;
69}
70
73 std::function<void(AudacityProject&, MixdownState)> onComplete)
74{
77 [&project, onComplete = std::move(onComplete)](const auto& response)
78 {
79 auto cancellationContext = concurrency::CancellationContext::Create();
80
81 auto progressDialog = BasicUI::MakeProgress(
82 XO("Save to audio.com"), XO("Generating audio preview..."),
84
85 auto mixdownUploader = MixdownUploader::Upload(
86 cancellationContext, GetServiceConfig(), project,
87 [progressDialog = progressDialog.get(),
88 cancellationContext](auto progress)
89 {
90 if (
91 progressDialog->Poll(
92 static_cast<unsigned>(progress * 10000), 10000) !=
93 BasicUI::ProgressResult::Success)
94 cancellationContext->Cancel();
95 });
96
97 mixdownUploader->SetUrls(response.SyncState.MixdownUrls);
98
100 [&project,
101 progressDialog = std::shared_ptr { std::move(progressDialog) },
102 mixdownUploader, cancellationContext, onComplete]() mutable
103 {
104 auto& projectCloudExtension =
106
107 auto subscription = projectCloudExtension.SubscribeStatusChanged(
108 [progressDialog = progressDialog.get(), mixdownUploader,
109 cancellationContext](
110 const CloudStatusChangedMessage& message)
111 {
112 if (message.Status != ProjectSyncStatus::Failed)
113 return;
114
115 cancellationContext->Cancel();
116 },
117 true);
118
119 auto future = mixdownUploader->GetResultFuture();
120
121 while (future.wait_for(std::chrono::milliseconds(50)) !=
122 std::future_status::ready)
124
125 auto result = future.get();
126
127 progressDialog.reset();
128
129 if (onComplete)
130 onComplete(project, result.State);
131 });
132 });
133}
134
135} // namespace audacity::cloud::audiocom::sync
Toolkit-neutral facade for basic user interface services.
#define ASSERT_MAIN_THREAD()
Definition: BasicUI.h:414
XO("Cut/Copy/Paste")
const auto project
QueryFields ParseUriQuery(std::string_view query, std::string_view delimiter) noexcept
Parses URI query and returns QueryFields structure with parsed fields.
Definition: UriParser.cpp:59
UriFields ParseUri(std::string_view uri) noexcept
Definition: UriParser.cpp:9
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
static ProjectWindow & Get(AudacityProject &project)
static std::shared_ptr< MixdownUploader > Upload(concurrency::CancellationContextPtr cancellationContext, const ServiceConfig &config, const AudacityProject &project, MixdownProgressCallback progressCallback)
static ProjectCloudExtension & Get(AudacityProject &project)
@ ProgressShowCancel
Definition: BasicUI.h:142
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
Definition: BasicUI.cpp:214
void Yield()
Dispatch waiting events, including actions enqueued by CallAfter.
Definition: BasicUI.cpp:225
std::unique_ptr< ProgressDialog > MakeProgress(const TranslatableString &title, const TranslatableString &message, unsigned flags=(ProgressShowStop|ProgressShowCancel), const TranslatableString &remainingLabelText={})
Create and display a progress dialog.
Definition: BasicUI.h:302
void UploadMixdown(AudacityProject &project, std::function< void(AudacityProject &, MixdownState)> onComplete)
void SaveToCloud(AudacityProject &project, UploadMode mode, CreateSnapshotCallback snapshotCallback)
AudacityProject * OpenProjectFromCloud(AudacityProject *potentialTarget, std::string_view projectId, std::string_view snapshotId, CloudSyncService::SyncMode mode)
bool HandleMixdownLink(std::string_view uri)
AudacityProject * GetOpenedProject(std::string_view projectId)
const ServiceConfig & GetServiceConfig()
Returns the instance of the ServiceConfig.