-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathpatternexpander.cpp
More file actions
328 lines (286 loc) · 9.95 KB
/
patternexpander.cpp
File metadata and controls
328 lines (286 loc) · 9.95 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* Flacon - audio File Encoder
* https://github.com/flacon/flacon
*
* Copyright: 2019
* Alexander Sokoloff <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "patternexpander.h"
#include "track.h"
#include "disc.h"
#include <QDir>
class Tokens : public QHash<QChar, QString>
{
public:
Tokens() :
QHash<QChar, QString>()
{
}
bool isEmptyForOptional(QChar key) const
{
// If album contains only one disc,
// discCount and discNum are optional
if (key == 'd' || key == 'D') {
return (value('D').toInt() <= 1);
}
return this->value(key).isEmpty();
}
};
/************************************************
*
************************************************/
PatternExpander::PatternExpander()
{
}
/**************************************
*
**************************************/
PatternExpander::PatternExpander(const AlbumTags &albumTags, const TrackTags &trackTags, const TrackTags &firstTrackTags) :
mAlbumTags(albumTags),
mTrackTags(trackTags),
mFirstTrackTags(firstTrackTags)
{
}
/************************************************
*
************************************************/
PatternExpander::PatternExpander(const Track &track) :
PatternExpander(track.disk()->toTags(), track.toTags(), track.disk()->tracks().first()->toTags())
{
}
/************************************************
************************************************/
static QString doExpandPattern(const QString &pattern, const Tokens &tokens, bool optional)
{
QString res;
bool perc = false;
bool hasVars = false;
bool isValid = true;
for (int i = 0; i < pattern.length(); ++i) {
QChar c = pattern.at(i);
// Sub pattern .................................
if (c == '{') {
int level = 0;
int start = i + 1;
QString s = "{";
for (int j = i; j < pattern.length(); ++j) {
c = pattern.at(j);
if (c == '{')
level++;
else if (c == '}')
level--;
if (level == 0) {
s = doExpandPattern(pattern.mid(start, j - start), tokens, true);
i = j;
break;
}
}
res += s;
}
// Sub pattern .................................
else {
if (perc) {
perc = false;
if (tokens.contains(c)) {
hasVars = true;
if (optional && tokens.isEmptyForOptional(c)) {
isValid = false;
}
else {
QString s = tokens.value(c);
res += s;
}
}
else {
if (c == '%')
res += "%";
else
res += QStringLiteral("%") + c;
}
}
else {
if (c == '%')
perc = true;
else
res += c;
}
}
}
if (perc)
res += "%";
if (optional) {
if (hasVars) {
if (!isValid)
return "";
}
else {
return "{" + res + "}";
}
}
return res;
}
/**************************************
*
**************************************/
QString PatternExpander::expand(const QString &pattern) const
{
int n = lastDirSeparattor(pattern);
if (n < 0) {
return expand(pattern, Mode::Track);
}
// If the disc is a collection, the files fall into different directories.
// So we use the tag Disc::Performer for expand the directory path.
QString dirs = expand(pattern.left(n), Mode::Album);
QString file = expand(pattern.mid(n), Mode::Track);
return dirs + file;
}
/**************************************
*
**************************************/
QString PatternExpander::expand(const QString &pattern, Mode mode) const
{
const TrackTags &trackTags = (mode == Mode::Album) ? mFirstTrackTags : mTrackTags;
Tokens tokens;
tokens.insert(QChar('N'), QStringLiteral("%1").arg(mAlbumTags.trackCount(), 2, 10, QChar('0')));
tokens.insert(QChar('n'), QStringLiteral("%1").arg(trackTags.trackNum(), 2, 10, QChar('0')));
tokens.insert(QChar('D'), QStringLiteral("%1").arg(mAlbumTags.discCount(), 2, 10, QChar('0')));
tokens.insert(QChar('d'), QStringLiteral("%1").arg(mAlbumTags.discNum(), 2, 10, QChar('0')));
tokens.insert(QChar('A'), safeString(mAlbumTags.album()));
tokens.insert(QChar('C'), safeString(mAlbumTags.catalog()));
tokens.insert(QChar('t'), safeString(trackTags.title()));
tokens.insert(QChar('g'), safeString(trackTags.genre()));
tokens.insert(QChar('a'), safeString(trackTags.performer()));
tokens.insert(QChar('y'), safeString(trackTags.date()));
return doExpandPattern(pattern, tokens, false);
}
QString PatternExpander::toolTip()
{
QString css = R"(
<style type="text/css">
.term {font-weight: bold;}
.def {
white-space: nowrap;
left-margin: 200px;
}
</style>)";
// clang-format off
QStringList items;
items << "%n" << tr("Track number", "Part of the tooltip for output pattern edit");
items << "%N" << tr("Total number of tracks", "Part of the tooltip for output pattern edit");
items << "%d" << tr("Disk number", "Part of the tooltip for output pattern edit");
items << "%D" << tr("Total number of disks", "Part of the tooltip for output pattern edit");
items << "%a" << tr("Artist", "Part of the tooltip for output pattern edit");
items << "%A" << tr("Album title", "Part of the tooltip for output pattern edit");
items << "%t" << tr("Track title", "Part of the tooltip for output pattern edit");
items << "%y" << tr("Year", "Part of the tooltip for output pattern edit");
items << "%g" << tr("Genre", "Part of the tooltip for output pattern edit");
items << "%C" << tr("Catalog number", "Part of the tooltip for output pattern edit");
// clang-format on
QString table;
table += "<table>";
for (int i = 0; i < items.count(); i += 4) {
table += "<tr>";
table += QString("<td class='term'>%1</td><td class='def'> - %2</td>").arg(items.at(i), items.at(i + 1));
if (i + 3 < items.count()) {
table += QString("<td> </td>");
table += QString("<td class='term'>%1</td><td class='def'> - %2</td>").arg(items.at(i + 2), items.at(i + 3));
}
table += "</tr>";
}
table += "</table>";
// clang-format off
return
css +
tr("Tokens start with %. You can use the following tokens:", "Part of the tooltip for output pattern edit") +
"<br>" +
table +
"<br><br>" +
tr("If you surround sections of text that contain a token with braces, these sections will be hidden if the token is empty.", "Part of the tooltip for output pattern edit");
// clang-format on
}
/************************************************
*
************************************************/
static QString safeFilePathLen(const QString &path)
{
QString file = path;
QString ext = QFileInfo(path).suffix();
if (!ext.isEmpty()) {
ext = "." + ext;
file.resize(file.length() - ext.length());
}
QStringList res;
for (QString f : file.split(QDir::separator())) {
while (f.toUtf8().length() > 250) {
f.resize(f.length() - 1);
}
res << f;
}
return res.join(QDir::separator()) + ext;
}
/************************************************
*
************************************************/
int PatternExpander::lastDirSeparattor(const QString &pattern)
{
int squareNum = 0;
for (int i = pattern.length() - 1; i >= 0; --i) {
QChar c = pattern.at(i);
if (c == QDir::separator() && squareNum == 0) {
return i;
}
if (c == '}') {
squareNum++;
}
if (c == '{') {
squareNum--;
}
}
return -1;
}
/************************************************
*
************************************************/
QString PatternExpander::resultFileName(const QString &aPattern, const Track *track, const QString &ext)
{
QString pattern = aPattern;
if (pattern.isEmpty()) {
pattern = QStringLiteral("%a/%y - %A/%n - %t");
}
PatternExpander expander(*track);
return safeFilePathLen(expander.expand(pattern) + "." + ext);
}
/************************************************
*
************************************************/
QString PatternExpander::example(const QString &pattern)
{
AlbumTags album;
album.setDiscNum(1);
album.setDiscCount(1);
album.setAlbum("Help");
album.setTrackCount(14);
TrackTags track;
track.setArtist("The Beatles");
track.setTrackNum(13);
track.setTitle("Yesterday");
track.setGenre("Pop");
track.setDate("1965");
PatternExpander expander(album, track, track);
return expander.expand(pattern);
}