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) 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 : #pragma once
23 :
24 : #include "types/string.h"
25 : #include "types/ret.h"
26 : #include "io/path.h"
27 : #include "io/iodevice.h"
28 : #include "mscio.h"
29 :
30 : namespace muse {
31 : class ZipWriter;
32 : class TextStream;
33 : }
34 :
35 : namespace mu::engraving {
36 : class MscWriter
37 : {
38 : public:
39 :
40 : struct Params
41 : {
42 : muse::io::IODevice* device = nullptr;
43 : muse::io::path_t filePath;
44 : muse::String mainFileName;
45 : MscIoMode mode = MscIoMode::Zip;
46 : };
47 :
48 : MscWriter() = default;
49 : MscWriter(const Params& params);
50 : ~MscWriter();
51 :
52 : void setParams(const Params& params);
53 : const Params& params() const;
54 :
55 : muse::Ret open();
56 : void close();
57 : bool isOpened() const;
58 : bool hasError() const;
59 :
60 : void writeStyleFile(const muse::ByteArray& data);
61 : void writeScoreFile(const muse::ByteArray& data);
62 : void addExcerptStyleFile(const muse::String& excerptFileName, const muse::ByteArray& data);
63 : void addExcerptFile(const muse::String& excerptFileName, const muse::ByteArray& data);
64 : void writeChordListFile(const muse::ByteArray& data);
65 : void writeThumbnailFile(const muse::ByteArray& data);
66 : void addImageFile(const muse::String& fileName, const muse::ByteArray& data);
67 : void writeAudioSettingsJsonFile(const muse::ByteArray& data, const muse::io::path_t& pathPrefix = "");
68 : void writeViewSettingsJsonFile(const muse::ByteArray& data, const muse::io::path_t& pathPrefix = "");
69 : void writeAutomationJsonFile(const muse::ByteArray& data);
70 :
71 : private:
72 :
73 : struct IWriter {
74 4 : virtual ~IWriter() = default;
75 :
76 : virtual muse::Ret open(muse::io::IODevice* device, const muse::io::path_t& filePath) = 0;
77 : virtual void close() = 0;
78 : virtual bool isOpened() const = 0;
79 : virtual bool hasError() const = 0;
80 : virtual bool addFileData(const muse::String& fileName, const muse::ByteArray& data) = 0;
81 : };
82 :
83 : struct ZipFileWriter : public IWriter
84 : {
85 : ~ZipFileWriter() override;
86 : muse::Ret open(muse::io::IODevice* device, const muse::io::path_t& filePath) override;
87 : void close() override;
88 : bool isOpened() const override;
89 : bool hasError() const override;
90 : bool addFileData(const muse::String& fileName, const muse::ByteArray& data) override;
91 :
92 : private:
93 : muse::io::IODevice* m_device = nullptr;
94 : muse::ZipWriter* m_zip = nullptr;
95 : };
96 :
97 : struct DirWriter : public IWriter
98 : {
99 : muse::Ret open(muse::io::IODevice* device, const muse::io::path_t& filePath) override;
100 : void close() override;
101 : bool isOpened() const override;
102 : bool hasError() const override;
103 : bool addFileData(const muse::String& fileName, const muse::ByteArray& data) override;
104 : private:
105 : muse::io::path_t m_rootPath;
106 : bool m_hasError = false;
107 : };
108 :
109 : struct XmlFileWriter : public IWriter
110 : {
111 : ~XmlFileWriter() override;
112 : muse::Ret open(muse::io::IODevice* device, const muse::io::path_t& filePath) override;
113 : void close() override;
114 : bool isOpened() const override;
115 : bool hasError() const override;
116 : bool addFileData(const muse::String& fileName, const muse::ByteArray& data) override;
117 : private:
118 : muse::io::IODevice* m_device = nullptr;
119 : muse::TextStream* m_stream = nullptr;
120 : };
121 :
122 : struct Meta {
123 : std::vector<muse::String> files;
124 : bool isWritten = false;
125 :
126 : bool contains(const muse::String& file) const;
127 : void addFile(const muse::String& file);
128 : };
129 :
130 : IWriter* writer() const;
131 :
132 : bool addFileData(const muse::String& fileName, const muse::ByteArray& data);
133 :
134 : void writeMeta();
135 : void writeContainer(const std::vector<muse::String>& paths);
136 :
137 : muse::String mainFileName() const;
138 :
139 : Params m_params;
140 : mutable IWriter* m_writer = nullptr;
141 : Meta m_meta;
142 : bool m_hadError = false;
143 : };
144 : }
|