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};
58
61using ExternalFileMapper = std::function<QString(const ExternalFileRef&)>;
62
79class SCORE_LIB_PROCESS_EXPORT ExternalFileMap
80{
81public:
82 explicit ExternalFileMap(ExternalFileMapper mapper) noexcept;
84
85 ExternalFileMap(const ExternalFileMap&) = delete;
86 ExternalFileMap& operator=(const ExternalFileMap&) = delete;
87
90 QString map(ExternalFileRef ref);
91
93 void control(
95 FileUsage usage = FileUsage::Input);
96
98 void folder(Process::ControlInlet& inlet);
99
101 template <typename T>
102 void property(
103 T& obj, const char* property, score::FileKind kind,
104 FileUsage usage = FileUsage::Input);
105
107 void readOnly(const QString& path, score::FileKind kind);
108
111 void addCommand(score::Command* cmd);
112
114 QString owner;
115
118 std::vector<score::Command*> takeCommands() noexcept;
119
120 bool hasCommands() const noexcept { return !m_commands.empty(); }
121
122private:
123 ExternalFileMapper m_mapper;
124 std::vector<score::Command*> m_commands;
125};
126
128SCORE_LIB_PROCESS_EXPORT
129void mapDocumentExternalFiles(const score::DocumentContext& ctx, ExternalFileMap& map);
130
133SCORE_LIB_PROCESS_EXPORT
135 const QString& value, const score::DocumentContext& ctx) noexcept;
136}
137
138#include <Process/Commands/RelocateFile.hpp>
139
140namespace Process
141{
142template <typename T>
144 T& obj, const char* property, score::FileKind kind, FileUsage usage)
145{
146 const QString cur = obj.property(property).toString().trimmed();
147 if(cur.isEmpty())
148 return;
149
150 const QString next
151 = map({.path = cur,
152 .kind = kind == score::FileKind::Unknown ? score::guessFileKind(cur) : kind,
153 .usage = usage,
154 .directory = false,
155 .rewritable = true,
156 .owner = owner});
157 if(next.isEmpty() || next == cur)
158 return;
159
160 addCommand(new Process::RelocateFilePath{obj, QString::fromUtf8(property), next});
161}
162}
Definition Port.hpp:206
Collects — and in the same pass rewrites — the files a document references.
Definition ExternalFiles.hpp:80
QString map(ExternalFileRef ref)
Definition ExternalFiles.cpp:40
void addCommand(score::Command *cmd)
Definition ExternalFiles.cpp:59
QString owner
Name attributed to the references reported next. Set by the traversal.
Definition ExternalFiles.hpp:114
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:143
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:221
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:163
std::function< QString(const ExternalFileRef &)> ExternalFileMapper
Definition ExternalFiles.hpp:61
Base toolkit upon which the software is built.
Definition Application.cpp:113
FileKind guessFileKind(const QString &path) noexcept
Definition ProjectFiles.cpp:30
FileKind
Definition ProjectFiles.hpp:15
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
bool directory
The reference designates a folder, not a file.
Definition ExternalFiles.hpp:42
QString path
Definition ExternalFiles.hpp:38
Definition DocumentContext.hpp:18