From 7192c8a1b7e18426464250cf9821e02ecc9ba8e9 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sat, 28 Sep 2024 23:46:00 +0800 Subject: [PATCH] refactor: option to use QTextCodec from Qt5compat --- .github/workflows/windows.yml | 8 ++++---- CMakeLists.txt | 16 +++++++++++++--- lyricsmanager.cpp | 28 ++++++++++++++++++++++------ mainwindow.cpp | 8 ++++++-- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 6dde7ae..76b217e 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -20,7 +20,7 @@ jobs: with: arch: 'win64_msvc2019_64' version: ${{ matrix.qt_ver }} - modules: 'qtmultimedia' + modules: 'qtmultimedia qt5compat' - name: Build shell: cmd run: | @@ -51,11 +51,11 @@ jobs: cmake .\dependencies_src\taglib -Bbuild_dependencies/taglib -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX="dependencies_bin" || goto :error cmake --build build_dependencies/taglib --config Release --target=install -j || goto :error :: ------ app ------ - cmake -Bbuild . -DCMAKE_INSTALL_PREFIX="%PWD%\build\" || goto :error + cmake -Bbuild . -DUSE_QTEXTCODEC=ON -DCMAKE_INSTALL_PREFIX="%PWD%\build\" || goto :error cmake --build build --config Release -j || goto :error - cmake --build build --config Release --target=install + cmake --build build --config Release --target=install || goto :error :: ------ pkg ------ - windeployqt --verbose=2 --no-quick-import --no-translations --no-opengl-sw --no-system-d3d-compiler --no-system-dxc-compiler --multimedia --skip-plugin-types tls,networkinformation build\bin\pmusic.exe + windeployqt --verbose=2 --no-quick-import --no-translations --no-opengl-sw --no-system-d3d-compiler --no-system-dxc-compiler --multimedia --core5compat --skip-plugin-types tls,networkinformation build\bin\pmusic.exe robocopy ./dependencies_bin/bin build/bin *.dll if ErrorLevel 8 (exit /B 1) copy LICENSE build/bin/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 87409cc..7e1bcd9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,10 +14,16 @@ set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) +option(USE_QTEXTCODEC "Use QTextCodec instead of QStringConverter, in case Qt is not built with ICU" OFF) + find_package(Qt6 6.6 COMPONENTS Widgets Multimedia Network LinguistTools REQUIRED) find_package(KF6Codecs 6.1.0) find_package(PkgConfig) +if (USE_QTEXTCODEC) + find_package(Qt6 6.6 COMPONENTS Core5Compat REQUIRED) +endif() + if (PKG_CONFIG_FOUND) pkg_check_modules(TagLib taglib IMPORTED_TARGET) endif () @@ -73,14 +79,18 @@ else () target_link_libraries(${EXE_NAME} PRIVATE PkgConfig::TagLib) endif () -if (NOT TARGET KF6::Codecs) - target_compile_definitions(${EXE_NAME} PRIVATE NO_KCODECS=1) -else () +if (TARGET KF6::Codecs) + target_compile_definitions(${EXE_NAME} PRIVATE HAVE_KCODECS=1) target_link_libraries (${EXE_NAME} PRIVATE KF6::Codecs) endif () target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network) +if (USE_QTEXTCODEC) + target_compile_definitions(${EXE_NAME} PRIVATE USE_QTEXTCODEC=1) + target_link_libraries(${EXE_NAME} PRIVATE Qt6::Core5Compat) +endif() + # Install settings if (WIN32) # FIXME: try to avoid install to a "bin" subfolder under windows... diff --git a/lyricsmanager.cpp b/lyricsmanager.cpp index 31cf928..fd02a6b 100644 --- a/lyricsmanager.cpp +++ b/lyricsmanager.cpp @@ -9,12 +9,16 @@ #include #include -#ifndef NO_KCODECS +#ifdef HAVE_KCODECS #include #include #include #endif +#ifdef USE_QTEXTCODEC +#include +#endif + Q_LOGGING_CATEGORY(lcLyrics, "pmusic.lyrics") Q_LOGGING_CATEGORY(lcLyricsParser, "pmusic.lyrics.parser") @@ -46,13 +50,24 @@ bool LyricsManager::loadLyrics(QString filepath) return false; } QByteArray fileContent(file.readAll()); -#ifndef NO_KCODECS + QStringList lines; +#ifdef HAVE_KCODECS KEncodingProber prober(KEncodingProber::Universal); prober.feed(fileContent); QByteArray encoding(prober.encoding()); qCDebug(lcLyrics) << "Detected encoding:" << QString(encoding) << "with confidence" << prober.confidence(); +#ifdef USE_QTEXTCODEC + qCDebug(lcLyrics) << "QTextCodec is used instead of QStringConverter."; + QTextCodec *codec = QTextCodec::codecForName(encoding); + if (codec) { + lines = codec->toUnicode(fileContent).split('\n'); + } else { + lines = QString(fileContent).split('\n'); + qCDebug(lcLyrics) << "No codec for the detected encoding. Available codecs are:" << QTextCodec::availableCodecs(); + qCDebug(lcLyrics) << "KCodecs offers these encodings:" << KCharsets::charsets()->availableEncodingNames(); + } +#else // NOT USE_QTEXTCODEC 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()) { @@ -63,9 +78,10 @@ bool LyricsManager::loadLyrics(QString filepath) qCDebug(lcLyrics) << "KCodecs offers these encodings:" << KCharsets::charsets()->availableEncodingNames(); lines = QString(fileContent).split('\n'); } -#else - QStringList lines = QString(fileContent).split('\n'); -#endif +#endif // USE_QTEXTCODEC +#else // NOT HAVE_KCODECS + lines = QString(fileContent).split('\n'); +#endif // HAVE_KCODECS file.close(); // parse lyrics timestamp diff --git a/mainwindow.cpp b/mainwindow.cpp index 6f7cb04..6d1b7ae 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -616,11 +616,15 @@ void MainWindow::on_actionHelp_triggered() "\n\n" % tr("Based on the following free software libraries:") % "\n\n" % - QStringLiteral("- [Qt](https://www.qt.io/) %1\n").arg(QT_VERSION_STR) % + QStringLiteral("- [Qt](https://www.qt.io/) %1 with the following module(s):\n").arg(QT_VERSION_STR) % + QStringLiteral(" - multimedia\n") % +#ifdef USE_QTEXTCODEC + QStringLiteral(" - core5compat\n") % +#endif #ifndef NO_TAGLIB QStringLiteral("- [TagLib](https://github.com/taglib/taglib)\n") % #endif // NO_TAGLIB -#ifndef NO_KCODECS +#ifdef HAVE_KCODECS QStringLiteral("- [KCodecs](https://invent.kde.org/frameworks/kcodecs)\n") % #endif // NO_TAGLIB "\n"