Skip to content

Commit

Permalink
added getting sounds from website
Browse files Browse the repository at this point in the history
  • Loading branch information
Eism committed Nov 29, 2024
1 parent 6b067d2 commit 924abc5
Show file tree
Hide file tree
Showing 17 changed files with 557 additions and 134 deletions.
2 changes: 1 addition & 1 deletion src/framework/cloud/qml/Muse/Cloud/CloudScoresView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ ScoresView {
anchors.rightMargin: root.sideMargin

title: qsTrc("project", "Unable to load online scores")
body: qsTrc("project", "Please check your internet connection or try again later.")
body: qsTrc("global", "Please check your internet connection or try again later.")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/framework/learn/qml/Muse/Learn/internal/Playlist.qml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ FocusScope {

StyledTextLabel {
width: parent.width
text: qsTrc("learn", "Please check your internet connection or try again later.")
text: qsTrc("global", "Please check your internet connection or try again later.")
}
}
}
10 changes: 9 additions & 1 deletion src/musesounds/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# MuseScore
# Music Composition & Notation
#
# Copyright (C) 2021 MuseScore BVBA and others
# Copyright (C) 2024 MuseScore BVBA and others
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
Expand All @@ -26,6 +26,14 @@ set(MODULE_QML_IMPORT ${CMAKE_CURRENT_LIST_DIR}/qml)
set(MODULE_SRC
${CMAKE_CURRENT_LIST_DIR}/musesoundsmodule.cpp
${CMAKE_CURRENT_LIST_DIR}/musesoundsmodule.h
${CMAKE_CURRENT_LIST_DIR}/imusesoundsrepository.h
${CMAKE_CURRENT_LIST_DIR}/imusesoundsconfiguration.h
${CMAKE_CURRENT_LIST_DIR}/musesoundstypes.h

${CMAKE_CURRENT_LIST_DIR}/internal/musesoundsrepository.cpp
${CMAKE_CURRENT_LIST_DIR}/internal/musesoundsrepository.h
${CMAKE_CURRENT_LIST_DIR}/internal/musesoundsconfiguration.cpp
${CMAKE_CURRENT_LIST_DIR}/internal/musesoundsconfiguration.h

${CMAKE_CURRENT_LIST_DIR}/view/musesoundslistmodel.cpp
${CMAKE_CURRENT_LIST_DIR}/view/musesoundslistmodel.h
Expand Down
40 changes: 40 additions & 0 deletions src/musesounds/imusesoundsconfiguration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2024 MuseScore BVBA and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once

#include "modularity/imoduleinterface.h"

#include "network/networktypes.h"

namespace mu::musesounds {
class IMuseSoundsConfiguration : MODULE_EXPORT_INTERFACE
{
INTERFACE_ID(IMuseSoundsConfiguration)

public:
virtual ~IMuseSoundsConfiguration() = default;

virtual muse::network::RequestHeaders headers() const = 0;

virtual QUrl soundsUrl() const = 0;
};
}
40 changes: 40 additions & 0 deletions src/musesounds/imusesoundsrepository.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2024 MuseScore BVBA and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once

#include "modularity/imoduleinterface.h"

#include "async/notification.h"
#include "musesoundstypes.h"

namespace mu::musesounds {
class IMuseSoundsRepository : MODULE_EXPORT_INTERFACE
{
INTERFACE_ID(IMuseSoundsRepository)

public:
virtual ~IMuseSoundsRepository() = default;

virtual const MuseSoundCategoryInfoList& soundsCategoryList() const = 0;
virtual muse::async::Notification soundsCategoryListChanged() const = 0;
};
}
54 changes: 54 additions & 0 deletions src/musesounds/internal/musesoundsconfiguration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2024 MuseScore BVBA and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "musesoundsconfiguration.h"

#include "settings.h"

using namespace mu::musesounds;
using namespace muse;
using namespace muse::network;

static const std::string module_name("musesounds");
static const Settings::Key GET_SOUNDS_TESTING_MODE_KEY(module_name, "musesounds/getSoundsTestingMode");

void MuseSoundsConfiguration::init()
{
settings()->setDefaultValue(GET_SOUNDS_TESTING_MODE_KEY, Val(false));
}

RequestHeaders MuseSoundsConfiguration::headers() const
{
RequestHeaders headers;
headers.rawHeaders["Accept"] = "application/json";
return headers;
}

QUrl MuseSoundsConfiguration::soundsUrl() const
{
return !isTestingMode() ? QUrl("https://cosmos-customer-webservice.azurewebsites.net/graphql")
: QUrl("https://cosmos-customer-webservice-dev.azurewebsites.net/graphql");
}

bool MuseSoundsConfiguration::isTestingMode() const
{
return settings()->value(GET_SOUNDS_TESTING_MODE_KEY).toBool();
}
47 changes: 47 additions & 0 deletions src/musesounds/internal/musesoundsconfiguration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2024 MuseScore BVBA and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once

#include "modularity/ioc.h"
#include "iglobalconfiguration.h"

#include "imusesoundsconfiguration.h"

namespace mu::musesounds {
class MuseSoundsConfiguration : public IMuseSoundsConfiguration, public muse::Injectable
{
Inject<muse::IGlobalConfiguration> globalConfiguration = { this };

public:
MuseSoundsConfiguration(const muse::modularity::ContextPtr& iocCtx)
: Injectable(iocCtx) {}

void init();

muse::network::RequestHeaders headers() const override;

QUrl soundsUrl() const override;

private:
bool isTestingMode() const;
};
}
149 changes: 149 additions & 0 deletions src/musesounds/internal/musesoundsrepository.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2024 MuseScore BVBA and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "musesoundsrepository.h"

#include "serialization/json.h"

#include "global/concurrency/concurrent.h"

using namespace mu::musesounds;
using namespace muse;
using namespace muse::network;

void MuseSoundsRepository::init()
{
auto soundsCallBack = [this](const RetVal<MuseSoundCategoryInfoList>& result) {
if (!result.ret) {
LOGE() << result.ret.toString();
return;
}

LOGE() << result.ret.toString();

m_soundsCategories = result.val;
m_soundsCategoriesChanged.notify();
};

Concurrent::run(this, &MuseSoundsRepository::th_requestSounds, soundsRequestUrl(), soundsCallBack);
}

const MuseSoundCategoryInfoList& MuseSoundsRepository::soundsCategoryList() const
{
return m_soundsCategories;
}

async::Notification MuseSoundsRepository::soundsCategoryListChanged() const
{
return m_soundsCategoriesChanged;
}

QUrl MuseSoundsRepository::soundsRequestUrl() const
{
String locale = QLocale().name();

String query = String(
R"(
query MyQuery {
product_pages_configuration(version: "default") {
librariesPageSections {
... on ProductPageSectionRegular {
title(locale: {locale: "%1"})
productCards {
... on ProductCardRegular {
title(locale: {locale: "%1"})
coverImageUrl
description(locale: {locale: "%1"})
}
}
}
}
}
}
)").arg(locale);

StringList params = {
"query=" + query
};

QUrl url = configuration()->soundsUrl();
url.setQuery(params.join(u"&"));

return url;
}

void MuseSoundsRepository::th_requestSounds(const QUrl& soundsUrl, std::function<void(RetVal<MuseSoundCategoryInfoList>)> callBack) const
{
TRACEFUNC;

network::INetworkManagerPtr networkManager = networkManagerCreator()->makeNetworkManager();
RequestHeaders headers = configuration()->headers();

QBuffer soundsInfoData;
Ret soundsItemsRet = networkManager->get(soundsUrl, &soundsInfoData, headers);
if (!soundsItemsRet) {
callBack(soundsItemsRet);
return;
}

JsonDocument soundsInfoDoc = JsonDocument::fromJson(ByteArray::fromQByteArray(soundsInfoData.data()));

RetVal<MuseSoundCategoryInfoList> result;
result.ret = make_ret(Ret::Code::Ok);
result.val = parseSounds(soundsInfoDoc);

callBack(result);
}

MuseSoundCategoryInfoList MuseSoundsRepository::parseSounds(const JsonDocument& soundsDoc) const
{
MuseSoundCategoryInfoList result;

JsonObject obj = soundsDoc.rootObject();
JsonObject data = !obj.empty() ? obj.value("data").toObject() : JsonObject();
JsonObject productsSearch = !data.empty() ? data.value("products_search").toObject() : JsonObject();
JsonArray items = !productsSearch.empty() ? productsSearch.value("items").toArray() : JsonArray();

for (size_t i = 0; i < items.size(); i++) {
JsonObject itemObj = items.at(i).toObject();

MuseSoundCategoryInfo category;
category.title = itemObj.value("title").toString();

JsonArray soundsItems = itemObj.value("items").toArray();

for (size_t i = 0; i < soundsItems.size(); i++) {
JsonObject soundItemObj = soundsItems.at(i).toObject();

MuseSoundInfo sound;
sound.title = itemObj.value("title").toString();
sound.description = itemObj.value("description").toString();
sound.thumbnail = itemObj.value("coverImageUrl").toString();

category.sounds.emplace_back(sound);
}

result.emplace_back(category);
}

return result;
}
Loading

0 comments on commit 924abc5

Please sign in to comment.