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) 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 "converterapi.h"
23 :
24 : #include <QCoreApplication>
25 :
26 : #include "log.h"
27 :
28 : using namespace muse;
29 : using namespace mu::converter::api;
30 :
31 0 : ConverterApi::ConverterApi(muse::api::IApiEngine* e)
32 0 : : muse::api::ApiObject(e) {}
33 :
34 0 : QString ConverterApi::selectDir(const QString& title, const QString& dir) const
35 : {
36 0 : io::path_t path = interactive()->selectDirectory(title.toStdString(), dir);
37 0 : return path.toQString();
38 0 : }
39 :
40 0 : QStringList ConverterApi::scanDir(const QString& dir, const QStringList& filters) const
41 : {
42 0 : std::vector<std::string> fv;
43 0 : fv.reserve(filters.size());
44 0 : for (const QString& f : filters) {
45 0 : fv.push_back(f.toStdString());
46 : }
47 0 : RetVal<io::paths_t> rv = fileSystem()->scanFiles(dir, fv);
48 0 : if (!rv.ret) {
49 0 : LOGE() << rv.ret.toString();
50 0 : return QStringList();
51 : }
52 :
53 0 : QStringList files;
54 0 : files.reserve(rv.val.size());
55 0 : for (const io::path_t& p : rv.val) {
56 0 : files << p.toQString();
57 : }
58 0 : return files;
59 0 : }
60 :
61 0 : QString ConverterApi::basename(const QString& filePath) const
62 : {
63 0 : return io::basename(filePath).toQString();
64 : }
65 :
66 0 : bool ConverterApi::batch(const QString& outDir, const QString& job, const QString& uriQuery, QJSValue progressCallback)
67 : {
68 0 : io::path_t jobFile = outDir + "/job.json";
69 0 : QByteArray jobData = job.toUtf8();
70 0 : Ret ret = fileSystem()->writeFile(jobFile, ByteArray::fromQByteArray(jobData));
71 0 : if (!ret) {
72 0 : LOGE() << ret.toString();
73 0 : return false;
74 : }
75 :
76 0 : muse::ProgressPtr progress = std::make_shared<muse::Progress>();
77 0 : progress->progressChanged().onReceive(this, [progressCallback](int64_t current, int64_t total, const std::string& title) {
78 0 : QCoreApplication::processEvents();
79 0 : if (progressCallback.isCallable()) {
80 0 : QJSValueList args;
81 0 : args << int(current) << int(total) << QString::fromStdString(title);
82 0 : progressCallback.call(args);
83 0 : }
84 0 : QCoreApplication::processEvents();
85 0 : });
86 :
87 0 : QCoreApplication::processEvents();
88 :
89 0 : ret = converter()->batchConvert(jobFile, {}, String(), UriQuery(uriQuery.toStdString()), progress);
90 0 : if (!ret) {
91 0 : LOGE() << ret.toString();
92 0 : return false;
93 : }
94 :
95 0 : return true;
96 0 : }
|