Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 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 : #include "extensionsactioncontroller.h"
23 :
24 : #include "translation.h"
25 : #include "types/ret.h"
26 :
27 : #include "../extensionscommands.h"
28 :
29 : #include "log.h"
30 :
31 : using namespace muse::extensions;
32 :
33 : static const muse::UriQuery SHOW_APIDUMP_URI("muse://extensions/apidump?modal=false&floating=true");
34 :
35 0 : void ExtensionsActionController::init()
36 : {
37 0 : provider()->manifestListChanged().onNotify(this, [this](){
38 0 : registerExtensions();
39 0 : });
40 :
41 0 : registerExtensions();
42 0 : }
43 :
44 0 : void ExtensionsActionController::registerExtensions()
45 : {
46 0 : commandDispatcher()->unreg(this);
47 :
48 0 : for (const Manifest& m : provider()->manifestList()) {
49 0 : for (const Action& a : m.actions) {
50 0 : ExtensionUri uri = m.uri;
51 0 : ExtensionActionCode actionCode = a.code;
52 0 : commandDispatcher()->onRequest(this, makeCommand(uri, actionCode), [this, uri, actionCode]() {
53 0 : return onExtensionTriggered(uri, actionCode);
54 : });
55 0 : }
56 0 : }
57 :
58 0 : commandDispatcher()->onRequest(this, OPEN_APIDUMP_COMMAND, [this]() { openUri(SHOW_APIDUMP_URI); return muse::make_ok(); });
59 0 : }
60 :
61 0 : muse::Ret ExtensionsActionController::onExtensionTriggered(const ExtensionUri& uri, const ExtensionActionCode& actionCode)
62 : {
63 0 : if (provider()->isEnabled(uri)) {
64 0 : return provider()->perform(uri, actionCode);
65 : }
66 :
67 0 : const Manifest& m = provider()->manifest(uri);
68 0 : IF_ASSERT_FAILED(m.isValid()) {
69 0 : LOGE() << "Not found extension, uri: " << uri.toString();
70 0 : return muse::make_ret(Ret::Code::BadArgs);
71 : }
72 :
73 0 : auto promise = interactive()->warning(
74 0 : muse::qtrc("extensions", "The plugin “%1” is currently disabled. Do you want to enable it now?").arg(m.title).toStdString(),
75 0 : muse::trc("extensions", "Alternatively, you can enable it at any time from Home > Plugins."),
76 0 : { IInteractive::Button::No, IInteractive::Button::Yes });
77 :
78 0 : promise.onResolve(this, [this, uri, actionCode](const IInteractive::Result& res) {
79 0 : if (res.isButton(IInteractive::Button::Yes)) {
80 0 : provider()->setEnabled(uri, true);
81 0 : provider()->perform(uri, actionCode);
82 : }
83 0 : });
84 :
85 0 : return muse::make_ok();
86 0 : }
87 :
88 0 : void ExtensionsActionController::openUri(const UriQuery& uri, bool isSingle)
89 : {
90 0 : if (isSingle && interactive()->isOpened(uri.uri()).val) {
91 0 : return;
92 : }
93 :
94 0 : interactive()->open(uri);
95 : }
|