Loading...
Searching...
No Matches
ExternalFiles.hpp
1#pragma once
2#include <Process/MediaRange.hpp>
3
4#include <score/tools/ProjectFiles.hpp>
5
6#include <QString>
7
8#include <score_lib_process_export.h>
9
10#include <functional>
11#include <memory>
12#include <optional>
13#include <vector>
14
15namespace score
16{
17class Command;
18struct DocumentContext;
19}
20
21namespace Process
22{
23class ControlInlet;
24class ProcessModel;
25
27enum class FileUsage
28{
29 Input,
30 Output
31};
32
35{
38 QString path;
39 score::FileKind kind{score::FileKind::Unknown};
42 bool directory{};
45 bool rewritable{true};
47 QString owner;
48
56 std::optional<MediaRange> usedRange;
57
65 std::vector<QString> companions;
66};
67
70using CompanionFiles = std::function<std::vector<QString>(const QString& storedPath)>;
71
74using ExternalFileMapper = std::function<QString(const ExternalFileRef&)>;
75
92class SCORE_LIB_PROCESS_EXPORT ExternalFileMap
93{
94public:
95 explicit ExternalFileMap(ExternalFileMapper mapper) noexcept;
97
98 ExternalFileMap(const ExternalFileMap&) = delete;
99 ExternalFileMap& operator=(const ExternalFileMap&) = delete;
100
104 QString map(ExternalFileRef ref);
105
107 void control(
109 FileUsage usage = FileUsage::Input, const CompanionFiles& companions = {});
110
112 void folder(Process::ControlInlet& inlet);
113
115 template <typename T>
116 void property(
117 T& obj, const char* property, score::FileKind kind,
118 FileUsage usage = FileUsage::Input);
119
121 void readOnly(const QString& path, score::FileKind kind);
122
125 void addCommand(score::Command* cmd);
126
128 QString owner;
129
132 std::vector<score::Command*> takeCommands() noexcept;
133
134 bool hasCommands() const noexcept { return !m_commands.empty(); }
135
136private:
137 ExternalFileMapper m_mapper;
138 std::vector<score::Command*> m_commands;
139};
140
142SCORE_LIB_PROCESS_EXPORT
143void mapDocumentExternalFiles(const score::DocumentContext& ctx, ExternalFileMap& map);
144
147SCORE_LIB_PROCESS_EXPORT
149 const QString& value, const score::DocumentContext& ctx) noexcept;
150}
151
152#include <Process/Commands/RelocateFile.hpp>
153
154namespace Process
155{
156template <typename T>
158 T& obj, const char* property, score::FileKind kind, FileUsage usage)
159{
160 const QString cur = obj.property(property).toString().trimmed();
161 if(cur.isEmpty())
162 return;
163
164 const QString next
165 = map({.path = cur,
166 .kind = kind == score::FileKind::Unknown ? score::guessFileKind(cur) : kind,
167 .usage = usage,
168 .directory = false,
169 .rewritable = true,
170 .owner = owner});
171 if(next.isEmpty() || next == cur)
172 return;
173
174 addCommand(new Process::RelocateFilePath{obj, QString::fromUtf8(property), next});
175}
176}
Definition Port.hpp:242
Collects — and in the same pass rewrites — the files a document references.
Definition ExternalFiles.hpp:93
QString map(ExternalFileRef ref)
Definition ExternalFiles.cpp:40
void addCommand(score::Command *cmd)
Definition ExternalFiles.cpp:62
QString owner
Name attributed to the references reported next. Set by the traversal.
Definition ExternalFiles.hpp:128
void property(T &obj, const char *property, score::FileKind kind, FileUsage usage=FileUsage::Input)
A path held in a QString Q_PROPERTY of a model object.
Definition ExternalFiles.hpp:157
Points a QString path property at another file.
Definition RelocateFile.hpp:18
The Command class.
Definition Command.hpp:34
Base classes and tools to implement processes and layers.
Definition JSONVisitor.hpp:1115
void mapDocumentExternalFiles(const score::DocumentContext &ctx, ExternalFileMap &map)
Walk every process of the document and let it report its files.
Definition ExternalFiles.cpp:228
std::function< std::vector< QString >(const QString &storedPath)> CompanionFiles
Definition ExternalFiles.hpp:70
FileUsage
Whether the process reads the file or writes to it.
Definition ExternalFiles.hpp:28
@ Output
Written by the process (recorders, encoders): only the path moves.
@ Input
Read by the process: must be collected next to the document.
bool looksLikeExistingFile(const QString &value, const score::DocumentContext &ctx) noexcept
Definition ExternalFiles.cpp:170
std::function< QString(const ExternalFileRef &)> ExternalFileMapper
Definition ExternalFiles.hpp:74
Base toolkit upon which the software is built.
Definition Application.cpp:83
FileKind guessFileKind(const QString &path) noexcept
Definition ProjectFiles.cpp:59
FileKind
Definition ProjectFiles.hpp:17
One external file referenced by a document.
Definition ExternalFiles.hpp:35
std::optional< MediaRange > usedRange
Definition ExternalFiles.hpp:56
QString owner
Human-readable name of the object holding the reference.
Definition ExternalFiles.hpp:47
bool rewritable
Definition ExternalFiles.hpp:45
std::vector< QString > companions
Definition ExternalFiles.hpp:65
bool directory
The reference designates a folder, not a file.
Definition ExternalFiles.hpp:42
QString path
Definition ExternalFiles.hpp:38
Definition DocumentContext.hpp:18