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) 2024 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 "extapiv1.h"
23 :
24 : #include <qqml.h>
25 :
26 : #include "messagedialog.h"
27 : #include "filedialog.h"
28 : #include "qqmlsettings_p.h"
29 : #include "iapiv1object.h"
30 :
31 : #include "log.h"
32 :
33 : using namespace muse::extensions::apiv1;
34 :
35 0 : void ExtApiV1::registerQmlTypes()
36 : {
37 0 : qmlRegisterUncreatableMetaObject(StandardButton::staticMetaObject, "MuseScore", 3, 0, "StandardButton",
38 : "Not creatable as it is an enum type");
39 0 : qmlRegisterType<MessageDialog>("MuseScore", 3, 0, "MessageDialog");
40 0 : qmlRegisterType<QQmlSettings>("MuseScore", 3, 0, "Settings");
41 0 : qmlRegisterType<FileDialog>("MuseScore", 3, 0, "FileDialog");
42 0 : }
43 :
44 0 : ExtApiV1::ExtApiV1(muse::api::IApiEngine* engine, QObject* parent)
45 0 : : QObject(parent), Contextable(engine->iocContext()), m_engine(engine)
46 : {
47 0 : }
48 :
49 0 : ExtApiV1::~ExtApiV1()
50 : {
51 0 : for (auto& a : m_apis) {
52 0 : if (a.isNeedDelete) {
53 0 : delete a.obj;
54 : }
55 : }
56 0 : }
57 :
58 0 : void ExtApiV1::setup(QJSValue globalObj)
59 : {
60 0 : QJSValue engApiVal = engraving();
61 0 : if (engApiVal.isNull()) {
62 0 : LOGE() << "not found MuseApi.Engraving";
63 0 : return;
64 : }
65 :
66 0 : QObject* engObj = engApiVal.toQObject();
67 0 : if (!engObj) {
68 0 : LOGE() << "MuseApi.Engraving is not QObject";
69 0 : return;
70 : }
71 :
72 0 : IApiV1Object* engApiV1 = dynamic_cast<IApiV1Object*>(engObj);
73 0 : if (!engApiV1) {
74 0 : LOGE() << "MuseApi.Engraving is not IApiV1Object";
75 0 : return;
76 : }
77 :
78 0 : engApiV1->setup(globalObj);
79 0 : }
80 :
81 0 : QJSValue ExtApiV1::api(const std::string& name) const
82 : {
83 0 : if (!apiRegister()) {
84 0 : return QJSValue();
85 : }
86 :
87 0 : Api a = m_apis.value(name);
88 0 : if (!a.jsval.isUndefined()) {
89 0 : return a.jsval;
90 : }
91 0 : auto api = apiRegister()->createApi(name, m_engine);
92 0 : a.obj = api.first;
93 0 : a.isNeedDelete = api.second;
94 0 : if (!a.obj) {
95 0 : LOGW() << "Not allowed api: " << name;
96 0 : return QJSValue();
97 : }
98 :
99 0 : a.jsval = m_engine->newQObject(a.obj);
100 :
101 0 : m_apis[name] = a;
102 :
103 0 : return a.jsval;
104 0 : }
|