tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_SourceFileReference.cpp
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11namespace tracktion { inline namespace engine
12{
13
14SourceFileReference::SourceFileReference (Edit& e, juce::ValueTree& v, const juce::Identifier& prop)
15 : edit (e), source (v, prop, &e.getUndoManager()), state (v)
16{
17 ignoreUnused (state);
18}
19
20SourceFileReference::~SourceFileReference()
21{
22}
23
24juce::String SourceFileReference::findPathFromFile (Edit& edit, const juce::File& newFile, bool useRelativePath)
25{
26 if (useRelativePath)
27 {
28 jassert (edit.editFileRetriever && edit.editFileRetriever().existsAsFile());
29 jassert (edit.filePathResolver);
30
31 if (edit.editFileRetriever && edit.filePathResolver)
32 return newFile.getRelativePathFrom (edit.editFileRetriever());
33
34 return newFile.getRelativePathFrom (getEditFileFromProjectManager (edit));
35 }
36
37 return newFile.getFullPathName();
38}
39
40juce::File SourceFileReference::findFileFromString (Edit& edit, const juce::String& sourceDescription)
41{
42 if (sourceDescription.isEmpty())
43 return {};
44
45 ProjectItemID pid (sourceDescription);
46
47 if (pid.isValid())
48 {
49 if (auto projectItem = edit.engine.getProjectManager().getProjectItem (pid))
50 return projectItem->getSourceFile();
51
52 return {};
53 }
54
55 if (edit.filePathResolver)
56 return edit.filePathResolver (sourceDescription);
57
58 return getEditFileFromProjectManager (edit).getChildFile (sourceDescription);
59}
60
61juce::File SourceFileReference::getFile() const
62{
63 jassert (source.get() == state[source.getPropertyID()].toString());
64 return findFileFromString (edit, source.get());
65}
66
67bool SourceFileReference::isUsingProjectReference() const
68{
69 return getSourceProjectItemID().isValid();
70}
71
72ProjectItemID SourceFileReference::getSourceProjectItemID() const
73{
74 jassert (source.get() == state[source.getPropertyID()].toString());
75 return ProjectItemID (source.get());
76}
77
78ProjectItem::Ptr SourceFileReference::getSourceProjectItem() const
79{
80 jassert (source.get() == state[source.getPropertyID()].toString());
81 ProjectItemID pid (source.get());
82
83 if (pid.isValid())
84 return edit.engine.getProjectManager().getProjectItem (pid);
85
86 return {};
87}
88
89void SourceFileReference::setToDirectFileReference (const juce::File& newFile, bool useRelativePath)
90{
91 source = findPathFromFile (edit, newFile, useRelativePath);
92}
93
94void SourceFileReference::setToProjectFileReference (const juce::File& file, bool updateProjectItem)
95{
96 auto oldFile = getFile();
97 auto project = getProjectForEdit (edit);
98
99 if (updateProjectItem)
100 {
101 if (auto projectItem = getSourceProjectItem())
102 {
103 // if we've got a proper source ProjectItem but its file is missing, reassign the ProjectItem..
104 if (! projectItem->getSourceFile().existsAsFile())
105 {
106 projectItem->setSourceFile (file);
107 }
108 else if (project != nullptr)
109 {
110 // see if there's another one that has this new file..
111 if (auto existingItem = project->getProjectItemForFile (file))
112 {
113 // point at the existing ProjectItem for this file
114 setToProjectFileReference (existingItem->getID());
115 }
116 else
117 {
118 // no such object in the project, so create one..
119 projectItem = project->createNewItem (file, ProjectItem::waveItemType(),
120 file.getFileNameWithoutExtension(),
121 {}, ProjectItem::Category::imported,
122 false);
123
124 if (projectItem != nullptr)
125 setToProjectFileReference (projectItem->getID());
126 }
127 }
128 }
129 else if (project != nullptr)
130 {
131 // if we haven't got a legit ProjectItem, create one..
132 projectItem = project->createNewItem (file, ProjectItem::waveItemType(),
133 file.getFileNameWithoutExtension(),
134 {},
135 ProjectItem::Category::imported,
136 false);
137
138 if (projectItem != nullptr)
139 setToProjectFileReference (projectItem->getID());
140 }
141 }
142 else
143 {
144 if (project != nullptr)
145 if (auto existingProjectItem = project->getProjectItemForFile (file))
146 setToProjectFileReference (existingProjectItem->getID());
147 }
148
149 if (getFile() != oldFile)
150 edit.restartPlayback();
151}
152
153void SourceFileReference::setToProjectFileReference (ProjectItemID newID)
154{
155 auto oldFile = getFile();
156 source = newID.toString();
157
158 if (getFile() != oldFile)
159 edit.restartPlayback();
160}
161
162}} // namespace tracktion { inline namespace engine
const String & getFullPathName() const noexcept
File getChildFile(StringRef relativeOrAbsolutePath) const
String getRelativePathFrom(const File &directoryToBeRelativeTo) const
const String & toString() const noexcept
bool isEmpty() const noexcept
An ID representing one of the items in a Project.
#define jassert(expression)
void ignoreUnused(Types &&...) noexcept
juce::File getEditFileFromProjectManager(Edit &edit)
Uses the ProjectManager to look up the file for an Edit.
Project::Ptr getProjectForEdit(const Edit &e)
Tries to find the project that contains this edit (but may return nullptr!)