Skip to content

Commit 0e6218a

Browse files
Use async NetworkManager in MuseSoundsRepository
1 parent be27754 commit 0e6218a

File tree

2 files changed

+42
-60
lines changed

2 files changed

+42
-60
lines changed

src/musesounds/internal/musesoundsrepository.cpp

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* MuseScore
66
* Music Composition & Notation
77
*
8-
* Copyright (C) 2024 MuseScore Limited and others
8+
* Copyright (C) 2025 MuseScore Limited and others
99
*
1010
* This program is free software: you can redistribute it and/or modify
1111
* it under the terms of the GNU General Public License version 3 as
@@ -24,8 +24,6 @@
2424

2525
#include "serialization/json.h"
2626

27-
#include "global/concurrency/concurrent.h"
28-
2927
using namespace mu::musesounds;
3028
using namespace muse;
3129
using namespace muse::network;
@@ -46,37 +44,7 @@ static muse::UriQuery correctThumbnailSize(const UriQuery& uri)
4644
return UriQuery(uriStr.toStdString());
4745
}
4846

49-
void MuseSoundsRepository::init()
50-
{
51-
auto soundsCallBack = [this](const RetVal<SoundCatalogueInfoList>& result) {
52-
if (!result.ret) {
53-
LOGE() << result.ret.toString();
54-
return;
55-
}
56-
57-
{
58-
std::lock_guard lock(m_mutex);
59-
m_soundsСatalogs = result.val;
60-
}
61-
62-
m_soundsСatalogsChanged.notify();
63-
};
64-
65-
Concurrent::run(this, &MuseSoundsRepository::th_requestSounds, configuration()->soundsUri(), soundsCallBack);
66-
}
67-
68-
const SoundCatalogueInfoList& MuseSoundsRepository::soundsCatalogueList() const
69-
{
70-
std::lock_guard lock(m_mutex);
71-
return m_soundsСatalogs;
72-
}
73-
74-
async::Notification MuseSoundsRepository::soundsCatalogueListChanged() const
75-
{
76-
return m_soundsСatalogsChanged;
77-
}
78-
79-
QByteArray MuseSoundsRepository::soundsRequestJson() const
47+
static QByteArray soundsRequestJson()
8048
{
8149
QLocale locale = QLocale();
8250
String localeStr = locale.bcp47Name() + "-" + QLocale::territoryToCode(locale.territory());
@@ -114,35 +82,51 @@ QByteArray MuseSoundsRepository::soundsRequestJson() const
11482
return JsonDocument(json).toJson(JsonDocument::Format::Compact).toQByteArray();
11583
}
11684

117-
void MuseSoundsRepository::th_requestSounds(const UriQuery& soundsUri, std::function<void(RetVal<SoundCatalogueInfoList>)> callBack) const
85+
void MuseSoundsRepository::init()
11886
{
11987
TRACEFUNC;
12088

121-
network::deprecated::INetworkManagerPtr networkManager = networkManagerCreator()->makeDeprecatedNetworkManager();
89+
QUrl url = QUrl(QString::fromStdString(configuration()->soundsUri().toString()));
90+
QByteArray jsonData = soundsRequestJson();
12291
RequestHeaders headers = configuration()->headers();
12392

124-
QByteArray jsonData = soundsRequestJson();
125-
QBuffer receivedData(&jsonData);
126-
OutgoingDevice device(&receivedData);
93+
auto device = std::make_shared<QBuffer>();
94+
device->setData(jsonData);
95+
auto receivedData = std::make_shared<QBuffer>();
12796

128-
Ret soundsItemsRet = networkManager->post(QUrl(QString::fromStdString(soundsUri.toString())), &device, &receivedData, headers);
129-
if (!soundsItemsRet) {
130-
callBack(soundsItemsRet);
131-
return;
132-
}
97+
m_networkManager = networkManagerCreator()->makeNetworkManager();
98+
m_networkManager->post(url, device, receivedData, headers)
99+
.onResolve(this, [this, receivedData](const muse::Ret& ret) {
100+
if (!ret) {
101+
LOGE() << ret.toString();
102+
return;
103+
}
133104

134-
RetVal<SoundCatalogueInfoList> result;
105+
RetVal<SoundCatalogueInfoList> result;
135106

136-
std::string err;
137-
JsonDocument soundsInfoDoc = JsonDocument::fromJson(ByteArray::fromQByteArray(receivedData.data()), &err);
138-
if (!err.empty()) {
139-
result.ret = make_ret(Ret::Code::InternalError, err);
140-
} else {
141-
result.ret = make_ok();
142-
result.val = parseSounds(soundsInfoDoc);
143-
}
107+
std::string err;
108+
JsonDocument soundsInfoDoc = JsonDocument::fromJson(ByteArray::fromQByteArray(receivedData->data()), &err);
109+
if (!err.empty()) {
110+
result.ret = make_ret(Ret::Code::InternalError, err);
111+
} else {
112+
result.ret = make_ok();
113+
result.val = parseSounds(soundsInfoDoc);
114+
}
115+
116+
m_soundsСatalogs = result.val;
117+
m_soundsСatalogsChanged.notify();
118+
m_networkManager = nullptr;
119+
});
120+
}
121+
122+
const SoundCatalogueInfoList& MuseSoundsRepository::soundsCatalogueList() const
123+
{
124+
return m_soundsСatalogs;
125+
}
144126

145-
callBack(result);
127+
async::Notification MuseSoundsRepository::soundsCatalogueListChanged() const
128+
{
129+
return m_soundsСatalogsChanged;
146130
}
147131

148132
SoundCatalogueInfoList MuseSoundsRepository::parseSounds(const JsonDocument& soundsDoc) const

src/musesounds/internal/musesoundsrepository.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* MuseScore
66
* Music Composition & Notation
77
*
8-
* Copyright (C) 2024 MuseScore Limited and others
8+
* Copyright (C) 2025 MuseScore Limited and others
99
*
1010
* This program is free software: you can redistribute it and/or modify
1111
* it under the terms of the GNU General Public License version 3 as
@@ -23,6 +23,7 @@
2323
#pragma once
2424

2525
#include "modularity/ioc.h"
26+
#include "async/asyncable.h"
2627
#include "network/inetworkmanagercreator.h"
2728
#include "imusesoundsconfiguration.h"
2829

@@ -33,7 +34,7 @@ class JsonDocument;
3334
}
3435

3536
namespace mu::musesounds {
36-
class MuseSoundsRepository : public IMuseSoundsRepository, public muse::Injectable
37+
class MuseSoundsRepository : public IMuseSoundsRepository, public muse::Injectable, public muse::async::Asyncable
3738
{
3839
muse::ThreadSafeInject<muse::network::INetworkManagerCreator> networkManagerCreator = { this };
3940
muse::ThreadSafeInject<IMuseSoundsConfiguration> configuration = { this };
@@ -48,13 +49,10 @@ class MuseSoundsRepository : public IMuseSoundsRepository, public muse::Injectab
4849
muse::async::Notification soundsCatalogueListChanged() const override;
4950

5051
private:
51-
QByteArray soundsRequestJson() const;
52-
void th_requestSounds(const muse::UriQuery& soundsUri, std::function<void(muse::RetVal<SoundCatalogueInfoList>)> callBack) const;
53-
5452
SoundCatalogueInfoList parseSounds(const muse::JsonDocument& soundsDoc) const;
5553

56-
mutable std::mutex m_mutex;
5754
SoundCatalogueInfoList m_soundsСatalogs;
5855
muse::async::Notification m_soundsСatalogsChanged;
56+
muse::network::INetworkManagerPtr m_networkManager;
5957
};
6058
}

0 commit comments

Comments
 (0)