chore: switch to use KCodecs

This commit is contained in:
Gary Wang 2024-09-28 12:56:49 +08:00
parent 54c604ee9d
commit c9fc9346b7
No known key found for this signature in database
GPG Key ID: 5D30A4F15EA78760
5 changed files with 43 additions and 27 deletions

View File

@ -33,12 +33,16 @@ jobs:
:: ------ dep ------ :: ------ dep ------
set CMAKE_PREFIX_PATH=%PWD%/dependencies_bin set CMAKE_PREFIX_PATH=%PWD%/dependencies_bin
mkdir dependencies_src mkdir dependencies_src
:: ===== uchardet ===== :: ===== ECM =====
echo ::group::build uchardet git clone -q https://invent.kde.org/frameworks/extra-cmake-modules.git dependencies_src/extra-cmake-modules
git clone -q https://gitlab.freedesktop.org/BLumia/uchardet.git --branch msvc dependencies_src/uchardet cmake .\dependencies_src\extra-cmake-modules -Bbuild_dependencies/extra-cmake-modules -DCMAKE_INSTALL_PREFIX="dependencies_bin" -DBUILD_TESTING=OFF || goto :error
cmake .\dependencies_src\uchardet -Bbuild_dependencies/uchardet -DBUILD_BINARY=OFF -DCMAKE_INSTALL_PREFIX="dependencies_bin" || goto :error cmake --build build_dependencies/extra-cmake-modules --config Release --target=install || goto :error
cmake --build build_dependencies/uchardet --config Release --target=install -j || goto :error :: ===== Gperf (required by KCodecs) =====
echo ::endgroup:: choco install gperf
:: ===== KCodecs =====
git clone -q https://invent.kde.org/frameworks/kcodecs.git dependencies_src/kcodecs
cmake .\dependencies_src\kcodecs -Bbuild_dependencies/kcodecs -DCMAKE_INSTALL_PREFIX="dependencies_bin" -DBUILD_TESTING=OFF || goto :error
cmake --build build_dependencies/kcodecs --config Release --target=install || goto :error
:: ===== pkg-config ===== :: ===== pkg-config =====
choco install pkgconfiglite choco install pkgconfiglite
set PKG_CONFIG_PATH=%PWD%/dependencies_bin/lib/pkgconfig set PKG_CONFIG_PATH=%PWD%/dependencies_bin/lib/pkgconfig

View File

@ -15,7 +15,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 6.6 COMPONENTS Widgets Multimedia Network LinguistTools REQUIRED) find_package(Qt6 6.6 COMPONENTS Widgets Multimedia Network LinguistTools REQUIRED)
find_package(uchardet) find_package(KF6Codecs 6.1.0)
find_package(PkgConfig) find_package(PkgConfig)
if (PKG_CONFIG_FOUND) if (PKG_CONFIG_FOUND)
@ -73,10 +73,10 @@ else ()
target_link_libraries(${EXE_NAME} PRIVATE PkgConfig::TagLib) target_link_libraries(${EXE_NAME} PRIVATE PkgConfig::TagLib)
endif () endif ()
if (NOT uchardet_FOUND) if (NOT TARGET KF6::Codecs)
target_compile_definitions(${EXE_NAME} PRIVATE NO_UCHARDET=1) target_compile_definitions(${EXE_NAME} PRIVATE NO_KCODECS=1)
else () else ()
target_link_libraries (${EXE_NAME} PRIVATE uchardet::libuchardet) target_link_libraries (${EXE_NAME} PRIVATE KF6::Codecs)
endif () endif ()
target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network) target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network)

View File

@ -22,12 +22,20 @@ build_script:
# prepare # prepare
- mkdir 3rdparty - mkdir 3rdparty
- choco install ninja - choco install ninja
- choco install gperf
- choco install pkgconfiglite - choco install pkgconfiglite
- cd 3rdparty - cd 3rdparty
# build uchardet # install ECM
- git clone -q https://gitlab.freedesktop.org/uchardet/uchardet.git - git clone -q https://invent.kde.org/frameworks/extra-cmake-modules.git
- cd uchardet - git rev-parse HEAD
- cmake -G "Ninja" . -DCMAKE_INSTALL_PREFIX=%PACKAGE_INSTALL_ROOT% -DBUILD_BINARY=OFF - cd extra-cmake-modules
- cmake -G "Ninja" . -DCMAKE_INSTALL_PREFIX=%PACKAGE_INSTALL_ROOT% -DBUILD_TESTING=OFF
- cmake --build . --target install
- cd %APPVEYOR_BUILD_FOLDER%
# build kcodecs
- git clone -q https://invent.kde.org/frameworks/kcodecs.git
- cd kcodecs
- cmake -G "Ninja" . -DCMAKE_INSTALL_PREFIX=%PACKAGE_INSTALL_ROOT% -DBUILD_TESTING=OFF
- cmake --build . --target install - cmake --build . --target install
- cd %APPVEYOR_BUILD_FOLDER% - cd %APPVEYOR_BUILD_FOLDER%
# build taglib # build taglib

View File

@ -9,8 +9,10 @@
#include <QRegularExpression> #include <QRegularExpression>
#include <QStringConverter> #include <QStringConverter>
#ifndef NO_UCHARDET #ifndef NO_KCODECS
#include <uchardet/uchardet.h> #include <KCharsets>
#include <KCodecs>
#include <KEncodingProber>
#endif #endif
Q_LOGGING_CATEGORY(lcLyrics, "pmusic.lyrics") Q_LOGGING_CATEGORY(lcLyrics, "pmusic.lyrics")
@ -44,21 +46,23 @@ bool LyricsManager::loadLyrics(QString filepath)
return false; return false;
} }
QByteArray fileContent(file.readAll()); QByteArray fileContent(file.readAll());
#ifndef NO_UCHARDET #ifndef NO_KCODECS
uchardet_t handle = uchardet_new(); KEncodingProber prober(KEncodingProber::Universal);
uchardet_handle_data(handle, fileContent.data(), fileContent.length()); prober.feed(fileContent);
uchardet_data_end(handle); QByteArray encoding(prober.encoding());
const char* encoding = uchardet_get_charset(handle); qCDebug(lcLyrics) << "Detected encoding:" << QString(encoding) << "with confidence" << prober.confidence();
qCDebug(lcLyrics) << "Detected encoding:" << (encoding == NULL ? "unknown" : encoding);
QStringList lines;
if (QStringConverter::availableCodecs().contains(QString(encoding))) {
auto toUtf16 = QStringDecoder(encoding); auto toUtf16 = QStringDecoder(encoding);
QStringList lines;
// Don't use `QStringConverter::availableCodecs().contains(QString(encoding))` here, since the charset
// encoding name might not match, e.g. GB18030 (from availableCodecs) != gb18030 (from KEncodingProber)
if (toUtf16.isValid()) {
QString decodedResult = toUtf16(fileContent); QString decodedResult = toUtf16(fileContent);
lines = decodedResult.split('\n'); lines = decodedResult.split('\n');
} else { } else {
qCDebug(lcLyrics) << "No codec for the detected encoding. Available codecs are:" << QStringConverter::availableCodecs();
qCDebug(lcLyrics) << "KCodecs offers these encodings:" << KCharsets::charsets()->availableEncodingNames();
lines = QString(fileContent).split('\n'); lines = QString(fileContent).split('\n');
} }
uchardet_delete(handle);
#else #else
QStringList lines = QString(fileContent).split('\n'); QStringList lines = QString(fileContent).split('\n');
#endif #endif

View File

@ -620,8 +620,8 @@ void MainWindow::on_actionHelp_triggered()
#ifndef NO_TAGLIB #ifndef NO_TAGLIB
QStringLiteral("- [TagLib](https://github.com/taglib/taglib)\n") % QStringLiteral("- [TagLib](https://github.com/taglib/taglib)\n") %
#endif // NO_TAGLIB #endif // NO_TAGLIB
#ifndef NO_UCHARDET #ifndef NO_KCODECS
QStringLiteral("- [uchardet](https://www.freedesktop.org/wiki/Software/uchardet/)\n") % QStringLiteral("- [KCodecs](https://invent.kde.org/frameworks/kcodecs)\n") %
#endif // NO_TAGLIB #endif // NO_TAGLIB
"\n" "\n"
"[Source Code](https://github.com/BLumia/pineapple-music)\n" "[Source Code](https://github.com/BLumia/pineapple-music)\n"