CueParser: Detect and handle different text encodings

Fixes #1429
This commit is contained in:
Jonas Kvinge 2024-05-19 01:49:37 +02:00
parent 9e25366f85
commit 9f2e4ac312
2 changed files with 22 additions and 3 deletions

View File

@ -2,7 +2,7 @@
* Strawberry Music Player * Strawberry Music Player
* This file was part of Clementine. * This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com> * Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net> * Copyright 2019-2024, Jonas Kvinge <jonas@jkvinge.net>
* *
* Strawberry is free software: you can redistribute it and/or modify * Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -32,11 +32,14 @@
#include <QTextStream> #include <QTextStream>
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
# include <QStringConverter> # include <QStringConverter>
#else
# include <QTextCodec>
#endif #endif
#include "core/shared_ptr.h" #include "core/shared_ptr.h"
#include "core/logging.h" #include "core/logging.h"
#include "utilities/timeconstants.h" #include "utilities/timeconstants.h"
#include "utilities/textencodingutils.h"
#include "settings/playlistsettingspage.h" #include "settings/playlistsettingspage.h"
#include "parserbase.h" #include "parserbase.h"
#include "cueparser.h" #include "cueparser.h"
@ -71,11 +74,27 @@ SongList CueParser::Load(QIODevice *device, const QString &playlist_path, const
QTextStream text_stream(device); QTextStream text_stream(device);
const QByteArray data_chunk = device->peek(1024);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
const std::optional<QStringConverter::Encoding> encoding = QStringConverter::encodingForData(device->peek(1024)); std::optional<QStringConverter::Encoding> encoding = QStringConverter::encodingForData(data_chunk);
if (encoding.has_value()) { if (encoding.has_value()) {
text_stream.setEncoding(encoding.value()); text_stream.setEncoding(encoding.value());
} }
else {
const QByteArray encoding_name = Utilities::TextEncodingFromData(data_chunk);
if (!encoding_name.isEmpty()) {
encoding = QStringConverter::encodingForName(encoding_name.constData());
if (encoding.has_value()) {
text_stream.setEncoding(encoding.value());
}
}
}
#else
const QByteArray encoding_name = Utilities::TextEncodingFromData(data_chunk);
if (!encoding_name.isEmpty()) {
text_stream.setCodec(encoding_name.constData());
}
#endif #endif
QString dir_path = dir.absolutePath(); QString dir_path = dir.absolutePath();

View File

@ -2,7 +2,7 @@
* Strawberry Music Player * Strawberry Music Player
* This file was part of Clementine. * This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com> * Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net> * Copyright 2019-2024, Jonas Kvinge <jonas@jkvinge.net>
* *
* Strawberry is free software: you can redistribute it and/or modify * Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by