Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "extensionscommandsstate.h"
24 :
25 : #include "global/async/async.h"
26 :
27 : #include "../extensionscommands.h"
28 :
29 : #include "log.h"
30 :
31 : using namespace muse;
32 : using namespace muse::rcommand;
33 : using namespace muse::extensions;
34 :
35 0 : std::string ExtensionsCommandsState::moduleName() const
36 : {
37 0 : return "extensions";
38 : }
39 :
40 0 : void ExtensionsCommandsState::init()
41 : {
42 0 : m_moduleRegister = commandsRegister()->moduleRegister(moduleName());
43 0 : IF_ASSERT_FAILED(m_moduleRegister) {
44 0 : return;
45 : }
46 :
47 0 : if (contextResolver()) {
48 0 : contextResolver()->contextChanged().onNotify(this, [this]() {
49 0 : updateCommandStates();
50 0 : });
51 : }
52 :
53 0 : m_moduleRegister->commandListChanged().onNotify(this, [this]() {
54 0 : updateCommandStates();
55 0 : });
56 :
57 0 : updateCommandStates();
58 : }
59 :
60 0 : void ExtensionsCommandsState::deinit()
61 : {
62 0 : }
63 :
64 0 : void ExtensionsCommandsState::updateCommandStates(const std::vector<Command>& commands)
65 : {
66 0 : IF_ASSERT_FAILED(m_moduleRegister) {
67 0 : return;
68 : }
69 :
70 0 : const auto& commandList = commands.empty() ? m_moduleRegister->commandList() : commands;
71 :
72 0 : for (const auto& command : commandList) {
73 0 : CommandState newState = commandState(command);
74 0 : if (m_commandStates[command] != newState) {
75 0 : m_commandStates[command] = newState;
76 0 : m_commandStateChanged.send(command, newState);
77 : }
78 : }
79 : }
80 :
81 0 : CommandState ExtensionsCommandsState::commandState(const Command& command) const
82 : {
83 0 : if (command == OPEN_APIDUMP_COMMAND) {
84 0 : return CommandState(true, true);
85 : }
86 :
87 0 : if (!contextResolver()) {
88 0 : return CommandState(true, false);
89 : }
90 :
91 0 : ExtensionUri extensionUri = extensionUriByCommand(command);
92 0 : const Manifest& manifest = extensionsRegister()->manifest(extensionUri);
93 0 : IF_ASSERT_FAILED(manifest.isValid()) {
94 0 : return CommandState(false, false);
95 : }
96 :
97 0 : if (!contextResolver()->isContextAllowed(manifest.context)) {
98 0 : return CommandState(false, false);
99 : }
100 :
101 0 : return CommandState(true, false);
102 0 : }
103 :
104 0 : async::Channel<Command, CommandState> ExtensionsCommandsState::commandStateChanged() const
105 : {
106 0 : return m_commandStateChanged;
107 : }
|