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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
/*
* Cantata
*
* Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "deviceoptions.h"
#include "mpd-interface/song.h"
#include "support/configuration.h"
#include <QDir>
#include <QRegularExpression>
#include <QUrl>
#ifndef _MSC_VER
#include <unistd.h>
#endif
static QString cleanPath(const QString& path)
{
/* Unicode uses combining characters to form accented versions of other characters.
* (Exception: Latin-1 table for compatibility with ASCII.)
* Those can be found in the Unicode tables listed at:
* http://en.wikipedia.org/w/index.php?title=Combining_character&oldid=255990982
* Removing those characters removes accents. :) */
QString result = path;
// German umlauts
result.replace(QChar(0x00e4), "ae").replace(QChar(0x00c4), "Ae");
result.replace(QChar(0x00f6), "oe").replace(QChar(0x00d6), "Oe");
result.replace(QChar(0x00fc), "ue").replace(QChar(0x00dc), "Ue");
result.replace(QChar(0x00df), "ss");
// other special cases
result.replace(QChar(0x00C6), "AE");
result.replace(QChar(0x00E6), "ae");
result.replace(QChar(0x00D8), "OE");
result.replace(QChar(0x00F8), "oe");
// normalize in a form where accents are separate characters
result = result.normalized(QString::NormalizationForm_D);
// remove accents from table "Combining Diacritical Marks"
for (int i = 0x0300; i <= 0x036F; i++) {
result.remove(QChar(i));
}
return result;
}
static QString asciiPath(const QString& path)
{
QString result = path;
for (int i = 0; i < result.length(); i++) {
QChar c = result[i];
if (c > QChar(0x7f) || QChar(0) == c) {
c = '_';
}
result[i] = c;
}
return result;
}
QString vfatPath(const QString& path)
{
QString s = path;
if ('/' == QDir::separator()) {// we are on *nix, \ is a valid character in file or directory names, NOT the dir separator
s.replace('\\', '_');
}
else {
s.replace('/', '_');// on windows we have to replace / instead
}
for (int i = 0; i < s.length(); i++) {
QChar c = s[i];
if (c < QChar(0x20) || c == QChar(0x7F)// 0x7F = 127 = DEL control character
|| '*' == c || '?' == c || '<' == c || '>' == c || '|' == c || '"' == c || ':' == c) {
c = '_';
}
else if ('[' == c) {
c = '(';
}
else if (']' == c) {
c = ')';
}
s[i] = c;
}
/* beware of reserved device names */
uint len = s.length();
if (3 == len || (len > 3 && '.' == s[3])) {
QString l = s.left(3).toLower();
if ("aux" == l || "con" == l || "nul" == l || "prn" == l) {
s = '_' + s;
}
}
else if (4 == len || (len > 4 && '.' == s[4])) {
QString l = s.left(3).toLower();
QString d = s.mid(3, 1);
if (("com" == l || "lpt" == l) && ("0" == d || "1" == d || "2" == d || "3" == d || "4" == d || "5" == d || "6" == d || "7" == d || "8" == d || "9" == d)) {
s = '_' + s;
}
}
// "clock$" is only allowed WITH extension, according to:
// http://en.wikipedia.org/w/index.php?title=Filename&oldid=303934888#Comparison_of_file_name_limitations
if (0 == QString::compare(s, "clock$", Qt::CaseInsensitive)) {
s = '_' + s;
}
/* max path length of Windows API */
s = s.left(255);
/* whitespace at the end of folder/file names or extensions are bad */
len = s.length();
if (len > 0 && ' ' == s[len - 1]) {
s[len - 1] = '_';
}
int extensionIndex = s.lastIndexOf('.');// correct trailing spaces in file name itself
if (s.length() > 1 && extensionIndex > 0 && ' ' == s.at(extensionIndex - 1)) {
s[extensionIndex - 1] = '_';
}
for (int i = 1; i < s.length(); i++) {// correct trailing whitespace in folder names
if ((s.at(i) == QDir::separator()) && ' ' == s.at(i - 1)) {
s[i - 1] = '_';
}
}
return s;
}
static void manipulateThe(QString& str, bool reverse)
{
if (reverse) {
if (!str.startsWith("the ", Qt::CaseInsensitive)) {
return;
}
QString begin = str.left(3);
str = str.append(", %1").arg(begin);
str = str.mid(4);
return;
}
if (!str.endsWith(", the", Qt::CaseInsensitive)) {
return;
}
QString end = str.right(3);
str = str.prepend("%1 ").arg(end);
str.truncate(str.length() - end.length() - 2);
}
const QLatin1String DeviceOptions::constAlbumArtist("%albumartist%");
const QLatin1String DeviceOptions::constComposer("%composer%");
const QLatin1String DeviceOptions::constAlbumTitle("%album%");
const QLatin1String DeviceOptions::constTrackArtist("%artist%");
const QLatin1String DeviceOptions::constTrackTitle("%title%");
const QLatin1String DeviceOptions::constTrackArtistAndTitle("%artistandtitle%");
const QLatin1String DeviceOptions::constTrackNumber("%track%");
const QLatin1String DeviceOptions::constCdNumber("%discnumber%");
const QLatin1String DeviceOptions::constGenre("%genre%");
const QLatin1String DeviceOptions::constYear("%year%");
#ifdef ENABLE_DEVICES_SUPPORT
DeviceOptions::DeviceOptions(const QString& cvrName)
#else
DeviceOptions::DeviceOptions()
#endif
: scheme(constAlbumArtist + QChar('/') + constAlbumTitle + QChar('/') + constTrackNumber + QChar(' ') + constTrackTitle), vfatSafe(true), asciiOnly(false), ignoreThe(false), replaceSpaces(false)
#ifdef ENABLE_DEVICES_SUPPORT
,
fixVariousArtists(false), transcoderValue(0), transcoderWhen(TW_Always), useCache(true), autoScan(false), coverName(cvrName), coverMaxSize(0)
#endif
{
}
static const QLatin1String constMpdGroup("mpd");
static const QLatin1String constSchemeKey("scheme");
static const QLatin1String constVFatKey("vfatSafe");
static const QLatin1String constAsciiKey("asciiOnly");
static const QLatin1String constIgnoreTheKey("ignoreThe");
static const QLatin1String constSpacesKey("replaceSpaces");
#ifdef ENABLE_DEVICES_SUPPORT
static const QLatin1String constTransCodecKey("transcoderCodec");
static const QLatin1String constTransValKey("transcoderValue");
static const QLatin1String constTransIfDiffKey("transcoderWhenDifferent");
static const QLatin1String constTransIfLosslessKey("transcoderWhenSourceIsLosssless");
static const QLatin1String constTransWhenKey("transcoderWhen");
#endif
Q_DECL_UNUSED static const QLatin1String constUseCacheKey("useCache");
Q_DECL_UNUSED static const QLatin1String constFixVaKey("fixVariousArtists");
Q_DECL_UNUSED static const QLatin1String constNameKey("name");
Q_DECL_UNUSED static const QLatin1String constCvrFileKey("coverFileName");
Q_DECL_UNUSED static const QLatin1String constCvrMaxKey("coverMaxSize");
Q_DECL_UNUSED static const QLatin1String constVolKey("volumeId");
bool DeviceOptions::isConfigured(const QString& group, bool isMpd)
{
if (Configuration(group).hasEntry(constSchemeKey)) {
return true;
}
if (isMpd) {
return Configuration(constMpdGroup).hasEntry(constSchemeKey);
}
return false;
}
void DeviceOptions::load(const QString& group, bool isMpd)
{
Configuration cfg(group);
if (isMpd && !cfg.hasEntry(constSchemeKey)) {
// Try old [mpd] group...
Configuration mpdGrp(constMpdGroup);
if (mpdGrp.hasEntry(constSchemeKey)) {
scheme = mpdGrp.get(constSchemeKey, scheme);
vfatSafe = mpdGrp.get(constVFatKey, vfatSafe);
asciiOnly = mpdGrp.get(constAsciiKey, asciiOnly);
ignoreThe = mpdGrp.get(constIgnoreTheKey, ignoreThe);
replaceSpaces = mpdGrp.get(constSpacesKey, replaceSpaces);
#ifdef ENABLE_DEVICES_SUPPORT
transcoderCodec = mpdGrp.get(constTransCodecKey, (isMpd ? "lame" : transcoderCodec));
transcoderValue = mpdGrp.get(constTransValKey, (isMpd ? 2 : transcoderValue));
if (mpdGrp.hasEntry(constTransWhenKey)) {
transcoderWhen = (TranscodeWhen)mpdGrp.get(constTransWhenKey, (int)transcoderWhen);
}
else {
if (mpdGrp.get(constTransIfLosslessKey, false)) {
transcoderWhen = TW_IfLossess;
}
else if (mpdGrp.get(constTransIfDiffKey, false)) {
transcoderWhen = TW_IfDifferent;
}
else {
transcoderWhen = TW_Always;
}
}
#endif
// Save these settings into new group, [mpd] is left for other connections...
save(group, true, true, true);
}
}
else {
scheme = cfg.get(constSchemeKey, scheme);
vfatSafe = cfg.get(constVFatKey, vfatSafe);
asciiOnly = cfg.get(constAsciiKey, asciiOnly);
ignoreThe = cfg.get(constIgnoreTheKey, ignoreThe);
replaceSpaces = cfg.get(constSpacesKey, replaceSpaces);
#ifdef ENABLE_DEVICES_SUPPORT
if (!isMpd) {
useCache = cfg.get(constUseCacheKey, useCache);
fixVariousArtists = cfg.get(constFixVaKey, fixVariousArtists);
name = cfg.get(constNameKey, name);
coverName = cfg.get(constCvrFileKey, coverName);
coverMaxSize = cfg.get(constCvrMaxKey, coverMaxSize);
volumeId = cfg.get(constVolKey, volumeId);
checkCoverSize();
}
transcoderCodec = cfg.get(constTransCodecKey, (isMpd ? "lame" : transcoderCodec));
transcoderValue = cfg.get(constTransValKey, (isMpd ? 2 : transcoderValue));
if (cfg.hasEntry(constTransWhenKey)) {
transcoderWhen = (TranscodeWhen)cfg.get(constTransWhenKey, (int)transcoderWhen);
}
else {
if (cfg.get(constTransIfLosslessKey, false)) {
transcoderWhen = TW_IfLossess;
}
else if (cfg.get(constTransIfDiffKey, false)) {
transcoderWhen = TW_IfDifferent;
}
else {
transcoderWhen = TW_Always;
}
}
#endif
}
}
void DeviceOptions::save(const QString& group, bool isMpd, bool saveTrans, bool saveFileName) const
{
Configuration cfg(group);
if (saveFileName) {
cfg.set(constSchemeKey, scheme);
cfg.set(constVFatKey, vfatSafe);
cfg.set(constAsciiKey, asciiOnly);
cfg.set(constIgnoreTheKey, ignoreThe);
cfg.set(constSpacesKey, replaceSpaces);
}
#ifdef ENABLE_DEVICES_SUPPORT
if (!isMpd) {
cfg.set(constUseCacheKey, useCache);
cfg.set(constFixVaKey, fixVariousArtists);
cfg.set(constNameKey, name);
cfg.set(constCvrFileKey, coverName);
cfg.set(constCvrMaxKey, coverMaxSize);
cfg.set(constVolKey, volumeId);
}
if (saveTrans) {
cfg.set(constTransCodecKey, transcoderCodec);
cfg.set(constTransValKey, transcoderValue);
cfg.set(constTransWhenKey, (int)transcoderWhen);
}
#else
Q_UNUSED(isMpd)
Q_UNUSED(saveTrans)
#endif
}
static QRegularExpression stringRegex = QRegularExpression("\\s");
QString DeviceOptions::clean(const QString& str) const
{
QString result(str);
if (asciiOnly) {
result = cleanPath(result);
result = asciiPath(result);
}
result = result.simplified();
if (replaceSpaces) {
result.replace(stringRegex, "_");
}
if (vfatSafe) {
result = vfatPath(result);
}
result.replace('/', '-');
return result;
}
Song DeviceOptions::clean(const Song& s) const
{
Song copy = s;
if (ignoreThe) {
manipulateThe(copy.albumartist, true);
manipulateThe(copy.artist, true);
}
copy.albumartist = clean(copy.albumartist);
copy.artist = clean(copy.artist);
copy.album = clean(copy.album);
copy.title = clean(copy.title);
for (int i = 0; i < Song::constNumGenres && !s.genres[i].isEmpty(); ++i) {
copy.addGenre(clean(s.genres[i]));
}
return copy;
}
QString DeviceOptions::createFilename(const Song& s) const
{
QString path = scheme;
static QStringList ignore;
if (ignore.isEmpty()) {
ignore << QLatin1String("%comment%")
<< QLatin1String("%filetype%")
<< QLatin1String("%ignore%")
<< QLatin1String("%folder%")
<< QLatin1String("%initial%");
}
for (const QString& i : ignore) {
path.replace(i, QLatin1String(""));
}
if (replaceSpaces) {
path.replace(stringRegex, "_");
}
Song copy = clean(s);
path.replace(constAlbumArtist, copy.albumArtist());
path.replace(constComposer, copy.composer());
path.replace(constAlbumTitle, copy.album);
path.replace(constTrackArtist, copy.artist);
path.replace(constTrackTitle, copy.title);
path.replace(constTrackArtistAndTitle, clean(copy.displayTitle()));
QString track(QString::number(copy.track));
if (copy.track < 10) {
track = QChar('0') + track;
}
path.replace(constTrackNumber, track);
path.replace(constCdNumber, copy.disc < 1 ? QLatin1String("") : QString::number(copy.disc));
path.replace(constGenre, copy.genres[0].isEmpty() ? Song::unknown() : copy.genres[0]);
path.replace(constYear, copy.year < 1 ? QLatin1String("") : QString::number(copy.year));
// For songs about to be downloaded from streams, we hide the filetype in genre...
if (s.file.startsWith("http:/")) {
if (s.genres[0] == QLatin1String("mp3") || s.genres[0] == QLatin1String("ogg") || s.genres[0] == QLatin1String("flac") || s.genres[0] == QLatin1String("wav")) {
path += '.' + s.genres[0];
}
else if (s.genres[0] == QLatin1String("vbr")) {
path += QLatin1String(".mp3");
}
else {
QString f = QUrl(s.file).path();
int extPos = f.lastIndexOf('.');
if (-1 != extPos) {
path += f.mid(extPos);
}
}
}
else {
int extPos = s.file.lastIndexOf('.');
if (-1 != extPos) {
path += s.file.mid(extPos);
}
}
return path;
}
|