-
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathProcessDropHandler.cpp
More file actions
218 lines (185 loc) · 5.65 KB
/
Copy pathProcessDropHandler.cpp
File metadata and controls
218 lines (185 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <Process/Drop/ProcessDropHandler.hpp>
#include <score/tools/File.hpp>
#include <ossia/detail/algorithms.hpp>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QSet>
#include <QUrl>
namespace Process
{
ProcessDropHandler::ProcessDropHandler() { }
ProcessDropHandler::~ProcessDropHandler() { }
void ProcessDropHandler::getCustomDrops(
std::vector<ProcessDropHandler::ProcessDrop>& drops, const QMimeData& mime,
const score::DocumentContext& ctx) const noexcept
{
// dropCustom is no longer noexcept (some overrides invoke parsers that
// can throw on malformed input — see ProcessDropHandler.hpp). Catch
// here so a throwing handler never escapes through the noexcept
// public API and tears down the editor.
try
{
dropCustom(drops, mime, ctx);
}
catch(const std::exception& e)
{
qWarning() << "ProcessDropHandler::dropCustom threw:" << e.what();
}
catch(...)
{
qWarning() << "ProcessDropHandler::dropCustom threw an unknown exception";
}
}
void ProcessDropHandler::getMimeDrops(
std::vector<ProcessDropHandler::ProcessDrop>& drops, const QMimeData& mime,
const QString& fmt, const score::DocumentContext& ctx) const noexcept
{
qDebug() << fmt << mime.data(fmt);
auto df = DroppedFile{{}, mime.data(fmt)};
dropData(drops, df, ctx);
}
void ProcessDropHandler::getFileDrops(
std::vector<ProcessDropHandler::ProcessDrop>& drops, const QMimeData& mime,
const score::FilePath& path, const score::DocumentContext& ctx) const noexcept
{
// Check for handling through paths
auto old_sz = drops.size();
dropPath(drops, path, ctx);
if(drops.size() != old_sz)
return;
// Fall back to manual handling
if(QFile file{path.absolute}; file.open(QIODevice::ReadOnly))
{
dropData(drops, {path, file.readAll()}, ctx);
}
}
QSet<QString> ProcessDropHandler::mimeTypes() const noexcept
{
return {};
}
QSet<QString> ProcessDropHandler::fileExtensions() const noexcept
{
return {};
}
void ProcessDropHandler::dropCustom(
std::vector<ProcessDropHandler::ProcessDrop>&, const QMimeData& data,
const score::DocumentContext& ctx) const
{
}
void ProcessDropHandler::dropPath(
std::vector<ProcessDropHandler::ProcessDrop>&, const score::FilePath& data,
const score::DocumentContext& ctx) const noexcept
{
}
void ProcessDropHandler::dropData(
std::vector<ProcessDropHandler::ProcessDrop>&, const DroppedFile& data,
const score::DocumentContext& ctx) const noexcept
{
}
ProcessDropHandlerList::~ProcessDropHandlerList() { }
std::vector<ProcessDropHandler::ProcessDrop> ProcessDropHandlerList::getDrop(
const QMimeData& mime, const score::DocumentContext& ctx) const noexcept
{
std::vector<ProcessDropHandler::ProcessDrop> res;
initCaches();
auto handleCustomDrop = [&](Process::ProcessDropHandler& handler) {
auto before = res.size();
handler.getCustomDrops(res, mime, ctx);
auto after = res.size();
return after != before;
};
// Look for drop handlers with the available MIME types
for(const auto& fmt : mime.formats())
{
if(auto it = m_perMimeTypes.find(fmt.toStdString()); it != m_perMimeTypes.end())
{
auto& handler = *it->second;
// First check if a custom drop is in order, which handles everything.
if(handleCustomDrop(handler))
return res;
// Then fall back to the normal mime data drop
handler.getMimeDrops(res, mime, fmt, ctx);
}
}
if(!res.empty())
{
// qDebug() << "handled through getMimeDrops";
return res;
}
// Look for drop handlers with the available file extensions
for(const auto& url : mime.urls())
{
auto path = url.toLocalFile();
QFileInfo f{path};
if(f.exists())
{
auto ext = f.suffix().toLower();
if(auto it = m_perFileExtension.find(ext.toStdString());
it != m_perFileExtension.end())
{
auto& handler = *it->second;
// First check if a custom drop is in order, which handles everything.
if(handleCustomDrop(handler))
{
// qDebug() << "handled through getCustomDrops";
return res;
}
// Then fall back to the normal mime data drop
score::FilePath p{
.absolute = path,
.relative = score::relativizeFilePath(path, ctx),
.filename = f.fileName(),
.basename = f.baseName()};
handler.getFileDrops(res, mime, p, ctx);
}
}
}
// TODO Fix Sound::DropHandler::drop so that we don't need to do that
// and remove the custom drop mechanism above (problem is that customData is empty)
/*
{
auto comp = [](auto& lhs, auto& rhs) {
return lhs.creation.customData < rhs.creation.customData;
};
ossia::remove_duplicates(res, comp);
}
if(!res.empty())
{
qDebug() << "handled through getFileDrops";
return res;
}
*/
return res;
}
std::optional<TimeVal> ProcessDropHandlerList::getMaxDuration(
const std::vector<ProcessDropHandler::ProcessDrop>& res)
{
using drop_t = Process::ProcessDropHandler::ProcessDrop;
SCORE_ASSERT(!res.empty());
auto max_t
= std::max_element(res.begin(), res.end(), [](const drop_t& l, const drop_t& r) {
return l.duration < r.duration;
});
return max_t->duration;
}
void ProcessDropHandlerList::initCaches() const
{
if(m_lastCacheSize != this->size())
{
m_perMimeTypes.clear();
m_perFileExtension.clear();
for(auto& handler : *this)
{
for(const auto& ext : handler.fileExtensions())
{
m_perFileExtension[ext.toLower().toStdString()] = &handler;
}
for(const auto& ext : handler.mimeTypes())
{
m_perMimeTypes[ext.toLower().toStdString()] = &handler;
}
}
}
}
}